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