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