Migrating render-texture and render-sampler to new API
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Sampler.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 #include <string.h>
21 #include <unistd.h>
22
23 // INTERNAL INCLUDES
24 #include <dali-test-suite-utils.h>
25 #include <mesh-builder.h>
26
27 using namespace Dali;
28
29 void sampler_test_startup(void)
30 {
31   test_return_value = TET_UNDEF;
32 }
33
34 void sampler_test_cleanup(void)
35 {
36   test_return_value = TET_PASS;
37 }
38
39 namespace
40 {
41 Texture CreateTexture(TextureType::Type type, Pixel::Format format, int width, int height)
42 {
43   Texture texture = Texture::New(type, format, width, height);
44
45   int       bufferSize = width * height * 2;
46   uint8_t*  buffer     = reinterpret_cast<uint8_t*>(malloc(bufferSize));
47   PixelData pixelData  = PixelData::New(buffer, bufferSize, width, height, format, PixelData::FREE);
48   texture.Upload(pixelData, 0u, 0u, 0u, 0u, width, height);
49   return texture;
50 }
51 } // namespace
52
53 int UtcDaliSamplerNew01(void)
54 {
55   TestApplication application;
56   Sampler         sampler = Sampler::New();
57
58   DALI_TEST_EQUALS((bool)sampler, true, TEST_LOCATION);
59   END_TEST;
60 }
61
62 int UtcDaliSamplerNew02(void)
63 {
64   TestApplication application;
65   Sampler         sampler;
66   DALI_TEST_EQUALS((bool)sampler, false, TEST_LOCATION);
67   END_TEST;
68 }
69
70 int UtcDaliSamplerCopyConstructor(void)
71 {
72   TestApplication application;
73   tet_infoline("Testing Dali::Handle::Handle(const Handle&)");
74
75   // Initialize an object, ref count == 1
76   Sampler sampler = Sampler::New();
77
78   DALI_TEST_EQUALS(1, sampler.GetBaseObject().ReferenceCount(), TEST_LOCATION);
79
80   // Copy the object, ref count == 2
81   Sampler copy(sampler);
82   DALI_TEST_CHECK(copy);
83   if(copy)
84   {
85     DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
86   }
87
88   END_TEST;
89 }
90
91 int UtcDaliSamplerMoveConstructor(void)
92 {
93   TestApplication application;
94
95   Sampler sampler = Sampler::New();
96   DALI_TEST_CHECK(sampler);
97   DALI_TEST_EQUALS(1, sampler.GetBaseObject().ReferenceCount(), TEST_LOCATION);
98
99   Sampler move = std::move(sampler);
100   DALI_TEST_CHECK(move);
101   DALI_TEST_EQUALS(1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION);
102   DALI_TEST_CHECK(!sampler);
103
104   END_TEST;
105 }
106
107 int UtcDaliSamplerMoveAssignment(void)
108 {
109   TestApplication application;
110
111   Sampler sampler = Sampler::New();
112   DALI_TEST_CHECK(sampler);
113   DALI_TEST_EQUALS(1, sampler.GetBaseObject().ReferenceCount(), TEST_LOCATION);
114
115   Sampler move;
116   move = std::move(sampler);
117   DALI_TEST_CHECK(move);
118   DALI_TEST_EQUALS(1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION);
119   DALI_TEST_CHECK(!sampler);
120
121   END_TEST;
122 }
123
124 int UtcDaliSamplerDownCast01(void)
125 {
126   TestApplication application;
127   Sampler         sampler = Sampler::New();
128
129   BaseHandle handle(sampler);
130   Sampler    sampler2 = Sampler::DownCast(handle);
131   DALI_TEST_EQUALS((bool)sampler2, true, TEST_LOCATION);
132   END_TEST;
133 }
134
135 int UtcDaliSamplerDownCast02(void)
136 {
137   TestApplication application;
138
139   BaseHandle handle;
140   Sampler    sampler = Sampler::DownCast(handle);
141   DALI_TEST_EQUALS((bool)sampler, false, TEST_LOCATION);
142   END_TEST;
143 }
144
145 int UtcDaliSamplerAssignmentOperator(void)
146 {
147   TestApplication application;
148   Sampler         sampler1 = Sampler::New();
149
150   Sampler sampler2;
151
152   DALI_TEST_CHECK(!(sampler1 == sampler2));
153
154   sampler2 = sampler1;
155
156   DALI_TEST_CHECK(sampler1 == sampler2);
157
158   sampler2 = Sampler::New();
159
160   DALI_TEST_CHECK(!(sampler1 == sampler2));
161
162   END_TEST;
163 }
164
165 int UtcSamplerSetFilterMode(void)
166 {
167   TestApplication application;
168
169   Texture image   = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
170   Sampler sampler = Sampler::New();
171
172   TextureSet textureSet = CreateTextureSet();
173   textureSet.SetTexture(0u, image);
174   textureSet.SetSampler(0u, sampler);
175
176   Shader   shader   = CreateShader();
177   Geometry geometry = CreateQuadGeometry();
178   Renderer renderer = Renderer::New(geometry, shader);
179   renderer.SetTextures(textureSet);
180   Actor actor = Actor::New();
181   actor.AddRenderer(renderer);
182   actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
183   actor.SetProperty(Actor::Property::SIZE, Vector2(400.0f, 400.0f));
184   application.GetScene().Add(actor);
185
186   TestGlAbstraction& gl = application.GetGlAbstraction();
187
188   /**************************************************************/
189   // Default/Default
190   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
191   texParameterTrace.Reset();
192   texParameterTrace.Enable(true);
193   texParameterTrace.EnableLogging(true);
194
195   sampler.SetFilterMode(FilterMode::DEFAULT, FilterMode::DEFAULT);
196   application.SendNotification();
197   application.Render();
198
199   texParameterTrace.Enable(false);
200
201   // Verify gl state
202
203   // There are 4 calls to TexParameteri when the texture is first created
204   DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 4, TEST_LOCATION);
205
206   std::stringstream out;
207   out << std::hex << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_LINEAR;
208   DALI_TEST_EQUALS(texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
209
210   /**************************************************************/
211   // Linear/Linear
212   texParameterTrace.Reset();
213   texParameterTrace.Enable(true);
214
215   sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR);
216
217   // Flush the queue and render once
218   application.SendNotification();
219   application.Render();
220
221   texParameterTrace.Enable(false);
222
223   // Verify gl state
224
225   // Should not make any calls when settings are the same (DEFAULT = LINEAR )
226   DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 0, TEST_LOCATION);
227
228   /**************************************************************/
229   // Nearest/Nearest
230   texParameterTrace.Reset();
231   texParameterTrace.Enable(true);
232
233   sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
234
235   // Flush the queue and render once
236   application.SendNotification();
237   application.Render();
238
239   texParameterTrace.Enable(false);
240
241   // Verify actor gl state
242   DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 2, TEST_LOCATION);
243
244   out.str("");
245   out << std::hex << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_NEAREST;
246   DALI_TEST_EQUALS(texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
247
248   out.str("");
249   out << std::hex << GL_TEXTURE_2D << ", " << GL_TEXTURE_MAG_FILTER << ", " << GL_NEAREST;
250   DALI_TEST_EQUALS(texParameterTrace.TestMethodAndParams(1, "TexParameteri", out.str()), true, TEST_LOCATION);
251
252   /**************************************************************/
253   // Nearest/Linear
254   texParameterTrace.Reset();
255   texParameterTrace.Enable(true);
256
257   sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::LINEAR);
258
259   // Flush the queue and render once
260   application.SendNotification();
261   application.Render();
262
263   texParameterTrace.Enable(false);
264
265   // Verify actor gl state
266   DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 1, TEST_LOCATION);
267
268   out.str("");
269   out << std::hex << GL_TEXTURE_2D << ", " << GL_TEXTURE_MAG_FILTER << ", " << GL_LINEAR;
270   DALI_TEST_EQUALS(texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
271
272   /**************************************************************/
273   // Nearest+mipmap nearest/Linear
274   texParameterTrace.Reset();
275   texParameterTrace.Enable(true);
276
277   sampler.SetFilterMode(FilterMode::NEAREST_MIPMAP_NEAREST, FilterMode::LINEAR);
278
279   // Flush the queue and render once
280   application.SendNotification();
281   application.Render();
282
283   texParameterTrace.Enable(false);
284
285   // Verify actor gl state
286   DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 1, TEST_LOCATION);
287
288   out.str("");
289   out << std::hex << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_NEAREST_MIPMAP_NEAREST;
290   DALI_TEST_EQUALS(texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
291
292   /**************************************************************/
293   // Nearest+mipmap linear/Linear
294   texParameterTrace.Reset();
295   texParameterTrace.Enable(true);
296
297   sampler.SetFilterMode(FilterMode::NEAREST_MIPMAP_LINEAR, FilterMode::LINEAR);
298
299   // Flush the queue and render once
300   application.SendNotification();
301   application.Render();
302
303   texParameterTrace.Enable(false);
304
305   // Verify actor gl state
306   DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 1, TEST_LOCATION);
307
308   out.str("");
309   out << std::hex << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_NEAREST_MIPMAP_LINEAR;
310   DALI_TEST_EQUALS(texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
311
312   /**************************************************************/
313   // linear+mipmap nearest/Linear
314   texParameterTrace.Reset();
315   texParameterTrace.Enable(true);
316
317   sampler.SetFilterMode(FilterMode::LINEAR_MIPMAP_NEAREST, FilterMode::LINEAR);
318
319   // Flush the queue and render once
320   application.SendNotification();
321   application.Render();
322
323   texParameterTrace.Enable(false);
324
325   // Verify actor gl state
326   DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 1, TEST_LOCATION);
327
328   out.str("");
329   out << std::hex << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_LINEAR_MIPMAP_NEAREST;
330   DALI_TEST_EQUALS(texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
331
332   /**************************************************************/
333   // linear+mipmap linear/Linear
334   texParameterTrace.Reset();
335   texParameterTrace.Enable(true);
336
337   sampler.SetFilterMode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR);
338
339   // Flush the queue and render once
340   application.SendNotification();
341   application.Render();
342
343   texParameterTrace.Enable(false);
344
345   // Verify actor gl state
346   DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 1, TEST_LOCATION);
347
348   out.str("");
349   out << std::hex << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_LINEAR_MIPMAP_LINEAR;
350   DALI_TEST_EQUALS(texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
351
352   /**************************************************************/
353   // NONE/NONE
354   texParameterTrace.Reset();
355   texParameterTrace.Enable(true);
356
357   sampler.SetFilterMode(FilterMode::NONE, FilterMode::NONE);
358
359   // Flush the queue and render once
360   application.SendNotification();
361   application.Render();
362
363   texParameterTrace.Enable(false);
364
365   // Verify actor gl state
366   DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 1, TEST_LOCATION);
367
368   out.str("");
369   out << std::hex << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_NEAREST_MIPMAP_LINEAR;
370   DALI_TEST_EQUALS(texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
371
372   END_TEST;
373 }
374
375 int UtcSamplerSetWrapMode1(void)
376 {
377   TestApplication application;
378
379   Texture    image      = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
380   TextureSet textureSet = CreateTextureSet();
381   Sampler    sampler    = Sampler::New();
382   textureSet.SetTexture(0u, image);
383   textureSet.SetSampler(0u, sampler);
384
385   Shader   shader   = CreateShader();
386   Geometry geometry = CreateQuadGeometry();
387   Renderer renderer = Renderer::New(geometry, shader);
388   renderer.SetTextures(textureSet);
389
390   Actor actor = Actor::New();
391   actor.AddRenderer(renderer);
392   actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
393   actor.SetProperty(Actor::Property::SIZE, Vector2(400.0f, 400.0f));
394   application.GetScene().Add(actor);
395
396   TestGlAbstraction& gl = application.GetGlAbstraction();
397
398   //****************************************
399   // CLAMP_TO_EDGE / CLAMP_TO_EDGE
400   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
401   texParameterTrace.Reset();
402   texParameterTrace.Enable(true);
403   texParameterTrace.EnableLogging(true);
404
405   application.SendNotification();
406   application.Render();
407
408   texParameterTrace.Enable(false);
409
410   // Verify gl state
411
412   // There are 4 calls to TexParameteri when the texture is first created
413   DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 4, TEST_LOCATION);
414
415   std::stringstream out;
416   out << std::hex << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_S << ", " << GL_CLAMP_TO_EDGE;
417   DALI_TEST_EQUALS(texParameterTrace.TestMethodAndParams(2, "TexParameteri", out.str()), true, TEST_LOCATION);
418
419   out.str("");
420   out << std::hex << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_T << ", " << GL_CLAMP_TO_EDGE;
421   DALI_TEST_EQUALS(texParameterTrace.TestMethodAndParams(3, "TexParameteri", out.str()), true, TEST_LOCATION);
422
423   texParameterTrace.Reset();
424   texParameterTrace.Enable(true);
425
426   sampler.SetWrapMode(WrapMode::DEFAULT, WrapMode::DEFAULT);
427
428   // Flush the queue and render once
429   application.SendNotification();
430   application.Render();
431
432   texParameterTrace.Enable(false);
433
434   // Verify gl state
435
436   // Should not make any calls when settings are the same
437   DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 0, TEST_LOCATION);
438
439   //Todo: Test the other wrap mode ( REPEAT, MIRRORED_REPEAT )  , currently not support!!
440
441   END_TEST;
442 }
443
444 int UtcSamplerSetWrapMode2(void)
445 {
446   TestApplication application;
447
448   // Create a cube-map texture.
449   unsigned int width   = 8u;
450   unsigned int height  = 8u;
451   Texture      texture = CreateTexture(TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height);
452
453   // Create source image data.
454   unsigned int   bufferSize(width * height * 4);
455   unsigned char* buffer = new unsigned char[bufferSize];
456   memset(buffer, 0u, bufferSize);
457
458   PixelData pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::DELETE_ARRAY);
459
460   // Upload the source image data to all 6 sides of our cube-map.
461   texture.Upload(pixelData, CubeMapLayer::POSITIVE_X, 0u, 0u, 0u, width, height);
462   texture.Upload(pixelData, CubeMapLayer::NEGATIVE_X, 0u, 0u, 0u, width, height);
463   texture.Upload(pixelData, CubeMapLayer::POSITIVE_Y, 0u, 0u, 0u, width, height);
464   texture.Upload(pixelData, CubeMapLayer::NEGATIVE_Y, 0u, 0u, 0u, width, height);
465   texture.Upload(pixelData, CubeMapLayer::POSITIVE_Z, 0u, 0u, 0u, width, height);
466   texture.Upload(pixelData, CubeMapLayer::NEGATIVE_Z, 0u, 0u, 0u, width, height);
467
468   // Finalize the cube-map setup.
469   TextureSet textureSet = TextureSet::New();
470   textureSet.SetTexture(0u, texture);
471
472   Sampler sampler = Sampler::New();
473   textureSet.SetSampler(0u, sampler);
474
475   Shader   shader   = CreateShader();
476   Geometry geometry = CreateQuadGeometry();
477   Renderer renderer = Renderer::New(geometry, shader);
478   renderer.SetTextures(textureSet);
479
480   Actor actor = Actor::New();
481   actor.AddRenderer(renderer);
482   actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
483   actor.SetProperty(Actor::Property::SIZE, Vector2(400.0f, 400.0f));
484   application.GetScene().Add(actor);
485
486   TestGlAbstraction& gl = application.GetGlAbstraction();
487
488   application.SendNotification();
489   application.Render();
490
491   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
492   texParameterTrace.Reset();
493   texParameterTrace.Enable(true);
494   texParameterTrace.EnableLogging(true);
495
496   // Call the 3 dimensional wrap mode API.
497   sampler.SetWrapMode(WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
498
499   application.SendNotification();
500   application.Render();
501
502   // Verify that no TexParameteri calls occurred since wrap mode hasn't changed.
503   DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 0u, TEST_LOCATION);
504
505   sampler.SetWrapMode(WrapMode::MIRRORED_REPEAT, WrapMode::REPEAT, WrapMode::REPEAT);
506   texParameterTrace.Reset();
507   application.SendNotification();
508   application.Render();
509
510   texParameterTrace.Enable(false);
511
512   // Verify that 3 TexParameteri calls occurred.
513   std::ostringstream out;
514   out << std::hex << GL_TEXTURE_CUBE_MAP << ", " << GL_TEXTURE_WRAP_R << ", " << GL_MIRRORED_REPEAT;
515   DALI_TEST_CHECK(texParameterTrace.FindMethodAndParams("TexParameteri", out.str()));
516   DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 3u, TEST_LOCATION);
517   END_TEST;
518 }
519
520 int UtcDaliSamplerSetWrapModeNegative01(void)
521 {
522   TestApplication application;
523   Dali::Sampler   instance;
524   try
525   {
526     Dali::WrapMode::Type arg1(static_cast<Dali::WrapMode::Type>(-1));
527     Dali::WrapMode::Type arg2(static_cast<Dali::WrapMode::Type>(-1));
528     instance.SetWrapMode(arg1, arg2);
529     DALI_TEST_CHECK(false); // Should not get here
530   }
531   catch(...)
532   {
533     DALI_TEST_CHECK(true); // We expect an assert
534   }
535   END_TEST;
536 }
537
538 int UtcDaliSamplerSetWrapModeNegative02(void)
539 {
540   TestApplication application;
541   Dali::Sampler   instance;
542   try
543   {
544     Dali::WrapMode::Type arg1(static_cast<Dali::WrapMode::Type>(-1));
545     Dali::WrapMode::Type arg2(static_cast<Dali::WrapMode::Type>(-1));
546     Dali::WrapMode::Type arg3(static_cast<Dali::WrapMode::Type>(-1));
547     instance.SetWrapMode(arg1, arg2, arg3);
548     DALI_TEST_CHECK(false); // Should not get here
549   }
550   catch(...)
551   {
552     DALI_TEST_CHECK(true); // We expect an assert
553   }
554   END_TEST;
555 }
556
557 int UtcDaliSamplerSetFilterModeNegative(void)
558 {
559   TestApplication application;
560   Dali::Sampler   instance;
561   try
562   {
563     Dali::FilterMode::Type arg1(static_cast<Dali::FilterMode::Type>(-1));
564     Dali::FilterMode::Type arg2(static_cast<Dali::FilterMode::Type>(-1));
565     instance.SetFilterMode(arg1, arg2);
566     DALI_TEST_CHECK(false); // Should not get here
567   }
568   catch(...)
569   {
570     DALI_TEST_CHECK(true); // We expect an assert
571   }
572   END_TEST;
573 }