(AnimatedVectorImageVisual) Change renderer on stage again
[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 #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   virtual void GetNaturalSize( Vector2& naturalSize );
176
177   /**
178    * @copydoc Visual::Base::CreatePropertyMap
179    */
180   virtual void DoCreatePropertyMap( Property::Map& map ) const;
181
182   /**
183    * @copydoc Visual::Base::CreateInstancePropertyMap
184    */
185   virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
186
187   /**
188    * @copydoc Visual::Base::OnDoAction
189    */
190   virtual 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   virtual void DoSetProperties( const Property::Map& propertyMap );
231
232   /**
233    * @copydoc Visual::Base::DoSetOnStage
234    */
235   virtual void DoSetOnStage( Actor& actor );
236
237   /**
238    * @copydoc Visual::Base::DoSetOffStage
239    */
240   virtual void DoSetOffStage( Actor& actor ) ;
241
242   /**
243    * @copydoc Visual::Base::OnSetTransform
244    */
245   virtual void OnSetTransform() ;
246
247   /**
248    * @copydoc Visual::Base::IsResourceReady
249    */
250   virtual bool IsResourceReady() const;
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   virtual 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   virtual void UploadComplete( bool success, int32_t textureId, TextureSet textureSet,
269                                bool usingAtlas, const Vector4& atlasRectangle, bool preMultiplied ) override;
270
271 private:
272
273   /**
274    * Allocate the mask data when a masking property is defined in the property map
275    */
276   void AllocateMaskData();
277
278   /**
279    * @brief Applies the image to the texture set used for this renderer
280    *
281    * @param[in] image The Image to apply to the texture set used for this renderer
282    */
283   void ApplyImageToSampler( const Image& image );
284
285   /**
286    * @brief Load the texture, will try to atlas unless unable or param set to false.
287    * @param[in, out] atlasing flag if the image has been put in a atlas (true), passing false will not atlas even if possible.
288    * @param[out] atlasRect if atlasing is used this the texture area of the image in the atlas.
289    * @param[out] textures resulting texture set from the image loading.
290    * @param[in] orientationCorrection flag determines if orientation correction should be performed
291    * @param[in] forceReload flag determines if the texture should be reloaded from its source or use the cached texture.
292    */
293   void LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection, TextureManager::ReloadPolicy forceReload );
294
295   /**
296    * @brief Checks if atlasing should be attempted
297    * @return bool returns true if atlasing can be attempted.
298    */
299   bool AttemptAtlasing();
300
301   /**
302    * @brief Initializes the Dali::Renderer from the image url
303    */
304   void InitializeRenderer();
305
306   /**
307    * @brief Initializes the Dali::Renderer from an image handle
308    *
309    * @param[in] image The image handle to intialize this ImageVisual from
310    */
311   void InitializeRenderer( const Image& image );
312
313   /**
314    * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
315    * @param[in] textures to use
316    */
317   void CreateRenderer( TextureSet& textures );
318
319   /**
320    * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
321    * @param NativeImageRenderer
322    */
323   void CreateNativeImageRenderer( NativeImage& nativeImage );
324
325   /**
326    * @brief Query whether resources requires to be loaded synchronously.
327    * @return Returns true if synchronous resource loading is required, false otherwise.
328    */
329   bool IsSynchronousResourceLoading() const;
330
331   /**
332    * Creates the texture set and adds the texture to it
333    * @param[out] textureRect The texture area of the texture in the atlas.
334    * @param[in] url The URL of the image resource to use.
335    * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
336    * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
337    * @return the texture set to use
338    */
339   TextureSet CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing );
340
341   /**
342    * Set the value to the uTextureRect uniform
343    * @param[in] textureRect The texture rectangular area.
344    */
345   void SetTextureRectUniform( const Vector4& textureRect  );
346
347   /**
348    * Remove texture with valid TextureId
349    */
350   void RemoveTexture();
351
352   /**
353    * Helper method to set individual values by index key.
354    * @param[in] index The index key of the value
355    * @param[in] value The value
356    */
357   void DoSetProperty( Property::Index index, const Property::Value& value );
358
359 private:
360
361   Image mImage;
362   Vector4 mPixelArea;
363   WeakHandle<Actor> mPlacementActor;
364   VisualUrl mImageUrl;
365   TextureManager::MaskingDataPointer mMaskingData;
366
367   Dali::ImageDimensions mDesiredSize;
368   TextureManager::TextureId mTextureId;
369   TextureSet mTextures;
370
371   ImageVisualShaderFactory& mImageVisualShaderFactory;
372
373   Dali::FittingMode::Type mFittingMode:3;
374   Dali::SamplingMode::Type mSamplingMode:4;
375   Dali::WrapMode::Type mWrapModeU:3;
376   Dali::WrapMode::Type mWrapModeV:3;
377   Dali::Toolkit::ImageVisual::LoadPolicy::Type mLoadPolicy;
378   Dali::Toolkit::ImageVisual::ReleasePolicy::Type mReleasePolicy;
379   Vector4 mAtlasRect;
380   Dali::ImageDimensions mAtlasRectSize;
381   bool mAttemptAtlasing; ///< If true will attempt atlasing, otherwise create unique texture
382   bool mLoading;  ///< True if the texture is still loading.
383   bool mOrientationCorrection; ///< true if the image will have it's orientation corrected.
384 };
385
386
387
388 } // namespace Internal
389
390 } // namespace Toolkit
391
392 } // namespace Dali
393
394 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */