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