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