1 #ifndef DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H
5 * Copyright (c) 2019 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>
36 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
49 class ImageVisualShaderFactory;
51 typedef IntrusivePtr< ImageVisual > ImageVisualPtr;
54 * The visual which renders an image to a quad geometry
56 * The following properties are optional
58 * | %Property Name | Type |
59 * |-----------------------|-------------------|
61 * | alphaMaskUrl | STRING |
62 * | fittingMode | INTEGER OR STRING |
63 * | samplingMode | INTEGER OR STRING |
64 * | desiredWidth | INTEGER |
65 * | desiredHeight | INTEGER |
66 * | synchronousLoading | BOOLEAN |
67 * | pixelArea | VECTOR4 |
68 * | wrapModeU | INTEGER OR STRING |
69 * | wrapModeV | INTEGER OR STRING |
70 * | loadPolicy | INTEGER OR STRING |
71 * | releasePolicy | INTEGER OR STRING |
72 * | orientationCorrection | BOOLEAN |
74 * where pixelArea is a rectangular area.
75 * In its Vector4 value, the first two elements indicate the top-left position of the area,
76 * and the last two elements are the area width and height respectively.
77 * If not specified, the default value is [0.0, 0.0, 1.0, 1.0], i.e. the entire area of the image.
79 * 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.
80 * Its value should be one of the following wrap mode:
86 * where imageFittingMode should be one of the following fitting modes:
93 * where imageSamplingMode should be one of the following sampling modes:
103 * where loadPolicy should be one of the following image loading modes
104 * "IMMEDIATE" // Loads image even if visual not attached to stage yet
105 * "ATTACHED" // Only loads image once visual is attached to stage
107 * where releasePolicy should be one of the following policies for when to cache the image
108 * "DETACHED" // Release image from cache when visual detached from stage
109 * "DESTROYED" // Keep image in cache until the visual is destroyed
110 * "NEVER" // Keep image in cache until application ends.
112 * If the Visual is in a LayerUI it will pixel align the image, using a Layer3D will disable pixel alignment.
113 * Changing layer behaviour between LayerUI to Layer3D whilst the visual is already staged will not have an effect.
115 class ImageVisual: public Visual::Base, public ConnectionTracker, public AtlasUploadObserver, public TextureUploadObserver
120 * @brief Create a new image visual with a URL.
122 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
124 * @param[in] factoryCache The VisualFactoryCache object
125 * @param[in] shaderFactory The ImageVisualShaderFactory object
126 * @param[in] imageUrl The URL of the image resource to use
127 * @param[in] properties A Property::Map containing settings for this visual
128 * @param[in] size The width and height to fit the loaded image to.
129 * @param[in] fittingMode The FittingMode of the resource to load
130 * @param[in] samplingMode The SamplingMode of the resource to load
131 * @return A smart-pointer to the newly allocated visual.
133 static ImageVisualPtr New( VisualFactoryCache& factoryCache,
134 ImageVisualShaderFactory& shaderFactory,
135 const VisualUrl& imageUrl,
136 const Property::Map& properties,
137 ImageDimensions size = ImageDimensions(),
138 FittingMode::Type fittingMode = FittingMode::DEFAULT,
139 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
142 * @brief Create a new image visual with a URL.
144 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
146 * @param[in] factoryCache The VisualFactoryCache object
147 * @param[in] shaderFactory The ImageVisualShaderFactory object
148 * @param[in] imageUrl The URL of the image resource to use
149 * @param[in] size The width and height to fit the loaded image to.
150 * @param[in] fittingMode The FittingMode of the resource to load
151 * @param[in] samplingMode The SamplingMode of the resource to load
152 * @return A smart-pointer to the newly allocated visual.
154 static ImageVisualPtr New( VisualFactoryCache& factoryCache,
155 ImageVisualShaderFactory& shaderFactory,
156 const VisualUrl& imageUrl,
157 ImageDimensions size = ImageDimensions(),
158 FittingMode::Type fittingMode = FittingMode::DEFAULT,
159 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
162 * @brief Create a new image visual with an Image type.
164 * @param[in] factoryCache The VisualFactoryCache object
165 * @param[in] shaderFactory The ImageVisualShaderFactory object
166 * @param[in] image The image to use
168 static ImageVisualPtr New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const Image& image );
170 public: // from Visual
173 * @copydoc Visual::Base::GetNaturalSize
175 void GetNaturalSize( Vector2& naturalSize ) override;
178 * @copydoc Visual::Base::CreatePropertyMap
180 void DoCreatePropertyMap( Property::Map& map ) const override;
183 * @copydoc Visual::Base::CreateInstancePropertyMap
185 void DoCreateInstancePropertyMap( Property::Map& map ) const override;
188 * @copydoc Visual::Base::OnDoAction
190 void OnDoAction( const Dali::Property::Index actionName, const Dali::Property::Value& attributes ) override;
195 * @brief Constructor with a URL.
197 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
199 * @param[in] factoryCache The VisualFactoryCache object
200 * @param[in] shaderFactory The ImageVisualShaderFactory object
201 * @param[in] imageUrl The URL of the image resource to use
202 * @param[in] size The width and height to fit the loaded image to.
203 * @param[in] fittingMode The FittingMode of the resource to load
204 * @param[in] samplingMode The SamplingMode of the resource to load
206 ImageVisual( VisualFactoryCache& factoryCache,
207 ImageVisualShaderFactory& shaderFactory,
208 const VisualUrl& imageUrl,
209 ImageDimensions size,
210 FittingMode::Type fittingMode,
211 Dali::SamplingMode::Type samplingMode );
214 * @brief Constructor with an Image type.
216 * @param[in] factoryCache The VisualFactoryCache object
217 * @param[in] shaderFactory The ImageVisualShaderFactory object
218 * @param[in] image The image to use
220 ImageVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const Image& image );
223 * @brief A reference counted object may only be deleted by calling Unreference().
225 virtual ~ImageVisual();
228 * @copydoc Visual::Base::DoSetProperties
230 void DoSetProperties( const Property::Map& propertyMap ) override;
233 * @copydoc Visual::Base::DoSetOnStage
235 void DoSetOnStage( Actor& actor ) override;
238 * @copydoc Visual::Base::DoSetOffStage
240 void DoSetOffStage( Actor& actor ) override;
243 * @copydoc Visual::Base::OnSetTransform
245 void OnSetTransform() override;
248 * @copydoc Visual::Base::IsResourceReady
250 bool IsResourceReady() const override;
255 * @copydoc AtlasUploadObserver::UploadCompleted
257 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
258 * This callback is the place to add the renderer as it would be called once the loading is finished.
260 void UploadCompleted() override;
263 * @copydoc TextureUploadObserver::UploadCompleted
265 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
266 * This callback is the place to add the renderer as it would be called once the loading is finished.
268 void UploadComplete( bool success, int32_t textureId, TextureSet textureSet,
269 bool usingAtlas, const Vector4& atlasRectangle, bool preMultiplied ) override;
274 * Allocate the mask data when a masking property is defined in the property map
276 void AllocateMaskData();
279 * @brief Applies the image to the texture set used for this renderer
281 * @param[in] image The Image to apply to the texture set used for this renderer
283 void ApplyImageToSampler( const Image& image );
286 * @brief Load the texture, will try to atlas unless unable or param set to false.
287 * @param[in, out] atlasing flag if the image has been put in a atlas (true), passing false will not atlas even if possible.
288 * @param[out] atlasRect if atlasing is used this the texture area of the image in the atlas.
289 * @param[out] textures resulting texture set from the image loading.
290 * @param[in] orientationCorrection flag determines if orientation correction should be performed
291 * @param[in] forceReload flag determines if the texture should be reloaded from its source or use the cached texture.
293 void LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection, TextureManager::ReloadPolicy forceReload );
296 * @brief Checks if atlasing should be attempted
297 * @return bool returns true if atlasing can be attempted.
299 bool AttemptAtlasing();
302 * @brief Initializes the Dali::Renderer from the image url
304 void InitializeRenderer();
307 * @brief Initializes the Dali::Renderer from an image handle
309 * @param[in] image The image handle to intialize this ImageVisual from
311 void InitializeRenderer( const Image& image );
314 * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
315 * @param[in] textures to use
317 void CreateRenderer( TextureSet& textures );
320 * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
321 * @param NativeImageRenderer
323 void CreateNativeImageRenderer( NativeImage& nativeImage );
326 * @brief Query whether resources requires to be loaded synchronously.
327 * @return Returns true if synchronous resource loading is required, false otherwise.
329 bool IsSynchronousResourceLoading() const;
332 * Creates the texture set and adds the texture to it
333 * @param[out] textureRect The texture area of the texture in the atlas.
334 * @param[in] url The URL of the image resource to use.
335 * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
336 * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
337 * @return the texture set to use
339 TextureSet CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing );
342 * Set the value to the uTextureRect uniform
343 * @param[in] textureRect The texture rectangular area.
345 void SetTextureRectUniform( const Vector4& textureRect );
348 * Remove texture with valid TextureId
350 void RemoveTexture();
353 * Helper method to set individual values by index key.
354 * @param[in] index The index key of the value
355 * @param[in] value The value
357 void DoSetProperty( Property::Index index, const Property::Value& value );
363 WeakHandle<Actor> mPlacementActor;
365 TextureManager::MaskingDataPointer mMaskingData;
367 Dali::ImageDimensions mDesiredSize;
368 TextureManager::TextureId mTextureId;
369 TextureSet mTextures;
371 ImageVisualShaderFactory& mImageVisualShaderFactory;
373 Dali::FittingMode::Type mFittingMode:3;
374 Dali::SamplingMode::Type mSamplingMode:4;
375 Dali::WrapMode::Type mWrapModeU:3;
376 Dali::WrapMode::Type mWrapModeV:3;
377 Dali::Toolkit::ImageVisual::LoadPolicy::Type mLoadPolicy;
378 Dali::Toolkit::ImageVisual::ReleasePolicy::Type mReleasePolicy;
380 Dali::ImageDimensions mAtlasRectSize;
381 bool mAttemptAtlasing; ///< If true will attempt atlasing, otherwise create unique texture
382 bool mLoading; ///< True if the texture is still loading.
383 bool mOrientationCorrection; ///< true if the image will have it's orientation corrected.
388 } // namespace Internal
390 } // namespace Toolkit
394 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */