1 #ifndef DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H
5 * Copyright (c) 2016 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.
22 #include <dali/public-api/common/intrusive-ptr.h>
23 #include <dali/public-api/images/image.h>
24 #include <dali/public-api/images/image-operations.h>
25 #include <dali/public-api/images/resource-image.h>
26 #include <dali/devel-api/object/weak-handle.h>
29 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
30 #include <dali-toolkit/devel-api/image-loader/atlas-upload-observer.h>
44 typedef IntrusivePtr< ImageVisual > ImageVisualPtr;
47 * The visual which renders an image to the control's quad
49 * The following properties are optional
51 * | %Property Name | Type |
52 * |--------------------|-------------------|
54 * | fittingMode | INTEGER OR STRING |
55 * | samplingMode | INTEGER OR STRING |
56 * | desiredWidth | INTEGER |
57 * | desiredHeight | INTEGER |
58 * | synchronousLoading | BOOLEAN |
59 * | pixelArea | VECTOR4 |
60 * | wrapModeU | INTEGER OR STRING |
61 * | wrapModeV | INTEGER OR STRING |
63 * where pixelArea is a rectangular area.
64 * In its Vector4 value, the first two elements indicate the top-left position of the area,
65 * and the last two elements are the area width and height respectively.
66 * If not specified, the default value is [0.0, 0.0, 1.0, 1.0], i.e. the entire area of the image.
68 * 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.
69 * Its value should be one of the following wrap mode:
75 * where imageFittingMode should be one of the following fitting modes:
82 * where imageSamplingMode should be one of the following sampling modes:
93 class ImageVisual: public Visual::Base, public ConnectionTracker, public AtlasUploadObserver
98 * @brief Create a new image visual with a URL.
100 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
102 * @param[in] factoryCache The VisualFactoryCache object
103 * @param[in] imageUrl The URL of the image resource to use
104 * @param[in] properties A Property::Map containing settings for this visual
105 * @param[in] size The width and height to fit the loaded image to.
106 * @param[in] fittingMode The FittingMode of the resource to load
107 * @param[in] samplingMode The SamplingMode of the resource to load
108 * @return A smart-pointer to the newly allocated visual.
110 static ImageVisualPtr New( VisualFactoryCache& factoryCache,
111 const std::string& imageUrl,
112 const Property::Map& properties,
113 ImageDimensions size = ImageDimensions(),
114 FittingMode::Type fittingMode = FittingMode::DEFAULT,
115 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
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] 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 std::string& imageUrl,
131 ImageDimensions size = ImageDimensions(),
132 FittingMode::Type fittingMode = FittingMode::DEFAULT,
133 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
136 * @brief Create a new image visual with an Image type.
138 * @param[in] factoryCache The VisualFactoryCache object
139 * @param[in] image The image to use
141 static ImageVisualPtr New( VisualFactoryCache& factoryCache, const Image& image );
143 public: // from Visual
146 * @copydoc Visual::Base::GetNaturalSize
148 virtual void GetNaturalSize( Vector2& naturalSize );
151 * @copydoc Visual::Base::CreatePropertyMap
153 virtual void DoCreatePropertyMap( Property::Map& map ) const;
158 * @brief Constructor with a URL.
160 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
162 * @param[in] factoryCache The VisualFactoryCache object
163 * @param[in] imageUrl The URL of the image resource to use
164 * @param[in] size The width and height to fit the loaded image to.
165 * @param[in] fittingMode The FittingMode of the resource to load
166 * @param[in] samplingMode The SamplingMode of the resource to load
168 ImageVisual( VisualFactoryCache& factoryCache,
169 const std::string& imageUrl,
170 ImageDimensions size,
171 FittingMode::Type fittingMode,
172 Dali::SamplingMode::Type samplingMode );
175 * @brief Constructor with an Image type.
177 * @param[in] factoryCache The VisualFactoryCache object
178 * @param[in] image The image to use
180 ImageVisual( VisualFactoryCache& factoryCache, const Image& image );
183 * @brief A reference counted object may only be deleted by calling Unreference().
185 virtual ~ImageVisual();
188 * @copydoc Visual::Base::DoSetProperties
190 virtual void DoSetProperties( const Property::Map& propertyMap );
193 * @copydoc Visual::Base::DoSetOnStage
195 virtual void DoSetOnStage( Actor& actor );
198 * @copydoc Visual::Base::DoSetOffStage
200 virtual void DoSetOffStage( Actor& actor );
203 * @copydoc Visual::Base::OnSetTransform
205 virtual void OnSetTransform();
210 * Get the standard image rendering shader.
211 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
212 * @param[in] atlasing Whether texture atlasing is applied.
213 * @param[in] defaultTextureWrapping Whether the default texture wrap mode is applied.
215 static Shader GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping );
218 * @copydoc AtlasUploadObserver::UploadCompleted
220 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
221 * This callback is the place to add the renderer as it would be called once the loading is finished.
223 virtual void UploadCompleted();
228 * @brief Applies the image to the texture set used for this renderer
230 * @param[in] image The Image to apply to the texture set used for this renderer
232 void ApplyImageToSampler( const Image& image );
235 * @brief Initializes the Dali::Renderer from an image url string
237 * @param[in] imageUrl The image url string to intialize this ImageVisual from
239 void InitializeRenderer( const std::string& imageUrl );
242 * @brief Initializes the Dali::Renderer from an image handle
244 * @param[in] image The image handle to intialize this ImageVisual from
246 void InitializeRenderer( const Image& image );
249 * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
250 * @param[in] textures to use
252 void CreateRenderer( TextureSet& textures );
255 * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
256 * @param NativeImageRenderer
258 void CreateNativeImageRenderer( NativeImage& nativeImage );
261 * @brief Query whether resources requires to be loaded synchronously.
262 * @return Returns true if synchronous resource loading is required, false otherwise.
264 bool IsSynchronousResourceLoading() const;
267 * @brief Load the resource synchronously
269 void LoadResourceSynchronously();
272 * Creates the texture set and adds the texture to it
273 * @param[out] textureRect The texture area of the texture in the atlas.
274 * @param[in] url The URL of the image resource to use.
275 * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
276 * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
277 * @return the texture set to use
279 TextureSet CreateTextureSet( Vector4& textureRect, const std::string& url, bool synchronousLoading, bool attemptAtlasing );
282 * Callback function of image resource loading succeed
283 * @param[in] image The Image content that we attempted to load from mImageUrl
285 void OnImageLoaded( ResourceImage image );
288 * Set the value to the uTextureRect uniform
289 * @param[in] textureRect The texture rectangular area.
291 void SetTextureRectUniform( const Vector4& textureRect );
294 * Clean the renderer from cache, and remove the image from atlas if it is not used anymore
296 void CleanCache(const std::string& url);
299 * Helper method to set individual values by index key.
300 * @param[in] index The index key of the value
301 * @param[in] value The value
303 void DoSetProperty( Property::Index index, const Property::Value& value );
310 WeakHandle<Actor> mPlacementActor;
311 std::string mImageUrl;
313 Dali::ImageDimensions mDesiredSize;
314 Dali::FittingMode::Type mFittingMode:3;
315 Dali::SamplingMode::Type mSamplingMode:4;
316 Dali::WrapMode::Type mWrapModeU:3;
317 Dali::WrapMode::Type mWrapModeV:3;
321 } // namespace Internal
323 } // namespace Toolkit
327 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */