Added a pblic Control Devel API GetVisualResourceStatus.
[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   /**
233    * @copydoc Visual::Base::IsResourceReady
234    */
235   virtual bool IsResourceReady() const;
236
237 public:
238
239   /**
240    * Get the standard image rendering shader.
241    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
242    * @param[in] atlasing Whether texture atlasing is applied.
243    * @param[in] defaultTextureWrapping Whether the default texture wrap mode is applied.
244    */
245   static Shader GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping );
246
247   /**
248    * @copydoc AtlasUploadObserver::UploadCompleted
249    *
250    * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
251    * This callback is the place to add the renderer as it would be called once the loading is finished.
252    */
253   virtual void UploadCompleted();
254
255   /**
256    * @copydoc TextureUploadObserver::UploadCompleted
257    *
258    * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
259    * This callback is the place to add the renderer as it would be called once the loading is finished.
260    */
261   virtual void UploadComplete( bool success, int32_t textureId, TextureSet textureSet, bool usingAtlas, const Vector4& atlasRectangle );
262
263 private:
264
265   /**
266    * Allocate the mask data when a masking property is defined in the property map
267    */
268   void AllocateMaskData();
269
270   /**
271    * @brief Applies the image to the texture set used for this renderer
272    *
273    * @param[in] image The Image to apply to the texture set used for this renderer
274    */
275   void ApplyImageToSampler( const Image& image );
276
277   /**
278    * @brief Load the texture, will try to atlas unless unable or param set to false.
279    * @param[in, out] atlasing flag if the image has been put in a atlas (true), passing false will not atlas even if possible.
280    * @param[out] atlasRect if atlasing is used this the texture area of the image in the atlas.
281    * @param[out] textures resulting texture set from the image loading.
282    * @param[in] orientationCorrection flag determines if orientation correction should be performed
283    */
284   void LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection );
285
286   /**
287    * @brief Initializes the Dali::Renderer from the image url
288    */
289   void InitializeRenderer();
290
291   /**
292    * @brief Initializes the Dali::Renderer from an image handle
293    *
294    * @param[in] image The image handle to intialize this ImageVisual from
295    */
296   void InitializeRenderer( const Image& image );
297
298   /**
299    * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
300    * @param[in] textures to use
301    */
302   void CreateRenderer( TextureSet& textures );
303
304   /**
305    * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
306    * @param NativeImageRenderer
307    */
308   void CreateNativeImageRenderer( NativeImage& nativeImage );
309
310   /**
311    * @brief Query whether resources requires to be loaded synchronously.
312    * @return Returns true if synchronous resource loading is required, false otherwise.
313    */
314   bool IsSynchronousResourceLoading() const;
315
316   /**
317    * Creates the texture set and adds the texture to it
318    * @param[out] textureRect The texture area of the texture in the atlas.
319    * @param[in] url The URL of the image resource to use.
320    * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
321    * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
322    * @return the texture set to use
323    */
324   TextureSet CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing );
325
326   /**
327    * Set the value to the uTextureRect uniform
328    * @param[in] textureRect The texture rectangular area.
329    */
330   void SetTextureRectUniform( const Vector4& textureRect  );
331
332   /**
333    * Remove texture with valid TextureId
334    */
335   void RemoveTexture();
336
337   /**
338    * Helper method to set individual values by index key.
339    * @param[in] index The index key of the value
340    * @param[in] value The value
341    */
342   void DoSetProperty( Property::Index index, const Property::Value& value );
343
344 private:
345
346   Image mImage;
347   Vector4 mPixelArea;
348   WeakHandle<Actor> mPlacementActor;
349   VisualUrl mImageUrl;
350   TextureManager::MaskingDataPointer mMaskingData;
351
352   Dali::ImageDimensions mDesiredSize;
353   TextureManager::TextureId mTextureId;
354   TextureSet mTextures;
355
356   Dali::FittingMode::Type mFittingMode:3;
357   Dali::SamplingMode::Type mSamplingMode:4;
358   Dali::WrapMode::Type mWrapModeU:3;
359   Dali::WrapMode::Type mWrapModeV:3;
360   DevelImageVisual::LoadPolicy::Type mLoadPolicy;
361   DevelImageVisual::ReleasePolicy::Type mReleasePolicy;
362   Vector4 mAtlasRect;
363   bool mAttemptAtlasing; ///< If true will attempt atlasing, otherwise create unique texture
364   bool mLoading;  ///< True if the texture is still loading.
365   bool mOrientationCorrection; ///< true if the image will have it's orientation corrected.
366 };
367
368
369
370 } // namespace Internal
371
372 } // namespace Toolkit
373
374 } // namespace Dali
375
376 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */