Remove Geometry scene object
[platform/core/uifw/dali-core.git] / automated-tests / src / dali-devel / utc-Dali-Geometry.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
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>
22
23 using namespace Dali;
24
25 #include <mesh-builder.h>
26
27 void geometry_test_startup(void)
28 {
29   test_return_value = TET_UNDEF;
30 }
31
32 void geometry_test_cleanup(void)
33 {
34   test_return_value = TET_PASS;
35 }
36
37 namespace
38 {
39
40 void TestConstraintNoBlue( Vector4& current, const PropertyInputContainer& inputs )
41 {
42   current.b = 0.0f;
43 }
44
45 struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
46
47 PropertyBuffer CreateVertexBuffer( const std::string& aPosition, const std::string& aTexCoord )
48 {
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) } };
55
56   Property::Map vertexFormat;
57   vertexFormat[aPosition] = Property::VECTOR2;
58   vertexFormat[aTexCoord] = Property::VECTOR2;
59
60   PropertyBuffer vertexData = PropertyBuffer::New( vertexFormat );
61   vertexData.SetData( texturedQuadVertexData, 4 );
62
63   return vertexData;
64 }
65
66
67 }
68
69
70 int UtcDaliGeometryNew01(void)
71 {
72   TestApplication application;
73
74   Geometry geometry = Geometry::New();
75
76   DALI_TEST_EQUALS( (bool)geometry, true, TEST_LOCATION );
77   END_TEST;
78 }
79
80 int UtcDaliGeometryNew02(void)
81 {
82   TestApplication application;
83   Geometry geometry;
84   DALI_TEST_EQUALS( (bool)geometry, false, TEST_LOCATION );
85   END_TEST;
86 }
87
88 int UtcDaliGeometryCopyConstructor(void)
89 {
90   TestApplication application;
91
92   Geometry geometry = Geometry::New();
93
94   Geometry geometryCopy(geometry);
95
96   DALI_TEST_EQUALS( (bool)geometryCopy, true, TEST_LOCATION );
97   END_TEST;
98 }
99
100 int UtcDaliGeometryAssignmentOperator(void)
101 {
102   TestApplication application;
103
104   Geometry geometry = Geometry::New();
105
106   Geometry geometry2;
107   DALI_TEST_EQUALS( (bool)geometry2, false, TEST_LOCATION );
108
109   geometry2 = geometry;
110   DALI_TEST_EQUALS( (bool)geometry2, true, TEST_LOCATION );
111
112   END_TEST;
113 }
114
115 int UtcDaliGeometryDownCast01(void)
116 {
117   TestApplication application;
118
119   Geometry geometry = Geometry::New();
120
121   BaseHandle handle(geometry);
122   Geometry geometry2 = Geometry::DownCast(handle);
123   DALI_TEST_EQUALS( (bool)geometry2, true, TEST_LOCATION );
124   END_TEST;
125 }
126
127 int UtcDaliGeometryDownCast02(void)
128 {
129   TestApplication application;
130
131   Handle handle = Handle::New(); // Create a custom object
132   Geometry geometry = Geometry::DownCast(handle);
133   DALI_TEST_EQUALS( (bool)geometry, false, TEST_LOCATION );
134   END_TEST;
135 }
136
137 int UtcDaliGeometryAddVertexBuffer(void)
138 {
139   TestApplication application;
140
141   tet_infoline("Test AddVertexBuffer");
142
143   PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
144   Geometry geometry = Geometry::New();
145   geometry.AddVertexBuffer( vertexBuffer1 );
146
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);
153
154   application.SendNotification();
155   application.Render(0);
156   application.Render();
157   application.SendNotification();
158
159   {
160     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
161         application.GetGlAbstraction().GetBufferDataCalls();
162
163     DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
164
165     DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
166   }
167
168   // add the second vertex buffer
169   application.GetGlAbstraction().ResetBufferDataCalls();
170
171   PropertyBuffer vertexBuffer2 = CreateVertexBuffer( "aPosition2", "aTexCoord2" );
172   geometry.AddVertexBuffer( vertexBuffer2 );
173   application.SendNotification();
174   application.Render(0);
175   application.Render();
176   application.SendNotification();
177
178   {
179     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
180         application.GetGlAbstraction().GetBufferDataCalls();
181
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 );
185   }
186
187   END_TEST;
188 }
189
190 int UtcDaliGeometryGetNumberOfVertexBuffers(void)
191 {
192   TestApplication application;
193
194   tet_infoline("Test GetNumberOfVertexBuffers");
195   PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
196   PropertyBuffer vertexBuffer2 = CreateVertexBuffer("aPosition2", "aTexCoord2" );
197   PropertyBuffer vertexBuffer3 = CreateVertexBuffer("aPosition3", "aTexCoord3" );
198
199   Geometry geometry = Geometry::New();
200   geometry.AddVertexBuffer( vertexBuffer1 );
201   DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
202
203   geometry.AddVertexBuffer( vertexBuffer2 );
204   geometry.AddVertexBuffer( vertexBuffer3 );
205   DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 3u, TEST_LOCATION );
206
207   geometry.RemoveVertexBuffer( 2u );
208   DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 2u, TEST_LOCATION );
209
210   END_TEST;
211 }
212
213 int UtcDaliGeometryRemoveVertexBuffer(void)
214 {
215   TestApplication application;
216
217   tet_infoline("Test RemoveVertexBuffer");
218
219   PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
220   PropertyBuffer vertexBuffer2 = CreateVertexBuffer("aPosition2", "aTexCoord2" );
221
222   Geometry geometry = Geometry::New();
223   geometry.AddVertexBuffer( vertexBuffer1 );
224
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);
231
232   DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
233
234   geometry.RemoveVertexBuffer( 0 );
235   geometry.AddVertexBuffer( vertexBuffer2 );
236   DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
237
238   geometry.RemoveVertexBuffer( 0 );
239   DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 0u, TEST_LOCATION );
240
241   //Todo: test by checking the BufferDataCalls
242   // make sure the vertex buffer in actually removed from gl
243
244    END_TEST;
245 }
246
247 int UtcDaliGeometrySetIndexBuffer(void)
248 {
249   TestApplication application;
250
251   tet_infoline("Test SetIndexBuffer");
252
253   PropertyBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord" );
254
255   Geometry geometry = Geometry::New();
256   geometry.AddVertexBuffer( vertexBuffer );
257
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);
264
265   application.SendNotification();
266   application.Render(0);
267   application.Render();
268   application.SendNotification();
269
270   {
271     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
272         application.GetGlAbstraction().GetBufferDataCalls();
273
274     DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
275
276     DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
277   }
278
279   // Set index buffer
280   application.GetGlAbstraction().ResetBufferDataCalls();
281
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();
288
289   {
290     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
291         application.GetGlAbstraction().GetBufferDataCalls();
292
293     //Only the index buffer should be uploaded
294     DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
295
296     // should be unsigned short instead of unsigned int
297     DALI_TEST_EQUALS( bufferDataCalls[0], 6*sizeof( unsigned short ), TEST_LOCATION );
298   }
299
300
301   END_TEST;
302 }
303
304 int UtcDaliGeometrySetGetGeometryType01(void)
305 {
306   TestApplication application;
307
308   tet_infoline("Test SetGeometryType and GetGeometryType: without index buffer");
309
310   unsigned int numVertex = 4u;
311   PropertyBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord" );
312
313   Geometry geometry = Geometry::New();
314   geometry.AddVertexBuffer( vertexBuffer );
315
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);
322
323   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
324   TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
325
326   /****************************************************/
327   // Default (TRIANGLES), no index buffer
328   drawTrace.Reset();
329   drawTrace.Enable(true);
330   application.SendNotification();
331   application.Render(0);
332   application.Render();
333   application.SendNotification();
334   drawTrace.Enable( false );
335
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);
342
343   DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::TRIANGLES, TEST_LOCATION);
344
345   /*********************************************************/
346   // LINES, no index buffer
347   geometry.SetGeometryType( Geometry::LINES );
348
349   drawTrace.Reset();
350   drawTrace.Enable(true);
351   application.SendNotification();
352   application.Render(0);
353   application.Render();
354   application.SendNotification();
355   drawTrace.Enable( false );
356
357   // geometry type is set as GL_LINES
358   // no index buffer, call glDrawArrays,
359   DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
360   out.str("");
361   out << GL_LINES << ", " << 0 << ", " << numVertex;
362   DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
363
364   DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::LINES, TEST_LOCATION);
365
366   /*****************************************************/
367   //POINTS
368   geometry.SetGeometryType( Geometry::POINTS );
369
370   drawTrace.Reset();
371   drawTrace.Enable(true);
372   application.SendNotification();
373   application.Render(0);
374   application.Render();
375   application.SendNotification();
376   drawTrace.Enable( false );
377
378   // geometry type is set as GL_POINTS
379   // no index buffer, call glDrawArrays,
380   DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
381   out.str("");
382   out << GL_POINTS << ", " << 0 << ", " << numVertex;
383   DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
384
385   DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::POINTS, TEST_LOCATION);
386
387   /*****************************************************/
388   //TRIANGLE_STRIP, no index buffer
389   geometry.SetGeometryType( Geometry::TRIANGLE_STRIP );
390
391   drawTrace.Reset();
392   drawTrace.Enable(true);
393   application.SendNotification();
394   application.Render(0);
395   application.Render();
396   application.SendNotification();
397   drawTrace.Enable( false );
398
399   // geometry type is set as GL_POINTS
400   // no index buffer, call glDrawArrays,
401   DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
402   out.str("");
403   out << GL_TRIANGLE_STRIP << ", " << 0 << ", " << numVertex;
404   DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
405
406   DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::TRIANGLE_STRIP, TEST_LOCATION);
407
408   /*****************************************************/
409   //TRIANGLE_FAN, no index buffer
410   geometry.SetGeometryType( Geometry::TRIANGLE_FAN );
411
412   drawTrace.Reset();
413   drawTrace.Enable(true);
414   application.SendNotification();
415   application.Render(0);
416   application.Render();
417   application.SendNotification();
418   drawTrace.Enable( false );
419
420   // geometry type is set as GL_POINTS
421   // no index buffer, call glDrawArrays,
422   DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
423   out.str("");
424   out << GL_TRIANGLE_FAN << ", " << 0 << ", " << numVertex;
425   DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
426
427   DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::TRIANGLE_FAN, TEST_LOCATION);
428
429   END_TEST;
430 }
431
432 int UtcDaliGeometrySetGetGeometryType02(void)
433 {
434   TestApplication application;
435
436   tet_infoline("Test SetGeometryType and GetGeometryType: with index buffer");
437
438   unsigned int numVertex = 4u;
439   unsigned int numIndex = 6u; // 6 unsigned short
440   PropertyBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord" );
441
442
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]) );
447
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);
454
455   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
456   TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
457
458   /****************************************************/
459   // Default (TRIANGLES), no index buffer
460   drawTrace.Reset();
461   drawTrace.Enable(true);
462   application.SendNotification();
463   application.Render(0);
464   application.Render();
465   application.SendNotification();
466   drawTrace.Enable( false );
467
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);
473
474   DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::TRIANGLES, TEST_LOCATION);
475
476   /*********************************************************/
477   // LINES, no index buffer
478   geometry.SetGeometryType( Geometry::LINES );
479
480   drawTrace.Reset();
481   drawTrace.Enable(true);
482   application.SendNotification();
483   application.Render(0);
484   application.Render();
485   application.SendNotification();
486   drawTrace.Enable( false );
487
488   // geometry type is set as GL_LINES
489   DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawElements" ), 2, TEST_LOCATION);
490   out.str("");
491   out << GL_LINES << ", " << numIndex << ", " << GL_UNSIGNED_SHORT<<", "<<"indices";
492   DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
493
494   DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::LINES, TEST_LOCATION);
495
496   /*****************************************************/
497   //POINTS
498   geometry.SetGeometryType( Geometry::POINTS );
499
500   drawTrace.Reset();
501   drawTrace.Enable(true);
502   application.SendNotification();
503   application.Render(0);
504   application.Render();
505   application.SendNotification();
506   drawTrace.Enable( false );
507
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);
511   out.str("");
512   out << GL_POINTS << ", " << 0 << ", " << numVertex;
513   DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
514
515   DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::POINTS, TEST_LOCATION);
516
517   /*****************************************************/
518   //TRIANGLE_STRIP
519   geometry.SetGeometryType( Geometry::TRIANGLE_STRIP );
520
521   drawTrace.Reset();
522   drawTrace.Enable(true);
523   application.SendNotification();
524   application.Render(0);
525   application.Render();
526   application.SendNotification();
527   drawTrace.Enable( false );
528
529   // geometry type is set as GL_POINTS
530   DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawElements" ), 2, TEST_LOCATION);
531   out.str("");
532   out << GL_TRIANGLE_STRIP << ", " << numIndex << ", " << GL_UNSIGNED_SHORT<<", "<<"indices";
533   DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
534
535   DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::TRIANGLE_STRIP, TEST_LOCATION);
536
537   /*****************************************************/
538   //TRIANGLE_FAN
539   geometry.SetGeometryType( Geometry::TRIANGLE_FAN );
540
541   drawTrace.Reset();
542   drawTrace.Enable(true);
543   application.SendNotification();
544   application.Render(0);
545   application.Render();
546   application.SendNotification();
547   drawTrace.Enable( false );
548
549   // geometry type is set as GL_POINTS
550   DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawElements" ), 2, TEST_LOCATION);
551   out.str("");
552   out << GL_TRIANGLE_FAN << ", " << numIndex << ", " << GL_UNSIGNED_SHORT<<", "<<"indices";
553   DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
554
555   DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::TRIANGLE_FAN, TEST_LOCATION);
556
557   END_TEST;
558 }
559
560 int UtcDaliGeometrySetGetRequireDepthTesting(void)
561 {
562   TestApplication application;
563
564   tet_infoline("Test SetRequiresDepthTesting, GetRequiresDepthTesting");
565
566   Shader shader = Shader::New("VertexSource", "FragmentSource");
567   Geometry geometry = CreateQuadGeometry();
568   Renderer renderer = Renderer::New( geometry, shader );
569
570   Actor actor = Actor::New();
571   actor.AddRenderer(renderer);
572   actor.SetSize(400, 400);
573   Stage::GetCurrent().Add(actor);
574
575   DALI_TEST_EQUALS( geometry.GetRequiresDepthTesting(), false, TEST_LOCATION );
576
577   geometry.SetRequiresDepthTesting(true);
578
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() ) );
588
589   DALI_TEST_EQUALS( geometry.GetRequiresDepthTesting(), true, TEST_LOCATION );
590
591   END_TEST;
592 }
593