Remove RenderSurface from Core
[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 struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
39
40 PropertyBuffer CreateVertexBuffer( const std::string& aPosition, const std::string& aTexCoord )
41 {
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) } };
48
49   Property::Map vertexFormat;
50   vertexFormat[aPosition] = Property::VECTOR2;
51   vertexFormat[aTexCoord] = Property::VECTOR2;
52
53   PropertyBuffer vertexData = PropertyBuffer::New( vertexFormat );
54   vertexData.SetData( texturedQuadVertexData, 4 );
55
56   return vertexData;
57 }
58
59
60 }
61
62
63 int UtcDaliGeometryNew01(void)
64 {
65   TestApplication application;
66
67   Geometry geometry = Geometry::New();
68
69   DALI_TEST_EQUALS( (bool)geometry, true, TEST_LOCATION );
70   END_TEST;
71 }
72
73 int UtcDaliGeometryNew02(void)
74 {
75   TestApplication application;
76   Geometry geometry;
77   DALI_TEST_EQUALS( (bool)geometry, false, TEST_LOCATION );
78   END_TEST;
79 }
80
81 int UtcDaliGeometryCopyConstructor(void)
82 {
83   TestApplication application;
84
85   Geometry geometry = Geometry::New();
86
87   Geometry geometryCopy(geometry);
88
89   DALI_TEST_EQUALS( (bool)geometryCopy, true, TEST_LOCATION );
90   END_TEST;
91 }
92
93 int UtcDaliGeometryAssignmentOperator(void)
94 {
95   TestApplication application;
96
97   Geometry geometry = Geometry::New();
98
99   Geometry geometry2;
100   DALI_TEST_EQUALS( (bool)geometry2, false, TEST_LOCATION );
101
102   geometry2 = geometry;
103   DALI_TEST_EQUALS( (bool)geometry2, true, TEST_LOCATION );
104
105   END_TEST;
106 }
107
108 int UtcDaliGeometryDownCast01(void)
109 {
110   TestApplication application;
111
112   Geometry geometry = Geometry::New();
113
114   BaseHandle handle(geometry);
115   Geometry geometry2 = Geometry::DownCast(handle);
116   DALI_TEST_EQUALS( (bool)geometry2, true, TEST_LOCATION );
117   END_TEST;
118 }
119
120 int UtcDaliGeometryDownCast02(void)
121 {
122   TestApplication application;
123
124   Handle handle = Handle::New(); // Create a custom object
125   Geometry geometry = Geometry::DownCast(handle);
126   DALI_TEST_EQUALS( (bool)geometry, false, TEST_LOCATION );
127   END_TEST;
128 }
129
130 int UtcDaliGeometryAddVertexBuffer(void)
131 {
132   TestApplication application;
133
134   tet_infoline("Test AddVertexBuffer");
135
136   PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
137   Geometry geometry = Geometry::New();
138   geometry.AddVertexBuffer( vertexBuffer1 );
139
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);
146
147   application.SendNotification();
148   application.Render(0);
149   application.Render();
150   application.SendNotification();
151
152   {
153     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
154         application.GetGlAbstraction().GetBufferDataCalls();
155
156     DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
157
158     DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
159   }
160
161   // add the second vertex buffer
162   application.GetGlAbstraction().ResetBufferDataCalls();
163
164   PropertyBuffer vertexBuffer2 = CreateVertexBuffer( "aPosition2", "aTexCoord2" );
165   geometry.AddVertexBuffer( vertexBuffer2 );
166   application.SendNotification();
167   application.Render(0);
168   application.Render();
169   application.SendNotification();
170
171   {
172     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
173         application.GetGlAbstraction().GetBufferDataCalls();
174
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 );
178   }
179
180   END_TEST;
181 }
182
183 int UtcDaliGeometryGetNumberOfVertexBuffers(void)
184 {
185   TestApplication application;
186
187   tet_infoline("Test GetNumberOfVertexBuffers");
188   PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
189   PropertyBuffer vertexBuffer2 = CreateVertexBuffer("aPosition2", "aTexCoord2" );
190   PropertyBuffer vertexBuffer3 = CreateVertexBuffer("aPosition3", "aTexCoord3" );
191
192   Geometry geometry = Geometry::New();
193   geometry.AddVertexBuffer( vertexBuffer1 );
194   DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
195
196   geometry.AddVertexBuffer( vertexBuffer2 );
197   geometry.AddVertexBuffer( vertexBuffer3 );
198   DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 3u, TEST_LOCATION );
199
200   geometry.RemoveVertexBuffer( 2u );
201   DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 2u, TEST_LOCATION );
202
203   END_TEST;
204 }
205
206 int UtcDaliGeometryRemoveVertexBuffer(void)
207 {
208   TestApplication application;
209
210   tet_infoline("Test RemoveVertexBuffer");
211
212   PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
213   PropertyBuffer vertexBuffer2 = CreateVertexBuffer("aPosition2", "aTexCoord2" );
214
215   Geometry geometry = Geometry::New();
216   geometry.AddVertexBuffer( vertexBuffer1 );
217
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);
224
225   DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
226
227   geometry.RemoveVertexBuffer( 0 );
228   geometry.AddVertexBuffer( vertexBuffer2 );
229   DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
230
231   geometry.RemoveVertexBuffer( 0 );
232   DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 0u, TEST_LOCATION );
233
234   //Todo: test by checking the BufferDataCalls
235   // make sure the vertex buffer in actually removed from gl
236
237    END_TEST;
238 }
239
240 int UtcDaliGeometrySetIndexBuffer(void)
241 {
242   TestApplication application;
243
244   tet_infoline("Test SetIndexBuffer");
245
246   PropertyBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord" );
247
248   Geometry geometry = Geometry::New();
249   geometry.AddVertexBuffer( vertexBuffer );
250
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);
257
258   application.SendNotification();
259   application.Render(0);
260   application.Render();
261   application.SendNotification();
262
263   {
264     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
265         application.GetGlAbstraction().GetBufferDataCalls();
266
267     DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
268
269     DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
270   }
271
272   // Set index buffer
273   application.GetGlAbstraction().ResetBufferDataCalls();
274
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();
281
282   {
283     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
284         application.GetGlAbstraction().GetBufferDataCalls();
285
286     //Only the index buffer should be uploaded
287     DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
288
289     // should be unsigned short instead of unsigned int
290     DALI_TEST_EQUALS( bufferDataCalls[0], 6*sizeof( unsigned short ), TEST_LOCATION );
291   }
292
293
294   END_TEST;
295 }
296
297 int UtcDaliGeometrySetGetGeometryType01(void)
298 {
299   TestApplication application;
300
301   tet_infoline("Test SetType and GetType: without index buffer");
302
303   unsigned int numVertex = 4u;
304   PropertyBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord" );
305
306   Geometry geometry = Geometry::New();
307   geometry.AddVertexBuffer( vertexBuffer );
308
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);
315
316   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
317   TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
318
319   /****************************************************/
320   // Default (TRIANGLES), no index buffer
321   drawTrace.Reset();
322   drawTrace.Enable(true);
323   application.SendNotification();
324   application.Render(0);
325   application.Render();
326   application.SendNotification();
327   drawTrace.Enable( false );
328
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);
335
336   DALI_TEST_EQUALS( geometry.GetType(), Geometry::TRIANGLES, TEST_LOCATION);
337
338   /*********************************************************/
339   // LINES, no index buffer
340   geometry.SetType( Geometry::LINES );
341
342   drawTrace.Reset();
343   drawTrace.Enable(true);
344   application.SendNotification();
345   application.Render(0);
346   application.Render();
347   application.SendNotification();
348   drawTrace.Enable( false );
349
350   // geometry type is set as GL_LINES
351   // no index buffer, call glDrawArrays,
352   DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
353   out.str("");
354   out << GL_LINES << ", " << 0 << ", " << numVertex;
355   DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
356
357   DALI_TEST_EQUALS( geometry.GetType(), Geometry::LINES, TEST_LOCATION);
358
359   /*****************************************************/
360   //POINTS
361   geometry.SetType( Geometry::POINTS );
362
363   drawTrace.Reset();
364   drawTrace.Enable(true);
365   application.SendNotification();
366   application.Render(0);
367   application.Render();
368   application.SendNotification();
369   drawTrace.Enable( false );
370
371   // geometry type is set as GL_POINTS
372   // no index buffer, call glDrawArrays,
373   DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
374   out.str("");
375   out << GL_POINTS << ", " << 0 << ", " << numVertex;
376   DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
377
378   DALI_TEST_EQUALS( geometry.GetType(), Geometry::POINTS, TEST_LOCATION);
379
380   /*****************************************************/
381   //TRIANGLE_STRIP, no index buffer
382   geometry.SetType( Geometry::TRIANGLE_STRIP );
383
384   drawTrace.Reset();
385   drawTrace.Enable(true);
386   application.SendNotification();
387   application.Render(0);
388   application.Render();
389   application.SendNotification();
390   drawTrace.Enable( false );
391
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);
395   out.str("");
396   out << GL_TRIANGLE_STRIP << ", " << 0 << ", " << numVertex;
397   DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
398
399   DALI_TEST_EQUALS( geometry.GetType(), Geometry::TRIANGLE_STRIP, TEST_LOCATION);
400
401   /*****************************************************/
402   //TRIANGLE_FAN, no index buffer
403   geometry.SetType( Geometry::TRIANGLE_FAN );
404
405   drawTrace.Reset();
406   drawTrace.Enable(true);
407   application.SendNotification();
408   application.Render(0);
409   application.Render();
410   application.SendNotification();
411   drawTrace.Enable( false );
412
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);
416   out.str("");
417   out << GL_TRIANGLE_FAN << ", " << 0 << ", " << numVertex;
418   DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
419
420   DALI_TEST_EQUALS( geometry.GetType(), Geometry::TRIANGLE_FAN, TEST_LOCATION);
421
422   END_TEST;
423 }
424
425 int UtcDaliGeometrySetGetGeometryType02(void)
426 {
427   TestApplication application;
428
429   tet_infoline("Test SetType and GetType: with index buffer");
430
431   unsigned int numVertex = 4u;
432   unsigned int numIndex = 6u; // 6 unsigned short
433   PropertyBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord" );
434
435
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]) );
440
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);
447
448   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
449   TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
450
451   /****************************************************/
452   // Default (TRIANGLES), with index buffer
453   drawTrace.Reset();
454   drawTrace.Enable(true);
455   application.SendNotification();
456   application.Render(0);
457   application.Render();
458   application.SendNotification();
459   drawTrace.Enable( false );
460
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);
466
467   DALI_TEST_EQUALS( geometry.GetType(), Geometry::TRIANGLES, TEST_LOCATION);
468
469   /*********************************************************/
470   // LINES, with index buffer
471   geometry.SetType( Geometry::LINES );
472
473   drawTrace.Reset();
474   drawTrace.Enable(true);
475   application.SendNotification();
476   application.Render(0);
477   application.Render();
478   application.SendNotification();
479   drawTrace.Enable( false );
480
481   // geometry type is set as GL_LINES
482   DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawElements" ), 2, TEST_LOCATION);
483   out.str("");
484   out << GL_LINES << ", " << numIndex << ", " << GL_UNSIGNED_SHORT<<", "<<"indices";
485   DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
486
487   DALI_TEST_EQUALS( geometry.GetType(), Geometry::LINES, TEST_LOCATION);
488
489   /*****************************************************/
490   //POINTS
491   geometry.SetType( Geometry::POINTS );
492
493   drawTrace.Reset();
494   drawTrace.Enable(true);
495   application.SendNotification();
496   application.Render(0);
497   application.Render();
498   application.SendNotification();
499   drawTrace.Enable( false );
500
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);
504   out.str("");
505   out << GL_POINTS << ", " << 0 << ", " << numVertex;
506   DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
507
508   DALI_TEST_EQUALS( geometry.GetType(), Geometry::POINTS, TEST_LOCATION);
509
510   /*****************************************************/
511   //TRIANGLE_STRIP
512   geometry.SetType( Geometry::TRIANGLE_STRIP );
513
514   drawTrace.Reset();
515   drawTrace.Enable(true);
516   application.SendNotification();
517   application.Render(0);
518   application.Render();
519   application.SendNotification();
520   drawTrace.Enable( false );
521
522   // geometry type is set as GL_TRIANGLE_STRIP
523   DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawElements" ), 2, TEST_LOCATION);
524   out.str("");
525   out << GL_TRIANGLE_STRIP << ", " << numIndex << ", " << GL_UNSIGNED_SHORT<<", "<<"indices";
526   DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
527
528   DALI_TEST_EQUALS( geometry.GetType(), Geometry::TRIANGLE_STRIP, TEST_LOCATION);
529
530   /*****************************************************/
531   //TRIANGLE_FAN
532   geometry.SetType( Geometry::TRIANGLE_FAN );
533
534   drawTrace.Reset();
535   drawTrace.Enable(true);
536   application.SendNotification();
537   application.Render(0);
538   application.Render();
539   application.SendNotification();
540   drawTrace.Enable( false );
541
542   // geometry type is set as GL_TRIANGLE_FAN
543   DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawElements" ), 2, TEST_LOCATION);
544   out.str("");
545   out << GL_TRIANGLE_FAN << ", " << numIndex << ", " << GL_UNSIGNED_SHORT<<", "<<"indices";
546   DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
547
548   DALI_TEST_EQUALS( geometry.GetType(), Geometry::TRIANGLE_FAN, TEST_LOCATION);
549
550   END_TEST;
551 }