Updates following rename of PropertyBuffer to VertexBuffer
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / control / control-renderers.cpp
1 /*
2  * Copyright (c) 2019 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 // INTERNAL INCLUDES
18 #include <dali-toolkit/internal/controls/control/control-renderers.h>
19
20 namespace Dali
21 {
22 namespace Toolkit
23 {
24 namespace Internal
25 {
26
27 #define DALI_COMPOSE_SHADER(STR) #STR
28
29 const char * const BASIC_VERTEX_SOURCE = DALI_COMPOSE_SHADER(
30   precision mediump float;\n
31   attribute mediump vec2 aPosition;\n
32   varying mediump vec2 vTexCoord;\n
33   uniform mediump mat4 uMvpMatrix;\n
34   uniform mediump vec3 uSize;\n
35   \n
36   void main()\n
37   {\n
38     mediump vec4 vertexPosition = vec4(aPosition * uSize.xy, 0.0, 1.0);\n
39     vTexCoord = aPosition + vec2(0.5);
40     gl_Position = uMvpMatrix * vertexPosition;\n
41   }\n
42 );
43
44 const char * const BASIC_FRAGMENT_SOURCE = DALI_COMPOSE_SHADER(
45   precision mediump float;\n
46   varying mediump vec2 vTexCoord;\n
47   uniform sampler2D sTexture;\n
48   uniform vec4 uColor;\n
49   \n
50   void main()\n
51   {\n
52     gl_FragColor = texture2D(sTexture, vTexCoord);\n
53     gl_FragColor *= uColor;
54   }\n
55 );
56
57 Geometry CreateGridGeometry( Uint16Pair gridSize )
58 {
59   uint16_t gridWidth = gridSize.GetWidth();
60   uint16_t gridHeight = gridSize.GetHeight();
61
62   // Create vertices
63   Vector< Vector2 > vertices;
64   vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
65
66   for( int y = 0; y < gridHeight + 1; ++y )
67   {
68     for( int x = 0; x < gridWidth + 1; ++x )
69     {
70       vertices.PushBack( Vector2( (float)x/gridWidth - 0.5f, (float)y/gridHeight  - 0.5f) );
71     }
72   }
73
74   // Create indices
75   Vector< unsigned short > indices;
76   indices.Reserve( (gridWidth+2)*gridHeight*2 - 2);
77
78   for( unsigned int row = 0u; row < gridHeight; ++row )
79   {
80     unsigned int rowStartIndex = row*(gridWidth+1u);
81     unsigned int nextRowStartIndex = rowStartIndex + gridWidth +1u;
82
83     if( row != 0u ) // degenerate index on non-first row
84     {
85       indices.PushBack( rowStartIndex );
86     }
87
88     for( unsigned int column = 0u; column < gridWidth+1u; column++) // main strip
89     {
90       indices.PushBack( rowStartIndex + column);
91       indices.PushBack( nextRowStartIndex + column);
92     }
93
94     if( row != gridHeight-1u ) // degenerate index on non-last row
95     {
96       indices.PushBack( nextRowStartIndex + gridWidth );
97     }
98   }
99
100   Property::Map vertexFormat;
101   vertexFormat[ "aPosition" ] = Property::VECTOR2;
102   VertexBuffer vertexBuffer = VertexBuffer::New( vertexFormat );
103   if( vertices.Size() > 0 )
104   {
105     vertexBuffer.SetData( &vertices[ 0 ], vertices.Size() );
106   }
107
108   // Create the geometry object
109   Geometry geometry = Geometry::New();
110   geometry.AddVertexBuffer( vertexBuffer );
111   if( indices.Size() > 0 )
112   {
113     geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
114   }
115
116   geometry.SetType( Geometry::TRIANGLE_STRIP );
117
118   return geometry;
119 }
120
121 Dali::Renderer CreateRenderer( const char* vertexSrc, const char* fragmentSrc )
122 {
123   Dali::Shader shader = Dali::Shader::New( vertexSrc, fragmentSrc );
124
125   Dali::Geometry texturedQuadGeometry = Dali::Geometry::New();
126
127   struct VertexPosition { Dali::Vector2 position; };
128   struct VertexTexture { Dali::Vector2 texture; };
129
130   VertexPosition positionArray[] =
131   {
132     { Dali::Vector2( -0.5f, -0.5f ) },
133     { Dali::Vector2(  0.5f, -0.5f ) },
134     { Dali::Vector2( -0.5f,  0.5f ) },
135     { Dali::Vector2(  0.5f,  0.5f ) }
136   };
137   uint32_t numberOfVertices = sizeof(positionArray)/sizeof(VertexPosition);
138
139   Dali::Property::Map positionVertexFormat;
140   positionVertexFormat["aPosition"] = Dali::Property::VECTOR2;
141   Dali::VertexBuffer positionVertices = Dali::VertexBuffer::New( positionVertexFormat );
142   positionVertices.SetData( positionArray, numberOfVertices );
143   texturedQuadGeometry.AddVertexBuffer( positionVertices );
144
145   const uint16_t indices[] = { 0, 3, 1, 0, 2, 3 };
146   texturedQuadGeometry.SetIndexBuffer ( &indices[0], sizeof( indices )/ sizeof( indices[0] ) );
147
148   Dali::Renderer renderer = Dali::Renderer::New( texturedQuadGeometry, shader );
149
150   Dali::TextureSet textureSet = Dali::TextureSet::New();
151   renderer.SetTextures( textureSet );
152
153   return renderer;
154 }
155
156 Dali::Renderer CreateRenderer( const char* vertexSrc, const char* fragmentSrc, Dali::Shader::Hint::Value hints, Uint16Pair gridSize )
157 {
158   Dali::Shader shader = Dali::Shader::New( vertexSrc, fragmentSrc, hints );
159
160   Dali::Geometry gridGeometry = CreateGridGeometry( gridSize );
161
162   Dali::Renderer renderer = Dali::Renderer::New( gridGeometry, shader );
163
164   Dali::TextureSet textureSet = Dali::TextureSet::New();
165   renderer.SetTextures( textureSet );
166
167   return renderer;
168 }
169
170 void SetRendererTexture( Dali::Renderer renderer, Dali::Texture texture )
171 {
172   if( renderer )
173   {
174     Dali::TextureSet textureSet = renderer.GetTextures();
175     textureSet.SetTexture( 0u, texture );
176   }
177 }
178
179 void SetRendererTexture( Dali::Renderer renderer, Dali::FrameBuffer frameBuffer )
180 {
181   if( frameBuffer )
182   {
183     Dali::Texture texture = frameBuffer.GetColorTexture();
184     SetRendererTexture( renderer, texture );
185   }
186 }
187
188 } // namespace Internal
189 } // namespace Toolkit
190 } // namespace Dali