Revert "Move new mesh API to devel-api"
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-PropertyBuffer.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
21 using namespace Dali;
22
23 #include <mesh-builder.h>
24
25 namespace
26 {
27 void TestConstraintNoBlue( Vector4& current, const PropertyInputContainer& inputs )
28 {
29   current.b = 0.0f;
30 }
31 }
32
33 void propertyBuffer_test_startup(void)
34 {
35   test_return_value = TET_UNDEF;
36 }
37
38 void propertyBuffer_test_cleanup(void)
39 {
40   test_return_value = TET_PASS;
41 }
42
43 int UtcDaliPropertyBufferNew01(void)
44 {
45   TestApplication application;
46
47   Property::Map texturedQuadVertexFormat;
48   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
49   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
50
51   PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat, 4 );
52
53   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
54   END_TEST;
55 }
56
57 int UtcDaliPropertyBufferNew02(void)
58 {
59   TestApplication application;
60   PropertyBuffer propertyBuffer;
61   DALI_TEST_EQUALS( (bool)propertyBuffer, false, TEST_LOCATION );
62   END_TEST;
63 }
64
65 int UtcDaliPropertyBufferDownCast01(void)
66 {
67   TestApplication application;
68
69   Property::Map texturedQuadVertexFormat;
70   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
71   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
72
73   PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat, 4 );
74
75   BaseHandle handle(propertyBuffer);
76   PropertyBuffer propertyBuffer2 = PropertyBuffer::DownCast(handle);
77   DALI_TEST_EQUALS( (bool)propertyBuffer2, true, TEST_LOCATION );
78   END_TEST;
79 }
80
81 int UtcDaliPropertyBufferDownCast02(void)
82 {
83   TestApplication application;
84
85   Handle handle = Handle::New(); // Create a custom object
86   PropertyBuffer propertyBuffer = PropertyBuffer::DownCast(handle);
87   DALI_TEST_EQUALS( (bool)propertyBuffer, false, TEST_LOCATION );
88   END_TEST;
89 }
90
91 int UtcDaliPropertyBufferCopyConstructor(void)
92 {
93   TestApplication application;
94
95   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
96
97   PropertyBuffer propertyBufferCopy(propertyBuffer);
98
99   DALI_TEST_EQUALS( (bool)propertyBufferCopy, true, TEST_LOCATION );
100   DALI_TEST_EQUALS( propertyBufferCopy.GetSize(), 4u, TEST_LOCATION );
101
102   END_TEST;
103 }
104
105 int UtcDaliPropertyBufferAssignmentOperator(void)
106 {
107   TestApplication application;
108
109   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
110
111   PropertyBuffer propertyBuffer2;
112   DALI_TEST_EQUALS( (bool)propertyBuffer2, false, TEST_LOCATION );
113
114   propertyBuffer2 = propertyBuffer;
115   DALI_TEST_EQUALS( (bool)propertyBuffer2, true, TEST_LOCATION );
116   DALI_TEST_EQUALS( propertyBuffer2.GetSize(), 4u, TEST_LOCATION );
117
118   END_TEST;
119 }
120
121 int UtcDaliPropertyBufferConstraint01(void)
122 {
123   TestApplication application;
124
125   tet_infoline("Test that a non-uniform propertyBuffer property can be constrained");
126
127   Shader shader = Shader::New("VertexSource", "FragmentSource");
128   Material material = Material::New( shader );
129   material.SetProperty(Material::Property::COLOR, Color::WHITE);
130
131   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
132   Geometry geometry = CreateQuadGeometryFromBuffer(propertyBuffer);
133   Renderer renderer = Renderer::New( geometry, material );
134
135   Actor actor = Actor::New();
136   actor.AddRenderer(renderer);
137   actor.SetSize(400, 400);
138   Stage::GetCurrent().Add(actor);
139
140   Vector4 initialColor = Color::WHITE;
141   Property::Index colorIndex = propertyBuffer.RegisterProperty( "uFadeColor", initialColor );
142
143   application.SendNotification();
144   application.Render(0);
145   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
146
147   // Apply constraint
148   Constraint constraint = Constraint::New<Vector4>( propertyBuffer, colorIndex, TestConstraintNoBlue );
149   constraint.Apply();
150   application.SendNotification();
151   application.Render(0);
152
153   // Expect no blue component in either buffer - yellow
154   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
155   application.Render(0);
156   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
157
158   propertyBuffer.RemoveConstraints();
159   propertyBuffer.SetProperty(colorIndex, Color::WHITE );
160   application.SendNotification();
161   application.Render(0);
162   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), Color::WHITE, TEST_LOCATION );
163
164   END_TEST;
165 }
166
167 int UtcDaliPropertyBufferConstraint02(void)
168 {
169   TestApplication application;
170
171   tet_infoline("Test that a uniform map propertyBuffer property can be constrained");
172
173   Shader shader = Shader::New( "VertexSource", "FragmentSource" );
174   Material material = Material::New( shader );
175   material.SetProperty(Material::Property::COLOR, Color::WHITE);
176
177   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
178   Geometry geometry = CreateQuadGeometryFromBuffer(propertyBuffer);
179   Renderer renderer = Renderer::New( geometry, material );
180
181   Actor actor = Actor::New();
182   actor.AddRenderer(renderer);
183   actor.SetSize(400, 400);
184   Stage::GetCurrent().Add(actor);
185   application.SendNotification();
186   application.Render(0);
187
188   Vector4 initialColor = Color::WHITE;
189   Property::Index colorIndex = propertyBuffer.RegisterProperty( "uFadeColor", initialColor );
190
191   TestGlAbstraction& gl = application.GetGlAbstraction();
192
193   application.SendNotification();
194   application.Render(0);
195
196   Vector4 actualValue(Vector4::ZERO);
197   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
198   DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
199
200   // Apply constraint
201   Constraint constraint = Constraint::New<Vector4>( propertyBuffer, colorIndex, TestConstraintNoBlue );
202   constraint.Apply();
203   application.SendNotification();
204   application.Render(0);
205
206    // Expect no blue component in either buffer - yellow
207   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
208   DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
209
210   application.Render(0);
211   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
212   DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
213
214   propertyBuffer.RemoveConstraints();
215   propertyBuffer.SetProperty(colorIndex, Color::WHITE );
216   application.SendNotification();
217   application.Render(0);
218
219   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
220   DALI_TEST_EQUALS( actualValue, Color::WHITE, TEST_LOCATION );
221
222   END_TEST;
223 }
224
225
226
227 int UtcDaliPropertyBufferAnimatedProperty01(void)
228 {
229   TestApplication application;
230
231   tet_infoline("Test that a non-uniform propertyBuffer property can be animated");
232
233   Shader shader = Shader::New("VertexSource", "FragmentSource");
234   Material material = Material::New( shader );
235   material.SetProperty(Material::Property::COLOR, Color::WHITE);
236
237   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
238   Geometry geometry = CreateQuadGeometryFromBuffer(propertyBuffer);
239   Renderer renderer = Renderer::New( geometry, material );
240
241   Actor actor = Actor::New();
242   actor.AddRenderer(renderer);
243   actor.SetSize(400, 400);
244   Stage::GetCurrent().Add(actor);
245
246   Vector4 initialColor = Color::WHITE;
247   Property::Index colorIndex = propertyBuffer.RegisterProperty( "uFadeColor", initialColor );
248
249   application.SendNotification();
250   application.Render(0);
251   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
252
253   Animation  animation = Animation::New(1.0f);
254   KeyFrames keyFrames = KeyFrames::New();
255   keyFrames.Add(0.0f, initialColor);
256   keyFrames.Add(1.0f, Color::TRANSPARENT);
257   animation.AnimateBetween( Property( propertyBuffer, colorIndex ), keyFrames );
258   animation.Play();
259
260   application.SendNotification();
261   application.Render(500);
262
263   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), Color::WHITE * 0.5f, TEST_LOCATION );
264
265   application.Render(500);
266
267   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), Color::TRANSPARENT, TEST_LOCATION );
268
269   END_TEST;
270 }
271
272 int UtcDaliPropertyBufferAnimatedProperty02(void)
273 {
274   TestApplication application;
275
276   tet_infoline("Test that a uniform map propertyBuffer property can be animated");
277
278   Shader shader = Shader::New("VertexSource", "FragmentSource");
279   Material material = Material::New( shader );
280   material.SetProperty(Material::Property::COLOR, Color::WHITE);
281
282   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
283   Geometry geometry = CreateQuadGeometryFromBuffer(propertyBuffer);
284   Renderer renderer = Renderer::New( geometry, material );
285
286   Actor actor = Actor::New();
287   actor.AddRenderer(renderer);
288   actor.SetSize(400, 400);
289   Stage::GetCurrent().Add(actor);
290   application.SendNotification();
291   application.Render(0);
292
293   Vector4 initialColor = Color::WHITE;
294   Property::Index colorIndex = propertyBuffer.RegisterProperty( "uFadeColor", initialColor );
295
296   TestGlAbstraction& gl = application.GetGlAbstraction();
297
298   application.SendNotification();
299   application.Render(0);
300
301   Vector4 actualValue(Vector4::ZERO);
302   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
303   DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
304
305   Animation  animation = Animation::New(1.0f);
306   KeyFrames keyFrames = KeyFrames::New();
307   keyFrames.Add(0.0f, initialColor);
308   keyFrames.Add(1.0f, Color::TRANSPARENT);
309   animation.AnimateBetween( Property( propertyBuffer, colorIndex ), keyFrames );
310   animation.Play();
311
312   application.SendNotification();
313   application.Render(500);
314
315   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
316   DALI_TEST_EQUALS( actualValue, Color::WHITE * 0.5f, TEST_LOCATION );
317
318   application.Render(500);
319   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
320   DALI_TEST_EQUALS( actualValue, Color::TRANSPARENT, TEST_LOCATION );
321
322   END_TEST;
323 }
324
325 int UtcDaliPropertyBufferSetData01(void)
326 {
327   TestApplication application;
328
329   Property::Map texturedQuadVertexFormat;
330   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
331   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
332
333   PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat, 4 );
334   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
335
336   const float halfQuadSize = .5f;
337   struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
338   TexturedQuadVertex texturedQuadVertexData[4] = {
339     { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
340     { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
341     { Vector2(-halfQuadSize,  halfQuadSize), Vector2(0.f, 1.f) },
342     { Vector2( halfQuadSize,  halfQuadSize), Vector2(1.f, 1.f) } };
343
344   propertyBuffer.SetData( texturedQuadVertexData );
345
346   Geometry geometry = Geometry::New();
347   geometry.AddVertexBuffer( propertyBuffer );
348
349   Material material = CreateMaterial(1.f);
350   Renderer renderer = Renderer::New(geometry, material);
351   Actor actor = Actor::New();
352   actor.SetSize(Vector3::ONE * 100.f);
353   actor.AddRenderer(renderer);
354   Stage::GetCurrent().Add(actor);
355
356   application.SendNotification();
357   application.Render(0);
358   application.Render();
359   application.SendNotification();
360
361   const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
362       application.GetGlAbstraction().GetBufferDataCalls();
363
364   DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
365
366   DALI_TEST_EQUALS( bufferDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
367
368   END_TEST;
369 }
370
371 int UtcDaliPropertyBufferSetData02(void)
372 {
373   TestApplication application;
374
375   Property::Map texturedQuadVertexFormat;
376   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
377   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
378
379   PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat, 4 );
380   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
381
382   const float halfQuadSize = .5f;
383   struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
384   TexturedQuadVertex texturedQuadVertexData[4] = {
385     { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
386     { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
387     { Vector2(-halfQuadSize,  halfQuadSize), Vector2(0.f, 1.f) },
388     { Vector2( halfQuadSize,  halfQuadSize), Vector2(1.f, 1.f) } };
389
390   propertyBuffer.SetData( texturedQuadVertexData );
391
392   Geometry geometry = Geometry::New();
393   geometry.AddVertexBuffer( propertyBuffer );
394
395   Material material = CreateMaterial(1.f);
396   Renderer renderer = Renderer::New(geometry, material);
397   Actor actor = Actor::New();
398   actor.SetSize(Vector3::ONE * 100.f);
399   actor.AddRenderer(renderer);
400   Stage::GetCurrent().Add(actor);
401
402   application.SendNotification();
403   application.Render(0);
404   application.Render();
405   application.SendNotification();
406
407   {
408     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
409       application.GetGlAbstraction().GetBufferDataCalls();
410
411     DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
412
413     DALI_TEST_EQUALS( bufferDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
414   }
415
416   // Re-upload the data on the propertyBuffer
417   propertyBuffer.SetData( texturedQuadVertexData );
418
419   application.SendNotification();
420   application.Render(0);
421   application.Render();
422   application.SendNotification();
423
424   {
425     const TestGlAbstraction::BufferSubDataCalls& bufferSubDataCalls =
426       application.GetGlAbstraction().GetBufferSubDataCalls();
427
428     DALI_TEST_EQUALS( bufferSubDataCalls.size(), 1u, TEST_LOCATION );
429
430     if ( bufferSubDataCalls.size() )
431     {
432       DALI_TEST_EQUALS( bufferSubDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
433     }
434   }
435
436   END_TEST;
437 }
438
439 int UtcDaliPropertyBufferSetGetSize01(void)
440 {
441   TestApplication application;
442
443   Property::Map texturedQuadVertexFormat;
444   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
445   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
446
447   PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat, 4u );
448   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
449
450   size_t size = propertyBuffer.GetSize();
451   DALI_TEST_EQUALS( size, 4u, TEST_LOCATION );
452
453   propertyBuffer.SetSize( 10u );
454   size = propertyBuffer.GetSize();
455   DALI_TEST_EQUALS( size, 10u, TEST_LOCATION );
456
457   END_TEST;
458 }
459
460 //Todo: also test that the SetSize function is equivalent to setting the property SIZE
461 int UtcDaliPropertyBufferSetGetSize02(void)
462 {
463   TestApplication application;
464
465   Property::Map texturedQuadVertexFormat;
466   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
467   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
468
469   unsigned int size = 5u;
470   PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat, size );
471   DALI_TEST_EQUALS( propertyBuffer.GetProperty<unsigned int>(PropertyBuffer::Property::SIZE), size, TEST_LOCATION );
472   DALI_TEST_EQUALS( propertyBuffer.GetSize(), size, TEST_LOCATION );
473
474   size += 3u;
475   propertyBuffer.SetSize( size );
476   DALI_TEST_EQUALS( propertyBuffer.GetProperty<unsigned int>(PropertyBuffer::Property::SIZE), size, TEST_LOCATION );
477   DALI_TEST_EQUALS( propertyBuffer.GetSize(), size, TEST_LOCATION );
478
479   size += 2u;
480   propertyBuffer.SetProperty(PropertyBuffer::Property::SIZE, size );
481   DALI_TEST_EQUALS( propertyBuffer.GetSize(), size, TEST_LOCATION );
482
483   END_TEST;
484 }
485