Merge "Add Visual descriptions to generate doxygen page" into devel/master
[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 protected:
156
157   /**
158    * @brief Constructor with a URL.
159    *
160    * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
161    *
162    * @param[in] factoryCache The VisualFactoryCache object
163    * @param[in] imageUrl The URL of the image resource to use
164    * @param[in] size The width and height to fit the loaded image to.
165    * @param[in] fittingMode The FittingMode of the resource to load
166    * @param[in] samplingMode The SamplingMode of the resource to load
167    */
168   ImageVisual( VisualFactoryCache& factoryCache,
169                const std::string& imageUrl,
170                ImageDimensions size,
171                FittingMode::Type fittingMode,
172                Dali::SamplingMode::Type samplingMode );
173
174   /**
175    * @brief Constructor with an Image type.
176    *
177    * @param[in] factoryCache The VisualFactoryCache object
178    * @param[in] image The image to use
179    */
180   ImageVisual( VisualFactoryCache& factoryCache, const Image& image );
181
182   /**
183    * @brief A reference counted object may only be deleted by calling Unreference().
184    */
185   virtual ~ImageVisual();
186
187   /**
188    * @copydoc Visual::Base::DoSetProperties
189    */
190   virtual void DoSetProperties( const Property::Map& propertyMap );
191
192   /**
193    * @copydoc Visual::Base::DoSetOnStage
194    */
195   virtual void DoSetOnStage( Actor& actor );
196
197   /**
198    * @copydoc Visual::Base::DoSetOffStage
199    */
200   virtual void DoSetOffStage( Actor& actor );
201
202   /**
203    * @copydoc Visual::Base::OnSetTransform
204    */
205   virtual void OnSetTransform();
206
207 public:
208
209   /**
210    * Get the standard image rendering shader.
211    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
212    * @param[in] atlasing Whether texture atlasing is applied.
213    * @param[in] defaultTextureWrapping Whether the default texture wrap mode is applied.
214    */
215   static Shader GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping );
216
217   /**
218    * @copydoc AtlasUploadObserver::UploadCompleted
219    *
220    * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
221    * This callback is the place to add the renderer as it would be called once the loading is finished.
222    */
223   virtual void UploadCompleted();
224
225 private:
226
227   /**
228    * @brief Applies the image to the texture set used for this renderer
229    *
230    * @param[in] image The Image to apply to the texture set used for this renderer
231    */
232   void ApplyImageToSampler( const Image& image );
233
234   /**
235    * @brief Initializes the Dali::Renderer from an image url string
236    *
237    * @param[in] imageUrl The image url string to intialize this ImageVisual from
238    */
239   void InitializeRenderer( const std::string& imageUrl );
240
241   /**
242    * @brief Initializes the Dali::Renderer from an image handle
243    *
244    * @param[in] image The image handle to intialize this ImageVisual from
245    */
246   void InitializeRenderer( const Image& image );
247
248   /**
249    * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
250    * @param[in] textures to use
251    */
252   void CreateRenderer( TextureSet& textures );
253
254   /**
255    * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
256    * @param NativeImageRenderer
257    */
258   void CreateNativeImageRenderer( NativeImage& nativeImage );
259
260   /**
261    * @brief Query whether resources requires to be loaded synchronously.
262    * @return Returns true if synchronous resource loading is required, false otherwise.
263    */
264   bool IsSynchronousResourceLoading() const;
265
266   /**
267    * @brief Load the resource synchronously
268    */
269   void LoadResourceSynchronously();
270
271   /**
272    * Creates the texture set and adds the texture to it
273    * @param[out] textureRect The texture area of the texture in the atlas.
274    * @param[in] url The URL of the image resource to use.
275    * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
276    * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
277    * @return the texture set to use
278    */
279   TextureSet CreateTextureSet( Vector4& textureRect, const std::string& url, bool synchronousLoading, bool attemptAtlasing );
280
281   /**
282    * Callback function of image resource loading succeed
283    * @param[in] image The Image content that we attempted to load from mImageUrl
284    */
285   void OnImageLoaded( ResourceImage image );
286
287   /**
288    * Set the value to the uTextureRect uniform
289    * @param[in] textureRect The texture rectangular area.
290    */
291   void SetTextureRectUniform( const Vector4& textureRect  );
292
293   /**
294    * Clean the renderer from cache, and remove the image from atlas if it is not used anymore
295    */
296   void CleanCache(const std::string& url);
297
298   /**
299    * Helper method to set individual values by index key.
300    * @param[in] index The index key of the value
301    * @param[in] value The value
302    */
303   void DoSetProperty( Property::Index index, const Property::Value& value );
304
305 private:
306
307   Image mImage;
308   PixelData mPixels;
309   Vector4 mPixelArea;
310   WeakHandle<Actor> mPlacementActor;
311   std::string mImageUrl;
312
313   Dali::ImageDimensions mDesiredSize;
314   Dali::FittingMode::Type mFittingMode:3;
315   Dali::SamplingMode::Type mSamplingMode:4;
316   Dali::WrapMode::Type mWrapModeU:3;
317   Dali::WrapMode::Type mWrapModeV:3;
318
319 };
320
321 } // namespace Internal
322
323 } // namespace Toolkit
324
325 } // namespace Dali
326
327 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */