Automatic image atlasing
[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 namespace Toolkit
34 {
35
36 namespace Internal
37 {
38
39 class ImageRenderer;
40 typedef IntrusivePtr< ImageRenderer > ImageRendererPtr;
41
42 /**
43  * The renderer which renders an image to the control's quad
44  *
45  * The following properties are optional
46  *
47  * | %Property Name          | Type             |
48  * |-------------------------|------------------|
49  * | imageUrl                | STRING           |
50  * | imageFittingMode        | STRING           |
51  * | imageSamplingMode       | STRING           |
52  * | imageDesiredWidth       | INT              |
53  * | imageDesiredHeight      | INT              |
54  *
55  * where imageFittingMode should be one of the following fitting modes:
56  *   "shrinkToFit"
57  *   "scaleToFill"
58  *   "fitWidth"
59  *   "fitHeight"
60  *   "default"
61  *
62  * where imageSamplingMode should be one of the following sampling modes:
63  *   "box"
64  *   "nearest"
65  *   "linear"
66  *   "boxThenNearest"
67  *   "boxThenLinear"
68  *   "noFilter"
69  *   "dontCare"
70  *   "default"
71  *
72  */
73 class ImageRenderer: public ControlRenderer, public ConnectionTracker
74 {
75 public:
76
77   /**
78    * @brief Constructor.
79    *
80    * @param[in] factoryCache The RendererFactoryCache object
81    * @param[in] atlasManager The atlasManager object
82    */
83   ImageRenderer( RendererFactoryCache& factoryCache, ImageAtlasManager& atlasManager );
84
85   /**
86    * @brief A reference counted object may only be deleted by calling Unreference().
87    */
88   ~ImageRenderer();
89
90 public:  // from ControlRenderer
91
92   /**
93    * @copydoc ControlRenderer::SetSize
94    */
95   virtual void SetSize( const Vector2& size );
96
97   /**
98    * @copydoc ControlRenderer::GetNaturalSize
99    */
100   virtual void GetNaturalSize( Vector2& naturalSize ) const;
101
102   /**
103    * @copydoc ControlRenderer::SetClipRect
104    */
105   virtual void SetClipRect( const Rect<int>& clipRect );
106
107   /**
108    * @copydoc ControlRenderer::SetOffset
109    */
110   virtual void SetOffset( const Vector2& offset );
111
112   /**
113    * @copydoc ControlRenderer::CreatePropertyMap
114    */
115   virtual void DoCreatePropertyMap( Property::Map& map ) const;
116
117 protected:
118   /**
119    * @copydoc ControlRenderer::DoInitialize
120    */
121   virtual void DoInitialize( Actor& actor, const Property::Map& propertyMap );
122
123   /**
124    * @copydoc ControlRenderer::DoSetOnStage
125    */
126   virtual void DoSetOnStage( Actor& actor );
127
128   /**
129    * @copydoc ControlRenderer::DoSetOffStage
130    */
131   virtual void DoSetOffStage( Actor& actor );
132
133 public:
134
135   /**
136    * Get the standard image rendering shader.
137    * @param[in] factoryCache A pointer pointing to the RendererFactoryCache object
138    */
139   static Shader GetImageShader( RendererFactoryCache& factoryCache );
140
141   /**
142    * @brief Sets the image of this renderer to the resource at imageUrl
143    * The renderer will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
144    *
145    * @param[in] actor The Actor the renderer is applied to if, empty if the renderer has not been applied to any Actor
146    * @param[in] imageUrl The URL to to image resource to use
147    * @param[in] size The width and height to fit the loaded image to.
148    * @param[in] fittingMode The FittingMode of the resource to load
149    * @param[in] samplingMode The SamplingMode of the resource to load
150    */
151   void SetImage( Actor& actor,
152                  const std::string& imageUrl,
153                  ImageDimensions size=ImageDimensions(),
154                  FittingMode::Type fittingMode = FittingMode::DEFAULT,
155                  Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
156
157   /**
158    * @brief Sets the image of this renderer to use
159    *
160    * @param[in] actor The Actor the renderer is applied to if, empty if the renderer has not been applied to any Actor
161    * @param[in] image The image to use
162    */
163   void SetImage( Actor& actor, const Image& image );
164
165 private:
166
167   /**
168    * @brief Applies the image to the material used for this renderer
169    *
170    * @param[in] image The Image to apply to the material used for this renderer
171    */
172   void ApplyImageToSampler( const Image& image );
173
174   /**
175    * @brief Initializes the Dali::Renderer from an image url string
176    *
177    * @param[in] imageUrl The image url string to intialize this ImageRenderer from
178    */
179   void InitializeRenderer( const std::string& imageUrl );
180
181   /**
182    * @brief Initializes the Dali::Renderer from an image handle
183    *
184    * @param[in] image The image handle to intialize this ImageRenderer from
185    */
186   void InitializeRenderer( const Image& image );
187
188   /**
189    * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
190    *
191    * @return Returns the created Dali::Renderer
192    */
193   Renderer CreateRenderer() const;
194
195   /**
196    * Callback function of image resource loading succeed
197    * @param[in] image The Image content that we attempted to load from mImageUrl
198    */
199   void OnImageLoaded( ResourceImage image );
200
201   /**
202    * Set the value to the uTextureRect uniform
203    * @param[in] textureRect The texture rectangular area.
204    */
205   void SetTextureRectUniform( const Vector4& textureRect  );
206
207   /**
208    * Clean the renderer from cache, and remove the image from atlas if it is not used anymore
209    */
210   void CleanCache(const std::string& url);
211
212 private:
213   Image mImage;
214   ImageAtlasManager& mAtlasManager;
215   Vector4 mTextureRect;
216
217   std::string mImageUrl;
218   Dali::ImageDimensions mDesiredSize;
219   Dali::FittingMode::Type mFittingMode;
220   Dali::SamplingMode::Type mSamplingMode;
221
222 };
223
224 } // namespace Internal
225
226 } // namespace Toolkit
227
228 } // namespace Dali
229
230 #endif /* __DALI_TOOLKIT_INTERNAL_IMAGE_RENDERER_H__ */