1 #ifndef DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H
5 * Copyright (c) 2022 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-operations.h>
26 #include <dali/public-api/object/weak-handle.h>
29 #include <dali-toolkit/devel-api/image-loader/atlas-upload-observer.h>
30 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
31 #include <dali-toolkit/internal/texture-manager/texture-upload-observer.h>
32 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
33 #include <dali-toolkit/internal/visuals/visual-url.h>
34 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
44 class ImageVisualShaderFactory;
46 typedef IntrusivePtr<ImageVisual> ImageVisualPtr;
49 * The visual which renders an image to a quad geometry
51 * The following properties are optional
53 * | %Property Name | Type |
54 * |-----------------------|-------------------|
56 * | alphaMaskUrl | STRING |
57 * | fittingMode | INTEGER OR STRING |
58 * | samplingMode | INTEGER OR STRING |
59 * | desiredWidth | INTEGER |
60 * | desiredHeight | INTEGER |
61 * | synchronousLoading | BOOLEAN |
62 * | pixelArea | VECTOR4 |
63 * | wrapModeU | INTEGER OR STRING |
64 * | wrapModeV | INTEGER OR STRING |
65 * | loadPolicy | INTEGER OR STRING |
66 * | releasePolicy | INTEGER OR STRING |
67 * | orientationCorrection | BOOLEAN |
69 * where pixelArea is a rectangular area.
70 * In its Vector4 value, the first two elements indicate the top-left position of the area,
71 * and the last two elements are the area width and height respectively.
72 * If not specified, the default value is [0.0, 0.0, 1.0, 1.0], i.e. the entire area of the image.
74 * 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.
75 * Its value should be one of the following wrap mode:
81 * where imageFittingMode should be one of the following fitting modes:
88 * where imageSamplingMode should be one of the following sampling modes:
98 * where loadPolicy should be one of the following image loading modes
99 * "IMMEDIATE" // Loads image even if visual not attached to stage yet
100 * "ATTACHED" // Only loads image once visual is attached to stage
102 * where releasePolicy should be one of the following policies for when to cache the image
103 * "DETACHED" // Release image from cache when visual detached from stage
104 * "DESTROYED" // Keep image in cache until the visual is destroyed
105 * "NEVER" // Keep image in cache until application ends.
107 * If the Visual is in a LayerUI it will pixel align the image, using a Layer3D will disable pixel alignment.
108 * Changing layer behaviour between LayerUI to Layer3D whilst the visual is already staged will not have an effect.
110 class ImageVisual : public Visual::Base, public ConnectionTracker, public AtlasUploadObserver, public TextureUploadObserver
114 * @brief Create a new image visual with a URL.
116 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
118 * @param[in] factoryCache The VisualFactoryCache object
119 * @param[in] shaderFactory The ImageVisualShaderFactory object
120 * @param[in] imageUrl The URL of the image resource to use
121 * @param[in] properties A Property::Map containing settings for this visual
122 * @param[in] size The width and height to fit the loaded image to.
123 * @param[in] fittingMode The FittingMode of the resource to load
124 * @param[in] samplingMode The SamplingMode of the resource to load
125 * @return A smart-pointer to the newly allocated visual.
127 static ImageVisualPtr New(VisualFactoryCache& factoryCache,
128 ImageVisualShaderFactory& shaderFactory,
129 const VisualUrl& imageUrl,
130 const Property::Map& properties,
131 ImageDimensions size = ImageDimensions(),
132 FittingMode::Type fittingMode = FittingMode::DEFAULT,
133 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR);
136 * @brief Create a new image visual with a URL.
138 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
140 * @param[in] factoryCache The VisualFactoryCache object
141 * @param[in] shaderFactory The ImageVisualShaderFactory object
142 * @param[in] imageUrl The URL of the image resource to use
143 * @param[in] size The width and height to fit the loaded image to.
144 * @param[in] fittingMode The FittingMode of the resource to load
145 * @param[in] samplingMode The SamplingMode of the resource to load
146 * @return A smart-pointer to the newly allocated visual.
148 static ImageVisualPtr New(VisualFactoryCache& factoryCache,
149 ImageVisualShaderFactory& shaderFactory,
150 const VisualUrl& imageUrl,
151 ImageDimensions size = ImageDimensions(),
152 FittingMode::Type fittingMode = FittingMode::DEFAULT,
153 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR);
155 public: // from Visual
157 * @copydoc Visual::Base::GetNaturalSize
159 void GetNaturalSize(Vector2& naturalSize) override;
162 * @copydoc Visual::Base::CreatePropertyMap
164 void DoCreatePropertyMap(Property::Map& map) const override;
167 * @copydoc Visual::Base::CreateInstancePropertyMap
169 void DoCreateInstancePropertyMap(Property::Map& map) const override;
172 * @copydoc Visual::Base::OnDoAction
174 void OnDoAction(const Dali::Property::Index actionId, const Dali::Property::Value& attributes) override;
178 * @brief Constructor with a URL.
180 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
182 * @param[in] factoryCache The VisualFactoryCache object
183 * @param[in] shaderFactory The ImageVisualShaderFactory object
184 * @param[in] imageUrl The URL of the image resource to use
185 * @param[in] size The width and height to fit the loaded image to.
186 * @param[in] fittingMode The FittingMode of the resource to load
187 * @param[in] samplingMode The SamplingMode of the resource to load
189 ImageVisual(VisualFactoryCache& factoryCache,
190 ImageVisualShaderFactory& shaderFactory,
191 const VisualUrl& imageUrl,
192 ImageDimensions size,
193 FittingMode::Type fittingMode,
194 Dali::SamplingMode::Type samplingMode);
197 * @brief A reference counted object may only be deleted by calling Unreference().
199 virtual ~ImageVisual();
202 * @copydoc Visual::Base::OnInitialize
204 void OnInitialize() override;
207 * @copydoc Visual::Base::DoSetProperties
209 void DoSetProperties(const Property::Map& propertyMap) override;
212 * @copydoc Visual::Base::DoSetOnScene
214 void DoSetOnScene(Actor& actor) override;
217 * @copydoc Visual::Base::DoSetOffScene
219 void DoSetOffScene(Actor& actor) override;
222 * @copydoc Visual::Base::OnSetTransform
224 void OnSetTransform() override;
227 * @copydoc Visual::Base::IsResourceReady
229 bool IsResourceReady() const override;
232 * @copydoc Visual::Base::UpdateShader
234 void UpdateShader() override;
237 * @copydoc Visual::Base::GenerateShader
239 Shader GenerateShader() const override;
243 * @copydoc AtlasUploadObserver::UploadCompleted
245 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
246 * This callback is the place to add the renderer as it would be called once the loading is finished.
248 void UploadCompleted() override;
251 * @copydoc TextureUploadObserver::LoadCompleted
253 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
254 * This callback is the place to add the renderer as it would be called once the loading is finished.
256 void LoadComplete(bool success, TextureInformation textureInformation) override;
260 * Allocate the mask data when a masking property is defined in the property map
262 void AllocateMaskData();
265 * @brief Load the texture, will try to atlas unless unable or param set to false.
266 * @param[in, out] atlasing flag if the image has been put in a atlas (true), passing false will not atlas even if possible.
267 * @param[out] atlasRect if atlasing is used this the texture area of the image in the atlas.
268 * @param[out] textures resulting texture set from the image loading.
269 * @param[in] orientationCorrection flag determines if orientation correction should be performed
270 * @param[in] forceReload flag determines if the texture should be reloaded from its source or use the cached texture.
272 void LoadTexture(bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection, TextureManager::ReloadPolicy forceReload);
275 * @brief Checks if atlasing should be attempted
276 * @return bool returns true if atlasing can be attempted.
278 bool AttemptAtlasing();
281 * @brief Initializes the Dali::Renderer from the image url
283 void InitializeRenderer();
286 * Creates the texture set and adds the texture to it
287 * @param[out] textureRect The texture area of the texture in the atlas.
288 * @param[in] url The URL of the image resource to use.
289 * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
290 * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
291 * @return the texture set to use
293 TextureSet CreateTextureSet(Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing);
296 * Set the value to the uTextureRect uniform
297 * @param[in] textureRect The texture rectangular area.
299 void SetTextureRectUniform(const Vector4& textureRect);
302 * Remove texture with valid TextureId
304 void RemoveTexture();
307 * Helper method to set individual values by index key.
308 * @param[in] index The index key of the value
309 * @param[in] value The value
311 void DoSetProperty(Property::Index index, const Property::Value& value);
315 WeakHandle<Actor> mPlacementActor;
317 TextureManager::MaskingDataPointer mMaskingData;
319 Dali::ImageDimensions mDesiredSize;
320 TextureManager::TextureId mTextureId;
321 TextureSet mTextures;
323 ImageVisualShaderFactory& mImageVisualShaderFactory;
325 Dali::FittingMode::Type mFittingMode : 3;
326 Dali::SamplingMode::Type mSamplingMode : 4;
327 Dali::WrapMode::Type mWrapModeU : 3;
328 Dali::WrapMode::Type mWrapModeV : 3;
329 Dali::Toolkit::ImageVisual::LoadPolicy::Type mLoadPolicy;
330 Dali::Toolkit::ImageVisual::ReleasePolicy::Type mReleasePolicy;
332 Dali::ImageDimensions mAtlasRectSize;
333 TextureManager::LoadState mLoadState; ///< The texture loading state
334 bool mAttemptAtlasing; ///< If true will attempt atlasing, otherwise create unique texture
335 bool mOrientationCorrection; ///< true if the image will have it's orientation corrected.
338 } // namespace Internal
340 } // namespace Toolkit
344 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */