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 |
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] imageUrl The URL of the image resource to use
124 * @param[in] properties A Property::Map containing settings for this visual
125 * @param[in] size The width and height to fit the loaded image to.
126 * @param[in] fittingMode The FittingMode of the resource to load
127 * @param[in] samplingMode The SamplingMode of the resource to load
128 * @return A smart-pointer to the newly allocated visual.
130 static ImageVisualPtr New( VisualFactoryCache& factoryCache,
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] imageUrl The URL of the image resource to use
144 * @param[in] size The width and height to fit the loaded image to.
145 * @param[in] fittingMode The FittingMode of the resource to load
146 * @param[in] samplingMode The SamplingMode of the resource to load
147 * @return A smart-pointer to the newly allocated visual.
149 static ImageVisualPtr New( VisualFactoryCache& factoryCache,
150 const VisualUrl& imageUrl,
151 ImageDimensions size = ImageDimensions(),
152 FittingMode::Type fittingMode = FittingMode::DEFAULT,
153 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
156 * @brief Create a new image visual with an Image type.
158 * @param[in] factoryCache The VisualFactoryCache object
159 * @param[in] image The image to use
161 static ImageVisualPtr New( VisualFactoryCache& factoryCache, const Image& image );
163 public: // from Visual
166 * @copydoc Visual::Base::GetNaturalSize
168 virtual void GetNaturalSize( Vector2& naturalSize );
171 * @copydoc Visual::Base::CreatePropertyMap
173 virtual void DoCreatePropertyMap( Property::Map& map ) const;
176 * @copydoc Visual::Base::CreateInstancePropertyMap
178 virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
183 * @brief Constructor with a URL.
185 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
187 * @param[in] factoryCache The VisualFactoryCache object
188 * @param[in] imageUrl The URL of the image resource to use
189 * @param[in] size The width and height to fit the loaded image to.
190 * @param[in] fittingMode The FittingMode of the resource to load
191 * @param[in] samplingMode The SamplingMode of the resource to load
193 ImageVisual( VisualFactoryCache& factoryCache,
194 const VisualUrl& imageUrl,
195 ImageDimensions size,
196 FittingMode::Type fittingMode,
197 Dali::SamplingMode::Type samplingMode );
200 * @brief Constructor with an Image type.
202 * @param[in] factoryCache The VisualFactoryCache object
203 * @param[in] image The image to use
205 ImageVisual( VisualFactoryCache& factoryCache, const Image& image );
208 * @brief A reference counted object may only be deleted by calling Unreference().
210 virtual ~ImageVisual();
213 * @copydoc Visual::Base::DoSetProperties
215 virtual void DoSetProperties( const Property::Map& propertyMap );
218 * @copydoc Visual::Base::DoSetOnStage
220 virtual void DoSetOnStage( Actor& actor );
223 * @copydoc Visual::Base::DoSetOffStage
225 virtual void DoSetOffStage( Actor& actor );
228 * @copydoc Visual::Base::OnSetTransform
230 virtual void OnSetTransform();
233 * @copydoc Visual::Base::IsResourceReady
235 virtual bool IsResourceReady() const;
240 * Get the standard image rendering shader.
241 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
242 * @param[in] atlasing Whether texture atlasing is applied.
243 * @param[in] defaultTextureWrapping Whether the default texture wrap mode is applied.
245 static Shader GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping );
248 * @copydoc AtlasUploadObserver::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 virtual void UploadCompleted();
256 * @copydoc TextureUploadObserver::UploadCompleted
258 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
259 * This callback is the place to add the renderer as it would be called once the loading is finished.
261 virtual void UploadComplete( bool success, int32_t textureId, TextureSet textureSet, bool usingAtlas, const Vector4& atlasRectangle );
266 * Allocate the mask data when a masking property is defined in the property map
268 void AllocateMaskData();
271 * @brief Applies the image to the texture set used for this renderer
273 * @param[in] image The Image to apply to the texture set used for this renderer
275 void ApplyImageToSampler( const Image& image );
278 * @brief Load the texture, will try to atlas unless unable or param set to false.
279 * @param[in, out] atlasing flag if the image has been put in a atlas (true), passing false will not atlas even if possible.
280 * @param[out] atlasRect if atlasing is used this the texture area of the image in the atlas.
281 * @param[out] textures resulting texture set from the image loading.
282 * @param[in] orientationCorrection flag determines if orientation correction should be performed
284 void LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection );
287 * @brief Initializes the Dali::Renderer from the image url
289 void InitializeRenderer();
292 * @brief Initializes the Dali::Renderer from an image handle
294 * @param[in] image The image handle to intialize this ImageVisual from
296 void InitializeRenderer( const Image& image );
299 * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
300 * @param[in] textures to use
302 void CreateRenderer( TextureSet& textures );
305 * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
306 * @param NativeImageRenderer
308 void CreateNativeImageRenderer( NativeImage& nativeImage );
311 * @brief Query whether resources requires to be loaded synchronously.
312 * @return Returns true if synchronous resource loading is required, false otherwise.
314 bool IsSynchronousResourceLoading() const;
317 * Creates the texture set and adds the texture to it
318 * @param[out] textureRect The texture area of the texture in the atlas.
319 * @param[in] url The URL of the image resource to use.
320 * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
321 * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
322 * @return the texture set to use
324 TextureSet CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing );
327 * Set the value to the uTextureRect uniform
328 * @param[in] textureRect The texture rectangular area.
330 void SetTextureRectUniform( const Vector4& textureRect );
333 * Remove texture with valid TextureId
335 void RemoveTexture();
338 * Helper method to set individual values by index key.
339 * @param[in] index The index key of the value
340 * @param[in] value The value
342 void DoSetProperty( Property::Index index, const Property::Value& value );
348 WeakHandle<Actor> mPlacementActor;
350 TextureManager::MaskingDataPointer mMaskingData;
352 Dali::ImageDimensions mDesiredSize;
353 TextureManager::TextureId mTextureId;
354 TextureSet mTextures;
356 Dali::FittingMode::Type mFittingMode:3;
357 Dali::SamplingMode::Type mSamplingMode:4;
358 Dali::WrapMode::Type mWrapModeU:3;
359 Dali::WrapMode::Type mWrapModeV:3;
360 DevelImageVisual::LoadPolicy::Type mLoadPolicy;
361 DevelImageVisual::ReleasePolicy::Type mReleasePolicy;
363 bool mAttemptAtlasing; ///< If true will attempt atlasing, otherwise create unique texture
364 bool mLoading; ///< True if the texture is still loading.
365 bool mOrientationCorrection; ///< true if the image will have it's orientation corrected.
370 } // namespace Internal
372 } // namespace Toolkit
376 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */