2 * Copyright (c) 2015 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>
20 #include <dali/devel-api/rendering/geometry.h>
21 #include <dali/devel-api/rendering/renderer.h>
25 #include <mesh-builder.h>
27 void geometry_test_startup(void)
29 test_return_value = TET_UNDEF;
32 void geometry_test_cleanup(void)
34 test_return_value = TET_PASS;
40 void TestConstraintNoBlue( Vector4& current, const PropertyInputContainer& inputs )
45 struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
47 PropertyBuffer CreateVertexBuffer( const std::string& aPosition, const std::string& aTexCoord )
49 const float halfQuadSize = .5f;
50 TexturedQuadVertex texturedQuadVertexData[4] = {
51 { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
52 { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
53 { Vector2(-halfQuadSize, halfQuadSize), Vector2(0.f, 1.f) },
54 { Vector2( halfQuadSize, halfQuadSize), Vector2(1.f, 1.f) } };
56 Property::Map vertexFormat;
57 vertexFormat[aPosition] = Property::VECTOR2;
58 vertexFormat[aTexCoord] = Property::VECTOR2;
60 PropertyBuffer vertexData = PropertyBuffer::New( vertexFormat );
61 vertexData.SetData( texturedQuadVertexData, 4 );
70 int UtcDaliGeometryNew01(void)
72 TestApplication application;
74 Geometry geometry = Geometry::New();
76 DALI_TEST_EQUALS( (bool)geometry, true, TEST_LOCATION );
80 int UtcDaliGeometryNew02(void)
82 TestApplication application;
84 DALI_TEST_EQUALS( (bool)geometry, false, TEST_LOCATION );
88 int UtcDaliGeometryCopyConstructor(void)
90 TestApplication application;
92 Geometry geometry = Geometry::New();
94 Geometry geometryCopy(geometry);
96 DALI_TEST_EQUALS( (bool)geometryCopy, true, TEST_LOCATION );
100 int UtcDaliGeometryAssignmentOperator(void)
102 TestApplication application;
104 Geometry geometry = Geometry::New();
107 DALI_TEST_EQUALS( (bool)geometry2, false, TEST_LOCATION );
109 geometry2 = geometry;
110 DALI_TEST_EQUALS( (bool)geometry2, true, TEST_LOCATION );
115 int UtcDaliGeometryDownCast01(void)
117 TestApplication application;
119 Geometry geometry = Geometry::New();
121 BaseHandle handle(geometry);
122 Geometry geometry2 = Geometry::DownCast(handle);
123 DALI_TEST_EQUALS( (bool)geometry2, true, TEST_LOCATION );
127 int UtcDaliGeometryDownCast02(void)
129 TestApplication application;
131 Handle handle = Handle::New(); // Create a custom object
132 Geometry geometry = Geometry::DownCast(handle);
133 DALI_TEST_EQUALS( (bool)geometry, false, TEST_LOCATION );
137 int UtcDaliGeometryAddVertexBuffer(void)
139 TestApplication application;
141 tet_infoline("Test AddVertexBuffer");
143 PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
144 Geometry geometry = Geometry::New();
145 geometry.AddVertexBuffer( vertexBuffer1 );
147 Shader shader = CreateShader();
148 Renderer renderer = Renderer::New(geometry, shader);
149 Actor actor = Actor::New();
150 actor.SetSize(Vector3::ONE * 100.f);
151 actor.AddRenderer(renderer);
152 Stage::GetCurrent().Add(actor);
154 application.SendNotification();
155 application.Render(0);
156 application.Render();
157 application.SendNotification();
160 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
161 application.GetGlAbstraction().GetBufferDataCalls();
163 DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
165 DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
168 // add the second vertex buffer
169 application.GetGlAbstraction().ResetBufferDataCalls();
171 PropertyBuffer vertexBuffer2 = CreateVertexBuffer( "aPosition2", "aTexCoord2" );
172 geometry.AddVertexBuffer( vertexBuffer2 );
173 application.SendNotification();
174 application.Render(0);
175 application.Render();
176 application.SendNotification();
179 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
180 application.GetGlAbstraction().GetBufferDataCalls();
182 //Check that only the new buffer gets uploaded
183 DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
184 DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
190 int UtcDaliGeometryGetNumberOfVertexBuffers(void)
192 TestApplication application;
194 tet_infoline("Test GetNumberOfVertexBuffers");
195 PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
196 PropertyBuffer vertexBuffer2 = CreateVertexBuffer("aPosition2", "aTexCoord2" );
197 PropertyBuffer vertexBuffer3 = CreateVertexBuffer("aPosition3", "aTexCoord3" );
199 Geometry geometry = Geometry::New();
200 geometry.AddVertexBuffer( vertexBuffer1 );
201 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
203 geometry.AddVertexBuffer( vertexBuffer2 );
204 geometry.AddVertexBuffer( vertexBuffer3 );
205 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 3u, TEST_LOCATION );
207 geometry.RemoveVertexBuffer( 2u );
208 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 2u, TEST_LOCATION );
213 int UtcDaliGeometryRemoveVertexBuffer(void)
215 TestApplication application;
217 tet_infoline("Test RemoveVertexBuffer");
219 PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
220 PropertyBuffer vertexBuffer2 = CreateVertexBuffer("aPosition2", "aTexCoord2" );
222 Geometry geometry = Geometry::New();
223 geometry.AddVertexBuffer( vertexBuffer1 );
225 Shader shader = CreateShader();
226 Renderer renderer = Renderer::New(geometry, shader);
227 Actor actor = Actor::New();
228 actor.SetSize(Vector3::ONE * 100.f);
229 actor.AddRenderer(renderer);
230 Stage::GetCurrent().Add(actor);
232 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
234 geometry.RemoveVertexBuffer( 0 );
235 geometry.AddVertexBuffer( vertexBuffer2 );
236 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
238 geometry.RemoveVertexBuffer( 0 );
239 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 0u, TEST_LOCATION );
241 //Todo: test by checking the BufferDataCalls
242 // make sure the vertex buffer in actually removed from gl
247 int UtcDaliGeometrySetIndexBuffer(void)
249 TestApplication application;
251 tet_infoline("Test SetIndexBuffer");
253 PropertyBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord" );
255 Geometry geometry = Geometry::New();
256 geometry.AddVertexBuffer( vertexBuffer );
258 Shader shader = CreateShader();
259 Renderer renderer = Renderer::New(geometry, shader);
260 Actor actor = Actor::New();
261 actor.SetSize(Vector3::ONE * 100.f);
262 actor.AddRenderer(renderer);
263 Stage::GetCurrent().Add(actor);
265 application.SendNotification();
266 application.Render(0);
267 application.Render();
268 application.SendNotification();
271 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
272 application.GetGlAbstraction().GetBufferDataCalls();
274 DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
276 DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
280 application.GetGlAbstraction().ResetBufferDataCalls();
282 const unsigned short indexData[6] = { 0, 3, 1, 0, 2, 3 };
283 geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
284 application.SendNotification();
285 application.Render(0);
286 application.Render();
287 application.SendNotification();
290 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
291 application.GetGlAbstraction().GetBufferDataCalls();
293 //Only the index buffer should be uploaded
294 DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
296 // should be unsigned short instead of unsigned int
297 DALI_TEST_EQUALS( bufferDataCalls[0], 6*sizeof( unsigned short ), TEST_LOCATION );
304 int UtcDaliGeometrySetGetGeometryType01(void)
306 TestApplication application;
308 tet_infoline("Test SetGeometryType and GetGeometryType: without index buffer");
310 unsigned int numVertex = 4u;
311 PropertyBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord" );
313 Geometry geometry = Geometry::New();
314 geometry.AddVertexBuffer( vertexBuffer );
316 Shader shader = CreateShader();
317 Renderer renderer = Renderer::New(geometry, shader);
318 Actor actor = Actor::New();
319 actor.SetSize(Vector3::ONE * 100.f);
320 actor.AddRenderer(renderer);
321 Stage::GetCurrent().Add(actor);
323 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
324 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
326 /****************************************************/
327 // Default (TRIANGLES), no index buffer
329 drawTrace.Enable(true);
330 application.SendNotification();
331 application.Render(0);
332 application.Render();
333 application.SendNotification();
334 drawTrace.Enable( false );
336 // Test the default geometry type is GL_TRIANGLE
337 // no index buffer, call glDrawArrays,
338 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
339 std::stringstream out;
340 out << GL_TRIANGLES << ", " << 0 << ", " << numVertex;
341 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
343 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::TRIANGLES, TEST_LOCATION);
345 /*********************************************************/
346 // LINES, no index buffer
347 geometry.SetGeometryType( Geometry::LINES );
350 drawTrace.Enable(true);
351 application.SendNotification();
352 application.Render(0);
353 application.Render();
354 application.SendNotification();
355 drawTrace.Enable( false );
357 // geometry type is set as GL_LINES
358 // no index buffer, call glDrawArrays,
359 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
361 out << GL_LINES << ", " << 0 << ", " << numVertex;
362 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
364 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::LINES, TEST_LOCATION);
366 /*****************************************************/
368 geometry.SetGeometryType( Geometry::POINTS );
371 drawTrace.Enable(true);
372 application.SendNotification();
373 application.Render(0);
374 application.Render();
375 application.SendNotification();
376 drawTrace.Enable( false );
378 // geometry type is set as GL_POINTS
379 // no index buffer, call glDrawArrays,
380 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
382 out << GL_POINTS << ", " << 0 << ", " << numVertex;
383 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
385 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::POINTS, TEST_LOCATION);
387 /*****************************************************/
388 //TRIANGLE_STRIP, no index buffer
389 geometry.SetGeometryType( Geometry::TRIANGLE_STRIP );
392 drawTrace.Enable(true);
393 application.SendNotification();
394 application.Render(0);
395 application.Render();
396 application.SendNotification();
397 drawTrace.Enable( false );
399 // geometry type is set as GL_POINTS
400 // no index buffer, call glDrawArrays,
401 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
403 out << GL_TRIANGLE_STRIP << ", " << 0 << ", " << numVertex;
404 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
406 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::TRIANGLE_STRIP, TEST_LOCATION);
408 /*****************************************************/
409 //TRIANGLE_FAN, no index buffer
410 geometry.SetGeometryType( Geometry::TRIANGLE_FAN );
413 drawTrace.Enable(true);
414 application.SendNotification();
415 application.Render(0);
416 application.Render();
417 application.SendNotification();
418 drawTrace.Enable( false );
420 // geometry type is set as GL_POINTS
421 // no index buffer, call glDrawArrays,
422 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
424 out << GL_TRIANGLE_FAN << ", " << 0 << ", " << numVertex;
425 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
427 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::TRIANGLE_FAN, TEST_LOCATION);
432 int UtcDaliGeometrySetGetGeometryType02(void)
434 TestApplication application;
436 tet_infoline("Test SetGeometryType and GetGeometryType: with index buffer");
438 unsigned int numVertex = 4u;
439 unsigned int numIndex = 6u; // 6 unsigned short
440 PropertyBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord" );
443 Geometry geometry = Geometry::New();
444 geometry.AddVertexBuffer( vertexBuffer );
445 const unsigned short indexData[6] = { 0, 3, 1, 0, 2, 3 };
446 geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
448 Shader shader = CreateShader();
449 Renderer renderer = Renderer::New(geometry, shader);
450 Actor actor = Actor::New();
451 actor.SetSize(Vector3::ONE * 100.f);
452 actor.AddRenderer(renderer);
453 Stage::GetCurrent().Add(actor);
455 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
456 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
458 /****************************************************/
459 // Default (TRIANGLES), no index buffer
461 drawTrace.Enable(true);
462 application.SendNotification();
463 application.Render(0);
464 application.Render();
465 application.SendNotification();
466 drawTrace.Enable( false );
468 // Test the default geometry type is GL_TRIANGLE
469 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawElements" ), 2, TEST_LOCATION);
470 std::stringstream out;
471 out << GL_TRIANGLES << ", " << numIndex << ", " << GL_UNSIGNED_SHORT<<", "<<"indices";
472 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
474 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::TRIANGLES, TEST_LOCATION);
476 /*********************************************************/
477 // LINES, no index buffer
478 geometry.SetGeometryType( Geometry::LINES );
481 drawTrace.Enable(true);
482 application.SendNotification();
483 application.Render(0);
484 application.Render();
485 application.SendNotification();
486 drawTrace.Enable( false );
488 // geometry type is set as GL_LINES
489 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawElements" ), 2, TEST_LOCATION);
491 out << GL_LINES << ", " << numIndex << ", " << GL_UNSIGNED_SHORT<<", "<<"indices";
492 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
494 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::LINES, TEST_LOCATION);
496 /*****************************************************/
498 geometry.SetGeometryType( Geometry::POINTS );
501 drawTrace.Enable(true);
502 application.SendNotification();
503 application.Render(0);
504 application.Render();
505 application.SendNotification();
506 drawTrace.Enable( false );
508 // geometry type is set as GL_POINTS
509 // As Points does not use the index buffer, call glDrawArrays,
510 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
512 out << GL_POINTS << ", " << 0 << ", " << numVertex;
513 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
515 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::POINTS, TEST_LOCATION);
517 /*****************************************************/
519 geometry.SetGeometryType( Geometry::TRIANGLE_STRIP );
522 drawTrace.Enable(true);
523 application.SendNotification();
524 application.Render(0);
525 application.Render();
526 application.SendNotification();
527 drawTrace.Enable( false );
529 // geometry type is set as GL_POINTS
530 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawElements" ), 2, TEST_LOCATION);
532 out << GL_TRIANGLE_STRIP << ", " << numIndex << ", " << GL_UNSIGNED_SHORT<<", "<<"indices";
533 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
535 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::TRIANGLE_STRIP, TEST_LOCATION);
537 /*****************************************************/
539 geometry.SetGeometryType( Geometry::TRIANGLE_FAN );
542 drawTrace.Enable(true);
543 application.SendNotification();
544 application.Render(0);
545 application.Render();
546 application.SendNotification();
547 drawTrace.Enable( false );
549 // geometry type is set as GL_POINTS
550 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawElements" ), 2, TEST_LOCATION);
552 out << GL_TRIANGLE_FAN << ", " << numIndex << ", " << GL_UNSIGNED_SHORT<<", "<<"indices";
553 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
555 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::TRIANGLE_FAN, TEST_LOCATION);
560 int UtcDaliGeometrySetGetRequireDepthTesting(void)
562 TestApplication application;
564 tet_infoline("Test SetRequiresDepthTesting, GetRequiresDepthTesting");
566 Shader shader = Shader::New("VertexSource", "FragmentSource");
567 Geometry geometry = CreateQuadGeometry();
568 Renderer renderer = Renderer::New( geometry, shader );
570 Actor actor = Actor::New();
571 actor.AddRenderer(renderer);
572 actor.SetSize(400, 400);
573 Stage::GetCurrent().Add(actor);
575 DALI_TEST_EQUALS( geometry.GetRequiresDepthTesting(), false, TEST_LOCATION );
577 geometry.SetRequiresDepthTesting(true);
579 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
580 glAbstraction.EnableEnableDisableCallTrace(true);
581 application.SendNotification();
582 application.Render();
583 // TODO: Not supported yes
584 // TraceCallStack& glEnableStack = glAbstraction.GetEnableDisableTrace();
585 // std::ostringstream out;
586 // out << GL_DEPTH_TEST;
587 // DALI_TEST_CHECK( glEnableStack.FindMethodAndParams( "Enable", out.str().c_str() ) );
589 DALI_TEST_EQUALS( geometry.GetRequiresDepthTesting(), true, TEST_LOCATION );