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