1 #ifndef DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H
5 * Copyright (c) 2017 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>
50 typedef IntrusivePtr< ImageVisual > ImageVisualPtr;
53 * The visual which renders an image to a quad geometry
55 * The following properties are optional
57 * | %Property Name | Type |
58 * |-----------------------|-------------------|
60 * | alphaMaskUrl | STRING |
61 * | fittingMode | INTEGER OR STRING |
62 * | samplingMode | INTEGER OR STRING |
63 * | desiredWidth | INTEGER |
64 * | desiredHeight | INTEGER |
65 * | synchronousLoading | BOOLEAN |
66 * | pixelArea | VECTOR4 |
67 * | wrapModeU | INTEGER OR STRING |
68 * | wrapModeV | INTEGER OR STRING |
69 * | loadPolicy | INTEGER OR STRING |
70 * | releasePolicy | INTEGER OR STRING |
71 * | orientationCorrection | BOOLEAN |
73 * where pixelArea is a rectangular area.
74 * In its Vector4 value, the first two elements indicate the top-left position of the area,
75 * and the last two elements are the area width and height respectively.
76 * If not specified, the default value is [0.0, 0.0, 1.0, 1.0], i.e. the entire area of the image.
78 * 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.
79 * Its value should be one of the following wrap mode:
85 * where imageFittingMode should be one of the following fitting modes:
92 * where imageSamplingMode should be one of the following sampling modes:
102 * where loadPolicy should be one of the following image loading modes
103 * "IMMEDIATE" // Loads image even if visual not attached to stage yet
104 * "ATTACHED" // Only loads image once visual is attached to stage
106 * where releasePolicy should be one of the following policies for when to cache the image
107 * "DETACHED" // Release image from cache when visual detached from stage
108 * "DESTROYED" // Keep image in cache until the visual is destroyed
109 * "NEVER" // Keep image in cache until application ends.
111 * If the Visual is in a LayerUI it will pixel align the image, using a Layer3D will disable pixel alignment.
112 * Changing layer behaviour between LayerUI to Layer3D whilst the visual is already staged will not have an effect.
114 class ImageVisual: public Visual::Base, public ConnectionTracker, public AtlasUploadObserver, public TextureUploadObserver
119 * @brief Create a new image visual with a URL.
121 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
123 * @param[in] factoryCache The VisualFactoryCache 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 const VisualUrl& imageUrl,
133 const Property::Map& properties,
134 ImageDimensions size = ImageDimensions(),
135 FittingMode::Type fittingMode = FittingMode::DEFAULT,
136 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
139 * @brief Create a new image visual with a URL.
141 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
143 * @param[in] factoryCache The VisualFactoryCache 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 const VisualUrl& imageUrl,
152 ImageDimensions size = ImageDimensions(),
153 FittingMode::Type fittingMode = FittingMode::DEFAULT,
154 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
157 * @brief Create a new image visual with an Image type.
159 * @param[in] factoryCache The VisualFactoryCache object
160 * @param[in] image The image to use
162 static ImageVisualPtr New( VisualFactoryCache& factoryCache, const Image& image );
164 public: // from Visual
167 * @copydoc Visual::Base::GetNaturalSize
169 virtual void GetNaturalSize( Vector2& naturalSize );
172 * @copydoc Visual::Base::CreatePropertyMap
174 virtual void DoCreatePropertyMap( Property::Map& map ) const;
177 * @copydoc Visual::Base::CreateInstancePropertyMap
179 virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
182 * @copydoc Visual::Base::OnDoAction
184 virtual void OnDoAction( const Dali::Property::Index actionName, const Dali::Property::Value& attributes ) override;
189 * @brief Constructor with a URL.
191 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
193 * @param[in] factoryCache The VisualFactoryCache object
194 * @param[in] imageUrl The URL of the image resource to use
195 * @param[in] size The width and height to fit the loaded image to.
196 * @param[in] fittingMode The FittingMode of the resource to load
197 * @param[in] samplingMode The SamplingMode of the resource to load
199 ImageVisual( VisualFactoryCache& factoryCache,
200 const VisualUrl& imageUrl,
201 ImageDimensions size,
202 FittingMode::Type fittingMode,
203 Dali::SamplingMode::Type samplingMode );
206 * @brief Constructor with an Image type.
208 * @param[in] factoryCache The VisualFactoryCache object
209 * @param[in] image The image to use
211 ImageVisual( VisualFactoryCache& factoryCache, const Image& image );
214 * @brief A reference counted object may only be deleted by calling Unreference().
216 virtual ~ImageVisual();
219 * @copydoc Visual::Base::DoSetProperties
221 virtual void DoSetProperties( const Property::Map& propertyMap );
224 * @copydoc Visual::Base::DoSetOnStage
226 virtual void DoSetOnStage( Actor& actor );
229 * @copydoc Visual::Base::DoSetOffStage
231 virtual void DoSetOffStage( Actor& actor ) ;
234 * @copydoc Visual::Base::OnSetTransform
236 virtual void OnSetTransform() ;
239 * @copydoc Visual::Base::IsResourceReady
241 virtual bool IsResourceReady() const;
246 * Get the standard image rendering shader.
247 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
248 * @param[in] atlasing Whether texture atlasing is applied.
249 * @param[in] defaultTextureWrapping Whether the default texture wrap mode is applied.
251 static Shader GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping );
254 * @copydoc AtlasUploadObserver::UploadCompleted
256 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
257 * This callback is the place to add the renderer as it would be called once the loading is finished.
259 virtual void UploadCompleted() override;
262 * @copydoc TextureUploadObserver::UploadCompleted
264 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
265 * This callback is the place to add the renderer as it would be called once the loading is finished.
267 virtual void UploadComplete( bool success, int32_t textureId, TextureSet textureSet,
268 bool usingAtlas, const Vector4& atlasRectangle, bool preMultiplied ) override;
273 * Allocate the mask data when a masking property is defined in the property map
275 void AllocateMaskData();
278 * @brief Applies the image to the texture set used for this renderer
280 * @param[in] image The Image to apply to the texture set used for this renderer
282 void ApplyImageToSampler( const Image& image );
285 * @brief Load the texture, will try to atlas unless unable or param set to false.
286 * @param[in, out] atlasing flag if the image has been put in a atlas (true), passing false will not atlas even if possible.
287 * @param[out] atlasRect if atlasing is used this the texture area of the image in the atlas.
288 * @param[out] textures resulting texture set from the image loading.
289 * @param[in] orientationCorrection flag determines if orientation correction should be performed
290 * @param[in] forceReload flag determines if the texture should be reloaded from its source or use the cached texture.
292 void LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection, TextureManager::ReloadPolicy forceReload );
295 * @brief Checks if atlasing should be attempted
296 * @return bool returns true if atlasing can be attempted.
298 bool AttemptAtlasing();
301 * @brief Initializes the Dali::Renderer from the image url
303 void InitializeRenderer();
306 * @brief Initializes the Dali::Renderer from an image handle
308 * @param[in] image The image handle to intialize this ImageVisual from
310 void InitializeRenderer( const Image& image );
313 * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
314 * @param[in] textures to use
316 void CreateRenderer( TextureSet& textures );
319 * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
320 * @param NativeImageRenderer
322 void CreateNativeImageRenderer( NativeImage& nativeImage );
325 * @brief Query whether resources requires to be loaded synchronously.
326 * @return Returns true if synchronous resource loading is required, false otherwise.
328 bool IsSynchronousResourceLoading() const;
331 * Creates the texture set and adds the texture to it
332 * @param[out] textureRect The texture area of the texture in the atlas.
333 * @param[in] url The URL of the image resource to use.
334 * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
335 * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
336 * @return the texture set to use
338 TextureSet CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing );
341 * Set the value to the uTextureRect uniform
342 * @param[in] textureRect The texture rectangular area.
344 void SetTextureRectUniform( const Vector4& textureRect );
347 * Remove texture with valid TextureId
349 void RemoveTexture();
352 * Helper method to set individual values by index key.
353 * @param[in] index The index key of the value
354 * @param[in] value The value
356 void DoSetProperty( Property::Index index, const Property::Value& value );
362 WeakHandle<Actor> mPlacementActor;
364 TextureManager::MaskingDataPointer mMaskingData;
366 Dali::ImageDimensions mDesiredSize;
367 TextureManager::TextureId mTextureId;
368 TextureSet mTextures;
370 Dali::FittingMode::Type mFittingMode:3;
371 Dali::SamplingMode::Type mSamplingMode:4;
372 Dali::WrapMode::Type mWrapModeU:3;
373 Dali::WrapMode::Type mWrapModeV:3;
374 Dali::Toolkit::ImageVisual::LoadPolicy::Type mLoadPolicy;
375 Dali::Toolkit::ImageVisual::ReleasePolicy::Type mReleasePolicy;
377 bool mAttemptAtlasing; ///< If true will attempt atlasing, otherwise create unique texture
378 bool mLoading; ///< True if the texture is still loading.
379 bool mOrientationCorrection; ///< true if the image will have it's orientation corrected.
384 } // namespace Internal
386 } // namespace Toolkit
390 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */