1 #ifndef DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H
5 * Copyright (c) 2023 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>
27 #include <dali/public-api/rendering/visual-renderer.h>
30 #include <dali-toolkit/devel-api/image-loader/atlas-upload-observer.h>
31 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
32 #include <dali-toolkit/internal/image-loader/fast-track-loading-task.h>
33 #include <dali-toolkit/internal/texture-manager/texture-upload-observer.h>
34 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
35 #include <dali-toolkit/internal/visuals/visual-url.h>
36 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
46 class ImageVisualShaderFactory;
48 typedef IntrusivePtr<ImageVisual> ImageVisualPtr;
51 * The visual which renders an image to a quad geometry
53 * The following properties are optional
55 * | %Property Name | Type |
56 * |-----------------------|-------------------|
58 * | alphaMaskUrl | STRING |
59 * | fittingMode | INTEGER OR STRING |
60 * | samplingMode | INTEGER OR STRING |
61 * | desiredWidth | INTEGER |
62 * | desiredHeight | INTEGER |
63 * | synchronousLoading | BOOLEAN |
64 * | pixelArea | VECTOR4 |
65 * | wrapModeU | INTEGER OR STRING |
66 * | wrapModeV | INTEGER OR STRING |
67 * | loadPolicy | INTEGER OR STRING |
68 * | releasePolicy | INTEGER OR STRING |
69 * | orientationCorrection | BOOLEAN |
71 * where pixelArea is a rectangular area.
72 * In its Vector4 value, the first two elements indicate the top-left position of the area,
73 * and the last two elements are the area width and height respectively.
74 * If not specified, the default value is [0.0, 0.0, 1.0, 1.0], i.e. the entire area of the image.
76 * 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.
77 * Its value should be one of the following wrap mode:
83 * where imageFittingMode should be one of the following fitting modes:
90 * where imageSamplingMode should be one of the following sampling modes:
100 * where loadPolicy should be one of the following image loading modes
101 * "IMMEDIATE" // Loads image even if visual not attached to stage yet
102 * "ATTACHED" // Only loads image once visual is attached to stage
104 * where releasePolicy should be one of the following policies for when to cache the image
105 * "DETACHED" // Release image from cache when visual detached from stage
106 * "DESTROYED" // Keep image in cache until the visual is destroyed
107 * "NEVER" // Keep image in cache until application ends.
109 * If the Visual is in a LayerUI it will pixel align the image, using a Layer3D will disable pixel alignment.
110 * Changing layer behaviour between LayerUI to Layer3D whilst the visual is already staged will not have an effect.
112 class ImageVisual : public Visual::Base, public ConnectionTracker, public AtlasUploadObserver, public TextureUploadObserver
116 * @brief Create a new image visual with a URL.
118 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
120 * @param[in] factoryCache The VisualFactoryCache object
121 * @param[in] shaderFactory The ImageVisualShaderFactory object
122 * @param[in] imageUrl The URL of the image resource to use
123 * @param[in] properties A Property::Map containing settings for this visual
124 * @param[in] size The width and height to fit the loaded image to.
125 * @param[in] fittingMode The FittingMode of the resource to load
126 * @param[in] samplingMode The SamplingMode of the resource to load
127 * @return A smart-pointer to the newly allocated visual.
129 static ImageVisualPtr New(VisualFactoryCache& factoryCache,
130 ImageVisualShaderFactory& shaderFactory,
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] shaderFactory The ImageVisualShaderFactory object
144 * @param[in] imageUrl The URL of the image resource to use
145 * @param[in] size The width and height to fit the loaded image to.
146 * @param[in] fittingMode The FittingMode of the resource to load
147 * @param[in] samplingMode The SamplingMode of the resource to load
148 * @return A smart-pointer to the newly allocated visual.
150 static ImageVisualPtr New(VisualFactoryCache& factoryCache,
151 ImageVisualShaderFactory& shaderFactory,
152 const VisualUrl& imageUrl,
153 ImageDimensions size = ImageDimensions(),
154 FittingMode::Type fittingMode = FittingMode::DEFAULT,
155 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR);
157 public: // from Visual
159 * @copydoc Visual::Base::GetNaturalSize
161 void GetNaturalSize(Vector2& naturalSize) override;
164 * @copydoc Visual::Base::CreatePropertyMap
166 void DoCreatePropertyMap(Property::Map& map) const override;
169 * @copydoc Visual::Base::CreateInstancePropertyMap
171 void DoCreateInstancePropertyMap(Property::Map& map) const override;
174 * @copydoc Visual::Base::OnDoAction
176 void OnDoAction(const Dali::Property::Index actionId, const Dali::Property::Value& attributes) override;
180 * @brief Constructor with a URL.
182 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
184 * @param[in] factoryCache The VisualFactoryCache object
185 * @param[in] shaderFactory The ImageVisualShaderFactory object
186 * @param[in] imageUrl The URL of the image resource to use
187 * @param[in] size The width and height to fit the loaded image to.
188 * @param[in] fittingMode The FittingMode of the resource to load
189 * @param[in] samplingMode The SamplingMode of the resource to load
191 ImageVisual(VisualFactoryCache& factoryCache,
192 ImageVisualShaderFactory& shaderFactory,
193 const VisualUrl& imageUrl,
194 ImageDimensions size,
195 FittingMode::Type fittingMode,
196 Dali::SamplingMode::Type samplingMode);
199 * @brief A reference counted object may only be deleted by calling Unreference().
201 virtual ~ImageVisual();
204 * @copydoc Visual::Base::OnInitialize
206 void OnInitialize() override;
209 * @copydoc Visual::Base::DoSetProperties
211 void DoSetProperties(const Property::Map& propertyMap) override;
214 * @copydoc Visual::Base::DoSetOnScene
216 void DoSetOnScene(Actor& actor) override;
219 * @copydoc Visual::Base::DoSetOffScene
221 void DoSetOffScene(Actor& actor) override;
224 * @copydoc Visual::Base::OnSetTransform
226 void OnSetTransform() override;
229 * @copydoc Visual::Base::UpdateShader
231 void UpdateShader() override;
234 * @copydoc Visual::Base::GenerateShader
236 Shader GenerateShader() const override;
240 * @copydoc AtlasUploadObserver::UploadCompleted
242 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
243 * This callback is the place to add the renderer as it would be called once the loading is finished.
245 void UploadCompleted() override;
248 * @copydoc TextureUploadObserver::LoadCompleted
250 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
251 * This callback is the place to add the renderer as it would be called once the loading is finished.
253 void LoadComplete(bool success, TextureInformation textureInformation) override;
256 * @brief Test callback for FastTrackLoadingTask
258 * @param[in] task The pointer of task who call this callback.
260 void FastLoadComplete(FastTrackLoadingTaskPtr task);
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 * Creates the texture set and adds the texture to it
291 * @param[out] textureRect The texture area of the texture in the atlas.
292 * @param[in] url The URL of the image resource to use.
293 * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
294 * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
295 * @return the texture set to use
297 TextureSet CreateTextureSet(Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing);
300 * Set the value to the uTextureRect uniform
301 * @param[in] textureRect The texture rectangular area.
303 void SetTextureRectUniform(const Vector4& textureRect);
306 * Remove texture with valid TextureId
308 void RemoveTexture();
311 * @brief Compute texture size
313 void ComputeTextureSize();
316 * @brief Compute mask texture ratio
317 * @return The Mask Texture Ratio
319 Vector2 ComputeMaskTextureRatio();
322 * Helper method to set individual values by index key.
323 * @param[in] index The index key of the value
324 * @param[in] value The value
326 void DoSetProperty(Property::Index index, const Property::Value& value);
329 * @brief Check whether the mask texture is loaded or not.
330 * If MaskingType is MASKING_ON_LOADING and mask texture is failed to load, update shader.
332 void CheckMaskTexture();
335 * @brief Reset Renderer using empty texture
336 * For drawing empty visual, reset the renderer.
338 void ResetRenderer();
341 * @brief Show broken image when image loading is failed.
343 void ShowBrokenImage();
346 * @brief Remove current added fast track upload task.
348 void ResetFastTrackLoadingTask();
352 WeakHandle<Actor> mPlacementActor;
354 TextureManager::MaskingDataPointer mMaskingData;
356 Dali::ImageDimensions mDesiredSize;
357 TextureManager::TextureId mTextureId;
358 TextureSet mTextures;
359 Vector2 mTextureSize;
360 Vector2 mPlacementActorSize;
362 FastTrackLoadingTaskPtr mFastTrackLoadingTask; ///< For fast track uploading.
364 ImageVisualShaderFactory& mImageVisualShaderFactory;
366 Dali::FittingMode::Type mFittingMode : 3;
367 Dali::SamplingMode::Type mSamplingMode : 4;
368 Dali::WrapMode::Type mWrapModeU : 3;
369 Dali::WrapMode::Type mWrapModeV : 3;
370 Dali::Toolkit::ImageVisual::LoadPolicy::Type mLoadPolicy;
371 Dali::Toolkit::ImageVisual::ReleasePolicy::Type mReleasePolicy;
373 Dali::ImageDimensions mAtlasRectSize;
374 TextureManager::LoadState mLoadState; ///< The texture loading state
375 bool mAttemptAtlasing; ///< If true will attempt atlasing, otherwise create unique texture
376 bool mOrientationCorrection; ///< true if the image will have it's orientation corrected.
377 bool mNeedYuvToRgb{false}; ///< true if we need to convert yuv to rgb.
378 bool mEnableBrokenImage{true}; ///< true if enable broken image.
379 bool mUseFastTrackUploading{false}; ///< True if we use fast tack feature.
380 bool mRendererAdded{false}; ///< True if renderer added into actor.
383 } // namespace Internal
385 } // namespace Toolkit
389 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */