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