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 | STRING |
55 * | samplingMode | STRING |
56 * | desiredWidth | INT |
57 * | desiredHeight | INT |
58 * | synchronousLoading | BOOLEAN |
60 * where imageFittingMode should be one of the following fitting modes:
67 * where imageSamplingMode should be one of the following sampling modes:
78 class ImageVisual: public Visual::Base, public ConnectionTracker, public AtlasUploadObserver
83 * @brief Create a new image visual with a URL.
85 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
87 * @param[in] factoryCache The VisualFactoryCache object
88 * @param[in] imageUrl The URL of the image resource to use
89 * @param[in] size The width and height to fit the loaded image to.
90 * @param[in] fittingMode The FittingMode of the resource to load
91 * @param[in] samplingMode The SamplingMode of the resource to load
93 static ImageVisualPtr New( VisualFactoryCache& factoryCache,
94 const std::string& imageUrl,
95 ImageDimensions size = ImageDimensions(),
96 FittingMode::Type fittingMode = FittingMode::DEFAULT,
97 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
100 * @brief Create a new image visual with an Image type.
102 * @param[in] factoryCache The VisualFactoryCache object
103 * @param[in] image The image to use
105 static ImageVisualPtr New( VisualFactoryCache& factoryCache, const Image& image );
107 public: // from Visual
110 * @copydoc Visual::Base::GetNaturalSize
112 virtual void GetNaturalSize( Vector2& naturalSize ) const;
115 * @copydoc Visual::Base::CreatePropertyMap
117 virtual void DoCreatePropertyMap( Property::Map& map ) const;
120 * @copydoc Visual::Base::DoSetProperty
122 virtual void DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue );
125 * @copydoc Visual::Base::DoGetProperty
127 virtual Dali::Property::Value DoGetProperty( Dali::Property::Index index );
132 * @brief Constructor with a URL.
134 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
136 * @param[in] factoryCache The VisualFactoryCache object
137 * @param[in] imageUrl The URL of the image resource to use
138 * @param[in] size The width and height to fit the loaded image to.
139 * @param[in] fittingMode The FittingMode of the resource to load
140 * @param[in] samplingMode The SamplingMode of the resource to load
142 ImageVisual( VisualFactoryCache& factoryCache,
143 const std::string& imageUrl,
144 ImageDimensions size,
145 FittingMode::Type fittingMode,
146 Dali::SamplingMode::Type samplingMode );
149 * @brief Constructor with an Image type.
151 * @param[in] factoryCache The VisualFactoryCache object
152 * @param[in] image The image to use
154 ImageVisual( VisualFactoryCache& factoryCache, const Image& image );
157 * @brief A reference counted object may only be deleted by calling Unreference().
159 virtual ~ImageVisual();
162 * @copydoc Visual::Base::DoSetProperties
164 virtual void DoSetProperties( const Property::Map& propertyMap );
167 * @copydoc Visual::Base::DoSetOnStage
169 virtual void DoSetOnStage( Actor& actor );
172 * @copydoc Visual::Base::DoSetOffStage
174 virtual void DoSetOffStage( Actor& actor );
177 * @copydoc Visual::Base::OnSetTransform
179 virtual void OnSetTransform();
184 * Get the standard image rendering shader.
185 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
186 * @param[in] atlasing Whether texture atlasing is applied.
187 * @param[in] defaultTextureWrapping Whether the default texture wrap mode is applied.
189 static Shader GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping );
192 * @copydoc AtlasUploadObserver::UploadCompleted
194 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
195 * This callback is the place to add the renderer as it would be called once the loading is finished.
197 virtual void UploadCompleted();
202 * @brief Applies the image to the texture set used for this renderer
204 * @param[in] image The Image to apply to the texture set used for this renderer
206 void ApplyImageToSampler( const Image& image );
209 * @brief Initializes the Dali::Renderer from an image url string
211 * @param[in] imageUrl The image url string to intialize this ImageVisual from
213 void InitializeRenderer( const std::string& imageUrl );
216 * @brief Initializes the Dali::Renderer from an image handle
218 * @param[in] image The image handle to intialize this ImageVisual from
220 void InitializeRenderer( const Image& image );
223 * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
224 * @param[in] textures to use
226 void CreateRenderer( TextureSet& textures );
229 * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
230 * @param NativeImageRenderer
232 void CreateNativeImageRenderer( NativeImage& nativeImage );
235 * @brief Query whether resources requires to be loaded synchronously.
236 * @return Returns true if synchronous resource loading is required, false otherwise.
238 bool IsSynchronousResourceLoading() const;
241 * @brief Load the resource synchronously
243 void LoadResourceSynchronously();
246 * Creates the texture set and adds the texture to it
247 * @param[out] textureRect The texture area of the texture in the atlas.
248 * @param[in] url The URL of the image resource to use.
249 * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
250 * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
251 * @return the texture set to use
253 TextureSet CreateTextureSet( Vector4& textureRect, const std::string& url, bool synchronousLoading, bool attemptAtlasing );
256 * Callback function of image resource loading succeed
257 * @param[in] image The Image content that we attempted to load from mImageUrl
259 void OnImageLoaded( ResourceImage image );
262 * Set the value to the uTextureRect uniform
263 * @param[in] textureRect The texture rectangular area.
265 void SetTextureRectUniform( const Vector4& textureRect );
268 * Clean the renderer from cache, and remove the image from atlas if it is not used anymore
270 void CleanCache(const std::string& url);
277 WeakHandle<Actor> mPlacementActor;
278 std::string mImageUrl;
280 Dali::ImageDimensions mDesiredSize;
281 Dali::FittingMode::Type mFittingMode:3;
282 Dali::SamplingMode::Type mSamplingMode:4;
283 Dali::WrapMode::Type mWrapModeU:3;
284 Dali::WrapMode::Type mWrapModeV:3;
288 } // namespace Internal
290 } // namespace Toolkit
294 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */