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