6432ae0a9ae0e03972b83574c51283806bf9cd40
[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("rendererType");
41 const char * const RENDERER_TYPE_VALUE("border");
42
43 const char * const COLOR_NAME("borderColor");
44 const char * const SIZE_NAME("borderSize");
45 const char * const ANTI_ALIASING("antiAliasing");
46
47 const char * const POSITION_ATTRIBUTE_NAME("aPosition");
48 const char * const DRIFT_ATTRIBUTE_NAME("aDrift");
49 const char * const INDEX_NAME("indices");
50
51
52 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
53   attribute mediump vec2 aPosition;\n
54   attribute mediump vec2 aDrift;\n
55   uniform mediump mat4 uMvpMatrix;\n
56   uniform mediump vec3 uSize;\n
57   uniform mediump float borderSize;\n
58   \n
59   void main()\n
60   {\n
61     vec2 position = aPosition*uSize.xy + aDrift*borderSize;\n
62     gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n
63   }\n
64 );
65
66 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
67   uniform lowp vec4 uColor;\n
68   uniform lowp vec4 borderColor;\n
69   \n
70   void main()\n
71   {\n
72     gl_FragColor = borderColor*uColor;\n
73   }\n
74 );
75
76 const char* VERTEX_SHADER_ANTI_ALIASING = DALI_COMPOSE_SHADER(
77   attribute mediump vec2 aPosition;\n
78   attribute mediump vec2 aDrift;\n
79   uniform mediump mat4 uMvpMatrix;\n
80   uniform mediump vec3 uSize;\n
81   uniform mediump float borderSize;\n
82   varying mediump float vAlpha;\n
83   \n
84   void main()\n
85   {\n
86     vec2 position = aPosition*(uSize.xy+vec2(0.75)) + aDrift*(borderSize+1.5);\n
87     gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n
88     vAlpha = min( abs(aDrift.x), abs(aDrift.y) )*(borderSize+1.5);
89   }\n
90 );
91
92 const char* FRAGMENT_SHADER_ANTI_ALIASING = DALI_COMPOSE_SHADER(
93   uniform lowp vec4 uColor;\n
94   uniform lowp vec4 borderColor;\n
95   uniform mediump float borderSize;\n
96   varying mediump float vAlpha;\n
97   \n
98   void main()\n
99   {\n
100     gl_FragColor = borderColor*uColor;\n
101     gl_FragColor.a *= smoothstep(0.0, 1.5, vAlpha)*smoothstep( borderSize+1.5, borderSize, vAlpha );\n
102   }\n
103 );
104 }
105
106 BorderRenderer::BorderRenderer( RendererFactoryCache& factoryCache )
107 : ControlRenderer( factoryCache ),
108   mBorderColor( Color::TRANSPARENT ),
109   mBorderSize( 0.f ),
110   mBorderColorIndex( Property::INVALID_INDEX ),
111   mBorderSizeIndex( Property::INVALID_INDEX ),
112   mAntiAliasing( false )
113 {
114 }
115
116 BorderRenderer::~BorderRenderer()
117 {
118 }
119
120 void BorderRenderer::DoInitialize( Actor& actor, const Property::Map& propertyMap )
121 {
122   Property::Value* color = propertyMap.Find( COLOR_NAME );
123   if( !( color && color->Get(mBorderColor) ) )
124   {
125     DALI_LOG_ERROR( "Fail to provide a border color to the BorderRenderer object" );
126   }
127
128   Property::Value* size = propertyMap.Find( SIZE_NAME );
129   if( !( size && size->Get(mBorderSize) ) )
130   {
131     DALI_LOG_ERROR( "Fail to provide a border size to the BorderRenderer object" );
132   }
133
134   Property::Value* antiAliasing = propertyMap.Find( ANTI_ALIASING );
135   if( antiAliasing )
136   {
137     antiAliasing->Get( mAntiAliasing );
138   }
139 }
140
141 void BorderRenderer::SetClipRect( const Rect<int>& clipRect )
142 {
143   ControlRenderer::SetClipRect( clipRect );
144
145   //ToDo: renderer responds to the clipRect change
146 }
147
148 void BorderRenderer::DoSetOnStage( Actor& actor )
149 {
150   InitializeRenderer();
151
152   mBorderColorIndex = (mImpl->mRenderer).RegisterProperty( COLOR_NAME, mBorderColor );
153   if( mBorderColor.a < 1.f || mAntiAliasing)
154   {
155     mImpl->mRenderer.SetProperty( Renderer::Property::BLENDING_MODE, BlendingMode::ON );
156   }
157   mBorderSizeIndex = (mImpl->mRenderer).RegisterProperty( SIZE_NAME, mBorderSize );
158 }
159
160 void BorderRenderer::DoCreatePropertyMap( Property::Map& map ) const
161 {
162   map.Clear();
163   map.Insert( RENDERER_TYPE, RENDERER_TYPE_VALUE );
164   map.Insert( COLOR_NAME, mBorderColor );
165   map.Insert( SIZE_NAME, mBorderSize );
166 }
167
168 void BorderRenderer::InitializeRenderer()
169 {
170   Geometry geometry = mFactoryCache.GetGeometry( RendererFactoryCache::BORDER_GEOMETRY );
171   if( !geometry )
172   {
173     geometry =  CreateBorderGeometry();
174     mFactoryCache.SaveGeometry( RendererFactoryCache::BORDER_GEOMETRY, geometry );
175   }
176
177   Material material = Material::New( GetBorderShader() );
178   mImpl->mRenderer = Renderer::New( geometry, material );
179 }
180
181 void BorderRenderer::SetBorderColor(const Vector4& color)
182 {
183   mBorderColor = color;
184
185   if( mImpl->mRenderer )
186   {
187     (mImpl->mRenderer).SetProperty( mBorderColorIndex, color );
188     if( color.a < 1.f )
189     {
190       mImpl->mRenderer.SetProperty( Renderer::Property::BLENDING_MODE, BlendingMode::ON );
191     }
192   }
193 }
194
195 void BorderRenderer::SetBorderSize( float size )
196 {
197   mBorderSize = size;
198
199   if( mImpl->mRenderer )
200   {
201     (mImpl->mRenderer).SetProperty( mBorderSizeIndex, size );
202   }
203 }
204
205 void BorderRenderer::RequireAntiAliasing( bool antiAliasing )
206 {
207   if( mAntiAliasing != antiAliasing )
208   {
209     mAntiAliasing = antiAliasing;
210     if( mImpl->mRenderer )
211     {
212       Material material =  mImpl->mRenderer.GetMaterial();
213       Shader shader = GetBorderShader();
214       material.SetShader( shader );
215       if( mAntiAliasing )
216       {
217         mImpl->mRenderer.SetProperty( Renderer::Property::BLENDING_MODE, BlendingMode::ON );
218       }
219     }
220   }
221 }
222
223 Shader BorderRenderer::GetBorderShader()
224 {
225   Shader shader;
226   if( mAntiAliasing )
227   {
228     shader = mFactoryCache.GetShader( RendererFactoryCache::BORDER_SHADER_ANTI_ALIASING );
229     if( !shader )
230     {
231       shader = Shader::New( VERTEX_SHADER_ANTI_ALIASING, FRAGMENT_SHADER_ANTI_ALIASING );
232       mFactoryCache.SaveShader( RendererFactoryCache::BORDER_SHADER_ANTI_ALIASING, shader );
233     }
234   }
235   else
236   {
237     shader = mFactoryCache.GetShader( RendererFactoryCache::BORDER_SHADER );
238     if( !shader )
239     {
240       shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
241       mFactoryCache.SaveShader( RendererFactoryCache::BORDER_SHADER, shader );
242     }
243   }
244
245   return shader;
246 }
247
248 /**
249  * Vertices and triangles of the border geometry:
250  *
251  * vertex position = aPosition*uSize.xy + aDrift*uBorderSize;
252  *
253  * 0--1--2--3
254  * |\ | /| /|
255  * | \|/ |/ |
256  * 4--5--6--7
257  * |\ |  |\ |
258  * | \|  | \|
259  * 8--9--10-11
260  * | /| /|\ |
261  * |/ |/ | \|
262  * 12-13-14-15
263  */
264 Geometry BorderRenderer::CreateBorderGeometry()
265 {
266   const float halfWidth = 0.5f;
267   const float halfHeight = 0.5f;
268   struct BorderVertex { Vector2 position; Vector2 drift;};
269   BorderVertex borderVertexData[16] =
270   {
271       { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 0.f) },
272       { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 0.f) },
273       { Vector2(halfWidth, -halfHeight),  Vector2(-1.f, 0.f) },
274       { Vector2(halfWidth, -halfHeight),  Vector2(0.f, 0.f) },
275
276       { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 1.f) },
277       { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 1.f) },
278       { Vector2(halfWidth, -halfHeight),  Vector2(-1.f, 1.f) },
279       { Vector2(halfWidth, -halfHeight),  Vector2(0.f, 1.f) },
280
281       { Vector2(-halfWidth, halfHeight), Vector2(0.f, -1.f) },
282       { Vector2(-halfWidth, halfHeight), Vector2(1.f, -1.f) },
283       { Vector2(halfWidth, halfHeight),  Vector2(-1.f, -1.f) },
284       { Vector2(halfWidth, halfHeight),  Vector2(0.f, -1.f) },
285
286       { Vector2(-halfWidth, halfHeight), Vector2(0.f, 0.f) },
287       { Vector2(-halfWidth, halfHeight), Vector2(1.f, 0.f) },
288       { Vector2(halfWidth, halfHeight),  Vector2(-1.f, 0.f) },
289       { Vector2(halfWidth, halfHeight),  Vector2(0.f, 0.f) },
290   };
291
292   Property::Map borderVertexFormat;
293   borderVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
294   borderVertexFormat[DRIFT_ATTRIBUTE_NAME] = Property::VECTOR2;
295   PropertyBuffer borderVertices = PropertyBuffer::New( borderVertexFormat );
296   borderVertices.SetData( borderVertexData, 16 );
297
298   // Create indices
299   unsigned int indexData[24] = { 1,5,2,6,3,7,7,6,11,10,15,14,14,10,13,9,12,8,8,9,4,5,0,1};
300   Property::Map indexFormat;
301   indexFormat[INDEX_NAME] = Property::INTEGER;
302   PropertyBuffer indices = PropertyBuffer::New( indexFormat );
303   indices.SetData( indexData, 24 );
304
305   // Create the geometry object
306   Geometry geometry = Geometry::New();
307   geometry.AddVertexBuffer( borderVertices );
308   geometry.SetIndexBuffer( indices );
309   geometry.SetGeometryType( Geometry::TRIANGLE_STRIP );
310
311   return geometry;
312 }
313
314 } // namespace Internal
315
316 } // namespace Toolkit
317
318 } // namespace Dali