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