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