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 * | releasePolicy | INTEGER OR STRING |
70 * where pixelArea is a rectangular area.
71 * In its Vector4 value, the first two elements indicate the top-left position of the area,
72 * and the last two elements are the area width and height respectively.
73 * If not specified, the default value is [0.0, 0.0, 1.0, 1.0], i.e. the entire area of the image.
75 * 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.
76 * Its value should be one of the following wrap mode:
82 * where imageFittingMode should be one of the following fitting modes:
89 * where imageSamplingMode should be one of the following sampling modes:
100 * where releasePolicy should be one of the following policies for when to cache the image
101 * "DETACHED" // Release image from cache when visual detached from stage
102 * "DESTROYED" // Keep image in cache until the visual is destroyed
103 * "NEVER" // Keep image in cache until application ends.
105 * If the Visual is in a LayerUI it will pixel align the image, using a Layer3D will disable pixel alignment.
106 * Changing layer behaviour between LayerUI to Layer3D whilst the visual is already staged will not have an effect.
108 class ImageVisual: public Visual::Base, public ConnectionTracker, public AtlasUploadObserver, public TextureUploadObserver
113 * @brief Create a new image visual with a URL.
115 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
117 * @param[in] factoryCache The VisualFactoryCache object
118 * @param[in] imageUrl The URL of the image resource to use
119 * @param[in] properties A Property::Map containing settings for this visual
120 * @param[in] size The width and height to fit the loaded image to.
121 * @param[in] fittingMode The FittingMode of the resource to load
122 * @param[in] samplingMode The SamplingMode of the resource to load
123 * @return A smart-pointer to the newly allocated visual.
125 static ImageVisualPtr New( VisualFactoryCache& factoryCache,
126 const VisualUrl& imageUrl,
127 const Property::Map& properties,
128 ImageDimensions size = ImageDimensions(),
129 FittingMode::Type fittingMode = FittingMode::DEFAULT,
130 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
133 * @brief Create a new image visual with a URL.
135 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
137 * @param[in] factoryCache The VisualFactoryCache object
138 * @param[in] imageUrl The URL of the image resource to use
139 * @param[in] size The width and height to fit the loaded image to.
140 * @param[in] fittingMode The FittingMode of the resource to load
141 * @param[in] samplingMode The SamplingMode of the resource to load
142 * @return A smart-pointer to the newly allocated visual.
144 static ImageVisualPtr New( VisualFactoryCache& factoryCache,
145 const VisualUrl& imageUrl,
146 ImageDimensions size = ImageDimensions(),
147 FittingMode::Type fittingMode = FittingMode::DEFAULT,
148 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
151 * @brief Create a new image visual with an Image type.
153 * @param[in] factoryCache The VisualFactoryCache object
154 * @param[in] image The image to use
156 static ImageVisualPtr New( VisualFactoryCache& factoryCache, const Image& image );
158 public: // from Visual
161 * @copydoc Visual::Base::GetNaturalSize
163 virtual void GetNaturalSize( Vector2& naturalSize );
166 * @copydoc Visual::Base::CreatePropertyMap
168 virtual void DoCreatePropertyMap( Property::Map& map ) const;
171 * @copydoc Visual::Base::CreateInstancePropertyMap
173 virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
178 * @brief Constructor with a URL.
180 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
182 * @param[in] factoryCache The VisualFactoryCache object
183 * @param[in] imageUrl The URL of the image resource to use
184 * @param[in] size The width and height to fit the loaded image to.
185 * @param[in] fittingMode The FittingMode of the resource to load
186 * @param[in] samplingMode The SamplingMode of the resource to load
188 ImageVisual( VisualFactoryCache& factoryCache,
189 const VisualUrl& imageUrl,
190 ImageDimensions size,
191 FittingMode::Type fittingMode,
192 Dali::SamplingMode::Type samplingMode );
195 * @brief Constructor with an Image type.
197 * @param[in] factoryCache The VisualFactoryCache object
198 * @param[in] image The image to use
200 ImageVisual( VisualFactoryCache& factoryCache, const Image& image );
203 * @brief A reference counted object may only be deleted by calling Unreference().
205 virtual ~ImageVisual();
208 * @copydoc Visual::Base::DoSetProperties
210 virtual void DoSetProperties( const Property::Map& propertyMap );
213 * @copydoc Visual::Base::DoSetOnStage
215 virtual void DoSetOnStage( Actor& actor );
218 * @copydoc Visual::Base::DoSetOffStage
220 virtual void DoSetOffStage( Actor& actor );
223 * @copydoc Visual::Base::OnSetTransform
225 virtual void OnSetTransform();
230 * Get the standard image rendering shader.
231 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
232 * @param[in] atlasing Whether texture atlasing is applied.
233 * @param[in] defaultTextureWrapping Whether the default texture wrap mode is applied.
235 static Shader GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping );
238 * @copydoc AtlasUploadObserver::UploadCompleted
240 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
241 * This callback is the place to add the renderer as it would be called once the loading is finished.
243 virtual void UploadCompleted();
246 * @copydoc TextureUploadObserver::UploadCompleted
248 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
249 * This callback is the place to add the renderer as it would be called once the loading is finished.
251 virtual void UploadComplete( bool success, int32_t textureId, TextureSet textureSet, bool usingAtlas, const Vector4& atlasRectangle );
256 * Allocate the mask data when a masking property is defined in the property map
258 void AllocateMaskData();
261 * @brief Applies the image to the texture set used for this renderer
263 * @param[in] image The Image to apply to the texture set used for this renderer
265 void ApplyImageToSampler( const Image& image );
268 * @brief Initializes the Dali::Renderer from the image url
270 void InitializeRenderer();
273 * @brief Initializes the Dali::Renderer from an image handle
275 * @param[in] image The image handle to intialize this ImageVisual from
277 void InitializeRenderer( const Image& image );
280 * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
281 * @param[in] textures to use
283 void CreateRenderer( TextureSet& textures );
286 * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
287 * @param NativeImageRenderer
289 void CreateNativeImageRenderer( NativeImage& nativeImage );
292 * @brief Query whether resources requires to be loaded synchronously.
293 * @return Returns true if synchronous resource loading is required, false otherwise.
295 bool IsSynchronousResourceLoading() const;
298 * Creates the texture set and adds the texture to it
299 * @param[out] textureRect The texture area of the texture in the atlas.
300 * @param[in] url The URL of the image resource to use.
301 * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
302 * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
303 * @return the texture set to use
305 TextureSet CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing );
308 * Set the value to the uTextureRect uniform
309 * @param[in] textureRect The texture rectangular area.
311 void SetTextureRectUniform( const Vector4& textureRect );
314 * Remove texture with valid TextureId
316 void RemoveTexture();
319 * Helper method to set individual values by index key.
320 * @param[in] index The index key of the value
321 * @param[in] value The value
323 void DoSetProperty( Property::Index index, const Property::Value& value );
329 WeakHandle<Actor> mPlacementActor;
331 TextureManager::MaskingDataPointer mMaskingData;
333 Dali::ImageDimensions mDesiredSize;
334 TextureManager::TextureId mTextureId;
336 Dali::FittingMode::Type mFittingMode:3;
337 Dali::SamplingMode::Type mSamplingMode:4;
338 Dali::WrapMode::Type mWrapModeU:3;
339 Dali::WrapMode::Type mWrapModeV:3;
340 DevelImageVisual::ReleasePolicy::Type mReleasePolicy;
341 bool mAttemptAtlasing; ///< If true will attempt atlasing, otherwise create unique texture
342 bool mLoading; ///< True if the texture is still loading.
347 } // namespace Internal
349 } // namespace Toolkit
353 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */