Implemented custom shader in ImageRenderer and changed Dissolve-effect to utilise...
[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  * | image-url                 | STRING           |
49  * | border-only               | BOOLEAN
50  *
51  */
52 class NPatchRenderer: public ControlRenderer
53 {
54 public:
55
56   /**
57    * @brief Constructor.
58    */
59   NPatchRenderer();
60
61   /**
62    * @brief A reference counted object may only be deleted by calling Unreference().
63    */
64   ~NPatchRenderer();
65
66 public:  // from ControlRenderer
67
68   /**
69    * @copydoc ControlRenderer::DoInitialize
70    */
71   virtual void DoInitialize( RendererFactoryCache& factoryCache, const Property::Map& propertyMap );
72
73   /**
74    * @copydoc ControlRenderer::GetNaturalSize
75    */
76   virtual void GetNaturalSize( Vector2& naturalSize ) const;
77
78   /**
79    * @copydoc ControlRenderer::SetClipRect
80    */
81   virtual void SetClipRect( const Rect<int>& clipRect );
82
83   /**
84    * @copydoc ControlRenderer::SetOffset
85    */
86   virtual void SetOffset( const Vector2& offset );
87
88   /**
89    * @copydoc ControlRenderer::CreatePropertyMap
90    */
91   virtual void DoCreatePropertyMap( Property::Map& map ) const;
92
93 protected:
94   /**
95    * @copydoc ControlRenderer::DoSetOnStage
96    */
97   virtual void DoSetOnStage( Actor& actor );
98
99   /**
100    * @copydoc ControlRenderer::DoSetOffStage
101    */
102   virtual void DoSetOffStage( Actor& actor );
103
104 public:
105
106   /**
107    * Request the geometry and shader from the cache, if not available, create and save to the cache for sharing.
108    *
109    * @param[in] factoryCache A pointer pointing to the RendererFactoryCache object
110    */
111   void Initialize( RendererFactoryCache& factoryCache );
112
113   /**
114    * @brief Sets the 9 patch image of this renderer to the resource at imageUrl
115    * The renderer will load the image synchronously when the associated actor is put on stage, and destroy the image when it is off stage
116    *
117    * @param[in] imageUrl The URL to 9 patch image resource to use
118    * @param[in] borderOnly A Flag to indicate if the image should omit the centre of the n-patch and only render the border
119    */
120   void SetImage( const std::string& imageUrl, bool borderOnly = false );
121
122   /**
123    * @brief Sets the 9 patch image of this renderer to the 9 patch image
124    *
125    * @param[in] image The NinePatchImage to use
126    * @param[in] borderOnly A Flag to indicate if the image should omit the centre of the n-patch and only render the border
127    */
128   void SetImage( NinePatchImage image, bool borderOnly = false );
129
130 private:
131
132   /**
133    * @brief Creates a geometry for the grid size to be used by this renderers' shaders
134    *
135    * @param gridSize The grid size of the solid geometry to create
136    * @return Returns the created geometry for the grid size
137    */
138   Geometry CreateGeometry( Uint16Pair gridSize );
139
140   /**
141    * @brief Creates a geometry with the border only for the grid size to be used by this renderers' shaders
142    * e.g. a 5x4 grid would create a geometry that would look like:
143    *
144    *   ---------------------
145    *   |  /|  /|  /|  /|  /|
146    *   |/  |/  |/  |/  |/  |
147    *   ---------------------
148    *   |  /|           |  /|
149    *   |/  |           |/  |
150    *   -----           -----
151    *   |  /|           |  /|
152    *   |/  |           |/  |
153    *   ---------------------
154    *   |  /|  /|  /|  /|  /|
155    *   |/  |/  |/  |/  |/  |
156    *   ---------------------
157    *
158    * @param gridSize The grid size of the solid geometry to create
159    * @return Returns the created geometry for the grid size
160    */
161   Geometry CreateGeometryBorder( Uint16Pair gridSize );
162
163   /**
164    * @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
165    *
166    * @param nPatchImage The NinePatchImage to base our cropped images and stretch borders from
167    */
168   void InitialiseFromImage( NinePatchImage nPatchImage );
169
170   /**
171    * @brief Creates a black Image to indicate that there was an error in either the image url or the parsing of the image
172    *
173    */
174   void CreateErrorImage();
175
176   /**
177    * @brief Applies this renderer's image to the sampler to the material used for this renderer
178    */
179   void ApplyImageToSampler();
180
181 private:
182
183   NinePatchImage mImage; ///< The image to render if the renderer was set from an NinePatchImage, empty otherwise
184   Image mCroppedImage;
185   Geometry mNinePatchGeometry;
186   Geometry mNinePatchBorderGeometry;
187   Shader mNinePatchShader;
188
189   std::string mImageUrl; ///< The url to the image resource to render if the renderer was set from an image resource url, empty otherwise
190   NinePatchImage::StretchRanges mStretchPixelsX;
191   NinePatchImage::StretchRanges mStretchPixelsY;
192   ImageDimensions mImageSize;
193   bool mBorderOnly;
194 };
195
196 } // namespace Internal
197
198 } // namespace Toolkit
199
200 } // namespace Dali
201
202 #endif /* __DALI_TOOLKIT_INTERNAL_N_PATCH_RENDERER_H__ */