3f4f84b87d5efc1d0dc30c159ca84b7334e103a9
[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
92 int UtcDaliPropertyBufferConstraint01(void)
93 {
94   TestApplication application;
95
96   tet_infoline("Test that a non-uniform propertyBuffer property can be constrained");
97
98   Shader shader = Shader::New("VertexSource", "FragmentSource");
99   Material material = Material::New( shader );
100   material.SetProperty(Material::Property::COLOR, Color::WHITE);
101
102   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
103   Geometry geometry = CreateQuadGeometryFromBuffer(propertyBuffer);
104   Renderer renderer = Renderer::New( geometry, material );
105
106   Actor actor = Actor::New();
107   actor.AddRenderer(renderer);
108   actor.SetSize(400, 400);
109   Stage::GetCurrent().Add(actor);
110
111   Vector4 initialColor = Color::WHITE;
112   Property::Index colorIndex = propertyBuffer.RegisterProperty( "fade-color", initialColor );
113
114   application.SendNotification();
115   application.Render(0);
116   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
117
118   // Apply constraint
119   Constraint constraint = Constraint::New<Vector4>( propertyBuffer, colorIndex, TestConstraintNoBlue );
120   constraint.Apply();
121   application.SendNotification();
122   application.Render(0);
123
124   // Expect no blue component in either buffer - yellow
125   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
126   application.Render(0);
127   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
128
129   propertyBuffer.RemoveConstraints();
130   propertyBuffer.SetProperty(colorIndex, Color::WHITE );
131   application.SendNotification();
132   application.Render(0);
133   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), Color::WHITE, TEST_LOCATION );
134
135   END_TEST;
136 }
137
138 int UtcDaliPropertyBufferConstraint02(void)
139 {
140   TestApplication application;
141
142   tet_infoline("Test that a uniform map propertyBuffer property can be constrained");
143
144   Shader shader = Shader::New( "VertexSource", "FragmentSource" );
145   Material material = Material::New( shader );
146   material.SetProperty(Material::Property::COLOR, Color::WHITE);
147
148   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
149   Geometry geometry = CreateQuadGeometryFromBuffer(propertyBuffer);
150   Renderer renderer = Renderer::New( geometry, material );
151
152   Actor actor = Actor::New();
153   actor.AddRenderer(renderer);
154   actor.SetSize(400, 400);
155   Stage::GetCurrent().Add(actor);
156   application.SendNotification();
157   application.Render(0);
158
159   Vector4 initialColor = Color::WHITE;
160   Property::Index colorIndex = propertyBuffer.RegisterProperty( "fade-color", initialColor );
161   propertyBuffer.AddUniformMapping( colorIndex, std::string("uFadeColor") );
162
163   TestGlAbstraction& gl = application.GetGlAbstraction();
164
165   application.SendNotification();
166   application.Render(0);
167
168   Vector4 actualValue(Vector4::ZERO);
169   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
170   DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
171
172   // Apply constraint
173   Constraint constraint = Constraint::New<Vector4>( propertyBuffer, colorIndex, TestConstraintNoBlue );
174   constraint.Apply();
175   application.SendNotification();
176   application.Render(0);
177
178    // Expect no blue component in either buffer - yellow
179   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
180   DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
181
182   application.Render(0);
183   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
184   DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
185
186   propertyBuffer.RemoveConstraints();
187   propertyBuffer.SetProperty(colorIndex, Color::WHITE );
188   application.SendNotification();
189   application.Render(0);
190
191   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
192   DALI_TEST_EQUALS( actualValue, Color::WHITE, TEST_LOCATION );
193
194   END_TEST;
195 }
196
197
198
199 int UtcDaliPropertyBufferAnimatedProperty01(void)
200 {
201   TestApplication application;
202
203   tet_infoline("Test that a non-uniform propertyBuffer property can be animated");
204
205   Shader shader = Shader::New("VertexSource", "FragmentSource");
206   Material material = Material::New( shader );
207   material.SetProperty(Material::Property::COLOR, Color::WHITE);
208
209   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
210   Geometry geometry = CreateQuadGeometryFromBuffer(propertyBuffer);
211   Renderer renderer = Renderer::New( geometry, material );
212
213   Actor actor = Actor::New();
214   actor.AddRenderer(renderer);
215   actor.SetSize(400, 400);
216   Stage::GetCurrent().Add(actor);
217
218   Vector4 initialColor = Color::WHITE;
219   Property::Index colorIndex = propertyBuffer.RegisterProperty( "fade-color", initialColor );
220
221   application.SendNotification();
222   application.Render(0);
223   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
224
225   Animation  animation = Animation::New(1.0f);
226   KeyFrames keyFrames = KeyFrames::New();
227   keyFrames.Add(0.0f, initialColor);
228   keyFrames.Add(1.0f, Color::TRANSPARENT);
229   animation.AnimateBetween( Property( propertyBuffer, colorIndex ), keyFrames );
230   animation.Play();
231
232   application.SendNotification();
233   application.Render(500);
234
235   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), Color::WHITE * 0.5f, TEST_LOCATION );
236
237   application.Render(500);
238
239   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), Color::TRANSPARENT, TEST_LOCATION );
240
241   END_TEST;
242 }
243
244 int UtcDaliPropertyBufferAnimatedProperty02(void)
245 {
246   TestApplication application;
247
248   tet_infoline("Test that a uniform map propertyBuffer property can be animated");
249
250   Shader shader = Shader::New("VertexSource", "FragmentSource");
251   Material material = Material::New( shader );
252   material.SetProperty(Material::Property::COLOR, Color::WHITE);
253
254   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
255   Geometry geometry = CreateQuadGeometryFromBuffer(propertyBuffer);
256   Renderer renderer = Renderer::New( geometry, material );
257
258   Actor actor = Actor::New();
259   actor.AddRenderer(renderer);
260   actor.SetSize(400, 400);
261   Stage::GetCurrent().Add(actor);
262   application.SendNotification();
263   application.Render(0);
264
265   Vector4 initialColor = Color::WHITE;
266   Property::Index colorIndex = propertyBuffer.RegisterProperty( "fade-color", initialColor );
267   propertyBuffer.AddUniformMapping( colorIndex, std::string("uFadeColor") );
268
269   TestGlAbstraction& gl = application.GetGlAbstraction();
270
271   application.SendNotification();
272   application.Render(0);
273
274   Vector4 actualValue(Vector4::ZERO);
275   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
276   DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
277
278   Animation  animation = Animation::New(1.0f);
279   KeyFrames keyFrames = KeyFrames::New();
280   keyFrames.Add(0.0f, initialColor);
281   keyFrames.Add(1.0f, Color::TRANSPARENT);
282   animation.AnimateBetween( Property( propertyBuffer, colorIndex ), keyFrames );
283   animation.Play();
284
285   application.SendNotification();
286   application.Render(500);
287
288   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
289   DALI_TEST_EQUALS( actualValue, Color::WHITE * 0.5f, TEST_LOCATION );
290
291   application.Render(500);
292   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
293   DALI_TEST_EQUALS( actualValue, Color::TRANSPARENT, TEST_LOCATION );
294
295   END_TEST;
296 }
297
298 int UtcDaliPropertyBufferSetData01(void)
299 {
300   TestApplication application;
301
302   Property::Map texturedQuadVertexFormat;
303   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
304   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
305
306   PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat, 4 );
307   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
308
309   const float halfQuadSize = .5f;
310   struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
311   TexturedQuadVertex texturedQuadVertexData[4] = {
312     { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
313     { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
314     { Vector2(-halfQuadSize,  halfQuadSize), Vector2(0.f, 1.f) },
315     { Vector2( halfQuadSize,  halfQuadSize), Vector2(1.f, 1.f) } };
316
317   propertyBuffer.SetData( texturedQuadVertexData );
318
319   Geometry geometry = Geometry::New();
320   geometry.AddVertexBuffer( propertyBuffer );
321
322   Material material = CreateMaterial(1.f);
323   Renderer renderer = Renderer::New(geometry, material);
324   Actor actor = Actor::New();
325   actor.SetSize(Vector3::ONE * 100.f);
326   actor.AddRenderer(renderer);
327   Stage::GetCurrent().Add(actor);
328
329   application.SendNotification();
330   application.Render(0);
331   application.Render();
332   application.SendNotification();
333
334   const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
335       application.GetGlAbstraction().GetBufferDataCalls();
336
337   DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
338
339   DALI_TEST_EQUALS( bufferDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
340
341   END_TEST;
342 }
343
344 int UtcDaliPropertyBufferSetData02(void)
345 {
346   TestApplication application;
347
348   Property::Map texturedQuadVertexFormat;
349   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
350   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
351
352   PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat, 4 );
353   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
354
355   const float halfQuadSize = .5f;
356   struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
357   TexturedQuadVertex texturedQuadVertexData[4] = {
358     { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
359     { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
360     { Vector2(-halfQuadSize,  halfQuadSize), Vector2(0.f, 1.f) },
361     { Vector2( halfQuadSize,  halfQuadSize), Vector2(1.f, 1.f) } };
362
363   propertyBuffer.SetData( texturedQuadVertexData );
364
365   Geometry geometry = Geometry::New();
366   geometry.AddVertexBuffer( propertyBuffer );
367
368   Material material = CreateMaterial(1.f);
369   Renderer renderer = Renderer::New(geometry, material);
370   Actor actor = Actor::New();
371   actor.SetSize(Vector3::ONE * 100.f);
372   actor.AddRenderer(renderer);
373   Stage::GetCurrent().Add(actor);
374
375   application.SendNotification();
376   application.Render(0);
377   application.Render();
378   application.SendNotification();
379
380   {
381     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
382       application.GetGlAbstraction().GetBufferDataCalls();
383
384     DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
385
386     DALI_TEST_EQUALS( bufferDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
387   }
388
389   // Re-upload the data on the propertyBuffer
390   propertyBuffer.SetData( texturedQuadVertexData );
391
392   application.SendNotification();
393   application.Render(0);
394   application.Render();
395   application.SendNotification();
396
397   {
398     const TestGlAbstraction::BufferSubDataCalls& bufferSubDataCalls =
399       application.GetGlAbstraction().GetBufferSubDataCalls();
400
401     DALI_TEST_EQUALS( bufferSubDataCalls.size(), 1u, TEST_LOCATION );
402
403     if ( bufferSubDataCalls.size() )
404     {
405       DALI_TEST_EQUALS( bufferSubDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
406     }
407   }
408
409   END_TEST;
410 }
411
412 int UtcDaliPropertyBufferSetSize01(void)
413 {
414   TestApplication application;
415
416   Property::Map texturedQuadVertexFormat;
417   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
418   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
419
420   PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat, 4u );
421   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
422
423   size_t size = propertyBuffer.GetSize();
424   DALI_TEST_EQUALS( size, 4u, TEST_LOCATION );
425
426   propertyBuffer.SetSize( 10u );
427   size = propertyBuffer.GetSize();
428   DALI_TEST_EQUALS( size, 10u, TEST_LOCATION );
429
430   END_TEST;
431 }
432