1 #ifndef DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H
5 * Copyright (c) 2015 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-toolkit/internal/visuals/visual-base-impl.h>
23 #include <dali-toolkit/devel-api/image-loader/atlas-upload-observer.h>
26 #include <dali/public-api/images/image.h>
27 #include <dali/public-api/images/image-operations.h>
28 #include <dali/public-api/images/resource-image.h>
29 #include <dali/devel-api/object/weak-handle.h>
43 typedef IntrusivePtr< ImageVisual > ImageVisualPtr;
46 * The visual which renders an image to the control's quad
48 * The following properties are optional
50 * | %Property Name | Type |
51 * |--------------------|------------------|
53 * | fittingMode | STRING |
54 * | samplingMode | STRING |
55 * | desiredWidth | INT |
56 * | desiredHeight | INT |
57 * | synchronousLoading | BOOLEAN |
59 * where imageFittingMode should be one of the following fitting modes:
66 * where imageSamplingMode should be one of the following sampling modes:
77 class ImageVisual: public Visual::Base, public ConnectionTracker, public AtlasUploadObserver
84 * @param[in] factoryCache The VisualFactoryCache object
86 ImageVisual( VisualFactoryCache& factoryCache );
89 * @brief A reference counted object may only be deleted by calling Unreference().
93 public: // from Visual
96 * @copydoc Visual::Base::GetNaturalSize
98 virtual void GetNaturalSize( Vector2& naturalSize ) const;
101 * @copydoc Visual::Base::CreatePropertyMap
103 virtual void DoCreatePropertyMap( Property::Map& map ) const;
106 * @copydoc Visual::Base::DoSetProperty
108 virtual void DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue );
111 * @copydoc Visual::Base::DoGetProperty
113 virtual Dali::Property::Value DoGetProperty( Dali::Property::Index index );
117 * @copydoc Visual::Base::DoInitialize
119 virtual void DoInitialize( Actor& actor, const Property::Map& propertyMap );
122 * @copydoc Visual::Base::DoSetOnStage
124 virtual void DoSetOnStage( Actor& actor );
127 * @copydoc Visual::Base::DoSetOffStage
129 virtual void DoSetOffStage( Actor& actor );
134 * Get the standard image rendering shader.
135 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
136 * @param[in] atlasing Whether texture atlasing is applied.
137 * @param[in] defaultTextureWrapping Whether the default texture wrap mode is applied.
139 static Shader GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping );
142 * @brief Sets the image of this visual to the resource at imageUrl
143 * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
145 * @param[in] actor The Actor the renderer is applied to if, empty if the renderer has not been applied to any Actor
146 * @param[in] imageUrl The URL of the image resource to use
147 * @param[in] size The width and height to fit the loaded image to.
148 * @param[in] fittingMode The FittingMode of the resource to load
149 * @param[in] samplingMode The SamplingMode of the resource to load
151 void SetImage( Actor& actor,
152 const std::string& imageUrl,
153 ImageDimensions size=ImageDimensions(),
154 FittingMode::Type fittingMode = FittingMode::DEFAULT,
155 Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
158 * @brief Sets the image to be rendered by this visual
160 * @param[in] actor The Actor the renderer is applied to if, empty if the renderer has not been applied to any Actor
161 * @param[in] image The image to use
163 void SetImage( Actor& actor, const Image& image );
166 * @copydoc AtlasUploadObserver::UploadCompleted
168 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
169 * This callback is the place to add the renderer as it would be called once the loading is finished.
171 virtual void UploadCompleted();
176 * @brief Applies the image to the texture set used for this renderer
178 * @param[in] image The Image to apply to the texture set used for this renderer
180 void ApplyImageToSampler( const Image& image );
183 * @brief Initializes the Dali::Renderer from an image url string
185 * @param[in] imageUrl The image url string to intialize this ImageVisual from
187 void InitializeRenderer( const std::string& imageUrl );
190 * @brief Initializes the Dali::Renderer from an image handle
192 * @param[in] image The image handle to intialize this ImageVisual from
194 void InitializeRenderer( const Image& image );
197 * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
199 * @return Returns the created Dali::Renderer
201 Renderer CreateRenderer() const;
204 * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
206 * @return Returns the created Dali::Renderer
208 Renderer CreateNativeImageRenderer() const;
211 * @brief Query whether resources requires to be loaded synchronously.
212 * @return Returns true if synchronoud resource loading is required, false otherwise.
214 bool IsSynchronousResourceLoading() const;
217 * @brief Do the synchronous resource loading
219 void DoSynchronousResourceLoading();
223 * @param[in] url The URL of the image resource to use.
224 * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
226 Image LoadImage( const std::string& url, bool synchronousLoading );
229 * Load the image and create a texture set to hold the texture, with automatic atlasing applied.
230 * @param [out] textureRect The texture area of the resource image in the atlas.
231 * @param[in] url The URL of the image resource to use.
232 * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
234 TextureSet CreateTextureSet( Vector4& textureRect, const std::string& url, bool synchronousLoading );
237 * Callback function of image resource loading succeed
238 * @param[in] image The Image content that we attempted to load from mImageUrl
240 void OnImageLoaded( ResourceImage image );
243 * Set the value to the uTextureRect uniform
244 * @param[in] textureRect The texture rectangular area.
246 void SetTextureRectUniform( const Vector4& textureRect );
249 * Clean the renderer from cache, and remove the image from atlas if it is not used anymore
251 void CleanCache(const std::string& url);
254 * Set shader code for nativeimage if it exists
256 void SetNativeFragmentShaderCode( Dali::NativeImage& nativeImage );
262 WeakHandle<Actor> mPlacementActor;
264 std::string mImageUrl;
265 Dali::ImageDimensions mDesiredSize;
266 Dali::FittingMode::Type mFittingMode;
267 Dali::SamplingMode::Type mSamplingMode;
268 Dali::WrapMode::Type mWrapModeU;
269 Dali::WrapMode::Type mWrapModeV;
271 std::string mNativeFragmentShaderCode;
272 bool mNativeImageFlag;
275 } // namespace Internal
277 } // namespace Toolkit
281 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */