19872bd350507ef9a2efa5ff7ec5ac9e429452de
[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("borderRenderer");
42
43 const char * const COLOR_NAME("borderColor");
44 const char * const SIZE_NAME("borderSize");
45
46 const char * const POSITION_ATTRIBUTE_NAME("aPosition");
47 const char * const DRIFT_ATTRIBUTE_NAME("aDrift");
48 const char * const INDEX_NAME("indices");
49
50
51 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
52   attribute mediump vec2 aPosition;\n
53   attribute mediump vec2 aDrift;\n
54   uniform mediump mat4 uMvpMatrix;\n
55   uniform mediump vec3 uSize;\n
56   uniform mediump float borderSize;\n
57   \n
58   void main()\n
59   {\n
60     vec2 position = aPosition*uSize.xy + aDrift*borderSize;\n
61     gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n
62   }\n
63 );
64
65 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
66   uniform lowp vec4 uColor;\n
67   uniform lowp vec4 borderColor;\n
68   \n
69   void main()\n
70   {\n
71     gl_FragColor = borderColor*uColor;\n
72   }\n
73 );
74 }
75
76 BorderRenderer::BorderRenderer( RendererFactoryCache& factoryCache )
77 : ControlRenderer( factoryCache ),
78   mBorderColor( Color::TRANSPARENT ),
79   mBorderSize( 0.f ),
80   mBorderColorIndex( Property::INVALID_INDEX ),
81   mBorderSizeIndex( Property::INVALID_INDEX )
82 {
83 }
84
85 BorderRenderer::~BorderRenderer()
86 {
87 }
88
89 void BorderRenderer::DoInitialize( Actor& actor, const Property::Map& propertyMap )
90 {
91   Property::Value* color = propertyMap.Find( COLOR_NAME );
92   if( !( color && color->Get(mBorderColor) ) )
93   {
94     DALI_LOG_ERROR( "Fail to provide a border color to the BorderRenderer object" );
95   }
96
97   Property::Value* size = propertyMap.Find( SIZE_NAME );
98   if( !( size && size->Get(mBorderSize) ) )
99   {
100     DALI_LOG_ERROR( "Fail to provide a border size to the BorderRenderer object" );
101   }
102 }
103
104 void BorderRenderer::SetClipRect( const Rect<int>& clipRect )
105 {
106   ControlRenderer::SetClipRect( clipRect );
107
108   //ToDo: renderer responds to the clipRect change
109 }
110
111 void BorderRenderer::DoSetOnStage( Actor& actor )
112 {
113   InitializeRenderer();
114
115   mBorderColorIndex = (mImpl->mRenderer).RegisterProperty( COLOR_NAME, mBorderColor );
116   if( mBorderColor.a < 1.f )
117   {
118     (mImpl->mRenderer).GetMaterial().SetBlendMode( BlendingMode::ON );
119   }
120   mBorderSizeIndex = (mImpl->mRenderer).RegisterProperty( SIZE_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()
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::BORDER_SHADER, shader );
145   }
146
147   Material material = Material::New( shader );
148   mImpl->mRenderer = Renderer::New( geometry, material );
149 }
150
151 void BorderRenderer::SetBorderColor(const Vector4& color)
152 {
153   mBorderColor = color;
154
155   if( mImpl->mRenderer )
156   {
157     (mImpl->mRenderer).SetProperty( mBorderColorIndex, color );
158     if( color.a < 1.f &&  (mImpl->mRenderer).GetMaterial().GetBlendMode() != BlendingMode::ON)
159     {
160       (mImpl->mRenderer).GetMaterial().SetBlendMode( BlendingMode::ON );
161     }
162   }
163 }
164
165 void BorderRenderer::SetBorderSize( float size )
166 {
167   mBorderSize = size;
168
169   if( mImpl->mRenderer )
170   {
171     (mImpl->mRenderer).SetProperty( mBorderSizeIndex, size );
172   }
173 }
174
175 /**
176  * Vertices and triangles of the border geometry:
177  *
178  * vertex position = aPosition*uSize.xy + aDrift*uBorderSize;
179  *
180  * 0--1--2--3
181  * | /| /| /|
182  * |/ |/ |/ |
183  * 4--5--6--7
184  * |\ |  |\ |
185  * | \|  | \|
186  * 8--9--10-11
187  * | /| /|\ |
188  * |/ |/ | \|
189  * 12-13-14-15
190  */
191 Geometry BorderRenderer::CreateBorderGeometry()
192 {
193   const float halfWidth = 0.5f;
194   const float halfHeight = 0.5f;
195   struct BorderVertex { Vector2 position; Vector2 drift;};
196   BorderVertex borderVertexData[16] =
197   {
198       { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 0.f) },
199       { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 0.f) },
200       { Vector2(halfWidth, -halfHeight),  Vector2(-1.f, 0.f) },
201       { Vector2(halfWidth, -halfHeight),  Vector2(0.f, 0.f) },
202
203       { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 1.f) },
204       { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 1.f) },
205       { Vector2(halfWidth, -halfHeight),  Vector2(-1.f, 1.f) },
206       { Vector2(halfWidth, -halfHeight),  Vector2(0.f, 1.f) },
207
208       { Vector2(-halfWidth, halfHeight), Vector2(0.f, -1.f) },
209       { Vector2(-halfWidth, halfHeight), Vector2(1.f, -1.f) },
210       { Vector2(halfWidth, halfHeight),  Vector2(-1.f, -1.f) },
211       { Vector2(halfWidth, halfHeight),  Vector2(0.f, -1.f) },
212
213       { Vector2(-halfWidth, halfHeight), Vector2(0.f, 0.f) },
214       { Vector2(-halfWidth, halfHeight), Vector2(1.f, 0.f) },
215       { Vector2(halfWidth, halfHeight),  Vector2(-1.f, 0.f) },
216       { Vector2(halfWidth, halfHeight),  Vector2(0.f, 0.f) },
217   };
218
219   Property::Map borderVertexFormat;
220   borderVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
221   borderVertexFormat[DRIFT_ATTRIBUTE_NAME] = Property::VECTOR2;
222   PropertyBuffer borderVertices = PropertyBuffer::New( borderVertexFormat, 16 );
223   borderVertices.SetData(borderVertexData);
224
225   // Create indices
226   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 };
227   Property::Map indexFormat;
228   indexFormat[INDEX_NAME] = Property::INTEGER;
229   PropertyBuffer indices = PropertyBuffer::New( indexFormat, 24 );
230   indices.SetData(indexData);
231
232   // Create the geometry object
233   Geometry geometry = Geometry::New();
234   geometry.AddVertexBuffer( borderVertices );
235   geometry.SetIndexBuffer( indices );
236   geometry.SetGeometryType( Geometry::TRIANGLE_STRIP );
237
238   return geometry;
239 }
240
241 } // namespace Internal
242
243 } // namespace Toolkit
244
245 } // namespace Dali