Add GetVisualProperty to Control
[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-operations.h>
26 #include <dali/public-api/object/weak-handle.h>
27
28 // INTERNAL INCLUDES
29 #include <dali-toolkit/devel-api/image-loader/atlas-upload-observer.h>
30 #include <dali-toolkit/internal/visuals/texture-upload-observer.h>
31 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
32 #include <dali-toolkit/internal/visuals/visual-url.h>
33 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
34 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
35
36 namespace Dali
37 {
38
39 class NativeImage;
40
41 namespace Toolkit
42 {
43
44 namespace Internal
45 {
46
47 class ImageVisualShaderFactory;
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_FILL"
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] shaderFactory The ImageVisualShaderFactory object
124    * @param[in] imageUrl The URL of the image resource to use
125    * @param[in] properties A Property::Map containing settings for this visual
126    * @param[in] size The width and height to fit the loaded image to.
127    * @param[in] fittingMode The FittingMode of the resource to load
128    * @param[in] samplingMode The SamplingMode of the resource to load
129    * @return A smart-pointer to the newly allocated visual.
130    */
131   static ImageVisualPtr New( VisualFactoryCache& factoryCache,
132                              ImageVisualShaderFactory& shaderFactory,
133                              const VisualUrl& imageUrl,
134                              const Property::Map& properties,
135                              ImageDimensions size = ImageDimensions(),
136                              FittingMode::Type fittingMode = FittingMode::DEFAULT,
137                              Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
138
139   /**
140    * @brief Create a new image visual with a URL.
141    *
142    * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
143    *
144    * @param[in] factoryCache The VisualFactoryCache object
145    * @param[in] shaderFactory The ImageVisualShaderFactory object
146    * @param[in] imageUrl The URL of the image resource to use
147    * @param[in] size The width and height to fit the loaded image to.
148    * @param[in] fittingMode The FittingMode of the resource to load
149    * @param[in] samplingMode The SamplingMode of the resource to load
150    * @return A smart-pointer to the newly allocated visual.
151    */
152   static ImageVisualPtr New( VisualFactoryCache& factoryCache,
153                              ImageVisualShaderFactory& shaderFactory,
154                              const VisualUrl& imageUrl,
155                              ImageDimensions size = ImageDimensions(),
156                              FittingMode::Type fittingMode = FittingMode::DEFAULT,
157                              Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
158
159 public:  // from Visual
160
161   /**
162    * @copydoc Visual::Base::GetNaturalSize
163    */
164   void GetNaturalSize( Vector2& naturalSize ) override;
165
166   /**
167    * @copydoc Visual::Base::CreatePropertyMap
168    */
169   void DoCreatePropertyMap( Property::Map& map ) const override;
170
171   /**
172    * @copydoc Visual::Base::CreateInstancePropertyMap
173    */
174   void DoCreateInstancePropertyMap( Property::Map& map ) const override;
175
176   /**
177    * @copydoc Visual::Base::OnDoAction
178    */
179   void OnDoAction( const Dali::Property::Index actionName, const Dali::Property::Value& attributes ) override;
180
181 protected:
182
183   /**
184    * @brief Constructor with a URL.
185    *
186    * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
187    *
188    * @param[in] factoryCache The VisualFactoryCache object
189    * @param[in] shaderFactory The ImageVisualShaderFactory object
190    * @param[in] imageUrl The URL of the image resource to use
191    * @param[in] size The width and height to fit the loaded image to.
192    * @param[in] fittingMode The FittingMode of the resource to load
193    * @param[in] samplingMode The SamplingMode of the resource to load
194    */
195   ImageVisual( VisualFactoryCache& factoryCache,
196                ImageVisualShaderFactory& shaderFactory,
197                const VisualUrl& imageUrl,
198                ImageDimensions size,
199                FittingMode::Type fittingMode,
200                Dali::SamplingMode::Type samplingMode );
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   void DoSetProperties( const Property::Map& propertyMap ) override;
211
212   /**
213    * @copydoc Visual::Base::DoSetOnScene
214    */
215   void DoSetOnScene( Actor& actor ) override;
216
217   /**
218    * @copydoc Visual::Base::DoSetOffScene
219    */
220   void DoSetOffScene( Actor& actor ) override;
221
222   /**
223    * @copydoc Visual::Base::OnSetTransform
224    */
225   void OnSetTransform() override;
226
227   /**
228    * @copydoc Visual::Base::IsResourceReady
229    */
230   bool IsResourceReady() const override;
231
232   /**
233    * @copydoc Visual::Base::UpdateShader
234    */
235   void UpdateShader() override;
236
237 public:
238
239   /**
240    * @copydoc AtlasUploadObserver::UploadCompleted
241    *
242    * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
243    * This callback is the place to add the renderer as it would be called once the loading is finished.
244    */
245   void UploadCompleted() override;
246
247   /**
248    * @copydoc TextureUploadObserver::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   void UploadComplete( bool success, int32_t textureId, TextureSet textureSet,
254                        bool usingAtlas, const Vector4& atlasRectangle, bool preMultiplied ) override;
255
256 private:
257
258   /**
259    * @copydoc TextureUploadObserver::LoadComplete
260    *
261    * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
262    * This callback is the place to add the renderer as it would be called once the PixelBuffer loading is finished.
263    */
264   void LoadComplete( bool loadSuccess, Devel::PixelBuffer pixelBuffer, const VisualUrl& url, bool preMultiplied ) override {}
265
266   /**
267    * Allocate the mask data when a masking property is defined in the property map
268    */
269   void AllocateMaskData();
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    * @param[in] orientationCorrection flag determines if orientation correction should be performed
277    * @param[in] forceReload flag determines if the texture should be reloaded from its source or use the cached texture.
278    */
279   void LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection, TextureManager::ReloadPolicy forceReload );
280
281   /**
282    * @brief Checks if atlasing should be attempted
283    * @return bool returns true if atlasing can be attempted.
284    */
285   bool AttemptAtlasing();
286
287   /**
288    * @brief Initializes the Dali::Renderer from the image url
289    */
290   void InitializeRenderer();
291
292   /**
293    * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
294    * @param[in] textures to use
295    */
296   void CreateRenderer( TextureSet& textures );
297
298   /**
299    * Creates the texture set and adds the texture to it
300    * @param[out] textureRect The texture area of the texture in the atlas.
301    * @param[in] url The URL of the image resource to use.
302    * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
303    * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
304    * @return the texture set to use
305    */
306   TextureSet CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing );
307
308   /**
309    * Set the value to the uTextureRect uniform
310    * @param[in] textureRect The texture rectangular area.
311    */
312   void SetTextureRectUniform( const Vector4& textureRect  );
313
314   /**
315    * Remove texture with valid TextureId
316    */
317   void RemoveTexture();
318
319   /**
320    * Helper method to set individual values by index key.
321    * @param[in] index The index key of the value
322    * @param[in] value The value
323    */
324   void DoSetProperty( Property::Index index, const Property::Value& value );
325
326   /**
327    * @brief Get a shader for the current properties.
328    * @return The shader for the current properties.
329    */
330   Shader GetShader();
331
332 private:
333
334   Vector4 mPixelArea;
335   WeakHandle<Actor> mPlacementActor;
336   VisualUrl mImageUrl;
337   TextureManager::MaskingDataPointer mMaskingData;
338
339   Dali::ImageDimensions mDesiredSize;
340   TextureManager::TextureId mTextureId;
341   TextureSet mTextures;
342
343   ImageVisualShaderFactory& mImageVisualShaderFactory;
344
345   Dali::FittingMode::Type mFittingMode:3;
346   Dali::SamplingMode::Type mSamplingMode:4;
347   Dali::WrapMode::Type mWrapModeU:3;
348   Dali::WrapMode::Type mWrapModeV:3;
349   Dali::Toolkit::ImageVisual::LoadPolicy::Type mLoadPolicy;
350   Dali::Toolkit::ImageVisual::ReleasePolicy::Type mReleasePolicy;
351   Vector4 mAtlasRect;
352   Dali::ImageDimensions mAtlasRectSize;
353   bool mAttemptAtlasing; ///< If true will attempt atlasing, otherwise create unique texture
354   bool mLoading;  ///< True if the texture is still loading.
355   bool mOrientationCorrection; ///< true if the image will have it's orientation corrected.
356 };
357
358
359
360 } // namespace Internal
361
362 } // namespace Toolkit
363
364 } // namespace Dali
365
366 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */