2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/public-api/dali-core.h>
24 #include <dali-test-suite-utils.h>
25 #include <mesh-builder.h>
29 void sampler_test_startup(void)
31 test_return_value = TET_UNDEF;
34 void sampler_test_cleanup(void)
36 test_return_value = TET_PASS;
41 Texture CreateTexture(TextureType::Type type, Pixel::Format format, int width, int height)
43 Texture texture = Texture::New(type, format, width, height);
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);
53 int UtcDaliSamplerNew01(void)
55 TestApplication application;
56 Sampler sampler = Sampler::New();
58 DALI_TEST_EQUALS((bool)sampler, true, TEST_LOCATION);
62 int UtcDaliSamplerNew02(void)
64 TestApplication application;
66 DALI_TEST_EQUALS((bool)sampler, false, TEST_LOCATION);
70 int UtcDaliSamplerCopyConstructor(void)
72 TestApplication application;
73 tet_infoline("Testing Dali::Handle::Handle(const Handle&)");
75 // Initialize an object, ref count == 1
76 Sampler sampler = Sampler::New();
78 DALI_TEST_EQUALS(1, sampler.GetBaseObject().ReferenceCount(), TEST_LOCATION);
80 // Copy the object, ref count == 2
81 Sampler copy(sampler);
82 DALI_TEST_CHECK(copy);
85 DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
91 int UtcDaliSamplerMoveConstructor(void)
93 TestApplication application;
95 Sampler sampler = Sampler::New();
96 DALI_TEST_CHECK(sampler);
97 DALI_TEST_EQUALS(1, sampler.GetBaseObject().ReferenceCount(), TEST_LOCATION);
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);
107 int UtcDaliSamplerMoveAssignment(void)
109 TestApplication application;
111 Sampler sampler = Sampler::New();
112 DALI_TEST_CHECK(sampler);
113 DALI_TEST_EQUALS(1, sampler.GetBaseObject().ReferenceCount(), TEST_LOCATION);
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);
124 int UtcDaliSamplerDownCast01(void)
126 TestApplication application;
127 Sampler sampler = Sampler::New();
129 BaseHandle handle(sampler);
130 Sampler sampler2 = Sampler::DownCast(handle);
131 DALI_TEST_EQUALS((bool)sampler2, true, TEST_LOCATION);
135 int UtcDaliSamplerDownCast02(void)
137 TestApplication application;
140 Sampler sampler = Sampler::DownCast(handle);
141 DALI_TEST_EQUALS((bool)sampler, false, TEST_LOCATION);
145 int UtcDaliSamplerAssignmentOperator(void)
147 TestApplication application;
148 Sampler sampler1 = Sampler::New();
152 DALI_TEST_CHECK(!(sampler1 == sampler2));
156 DALI_TEST_CHECK(sampler1 == sampler2);
158 sampler2 = Sampler::New();
160 DALI_TEST_CHECK(!(sampler1 == sampler2));
165 int UtcSamplerSetFilterMode(void)
167 TestApplication application;
169 Texture image = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
170 Sampler sampler = Sampler::New();
172 TextureSet textureSet = CreateTextureSet();
173 textureSet.SetTexture(0u, image);
174 textureSet.SetSampler(0u, sampler);
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);
186 TestGlAbstraction& gl = application.GetGlAbstraction();
188 /**************************************************************/
190 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
191 texParameterTrace.Reset();
192 texParameterTrace.Enable(true);
193 texParameterTrace.EnableLogging(true);
195 sampler.SetFilterMode(FilterMode::DEFAULT, FilterMode::DEFAULT);
196 application.SendNotification();
197 application.Render();
199 texParameterTrace.Enable(false);
203 // There are 4 calls to TexParameteri when the texture is first created
204 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 4, TEST_LOCATION);
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);
210 /**************************************************************/
212 texParameterTrace.Reset();
213 texParameterTrace.Enable(true);
215 sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR);
217 // Flush the queue and render once
218 application.SendNotification();
219 application.Render();
221 texParameterTrace.Enable(false);
225 // Should not make any calls when settings are the same (DEFAULT = LINEAR )
226 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 0, TEST_LOCATION);
228 /**************************************************************/
230 texParameterTrace.Reset();
231 texParameterTrace.Enable(true);
233 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
235 // Flush the queue and render once
236 application.SendNotification();
237 application.Render();
239 texParameterTrace.Enable(false);
241 // Verify actor gl state
242 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 2, TEST_LOCATION);
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);
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);
252 /**************************************************************/
254 texParameterTrace.Reset();
255 texParameterTrace.Enable(true);
257 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::LINEAR);
259 // Flush the queue and render once
260 application.SendNotification();
261 application.Render();
263 texParameterTrace.Enable(false);
265 // Verify actor gl state
266 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 1, TEST_LOCATION);
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);
272 /**************************************************************/
273 // Nearest+mipmap nearest/Linear
274 texParameterTrace.Reset();
275 texParameterTrace.Enable(true);
277 sampler.SetFilterMode(FilterMode::NEAREST_MIPMAP_NEAREST, FilterMode::LINEAR);
279 // Flush the queue and render once
280 application.SendNotification();
281 application.Render();
283 texParameterTrace.Enable(false);
285 // Verify actor gl state
286 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 1, TEST_LOCATION);
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);
292 /**************************************************************/
293 // Nearest+mipmap linear/Linear
294 texParameterTrace.Reset();
295 texParameterTrace.Enable(true);
297 sampler.SetFilterMode(FilterMode::NEAREST_MIPMAP_LINEAR, FilterMode::LINEAR);
299 // Flush the queue and render once
300 application.SendNotification();
301 application.Render();
303 texParameterTrace.Enable(false);
305 // Verify actor gl state
306 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 1, TEST_LOCATION);
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);
312 /**************************************************************/
313 // linear+mipmap nearest/Linear
314 texParameterTrace.Reset();
315 texParameterTrace.Enable(true);
317 sampler.SetFilterMode(FilterMode::LINEAR_MIPMAP_NEAREST, FilterMode::LINEAR);
319 // Flush the queue and render once
320 application.SendNotification();
321 application.Render();
323 texParameterTrace.Enable(false);
325 // Verify actor gl state
326 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 1, TEST_LOCATION);
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);
332 /**************************************************************/
333 // linear+mipmap linear/Linear
334 texParameterTrace.Reset();
335 texParameterTrace.Enable(true);
337 sampler.SetFilterMode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR);
339 // Flush the queue and render once
340 application.SendNotification();
341 application.Render();
343 texParameterTrace.Enable(false);
345 // Verify actor gl state
346 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 1, TEST_LOCATION);
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);
352 /**************************************************************/
354 texParameterTrace.Reset();
355 texParameterTrace.Enable(true);
357 sampler.SetFilterMode(FilterMode::NONE, FilterMode::NONE);
359 // Flush the queue and render once
360 application.SendNotification();
361 application.Render();
363 texParameterTrace.Enable(false);
365 // Verify actor gl state
366 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 1, TEST_LOCATION);
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);
375 int UtcSamplerSetWrapMode1(void)
377 TestApplication application;
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);
385 Shader shader = CreateShader();
386 Geometry geometry = CreateQuadGeometry();
387 Renderer renderer = Renderer::New(geometry, shader);
388 renderer.SetTextures(textureSet);
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);
396 TestGlAbstraction& gl = application.GetGlAbstraction();
398 //****************************************
399 // CLAMP_TO_EDGE / CLAMP_TO_EDGE
400 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
401 texParameterTrace.Reset();
402 texParameterTrace.Enable(true);
403 texParameterTrace.EnableLogging(true);
405 application.SendNotification();
406 application.Render();
408 texParameterTrace.Enable(false);
412 // There are 4 calls to TexParameteri when the texture is first created
413 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 4, TEST_LOCATION);
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);
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);
423 texParameterTrace.Reset();
424 texParameterTrace.Enable(true);
426 sampler.SetWrapMode(WrapMode::DEFAULT, WrapMode::DEFAULT);
428 // Flush the queue and render once
429 application.SendNotification();
430 application.Render();
432 texParameterTrace.Enable(false);
436 // Should not make any calls when settings are the same
437 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 0, TEST_LOCATION);
439 //Todo: Test the other wrap mode ( REPEAT, MIRRORED_REPEAT ) , currently not support!!
444 int UtcSamplerSetWrapMode2(void)
446 TestApplication application;
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);
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);
458 PixelData pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::DELETE_ARRAY);
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);
468 // Finalize the cube-map setup.
469 TextureSet textureSet = TextureSet::New();
470 textureSet.SetTexture(0u, texture);
472 Sampler sampler = Sampler::New();
473 textureSet.SetSampler(0u, sampler);
475 Shader shader = CreateShader();
476 Geometry geometry = CreateQuadGeometry();
477 Renderer renderer = Renderer::New(geometry, shader);
478 renderer.SetTextures(textureSet);
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);
486 TestGlAbstraction& gl = application.GetGlAbstraction();
488 application.SendNotification();
489 application.Render();
491 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
492 texParameterTrace.Reset();
493 texParameterTrace.Enable(true);
494 texParameterTrace.EnableLogging(true);
496 // Call the 3 dimensional wrap mode API.
497 sampler.SetWrapMode(WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
499 application.SendNotification();
500 application.Render();
502 // Verify that no TexParameteri calls occurred since wrap mode hasn't changed.
503 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 0u, TEST_LOCATION);
505 sampler.SetWrapMode(WrapMode::MIRRORED_REPEAT, WrapMode::REPEAT, WrapMode::REPEAT);
506 texParameterTrace.Reset();
507 application.SendNotification();
508 application.Render();
510 texParameterTrace.Enable(false);
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);
520 int UtcDaliSamplerSetWrapModeNegative01(void)
522 TestApplication application;
523 Dali::Sampler instance;
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
533 DALI_TEST_CHECK(true); // We expect an assert
538 int UtcDaliSamplerSetWrapModeNegative02(void)
540 TestApplication application;
541 Dali::Sampler instance;
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
552 DALI_TEST_CHECK(true); // We expect an assert
557 int UtcDaliSamplerSetFilterModeNegative(void)
559 TestApplication application;
560 Dali::Sampler instance;
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
570 DALI_TEST_CHECK(true); // We expect an assert