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