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/devel-api/image-loader/atlas-upload-observer.h>
30 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
31 #include <dali-toolkit/internal/visuals/visual-url.h>
45 typedef IntrusivePtr< ImageVisual > ImageVisualPtr;
48 * The visual which renders an image to the control's quad
50 * The following properties are optional
52 * | %Property Name | Type |
53 * |--------------------|-------------------|
55 * | fittingMode | INTEGER OR STRING |
56 * | samplingMode | INTEGER OR STRING |
57 * | desiredWidth | INTEGER |
58 * | desiredHeight | INTEGER |
59 * | synchronousLoading | BOOLEAN |
60 * | pixelArea | VECTOR4 |
61 * | wrapModeU | INTEGER OR STRING |
62 * | wrapModeV | INTEGER OR STRING |
64 * where pixelArea is a rectangular area.
65 * In its Vector4 value, the first two elements indicate the top-left position of the area,
66 * and the last two elements are the area width and height respectively.
67 * If not specified, the default value is [0.0, 0.0, 1.0, 1.0], i.e. the entire area of the image.
69 * 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.
70 * Its value should be one of the following wrap mode:
76 * where imageFittingMode should be one of the following fitting modes:
83 * where imageSamplingMode should be one of the following sampling modes:
94 class ImageVisual: public Visual::Base, public ConnectionTracker, public AtlasUploadObserver
99 * @brief Create a new image visual with a URL.
101 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
103 * @param[in] factoryCache The VisualFactoryCache object
104 * @param[in] imageUrl The URL of the image resource to use
105 * @param[in] properties A Property::Map containing settings for this visual
106 * @param[in] size The width and height to fit the loaded image to.
107 * @param[in] fittingMode The FittingMode of the resource to load
108 * @param[in] samplingMode The SamplingMode of the resource to load
109 * @return A smart-pointer to the newly allocated visual.
111 static ImageVisualPtr New( VisualFactoryCache& factoryCache,
112 const VisualUrl& imageUrl,
113 const Property::Map& properties,
114 ImageDimensions size = ImageDimensions(),
115 FittingMode::Type fittingMode = FittingMode::DEFAULT,
116 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
119 * @brief Create a new image visual with a URL.
121 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
123 * @param[in] factoryCache The VisualFactoryCache object
124 * @param[in] imageUrl The URL of the image resource to use
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 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 an Image type.
139 * @param[in] factoryCache The VisualFactoryCache object
140 * @param[in] image The image to use
142 static ImageVisualPtr New( VisualFactoryCache& factoryCache, const Image& image );
144 public: // from Visual
147 * @copydoc Visual::Base::GetNaturalSize
149 virtual void GetNaturalSize( Vector2& naturalSize );
152 * @copydoc Visual::Base::CreatePropertyMap
154 virtual void DoCreatePropertyMap( Property::Map& map ) const;
157 * @copydoc Visual::Base::CreateInstancePropertyMap
159 virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
164 * @brief Constructor with a URL.
166 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
168 * @param[in] factoryCache The VisualFactoryCache object
169 * @param[in] imageUrl The URL of the image resource to use
170 * @param[in] size The width and height to fit the loaded image to.
171 * @param[in] fittingMode The FittingMode of the resource to load
172 * @param[in] samplingMode The SamplingMode of the resource to load
174 ImageVisual( VisualFactoryCache& factoryCache,
175 const VisualUrl& imageUrl,
176 ImageDimensions size,
177 FittingMode::Type fittingMode,
178 Dali::SamplingMode::Type samplingMode );
181 * @brief Constructor with an Image type.
183 * @param[in] factoryCache The VisualFactoryCache object
184 * @param[in] image The image to use
186 ImageVisual( VisualFactoryCache& factoryCache, const Image& image );
189 * @brief A reference counted object may only be deleted by calling Unreference().
191 virtual ~ImageVisual();
194 * @copydoc Visual::Base::DoSetProperties
196 virtual void DoSetProperties( const Property::Map& propertyMap );
199 * @copydoc Visual::Base::DoSetOnStage
201 virtual void DoSetOnStage( Actor& actor );
204 * @copydoc Visual::Base::DoSetOffStage
206 virtual void DoSetOffStage( Actor& actor );
209 * @copydoc Visual::Base::OnSetTransform
211 virtual void OnSetTransform();
216 * Get the standard image rendering shader.
217 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
218 * @param[in] atlasing Whether texture atlasing is applied.
219 * @param[in] defaultTextureWrapping Whether the default texture wrap mode is applied.
221 static Shader GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping );
224 * @copydoc AtlasUploadObserver::UploadCompleted
226 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
227 * This callback is the place to add the renderer as it would be called once the loading is finished.
229 virtual void UploadCompleted();
234 * @brief Applies the image to the texture set used for this renderer
236 * @param[in] image The Image to apply to the texture set used for this renderer
238 void ApplyImageToSampler( const Image& image );
241 * @brief Initializes the Dali::Renderer from the image url
243 void InitializeRenderer();
246 * @brief Initializes the Dali::Renderer from an image handle
248 * @param[in] image The image handle to intialize this ImageVisual from
250 void InitializeRenderer( const Image& image );
253 * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
254 * @param[in] textures to use
256 void CreateRenderer( TextureSet& textures );
259 * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
260 * @param NativeImageRenderer
262 void CreateNativeImageRenderer( NativeImage& nativeImage );
265 * @brief Query whether resources requires to be loaded synchronously.
266 * @return Returns true if synchronous resource loading is required, false otherwise.
268 bool IsSynchronousResourceLoading() const;
271 * @brief Load the resource synchronously
273 void LoadResourceSynchronously();
276 * Creates the texture set and adds the texture to it
277 * @param[out] textureRect The texture area of the texture in the atlas.
278 * @param[in] url The URL of the image resource to use.
279 * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
280 * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
281 * @return the texture set to use
283 TextureSet CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing );
286 * Callback function of image resource loading succeed
287 * @param[in] image The Image content that we attempted to load from mImageUrl
289 void OnImageLoaded( ResourceImage image );
292 * Set the value to the uTextureRect uniform
293 * @param[in] textureRect The texture rectangular area.
295 void SetTextureRectUniform( const Vector4& textureRect );
298 * Remove the image from atlas if it is not used anymore.
300 void RemoveFromAtlas(const std::string& url);
303 * Helper method to set individual values by index key.
304 * @param[in] index The index key of the value
305 * @param[in] value The value
307 void DoSetProperty( Property::Index index, const Property::Value& value );
314 WeakHandle<Actor> mPlacementActor;
317 Dali::ImageDimensions mDesiredSize;
318 Dali::FittingMode::Type mFittingMode:3;
319 Dali::SamplingMode::Type mSamplingMode:4;
320 Dali::WrapMode::Type mWrapModeU:3;
321 Dali::WrapMode::Type mWrapModeV:3;
323 bool mAttemptAtlasing:1; // If true will attempt atlasing, otherwise create unique texture
327 } // namespace Internal
329 } // namespace Toolkit
333 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */