bf7104869e9d59545ba95823ef496a5f7ec1352f
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / image / image-visual.h
1 #ifndef DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H
3
4 /*
5  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
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>
27
28 // INTERNAL INCLUDES
29 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
30 #include <dali-toolkit/devel-api/image-loader/atlas-upload-observer.h>
31
32 namespace Dali
33 {
34
35 class NativeImage;
36
37 namespace Toolkit
38 {
39
40 namespace Internal
41 {
42
43 class ImageVisual;
44 typedef IntrusivePtr< ImageVisual > ImageVisualPtr;
45
46 /**
47  * The visual which renders an image to the control's quad
48  *
49  * The following properties are optional
50  *
51  * | %Property Name     | Type              |
52  * |--------------------|-------------------|
53  * | url                | STRING            |
54  * | fittingMode        | INTEGER OR STRING |
55  * | samplingMode       | INTEGER OR STRING |
56  * | desiredWidth       | INTEGER           |
57  * | desiredHeight      | INTEGER           |
58  * | synchronousLoading | BOOLEAN           |
59  * | pixelArea          | VECTOR4           |
60  * | wrapModeU          | INTEGER OR STRING |
61  * | wrapModeV          | INTEGER OR STRING |
62  *
63  * where pixelArea is a rectangular area.
64  * In its Vector4 value, the first two elements indicate the top-left position of the area,
65  * and the last two elements are the area width and height respectively.
66  * If not specified, the default value is [0.0, 0.0, 1.0, 1.0], i.e. the entire area of the image.
67  *
68  * 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.
69  * Its value should be one of the following wrap mode:
70  *   "DEFAULT"
71  *   "CLAMP_TO_EDGE"
72  *   "REPEAT"
73  *   "MIRRORED_REPEAT"
74  *
75  * where imageFittingMode should be one of the following fitting modes:
76  *   "SHRINK_TO_FIT"
77  *   "SCALE_TO_FIT"
78  *   "FIT_WIDTH"
79  *   "FIT_HEIGHT"
80  *   "DEFAULT"
81  *
82  * where imageSamplingMode should be one of the following sampling modes:
83  *   "BOX"
84  *   "NEAREST"
85  *   "LINEAR"
86  *   "BOX_THEN_NEAREST"
87  *   "BOX_THEN_LINEAR"
88  *   "NO_FILTER"
89  *   "DONT_CARE"
90  *   "DEFAULT"
91  *
92  */
93 class ImageVisual: public Visual::Base, public ConnectionTracker, public AtlasUploadObserver
94 {
95 public:
96
97   /**
98    * @brief Create a new image visual with a URL.
99    *
100    * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
101    *
102    * @param[in] factoryCache The VisualFactoryCache object
103    * @param[in] imageUrl The URL of the image resource to use
104    * @param[in] properties A Property::Map containing settings for this visual
105    * @param[in] size The width and height to fit the loaded image to.
106    * @param[in] fittingMode The FittingMode of the resource to load
107    * @param[in] samplingMode The SamplingMode of the resource to load
108    * @return A smart-pointer to the newly allocated visual.
109    */
110   static ImageVisualPtr New( VisualFactoryCache& factoryCache,
111                              const std::string& imageUrl,
112                              const Property::Map& properties,
113                              ImageDimensions size = ImageDimensions(),
114                              FittingMode::Type fittingMode = FittingMode::DEFAULT,
115                              Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
116
117   /**
118    * @brief Create a new image visual with a URL.
119    *
120    * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
121    *
122    * @param[in] factoryCache The VisualFactoryCache object
123    * @param[in] imageUrl The URL of the image resource to use
124    * @param[in] size The width and height to fit the loaded image to.
125    * @param[in] fittingMode The FittingMode of the resource to load
126    * @param[in] samplingMode The SamplingMode of the resource to load
127    * @return A smart-pointer to the newly allocated visual.
128    */
129   static ImageVisualPtr New( VisualFactoryCache& factoryCache,
130                              const std::string& imageUrl,
131                              ImageDimensions size = ImageDimensions(),
132                              FittingMode::Type fittingMode = FittingMode::DEFAULT,
133                              Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
134
135   /**
136    * @brief Create a new image visual with an Image type.
137    *
138    * @param[in] factoryCache The VisualFactoryCache object
139    * @param[in] image The image to use
140    */
141   static ImageVisualPtr New( VisualFactoryCache& factoryCache, const Image& image );
142
143 public:  // from Visual
144
145   /**
146    * @copydoc Visual::Base::GetNaturalSize
147    */
148   virtual void GetNaturalSize( Vector2& naturalSize );
149
150   /**
151    * @copydoc Visual::Base::CreatePropertyMap
152    */
153   virtual void DoCreatePropertyMap( Property::Map& map ) const;
154
155   /**
156    * @copydoc Visual::Base::CreateInstancePropertyMap
157    */
158   virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
159
160 protected:
161
162   /**
163    * @brief Constructor with a URL.
164    *
165    * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
166    *
167    * @param[in] factoryCache The VisualFactoryCache object
168    * @param[in] imageUrl The URL of the image resource to use
169    * @param[in] size The width and height to fit the loaded image to.
170    * @param[in] fittingMode The FittingMode of the resource to load
171    * @param[in] samplingMode The SamplingMode of the resource to load
172    */
173   ImageVisual( VisualFactoryCache& factoryCache,
174                const std::string& imageUrl,
175                ImageDimensions size,
176                FittingMode::Type fittingMode,
177                Dali::SamplingMode::Type samplingMode );
178
179   /**
180    * @brief Constructor with an Image type.
181    *
182    * @param[in] factoryCache The VisualFactoryCache object
183    * @param[in] image The image to use
184    */
185   ImageVisual( VisualFactoryCache& factoryCache, const Image& image );
186
187   /**
188    * @brief A reference counted object may only be deleted by calling Unreference().
189    */
190   virtual ~ImageVisual();
191
192   /**
193    * @copydoc Visual::Base::DoSetProperties
194    */
195   virtual void DoSetProperties( const Property::Map& propertyMap );
196
197   /**
198    * @copydoc Visual::Base::DoSetOnStage
199    */
200   virtual void DoSetOnStage( Actor& actor );
201
202   /**
203    * @copydoc Visual::Base::DoSetOffStage
204    */
205   virtual void DoSetOffStage( Actor& actor );
206
207   /**
208    * @copydoc Visual::Base::OnSetTransform
209    */
210   virtual void OnSetTransform();
211
212 public:
213
214   /**
215    * Get the standard image rendering shader.
216    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
217    * @param[in] atlasing Whether texture atlasing is applied.
218    * @param[in] defaultTextureWrapping Whether the default texture wrap mode is applied.
219    */
220   static Shader GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping );
221
222   /**
223    * @copydoc AtlasUploadObserver::UploadCompleted
224    *
225    * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
226    * This callback is the place to add the renderer as it would be called once the loading is finished.
227    */
228   virtual void UploadCompleted();
229
230 private:
231
232   /**
233    * @brief Applies the image to the texture set used for this renderer
234    *
235    * @param[in] image The Image to apply to the texture set used for this renderer
236    */
237   void ApplyImageToSampler( const Image& image );
238
239   /**
240    * @brief Initializes the Dali::Renderer from an image url string
241    *
242    * @param[in] imageUrl The image url string to intialize this ImageVisual from
243    */
244   void InitializeRenderer( const std::string& imageUrl );
245
246   /**
247    * @brief Initializes the Dali::Renderer from an image handle
248    *
249    * @param[in] image The image handle to intialize this ImageVisual from
250    */
251   void InitializeRenderer( const Image& image );
252
253   /**
254    * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
255    * @param[in] textures to use
256    */
257   void CreateRenderer( TextureSet& textures );
258
259   /**
260    * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
261    * @param NativeImageRenderer
262    */
263   void CreateNativeImageRenderer( NativeImage& nativeImage );
264
265   /**
266    * @brief Query whether resources requires to be loaded synchronously.
267    * @return Returns true if synchronous resource loading is required, false otherwise.
268    */
269   bool IsSynchronousResourceLoading() const;
270
271   /**
272    * @brief Load the resource synchronously
273    */
274   void LoadResourceSynchronously();
275
276   /**
277    * Creates the texture set and adds the texture to it
278    * @param[out] textureRect The texture area of the texture in the atlas.
279    * @param[in] url The URL of the image resource to use.
280    * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
281    * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
282    * @return the texture set to use
283    */
284   TextureSet CreateTextureSet( Vector4& textureRect, const std::string& url, bool synchronousLoading, bool attemptAtlasing );
285
286   /**
287    * Callback function of image resource loading succeed
288    * @param[in] image The Image content that we attempted to load from mImageUrl
289    */
290   void OnImageLoaded( ResourceImage image );
291
292   /**
293    * Set the value to the uTextureRect uniform
294    * @param[in] textureRect The texture rectangular area.
295    */
296   void SetTextureRectUniform( const Vector4& textureRect  );
297
298   /**
299    * Remove the image from atlas if it is not used anymore.
300    */
301   void RemoveFromAtlas(const std::string& url);
302
303   /**
304    * Helper method to set individual values by index key.
305    * @param[in] index The index key of the value
306    * @param[in] value The value
307    */
308   void DoSetProperty( Property::Index index, const Property::Value& value );
309
310 private:
311
312   Image mImage;
313   PixelData mPixels;
314   Vector4 mPixelArea;
315   WeakHandle<Actor> mPlacementActor;
316   std::string mImageUrl;
317
318   Dali::ImageDimensions mDesiredSize;
319   Dali::FittingMode::Type mFittingMode:3;
320   Dali::SamplingMode::Type mSamplingMode:4;
321   Dali::WrapMode::Type mWrapModeU:3;
322   Dali::WrapMode::Type mWrapModeV:3;
323
324 };
325
326 } // namespace Internal
327
328 } // namespace Toolkit
329
330 } // namespace Dali
331
332 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */