1 #ifndef DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H
5 * Copyright (c) 2020 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 );
161 public: // from Visual
164 * @copydoc Visual::Base::GetNaturalSize
166 void GetNaturalSize( Vector2& naturalSize ) override;
169 * @copydoc Visual::Base::CreatePropertyMap
171 void DoCreatePropertyMap( Property::Map& map ) const override;
174 * @copydoc Visual::Base::CreateInstancePropertyMap
176 void DoCreateInstancePropertyMap( Property::Map& map ) const override;
179 * @copydoc Visual::Base::OnDoAction
181 void OnDoAction( const Dali::Property::Index actionName, const Dali::Property::Value& attributes ) override;
186 * @brief Constructor with a URL.
188 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
190 * @param[in] factoryCache The VisualFactoryCache object
191 * @param[in] shaderFactory The ImageVisualShaderFactory object
192 * @param[in] imageUrl The URL of the image resource to use
193 * @param[in] size The width and height to fit the loaded image to.
194 * @param[in] fittingMode The FittingMode of the resource to load
195 * @param[in] samplingMode The SamplingMode of the resource to load
197 ImageVisual( VisualFactoryCache& factoryCache,
198 ImageVisualShaderFactory& shaderFactory,
199 const VisualUrl& imageUrl,
200 ImageDimensions size,
201 FittingMode::Type fittingMode,
202 Dali::SamplingMode::Type samplingMode );
205 * @brief A reference counted object may only be deleted by calling Unreference().
207 virtual ~ImageVisual();
210 * @copydoc Visual::Base::DoSetProperties
212 void DoSetProperties( const Property::Map& propertyMap ) override;
215 * @copydoc Visual::Base::DoSetOnStage
217 void DoSetOnStage( Actor& actor ) override;
220 * @copydoc Visual::Base::DoSetOffStage
222 void DoSetOffStage( Actor& actor ) override;
225 * @copydoc Visual::Base::OnSetTransform
227 void OnSetTransform() override;
230 * @copydoc Visual::Base::IsResourceReady
232 bool IsResourceReady() const override;
237 * @copydoc AtlasUploadObserver::UploadCompleted
239 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
240 * This callback is the place to add the renderer as it would be called once the loading is finished.
242 void UploadCompleted() override;
245 * @copydoc TextureUploadObserver::UploadCompleted
247 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
248 * This callback is the place to add the renderer as it would be called once the loading is finished.
250 void UploadComplete( bool success, int32_t textureId, TextureSet textureSet,
251 bool usingAtlas, const Vector4& atlasRectangle, bool preMultiplied ) override;
256 * @copydoc TextureUploadObserver::LoadComplete
258 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
259 * This callback is the place to add the renderer as it would be called once the PixelBuffer loading is finished.
261 void LoadComplete( bool loadSuccess, Devel::PixelBuffer pixelBuffer, const VisualUrl& url, bool preMultiplied ) override {}
264 * Allocate the mask data when a masking property is defined in the property map
266 void AllocateMaskData();
269 * @brief Load the texture, will try to atlas unless unable or param set to false.
270 * @param[in, out] atlasing flag if the image has been put in a atlas (true), passing false will not atlas even if possible.
271 * @param[out] atlasRect if atlasing is used this the texture area of the image in the atlas.
272 * @param[out] textures resulting texture set from the image loading.
273 * @param[in] orientationCorrection flag determines if orientation correction should be performed
274 * @param[in] forceReload flag determines if the texture should be reloaded from its source or use the cached texture.
276 void LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection, TextureManager::ReloadPolicy forceReload );
279 * @brief Checks if atlasing should be attempted
280 * @return bool returns true if atlasing can be attempted.
282 bool AttemptAtlasing();
285 * @brief Initializes the Dali::Renderer from the image url
287 void InitializeRenderer();
290 * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
291 * @param[in] textures to use
293 void CreateRenderer( TextureSet& textures );
296 * Creates the texture set and adds the texture to it
297 * @param[out] textureRect The texture area of the texture in the atlas.
298 * @param[in] url The URL of the image resource to use.
299 * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
300 * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
301 * @return the texture set to use
303 TextureSet CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing );
306 * Set the value to the uTextureRect uniform
307 * @param[in] textureRect The texture rectangular area.
309 void SetTextureRectUniform( const Vector4& textureRect );
312 * Remove texture with valid TextureId
314 void RemoveTexture();
317 * Helper method to set individual values by index key.
318 * @param[in] index The index key of the value
319 * @param[in] value The value
321 void DoSetProperty( Property::Index index, const Property::Value& value );
326 WeakHandle<Actor> mPlacementActor;
328 TextureManager::MaskingDataPointer mMaskingData;
330 Dali::ImageDimensions mDesiredSize;
331 TextureManager::TextureId mTextureId;
332 TextureSet mTextures;
334 ImageVisualShaderFactory& mImageVisualShaderFactory;
336 Dali::FittingMode::Type mFittingMode:3;
337 Dali::SamplingMode::Type mSamplingMode:4;
338 Dali::WrapMode::Type mWrapModeU:3;
339 Dali::WrapMode::Type mWrapModeV:3;
340 Dali::Toolkit::ImageVisual::LoadPolicy::Type mLoadPolicy;
341 Dali::Toolkit::ImageVisual::ReleasePolicy::Type mReleasePolicy;
343 Dali::ImageDimensions mAtlasRectSize;
344 bool mAttemptAtlasing; ///< If true will attempt atlasing, otherwise create unique texture
345 bool mLoading; ///< True if the texture is still loading.
346 bool mOrientationCorrection; ///< true if the image will have it's orientation corrected.
351 } // namespace Internal
353 } // namespace Toolkit
357 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */