VertexBuffer can now be created from Property::Map by initializer list
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-VertexBuffer.cpp
1 /*
2  * Copyright (c) 2023 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 #include <dali-test-suite-utils.h>
19 #include <dali/public-api/dali-core.h>
20
21 using namespace Dali;
22
23 #include <mesh-builder.h>
24
25 void vertexBuffer_test_startup(void)
26 {
27   test_return_value = TET_UNDEF;
28 }
29
30 void vertexBuffer_test_cleanup(void)
31 {
32   test_return_value = TET_PASS;
33 }
34
35 int UtcDaliVertexBufferNew01(void)
36 {
37   TestApplication application;
38
39   Property::Map texturedQuadVertexFormat;
40   texturedQuadVertexFormat["aPosition"]    = Property::VECTOR2;
41   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
42
43   VertexBuffer vertexBuffer = VertexBuffer::New(texturedQuadVertexFormat);
44
45   DALI_TEST_EQUALS((bool)vertexBuffer, true, TEST_LOCATION);
46   END_TEST;
47 }
48
49 int UtcDaliVertexBufferNew02(void)
50 {
51   TestApplication application;
52   VertexBuffer    vertexBuffer;
53   DALI_TEST_EQUALS((bool)vertexBuffer, false, TEST_LOCATION);
54   END_TEST;
55 }
56
57 int UtcDaliVertexBufferDownCast01(void)
58 {
59   TestApplication application;
60
61   Property::Map texturedQuadVertexFormat;
62   texturedQuadVertexFormat["aPosition"]    = Property::VECTOR2;
63   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
64
65   VertexBuffer vertexBuffer = VertexBuffer::New(texturedQuadVertexFormat);
66
67   BaseHandle   handle(vertexBuffer);
68   VertexBuffer vertexBuffer2 = VertexBuffer::DownCast(handle);
69   DALI_TEST_EQUALS((bool)vertexBuffer2, true, TEST_LOCATION);
70   END_TEST;
71 }
72
73 int UtcDaliVertexBufferDownCast02(void)
74 {
75   TestApplication application;
76
77   Handle       handle       = Handle::New(); // Create a custom object
78   VertexBuffer vertexBuffer = VertexBuffer::DownCast(handle);
79   DALI_TEST_EQUALS((bool)vertexBuffer, false, TEST_LOCATION);
80   END_TEST;
81 }
82
83 int UtcDaliVertexBufferCopyConstructor(void)
84 {
85   TestApplication application;
86
87   VertexBuffer vertexBuffer = CreateVertexBuffer();
88
89   VertexBuffer vertexBufferCopy(vertexBuffer);
90
91   DALI_TEST_EQUALS((bool)vertexBufferCopy, true, TEST_LOCATION);
92   DALI_TEST_EQUALS(vertexBufferCopy.GetSize(), 0u, TEST_LOCATION);
93
94   END_TEST;
95 }
96
97 int UtcDaliVertexBufferAssignmentOperator(void)
98 {
99   TestApplication application;
100
101   VertexBuffer vertexBuffer = CreateVertexBuffer();
102
103   VertexBuffer vertexBuffer2;
104   DALI_TEST_EQUALS((bool)vertexBuffer2, false, TEST_LOCATION);
105
106   vertexBuffer2 = vertexBuffer;
107   DALI_TEST_EQUALS((bool)vertexBuffer2, true, TEST_LOCATION);
108   DALI_TEST_EQUALS(vertexBuffer2.GetSize(), 0u, TEST_LOCATION);
109
110   END_TEST;
111 }
112
113 int UtcDaliVertexBufferMoveConstructor(void)
114 {
115   TestApplication application;
116
117   VertexBuffer vertexBuffer = CreateVertexBuffer();
118   DALI_TEST_CHECK(vertexBuffer);
119   DALI_TEST_EQUALS(1, vertexBuffer.GetBaseObject().ReferenceCount(), TEST_LOCATION);
120   DALI_TEST_EQUALS(0u, vertexBuffer.GetSize(), TEST_LOCATION);
121
122   VertexBuffer move = std::move(vertexBuffer);
123   DALI_TEST_CHECK(move);
124   DALI_TEST_EQUALS(1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION);
125   DALI_TEST_EQUALS(0u, move.GetSize(), TEST_LOCATION);
126   DALI_TEST_CHECK(!vertexBuffer);
127
128   END_TEST;
129 }
130
131 int UtcDaliVertexBufferMoveAssignment(void)
132 {
133   TestApplication application;
134
135   VertexBuffer vertexBuffer = CreateVertexBuffer();
136   DALI_TEST_CHECK(vertexBuffer);
137   DALI_TEST_EQUALS(1, vertexBuffer.GetBaseObject().ReferenceCount(), TEST_LOCATION);
138   DALI_TEST_EQUALS(0u, vertexBuffer.GetSize(), TEST_LOCATION);
139
140   VertexBuffer move;
141   move = std::move(vertexBuffer);
142   DALI_TEST_CHECK(move);
143   DALI_TEST_EQUALS(1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION);
144   DALI_TEST_EQUALS(0u, move.GetSize(), TEST_LOCATION);
145   DALI_TEST_CHECK(!vertexBuffer);
146
147   END_TEST;
148 }
149
150 int UtcDaliVertexBufferSetData01(void)
151 {
152   TestApplication application;
153
154   Property::Map texturedQuadVertexFormat;
155   texturedQuadVertexFormat["aPosition"]    = Property::VECTOR2;
156   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
157
158   {
159     VertexBuffer vertexBuffer = VertexBuffer::New(texturedQuadVertexFormat);
160     DALI_TEST_EQUALS((bool)vertexBuffer, true, TEST_LOCATION);
161
162     const float halfQuadSize = .5f;
163     struct TexturedQuadVertex
164     {
165       Vector2 position;
166       Vector2 textureCoordinates;
167     };
168     TexturedQuadVertex texturedQuadVertexData[4] = {
169       {Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f)},
170       {Vector2(halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f)},
171       {Vector2(-halfQuadSize, halfQuadSize), Vector2(0.f, 1.f)},
172       {Vector2(halfQuadSize, halfQuadSize), Vector2(1.f, 1.f)}};
173
174     vertexBuffer.SetData(texturedQuadVertexData, 4);
175
176     Geometry geometry = Geometry::New();
177     geometry.AddVertexBuffer(vertexBuffer);
178
179     Shader   shader   = CreateShader();
180     Renderer renderer = Renderer::New(geometry, shader);
181     Actor    actor    = Actor::New();
182     actor.SetProperty(Actor::Property::SIZE, Vector3::ONE * 100.f);
183     actor.AddRenderer(renderer);
184     application.GetScene().Add(actor);
185
186     application.SendNotification();
187     application.Render(0);
188     application.Render();
189     application.SendNotification();
190
191     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
192       application.GetGlAbstraction().GetBufferDataCalls();
193
194     DALI_TEST_EQUALS(bufferDataCalls.size(), 3u, TEST_LOCATION);
195
196     DALI_TEST_EQUALS(bufferDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION);
197   }
198   // end of scope to let the buffer and geometry die; do another notification and render to get the deletion processed
199   application.SendNotification();
200   application.Render(0);
201
202   END_TEST;
203 }
204
205 int UtcDaliVertexBufferSetData02(void)
206 {
207   TestApplication application;
208
209   Property::Map texturedQuadVertexFormat;
210   texturedQuadVertexFormat["aPosition"]    = Property::VECTOR2;
211   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
212
213   VertexBuffer vertexBuffer = VertexBuffer::New(texturedQuadVertexFormat);
214   DALI_TEST_EQUALS((bool)vertexBuffer, true, TEST_LOCATION);
215
216   const float halfQuadSize = .5f;
217   struct TexturedQuadVertex
218   {
219     Vector2 position;
220     Vector2 textureCoordinates;
221   };
222   TexturedQuadVertex texturedQuadVertexData[4] = {
223     {Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f)},
224     {Vector2(halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f)},
225     {Vector2(-halfQuadSize, halfQuadSize), Vector2(0.f, 1.f)},
226     {Vector2(halfQuadSize, halfQuadSize), Vector2(1.f, 1.f)}};
227
228   vertexBuffer.SetData(texturedQuadVertexData, 4);
229
230   Geometry geometry = Geometry::New();
231   geometry.AddVertexBuffer(vertexBuffer);
232
233   Shader   shader   = CreateShader();
234   Renderer renderer = Renderer::New(geometry, shader);
235   Actor    actor    = Actor::New();
236   actor.SetProperty(Actor::Property::SIZE, Vector3::ONE * 100.f);
237   actor.AddRenderer(renderer);
238   application.GetScene().Add(actor);
239
240   application.SendNotification();
241   application.Render();
242
243   {
244     const TestGlAbstraction::BufferSubDataCalls& bufferSubDataCalls =
245       application.GetGlAbstraction().GetBufferSubDataCalls();
246
247     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
248       application.GetGlAbstraction().GetBufferDataCalls();
249
250     // Should be 1 (Flush standalone uniform buffer per each RenderScene)
251     DALI_TEST_EQUALS(bufferSubDataCalls.size(), 1u, TEST_LOCATION);
252     DALI_TEST_EQUALS(bufferDataCalls.size(), 2u, TEST_LOCATION);
253
254     DALI_TEST_EQUALS(bufferDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION);
255   }
256
257   // Re-upload the data on the vertexBuffer
258   vertexBuffer.SetData(texturedQuadVertexData, 4);
259
260   application.SendNotification();
261   application.Render(0);
262
263   {
264     const TestGlAbstraction::BufferSubDataCalls& bufferSubDataCalls =
265       application.GetGlAbstraction().GetBufferSubDataCalls();
266
267     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
268       application.GetGlAbstraction().GetBufferDataCalls();
269
270     // Should be 3 (2 Render + 1 vertexBuffer reload)
271     DALI_TEST_EQUALS(bufferSubDataCalls.size(), 3u, TEST_LOCATION);
272     DALI_TEST_EQUALS(bufferDataCalls.size(), 3u, TEST_LOCATION);
273
274     if(bufferSubDataCalls.size() >= 2)
275     {
276       DALI_TEST_EQUALS(bufferSubDataCalls[1], sizeof(texturedQuadVertexData), TEST_LOCATION);
277     }
278   }
279
280   END_TEST;
281 }
282
283 int UtcDaliVertexBufferMapInitializerList(void)
284 {
285   TestApplication application;
286
287   Property::Map texturedQuadVertexFormat = Property::Map{{"aPosition", Property::VECTOR2},
288                                                          {"aTexCoord", Property::VECTOR2},
289                                                          {"aColor", Property::VECTOR4}};
290
291   try
292   {
293     VertexBuffer vertexBuffer = VertexBuffer::New(texturedQuadVertexFormat);
294     tet_result(TET_PASS);
295   }
296   catch(Dali::DaliException& e)
297   {
298     // Shouldn't assert any more
299     tet_result(TET_FAIL);
300   }
301   END_TEST;
302 }
303
304 int UtcDaliVertexBufferInvalidTypeN01(void)
305 {
306   TestApplication application;
307
308   Property::Map texturedQuadVertexFormat;
309   texturedQuadVertexFormat["aPosition"]    = Property::MAP;
310   texturedQuadVertexFormat["aVertexCoord"] = Property::STRING;
311
312   try
313   {
314     VertexBuffer vertexBuffer = VertexBuffer::New(texturedQuadVertexFormat);
315     tet_result(TET_FAIL);
316   }
317   catch(Dali::DaliException& e)
318   {
319     DALI_TEST_ASSERT(e, "Property::Type not supported in VertexBuffer", TEST_LOCATION);
320   }
321   END_TEST;
322 }
323
324 int UtcDaliVertexBufferInvalidTypeN02(void)
325 {
326   TestApplication application;
327
328   Property::Map texturedQuadVertexFormat = Property::Map{{"aPosition", Property::MAP},
329                                                          {"aTexCoord", Property::STRING},
330                                                          {"aColor", Property::VECTOR4}};
331
332   try
333   {
334     VertexBuffer vertexBuffer = VertexBuffer::New(texturedQuadVertexFormat);
335     tet_result(TET_FAIL);
336   }
337   catch(Dali::DaliException& e)
338   {
339     DALI_TEST_ASSERT(e, "Property::Type not supported in VertexBuffer", TEST_LOCATION);
340   }
341   END_TEST;
342 }
343
344 int UtcDaliVertexBufferSetDataNegative(void)
345 {
346   TestApplication    application;
347   Dali::VertexBuffer instance;
348   try
349   {
350     void*         arg1(nullptr);
351     unsigned long arg2(0u);
352     instance.SetData(arg1, arg2);
353     DALI_TEST_CHECK(false); // Should not get here
354   }
355   catch(...)
356   {
357     DALI_TEST_CHECK(true); // We expect an assert
358   }
359   END_TEST;
360 }
361
362 int UtcDaliVertexBufferGetSizeNegative(void)
363 {
364   TestApplication    application;
365   Dali::VertexBuffer instance;
366   try
367   {
368     instance.GetSize();
369     DALI_TEST_CHECK(false); // Should not get here
370   }
371   catch(...)
372   {
373     DALI_TEST_CHECK(true); // We expect an assert
374   }
375   END_TEST;
376 }