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>
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 |
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
117 * @brief Create a new image visual with a URL.
119 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
121 * @param[in] factoryCache The VisualFactoryCache 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 const VisualUrl& imageUrl,
131 const Property::Map& properties,
132 ImageDimensions size = ImageDimensions(),
133 FittingMode::Type fittingMode = FittingMode::DEFAULT,
134 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
137 * @brief Create a new image visual with a URL.
139 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
141 * @param[in] factoryCache The VisualFactoryCache 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 const VisualUrl& imageUrl,
150 ImageDimensions size = ImageDimensions(),
151 FittingMode::Type fittingMode = FittingMode::DEFAULT,
152 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
155 * @brief Create a new image visual with an Image type.
157 * @param[in] factoryCache The VisualFactoryCache object
158 * @param[in] image The image to use
160 static ImageVisualPtr New( VisualFactoryCache& factoryCache, const Image& image );
162 public: // from Visual
165 * @copydoc Visual::Base::GetNaturalSize
167 virtual void GetNaturalSize( Vector2& naturalSize );
170 * @copydoc Visual::Base::CreatePropertyMap
172 virtual void DoCreatePropertyMap( Property::Map& map ) const;
175 * @copydoc Visual::Base::CreateInstancePropertyMap
177 virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
182 * @brief Constructor with a URL.
184 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
186 * @param[in] factoryCache The VisualFactoryCache object
187 * @param[in] imageUrl The URL of the image resource to use
188 * @param[in] size The width and height to fit the loaded image to.
189 * @param[in] fittingMode The FittingMode of the resource to load
190 * @param[in] samplingMode The SamplingMode of the resource to load
192 ImageVisual( VisualFactoryCache& factoryCache,
193 const VisualUrl& imageUrl,
194 ImageDimensions size,
195 FittingMode::Type fittingMode,
196 Dali::SamplingMode::Type samplingMode );
199 * @brief Constructor with an Image type.
201 * @param[in] factoryCache The VisualFactoryCache object
202 * @param[in] image The image to use
204 ImageVisual( VisualFactoryCache& factoryCache, const Image& image );
207 * @brief A reference counted object may only be deleted by calling Unreference().
209 virtual ~ImageVisual();
212 * @copydoc Visual::Base::DoSetProperties
214 virtual void DoSetProperties( const Property::Map& propertyMap );
217 * @copydoc Visual::Base::DoSetOnStage
219 virtual void DoSetOnStage( Actor& actor );
222 * @copydoc Visual::Base::DoSetOffStage
224 virtual void DoSetOffStage( Actor& actor );
227 * @copydoc Visual::Base::OnSetTransform
229 virtual void OnSetTransform();
234 * Get the standard image rendering shader.
235 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
236 * @param[in] atlasing Whether texture atlasing is applied.
237 * @param[in] defaultTextureWrapping Whether the default texture wrap mode is applied.
239 static Shader GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping );
242 * @copydoc AtlasUploadObserver::UploadCompleted
244 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
245 * This callback is the place to add the renderer as it would be called once the loading is finished.
247 virtual void UploadCompleted();
250 * @copydoc TextureUploadObserver::UploadCompleted
252 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
253 * This callback is the place to add the renderer as it would be called once the loading is finished.
255 virtual void UploadComplete( bool success, int32_t textureId, TextureSet textureSet, bool usingAtlas, const Vector4& atlasRectangle );
260 * Allocate the mask data when a masking property is defined in the property map
262 void AllocateMaskData();
265 * @brief Applies the image to the texture set used for this renderer
267 * @param[in] image The Image to apply to the texture set used for this renderer
269 void ApplyImageToSampler( const Image& image );
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.
277 void LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures );
280 * @brief Initializes the Dali::Renderer from the image url
282 void InitializeRenderer();
285 * @brief Initializes the Dali::Renderer from an image handle
287 * @param[in] image The image handle to intialize this ImageVisual from
289 void InitializeRenderer( const Image& image );
292 * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
293 * @param[in] textures to use
295 void CreateRenderer( TextureSet& textures );
298 * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
299 * @param NativeImageRenderer
301 void CreateNativeImageRenderer( NativeImage& nativeImage );
304 * @brief Query whether resources requires to be loaded synchronously.
305 * @return Returns true if synchronous resource loading is required, false otherwise.
307 bool IsSynchronousResourceLoading() const;
310 * Creates the texture set and adds the texture to it
311 * @param[out] textureRect The texture area of the texture in the atlas.
312 * @param[in] url The URL of the image resource to use.
313 * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
314 * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
315 * @return the texture set to use
317 TextureSet CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing );
320 * Set the value to the uTextureRect uniform
321 * @param[in] textureRect The texture rectangular area.
323 void SetTextureRectUniform( const Vector4& textureRect );
326 * Remove texture with valid TextureId
328 void RemoveTexture();
331 * Helper method to set individual values by index key.
332 * @param[in] index The index key of the value
333 * @param[in] value The value
335 void DoSetProperty( Property::Index index, const Property::Value& value );
341 WeakHandle<Actor> mPlacementActor;
343 TextureManager::MaskingDataPointer mMaskingData;
345 Dali::ImageDimensions mDesiredSize;
346 TextureManager::TextureId mTextureId;
347 TextureSet mTextures;
349 Dali::FittingMode::Type mFittingMode:3;
350 Dali::SamplingMode::Type mSamplingMode:4;
351 Dali::WrapMode::Type mWrapModeU:3;
352 Dali::WrapMode::Type mWrapModeV:3;
353 DevelImageVisual::LoadPolicy::Type mLoadPolicy;
354 DevelImageVisual::ReleasePolicy::Type mReleasePolicy;
356 bool mAttemptAtlasing; ///< If true will attempt atlasing, otherwise create unique texture
357 bool mLoading; ///< True if the texture is still loading.
362 } // namespace Internal
364 } // namespace Toolkit
368 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */