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