Merge "Added Auxiliary image to NPatch visual." into 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) 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   /**
181    * @copydoc Visual::Base::OnDoAction
182    */
183   virtual void OnDoAction( const Dali::Property::Index actionName, const Dali::Property::Value& attributes ) override;
184
185 protected:
186
187   /**
188    * @brief Constructor with a URL.
189    *
190    * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
191    *
192    * @param[in] factoryCache The VisualFactoryCache object
193    * @param[in] imageUrl The URL of the image resource to use
194    * @param[in] size The width and height to fit the loaded image to.
195    * @param[in] fittingMode The FittingMode of the resource to load
196    * @param[in] samplingMode The SamplingMode of the resource to load
197    */
198   ImageVisual( VisualFactoryCache& factoryCache,
199                const VisualUrl& imageUrl,
200                ImageDimensions size,
201                FittingMode::Type fittingMode,
202                Dali::SamplingMode::Type samplingMode );
203
204   /**
205    * @brief Constructor with an Image type.
206    *
207    * @param[in] factoryCache The VisualFactoryCache object
208    * @param[in] image The image to use
209    */
210   ImageVisual( VisualFactoryCache& factoryCache, const Image& image );
211
212   /**
213    * @brief A reference counted object may only be deleted by calling Unreference().
214    */
215   virtual ~ImageVisual();
216
217   /**
218    * @copydoc Visual::Base::DoSetProperties
219    */
220   virtual void DoSetProperties( const Property::Map& propertyMap );
221
222   /**
223    * @copydoc Visual::Base::DoSetOnStage
224    */
225   virtual void DoSetOnStage( Actor& actor );
226
227   /**
228    * @copydoc Visual::Base::DoSetOffStage
229    */
230   virtual void DoSetOffStage( Actor& actor ) ;
231
232   /**
233    * @copydoc Visual::Base::OnSetTransform
234    */
235   virtual void OnSetTransform() ;
236
237   /**
238    * @copydoc Visual::Base::IsResourceReady
239    */
240   virtual bool IsResourceReady() const;
241
242 public:
243
244   /**
245    * Get the standard image rendering shader.
246    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
247    * @param[in] atlasing Whether texture atlasing is applied.
248    * @param[in] defaultTextureWrapping Whether the default texture wrap mode is applied.
249    */
250   static Shader GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping );
251
252   /**
253    * @copydoc AtlasUploadObserver::UploadCompleted
254    *
255    * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
256    * This callback is the place to add the renderer as it would be called once the loading is finished.
257    */
258   virtual void UploadCompleted();
259
260   /**
261    * @copydoc TextureUploadObserver::UploadCompleted
262    *
263    * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
264    * This callback is the place to add the renderer as it would be called once the loading is finished.
265    */
266   virtual void UploadComplete( bool success, int32_t textureId, TextureSet textureSet, bool usingAtlas, const Vector4& atlasRectangle );
267
268 private:
269
270   /**
271    * Allocate the mask data when a masking property is defined in the property map
272    */
273   void AllocateMaskData();
274
275   /**
276    * @brief Applies the image to the texture set used for this renderer
277    *
278    * @param[in] image The Image to apply to the texture set used for this renderer
279    */
280   void ApplyImageToSampler( const Image& image );
281
282   /**
283    * @brief Load the texture, will try to atlas unless unable or param set to false.
284    * @param[in, out] atlasing flag if the image has been put in a atlas (true), passing false will not atlas even if possible.
285    * @param[out] atlasRect if atlasing is used this the texture area of the image in the atlas.
286    * @param[out] textures resulting texture set from the image loading.
287    * @param[in] orientationCorrection flag determines if orientation correction should be performed
288    * @param[in] forceReload flag determines if the texture should be reloaded from its source or use the cached texture.
289    */
290   void LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection, TextureManager::ReloadPolicy forceReload );
291
292   /**
293    * @brief Checks if atlasing should be attempted
294    * @return bool returns true if atlasing can be attempted.
295    */
296   bool AttemptAtlasing();
297
298   /**
299    * @brief Initializes the Dali::Renderer from the image url
300    */
301   void InitializeRenderer();
302
303   /**
304    * @brief Initializes the Dali::Renderer from an image handle
305    *
306    * @param[in] image The image handle to intialize this ImageVisual from
307    */
308   void InitializeRenderer( const Image& image );
309
310   /**
311    * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
312    * @param[in] textures to use
313    */
314   void CreateRenderer( TextureSet& textures );
315
316   /**
317    * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
318    * @param NativeImageRenderer
319    */
320   void CreateNativeImageRenderer( NativeImage& nativeImage );
321
322   /**
323    * @brief Query whether resources requires to be loaded synchronously.
324    * @return Returns true if synchronous resource loading is required, false otherwise.
325    */
326   bool IsSynchronousResourceLoading() const;
327
328   /**
329    * Creates the texture set and adds the texture to it
330    * @param[out] textureRect The texture area of the texture in the atlas.
331    * @param[in] url The URL of the image resource to use.
332    * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
333    * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
334    * @return the texture set to use
335    */
336   TextureSet CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing );
337
338   /**
339    * Set the value to the uTextureRect uniform
340    * @param[in] textureRect The texture rectangular area.
341    */
342   void SetTextureRectUniform( const Vector4& textureRect  );
343
344   /**
345    * Remove texture with valid TextureId
346    */
347   void RemoveTexture();
348
349   /**
350    * Helper method to set individual values by index key.
351    * @param[in] index The index key of the value
352    * @param[in] value The value
353    */
354   void DoSetProperty( Property::Index index, const Property::Value& value );
355
356 private:
357
358   Image mImage;
359   Vector4 mPixelArea;
360   WeakHandle<Actor> mPlacementActor;
361   VisualUrl mImageUrl;
362   TextureManager::MaskingDataPointer mMaskingData;
363
364   Dali::ImageDimensions mDesiredSize;
365   TextureManager::TextureId mTextureId;
366   TextureSet mTextures;
367
368   Dali::FittingMode::Type mFittingMode:3;
369   Dali::SamplingMode::Type mSamplingMode:4;
370   Dali::WrapMode::Type mWrapModeU:3;
371   Dali::WrapMode::Type mWrapModeV:3;
372   DevelImageVisual::LoadPolicy::Type mLoadPolicy;
373   DevelImageVisual::ReleasePolicy::Type mReleasePolicy;
374   Vector4 mAtlasRect;
375   bool mAttemptAtlasing; ///< If true will attempt atlasing, otherwise create unique texture
376   bool mLoading;  ///< True if the texture is still loading.
377   bool mOrientationCorrection; ///< true if the image will have it's orientation corrected.
378 };
379
380
381
382 } // namespace Internal
383
384 } // namespace Toolkit
385
386 } // namespace Dali
387
388 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */