Rendering API clean-up
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / renderers / npatch / npatch-renderer.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_N_PATCH_RENDERER_H__
2 #define __DALI_TOOLKIT_INTERNAL_N_PATCH_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
24 // EXTERNAL INCLUDES
25 #include <dali/public-api/images/image.h>
26 #include <dali/public-api/images/nine-patch-image.h>
27 #include <dali/public-api/images/image-operations.h>
28 #include <dali/devel-api/rendering/geometry.h>
29 #include <dali/devel-api/rendering/shader.h>
30 #include <dali/devel-api/rendering/sampler.h>
31
32 namespace Dali
33 {
34
35 namespace Toolkit
36 {
37
38 namespace Internal
39 {
40
41 /**
42  * The renderer which renders an 9 patch image to the control's quad
43  *
44  * The following properties are optional
45  *
46  * | %Property Name           | Type             |
47  * |--------------------------|------------------|
48  * | imageUrl                 | STRING           |
49  * | borderOnly               | BOOLEAN
50  *
51  */
52 class NPatchRenderer: public ControlRenderer
53 {
54 public:
55
56   /**
57    * @brief Constructor.
58    *
59    * @param[in] factoryCache A pointer pointing to the RendererFactoryCache object
60    */
61   NPatchRenderer( RendererFactoryCache& factoryCache );
62
63   /**
64    * @brief A reference counted object may only be deleted by calling Unreference().
65    */
66   ~NPatchRenderer();
67
68 public:  // from ControlRenderer
69
70   /**
71    * @copydoc ControlRenderer::GetNaturalSize
72    */
73   virtual void GetNaturalSize( Vector2& naturalSize ) const;
74
75   /**
76    * @copydoc ControlRenderer::SetClipRect
77    */
78   virtual void SetClipRect( const Rect<int>& clipRect );
79
80   /**
81    * @copydoc ControlRenderer::SetOffset
82    */
83   virtual void SetOffset( const Vector2& offset );
84
85   /**
86    * @copydoc ControlRenderer::CreatePropertyMap
87    */
88   virtual void DoCreatePropertyMap( Property::Map& map ) const;
89
90 protected:
91
92   /**
93    * @copydoc ControlRenderer::DoInitialize
94    */
95   virtual void DoInitialize( Actor& actor, const Property::Map& propertyMap );
96
97   /**
98    * @copydoc ControlRenderer::DoSetOnStage
99    */
100   virtual void DoSetOnStage( Actor& actor );
101
102   /**
103    * @copydoc ControlRenderer::DoSetOffStage
104    */
105   virtual void DoSetOffStage( Actor& actor );
106
107 public:
108
109   /**
110    * @brief Sets the 9 patch image of this renderer to the resource at imageUrl
111    * The renderer will load the image synchronously when the associated actor is put on stage, and destroy the image when it is off stage
112    *
113    * @param[in] imageUrl The URL to 9 patch image resource to use
114    * @param[in] borderOnly A Flag to indicate if the image should omit the centre of the n-patch and only render the border
115    */
116   void SetImage( const std::string& imageUrl, bool borderOnly = false );
117
118   /**
119    * @brief Sets the 9 patch image of this renderer to the 9 patch image
120    *
121    * @param[in] image The NinePatchImage to use
122    * @param[in] borderOnly A Flag to indicate if the image should omit the centre of the n-patch and only render the border
123    */
124   void SetImage( NinePatchImage image, bool borderOnly = false );
125
126 private:
127
128   /**
129    * @brief Initialize the renderer with the geometry and shader from the cache, if not available, create and save to the cache for sharing.
130    */
131   void InitializeRenderer();
132
133   /**
134    * @brief Creates a geometry for this renderer's grid size
135    *
136    * @return Returns the created geometry for this renderer's grid size
137    */
138   Geometry CreateGeometry();
139
140   /**
141    * @brief Creates a shader for this renderer's grid size
142    *
143    * @return Returns the created shader for this renderer's grid size
144    */
145   Shader CreateShader();
146
147   /**
148    * @brief Creates a geometry for the grid size to be used by this renderers' shaders
149    *
150    * @param[in] gridSize The grid size of the solid geometry to create
151    * @return Returns the created geometry for the grid size
152    */
153   Geometry CreateGeometry( Uint16Pair gridSize );
154
155   /**
156    * @brief Creates a geometry with the border only for the grid size to be used by this renderers' shaders
157    * e.g. a 5x4 grid would create a geometry that would look like:
158    *
159    *   ---------------------
160    *   |  /|  /|  /|  /|  /|
161    *   |/  |/  |/  |/  |/  |
162    *   ---------------------
163    *   |  /|           |  /|
164    *   |/  |           |/  |
165    *   -----           -----
166    *   |  /|           |  /|
167    *   |/  |           |/  |
168    *   ---------------------
169    *   |  /|  /|  /|  /|  /|
170    *   |/  |/  |/  |/  |/  |
171    *   ---------------------
172    *
173    * @param[in] gridSize The grid size of the solid geometry to create
174    * @return Returns the created geometry for the grid size
175    */
176   Geometry CreateGeometryBorder( Uint16Pair gridSize );
177
178   /**
179    * @brief Creates Image from the image url and parses the image for the stretch borders. Will create a error image if the n patch image is invalid
180    *
181    * @param[in] nPatchImage The NinePatchImage to base our cropped images and stretch borders from
182    */
183   void InitializeFromImage( NinePatchImage nPatchImage );
184
185   /**
186    * @brief Creates an error Image to indicate that there was an error in either the image url or the parsing of the image
187    *
188    */
189   void InitializeFromBrokenImage();
190
191   /**
192    * @brief Applies this renderer's image to the sampler to the texture set used for this renderer
193    */
194   void ApplyImageToSampler();
195
196   /**
197    * @brief Changes the current renderer if the n-patch meta data has changed
198    *
199    * @param[in] oldBorderOnly The old flag indicating if the image should omit the centre of the n-patch and only render the border
200    * @param[in] oldGridX The old horizontal grid size of the solid geometry
201    * @param[in] oldGridY The old vertical grid size of the solid geometry
202    */
203   void ChangeRenderer( bool oldBorderOnly, size_t oldGridX, size_t oldGridY );
204
205 private:
206
207   NinePatchImage mImage; ///< The image to render if the renderer was set from an NinePatchImage, empty otherwise
208   Image mCroppedImage;
209
210   std::string mImageUrl; ///< The url to the image resource to render if the renderer was set from an image resource url, empty otherwise
211   NinePatchImage::StretchRanges mStretchPixelsX;
212   NinePatchImage::StretchRanges mStretchPixelsY;
213   ImageDimensions mImageSize;
214   bool mBorderOnly;
215 };
216
217 } // namespace Internal
218
219 } // namespace Toolkit
220
221 } // namespace Dali
222
223 #endif /* __DALI_TOOLKIT_INTERNAL_N_PATCH_RENDERER_H__ */