[dali_1.2.63] Merge branch '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) 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  * | loadPolicy            | INTEGER OR STRING |
69  * | releasePolicy         | INTEGER OR STRING |
70  * | orientationCorrection | BOOLEAN           |
71  *
72  * where pixelArea is a rectangular area.
73  * In its Vector4 value, the first two elements indicate the top-left position of the area,
74  * and the last two elements are the area width and height respectively.
75  * If not specified, the default value is [0.0, 0.0, 1.0, 1.0], i.e. the entire area of the image.
76  *
77  * 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.
78  * Its value should be one of the following wrap mode:
79  *   "DEFAULT"
80  *   "CLAMP_TO_EDGE"
81  *   "REPEAT"
82  *   "MIRRORED_REPEAT"
83  *
84  * where imageFittingMode should be one of the following fitting modes:
85  *   "SHRINK_TO_FIT"
86  *   "SCALE_TO_FIT"
87  *   "FIT_WIDTH"
88  *   "FIT_HEIGHT"
89  *   "DEFAULT"
90  *
91  * where imageSamplingMode should be one of the following sampling modes:
92  *   "BOX"
93  *   "NEAREST"
94  *   "LINEAR"
95  *   "BOX_THEN_NEAREST"
96  *   "BOX_THEN_LINEAR"
97  *   "NO_FILTER"
98  *   "DONT_CARE"
99  *   "DEFAULT"
100  *
101  * where loadPolicy should be one of the following image loading modes
102  *   "IMMEDIATE"   // Loads image even if visual not attached to stage yet
103  *   "ATTACHED"    // Only loads image once visual is attached to stage
104  *
105  * where releasePolicy should be one of the following policies for when to cache the image
106  *   "DETACHED"    //  Release image from cache when visual detached from stage
107  *   "DESTROYED"   //  Keep image in cache until the visual is destroyed
108  *   "NEVER"       //  Keep image in cache until application ends.
109  *
110  * If the Visual is in a LayerUI it will pixel align the image, using a Layer3D will disable pixel alignment.
111  * Changing layer behaviour between LayerUI to Layer3D whilst the visual is already staged will not have an effect.
112  */
113 class ImageVisual: public Visual::Base, public ConnectionTracker, public AtlasUploadObserver, public TextureUploadObserver
114 {
115 public:
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] properties A Property::Map containing settings for this visual
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                              const Property::Map& properties,
133                              ImageDimensions size = ImageDimensions(),
134                              FittingMode::Type fittingMode = FittingMode::DEFAULT,
135                              Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
136
137   /**
138    * @brief Create a new image visual with a URL.
139    *
140    * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
141    *
142    * @param[in] factoryCache The VisualFactoryCache object
143    * @param[in] imageUrl The URL of the image resource to use
144    * @param[in] size The width and height to fit the loaded image to.
145    * @param[in] fittingMode The FittingMode of the resource to load
146    * @param[in] samplingMode The SamplingMode of the resource to load
147    * @return A smart-pointer to the newly allocated visual.
148    */
149   static ImageVisualPtr New( VisualFactoryCache& factoryCache,
150                              const VisualUrl& imageUrl,
151                              ImageDimensions size = ImageDimensions(),
152                              FittingMode::Type fittingMode = FittingMode::DEFAULT,
153                              Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
154
155   /**
156    * @brief Create a new image visual with an Image type.
157    *
158    * @param[in] factoryCache The VisualFactoryCache object
159    * @param[in] image The image to use
160    */
161   static ImageVisualPtr New( VisualFactoryCache& factoryCache, const Image& image );
162
163 public:  // from Visual
164
165   /**
166    * @copydoc Visual::Base::GetNaturalSize
167    */
168   virtual void GetNaturalSize( Vector2& naturalSize );
169
170   /**
171    * @copydoc Visual::Base::CreatePropertyMap
172    */
173   virtual void DoCreatePropertyMap( Property::Map& map ) const;
174
175   /**
176    * @copydoc Visual::Base::CreateInstancePropertyMap
177    */
178   virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
179
180 protected:
181
182   /**
183    * @brief Constructor with a URL.
184    *
185    * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
186    *
187    * @param[in] factoryCache The VisualFactoryCache object
188    * @param[in] imageUrl The URL of the image resource to use
189    * @param[in] size The width and height to fit the loaded image to.
190    * @param[in] fittingMode The FittingMode of the resource to load
191    * @param[in] samplingMode The SamplingMode of the resource to load
192    */
193   ImageVisual( VisualFactoryCache& factoryCache,
194                const VisualUrl& imageUrl,
195                ImageDimensions size,
196                FittingMode::Type fittingMode,
197                Dali::SamplingMode::Type samplingMode );
198
199   /**
200    * @brief Constructor with an Image type.
201    *
202    * @param[in] factoryCache The VisualFactoryCache object
203    * @param[in] image The image to use
204    */
205   ImageVisual( VisualFactoryCache& factoryCache, const Image& image );
206
207   /**
208    * @brief A reference counted object may only be deleted by calling Unreference().
209    */
210   virtual ~ImageVisual();
211
212   /**
213    * @copydoc Visual::Base::DoSetProperties
214    */
215   virtual void DoSetProperties( const Property::Map& propertyMap );
216
217   /**
218    * @copydoc Visual::Base::DoSetOnStage
219    */
220   virtual void DoSetOnStage( Actor& actor );
221
222   /**
223    * @copydoc Visual::Base::DoSetOffStage
224    */
225   virtual void DoSetOffStage( Actor& actor );
226
227   /**
228    * @copydoc Visual::Base::OnSetTransform
229    */
230   virtual void OnSetTransform();
231
232 public:
233
234   /**
235    * Get the standard image rendering shader.
236    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
237    * @param[in] atlasing Whether texture atlasing is applied.
238    * @param[in] defaultTextureWrapping Whether the default texture wrap mode is applied.
239    */
240   static Shader GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping );
241
242   /**
243    * @copydoc AtlasUploadObserver::UploadCompleted
244    *
245    * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
246    * This callback is the place to add the renderer as it would be called once the loading is finished.
247    */
248   virtual void UploadCompleted();
249
250   /**
251    * @copydoc TextureUploadObserver::UploadCompleted
252    *
253    * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
254    * This callback is the place to add the renderer as it would be called once the loading is finished.
255    */
256   virtual void UploadComplete( bool success, int32_t textureId, TextureSet textureSet, bool usingAtlas, const Vector4& atlasRectangle );
257
258 private:
259
260   /**
261    * Allocate the mask data when a masking property is defined in the property map
262    */
263   void AllocateMaskData();
264
265   /**
266    * @brief Applies the image to the texture set used for this renderer
267    *
268    * @param[in] image The Image to apply to the texture set used for this renderer
269    */
270   void ApplyImageToSampler( const Image& image );
271
272   /**
273    * @brief Load the texture, will try to atlas unless unable or param set to false.
274    * @param[in, out] atlasing flag if the image has been put in a atlas (true), passing false will not atlas even if possible.
275    * @param[out] atlasRect if atlasing is used this the texture area of the image in the atlas.
276    * @param[out] textures resulting texture set from the image loading.
277    * @param[in] orientationCorrection flag determines if orientation correction should be performed
278    */
279   void LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection );
280
281   /**
282    * @brief Initializes the Dali::Renderer from the image url
283    */
284   void InitializeRenderer();
285
286   /**
287    * @brief Initializes the Dali::Renderer from an image handle
288    *
289    * @param[in] image The image handle to intialize this ImageVisual from
290    */
291   void InitializeRenderer( const Image& image );
292
293   /**
294    * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
295    * @param[in] textures to use
296    */
297   void CreateRenderer( TextureSet& textures );
298
299   /**
300    * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
301    * @param NativeImageRenderer
302    */
303   void CreateNativeImageRenderer( NativeImage& nativeImage );
304
305   /**
306    * @brief Query whether resources requires to be loaded synchronously.
307    * @return Returns true if synchronous resource loading is required, false otherwise.
308    */
309   bool IsSynchronousResourceLoading() const;
310
311   /**
312    * Creates the texture set and adds the texture to it
313    * @param[out] textureRect The texture area of the texture in the atlas.
314    * @param[in] url The URL of the image resource to use.
315    * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
316    * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
317    * @return the texture set to use
318    */
319   TextureSet CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing );
320
321   /**
322    * Set the value to the uTextureRect uniform
323    * @param[in] textureRect The texture rectangular area.
324    */
325   void SetTextureRectUniform( const Vector4& textureRect  );
326
327   /**
328    * Remove texture with valid TextureId
329    */
330   void RemoveTexture();
331
332   /**
333    * Helper method to set individual values by index key.
334    * @param[in] index The index key of the value
335    * @param[in] value The value
336    */
337   void DoSetProperty( Property::Index index, const Property::Value& value );
338
339 private:
340
341   Image mImage;
342   Vector4 mPixelArea;
343   WeakHandle<Actor> mPlacementActor;
344   VisualUrl mImageUrl;
345   TextureManager::MaskingDataPointer mMaskingData;
346
347   Dali::ImageDimensions mDesiredSize;
348   TextureManager::TextureId mTextureId;
349   TextureSet mTextures;
350
351   Dali::FittingMode::Type mFittingMode:3;
352   Dali::SamplingMode::Type mSamplingMode:4;
353   Dali::WrapMode::Type mWrapModeU:3;
354   Dali::WrapMode::Type mWrapModeV:3;
355   DevelImageVisual::LoadPolicy::Type mLoadPolicy;
356   DevelImageVisual::ReleasePolicy::Type mReleasePolicy;
357   Vector4 mAtlasRect;
358   bool mAttemptAtlasing; ///< If true will attempt atlasing, otherwise create unique texture
359   bool mLoading;  ///< True if the texture is still loading.
360   bool mOrientationCorrection; ///< true if the image will have it's orientation corrected.
361 };
362
363
364
365 } // namespace Internal
366
367 } // namespace Toolkit
368
369 } // namespace Dali
370
371 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */