Merge "Fix resource ready state" into devel/master
[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 Geometry CreateGridGeometry( Uint16Pair gridSize )
28 {
29   uint16_t gridWidth = gridSize.GetWidth();
30   uint16_t gridHeight = gridSize.GetHeight();
31
32   // Create vertices
33   Vector< Vector2 > vertices;
34   vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
35
36   for( int y = 0; y < gridHeight + 1; ++y )
37   {
38     for( int x = 0; x < gridWidth + 1; ++x )
39     {
40       vertices.PushBack( Vector2( (float)x/gridWidth - 0.5f, (float)y/gridHeight  - 0.5f) );
41     }
42   }
43
44   // Create indices
45   Vector< unsigned short > indices;
46   indices.Reserve( (gridWidth+2)*gridHeight*2 - 2);
47
48   for( unsigned int row = 0u; row < gridHeight; ++row )
49   {
50     unsigned int rowStartIndex = row*(gridWidth+1u);
51     unsigned int nextRowStartIndex = rowStartIndex + gridWidth +1u;
52
53     if( row != 0u ) // degenerate index on non-first row
54     {
55       indices.PushBack( rowStartIndex );
56     }
57
58     for( unsigned int column = 0u; column < gridWidth+1u; column++) // main strip
59     {
60       indices.PushBack( rowStartIndex + column);
61       indices.PushBack( nextRowStartIndex + column);
62     }
63
64     if( row != gridHeight-1u ) // degenerate index on non-last row
65     {
66       indices.PushBack( nextRowStartIndex + gridWidth );
67     }
68   }
69
70   Property::Map vertexFormat;
71   vertexFormat[ "aPosition" ] = Property::VECTOR2;
72   VertexBuffer vertexBuffer = VertexBuffer::New( vertexFormat );
73   if( vertices.Size() > 0 )
74   {
75     vertexBuffer.SetData( &vertices[ 0 ], vertices.Size() );
76   }
77
78   // Create the geometry object
79   Geometry geometry = Geometry::New();
80   geometry.AddVertexBuffer( vertexBuffer );
81   if( indices.Size() > 0 )
82   {
83     geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
84   }
85
86   geometry.SetType( Geometry::TRIANGLE_STRIP );
87
88   return geometry;
89 }
90
91 Dali::Renderer CreateRenderer( std::string_view vertexSrc, std::string_view fragmentSrc )
92 {
93   Dali::Shader shader = Dali::Shader::New( vertexSrc, fragmentSrc );
94
95   Dali::Geometry texturedQuadGeometry = Dali::Geometry::New();
96
97   struct VertexPosition { Dali::Vector2 position; };
98   struct VertexTexture { Dali::Vector2 texture; };
99
100   VertexPosition positionArray[] =
101   {
102     { Dali::Vector2( -0.5f, -0.5f ) },
103     { Dali::Vector2(  0.5f, -0.5f ) },
104     { Dali::Vector2( -0.5f,  0.5f ) },
105     { Dali::Vector2(  0.5f,  0.5f ) }
106   };
107   uint32_t numberOfVertices = sizeof(positionArray)/sizeof(VertexPosition);
108
109   Dali::Property::Map positionVertexFormat;
110   positionVertexFormat["aPosition"] = Dali::Property::VECTOR2;
111   Dali::VertexBuffer positionVertices = Dali::VertexBuffer::New( positionVertexFormat );
112   positionVertices.SetData( positionArray, numberOfVertices );
113   texturedQuadGeometry.AddVertexBuffer( positionVertices );
114
115   const uint16_t indices[] = { 0, 3, 1, 0, 2, 3 };
116   texturedQuadGeometry.SetIndexBuffer ( &indices[0], sizeof( indices )/ sizeof( indices[0] ) );
117
118   Dali::Renderer renderer = Dali::Renderer::New( texturedQuadGeometry, shader );
119
120   Dali::TextureSet textureSet = Dali::TextureSet::New();
121   renderer.SetTextures( textureSet );
122
123   return renderer;
124 }
125
126 Dali::Renderer CreateRenderer( std::string_view vertexSrc, std::string_view fragmentSrc, Dali::Shader::Hint::Value hints, Uint16Pair gridSize )
127 {
128   Dali::Shader shader = Dali::Shader::New( vertexSrc, fragmentSrc, hints );
129
130   Dali::Geometry gridGeometry = CreateGridGeometry( gridSize );
131
132   Dali::Renderer renderer = Dali::Renderer::New( gridGeometry, shader );
133
134   Dali::TextureSet textureSet = Dali::TextureSet::New();
135   renderer.SetTextures( textureSet );
136
137   return renderer;
138 }
139
140 void SetRendererTexture( Dali::Renderer renderer, Dali::Texture texture )
141 {
142   if( renderer )
143   {
144     Dali::TextureSet textureSet = renderer.GetTextures();
145     textureSet.SetTexture( 0u, texture );
146   }
147 }
148
149 void SetRendererTexture( Dali::Renderer renderer, Dali::FrameBuffer frameBuffer )
150 {
151   if( frameBuffer )
152   {
153     Dali::Texture texture = frameBuffer.GetColorTexture();
154     SetRendererTexture( renderer, texture );
155   }
156 }
157
158 } // namespace Internal
159 } // namespace Toolkit
160 } // namespace Dali