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