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