2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali-test-suite-utils.h>
19 #include <dali/public-api/dali-core.h>
23 #include <mesh-builder.h>
25 void geometry_test_startup(void)
27 test_return_value = TET_UNDEF;
30 void geometry_test_cleanup(void)
32 test_return_value = TET_PASS;
37 struct TexturedQuadVertex
40 Vector2 textureCoordinates;
43 VertexBuffer CreateVertexBuffer(const std::string& aPosition, const std::string& aTexCoord)
45 const float halfQuadSize = .5f;
46 TexturedQuadVertex texturedQuadVertexData[4] = {
47 {Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f)},
48 {Vector2(halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f)},
49 {Vector2(-halfQuadSize, halfQuadSize), Vector2(0.f, 1.f)},
50 {Vector2(halfQuadSize, halfQuadSize), Vector2(1.f, 1.f)}};
52 Property::Map vertexFormat;
53 vertexFormat[aPosition] = Property::VECTOR2;
54 vertexFormat[aTexCoord] = Property::VECTOR2;
56 VertexBuffer vertexData = VertexBuffer::New(vertexFormat);
57 vertexData.SetData(texturedQuadVertexData, 4);
64 int UtcDaliGeometryNew01(void)
66 TestApplication application;
68 Geometry geometry = Geometry::New();
70 DALI_TEST_EQUALS((bool)geometry, true, TEST_LOCATION);
74 int UtcDaliGeometryNew02(void)
76 TestApplication application;
78 DALI_TEST_EQUALS((bool)geometry, false, TEST_LOCATION);
82 int UtcDaliGeometryCopyConstructor(void)
84 TestApplication application;
86 Geometry geometry = Geometry::New();
88 Geometry geometryCopy(geometry);
90 DALI_TEST_EQUALS((bool)geometryCopy, true, TEST_LOCATION);
94 int UtcDaliGeometryAssignmentOperator(void)
96 TestApplication application;
98 Geometry geometry = Geometry::New();
101 DALI_TEST_EQUALS((bool)geometry2, false, TEST_LOCATION);
103 geometry2 = geometry;
104 DALI_TEST_EQUALS((bool)geometry2, true, TEST_LOCATION);
109 int UtcDaliGeometryMoveConstructor(void)
111 TestApplication application;
113 Geometry geometry = Geometry::New();
114 DALI_TEST_CHECK(geometry);
115 DALI_TEST_EQUALS(1, geometry.GetBaseObject().ReferenceCount(), TEST_LOCATION);
116 DALI_TEST_EQUALS(0u, geometry.GetNumberOfVertexBuffers(), TEST_LOCATION);
118 VertexBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord");
119 geometry.AddVertexBuffer(vertexBuffer);
120 DALI_TEST_EQUALS(1u, geometry.GetNumberOfVertexBuffers(), TEST_LOCATION);
122 Geometry move = std::move(geometry);
123 DALI_TEST_CHECK(move);
124 DALI_TEST_EQUALS(1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION);
125 DALI_TEST_EQUALS(1u, move.GetNumberOfVertexBuffers(), TEST_LOCATION);
126 DALI_TEST_CHECK(!geometry);
131 int UtcDaliGeometryMoveAssignment(void)
133 TestApplication application;
135 Geometry geometry = Geometry::New();
136 DALI_TEST_CHECK(geometry);
137 DALI_TEST_EQUALS(1, geometry.GetBaseObject().ReferenceCount(), TEST_LOCATION);
138 DALI_TEST_EQUALS(0u, geometry.GetNumberOfVertexBuffers(), TEST_LOCATION);
140 VertexBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord");
141 geometry.AddVertexBuffer(vertexBuffer);
142 DALI_TEST_EQUALS(1u, geometry.GetNumberOfVertexBuffers(), TEST_LOCATION);
145 move = std::move(geometry);
146 DALI_TEST_CHECK(move);
147 DALI_TEST_EQUALS(1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION);
148 DALI_TEST_EQUALS(1u, move.GetNumberOfVertexBuffers(), TEST_LOCATION);
149 DALI_TEST_CHECK(!geometry);
154 int UtcDaliGeometryDownCast01(void)
156 TestApplication application;
158 Geometry geometry = Geometry::New();
160 BaseHandle handle(geometry);
161 Geometry geometry2 = Geometry::DownCast(handle);
162 DALI_TEST_EQUALS((bool)geometry2, true, TEST_LOCATION);
166 int UtcDaliGeometryDownCast02(void)
168 TestApplication application;
170 Handle handle = Handle::New(); // Create a custom object
171 Geometry geometry = Geometry::DownCast(handle);
172 DALI_TEST_EQUALS((bool)geometry, false, TEST_LOCATION);
176 int UtcDaliGeometryAddVertexBuffer(void)
178 TestApplication application;
180 tet_infoline("Test AddVertexBuffer");
182 VertexBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1");
183 Geometry geometry = Geometry::New();
184 geometry.AddVertexBuffer(vertexBuffer1);
186 Shader shader = CreateShader();
187 Renderer renderer = Renderer::New(geometry, shader);
188 Actor actor = Actor::New();
189 actor.SetProperty(Actor::Property::SIZE, Vector3::ONE * 100.f);
190 actor.AddRenderer(renderer);
191 application.GetScene().Add(actor);
193 application.SendNotification();
194 application.Render(0);
195 application.Render();
196 application.SendNotification();
199 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
200 application.GetGlAbstraction().GetBufferDataCalls();
202 DALI_TEST_EQUALS(bufferDataCalls.size(), 1u, TEST_LOCATION);
204 DALI_TEST_EQUALS(bufferDataCalls[0], 4 * sizeof(TexturedQuadVertex), TEST_LOCATION);
207 // add the second vertex buffer
208 application.GetGlAbstraction().ResetBufferDataCalls();
210 VertexBuffer vertexBuffer2 = CreateVertexBuffer("aPosition2", "aTexCoord2");
211 geometry.AddVertexBuffer(vertexBuffer2);
212 application.SendNotification();
213 application.Render(0);
214 application.Render();
215 application.SendNotification();
218 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
219 application.GetGlAbstraction().GetBufferDataCalls();
221 //Check that only the new buffer gets uploaded
222 DALI_TEST_EQUALS(bufferDataCalls.size(), 1u, TEST_LOCATION);
223 DALI_TEST_EQUALS(bufferDataCalls[0], 4 * sizeof(TexturedQuadVertex), TEST_LOCATION);
229 int UtcDaliGeometryGetNumberOfVertexBuffers(void)
231 TestApplication application;
233 tet_infoline("Test GetNumberOfVertexBuffers");
234 VertexBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1");
235 VertexBuffer vertexBuffer2 = CreateVertexBuffer("aPosition2", "aTexCoord2");
236 VertexBuffer vertexBuffer3 = CreateVertexBuffer("aPosition3", "aTexCoord3");
238 Geometry geometry = Geometry::New();
239 geometry.AddVertexBuffer(vertexBuffer1);
240 DALI_TEST_EQUALS(geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION);
242 geometry.AddVertexBuffer(vertexBuffer2);
243 geometry.AddVertexBuffer(vertexBuffer3);
244 DALI_TEST_EQUALS(geometry.GetNumberOfVertexBuffers(), 3u, TEST_LOCATION);
246 geometry.RemoveVertexBuffer(2u);
247 DALI_TEST_EQUALS(geometry.GetNumberOfVertexBuffers(), 2u, TEST_LOCATION);
252 int UtcDaliGeometryRemoveVertexBuffer(void)
254 TestApplication application;
256 tet_infoline("Test RemoveVertexBuffer");
258 VertexBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1");
259 VertexBuffer vertexBuffer2 = CreateVertexBuffer("aPosition2", "aTexCoord2");
261 Geometry geometry = Geometry::New();
262 geometry.AddVertexBuffer(vertexBuffer1);
264 Shader shader = CreateShader();
265 Renderer renderer = Renderer::New(geometry, shader);
266 Actor actor = Actor::New();
267 actor.SetProperty(Actor::Property::SIZE, Vector3::ONE * 100.f);
268 actor.AddRenderer(renderer);
269 application.GetScene().Add(actor);
271 DALI_TEST_EQUALS(geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION);
273 geometry.RemoveVertexBuffer(0);
274 geometry.AddVertexBuffer(vertexBuffer2);
275 DALI_TEST_EQUALS(geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION);
277 geometry.RemoveVertexBuffer(0);
278 DALI_TEST_EQUALS(geometry.GetNumberOfVertexBuffers(), 0u, TEST_LOCATION);
280 //Todo: test by checking the BufferDataCalls
281 // make sure the vertex buffer in actually removed from gl
286 int UtcDaliGeometrySetIndexBuffer(void)
288 TestApplication application;
290 tet_infoline("Test SetIndexBuffer");
292 VertexBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord");
294 Geometry geometry = Geometry::New();
295 geometry.AddVertexBuffer(vertexBuffer);
297 Shader shader = CreateShader();
298 Renderer renderer = Renderer::New(geometry, shader);
299 Actor actor = Actor::New();
300 actor.SetProperty(Actor::Property::SIZE, Vector3::ONE * 100.f);
301 actor.AddRenderer(renderer);
302 application.GetScene().Add(actor);
304 application.SendNotification();
305 application.Render(0);
306 application.Render();
307 application.SendNotification();
310 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
311 application.GetGlAbstraction().GetBufferDataCalls();
313 DALI_TEST_EQUALS(bufferDataCalls.size(), 1u, TEST_LOCATION);
315 DALI_TEST_EQUALS(bufferDataCalls[0], 4 * sizeof(TexturedQuadVertex), TEST_LOCATION);
319 application.GetGlAbstraction().ResetBufferDataCalls();
321 const unsigned short indexData[6] = {0, 3, 1, 0, 2, 3};
322 geometry.SetIndexBuffer(indexData, sizeof(indexData) / sizeof(indexData[0]));
323 application.SendNotification();
324 application.Render(0);
325 application.Render();
326 application.SendNotification();
329 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
330 application.GetGlAbstraction().GetBufferDataCalls();
332 //Only the index buffer should be uploaded
333 DALI_TEST_EQUALS(bufferDataCalls.size(), 1u, TEST_LOCATION);
335 // should be unsigned short instead of unsigned int
336 DALI_TEST_EQUALS(bufferDataCalls[0], 6 * sizeof(unsigned short), TEST_LOCATION);
342 int UtcDaliGeometrySetGetGeometryType01(void)
344 TestApplication application;
346 tet_infoline("Test SetType and GetType: without index buffer");
348 unsigned int numVertex = 4u;
349 VertexBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord");
351 Geometry geometry = Geometry::New();
352 geometry.AddVertexBuffer(vertexBuffer);
354 Shader shader = CreateShader();
355 Renderer renderer = Renderer::New(geometry, shader);
356 Actor actor = Actor::New();
357 actor.SetProperty(Actor::Property::SIZE, Vector3::ONE * 100.f);
358 actor.AddRenderer(renderer);
359 application.GetScene().Add(actor);
361 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
362 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
364 /****************************************************/
365 // Default (TRIANGLES), no index buffer
367 drawTrace.Enable(true);
368 application.SendNotification();
369 application.Render(0);
370 application.Render();
371 application.SendNotification();
372 drawTrace.Enable(false);
374 // Test the default geometry type is GL_TRIANGLE
375 // no index buffer, call glDrawArrays,
376 DALI_TEST_EQUALS(drawTrace.CountMethod("DrawArrays"), 2, TEST_LOCATION);
377 std::stringstream out;
378 out << GL_TRIANGLES << ", " << 0 << ", " << numVertex;
379 DALI_TEST_EQUALS(drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
381 DALI_TEST_EQUALS(geometry.GetType(), Geometry::TRIANGLES, TEST_LOCATION);
383 /*********************************************************/
384 // LINES, no index buffer
385 geometry.SetType(Geometry::LINES);
388 drawTrace.Enable(true);
389 application.SendNotification();
390 application.Render(0);
391 application.Render();
392 application.SendNotification();
393 drawTrace.Enable(false);
395 // geometry type is set as GL_LINES
396 // no index buffer, call glDrawArrays,
397 DALI_TEST_EQUALS(drawTrace.CountMethod("DrawArrays"), 2, TEST_LOCATION);
399 out << GL_LINES << ", " << 0 << ", " << numVertex;
400 DALI_TEST_EQUALS(drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
402 DALI_TEST_EQUALS(geometry.GetType(), Geometry::LINES, TEST_LOCATION);
404 /*****************************************************/
406 geometry.SetType(Geometry::POINTS);
409 drawTrace.Enable(true);
410 application.SendNotification();
411 application.Render(0);
412 application.Render();
413 application.SendNotification();
414 drawTrace.Enable(false);
416 // geometry type is set as GL_POINTS
417 // no index buffer, call glDrawArrays,
418 DALI_TEST_EQUALS(drawTrace.CountMethod("DrawArrays"), 2, TEST_LOCATION);
420 out << GL_POINTS << ", " << 0 << ", " << numVertex;
421 DALI_TEST_EQUALS(drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
423 DALI_TEST_EQUALS(geometry.GetType(), Geometry::POINTS, TEST_LOCATION);
425 /*****************************************************/
426 //TRIANGLE_STRIP, no index buffer
427 geometry.SetType(Geometry::TRIANGLE_STRIP);
430 drawTrace.Enable(true);
431 application.SendNotification();
432 application.Render(0);
433 application.Render();
434 application.SendNotification();
435 drawTrace.Enable(false);
437 // geometry type is set as GL_TRIANGLE_STRIP
438 // no index buffer, call glDrawArrays,
439 DALI_TEST_EQUALS(drawTrace.CountMethod("DrawArrays"), 2, TEST_LOCATION);
441 out << GL_TRIANGLE_STRIP << ", " << 0 << ", " << numVertex;
442 DALI_TEST_EQUALS(drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
444 DALI_TEST_EQUALS(geometry.GetType(), Geometry::TRIANGLE_STRIP, TEST_LOCATION);
446 /*****************************************************/
447 //TRIANGLE_FAN, no index buffer
448 geometry.SetType(Geometry::TRIANGLE_FAN);
451 drawTrace.Enable(true);
452 application.SendNotification();
453 application.Render(0);
454 application.Render();
455 application.SendNotification();
456 drawTrace.Enable(false);
458 // geometry type is set as GL_TRIANGLE_FAN
459 // no index buffer, call glDrawArrays,
460 DALI_TEST_EQUALS(drawTrace.CountMethod("DrawArrays"), 2, TEST_LOCATION);
462 out << GL_TRIANGLE_FAN << ", " << 0 << ", " << numVertex;
463 DALI_TEST_EQUALS(drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
465 DALI_TEST_EQUALS(geometry.GetType(), Geometry::TRIANGLE_FAN, TEST_LOCATION);
470 int UtcDaliGeometrySetGetGeometryType02(void)
472 TestApplication application;
474 tet_infoline("Test SetType and GetType: with index buffer");
476 unsigned int numVertex = 4u;
477 unsigned int numIndex = 6u; // 6 unsigned short
478 VertexBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord");
480 Geometry geometry = Geometry::New();
481 geometry.AddVertexBuffer(vertexBuffer);
482 const unsigned short indexData[6] = {0, 3, 1, 0, 2, 3};
483 geometry.SetIndexBuffer(indexData, sizeof(indexData) / sizeof(indexData[0]));
485 Shader shader = CreateShader();
486 Renderer renderer = Renderer::New(geometry, shader);
487 Actor actor = Actor::New();
488 actor.SetProperty(Actor::Property::SIZE, Vector3::ONE * 100.f);
489 actor.AddRenderer(renderer);
490 application.GetScene().Add(actor);
492 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
493 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
495 /****************************************************/
496 // Default (TRIANGLES), with index buffer
498 drawTrace.Enable(true);
499 application.SendNotification();
500 application.Render(0);
501 application.Render();
502 application.SendNotification();
503 drawTrace.Enable(false);
505 // Test the default geometry type is GL_TRIANGLE
506 DALI_TEST_EQUALS(drawTrace.CountMethod("DrawElements"), 2, TEST_LOCATION);
507 std::stringstream out;
508 out << GL_TRIANGLES << ", " << numIndex << ", " << GL_UNSIGNED_SHORT << ", "
510 DALI_TEST_EQUALS(drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
512 DALI_TEST_EQUALS(geometry.GetType(), Geometry::TRIANGLES, TEST_LOCATION);
514 /*********************************************************/
515 // LINES, with index buffer
516 geometry.SetType(Geometry::LINES);
519 drawTrace.Enable(true);
520 application.SendNotification();
521 application.Render(0);
522 application.Render();
523 application.SendNotification();
524 drawTrace.Enable(false);
526 // geometry type is set as GL_LINES
527 DALI_TEST_EQUALS(drawTrace.CountMethod("DrawElements"), 2, TEST_LOCATION);
529 out << GL_LINES << ", " << numIndex << ", " << GL_UNSIGNED_SHORT << ", "
531 DALI_TEST_EQUALS(drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
533 DALI_TEST_EQUALS(geometry.GetType(), Geometry::LINES, TEST_LOCATION);
535 /*****************************************************/
537 geometry.SetType(Geometry::POINTS);
540 drawTrace.Enable(true);
541 application.SendNotification();
542 application.Render(0);
543 application.Render();
544 application.SendNotification();
545 drawTrace.Enable(false);
547 // geometry type is set as GL_POINTS
548 // As Points does not use the index buffer, call glDrawArrays,
549 DALI_TEST_EQUALS(drawTrace.CountMethod("DrawArrays"), 2, TEST_LOCATION);
551 out << GL_POINTS << ", " << 0 << ", " << numVertex;
552 DALI_TEST_EQUALS(drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
554 DALI_TEST_EQUALS(geometry.GetType(), Geometry::POINTS, TEST_LOCATION);
556 /*****************************************************/
558 geometry.SetType(Geometry::TRIANGLE_STRIP);
561 drawTrace.Enable(true);
562 application.SendNotification();
563 application.Render(0);
564 application.Render();
565 application.SendNotification();
566 drawTrace.Enable(false);
568 // geometry type is set as GL_TRIANGLE_STRIP
569 DALI_TEST_EQUALS(drawTrace.CountMethod("DrawElements"), 2, TEST_LOCATION);
571 out << GL_TRIANGLE_STRIP << ", " << numIndex << ", " << GL_UNSIGNED_SHORT << ", "
573 DALI_TEST_EQUALS(drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
575 DALI_TEST_EQUALS(geometry.GetType(), Geometry::TRIANGLE_STRIP, TEST_LOCATION);
577 /*****************************************************/
579 geometry.SetType(Geometry::TRIANGLE_FAN);
582 drawTrace.Enable(true);
583 application.SendNotification();
584 application.Render(0);
585 application.Render();
586 application.SendNotification();
587 drawTrace.Enable(false);
589 // geometry type is set as GL_TRIANGLE_FAN
590 DALI_TEST_EQUALS(drawTrace.CountMethod("DrawElements"), 2, TEST_LOCATION);
592 out << GL_TRIANGLE_FAN << ", " << numIndex << ", " << GL_UNSIGNED_SHORT << ", "
594 DALI_TEST_EQUALS(drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
596 DALI_TEST_EQUALS(geometry.GetType(), Geometry::TRIANGLE_FAN, TEST_LOCATION);
601 int UtcDaliGeometrySetIndexBufferNegative(void)
603 TestApplication application;
604 Dali::Geometry instance;
607 unsigned short* arg1(nullptr);
608 unsigned long arg2(0u);
609 instance.SetIndexBuffer(arg1, arg2);
610 DALI_TEST_CHECK(false); // Should not get here
614 DALI_TEST_CHECK(true); // We expect an assert
619 int UtcDaliGeometryAddVertexBufferNegative(void)
621 TestApplication application;
622 Dali::Geometry instance;
625 Dali::VertexBuffer arg1;
626 instance.AddVertexBuffer(arg1);
627 DALI_TEST_CHECK(false); // Should not get here
631 DALI_TEST_CHECK(true); // We expect an assert
636 int UtcDaliGeometryRemoveVertexBufferNegative(void)
638 TestApplication application;
639 Dali::Geometry instance;
642 unsigned long arg1(0u);
643 instance.RemoveVertexBuffer(arg1);
644 DALI_TEST_CHECK(false); // Should not get here
648 DALI_TEST_CHECK(true); // We expect an assert
653 int UtcDaliGeometrySetTypeNegative(void)
655 TestApplication application;
656 Dali::Geometry instance;
659 Dali::Geometry::Type arg1(Geometry::POINTS);
660 instance.SetType(arg1);
661 DALI_TEST_CHECK(false); // Should not get here
665 DALI_TEST_CHECK(true); // We expect an assert
670 int UtcDaliGeometryGetNumberOfVertexBuffersNegative(void)
672 TestApplication application;
673 Dali::Geometry instance;
676 instance.GetNumberOfVertexBuffers();
677 DALI_TEST_CHECK(false); // Should not get here
681 DALI_TEST_CHECK(true); // We expect an assert
686 int UtcDaliGeometryGetTypeNegative(void)
688 TestApplication application;
689 Dali::Geometry instance;
693 DALI_TEST_CHECK(false); // Should not get here
697 DALI_TEST_CHECK(true); // We expect an assert