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>
26 #include <test-actor-utils.h>
30 void sampler_test_startup(void)
32 test_return_value = TET_UNDEF;
35 void sampler_test_cleanup(void)
37 test_return_value = TET_PASS;
40 int UtcDaliSamplerNew01(void)
42 TestApplication application;
43 Sampler sampler = Sampler::New();
45 DALI_TEST_EQUALS((bool)sampler, true, TEST_LOCATION);
49 int UtcDaliSamplerNew02(void)
51 TestApplication application;
53 DALI_TEST_EQUALS((bool)sampler, false, TEST_LOCATION);
57 int UtcDaliSamplerCopyConstructor(void)
59 TestApplication application;
60 tet_infoline("Testing Dali::Handle::Handle(const Handle&)");
62 // Initialize an object, ref count == 1
63 Sampler sampler = Sampler::New();
65 DALI_TEST_EQUALS(1, sampler.GetBaseObject().ReferenceCount(), TEST_LOCATION);
67 // Copy the object, ref count == 2
68 Sampler copy(sampler);
69 DALI_TEST_CHECK(copy);
72 DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
78 int UtcDaliSamplerMoveConstructor(void)
80 TestApplication application;
82 Sampler sampler = Sampler::New();
83 DALI_TEST_CHECK(sampler);
84 DALI_TEST_EQUALS(1, sampler.GetBaseObject().ReferenceCount(), TEST_LOCATION);
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);
94 int UtcDaliSamplerMoveAssignment(void)
96 TestApplication application;
98 Sampler sampler = Sampler::New();
99 DALI_TEST_CHECK(sampler);
100 DALI_TEST_EQUALS(1, sampler.GetBaseObject().ReferenceCount(), TEST_LOCATION);
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);
111 int UtcDaliSamplerDownCast01(void)
113 TestApplication application;
114 Sampler sampler = Sampler::New();
116 BaseHandle handle(sampler);
117 Sampler sampler2 = Sampler::DownCast(handle);
118 DALI_TEST_EQUALS((bool)sampler2, true, TEST_LOCATION);
122 int UtcDaliSamplerDownCast02(void)
124 TestApplication application;
127 Sampler sampler = Sampler::DownCast(handle);
128 DALI_TEST_EQUALS((bool)sampler, false, TEST_LOCATION);
132 int UtcDaliSamplerAssignmentOperator(void)
134 TestApplication application;
135 Sampler sampler1 = Sampler::New();
139 DALI_TEST_CHECK(!(sampler1 == sampler2));
143 DALI_TEST_CHECK(sampler1 == sampler2);
145 sampler2 = Sampler::New();
147 DALI_TEST_CHECK(!(sampler1 == sampler2));
152 int UtcSamplerSetFilterMode(void)
154 TestApplication application;
156 Texture image = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
157 Sampler sampler = Sampler::New();
159 TextureSet textureSet = CreateTextureSet();
160 textureSet.SetTexture(0u, image);
161 textureSet.SetSampler(0u, sampler);
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);
173 TestGlAbstraction& gl = application.GetGlAbstraction();
175 /**************************************************************/
177 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
178 texParameterTrace.Reset();
179 texParameterTrace.Enable(true);
180 texParameterTrace.EnableLogging(true);
182 sampler.SetFilterMode(FilterMode::DEFAULT, FilterMode::DEFAULT);
183 application.SendNotification();
184 application.Render();
186 texParameterTrace.Enable(false);
190 // There are 4 calls to TexParameteri when the texture is first created
191 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 4, TEST_LOCATION);
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);
197 /**************************************************************/
199 texParameterTrace.Reset();
200 texParameterTrace.Enable(true);
202 sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR);
204 // Flush the queue and render once
205 application.SendNotification();
206 application.Render();
208 texParameterTrace.Enable(false);
212 // Should not make any calls when settings are the same (DEFAULT = LINEAR )
213 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 0, TEST_LOCATION);
215 /**************************************************************/
217 texParameterTrace.Reset();
218 texParameterTrace.Enable(true);
220 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
222 // Flush the queue and render once
223 application.SendNotification();
224 application.Render();
226 texParameterTrace.Enable(false);
228 // Verify actor gl state
229 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 2, TEST_LOCATION);
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);
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);
239 /**************************************************************/
241 texParameterTrace.Reset();
242 texParameterTrace.Enable(true);
244 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::LINEAR);
246 // Flush the queue and render once
247 application.SendNotification();
248 application.Render();
250 texParameterTrace.Enable(false);
252 // Verify actor gl state
253 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 1, TEST_LOCATION);
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);
259 /**************************************************************/
260 // Nearest+mipmap nearest/Linear
261 texParameterTrace.Reset();
262 texParameterTrace.Enable(true);
264 sampler.SetFilterMode(FilterMode::NEAREST_MIPMAP_NEAREST, FilterMode::LINEAR);
266 // Flush the queue and render once
267 application.SendNotification();
268 application.Render();
270 texParameterTrace.Enable(false);
272 // Verify actor gl state
273 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 1, TEST_LOCATION);
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);
279 /**************************************************************/
280 // Nearest+mipmap linear/Linear
281 texParameterTrace.Reset();
282 texParameterTrace.Enable(true);
284 sampler.SetFilterMode(FilterMode::NEAREST_MIPMAP_LINEAR, FilterMode::LINEAR);
286 // Flush the queue and render once
287 application.SendNotification();
288 application.Render();
290 texParameterTrace.Enable(false);
292 // Verify actor gl state
293 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 1, TEST_LOCATION);
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);
299 /**************************************************************/
300 // linear+mipmap nearest/Linear
301 texParameterTrace.Reset();
302 texParameterTrace.Enable(true);
304 sampler.SetFilterMode(FilterMode::LINEAR_MIPMAP_NEAREST, FilterMode::LINEAR);
306 // Flush the queue and render once
307 application.SendNotification();
308 application.Render();
310 texParameterTrace.Enable(false);
312 // Verify actor gl state
313 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 1, TEST_LOCATION);
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);
319 /**************************************************************/
320 // linear+mipmap linear/Linear
321 texParameterTrace.Reset();
322 texParameterTrace.Enable(true);
324 sampler.SetFilterMode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR);
326 // Flush the queue and render once
327 application.SendNotification();
328 application.Render();
330 texParameterTrace.Enable(false);
332 // Verify actor gl state
333 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 1, TEST_LOCATION);
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);
339 /**************************************************************/
341 texParameterTrace.Reset();
342 texParameterTrace.Enable(true);
344 sampler.SetFilterMode(FilterMode::NONE, FilterMode::NONE);
346 // Flush the queue and render once
347 application.SendNotification();
348 application.Render();
350 texParameterTrace.Enable(false);
352 // Verify actor gl state
353 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 1, TEST_LOCATION);
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);
362 int UtcSamplerSetWrapMode1(void)
364 TestApplication application;
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);
372 Shader shader = CreateShader();
373 Geometry geometry = CreateQuadGeometry();
374 Renderer renderer = Renderer::New(geometry, shader);
375 renderer.SetTextures(textureSet);
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);
383 TestGlAbstraction& gl = application.GetGlAbstraction();
385 //****************************************
386 // CLAMP_TO_EDGE / CLAMP_TO_EDGE
387 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
388 texParameterTrace.Reset();
389 texParameterTrace.Enable(true);
390 texParameterTrace.EnableLogging(true);
392 application.SendNotification();
393 application.Render();
395 texParameterTrace.Enable(false);
399 // There are 4 calls to TexParameteri when the texture is first created
400 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 4, TEST_LOCATION);
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);
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);
410 texParameterTrace.Reset();
411 texParameterTrace.Enable(true);
413 sampler.SetWrapMode(WrapMode::DEFAULT, WrapMode::DEFAULT);
415 // Flush the queue and render once
416 application.SendNotification();
417 application.Render();
419 texParameterTrace.Enable(false);
423 // Should not make any calls when settings are the same
424 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 0, TEST_LOCATION);
426 //Todo: Test the other wrap mode ( REPEAT, MIRRORED_REPEAT ) , currently not support!!
431 int UtcSamplerSetWrapMode2(void)
433 TestApplication application;
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);
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);
445 PixelData pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::DELETE_ARRAY);
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);
455 // Finalize the cube-map setup.
456 TextureSet textureSet = TextureSet::New();
457 textureSet.SetTexture(0u, texture);
459 Sampler sampler = Sampler::New();
460 textureSet.SetSampler(0u, sampler);
462 Shader shader = CreateShader();
463 Geometry geometry = CreateQuadGeometry();
464 Renderer renderer = Renderer::New(geometry, shader);
465 renderer.SetTextures(textureSet);
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);
473 TestGlAbstraction& gl = application.GetGlAbstraction();
475 application.SendNotification();
476 application.Render();
478 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
479 texParameterTrace.Reset();
480 texParameterTrace.Enable(true);
481 texParameterTrace.EnableLogging(true);
483 // Call the 3 dimensional wrap mode API.
484 sampler.SetWrapMode(WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
486 application.SendNotification();
487 application.Render();
489 // Verify that no TexParameteri calls occurred since wrap mode hasn't changed.
490 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 0u, TEST_LOCATION);
492 sampler.SetWrapMode(WrapMode::MIRRORED_REPEAT, WrapMode::REPEAT, WrapMode::REPEAT);
493 texParameterTrace.Reset();
494 application.SendNotification();
495 application.Render();
497 texParameterTrace.Enable(false);
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);
507 int UtcDaliSamplerSetWrapModeNegative01(void)
509 TestApplication application;
510 Dali::Sampler instance;
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
520 DALI_TEST_CHECK(true); // We expect an assert
525 int UtcDaliSamplerSetWrapModeNegative02(void)
527 TestApplication application;
528 Dali::Sampler instance;
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
539 DALI_TEST_CHECK(true); // We expect an assert
544 int UtcDaliSamplerSetFilterModeNegative(void)
546 TestApplication application;
547 Dali::Sampler instance;
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
557 DALI_TEST_CHECK(true); // We expect an assert