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-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/internal/visuals/texture-upload-observer.h>
31 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
32 #include <dali-toolkit/internal/visuals/visual-url.h>
33 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
34 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
47 class ImageVisualShaderFactory;
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] shaderFactory The ImageVisualShaderFactory object
124 * @param[in] imageUrl The URL of the image resource to use
125 * @param[in] properties A Property::Map containing settings for this visual
126 * @param[in] size The width and height to fit the loaded image to.
127 * @param[in] fittingMode The FittingMode of the resource to load
128 * @param[in] samplingMode The SamplingMode of the resource to load
129 * @return A smart-pointer to the newly allocated visual.
131 static ImageVisualPtr New( VisualFactoryCache& factoryCache,
132 ImageVisualShaderFactory& shaderFactory,
133 const VisualUrl& imageUrl,
134 const Property::Map& properties,
135 ImageDimensions size = ImageDimensions(),
136 FittingMode::Type fittingMode = FittingMode::DEFAULT,
137 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
140 * @brief Create a new image visual with a URL.
142 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
144 * @param[in] factoryCache The VisualFactoryCache object
145 * @param[in] shaderFactory The ImageVisualShaderFactory object
146 * @param[in] imageUrl The URL of the image resource to use
147 * @param[in] size The width and height to fit the loaded image to.
148 * @param[in] fittingMode The FittingMode of the resource to load
149 * @param[in] samplingMode The SamplingMode of the resource to load
150 * @return A smart-pointer to the newly allocated visual.
152 static ImageVisualPtr New( VisualFactoryCache& factoryCache,
153 ImageVisualShaderFactory& shaderFactory,
154 const VisualUrl& imageUrl,
155 ImageDimensions size = ImageDimensions(),
156 FittingMode::Type fittingMode = FittingMode::DEFAULT,
157 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
159 public: // from Visual
162 * @copydoc Visual::Base::GetNaturalSize
164 void GetNaturalSize( Vector2& naturalSize ) override;
167 * @copydoc Visual::Base::CreatePropertyMap
169 void DoCreatePropertyMap( Property::Map& map ) const override;
172 * @copydoc Visual::Base::CreateInstancePropertyMap
174 void DoCreateInstancePropertyMap( Property::Map& map ) const override;
177 * @copydoc Visual::Base::OnDoAction
179 void OnDoAction( const Dali::Property::Index actionName, const Dali::Property::Value& attributes ) override;
184 * @brief Constructor with a URL.
186 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
188 * @param[in] factoryCache The VisualFactoryCache object
189 * @param[in] shaderFactory The ImageVisualShaderFactory object
190 * @param[in] imageUrl The URL of the image resource to use
191 * @param[in] size The width and height to fit the loaded image to.
192 * @param[in] fittingMode The FittingMode of the resource to load
193 * @param[in] samplingMode The SamplingMode of the resource to load
195 ImageVisual( VisualFactoryCache& factoryCache,
196 ImageVisualShaderFactory& shaderFactory,
197 const VisualUrl& imageUrl,
198 ImageDimensions size,
199 FittingMode::Type fittingMode,
200 Dali::SamplingMode::Type samplingMode );
203 * @brief A reference counted object may only be deleted by calling Unreference().
205 virtual ~ImageVisual();
208 * @copydoc Visual::Base::DoSetProperties
210 void DoSetProperties( const Property::Map& propertyMap ) override;
213 * @copydoc Visual::Base::DoSetOnScene
215 void DoSetOnScene( Actor& actor ) override;
218 * @copydoc Visual::Base::DoSetOffScene
220 void DoSetOffScene( Actor& actor ) override;
223 * @copydoc Visual::Base::OnSetTransform
225 void OnSetTransform() override;
228 * @copydoc Visual::Base::IsResourceReady
230 bool IsResourceReady() const override;
233 * @copydoc Visual::Base::UpdateShader
235 void UpdateShader() 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::UploadCompleted
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 UploadComplete( bool success, int32_t textureId, TextureSet textureSet,
254 bool usingAtlas, const Vector4& atlasRectangle, bool preMultiplied ) override;
259 * @copydoc TextureUploadObserver::LoadComplete
261 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
262 * This callback is the place to add the renderer as it would be called once the PixelBuffer loading is finished.
264 void LoadComplete( bool loadSuccess, Devel::PixelBuffer pixelBuffer, const VisualUrl& url, bool preMultiplied ) override {}
267 * Allocate the mask data when a masking property is defined in the property map
269 void AllocateMaskData();
272 * @brief Load the texture, will try to atlas unless unable or param set to false.
273 * @param[in, out] atlasing flag if the image has been put in a atlas (true), passing false will not atlas even if possible.
274 * @param[out] atlasRect if atlasing is used this the texture area of the image in the atlas.
275 * @param[out] textures resulting texture set from the image loading.
276 * @param[in] orientationCorrection flag determines if orientation correction should be performed
277 * @param[in] forceReload flag determines if the texture should be reloaded from its source or use the cached texture.
279 void LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection, TextureManager::ReloadPolicy forceReload );
282 * @brief Checks if atlasing should be attempted
283 * @return bool returns true if atlasing can be attempted.
285 bool AttemptAtlasing();
288 * @brief Initializes the Dali::Renderer from the image url
290 void InitializeRenderer();
293 * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
294 * @param[in] textures to use
296 void CreateRenderer( TextureSet& textures );
299 * Creates the texture set and adds the texture to it
300 * @param[out] textureRect The texture area of the texture in the atlas.
301 * @param[in] url The URL of the image resource to use.
302 * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
303 * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
304 * @return the texture set to use
306 TextureSet CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing );
309 * Set the value to the uTextureRect uniform
310 * @param[in] textureRect The texture rectangular area.
312 void SetTextureRectUniform( const Vector4& textureRect );
315 * Remove texture with valid TextureId
317 void RemoveTexture();
320 * Helper method to set individual values by index key.
321 * @param[in] index The index key of the value
322 * @param[in] value The value
324 void DoSetProperty( Property::Index index, const Property::Value& value );
327 * @brief Get a shader for the current properties.
328 * @return The shader for the current properties.
335 WeakHandle<Actor> mPlacementActor;
337 TextureManager::MaskingDataPointer mMaskingData;
339 Dali::ImageDimensions mDesiredSize;
340 TextureManager::TextureId mTextureId;
341 TextureSet mTextures;
343 ImageVisualShaderFactory& mImageVisualShaderFactory;
345 Dali::FittingMode::Type mFittingMode:3;
346 Dali::SamplingMode::Type mSamplingMode:4;
347 Dali::WrapMode::Type mWrapModeU:3;
348 Dali::WrapMode::Type mWrapModeV:3;
349 Dali::Toolkit::ImageVisual::LoadPolicy::Type mLoadPolicy;
350 Dali::Toolkit::ImageVisual::ReleasePolicy::Type mReleasePolicy;
352 Dali::ImageDimensions mAtlasRectSize;
353 TextureManager::LoadState mLoadState; ///< The texture loading state
354 bool mAttemptAtlasing; ///< If true will attempt atlasing, otherwise create unique texture
355 bool mOrientationCorrection; ///< true if the image will have it's orientation corrected.
358 } // namespace Internal
360 } // namespace Toolkit
364 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */