2 * Copyright (c) 2016 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 UtcDaliGeometryDownCast01(void)
110 TestApplication application;
112 Geometry geometry = Geometry::New();
114 BaseHandle handle(geometry);
115 Geometry geometry2 = Geometry::DownCast(handle);
116 DALI_TEST_EQUALS( (bool)geometry2, true, TEST_LOCATION );
120 int UtcDaliGeometryDownCast02(void)
122 TestApplication application;
124 Handle handle = Handle::New(); // Create a custom object
125 Geometry geometry = Geometry::DownCast(handle);
126 DALI_TEST_EQUALS( (bool)geometry, false, TEST_LOCATION );
130 int UtcDaliGeometryAddVertexBuffer(void)
132 TestApplication application;
134 tet_infoline("Test AddVertexBuffer");
136 PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
137 Geometry geometry = Geometry::New();
138 geometry.AddVertexBuffer( vertexBuffer1 );
140 Shader shader = CreateShader();
141 Renderer renderer = Renderer::New(geometry, shader);
142 Actor actor = Actor::New();
143 actor.SetSize(Vector3::ONE * 100.f);
144 actor.AddRenderer(renderer);
145 Stage::GetCurrent().Add(actor);
147 application.SendNotification();
148 application.Render(0);
149 application.Render();
150 application.SendNotification();
153 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
154 application.GetGlAbstraction().GetBufferDataCalls();
156 DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
158 DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
161 // add the second vertex buffer
162 application.GetGlAbstraction().ResetBufferDataCalls();
164 PropertyBuffer vertexBuffer2 = CreateVertexBuffer( "aPosition2", "aTexCoord2" );
165 geometry.AddVertexBuffer( vertexBuffer2 );
166 application.SendNotification();
167 application.Render(0);
168 application.Render();
169 application.SendNotification();
172 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
173 application.GetGlAbstraction().GetBufferDataCalls();
175 //Check that only the new buffer gets uploaded
176 DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
177 DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
183 int UtcDaliGeometryGetNumberOfVertexBuffers(void)
185 TestApplication application;
187 tet_infoline("Test GetNumberOfVertexBuffers");
188 PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
189 PropertyBuffer vertexBuffer2 = CreateVertexBuffer("aPosition2", "aTexCoord2" );
190 PropertyBuffer vertexBuffer3 = CreateVertexBuffer("aPosition3", "aTexCoord3" );
192 Geometry geometry = Geometry::New();
193 geometry.AddVertexBuffer( vertexBuffer1 );
194 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
196 geometry.AddVertexBuffer( vertexBuffer2 );
197 geometry.AddVertexBuffer( vertexBuffer3 );
198 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 3u, TEST_LOCATION );
200 geometry.RemoveVertexBuffer( 2u );
201 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 2u, TEST_LOCATION );
206 int UtcDaliGeometryRemoveVertexBuffer(void)
208 TestApplication application;
210 tet_infoline("Test RemoveVertexBuffer");
212 PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
213 PropertyBuffer vertexBuffer2 = CreateVertexBuffer("aPosition2", "aTexCoord2" );
215 Geometry geometry = Geometry::New();
216 geometry.AddVertexBuffer( vertexBuffer1 );
218 Shader shader = CreateShader();
219 Renderer renderer = Renderer::New(geometry, shader);
220 Actor actor = Actor::New();
221 actor.SetSize(Vector3::ONE * 100.f);
222 actor.AddRenderer(renderer);
223 Stage::GetCurrent().Add(actor);
225 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
227 geometry.RemoveVertexBuffer( 0 );
228 geometry.AddVertexBuffer( vertexBuffer2 );
229 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
231 geometry.RemoveVertexBuffer( 0 );
232 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 0u, TEST_LOCATION );
234 //Todo: test by checking the BufferDataCalls
235 // make sure the vertex buffer in actually removed from gl
240 int UtcDaliGeometrySetIndexBuffer(void)
242 TestApplication application;
244 tet_infoline("Test SetIndexBuffer");
246 PropertyBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord" );
248 Geometry geometry = Geometry::New();
249 geometry.AddVertexBuffer( vertexBuffer );
251 Shader shader = CreateShader();
252 Renderer renderer = Renderer::New(geometry, shader);
253 Actor actor = Actor::New();
254 actor.SetSize(Vector3::ONE * 100.f);
255 actor.AddRenderer(renderer);
256 Stage::GetCurrent().Add(actor);
258 application.SendNotification();
259 application.Render(0);
260 application.Render();
261 application.SendNotification();
264 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
265 application.GetGlAbstraction().GetBufferDataCalls();
267 DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
269 DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
273 application.GetGlAbstraction().ResetBufferDataCalls();
275 const unsigned short indexData[6] = { 0, 3, 1, 0, 2, 3 };
276 geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
277 application.SendNotification();
278 application.Render(0);
279 application.Render();
280 application.SendNotification();
283 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
284 application.GetGlAbstraction().GetBufferDataCalls();
286 //Only the index buffer should be uploaded
287 DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
289 // should be unsigned short instead of unsigned int
290 DALI_TEST_EQUALS( bufferDataCalls[0], 6*sizeof( unsigned short ), TEST_LOCATION );
297 int UtcDaliGeometrySetGetGeometryType01(void)
299 TestApplication application;
301 tet_infoline("Test SetType and GetType: without index buffer");
303 unsigned int numVertex = 4u;
304 PropertyBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord" );
306 Geometry geometry = Geometry::New();
307 geometry.AddVertexBuffer( vertexBuffer );
309 Shader shader = CreateShader();
310 Renderer renderer = Renderer::New(geometry, shader);
311 Actor actor = Actor::New();
312 actor.SetSize(Vector3::ONE * 100.f);
313 actor.AddRenderer(renderer);
314 Stage::GetCurrent().Add(actor);
316 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
317 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
319 /****************************************************/
320 // Default (TRIANGLES), no index buffer
322 drawTrace.Enable(true);
323 application.SendNotification();
324 application.Render(0);
325 application.Render();
326 application.SendNotification();
327 drawTrace.Enable( false );
329 // Test the default geometry type is GL_TRIANGLE
330 // no index buffer, call glDrawArrays,
331 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
332 std::stringstream out;
333 out << GL_TRIANGLES << ", " << 0 << ", " << numVertex;
334 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
336 DALI_TEST_EQUALS( geometry.GetType(), Geometry::TRIANGLES, TEST_LOCATION);
338 /*********************************************************/
339 // LINES, no index buffer
340 geometry.SetType( Geometry::LINES );
343 drawTrace.Enable(true);
344 application.SendNotification();
345 application.Render(0);
346 application.Render();
347 application.SendNotification();
348 drawTrace.Enable( false );
350 // geometry type is set as GL_LINES
351 // no index buffer, call glDrawArrays,
352 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
354 out << GL_LINES << ", " << 0 << ", " << numVertex;
355 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
357 DALI_TEST_EQUALS( geometry.GetType(), Geometry::LINES, TEST_LOCATION);
359 /*****************************************************/
361 geometry.SetType( Geometry::POINTS );
364 drawTrace.Enable(true);
365 application.SendNotification();
366 application.Render(0);
367 application.Render();
368 application.SendNotification();
369 drawTrace.Enable( false );
371 // geometry type is set as GL_POINTS
372 // no index buffer, call glDrawArrays,
373 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
375 out << GL_POINTS << ", " << 0 << ", " << numVertex;
376 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
378 DALI_TEST_EQUALS( geometry.GetType(), Geometry::POINTS, TEST_LOCATION);
380 /*****************************************************/
381 //TRIANGLE_STRIP, no index buffer
382 geometry.SetType( Geometry::TRIANGLE_STRIP );
385 drawTrace.Enable(true);
386 application.SendNotification();
387 application.Render(0);
388 application.Render();
389 application.SendNotification();
390 drawTrace.Enable( false );
392 // geometry type is set as GL_TRIANGLE_STRIP
393 // no index buffer, call glDrawArrays,
394 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
396 out << GL_TRIANGLE_STRIP << ", " << 0 << ", " << numVertex;
397 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
399 DALI_TEST_EQUALS( geometry.GetType(), Geometry::TRIANGLE_STRIP, TEST_LOCATION);
401 /*****************************************************/
402 //TRIANGLE_FAN, no index buffer
403 geometry.SetType( Geometry::TRIANGLE_FAN );
406 drawTrace.Enable(true);
407 application.SendNotification();
408 application.Render(0);
409 application.Render();
410 application.SendNotification();
411 drawTrace.Enable( false );
413 // geometry type is set as GL_TRIANGLE_FAN
414 // no index buffer, call glDrawArrays,
415 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
417 out << GL_TRIANGLE_FAN << ", " << 0 << ", " << numVertex;
418 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
420 DALI_TEST_EQUALS( geometry.GetType(), Geometry::TRIANGLE_FAN, TEST_LOCATION);
425 int UtcDaliGeometrySetGetGeometryType02(void)
427 TestApplication application;
429 tet_infoline("Test SetType and GetType: with index buffer");
431 unsigned int numVertex = 4u;
432 unsigned int numIndex = 6u; // 6 unsigned short
433 PropertyBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord" );
436 Geometry geometry = Geometry::New();
437 geometry.AddVertexBuffer( vertexBuffer );
438 const unsigned short indexData[6] = { 0, 3, 1, 0, 2, 3 };
439 geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
441 Shader shader = CreateShader();
442 Renderer renderer = Renderer::New(geometry, shader);
443 Actor actor = Actor::New();
444 actor.SetSize(Vector3::ONE * 100.f);
445 actor.AddRenderer(renderer);
446 Stage::GetCurrent().Add(actor);
448 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
449 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
451 /****************************************************/
452 // Default (TRIANGLES), with index buffer
454 drawTrace.Enable(true);
455 application.SendNotification();
456 application.Render(0);
457 application.Render();
458 application.SendNotification();
459 drawTrace.Enable( false );
461 // Test the default geometry type is GL_TRIANGLE
462 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawElements" ), 2, TEST_LOCATION);
463 std::stringstream out;
464 out << GL_TRIANGLES << ", " << numIndex << ", " << GL_UNSIGNED_SHORT<<", "<<"indices";
465 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
467 DALI_TEST_EQUALS( geometry.GetType(), Geometry::TRIANGLES, TEST_LOCATION);
469 /*********************************************************/
470 // LINES, with index buffer
471 geometry.SetType( Geometry::LINES );
474 drawTrace.Enable(true);
475 application.SendNotification();
476 application.Render(0);
477 application.Render();
478 application.SendNotification();
479 drawTrace.Enable( false );
481 // geometry type is set as GL_LINES
482 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawElements" ), 2, TEST_LOCATION);
484 out << GL_LINES << ", " << numIndex << ", " << GL_UNSIGNED_SHORT<<", "<<"indices";
485 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
487 DALI_TEST_EQUALS( geometry.GetType(), Geometry::LINES, TEST_LOCATION);
489 /*****************************************************/
491 geometry.SetType( Geometry::POINTS );
494 drawTrace.Enable(true);
495 application.SendNotification();
496 application.Render(0);
497 application.Render();
498 application.SendNotification();
499 drawTrace.Enable( false );
501 // geometry type is set as GL_POINTS
502 // As Points does not use the index buffer, call glDrawArrays,
503 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
505 out << GL_POINTS << ", " << 0 << ", " << numVertex;
506 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
508 DALI_TEST_EQUALS( geometry.GetType(), Geometry::POINTS, TEST_LOCATION);
510 /*****************************************************/
512 geometry.SetType( Geometry::TRIANGLE_STRIP );
515 drawTrace.Enable(true);
516 application.SendNotification();
517 application.Render(0);
518 application.Render();
519 application.SendNotification();
520 drawTrace.Enable( false );
522 // geometry type is set as GL_TRIANGLE_STRIP
523 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawElements" ), 2, TEST_LOCATION);
525 out << GL_TRIANGLE_STRIP << ", " << numIndex << ", " << GL_UNSIGNED_SHORT<<", "<<"indices";
526 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
528 DALI_TEST_EQUALS( geometry.GetType(), Geometry::TRIANGLE_STRIP, TEST_LOCATION);
530 /*****************************************************/
532 geometry.SetType( Geometry::TRIANGLE_FAN );
535 drawTrace.Enable(true);
536 application.SendNotification();
537 application.Render(0);
538 application.Render();
539 application.SendNotification();
540 drawTrace.Enable( false );
542 // geometry type is set as GL_TRIANGLE_FAN
543 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawElements" ), 2, TEST_LOCATION);
545 out << GL_TRIANGLE_FAN << ", " << numIndex << ", " << GL_UNSIGNED_SHORT<<", "<<"indices";
546 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
548 DALI_TEST_EQUALS( geometry.GetType(), Geometry::TRIANGLE_FAN, TEST_LOCATION);