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;
156 * @copydoc Visual::Base::CreateInstancePropertyMap
158 virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
163 * @brief Constructor with a URL.
165 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
167 * @param[in] factoryCache The VisualFactoryCache object
168 * @param[in] imageUrl The URL of the image resource to use
169 * @param[in] size The width and height to fit the loaded image to.
170 * @param[in] fittingMode The FittingMode of the resource to load
171 * @param[in] samplingMode The SamplingMode of the resource to load
173 ImageVisual( VisualFactoryCache& factoryCache,
174 const std::string& imageUrl,
175 ImageDimensions size,
176 FittingMode::Type fittingMode,
177 Dali::SamplingMode::Type samplingMode );
180 * @brief Constructor with an Image type.
182 * @param[in] factoryCache The VisualFactoryCache object
183 * @param[in] image The image to use
185 ImageVisual( VisualFactoryCache& factoryCache, const Image& image );
188 * @brief A reference counted object may only be deleted by calling Unreference().
190 virtual ~ImageVisual();
193 * @copydoc Visual::Base::DoSetProperties
195 virtual void DoSetProperties( const Property::Map& propertyMap );
198 * @copydoc Visual::Base::DoSetOnStage
200 virtual void DoSetOnStage( Actor& actor );
203 * @copydoc Visual::Base::DoSetOffStage
205 virtual void DoSetOffStage( Actor& actor );
208 * @copydoc Visual::Base::OnSetTransform
210 virtual void OnSetTransform();
215 * Get the standard image rendering shader.
216 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
217 * @param[in] atlasing Whether texture atlasing is applied.
218 * @param[in] defaultTextureWrapping Whether the default texture wrap mode is applied.
220 static Shader GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping );
223 * @copydoc AtlasUploadObserver::UploadCompleted
225 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
226 * This callback is the place to add the renderer as it would be called once the loading is finished.
228 virtual void UploadCompleted();
233 * @brief Applies the image to the texture set used for this renderer
235 * @param[in] image The Image to apply to the texture set used for this renderer
237 void ApplyImageToSampler( const Image& image );
240 * @brief Initializes the Dali::Renderer from an image url string
242 * @param[in] imageUrl The image url string to intialize this ImageVisual from
244 void InitializeRenderer( const std::string& imageUrl );
247 * @brief Initializes the Dali::Renderer from an image handle
249 * @param[in] image The image handle to intialize this ImageVisual from
251 void InitializeRenderer( const Image& image );
254 * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
255 * @param[in] textures to use
257 void CreateRenderer( TextureSet& textures );
260 * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
261 * @param NativeImageRenderer
263 void CreateNativeImageRenderer( NativeImage& nativeImage );
266 * @brief Query whether resources requires to be loaded synchronously.
267 * @return Returns true if synchronous resource loading is required, false otherwise.
269 bool IsSynchronousResourceLoading() const;
272 * @brief Load the resource synchronously
274 void LoadResourceSynchronously();
277 * Creates the texture set and adds the texture to it
278 * @param[out] textureRect The texture area of the texture in the atlas.
279 * @param[in] url The URL of the image resource to use.
280 * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
281 * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
282 * @return the texture set to use
284 TextureSet CreateTextureSet( Vector4& textureRect, const std::string& url, bool synchronousLoading, bool attemptAtlasing );
287 * Callback function of image resource loading succeed
288 * @param[in] image The Image content that we attempted to load from mImageUrl
290 void OnImageLoaded( ResourceImage image );
293 * Set the value to the uTextureRect uniform
294 * @param[in] textureRect The texture rectangular area.
296 void SetTextureRectUniform( const Vector4& textureRect );
299 * Remove the image from atlas if it is not used anymore.
301 void RemoveFromAtlas(const std::string& url);
304 * Helper method to set individual values by index key.
305 * @param[in] index The index key of the value
306 * @param[in] value The value
308 void DoSetProperty( Property::Index index, const Property::Value& value );
315 WeakHandle<Actor> mPlacementActor;
316 std::string mImageUrl;
318 Dali::ImageDimensions mDesiredSize;
319 Dali::FittingMode::Type mFittingMode:3;
320 Dali::SamplingMode::Type mSamplingMode:4;
321 Dali::WrapMode::Type mWrapModeU:3;
322 Dali::WrapMode::Type mWrapModeV:3;
326 } // namespace Internal
328 } // namespace Toolkit
332 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */