Disable Image Visual Pixel alignment when using Layer3D
[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) 2016 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 <dali/public-api/common/intrusive-ptr.h>
23 #include <dali/public-api/images/image.h>
24 #include <dali/public-api/images/image-operations.h>
25 #include <dali/public-api/images/resource-image.h>
26 #include <dali/devel-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/visual-base-impl.h>
31 #include <dali-toolkit/internal/visuals/visual-url.h>
32
33 namespace Dali
34 {
35
36 class NativeImage;
37
38 namespace Toolkit
39 {
40
41 namespace Internal
42 {
43
44 class ImageVisual;
45 typedef IntrusivePtr< ImageVisual > ImageVisualPtr;
46
47 /**
48  * The visual which renders an image to the control's quad
49  *
50  * The following properties are optional
51  *
52  * | %Property Name     | Type              |
53  * |--------------------|-------------------|
54  * | url                | STRING            |
55  * | fittingMode        | INTEGER OR STRING |
56  * | samplingMode       | INTEGER OR STRING |
57  * | desiredWidth       | INTEGER           |
58  * | desiredHeight      | INTEGER           |
59  * | synchronousLoading | BOOLEAN           |
60  * | pixelArea          | VECTOR4           |
61  * | wrapModeU          | INTEGER OR STRING |
62  * | wrapModeV          | INTEGER OR STRING |
63  *
64  * where pixelArea is a rectangular area.
65  * In its Vector4 value, the first two elements indicate the top-left position of the area,
66  * and the last two elements are the area width and height respectively.
67  * If not specified, the default value is [0.0, 0.0, 1.0, 1.0], i.e. the entire area of the image.
68  *
69  * 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.
70  * Its value should be one of the following wrap mode:
71  *   "DEFAULT"
72  *   "CLAMP_TO_EDGE"
73  *   "REPEAT"
74  *   "MIRRORED_REPEAT"
75  *
76  * where imageFittingMode should be one of the following fitting modes:
77  *   "SHRINK_TO_FIT"
78  *   "SCALE_TO_FIT"
79  *   "FIT_WIDTH"
80  *   "FIT_HEIGHT"
81  *   "DEFAULT"
82  *
83  * where imageSamplingMode should be one of the following sampling modes:
84  *   "BOX"
85  *   "NEAREST"
86  *   "LINEAR"
87  *   "BOX_THEN_NEAREST"
88  *   "BOX_THEN_LINEAR"
89  *   "NO_FILTER"
90  *   "DONT_CARE"
91  *   "DEFAULT"
92  *
93  *
94  * If the Visual is in a LayerUI it will pixel align the image, using a Layer3D will disable pixel alignment.
95  * Changing layer behaviour between LayerUI to Layer3D whilst the visual is already staged will not have an effect.
96  */
97 class ImageVisual: public Visual::Base, public ConnectionTracker, public AtlasUploadObserver
98 {
99 public:
100
101   /**
102    * @brief Create a new image visual with a URL.
103    *
104    * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
105    *
106    * @param[in] factoryCache The VisualFactoryCache object
107    * @param[in] imageUrl The URL of the image resource to use
108    * @param[in] properties A Property::Map containing settings for this visual
109    * @param[in] size The width and height to fit the loaded image to.
110    * @param[in] fittingMode The FittingMode of the resource to load
111    * @param[in] samplingMode The SamplingMode of the resource to load
112    * @return A smart-pointer to the newly allocated visual.
113    */
114   static ImageVisualPtr New( VisualFactoryCache& factoryCache,
115                              const VisualUrl& imageUrl,
116                              const Property::Map& properties,
117                              ImageDimensions size = ImageDimensions(),
118                              FittingMode::Type fittingMode = FittingMode::DEFAULT,
119                              Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
120
121   /**
122    * @brief Create a new image visual with a URL.
123    *
124    * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
125    *
126    * @param[in] factoryCache The VisualFactoryCache object
127    * @param[in] imageUrl The URL of the image resource to use
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                              const VisualUrl& imageUrl,
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 an Image type.
141    *
142    * @param[in] factoryCache The VisualFactoryCache object
143    * @param[in] image The image to use
144    */
145   static ImageVisualPtr New( VisualFactoryCache& factoryCache, const Image& image );
146
147 public:  // from Visual
148
149   /**
150    * @copydoc Visual::Base::GetNaturalSize
151    */
152   virtual void GetNaturalSize( Vector2& naturalSize );
153
154   /**
155    * @copydoc Visual::Base::CreatePropertyMap
156    */
157   virtual void DoCreatePropertyMap( Property::Map& map ) const;
158
159   /**
160    * @copydoc Visual::Base::CreateInstancePropertyMap
161    */
162   virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
163
164 protected:
165
166   /**
167    * @brief Constructor with a URL.
168    *
169    * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
170    *
171    * @param[in] factoryCache The VisualFactoryCache object
172    * @param[in] imageUrl The URL of the image resource to use
173    * @param[in] size The width and height to fit the loaded image to.
174    * @param[in] fittingMode The FittingMode of the resource to load
175    * @param[in] samplingMode The SamplingMode of the resource to load
176    */
177   ImageVisual( VisualFactoryCache& factoryCache,
178                const VisualUrl& imageUrl,
179                ImageDimensions size,
180                FittingMode::Type fittingMode,
181                Dali::SamplingMode::Type samplingMode );
182
183   /**
184    * @brief Constructor with an Image type.
185    *
186    * @param[in] factoryCache The VisualFactoryCache object
187    * @param[in] image The image to use
188    */
189   ImageVisual( VisualFactoryCache& factoryCache, const Image& image );
190
191   /**
192    * @brief A reference counted object may only be deleted by calling Unreference().
193    */
194   virtual ~ImageVisual();
195
196   /**
197    * @copydoc Visual::Base::DoSetProperties
198    */
199   virtual void DoSetProperties( const Property::Map& propertyMap );
200
201   /**
202    * @copydoc Visual::Base::DoSetOnStage
203    */
204   virtual void DoSetOnStage( Actor& actor );
205
206   /**
207    * @copydoc Visual::Base::DoSetOffStage
208    */
209   virtual void DoSetOffStage( Actor& actor );
210
211   /**
212    * @copydoc Visual::Base::OnSetTransform
213    */
214   virtual void OnSetTransform();
215
216 public:
217
218   /**
219    * Get the standard image rendering shader.
220    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
221    * @param[in] atlasing Whether texture atlasing is applied.
222    * @param[in] defaultTextureWrapping Whether the default texture wrap mode is applied.
223    */
224   static Shader GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping );
225
226   /**
227    * @copydoc AtlasUploadObserver::UploadCompleted
228    *
229    * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
230    * This callback is the place to add the renderer as it would be called once the loading is finished.
231    */
232   virtual void UploadCompleted();
233
234 private:
235
236   /**
237    * @brief Applies the image to the texture set used for this renderer
238    *
239    * @param[in] image The Image to apply to the texture set used for this renderer
240    */
241   void ApplyImageToSampler( const Image& image );
242
243   /**
244    * @brief Initializes the Dali::Renderer from the image url
245    */
246   void InitializeRenderer();
247
248   /**
249    * @brief Initializes the Dali::Renderer from an image handle
250    *
251    * @param[in] image The image handle to intialize this ImageVisual from
252    */
253   void InitializeRenderer( const Image& image );
254
255   /**
256    * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
257    * @param[in] textures to use
258    */
259   void CreateRenderer( TextureSet& textures );
260
261   /**
262    * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
263    * @param NativeImageRenderer
264    */
265   void CreateNativeImageRenderer( NativeImage& nativeImage );
266
267   /**
268    * @brief Query whether resources requires to be loaded synchronously.
269    * @return Returns true if synchronous resource loading is required, false otherwise.
270    */
271   bool IsSynchronousResourceLoading() const;
272
273   /**
274    * @brief Load the resource synchronously
275    */
276   void LoadResourceSynchronously();
277
278   /**
279    * Creates the texture set and adds the texture to it
280    * @param[out] textureRect The texture area of the texture in the atlas.
281    * @param[in] url The URL of the image resource to use.
282    * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
283    * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
284    * @return the texture set to use
285    */
286   TextureSet CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing );
287
288   /**
289    * Callback function of image resource loading succeed
290    * @param[in] image The Image content that we attempted to load from mImageUrl
291    */
292   void OnImageLoaded( ResourceImage image );
293
294   /**
295    * Set the value to the uTextureRect uniform
296    * @param[in] textureRect The texture rectangular area.
297    */
298   void SetTextureRectUniform( const Vector4& textureRect  );
299
300   /**
301    * Remove the image from atlas if it is not used anymore.
302    */
303   void RemoveFromAtlas(const std::string& url);
304
305   /**
306    * Helper method to set individual values by index key.
307    * @param[in] index The index key of the value
308    * @param[in] value The value
309    */
310   void DoSetProperty( Property::Index index, const Property::Value& value );
311
312 private:
313
314   Image mImage;
315   PixelData mPixels;
316   Vector4 mPixelArea;
317   WeakHandle<Actor> mPlacementActor;
318   VisualUrl mImageUrl;
319
320   Dali::ImageDimensions mDesiredSize;
321   Dali::FittingMode::Type mFittingMode:3;
322   Dali::SamplingMode::Type mSamplingMode:4;
323   Dali::WrapMode::Type mWrapModeU:3;
324   Dali::WrapMode::Type mWrapModeV:3;
325
326   bool mAttemptAtlasing:1; // If true will attempt atlasing, otherwise create unique texture
327
328 };
329
330 } // namespace Internal
331
332 } // namespace Toolkit
333
334 } // namespace Dali
335
336 #endif /* DALI_TOOLKIT_INTERNAL_IMAGE_VISUAL_H */