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