7ab33c7612f0f3fb777623bd52d6e49f096c95a1
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / renderers / debug / debug-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
19 // CLASS HEADER
20 #include "debug-renderer.h"
21
22 //INTERNAL INCLUDES
23 #include <dali-toolkit/internal/controls/renderers/renderer-factory-impl.h>
24 #include <dali-toolkit/internal/controls/renderers/renderer-factory-cache.h>
25 #include <dali-toolkit/internal/controls/renderers/control-renderer-data-impl.h>
26
27 namespace Dali
28 {
29
30 namespace Toolkit
31 {
32
33 namespace Internal
34 {
35
36 namespace
37 {
38 const char * const RENDERER_TYPE("rendererType");
39 const char * const RENDERER_TYPE_VALUE("debug");
40
41 const char * const POSITION_ATTRIBUTE_NAME("aPosition");
42 const char * const INDEX_NAME("indices");
43
44 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
45 attribute mediump vec2  aPosition;\n
46 uniform   mediump mat4  uMvpMatrix;\n
47 uniform   mediump vec3  uSize;\n
48 \n
49 void main()\n
50 {\n
51   mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
52   vertexPosition.xyz *= uSize;\n
53   gl_Position = uMvpMatrix * vertexPosition;\n
54 }\n
55 );
56
57 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(\n
58 uniform lowp vec4 uColor;\n
59 \n
60 void main()\n
61 {\n
62   gl_FragColor = uColor;\n
63 }\n
64 );
65
66 }
67
68
69 DebugRenderer::DebugRenderer( RendererFactoryCache& factoryCache )
70 : ControlRenderer( factoryCache )
71 {
72 }
73
74 DebugRenderer::~DebugRenderer()
75 {}
76
77 void DebugRenderer::DoSetOnStage( Actor& actor )
78 {
79   InitializeRenderer();
80 }
81
82 void DebugRenderer::DoCreatePropertyMap( Property::Map& map ) const
83 {
84   map.Clear();
85   map.Insert( RENDERER_TYPE, RENDERER_TYPE_VALUE );
86 }
87
88 void DebugRenderer::InitializeRenderer()
89 {
90   mImpl->mRenderer = mFactoryCache.GetDebugRenderer();
91   if( !mImpl->mRenderer )
92   {
93     Geometry geometry = CreateQuadWireframeGeometry();
94     Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
95     Material material = Material::New( shader );
96     mImpl->mRenderer = Renderer::New( geometry,
97                                       material );
98     mFactoryCache.CacheDebugRenderer( mImpl->mRenderer );
99   }
100 }
101
102 Geometry DebugRenderer::CreateQuadWireframeGeometry()
103 {
104   const float halfWidth = 0.5f;
105   const float halfHeight = 0.5f;
106   struct QuadVertex { Vector2 position;};
107   QuadVertex quadVertexData[4] =
108   {
109       { Vector2(-halfWidth, -halfHeight) },
110       { Vector2( halfWidth, -halfHeight) },
111       { Vector2( halfWidth,  halfHeight) },
112       { Vector2(-halfWidth,  halfHeight) }
113   };
114
115   Property::Map quadVertexFormat;
116   quadVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
117   PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat );
118   quadVertices.SetData( quadVertexData, 4 );
119
120   // Create indices
121   unsigned int indexData[10] = { 0, 1, 1, 2, 2, 3, 3, 0 };
122   Property::Map indexFormat;
123   indexFormat[INDEX_NAME] = Property::INTEGER;
124   PropertyBuffer indices = PropertyBuffer::New( indexFormat );
125   indices.SetData( indexData, sizeof(indexData)/sizeof(indexData[0]) );
126
127   // Create the geometry object
128   Geometry geometry = Geometry::New();
129   geometry.AddVertexBuffer( quadVertices );
130   geometry.SetIndexBuffer( indices );
131   geometry.SetGeometryType( Geometry::LINES );
132
133   return geometry;
134 }
135
136 } // namespace Internal
137
138 } // namespace Toolkit
139
140 } // namespace Dali