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, 4 );
61 vertexData.SetData(texturedQuadVertexData);
66 PropertyBuffer CreateIndexBuffer()
68 unsigned short indexData[6] = { 0, 3, 1, 0, 2, 3 };
69 Property::Map indexFormat;
70 indexFormat["indices"] = Property::UNSIGNED_INTEGER; // Should be Unsigned Short
71 PropertyBuffer indices = PropertyBuffer::New( indexFormat, 3 );
72 indices.SetData(indexData);
80 int UtcDaliGeometryNew01(void)
82 TestApplication application;
84 Geometry geometry = Geometry::New();
86 DALI_TEST_EQUALS( (bool)geometry, true, TEST_LOCATION );
90 int UtcDaliGeometryNew02(void)
92 TestApplication application;
94 DALI_TEST_EQUALS( (bool)geometry, false, TEST_LOCATION );
98 int UtcDaliGeometryCopyConstructor(void)
100 TestApplication application;
102 Geometry geometry = Geometry::New();
104 Geometry geometryCopy(geometry);
106 DALI_TEST_EQUALS( (bool)geometryCopy, true, TEST_LOCATION );
110 int UtcDaliGeometryAssignmentOperator(void)
112 TestApplication application;
114 Geometry geometry = Geometry::New();
117 DALI_TEST_EQUALS( (bool)geometry2, false, TEST_LOCATION );
119 geometry2 = geometry;
120 DALI_TEST_EQUALS( (bool)geometry2, true, TEST_LOCATION );
125 int UtcDaliGeometryDownCast01(void)
127 TestApplication application;
129 Geometry geometry = Geometry::New();
131 BaseHandle handle(geometry);
132 Geometry geometry2 = Geometry::DownCast(handle);
133 DALI_TEST_EQUALS( (bool)geometry2, true, TEST_LOCATION );
137 int UtcDaliGeometryDownCast02(void)
139 TestApplication application;
141 Handle handle = Handle::New(); // Create a custom object
142 Geometry geometry = Geometry::DownCast(handle);
143 DALI_TEST_EQUALS( (bool)geometry, false, TEST_LOCATION );
147 int UtcDaliGeometryAddVertexBuffer(void)
149 TestApplication application;
151 tet_infoline("Test AddVertexBuffer");
153 PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
154 Geometry geometry = Geometry::New();
155 geometry.AddVertexBuffer( vertexBuffer1 );
157 Material material = CreateMaterial(1.f);
158 Renderer renderer = Renderer::New(geometry, material);
159 Actor actor = Actor::New();
160 actor.SetSize(Vector3::ONE * 100.f);
161 actor.AddRenderer(renderer);
162 Stage::GetCurrent().Add(actor);
164 application.SendNotification();
165 application.Render(0);
166 application.Render();
167 application.SendNotification();
170 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
171 application.GetGlAbstraction().GetBufferDataCalls();
173 DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
175 DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
178 // add the second vertex buffer
179 application.GetGlAbstraction().ResetBufferDataCalls();
181 PropertyBuffer vertexBuffer2 = CreateVertexBuffer( "aPosition2", "aTexCoord2" );
182 geometry.AddVertexBuffer( vertexBuffer2 );
183 application.SendNotification();
184 application.Render(0);
185 application.Render();
186 application.SendNotification();
189 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
190 application.GetGlAbstraction().GetBufferDataCalls();
192 DALI_TEST_EQUALS( bufferDataCalls.size(), 2u, TEST_LOCATION );
194 DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
195 DALI_TEST_EQUALS( bufferDataCalls[1], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
201 int UtcDaliGeometryGetNumberOfVertexBuffers(void)
203 TestApplication application;
205 tet_infoline("Test GetNumberOfVertexBuffers");
206 PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
207 PropertyBuffer vertexBuffer2 = CreateVertexBuffer("aPosition2", "aTexCoord2" );
208 PropertyBuffer vertexBuffer3 = CreateVertexBuffer("aPosition3", "aTexCoord3" );
210 Geometry geometry = Geometry::New();
211 geometry.AddVertexBuffer( vertexBuffer1 );
212 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
214 geometry.AddVertexBuffer( vertexBuffer2 );
215 geometry.AddVertexBuffer( vertexBuffer3 );
216 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 3u, TEST_LOCATION );
218 geometry.RemoveVertexBuffer( 2u );
219 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 2u, TEST_LOCATION );
224 int UtcDaliGeometryRemoveVertexBuffer(void)
226 TestApplication application;
228 tet_infoline("Test RemoveVertexBuffer");
230 PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
231 PropertyBuffer vertexBuffer2 = CreateVertexBuffer("aPosition2", "aTexCoord2" );
233 Geometry geometry = Geometry::New();
234 geometry.AddVertexBuffer( vertexBuffer1 );
236 Material material = CreateMaterial(1.f);
237 Renderer renderer = Renderer::New(geometry, material);
238 Actor actor = Actor::New();
239 actor.SetSize(Vector3::ONE * 100.f);
240 actor.AddRenderer(renderer);
241 Stage::GetCurrent().Add(actor);
243 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
245 geometry.RemoveVertexBuffer( 0 );
246 geometry.AddVertexBuffer( vertexBuffer2 );
247 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
249 geometry.RemoveVertexBuffer( 0 );
250 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 0u, TEST_LOCATION );
252 //Todo: test by checking the BufferDataCalls
253 // make sure the vertex buffer in actually removed from gl
258 int UtcDaliGeometrySetIndexBuffer(void)
260 TestApplication application;
262 tet_infoline("Test SetIndexBuffer");
264 PropertyBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord" );
265 PropertyBuffer indexBuffer = CreateIndexBuffer( );
267 Geometry geometry = Geometry::New();
268 geometry.AddVertexBuffer( vertexBuffer );
270 Material material = CreateMaterial(1.f);
271 Renderer renderer = Renderer::New(geometry, material);
272 Actor actor = Actor::New();
273 actor.SetSize(Vector3::ONE * 100.f);
274 actor.AddRenderer(renderer);
275 Stage::GetCurrent().Add(actor);
277 application.SendNotification();
278 application.Render(0);
279 application.Render();
280 application.SendNotification();
283 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
284 application.GetGlAbstraction().GetBufferDataCalls();
286 DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
288 DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
292 application.GetGlAbstraction().ResetBufferDataCalls();
294 geometry.SetIndexBuffer( indexBuffer );
295 application.SendNotification();
296 application.Render(0);
297 application.Render();
298 application.SendNotification();
301 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
302 application.GetGlAbstraction().GetBufferDataCalls();
304 DALI_TEST_EQUALS( bufferDataCalls.size(), 2u, TEST_LOCATION );
306 DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
307 // should be unsigned short instead of unsigned int
308 DALI_TEST_EQUALS( bufferDataCalls[1], 6*sizeof( unsigned short ), TEST_LOCATION );
315 int UtcDaliGeometrySetGetGeometryType(void)
317 TestApplication application;
319 tet_infoline("Test SetGeometryType and GetGeometryType");
321 unsigned int numVertex = 4u;
322 PropertyBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord" );
324 Geometry geometry = Geometry::New();
325 geometry.AddVertexBuffer( vertexBuffer );
327 Material material = CreateMaterial(1.f);
328 Renderer renderer = Renderer::New(geometry, material);
329 Actor actor = Actor::New();
330 actor.SetSize(Vector3::ONE * 100.f);
331 actor.AddRenderer(renderer);
332 Stage::GetCurrent().Add(actor);
334 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
335 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
337 /****************************************************/
338 // Default (TRIANGLES), no index buffer
340 drawTrace.Enable(true);
341 application.SendNotification();
342 application.Render(0);
343 application.Render();
344 application.SendNotification();
345 drawTrace.Enable( false );
347 // Test the default geometry type is GL_TRIANGLE
348 // no index buffer, call glDrawArrays,
349 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
350 std::stringstream out;
351 out << GL_TRIANGLES << ", " << 0 << ", " << numVertex;
352 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
354 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::TRIANGLES, TEST_LOCATION);
356 /*********************************************************/
357 // LINES, no index buffer
358 geometry.SetGeometryType( Geometry::LINES );
361 drawTrace.Enable(true);
362 application.SendNotification();
363 application.Render(0);
364 application.Render();
365 application.SendNotification();
366 drawTrace.Enable( false );
368 // geometry type is set as GL_LINES
369 // no index buffer, call glDrawArrays,
370 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
372 out << GL_LINES << ", " << 0 << ", " << numVertex;
373 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
375 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::LINES, TEST_LOCATION);
377 /*****************************************************/
379 geometry.SetGeometryType( Geometry::POINTS );
382 drawTrace.Enable(true);
383 application.SendNotification();
384 application.Render(0);
385 application.Render();
386 application.SendNotification();
387 drawTrace.Enable( false );
389 // geometry type is set as GL_POINTS
390 // no index buffer, call glDrawArrays,
391 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
393 out << GL_POINTS << ", " << 0 << ", " << numVertex;
394 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
396 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::POINTS, TEST_LOCATION);
401 int UtcDaliGeometrySetGetRequireDepthTesting(void)
403 TestApplication application;
405 tet_infoline("Test SetRequiresDepthTesting, GetRequiresDepthTesting");
407 Shader shader = Shader::New("VertexSource", "FragmentSource");
408 Material material = Material::New( shader );
410 Geometry geometry = CreateQuadGeometry();
411 Renderer renderer = Renderer::New( geometry, material );
413 Actor actor = Actor::New();
414 actor.AddRenderer(renderer);
415 actor.SetSize(400, 400);
416 Stage::GetCurrent().Add(actor);
418 DALI_TEST_EQUALS( geometry.GetRequiresDepthTesting(), false, TEST_LOCATION );
420 geometry.SetRequiresDepthTesting(true);
422 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
423 glAbstraction.EnableCullFaceCallTrace(true);
424 application.SendNotification();
425 application.Render();
426 // TODO: Not supported yes
427 // TraceCallStack& glEnableStack = glAbstraction.GetCullFaceTrace();
428 // std::ostringstream out;
429 // out << GL_DEPTH_TEST;
430 // DALI_TEST_CHECK( glEnableStack.FindMethodAndParams( "Enable", out.str().c_str() ) );
432 DALI_TEST_EQUALS( geometry.GetRequiresDepthTesting(), true, TEST_LOCATION );
437 int UtcDaliGeometryPropertyRequiresDepthTest(void)
439 TestApplication application;
441 tet_infoline("Test SetRequiresDepthTesting, GetRequiresDepthTesting");
443 Shader shader = Shader::New("VertexSource", "FragmentSource");
444 Material material = Material::New( shader );
446 Geometry geometry = CreateQuadGeometry();
447 Renderer renderer = Renderer::New( geometry, material );
449 Actor actor = Actor::New();
450 actor.AddRenderer(renderer);
451 actor.SetSize(400, 400);
452 Stage::GetCurrent().Add(actor);
454 DALI_TEST_EQUALS( geometry.GetProperty<bool>(Geometry::Property::REQUIRES_DEPTH_TEST), false, TEST_LOCATION );
456 geometry.SetProperty(Geometry::Property::REQUIRES_DEPTH_TEST, true );
458 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
459 glAbstraction.EnableCullFaceCallTrace(true);
460 application.SendNotification();
461 application.Render();
462 // TODO: Not supported yes
463 // TraceCallStack& glEnableStack = glAbstraction.GetCullFaceTrace();
464 // std::ostringstream out;
465 // out << GL_DEPTH_TEST;
466 // DALI_TEST_CHECK( glEnableStack.FindMethodAndParams( "Enable", out.str().c_str() ) );
468 DALI_TEST_EQUALS( geometry.GetProperty<bool>(Geometry::Property::REQUIRES_DEPTH_TEST), true, TEST_LOCATION );
473 int UtcDaliGeometryConstraint(void)
475 TestApplication application;
477 tet_infoline("Test that a custom geometry property can be constrained");
479 Shader shader = Shader::New("VertexSource", "FragmentSource");
480 Material material = Material::New( shader );
481 material.SetProperty(Material::Property::COLOR, Color::WHITE);
483 Geometry geometry = CreateQuadGeometry();
484 Renderer renderer = Renderer::New( geometry, material );
486 Actor actor = Actor::New();
487 actor.AddRenderer(renderer);
488 actor.SetSize(400, 400);
489 Stage::GetCurrent().Add(actor);
491 Vector4 initialColor = Color::WHITE;
492 Property::Index colorIndex = geometry.RegisterProperty( "uFadeColor", initialColor );
494 application.SendNotification();
495 application.Render(0);
496 DALI_TEST_EQUALS( geometry.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
499 Constraint constraint = Constraint::New<Vector4>( geometry, colorIndex, TestConstraintNoBlue );
501 application.SendNotification();
502 application.Render(0);
504 // Expect no blue component in either buffer - yellow
505 DALI_TEST_EQUALS( geometry.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
506 application.Render(0);
507 DALI_TEST_EQUALS( geometry.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
509 geometry.RemoveConstraints();
510 geometry.SetProperty(colorIndex, Color::WHITE );
511 application.SendNotification();
512 application.Render(0);
513 DALI_TEST_EQUALS( geometry.GetProperty<Vector4>(colorIndex), Color::WHITE, TEST_LOCATION );
518 int UtcDaliGeometryConstraint02(void)
520 TestApplication application;
522 tet_infoline("Test that a uniform map geometry property can be constrained");
524 Shader shader = Shader::New("VertexSource", "FragmentSource");
525 Material material = Material::New( shader );
526 material.SetProperty(Material::Property::COLOR, Color::WHITE);
528 Geometry geometry = CreateQuadGeometry();
529 Renderer renderer = Renderer::New( geometry, material );
531 Actor actor = Actor::New();
532 actor.AddRenderer(renderer);
533 actor.SetSize(400, 400);
534 Stage::GetCurrent().Add(actor);
535 application.SendNotification();
536 application.Render(0);
538 Vector4 initialColor = Color::WHITE;
539 Property::Index colorIndex = geometry.RegisterProperty( "uFadeColor", initialColor );
541 TestGlAbstraction& gl = application.GetGlAbstraction();
543 application.SendNotification();
544 application.Render(0);
546 Vector4 actualValue(Vector4::ZERO);
547 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
548 DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
551 Constraint constraint = Constraint::New<Vector4>( geometry, colorIndex, TestConstraintNoBlue );
553 application.SendNotification();
554 application.Render(0);
556 // Expect no blue component in either buffer - yellow
557 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
558 DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
560 application.Render(0);
561 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
562 DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
564 geometry.RemoveConstraints();
565 geometry.SetProperty(colorIndex, Color::WHITE );
566 application.SendNotification();
567 application.Render(0);
569 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
570 DALI_TEST_EQUALS( actualValue, Color::WHITE, TEST_LOCATION );
577 int UtcDaliGeometryAnimatedProperty01(void)
579 TestApplication application;
581 tet_infoline("Test that a custom geometry property can be animated");
583 Shader shader = Shader::New("VertexSource", "FragmentSource");
584 Material material = Material::New( shader );
585 material.SetProperty(Material::Property::COLOR, Color::WHITE);
587 Geometry geometry = CreateQuadGeometry();
588 Renderer renderer = Renderer::New( geometry, material );
590 Actor actor = Actor::New();
591 actor.AddRenderer(renderer);
592 actor.SetSize(400, 400);
593 Stage::GetCurrent().Add(actor);
595 Vector4 initialColor = Color::WHITE;
596 Property::Index colorIndex = geometry.RegisterProperty( "uFadeColor", initialColor );
598 application.SendNotification();
599 application.Render(0);
600 DALI_TEST_EQUALS( geometry.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
602 Animation animation = Animation::New(1.0f);
603 KeyFrames keyFrames = KeyFrames::New();
604 keyFrames.Add(0.0f, initialColor);
605 keyFrames.Add(1.0f, Color::TRANSPARENT);
606 animation.AnimateBetween( Property( geometry, colorIndex ), keyFrames );
609 application.SendNotification();
610 application.Render(500);
612 DALI_TEST_EQUALS( geometry.GetProperty<Vector4>(colorIndex), Color::WHITE * 0.5f, TEST_LOCATION );
614 application.Render(500);
616 DALI_TEST_EQUALS( geometry.GetProperty<Vector4>(colorIndex), Color::TRANSPARENT, TEST_LOCATION );
621 int UtcDaliGeometryAnimatedProperty02(void)
623 TestApplication application;
625 tet_infoline("Test that a uniform map geometry property can be animated");
627 Shader shader = Shader::New("VertexSource", "FragmentSource");
628 Material material = Material::New( shader );
629 material.SetProperty(Material::Property::COLOR, Color::WHITE);
631 Geometry geometry = CreateQuadGeometry();
632 Renderer renderer = Renderer::New( geometry, material );
634 Actor actor = Actor::New();
635 actor.AddRenderer(renderer);
636 actor.SetSize(400, 400);
637 Stage::GetCurrent().Add(actor);
638 application.SendNotification();
639 application.Render(0);
641 Vector4 initialColor = Color::WHITE;
642 Property::Index colorIndex = geometry.RegisterProperty( "uFadeColor", initialColor );
644 TestGlAbstraction& gl = application.GetGlAbstraction();
646 application.SendNotification();
647 application.Render(0);
649 Vector4 actualValue(Vector4::ZERO);
650 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
651 DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
653 Animation animation = Animation::New(1.0f);
654 KeyFrames keyFrames = KeyFrames::New();
655 keyFrames.Add(0.0f, initialColor);
656 keyFrames.Add(1.0f, Color::TRANSPARENT);
657 animation.AnimateBetween( Property( geometry, colorIndex ), keyFrames );
660 application.SendNotification();
661 application.Render(500);
663 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
664 DALI_TEST_EQUALS( actualValue, Color::WHITE * 0.5f, TEST_LOCATION );
666 application.Render(500);
667 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
668 DALI_TEST_EQUALS( actualValue, Color::TRANSPARENT, TEST_LOCATION );