1 #ifndef DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H
5 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
24 #include <dali/public-api/common/intrusive-ptr.h>
25 #include <dali/public-api/images/image.h>
26 #include <dali/public-api/images/image-operations.h>
27 #include <dali/public-api/images/resource-image.h>
28 #include <dali/public-api/object/weak-handle.h>
31 #include <dali-toolkit/devel-api/image-loader/atlas-upload-observer.h>
32 #include <dali-toolkit/internal/visuals/texture-upload-observer.h>
33 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
34 #include <dali-toolkit/internal/visuals/visual-url.h>
35 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
49 typedef IntrusivePtr< ImageVisual > ImageVisualPtr;
52 * The visual which renders an image to a quad geometry
54 * The following properties are optional
56 * | %Property Name | Type |
57 * |-----------------------|-------------------|
59 * | alphaMaskUrl | STRING |
60 * | fittingMode | INTEGER OR STRING |
61 * | samplingMode | INTEGER OR STRING |
62 * | desiredWidth | INTEGER |
63 * | desiredHeight | INTEGER |
64 * | synchronousLoading | BOOLEAN |
65 * | pixelArea | VECTOR4 |
66 * | wrapModeU | INTEGER OR STRING |
67 * | wrapModeV | INTEGER OR STRING |
68 * | loadPolicy | INTEGER OR STRING |
69 * | releasePolicy | INTEGER OR STRING |
70 * | orientationCorrection | BOOLEAN |
72 * where pixelArea is a rectangular area.
73 * In its Vector4 value, the first two elements indicate the top-left position of the area,
74 * and the last two elements are the area width and height respectively.
75 * If not specified, the default value is [0.0, 0.0, 1.0, 1.0], i.e. the entire area of the image.
77 * where wrapModeU and wrapModeV separately decide how the texture should be sampled when the u and v coordinate exceeds the range of 0.0 to 1.0.
78 * Its value should be one of the following wrap mode:
84 * where imageFittingMode should be one of the following fitting modes:
91 * where imageSamplingMode should be one of the following sampling modes:
101 * where loadPolicy should be one of the following image loading modes
102 * "IMMEDIATE" // Loads image even if visual not attached to stage yet
103 * "ATTACHED" // Only loads image once visual is attached to stage
105 * where releasePolicy should be one of the following policies for when to cache the image
106 * "DETACHED" // Release image from cache when visual detached from stage
107 * "DESTROYED" // Keep image in cache until the visual is destroyed
108 * "NEVER" // Keep image in cache until application ends.
110 * If the Visual is in a LayerUI it will pixel align the image, using a Layer3D will disable pixel alignment.
111 * Changing layer behaviour between LayerUI to Layer3D whilst the visual is already staged will not have an effect.
113 class ImageVisual: public Visual::Base, public ConnectionTracker, public AtlasUploadObserver, public TextureUploadObserver
118 * @brief Create a new image visual with a URL.
120 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
122 * @param[in] factoryCache The VisualFactoryCache object
123 * @param[in] imageUrl The URL of the image resource to use
124 * @param[in] properties A Property::Map containing settings for this visual
125 * @param[in] size The width and height to fit the loaded image to.
126 * @param[in] fittingMode The FittingMode of the resource to load
127 * @param[in] samplingMode The SamplingMode of the resource to load
128 * @return A smart-pointer to the newly allocated visual.
130 static ImageVisualPtr New( VisualFactoryCache& factoryCache,
131 const VisualUrl& imageUrl,
132 const Property::Map& properties,
133 ImageDimensions size = ImageDimensions(),
134 FittingMode::Type fittingMode = FittingMode::DEFAULT,
135 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
138 * @brief Create a new image visual with a URL.
140 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
142 * @param[in] factoryCache The VisualFactoryCache object
143 * @param[in] imageUrl The URL of the image resource to use
144 * @param[in] size The width and height to fit the loaded image to.
145 * @param[in] fittingMode The FittingMode of the resource to load
146 * @param[in] samplingMode The SamplingMode of the resource to load
147 * @return A smart-pointer to the newly allocated visual.
149 static ImageVisualPtr New( VisualFactoryCache& factoryCache,
150 const VisualUrl& imageUrl,
151 ImageDimensions size = ImageDimensions(),
152 FittingMode::Type fittingMode = FittingMode::DEFAULT,
153 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
156 * @brief Create a new image visual with an Image type.
158 * @param[in] factoryCache The VisualFactoryCache object
159 * @param[in] image The image to use
161 static ImageVisualPtr New( VisualFactoryCache& factoryCache, const Image& image );
163 public: // from Visual
166 * @copydoc Visual::Base::GetNaturalSize
168 virtual void GetNaturalSize( Vector2& naturalSize );
171 * @copydoc Visual::Base::CreatePropertyMap
173 virtual void DoCreatePropertyMap( Property::Map& map ) const;
176 * @copydoc Visual::Base::CreateInstancePropertyMap
178 virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
181 * @copydoc Visual::Base::OnDoAction
183 virtual void OnDoAction( const Dali::Property::Index actionName, const Dali::Property::Value& attributes ) override;
188 * @brief Constructor with a URL.
190 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
192 * @param[in] factoryCache The VisualFactoryCache object
193 * @param[in] imageUrl The URL of the image resource to use
194 * @param[in] size The width and height to fit the loaded image to.
195 * @param[in] fittingMode The FittingMode of the resource to load
196 * @param[in] samplingMode The SamplingMode of the resource to load
198 ImageVisual( VisualFactoryCache& factoryCache,
199 const VisualUrl& imageUrl,
200 ImageDimensions size,
201 FittingMode::Type fittingMode,
202 Dali::SamplingMode::Type samplingMode );
205 * @brief Constructor with an Image type.
207 * @param[in] factoryCache The VisualFactoryCache object
208 * @param[in] image The image to use
210 ImageVisual( VisualFactoryCache& factoryCache, const Image& image );
213 * @brief A reference counted object may only be deleted by calling Unreference().
215 virtual ~ImageVisual();
218 * @copydoc Visual::Base::DoSetProperties
220 virtual void DoSetProperties( const Property::Map& propertyMap );
223 * @copydoc Visual::Base::DoSetOnStage
225 virtual void DoSetOnStage( Actor& actor );
228 * @copydoc Visual::Base::DoSetOffStage
230 virtual void DoSetOffStage( Actor& actor ) ;
233 * @copydoc Visual::Base::OnSetTransform
235 virtual void OnSetTransform() ;
238 * @copydoc Visual::Base::IsResourceReady
240 virtual bool IsResourceReady() const;
245 * Get the standard image rendering shader.
246 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
247 * @param[in] atlasing Whether texture atlasing is applied.
248 * @param[in] defaultTextureWrapping Whether the default texture wrap mode is applied.
250 static Shader GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping );
253 * @copydoc AtlasUploadObserver::UploadCompleted
255 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
256 * This callback is the place to add the renderer as it would be called once the loading is finished.
258 virtual void UploadCompleted();
261 * @copydoc TextureUploadObserver::UploadCompleted
263 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
264 * This callback is the place to add the renderer as it would be called once the loading is finished.
266 virtual void UploadComplete( bool success, int32_t textureId, TextureSet textureSet, bool usingAtlas, const Vector4& atlasRectangle );
271 * Allocate the mask data when a masking property is defined in the property map
273 void AllocateMaskData();
276 * @brief Applies the image to the texture set used for this renderer
278 * @param[in] image The Image to apply to the texture set used for this renderer
280 void ApplyImageToSampler( const Image& image );
283 * @brief Load the texture, will try to atlas unless unable or param set to false.
284 * @param[in, out] atlasing flag if the image has been put in a atlas (true), passing false will not atlas even if possible.
285 * @param[out] atlasRect if atlasing is used this the texture area of the image in the atlas.
286 * @param[out] textures resulting texture set from the image loading.
287 * @param[in] orientationCorrection flag determines if orientation correction should be performed
288 * @param[in] forceReload flag determines if the texture should be reloaded from its source or use the cached texture.
290 void LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection, TextureManager::ReloadPolicy forceReload );
293 * @brief Checks if atlasing should be attempted
294 * @return bool returns true if atlasing can be attempted.
296 bool AttemptAtlasing();
299 * @brief Initializes the Dali::Renderer from the image url
301 void InitializeRenderer();
304 * @brief Initializes the Dali::Renderer from an image handle
306 * @param[in] image The image handle to intialize this ImageVisual from
308 void InitializeRenderer( const Image& image );
311 * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
312 * @param[in] textures to use
314 void CreateRenderer( TextureSet& textures );
317 * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
318 * @param NativeImageRenderer
320 void CreateNativeImageRenderer( NativeImage& nativeImage );
323 * @brief Query whether resources requires to be loaded synchronously.
324 * @return Returns true if synchronous resource loading is required, false otherwise.
326 bool IsSynchronousResourceLoading() const;
329 * Creates the texture set and adds the texture to it
330 * @param[out] textureRect The texture area of the texture in the atlas.
331 * @param[in] url The URL of the image resource to use.
332 * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
333 * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
334 * @return the texture set to use
336 TextureSet CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing );
339 * Set the value to the uTextureRect uniform
340 * @param[in] textureRect The texture rectangular area.
342 void SetTextureRectUniform( const Vector4& textureRect );
345 * Remove texture with valid TextureId
347 void RemoveTexture();
350 * Helper method to set individual values by index key.
351 * @param[in] index The index key of the value
352 * @param[in] value The value
354 void DoSetProperty( Property::Index index, const Property::Value& value );
360 WeakHandle<Actor> mPlacementActor;
362 TextureManager::MaskingDataPointer mMaskingData;
364 Dali::ImageDimensions mDesiredSize;
365 TextureManager::TextureId mTextureId;
366 TextureSet mTextures;
368 Dali::FittingMode::Type mFittingMode:3;
369 Dali::SamplingMode::Type mSamplingMode:4;
370 Dali::WrapMode::Type mWrapModeU:3;
371 Dali::WrapMode::Type mWrapModeV:3;
372 DevelImageVisual::LoadPolicy::Type mLoadPolicy;
373 DevelImageVisual::ReleasePolicy::Type mReleasePolicy;
375 bool mAttemptAtlasing; ///< If true will attempt atlasing, otherwise create unique texture
376 bool mLoading; ///< True if the texture is still loading.
377 bool mOrientationCorrection; ///< true if the image will have it's orientation corrected.
382 } // namespace Internal
384 } // namespace Toolkit
388 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */