2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "border-renderer.h"
22 #include <dali/integration-api/debug.h>
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>
40 const char * const RENDERER_TYPE("rendererType");
41 const char * const RENDERER_TYPE_VALUE("borderRenderer");
43 const char * const COLOR_NAME("borderColor");
44 const char * const COLOR_UNIFORM_NAME("uBorderColor");
45 const char * const SIZE_NAME("borderSize");
46 const char * const SIZE_UNIFORM_NAME("uBorderSize");
48 const char * const POSITION_ATTRIBUTE_NAME("aPosition");
49 const char * const DRIFT_ATTRIBUTE_NAME("aDrift");
50 const char * const INDEX_NAME("indices");
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
62 vec2 position = aPosition*uSize.xy + aDrift*uBorderSize;\n
63 gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n
67 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
68 uniform lowp vec4 uColor;\n
69 uniform lowp vec4 uBorderColor;\n
73 gl_FragColor = uBorderColor*uColor;\n
78 BorderRenderer::BorderRenderer( RendererFactoryCache& factoryCache )
79 : ControlRenderer( factoryCache ),
80 mBorderColor( Color::TRANSPARENT ),
82 mBorderColorIndex( Property::INVALID_INDEX ),
83 mBorderSizeIndex( Property::INVALID_INDEX )
87 BorderRenderer::~BorderRenderer()
91 void BorderRenderer::DoInitialize( Actor& actor, const Property::Map& propertyMap )
93 Property::Value* color = propertyMap.Find( COLOR_NAME );
94 if( !( color && color->Get(mBorderColor) ) )
96 DALI_LOG_ERROR( "Fail to provide a border color to the BorderRenderer object" );
99 Property::Value* size = propertyMap.Find( SIZE_NAME );
100 if( !( size && size->Get(mBorderSize) ) )
102 DALI_LOG_ERROR( "Fail to provide a border size to the BorderRenderer object" );
106 void BorderRenderer::SetClipRect( const Rect<int>& clipRect )
108 ControlRenderer::SetClipRect( clipRect );
110 //ToDo: renderer responds to the clipRect change
113 void BorderRenderer::DoSetOnStage( Actor& actor )
115 InitializeRenderer();
117 mBorderColorIndex = (mImpl->mRenderer).RegisterProperty( COLOR_UNIFORM_NAME, mBorderColor );
118 if( mBorderColor.a < 1.f )
120 (mImpl->mRenderer).GetMaterial().SetBlendMode( BlendingMode::ON );
122 mBorderSizeIndex = (mImpl->mRenderer).RegisterProperty( SIZE_UNIFORM_NAME, mBorderSize );
125 void BorderRenderer::DoCreatePropertyMap( Property::Map& map ) const
128 map.Insert( RENDERER_TYPE, RENDERER_TYPE_VALUE );
129 map.Insert( COLOR_NAME, mBorderColor );
130 map.Insert( SIZE_NAME, mBorderSize );
133 void BorderRenderer::InitializeRenderer()
135 Geometry geometry = mFactoryCache.GetGeometry( RendererFactoryCache::BORDER_GEOMETRY );
138 geometry = CreateBorderGeometry();
139 mFactoryCache.SaveGeometry( RendererFactoryCache::BORDER_GEOMETRY, geometry );
142 Shader shader = mFactoryCache.GetShader( RendererFactoryCache::BORDER_SHADER );
145 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
146 mFactoryCache.SaveShader( RendererFactoryCache::BORDER_SHADER, shader );
149 Material material = Material::New( shader );
150 mImpl->mRenderer = Renderer::New( geometry, material );
153 void BorderRenderer::SetBorderColor(const Vector4& color)
155 mBorderColor = color;
157 if( mImpl->mRenderer )
159 (mImpl->mRenderer).SetProperty( mBorderColorIndex, color );
160 if( color.a < 1.f && (mImpl->mRenderer).GetMaterial().GetBlendMode() != BlendingMode::ON)
162 (mImpl->mRenderer).GetMaterial().SetBlendMode( BlendingMode::ON );
167 void BorderRenderer::SetBorderSize( float size )
171 if( mImpl->mRenderer )
173 (mImpl->mRenderer).SetProperty( mBorderSizeIndex, size );
178 * Vertices and triangles of the border geometry:
180 * vertex position = aPosition*uSize.xy + aDrift*uBorderSize;
193 Geometry BorderRenderer::CreateBorderGeometry()
195 const float halfWidth = 0.5f;
196 const float halfHeight = 0.5f;
197 struct BorderVertex { Vector2 position; Vector2 drift;};
198 BorderVertex borderVertexData[16] =
200 { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 0.f) },
201 { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 0.f) },
202 { Vector2(halfWidth, -halfHeight), Vector2(-1.f, 0.f) },
203 { Vector2(halfWidth, -halfHeight), Vector2(0.f, 0.f) },
205 { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 1.f) },
206 { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 1.f) },
207 { Vector2(halfWidth, -halfHeight), Vector2(-1.f, 1.f) },
208 { Vector2(halfWidth, -halfHeight), Vector2(0.f, 1.f) },
210 { Vector2(-halfWidth, halfHeight), Vector2(0.f, -1.f) },
211 { Vector2(-halfWidth, halfHeight), Vector2(1.f, -1.f) },
212 { Vector2(halfWidth, halfHeight), Vector2(-1.f, -1.f) },
213 { Vector2(halfWidth, halfHeight), Vector2(0.f, -1.f) },
215 { Vector2(-halfWidth, halfHeight), Vector2(0.f, 0.f) },
216 { Vector2(-halfWidth, halfHeight), Vector2(1.f, 0.f) },
217 { Vector2(halfWidth, halfHeight), Vector2(-1.f, 0.f) },
218 { Vector2(halfWidth, halfHeight), Vector2(0.f, 0.f) },
221 Property::Map borderVertexFormat;
222 borderVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
223 borderVertexFormat[DRIFT_ATTRIBUTE_NAME] = Property::VECTOR2;
224 PropertyBuffer borderVertices = PropertyBuffer::New( borderVertexFormat, 16 );
225 borderVertices.SetData(borderVertexData);
228 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 };
229 Property::Map indexFormat;
230 indexFormat[INDEX_NAME] = Property::INTEGER;
231 PropertyBuffer indices = PropertyBuffer::New( indexFormat, 24 );
232 indices.SetData(indexData);
234 // Create the geometry object
235 Geometry geometry = Geometry::New();
236 geometry.AddVertexBuffer( borderVertices );
237 geometry.SetIndexBuffer( indices );
238 geometry.SetGeometryType( Geometry::TRIANGLE_STRIP );
243 } // namespace Internal
245 } // namespace Toolkit