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/public-api/dali-core.h>
19 #include <dali-test-suite-utils.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;
38 struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
40 PropertyBuffer CreateVertexBuffer( const std::string& aPosition, const std::string& aTexCoord )
42 const float halfQuadSize = .5f;
43 TexturedQuadVertex texturedQuadVertexData[4] = {
44 { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
45 { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
46 { Vector2(-halfQuadSize, halfQuadSize), Vector2(0.f, 1.f) },
47 { Vector2( halfQuadSize, halfQuadSize), Vector2(1.f, 1.f) } };
49 Property::Map vertexFormat;
50 vertexFormat[aPosition] = Property::VECTOR2;
51 vertexFormat[aTexCoord] = Property::VECTOR2;
53 PropertyBuffer vertexData = PropertyBuffer::New( vertexFormat );
54 vertexData.SetData( texturedQuadVertexData, 4 );
63 int UtcDaliGeometryNew01(void)
65 TestApplication application;
67 Geometry geometry = Geometry::New();
69 DALI_TEST_EQUALS( (bool)geometry, true, TEST_LOCATION );
73 int UtcDaliGeometryNew02(void)
75 TestApplication application;
77 DALI_TEST_EQUALS( (bool)geometry, false, TEST_LOCATION );
81 int UtcDaliGeometryCopyConstructor(void)
83 TestApplication application;
85 Geometry geometry = Geometry::New();
87 Geometry geometryCopy(geometry);
89 DALI_TEST_EQUALS( (bool)geometryCopy, true, TEST_LOCATION );
93 int UtcDaliGeometryAssignmentOperator(void)
95 TestApplication application;
97 Geometry geometry = Geometry::New();
100 DALI_TEST_EQUALS( (bool)geometry2, false, TEST_LOCATION );
102 geometry2 = geometry;
103 DALI_TEST_EQUALS( (bool)geometry2, true, TEST_LOCATION );
108 int UtcDaliGeometryMoveConstructor(void)
110 TestApplication application;
112 Geometry geometry = Geometry::New();
113 DALI_TEST_CHECK( geometry );
114 DALI_TEST_EQUALS( 1, geometry.GetBaseObject().ReferenceCount(), TEST_LOCATION );
115 DALI_TEST_EQUALS( 0u, geometry.GetNumberOfVertexBuffers(), TEST_LOCATION );
117 PropertyBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord" );
118 geometry.AddVertexBuffer( vertexBuffer );
119 DALI_TEST_EQUALS( 1u, geometry.GetNumberOfVertexBuffers(), TEST_LOCATION );
121 Geometry move = std::move( geometry );
122 DALI_TEST_CHECK( move );
123 DALI_TEST_EQUALS( 1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION );
124 DALI_TEST_EQUALS( 1u, move.GetNumberOfVertexBuffers(), TEST_LOCATION );
125 DALI_TEST_CHECK( !geometry );
130 int UtcDaliGeometryMoveAssignment(void)
132 TestApplication application;
134 Geometry geometry = Geometry::New();
135 DALI_TEST_CHECK( geometry );
136 DALI_TEST_EQUALS( 1, geometry.GetBaseObject().ReferenceCount(), TEST_LOCATION );
137 DALI_TEST_EQUALS( 0u, geometry.GetNumberOfVertexBuffers(), TEST_LOCATION );
139 PropertyBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord" );
140 geometry.AddVertexBuffer( vertexBuffer );
141 DALI_TEST_EQUALS( 1u, geometry.GetNumberOfVertexBuffers(), TEST_LOCATION );
144 move = std::move( geometry );
145 DALI_TEST_CHECK( move );
146 DALI_TEST_EQUALS( 1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION );
147 DALI_TEST_EQUALS( 1u, move.GetNumberOfVertexBuffers(), TEST_LOCATION );
148 DALI_TEST_CHECK( !geometry );
153 int UtcDaliGeometryDownCast01(void)
155 TestApplication application;
157 Geometry geometry = Geometry::New();
159 BaseHandle handle(geometry);
160 Geometry geometry2 = Geometry::DownCast(handle);
161 DALI_TEST_EQUALS( (bool)geometry2, true, TEST_LOCATION );
165 int UtcDaliGeometryDownCast02(void)
167 TestApplication application;
169 Handle handle = Handle::New(); // Create a custom object
170 Geometry geometry = Geometry::DownCast(handle);
171 DALI_TEST_EQUALS( (bool)geometry, false, TEST_LOCATION );
175 int UtcDaliGeometryAddVertexBuffer(void)
177 TestApplication application;
179 tet_infoline("Test AddVertexBuffer");
181 PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
182 Geometry geometry = Geometry::New();
183 geometry.AddVertexBuffer( vertexBuffer1 );
185 Shader shader = CreateShader();
186 Renderer renderer = Renderer::New(geometry, shader);
187 Actor actor = Actor::New();
188 actor.SetProperty( Actor::Property::SIZE,Vector3::ONE * 100.f);
189 actor.AddRenderer(renderer);
190 application.GetScene().Add(actor);
192 application.SendNotification();
193 application.Render(0);
194 application.Render();
195 application.SendNotification();
198 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
199 application.GetGlAbstraction().GetBufferDataCalls();
201 DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
203 DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
206 // add the second vertex buffer
207 application.GetGlAbstraction().ResetBufferDataCalls();
209 PropertyBuffer vertexBuffer2 = CreateVertexBuffer( "aPosition2", "aTexCoord2" );
210 geometry.AddVertexBuffer( vertexBuffer2 );
211 application.SendNotification();
212 application.Render(0);
213 application.Render();
214 application.SendNotification();
217 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
218 application.GetGlAbstraction().GetBufferDataCalls();
220 //Check that only the new buffer gets uploaded
221 DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
222 DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
228 int UtcDaliGeometryGetNumberOfVertexBuffers(void)
230 TestApplication application;
232 tet_infoline("Test GetNumberOfVertexBuffers");
233 PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
234 PropertyBuffer vertexBuffer2 = CreateVertexBuffer("aPosition2", "aTexCoord2" );
235 PropertyBuffer vertexBuffer3 = CreateVertexBuffer("aPosition3", "aTexCoord3" );
237 Geometry geometry = Geometry::New();
238 geometry.AddVertexBuffer( vertexBuffer1 );
239 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
241 geometry.AddVertexBuffer( vertexBuffer2 );
242 geometry.AddVertexBuffer( vertexBuffer3 );
243 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 3u, TEST_LOCATION );
245 geometry.RemoveVertexBuffer( 2u );
246 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 2u, TEST_LOCATION );
251 int UtcDaliGeometryRemoveVertexBuffer(void)
253 TestApplication application;
255 tet_infoline("Test RemoveVertexBuffer");
257 PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
258 PropertyBuffer vertexBuffer2 = CreateVertexBuffer("aPosition2", "aTexCoord2" );
260 Geometry geometry = Geometry::New();
261 geometry.AddVertexBuffer( vertexBuffer1 );
263 Shader shader = CreateShader();
264 Renderer renderer = Renderer::New(geometry, shader);
265 Actor actor = Actor::New();
266 actor.SetProperty( Actor::Property::SIZE,Vector3::ONE * 100.f);
267 actor.AddRenderer(renderer);
268 application.GetScene().Add(actor);
270 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
272 geometry.RemoveVertexBuffer( 0 );
273 geometry.AddVertexBuffer( vertexBuffer2 );
274 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
276 geometry.RemoveVertexBuffer( 0 );
277 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 0u, TEST_LOCATION );
279 //Todo: test by checking the BufferDataCalls
280 // make sure the vertex buffer in actually removed from gl
285 int UtcDaliGeometrySetIndexBuffer(void)
287 TestApplication application;
289 tet_infoline("Test SetIndexBuffer");
291 PropertyBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord" );
293 Geometry geometry = Geometry::New();
294 geometry.AddVertexBuffer( vertexBuffer );
296 Shader shader = CreateShader();
297 Renderer renderer = Renderer::New(geometry, shader);
298 Actor actor = Actor::New();
299 actor.SetProperty( Actor::Property::SIZE,Vector3::ONE * 100.f);
300 actor.AddRenderer(renderer);
301 application.GetScene().Add(actor);
303 application.SendNotification();
304 application.Render(0);
305 application.Render();
306 application.SendNotification();
309 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
310 application.GetGlAbstraction().GetBufferDataCalls();
312 DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
314 DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
318 application.GetGlAbstraction().ResetBufferDataCalls();
320 const unsigned short indexData[6] = { 0, 3, 1, 0, 2, 3 };
321 geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
322 application.SendNotification();
323 application.Render(0);
324 application.Render();
325 application.SendNotification();
328 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
329 application.GetGlAbstraction().GetBufferDataCalls();
331 //Only the index buffer should be uploaded
332 DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
334 // should be unsigned short instead of unsigned int
335 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 PropertyBuffer 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 PropertyBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord" );
481 Geometry geometry = Geometry::New();
482 geometry.AddVertexBuffer( vertexBuffer );
483 const unsigned short indexData[6] = { 0, 3, 1, 0, 2, 3 };
484 geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
486 Shader shader = CreateShader();
487 Renderer renderer = Renderer::New(geometry, shader);
488 Actor actor = Actor::New();
489 actor.SetProperty( Actor::Property::SIZE,Vector3::ONE * 100.f);
490 actor.AddRenderer(renderer);
491 application.GetScene().Add(actor);
493 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
494 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
496 /****************************************************/
497 // Default (TRIANGLES), with index buffer
499 drawTrace.Enable(true);
500 application.SendNotification();
501 application.Render(0);
502 application.Render();
503 application.SendNotification();
504 drawTrace.Enable( false );
506 // Test the default geometry type is GL_TRIANGLE
507 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawElements" ), 2, TEST_LOCATION);
508 std::stringstream out;
509 out << GL_TRIANGLES << ", " << numIndex << ", " << GL_UNSIGNED_SHORT<<", "<<"indices";
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<<", "<<"indices";
530 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
532 DALI_TEST_EQUALS( geometry.GetType(), Geometry::LINES, TEST_LOCATION);
534 /*****************************************************/
536 geometry.SetType( Geometry::POINTS );
539 drawTrace.Enable(true);
540 application.SendNotification();
541 application.Render(0);
542 application.Render();
543 application.SendNotification();
544 drawTrace.Enable( false );
546 // geometry type is set as GL_POINTS
547 // As Points does not use the index buffer, call glDrawArrays,
548 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
550 out << GL_POINTS << ", " << 0 << ", " << numVertex;
551 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
553 DALI_TEST_EQUALS( geometry.GetType(), Geometry::POINTS, TEST_LOCATION);
555 /*****************************************************/
557 geometry.SetType( Geometry::TRIANGLE_STRIP );
560 drawTrace.Enable(true);
561 application.SendNotification();
562 application.Render(0);
563 application.Render();
564 application.SendNotification();
565 drawTrace.Enable( false );
567 // geometry type is set as GL_TRIANGLE_STRIP
568 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawElements" ), 2, TEST_LOCATION);
570 out << GL_TRIANGLE_STRIP << ", " << numIndex << ", " << GL_UNSIGNED_SHORT<<", "<<"indices";
571 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
573 DALI_TEST_EQUALS( geometry.GetType(), Geometry::TRIANGLE_STRIP, TEST_LOCATION);
575 /*****************************************************/
577 geometry.SetType( Geometry::TRIANGLE_FAN );
580 drawTrace.Enable(true);
581 application.SendNotification();
582 application.Render(0);
583 application.Render();
584 application.SendNotification();
585 drawTrace.Enable( false );
587 // geometry type is set as GL_TRIANGLE_FAN
588 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawElements" ), 2, TEST_LOCATION);
590 out << GL_TRIANGLE_FAN << ", " << numIndex << ", " << GL_UNSIGNED_SHORT<<", "<<"indices";
591 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
593 DALI_TEST_EQUALS( geometry.GetType(), Geometry::TRIANGLE_FAN, TEST_LOCATION);