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