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