868c448c8aac4c24400678a0515329a3b6c9c175
[platform/core/uifw/dali-core.git] / automated-tests / src / dali-devel / utc-Dali-TextureSet.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
34 void texture_set_test_startup(void)
35 {
36   test_return_value = TET_UNDEF;
37 }
38
39 void texture_set_test_cleanup(void)
40 {
41   test_return_value = TET_PASS;
42 }
43
44 int UtcDaliTextureSetNew01(void)
45 {
46   TestApplication application;
47
48   TextureSet textureSet = TextureSet::New();
49
50   DALI_TEST_CHECK( textureSet );
51   END_TEST;
52 }
53
54 int UtcDaliTextureSetNew02(void)
55 {
56   TestApplication application;
57   TextureSet textureSet;
58   DALI_TEST_CHECK( !textureSet );
59   END_TEST;
60 }
61
62 int UtcDaliTextureSetCopyConstructor(void)
63 {
64   TestApplication application;
65
66   Image image = BufferImage::New(32, 32, Pixel::RGBA8888);
67   TextureSet textureSet = TextureSet::New();
68   textureSet.SetImage( 0u, image );
69
70   TextureSet textureSetCopy(textureSet);
71
72   DALI_TEST_CHECK( textureSetCopy );
73
74   END_TEST;
75 }
76
77 int UtcDaliTextureSetAssignmentOperator(void)
78 {
79   TestApplication application;
80   TextureSet textureSet = TextureSet::New();
81
82   TextureSet textureSet2;
83   DALI_TEST_CHECK( !textureSet2 );
84
85   textureSet2 = textureSet;
86   DALI_TEST_CHECK( textureSet2 );
87
88   END_TEST;
89 }
90
91 int UtcDaliTextureSetDownCast01(void)
92 {
93   TestApplication application;
94   TextureSet textureSet = TextureSet::New();
95
96   BaseHandle handle(textureSet);
97   TextureSet textureSet2 = TextureSet::DownCast(handle);
98   DALI_TEST_CHECK( textureSet2 );
99
100   END_TEST;
101 }
102
103 int UtcDaliTextureSetDownCast02(void)
104 {
105   TestApplication application;
106
107   Handle handle = Handle::New(); // Create a custom object
108   TextureSet textureSet = TextureSet::DownCast(handle);
109   DALI_TEST_CHECK( !textureSet );
110   END_TEST;
111 }
112
113 int UtcDaliTextureSetConstraint(void)
114 {
115   TestApplication application;
116
117   tet_infoline("Test that a custom texture set property can be constrained");
118
119   Shader shader = Shader::New( "VertexSource", "FragmentSource");
120   TextureSet textureSet = TextureSet::New();
121
122   Geometry geometry = CreateQuadGeometry();
123   Renderer renderer = Renderer::New( geometry, shader );
124   renderer.SetTextures( textureSet );
125
126   Actor actor = Actor::New();
127   actor.AddRenderer(renderer);
128   actor.SetSize(400, 400);
129   Stage::GetCurrent().Add(actor);
130
131   Vector4 initialColor = Color::WHITE;
132   Property::Index colorIndex = textureSet.RegisterProperty( "uFadeColor", initialColor );
133
134   application.SendNotification();
135   application.Render(0);
136   DALI_TEST_EQUALS( textureSet.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
137
138   // Apply constraint
139   Constraint constraint = Constraint::New<Vector4>( textureSet, colorIndex, TestConstraintNoBlue );
140   constraint.Apply();
141   application.SendNotification();
142   application.Render(0);
143
144   // Expect no blue component in either buffer - yellow
145   DALI_TEST_EQUALS( textureSet.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
146   application.Render(0);
147   DALI_TEST_EQUALS( textureSet.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
148
149   textureSet.RemoveConstraints();
150   textureSet.SetProperty(colorIndex, Color::WHITE );
151   application.SendNotification();
152   application.Render(0);
153   DALI_TEST_EQUALS( textureSet.GetProperty<Vector4>(colorIndex), Color::WHITE, TEST_LOCATION );
154
155   END_TEST;
156 }
157
158 int UtcDaliTextureSetConstraint02(void)
159 {
160   TestApplication application;
161
162   tet_infoline("Test that a uniform map texture set property can be constrained");
163
164   Shader shader = Shader::New( "VertexSource", "FragmentSource");
165   TextureSet textureSet = TextureSet::New();
166
167   Geometry geometry = CreateQuadGeometry();
168   Renderer renderer = Renderer::New( geometry, shader );
169   renderer.SetTextures( textureSet );
170
171   Actor actor = Actor::New();
172   actor.AddRenderer(renderer);
173   actor.SetSize(400, 400);
174   Stage::GetCurrent().Add(actor);
175   application.SendNotification();
176   application.Render(0);
177
178   Vector4 initialColor = Color::WHITE;
179   Property::Index colorIndex = textureSet.RegisterProperty( "uFadeColor", initialColor );
180
181   TestGlAbstraction& gl = application.GetGlAbstraction();
182
183   application.SendNotification();
184   application.Render(0);
185
186   Vector4 actualValue(Vector4::ZERO);
187   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
188   DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
189
190   // Apply constraint
191   Constraint constraint = Constraint::New<Vector4>( textureSet, colorIndex, TestConstraintNoBlue );
192   constraint.Apply();
193   application.SendNotification();
194   application.Render(0);
195
196    // Expect no blue component in either buffer - yellow
197   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
198   DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
199
200   application.Render(0);
201   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
202   DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
203
204   textureSet.RemoveConstraints();
205   textureSet.SetProperty(colorIndex, Color::WHITE );
206   application.SendNotification();
207   application.Render(0);
208
209   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
210   DALI_TEST_EQUALS( actualValue, Color::WHITE, TEST_LOCATION );
211
212   END_TEST;
213 }
214
215 int UtcDaliTextureSetAnimatedProperty01(void)
216 {
217   TestApplication application;
218
219   tet_infoline("Test that a non-uniform texture set property can be animated");
220
221   Shader shader = Shader::New( "VertexSource", "FragmentSource");
222   TextureSet textureSet = TextureSet::New();
223
224   Geometry geometry = CreateQuadGeometry();
225   Renderer renderer = Renderer::New( geometry, shader );
226   renderer.SetTextures( textureSet );
227
228   Actor actor = Actor::New();
229   actor.AddRenderer(renderer);
230   actor.SetSize(400, 400);
231   Stage::GetCurrent().Add(actor);
232
233   Vector4 initialColor = Color::WHITE;
234   Property::Index colorIndex = textureSet.RegisterProperty( "uFadeColor", initialColor );
235
236   application.SendNotification();
237   application.Render(0);
238   DALI_TEST_EQUALS( textureSet.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
239
240   Animation  animation = Animation::New(1.0f);
241   KeyFrames keyFrames = KeyFrames::New();
242   keyFrames.Add(0.0f, initialColor);
243   keyFrames.Add(1.0f, Color::TRANSPARENT);
244   animation.AnimateBetween( Property( textureSet, colorIndex ), keyFrames );
245   animation.Play();
246
247   application.SendNotification();
248   application.Render(500);
249
250   DALI_TEST_EQUALS( textureSet.GetProperty<Vector4>(colorIndex), Color::WHITE * 0.5f, TEST_LOCATION );
251
252   application.Render(500);
253
254   DALI_TEST_EQUALS( textureSet.GetProperty<Vector4>(colorIndex), Color::TRANSPARENT, TEST_LOCATION );
255
256   END_TEST;
257 }
258
259 int UtcDaliTextureSetAnimatedProperty02(void)
260 {
261   TestApplication application;
262
263   tet_infoline("Test that a uniform map texture set property can be animated");
264
265   Shader shader = Shader::New( "VertexSource", "FragmentSource");
266   TextureSet textureSet = TextureSet::New();
267
268   Geometry geometry = CreateQuadGeometry();
269   Renderer renderer = Renderer::New( geometry, shader );
270   renderer.SetTextures( textureSet );
271
272   Actor actor = Actor::New();
273   actor.AddRenderer(renderer);
274   actor.SetSize(400, 400);
275   Stage::GetCurrent().Add(actor);
276   application.SendNotification();
277   application.Render(0);
278
279   Vector4 initialColor = Color::WHITE;
280   Property::Index colorIndex = textureSet.RegisterProperty( "uFadeColor", initialColor );
281
282   TestGlAbstraction& gl = application.GetGlAbstraction();
283
284   application.SendNotification();
285   application.Render(0);
286
287   Vector4 actualValue(Vector4::ZERO);
288   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
289   DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
290
291   Animation  animation = Animation::New(1.0f);
292   KeyFrames keyFrames = KeyFrames::New();
293   keyFrames.Add(0.0f, initialColor);
294   keyFrames.Add(1.0f, Color::TRANSPARENT);
295   animation.AnimateBetween( Property( textureSet, colorIndex ), keyFrames );
296   animation.Play();
297
298   application.SendNotification();
299   application.Render(500);
300
301   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
302   DALI_TEST_EQUALS( actualValue, Color::WHITE * 0.5f, TEST_LOCATION );
303
304   application.Render(500);
305   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
306   DALI_TEST_EQUALS( actualValue, Color::TRANSPARENT, TEST_LOCATION );
307
308   END_TEST;
309 }
310
311 int UtcDaliTextureSetSetImage01(void)
312 {
313   TestApplication application;
314
315   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
316
317   Shader shader = CreateShader();
318   TextureSet textureSet = CreateTextureSet();
319   textureSet.SetImage( 0u, image );
320
321   Geometry geometry = CreateQuadGeometry();
322   Renderer renderer = Renderer::New( geometry, shader );
323   renderer.SetTextures( textureSet );
324
325   Actor actor = Actor::New();
326   actor.AddRenderer(renderer);
327   actor.SetParentOrigin( ParentOrigin::CENTER );
328   actor.SetSize(400, 400);
329
330   Stage::GetCurrent().Add( actor );
331
332   TestGlAbstraction& gl = application.GetGlAbstraction();
333
334   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
335   texParameterTrace.Reset();
336   texParameterTrace.Enable( true );
337   application.SendNotification();
338   application.Render();
339
340   int textureUnit=-1;
341   DALI_TEST_CHECK( gl.GetUniformValue<int>( "sTexture", textureUnit ) );
342   DALI_TEST_EQUALS( textureUnit, 0, TEST_LOCATION );
343
344   texParameterTrace.Enable( false );
345
346   // Verify gl state
347   // There are three calls to TexParameteri when the texture is first created
348   // as the texture is using default sampling parametrers there shouldn't be any more calls to TexParameteri
349   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 3, TEST_LOCATION);
350
351   END_TEST;
352 }
353
354 int UtcDaliTextureSetSetImage02(void)
355 {
356   TestApplication application;
357
358   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
359
360   Shader shader = CreateShader();
361   TextureSet textureSet = CreateTextureSet();
362
363   Sampler sampler = Sampler::New();
364   sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
365   textureSet.SetImage( 0u, image );
366   textureSet.SetSampler( 0u, sampler );
367
368   Geometry geometry = CreateQuadGeometry();
369   Renderer renderer = Renderer::New( geometry, shader );
370   renderer.SetTextures( textureSet );
371
372   Actor actor = Actor::New();
373   actor.AddRenderer(renderer);
374   actor.SetParentOrigin( ParentOrigin::CENTER );
375   actor.SetSize(400, 400);
376
377   Stage::GetCurrent().Add( actor );
378
379   TestGlAbstraction& gl = application.GetGlAbstraction();
380
381   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
382   texParameterTrace.Reset();
383   texParameterTrace.Enable( true );
384   application.SendNotification();
385   application.Render();
386
387   int textureUnit=-1;
388   DALI_TEST_CHECK( gl.GetUniformValue<int>( "sTexture", textureUnit ) );
389   DALI_TEST_EQUALS( textureUnit, 0, TEST_LOCATION );
390
391   texParameterTrace.Enable( false );
392
393   // Verify gl state
394   // There are three calls to TexParameteri when the texture is first created
395   // Texture minification and magnification filters are now different than default so
396   //there should have been two extra TexParameteri calls to set the new filter mode
397   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 4, TEST_LOCATION);
398
399   END_TEST;
400 }
401
402 int UtcDaliTextureSetSetSampler(void)
403 {
404   TestApplication application;
405
406   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
407
408   Shader shader = CreateShader();
409   TextureSet textureSet = CreateTextureSet( image );
410
411   Geometry geometry = CreateQuadGeometry();
412   Renderer renderer = Renderer::New( geometry, shader );
413   renderer.SetTextures( textureSet );
414
415   Actor actor = Actor::New();
416   actor.AddRenderer(renderer);
417   actor.SetParentOrigin( ParentOrigin::CENTER );
418   actor.SetSize(400, 400);
419
420   Stage::GetCurrent().Add( actor );
421
422   TestGlAbstraction& gl = application.GetGlAbstraction();
423
424   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
425   texParameterTrace.Reset();
426   texParameterTrace.Enable( true );
427   application.SendNotification();
428   application.Render();
429
430   int textureUnit=-1;
431   DALI_TEST_CHECK( gl.GetUniformValue<int>( "sTexture", textureUnit ) );
432   DALI_TEST_EQUALS( textureUnit, 0, TEST_LOCATION );
433
434   texParameterTrace.Enable( false );
435
436   // Verify gl state
437   // There are three calls to TexParameteri when the texture is first created
438   // as the texture is using default sampling parametrers there shouldn't be any more calls to TexParameteri
439   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 3, TEST_LOCATION);
440
441   texParameterTrace.Reset();
442   texParameterTrace.Enable( true );
443
444   Sampler sampler = Sampler::New();
445   sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
446   textureSet.SetSampler( 0u, sampler );
447
448
449   application.SendNotification();
450   application.Render();
451
452   texParameterTrace.Enable( false );
453
454   // Verify gl state
455   //There should have been two calls to TexParameteri to set the new filtering mode
456   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 2, TEST_LOCATION);
457
458
459   END_TEST;
460 }
461
462 int UtcDaliTextureSetGetImage(void)
463 {
464   TestApplication application;
465
466   TextureSet textureSet = CreateTextureSet();
467   DALI_TEST_EQUALS( textureSet.GetImage(0), Image(), TEST_LOCATION );
468   DALI_TEST_EQUALS( textureSet.GetImage(1), Image(), TEST_LOCATION );
469   DALI_TEST_EQUALS( textureSet.GetImage(2), Image(), TEST_LOCATION );
470
471   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
472   textureSet.SetImage( 0u, image );
473
474   DALI_TEST_EQUALS( textureSet.GetImage(0), image, TEST_LOCATION );
475   DALI_TEST_EQUALS( textureSet.GetImage(1), Image(), TEST_LOCATION );
476   DALI_TEST_EQUALS( textureSet.GetImage(2), Image(), TEST_LOCATION );
477
478   textureSet.SetImage( 2u, image );
479   DALI_TEST_EQUALS( textureSet.GetImage(0), image, TEST_LOCATION );
480   DALI_TEST_EQUALS( textureSet.GetImage(1), Image(), TEST_LOCATION );
481   DALI_TEST_EQUALS( textureSet.GetImage(2), image, TEST_LOCATION );
482
483   textureSet.SetImage( 2u, Image() );
484   DALI_TEST_EQUALS( textureSet.GetImage(0), image, TEST_LOCATION );
485   DALI_TEST_EQUALS( textureSet.GetImage(1), Image(), TEST_LOCATION );
486   DALI_TEST_EQUALS( textureSet.GetImage(2), Image(), TEST_LOCATION );
487
488   END_TEST;
489 }
490
491 int UtcDaliTextureSetGetSampler(void)
492 {
493   TestApplication application;
494
495   TextureSet textureSet = CreateTextureSet();
496   DALI_TEST_EQUALS( textureSet.GetSampler(0), Sampler(), TEST_LOCATION );
497   DALI_TEST_EQUALS( textureSet.GetSampler(1), Sampler(), TEST_LOCATION );
498   DALI_TEST_EQUALS( textureSet.GetSampler(2), Sampler(), TEST_LOCATION );
499
500   Sampler sampler = Sampler::New();
501   sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
502   textureSet.SetSampler( 0u, sampler );
503
504   DALI_TEST_EQUALS( textureSet.GetSampler(0), sampler, TEST_LOCATION );
505   DALI_TEST_EQUALS( textureSet.GetSampler(1), Sampler(), TEST_LOCATION );
506   DALI_TEST_EQUALS( textureSet.GetSampler(2), Sampler(), TEST_LOCATION );
507
508   textureSet.SetSampler( 2u, sampler );
509   DALI_TEST_EQUALS( textureSet.GetSampler(0), sampler, TEST_LOCATION );
510   DALI_TEST_EQUALS( textureSet.GetSampler(1), Sampler(), TEST_LOCATION );
511   DALI_TEST_EQUALS( textureSet.GetSampler(2), sampler, TEST_LOCATION );
512
513   textureSet.SetSampler( 2u, Sampler() );
514   DALI_TEST_EQUALS( textureSet.GetSampler(0), sampler, TEST_LOCATION );
515   DALI_TEST_EQUALS( textureSet.GetSampler(1), Sampler(), TEST_LOCATION );
516   DALI_TEST_EQUALS( textureSet.GetSampler(2), Sampler(), TEST_LOCATION );
517
518   END_TEST;
519 }
520
521 int UtcDaliTextureSetGetTextureCount(void)
522 {
523   TestApplication application;
524
525   TextureSet textureSet = CreateTextureSet();
526   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 0u, TEST_LOCATION );
527
528   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
529   textureSet.SetImage( 0u, image );
530   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 1u, TEST_LOCATION );
531
532   textureSet.SetImage( 1u, image );
533   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 2u, TEST_LOCATION );
534
535   textureSet.SetSampler( 2u, Sampler::New() );
536   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 3u, TEST_LOCATION );
537
538   END_TEST;
539 }