3284d0733d7921d0e5564f33f196f89f6efc7e18
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / renderers / border / border-renderer.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "border-renderer.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 //INTERNAL INCLUDES
25 #include <dali-toolkit/internal/controls/renderers/renderer-factory-impl.h>
26 #include <dali-toolkit/internal/controls/renderers/renderer-factory-cache.h>
27 #include <dali-toolkit/internal/controls/renderers/control-renderer-data-impl.h>
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 namespace Internal
36 {
37
38 namespace
39 {
40 const char * const RENDERER_TYPE("renderer-type");
41 const char * const RENDERER_TYPE_VALUE("border-renderer");
42
43 const char * const COLOR_NAME("border-color");
44 const char * const COLOR_UNIFORM_NAME("uBorderColor");
45 const char * const SIZE_NAME("border-size");
46 const char * const SIZE_UNIFORM_NAME("uBorderSize");
47
48 const char * const POSITION_ATTRIBUTE_NAME("aPosition");
49 const char * const DRIFT_ATTRIBUTE_NAME("aDrift");
50 const char * const INDEX_NAME("indices");
51
52
53 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
54   attribute mediump vec2 aPosition;\n
55   attribute mediump vec2 aDrift;\n
56   uniform mediump mat4 uMvpMatrix;\n
57   uniform mediump vec3 uSize;\n
58   uniform mediump float uBorderSize;\n
59   \n
60   void main()\n
61   {\n
62     vec2 position = aPosition*uSize.xy + aDrift*uBorderSize;\n
63     gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n
64   }\n
65 );
66
67 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
68   uniform lowp vec4 uColor;\n
69   uniform lowp vec4 uBorderColor;\n
70   \n
71   void main()\n
72   {\n
73     gl_FragColor = uBorderColor*uColor;\n
74   }\n
75 );
76 }
77
78 BorderRenderer::BorderRenderer( RendererFactoryCache& factoryCache )
79 : ControlRenderer( factoryCache ),
80   mBorderColor( Color::TRANSPARENT ),
81   mBorderSize( 0.f ),
82   mBorderColorIndex( Property::INVALID_INDEX ),
83   mBorderSizeIndex( Property::INVALID_INDEX )
84 {
85 }
86
87 BorderRenderer::~BorderRenderer()
88 {
89 }
90
91 void BorderRenderer::DoInitialize( const Property::Map& propertyMap )
92 {
93   Property::Value* color = propertyMap.Find( COLOR_NAME );
94   if( !( color && color->Get(mBorderColor) ) )
95   {
96     DALI_LOG_ERROR( "Fail to provide a border color to the BorderRenderer object" );
97   }
98
99   Property::Value* size = propertyMap.Find( SIZE_NAME );
100   if( !( size && size->Get(mBorderSize) ) )
101   {
102     DALI_LOG_ERROR( "Fail to provide a border size to the BorderRenderer object" );
103   }
104 }
105
106 void BorderRenderer::SetClipRect( const Rect<int>& clipRect )
107 {
108   ControlRenderer::SetClipRect( clipRect );
109
110   //ToDo: renderer responds to the clipRect change
111 }
112
113 void BorderRenderer::DoSetOnStage( Actor& actor )
114 {
115   mBorderColorIndex = (mImpl->mRenderer).RegisterProperty( COLOR_UNIFORM_NAME, mBorderColor );
116   if( mBorderColor.a < 1.f )
117   {
118     (mImpl->mRenderer).GetMaterial().SetBlendMode( BlendingMode::ON );
119   }
120   mBorderSizeIndex = (mImpl->mRenderer).RegisterProperty( SIZE_UNIFORM_NAME, mBorderSize );
121 }
122
123 void BorderRenderer::DoCreatePropertyMap( Property::Map& map ) const
124 {
125   map.Clear();
126   map.Insert( RENDERER_TYPE, RENDERER_TYPE_VALUE );
127   map.Insert( COLOR_NAME, mBorderColor );
128   map.Insert( SIZE_NAME, mBorderSize );
129 }
130
131 void BorderRenderer::InitializeRenderer( Renderer& renderer )
132 {
133   Geometry geometry = mFactoryCache.GetGeometry( RendererFactoryCache::BORDER_GEOMETRY );
134   if( !geometry )
135   {
136     geometry =  CreateBorderGeometry();
137     mFactoryCache.SaveGeometry( RendererFactoryCache::BORDER_GEOMETRY, geometry );
138   }
139
140   Shader shader = mFactoryCache.GetShader( RendererFactoryCache::BORDER_SHADER );
141   if( !shader )
142   {
143     shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
144     mFactoryCache.SaveShader( RendererFactoryCache::COLOR_SHADER, shader );
145   }
146
147   if( !renderer )
148   {
149     Material material = Material::New( shader );
150     renderer = Renderer::New( geometry, material );
151   }
152   else
153   {
154     mImpl->mRenderer.SetGeometry( geometry );
155     Material material = mImpl->mRenderer.GetMaterial();
156     if( material )
157     {
158       material.SetShader( shader );
159     }
160   }
161 }
162
163 void BorderRenderer::SetBorderColor(const Vector4& color)
164 {
165   mBorderColor = color;
166
167   if( mImpl->mIsOnStage )
168   {
169     (mImpl->mRenderer).SetProperty( mBorderColorIndex, color );
170     if( color.a < 1.f &&  (mImpl->mRenderer).GetMaterial().GetBlendMode() != BlendingMode::ON)
171     {
172       (mImpl->mRenderer).GetMaterial().SetBlendMode( BlendingMode::ON );
173     }
174   }
175 }
176
177 void BorderRenderer::SetBorderSize( float size )
178 {
179   mBorderSize = size;
180
181   if( mImpl->mIsOnStage )
182   {
183     (mImpl->mRenderer).SetProperty( mBorderSizeIndex, size );
184   }
185 }
186
187 /**
188  * Vertices and triangles of the border geometry:
189  *
190  * vertex position = aPosition*uSize.xy + aDrift*uBorderSize;
191  *
192  * 0--1--2--3
193  * | /| /| /|
194  * |/ |/ |/ |
195  * 4--5--6--7
196  * |\ |  |\ |
197  * | \|  | \|
198  * 8--9--10-11
199  * | /| /|\ |
200  * |/ |/ | \|
201  * 12-13-14-15
202  */
203 Geometry BorderRenderer::CreateBorderGeometry()
204 {
205   const float halfWidth = 0.5f;
206   const float halfHeight = 0.5f;
207   struct BorderVertex { Vector2 position; Vector2 drift;};
208   BorderVertex borderVertexData[16] =
209   {
210       { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 0.f) },
211       { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 0.f) },
212       { Vector2(halfWidth, -halfHeight),  Vector2(-1.f, 0.f) },
213       { Vector2(halfWidth, -halfHeight),  Vector2(0.f, 0.f) },
214
215       { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 1.f) },
216       { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 1.f) },
217       { Vector2(halfWidth, -halfHeight),  Vector2(-1.f, 1.f) },
218       { Vector2(halfWidth, -halfHeight),  Vector2(0.f, 1.f) },
219
220       { Vector2(-halfWidth, halfHeight), Vector2(0.f, -1.f) },
221       { Vector2(-halfWidth, halfHeight), Vector2(1.f, -1.f) },
222       { Vector2(halfWidth, halfHeight),  Vector2(-1.f, -1.f) },
223       { Vector2(halfWidth, halfHeight),  Vector2(0.f, -1.f) },
224
225       { Vector2(-halfWidth, halfHeight), Vector2(0.f, 0.f) },
226       { Vector2(-halfWidth, halfHeight), Vector2(1.f, 0.f) },
227       { Vector2(halfWidth, halfHeight),  Vector2(-1.f, 0.f) },
228       { Vector2(halfWidth, halfHeight),  Vector2(0.f, 0.f) },
229   };
230
231   Property::Map borderVertexFormat;
232   borderVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
233   borderVertexFormat[DRIFT_ATTRIBUTE_NAME] = Property::VECTOR2;
234   PropertyBuffer borderVertices = PropertyBuffer::New( borderVertexFormat, 16 );
235   borderVertices.SetData(borderVertexData);
236
237   // Create indices
238   unsigned int indexData[24] = { 0,4,1,5,2,6,3,7,7,6,11,10,15,14,14,10,13,9,12,8,8,9,4,5 };
239   Property::Map indexFormat;
240   indexFormat[INDEX_NAME] = Property::INTEGER;
241   PropertyBuffer indices = PropertyBuffer::New( indexFormat, 24 );
242   indices.SetData(indexData);
243
244   // Create the geometry object
245   Geometry geometry = Geometry::New();
246   geometry.AddVertexBuffer( borderVertices );
247   geometry.SetIndexBuffer( indices );
248   geometry.SetGeometryType( Geometry::TRIANGLE_STRIP );
249
250   return geometry;
251 }
252
253 } // namespace Internal
254
255 } // namespace Toolkit
256
257 } // namespace Dali