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