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