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 * If the Visual is in a LayerUI it will pixel align the image, using a Layer3D will disable pixel alignment.
95 * Changing layer behaviour between LayerUI to Layer3D whilst the visual is already staged will not have an effect.
97 class ImageVisual: public Visual::Base, public ConnectionTracker, public AtlasUploadObserver
102 * @brief Create a new image visual with a URL.
104 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
106 * @param[in] factoryCache The VisualFactoryCache object
107 * @param[in] imageUrl The URL of the image resource to use
108 * @param[in] properties A Property::Map containing settings for this visual
109 * @param[in] size The width and height to fit the loaded image to.
110 * @param[in] fittingMode The FittingMode of the resource to load
111 * @param[in] samplingMode The SamplingMode of the resource to load
112 * @return A smart-pointer to the newly allocated visual.
114 static ImageVisualPtr New( VisualFactoryCache& factoryCache,
115 const VisualUrl& imageUrl,
116 const Property::Map& properties,
117 ImageDimensions size = ImageDimensions(),
118 FittingMode::Type fittingMode = FittingMode::DEFAULT,
119 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
122 * @brief Create a new image visual with a URL.
124 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
126 * @param[in] factoryCache The VisualFactoryCache object
127 * @param[in] imageUrl The URL of the image resource to use
128 * @param[in] size The width and height to fit the loaded image to.
129 * @param[in] fittingMode The FittingMode of the resource to load
130 * @param[in] samplingMode The SamplingMode of the resource to load
131 * @return A smart-pointer to the newly allocated visual.
133 static ImageVisualPtr New( VisualFactoryCache& factoryCache,
134 const VisualUrl& imageUrl,
135 ImageDimensions size = ImageDimensions(),
136 FittingMode::Type fittingMode = FittingMode::DEFAULT,
137 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
140 * @brief Create a new image visual with an Image type.
142 * @param[in] factoryCache The VisualFactoryCache object
143 * @param[in] image The image to use
145 static ImageVisualPtr New( VisualFactoryCache& factoryCache, const Image& image );
147 public: // from Visual
150 * @copydoc Visual::Base::GetNaturalSize
152 virtual void GetNaturalSize( Vector2& naturalSize );
155 * @copydoc Visual::Base::CreatePropertyMap
157 virtual void DoCreatePropertyMap( Property::Map& map ) const;
160 * @copydoc Visual::Base::CreateInstancePropertyMap
162 virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
167 * @brief Constructor with a URL.
169 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
171 * @param[in] factoryCache The VisualFactoryCache object
172 * @param[in] imageUrl The URL of the image resource to use
173 * @param[in] size The width and height to fit the loaded image to.
174 * @param[in] fittingMode The FittingMode of the resource to load
175 * @param[in] samplingMode The SamplingMode of the resource to load
177 ImageVisual( VisualFactoryCache& factoryCache,
178 const VisualUrl& imageUrl,
179 ImageDimensions size,
180 FittingMode::Type fittingMode,
181 Dali::SamplingMode::Type samplingMode );
184 * @brief Constructor with an Image type.
186 * @param[in] factoryCache The VisualFactoryCache object
187 * @param[in] image The image to use
189 ImageVisual( VisualFactoryCache& factoryCache, const Image& image );
192 * @brief A reference counted object may only be deleted by calling Unreference().
194 virtual ~ImageVisual();
197 * @copydoc Visual::Base::DoSetProperties
199 virtual void DoSetProperties( const Property::Map& propertyMap );
202 * @copydoc Visual::Base::DoSetOnStage
204 virtual void DoSetOnStage( Actor& actor );
207 * @copydoc Visual::Base::DoSetOffStage
209 virtual void DoSetOffStage( Actor& actor );
212 * @copydoc Visual::Base::OnSetTransform
214 virtual void OnSetTransform();
219 * Get the standard image rendering shader.
220 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
221 * @param[in] atlasing Whether texture atlasing is applied.
222 * @param[in] defaultTextureWrapping Whether the default texture wrap mode is applied.
224 static Shader GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping );
227 * @copydoc AtlasUploadObserver::UploadCompleted
229 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
230 * This callback is the place to add the renderer as it would be called once the loading is finished.
232 virtual void UploadCompleted();
237 * @brief Applies the image to the texture set used for this renderer
239 * @param[in] image The Image to apply to the texture set used for this renderer
241 void ApplyImageToSampler( const Image& image );
244 * @brief Initializes the Dali::Renderer from the image url
246 void InitializeRenderer();
249 * @brief Initializes the Dali::Renderer from an image handle
251 * @param[in] image The image handle to intialize this ImageVisual from
253 void InitializeRenderer( const Image& image );
256 * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
257 * @param[in] textures to use
259 void CreateRenderer( TextureSet& textures );
262 * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
263 * @param NativeImageRenderer
265 void CreateNativeImageRenderer( NativeImage& nativeImage );
268 * @brief Query whether resources requires to be loaded synchronously.
269 * @return Returns true if synchronous resource loading is required, false otherwise.
271 bool IsSynchronousResourceLoading() const;
274 * @brief Load the resource synchronously
276 void LoadResourceSynchronously();
279 * Creates the texture set and adds the texture to it
280 * @param[out] textureRect The texture area of the texture in the atlas.
281 * @param[in] url The URL of the image resource to use.
282 * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
283 * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
284 * @return the texture set to use
286 TextureSet CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing );
289 * Callback function of image resource loading succeed
290 * @param[in] image The Image content that we attempted to load from mImageUrl
292 void OnImageLoaded( ResourceImage image );
295 * Set the value to the uTextureRect uniform
296 * @param[in] textureRect The texture rectangular area.
298 void SetTextureRectUniform( const Vector4& textureRect );
301 * Remove the image from atlas if it is not used anymore.
303 void RemoveFromAtlas(const std::string& url);
306 * Helper method to set individual values by index key.
307 * @param[in] index The index key of the value
308 * @param[in] value The value
310 void DoSetProperty( Property::Index index, const Property::Value& value );
317 WeakHandle<Actor> mPlacementActor;
320 Dali::ImageDimensions mDesiredSize;
321 Dali::FittingMode::Type mFittingMode:3;
322 Dali::SamplingMode::Type mSamplingMode:4;
323 Dali::WrapMode::Type mWrapModeU:3;
324 Dali::WrapMode::Type mWrapModeV:3;
326 bool mAttemptAtlasing:1; // If true will attempt atlasing, otherwise create unique texture
330 } // namespace Internal
332 } // namespace Toolkit
336 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */