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>
48 typedef IntrusivePtr< ImageVisual > ImageVisualPtr;
51 * The visual which renders an image to a quad geometry
53 * The following properties are optional
55 * | %Property Name | Type |
56 * |--------------------|-------------------|
58 * | alphaMaskUrl | STRING |
59 * | fittingMode | INTEGER OR STRING |
60 * | samplingMode | INTEGER OR STRING |
61 * | desiredWidth | INTEGER |
62 * | desiredHeight | INTEGER |
63 * | synchronousLoading | BOOLEAN |
64 * | pixelArea | VECTOR4 |
65 * | wrapModeU | INTEGER OR STRING |
66 * | wrapModeV | INTEGER OR STRING |
68 * where pixelArea is a rectangular area.
69 * In its Vector4 value, the first two elements indicate the top-left position of the area,
70 * and the last two elements are the area width and height respectively.
71 * If not specified, the default value is [0.0, 0.0, 1.0, 1.0], i.e. the entire area of the image.
73 * 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.
74 * Its value should be one of the following wrap mode:
80 * where imageFittingMode should be one of the following fitting modes:
87 * where imageSamplingMode should be one of the following sampling modes:
98 * If the Visual is in a LayerUI it will pixel align the image, using a Layer3D will disable pixel alignment.
99 * Changing layer behaviour between LayerUI to Layer3D whilst the visual is already staged will not have an effect.
101 class ImageVisual: public Visual::Base, public ConnectionTracker, public AtlasUploadObserver, public TextureUploadObserver
106 * @brief Create a new image visual with a URL.
108 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
110 * @param[in] factoryCache The VisualFactoryCache object
111 * @param[in] imageUrl The URL of the image resource to use
112 * @param[in] properties A Property::Map containing settings for this visual
113 * @param[in] size The width and height to fit the loaded image to.
114 * @param[in] fittingMode The FittingMode of the resource to load
115 * @param[in] samplingMode The SamplingMode of the resource to load
116 * @return A smart-pointer to the newly allocated visual.
118 static ImageVisualPtr New( VisualFactoryCache& factoryCache,
119 const VisualUrl& imageUrl,
120 const Property::Map& properties,
121 ImageDimensions size = ImageDimensions(),
122 FittingMode::Type fittingMode = FittingMode::DEFAULT,
123 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
126 * @brief Create a new image visual with a URL.
128 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
130 * @param[in] factoryCache The VisualFactoryCache object
131 * @param[in] imageUrl The URL of the image resource to use
132 * @param[in] size The width and height to fit the loaded image to.
133 * @param[in] fittingMode The FittingMode of the resource to load
134 * @param[in] samplingMode The SamplingMode of the resource to load
135 * @return A smart-pointer to the newly allocated visual.
137 static ImageVisualPtr New( VisualFactoryCache& factoryCache,
138 const VisualUrl& imageUrl,
139 ImageDimensions size = ImageDimensions(),
140 FittingMode::Type fittingMode = FittingMode::DEFAULT,
141 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
144 * @brief Create a new image visual with an Image type.
146 * @param[in] factoryCache The VisualFactoryCache object
147 * @param[in] image The image to use
149 static ImageVisualPtr New( VisualFactoryCache& factoryCache, const Image& image );
151 public: // from Visual
154 * @copydoc Visual::Base::GetNaturalSize
156 virtual void GetNaturalSize( Vector2& naturalSize );
159 * @copydoc Visual::Base::CreatePropertyMap
161 virtual void DoCreatePropertyMap( Property::Map& map ) const;
164 * @copydoc Visual::Base::CreateInstancePropertyMap
166 virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
171 * @brief Constructor with a URL.
173 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
175 * @param[in] factoryCache The VisualFactoryCache object
176 * @param[in] imageUrl The URL of the image resource to use
177 * @param[in] size The width and height to fit the loaded image to.
178 * @param[in] fittingMode The FittingMode of the resource to load
179 * @param[in] samplingMode The SamplingMode of the resource to load
181 ImageVisual( VisualFactoryCache& factoryCache,
182 const VisualUrl& imageUrl,
183 ImageDimensions size,
184 FittingMode::Type fittingMode,
185 Dali::SamplingMode::Type samplingMode );
188 * @brief Constructor with an Image type.
190 * @param[in] factoryCache The VisualFactoryCache object
191 * @param[in] image The image to use
193 ImageVisual( VisualFactoryCache& factoryCache, const Image& image );
196 * @brief A reference counted object may only be deleted by calling Unreference().
198 virtual ~ImageVisual();
201 * @copydoc Visual::Base::DoSetProperties
203 virtual void DoSetProperties( const Property::Map& propertyMap );
206 * @copydoc Visual::Base::DoSetOnStage
208 virtual void DoSetOnStage( Actor& actor );
211 * @copydoc Visual::Base::DoSetOffStage
213 virtual void DoSetOffStage( Actor& actor );
216 * @copydoc Visual::Base::OnSetTransform
218 virtual void OnSetTransform();
223 * Get the standard image rendering shader.
224 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
225 * @param[in] atlasing Whether texture atlasing is applied.
226 * @param[in] defaultTextureWrapping Whether the default texture wrap mode is applied.
228 static Shader GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping );
231 * @copydoc AtlasUploadObserver::UploadCompleted
233 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
234 * This callback is the place to add the renderer as it would be called once the loading is finished.
236 virtual void UploadCompleted();
239 * @copydoc TextureUploadObserver::UploadCompleted
241 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
242 * This callback is the place to add the renderer as it would be called once the loading is finished.
244 virtual void UploadComplete( bool success, int32_t textureId, TextureSet textureSet, bool usingAtlas, const Vector4& atlasRectangle );
249 * Allocate the mask data when a masking property is defined in the property map
251 void AllocateMaskData();
254 * @brief Applies the image to the texture set used for this renderer
256 * @param[in] image The Image to apply to the texture set used for this renderer
258 void ApplyImageToSampler( const Image& image );
261 * @brief Initializes the Dali::Renderer from the image url
263 void InitializeRenderer();
266 * @brief Initializes the Dali::Renderer from an image handle
268 * @param[in] image The image handle to intialize this ImageVisual from
270 void InitializeRenderer( const Image& image );
273 * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
274 * @param[in] textures to use
276 void CreateRenderer( TextureSet& textures );
279 * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
280 * @param NativeImageRenderer
282 void CreateNativeImageRenderer( NativeImage& nativeImage );
285 * @brief Query whether resources requires to be loaded synchronously.
286 * @return Returns true if synchronous resource loading is required, false otherwise.
288 bool IsSynchronousResourceLoading() const;
291 * Creates the texture set and adds the texture to it
292 * @param[out] textureRect The texture area of the texture in the atlas.
293 * @param[in] url The URL of the image resource to use.
294 * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
295 * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
296 * @return the texture set to use
298 TextureSet CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing );
301 * Set the value to the uTextureRect uniform
302 * @param[in] textureRect The texture rectangular area.
304 void SetTextureRectUniform( const Vector4& textureRect );
307 * Remove the texture if it is not used anymore.
309 void RemoveTexture(const std::string& url);
312 * Helper method to set individual values by index key.
313 * @param[in] index The index key of the value
314 * @param[in] value The value
316 void DoSetProperty( Property::Index index, const Property::Value& value );
322 WeakHandle<Actor> mPlacementActor;
324 TextureManager::MaskingDataPointer mMaskingData;
326 Dali::ImageDimensions mDesiredSize;
327 TextureManager::TextureId mTextureId;
329 Dali::FittingMode::Type mFittingMode:3;
330 Dali::SamplingMode::Type mSamplingMode:4;
331 Dali::WrapMode::Type mWrapModeU:3;
332 Dali::WrapMode::Type mWrapModeV:3;
333 bool mAttemptAtlasing; ///< If true will attempt atlasing, otherwise create unique texture
334 bool mLoadingStatus; ///< True if the texture is being loaded asynchronously, or false when it has loaded.
339 } // namespace Internal
341 } // namespace Toolkit
345 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */