2 * Copyright (c) 2020 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;
39 int UtcDaliSamplerNew01(void)
41 TestApplication application;
42 Sampler sampler = Sampler::New();
44 DALI_TEST_EQUALS((bool)sampler, true, TEST_LOCATION);
48 int UtcDaliSamplerNew02(void)
50 TestApplication application;
52 DALI_TEST_EQUALS((bool)sampler, false, TEST_LOCATION);
56 int UtcDaliSamplerCopyConstructor(void)
58 TestApplication application;
59 tet_infoline("Testing Dali::Handle::Handle(const Handle&)");
61 // Initialize an object, ref count == 1
62 Sampler sampler = Sampler::New();
64 DALI_TEST_EQUALS(1, sampler.GetBaseObject().ReferenceCount(), TEST_LOCATION);
66 // Copy the object, ref count == 2
67 Sampler copy(sampler);
68 DALI_TEST_CHECK(copy);
71 DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
77 int UtcDaliSamplerMoveConstructor(void)
79 TestApplication application;
81 Sampler sampler = Sampler::New();
82 DALI_TEST_CHECK(sampler);
83 DALI_TEST_EQUALS(1, sampler.GetBaseObject().ReferenceCount(), TEST_LOCATION);
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);
93 int UtcDaliSamplerMoveAssignment(void)
95 TestApplication application;
97 Sampler sampler = Sampler::New();
98 DALI_TEST_CHECK(sampler);
99 DALI_TEST_EQUALS(1, sampler.GetBaseObject().ReferenceCount(), TEST_LOCATION);
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);
110 int UtcDaliSamplerDownCast01(void)
112 TestApplication application;
113 Sampler sampler = Sampler::New();
115 BaseHandle handle(sampler);
116 Sampler sampler2 = Sampler::DownCast(handle);
117 DALI_TEST_EQUALS((bool)sampler2, true, TEST_LOCATION);
121 int UtcDaliSamplerDownCast02(void)
123 TestApplication application;
126 Sampler sampler = Sampler::DownCast(handle);
127 DALI_TEST_EQUALS((bool)sampler, false, TEST_LOCATION);
131 int UtcDaliSamplerAssignmentOperator(void)
133 TestApplication application;
134 Sampler sampler1 = Sampler::New();
138 DALI_TEST_CHECK(!(sampler1 == sampler2));
142 DALI_TEST_CHECK(sampler1 == sampler2);
144 sampler2 = Sampler::New();
146 DALI_TEST_CHECK(!(sampler1 == sampler2));
151 int UtcSamplerSetFilterMode(void)
153 TestApplication application;
155 Texture image = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
156 Sampler sampler = Sampler::New();
158 TextureSet textureSet = CreateTextureSet();
159 textureSet.SetTexture(0u, image);
160 textureSet.SetSampler(0u, sampler);
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);
172 TestGlAbstraction& gl = application.GetGlAbstraction();
174 /**************************************************************/
176 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
177 texParameterTrace.Reset();
178 texParameterTrace.Enable(true);
180 sampler.SetFilterMode(FilterMode::DEFAULT, FilterMode::DEFAULT);
181 application.SendNotification();
182 application.Render();
184 texParameterTrace.Enable(false);
188 // There are 4 calls to TexParameteri when the texture is first created
189 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 4, TEST_LOCATION);
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);
195 /**************************************************************/
197 texParameterTrace.Reset();
198 texParameterTrace.Enable(true);
200 sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR);
202 // Flush the queue and render once
203 application.SendNotification();
204 application.Render();
206 texParameterTrace.Enable(false);
210 // Should not make any calls when settings are the same (DEFAULT = LINEAR )
211 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 0, TEST_LOCATION);
213 /**************************************************************/
215 texParameterTrace.Reset();
216 texParameterTrace.Enable(true);
218 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
220 // Flush the queue and render once
221 application.SendNotification();
222 application.Render();
224 texParameterTrace.Enable(false);
226 // Verify actor gl state
227 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 2, TEST_LOCATION);
230 out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_NEAREST;
231 DALI_TEST_EQUALS(texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
234 out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MAG_FILTER << ", " << GL_NEAREST;
235 DALI_TEST_EQUALS(texParameterTrace.TestMethodAndParams(1, "TexParameteri", out.str()), true, TEST_LOCATION);
237 /**************************************************************/
239 texParameterTrace.Reset();
240 texParameterTrace.Enable(true);
242 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::LINEAR);
244 // Flush the queue and render once
245 application.SendNotification();
246 application.Render();
248 texParameterTrace.Enable(false);
250 // Verify actor gl state
251 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 1, TEST_LOCATION);
254 out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MAG_FILTER << ", " << GL_LINEAR;
255 DALI_TEST_EQUALS(texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
257 /**************************************************************/
259 texParameterTrace.Reset();
260 texParameterTrace.Enable(true);
262 sampler.SetFilterMode(FilterMode::NONE, FilterMode::NONE);
264 // Flush the queue and render once
265 application.SendNotification();
266 application.Render();
268 texParameterTrace.Enable(false);
270 // Verify actor gl state
271 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 1, TEST_LOCATION);
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);
280 int UtcSamplerSetWrapMode1(void)
282 TestApplication application;
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);
290 Shader shader = CreateShader();
291 Geometry geometry = CreateQuadGeometry();
292 Renderer renderer = Renderer::New(geometry, shader);
293 renderer.SetTextures(textureSet);
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);
301 TestGlAbstraction& gl = application.GetGlAbstraction();
303 //****************************************
304 // CLAMP_TO_EDGE / CLAMP_TO_EDGE
305 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
306 texParameterTrace.Reset();
307 texParameterTrace.Enable(true);
309 application.SendNotification();
310 application.Render();
312 texParameterTrace.Enable(false);
316 // There are 4 calls to TexParameteri when the texture is first created
317 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 4, TEST_LOCATION);
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);
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);
327 texParameterTrace.Reset();
328 texParameterTrace.Enable(true);
330 sampler.SetWrapMode(WrapMode::DEFAULT, WrapMode::DEFAULT);
332 // Flush the queue and render once
333 application.SendNotification();
334 application.Render();
336 texParameterTrace.Enable(false);
340 // Should not make any calls when settings are the same
341 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 0, TEST_LOCATION);
343 //Todo: Test the other wrap mode ( REPEAT, MIRRORED_REPEAT ) , currently not support!!
348 int UtcSamplerSetWrapMode2(void)
350 TestApplication application;
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);
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);
362 PixelData pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::DELETE_ARRAY);
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);
372 // Finalize the cube-map setup.
373 TextureSet textureSet = TextureSet::New();
374 textureSet.SetTexture(0u, texture);
376 Sampler sampler = Sampler::New();
377 textureSet.SetSampler(0u, sampler);
379 Shader shader = CreateShader();
380 Geometry geometry = CreateQuadGeometry();
381 Renderer renderer = Renderer::New(geometry, shader);
382 renderer.SetTextures(textureSet);
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);
390 TestGlAbstraction& gl = application.GetGlAbstraction();
392 application.SendNotification();
393 application.Render();
395 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
396 texParameterTrace.Reset();
397 texParameterTrace.Enable(true);
399 // Call the 3 dimensional wrap mode API.
400 sampler.SetWrapMode(WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
402 application.SendNotification();
403 application.Render();
405 // Verify that no TexParameteri calls occurred since wrap mode hasn't changed.
406 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 0u, TEST_LOCATION);
408 sampler.SetWrapMode(WrapMode::REPEAT, WrapMode::REPEAT, WrapMode::REPEAT);
409 texParameterTrace.Reset();
410 application.SendNotification();
411 application.Render();
413 texParameterTrace.Enable(false);
415 // Verify that 3 TexParameteri calls occurred.
416 DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 3u, TEST_LOCATION);
420 int UtcDaliSamplerSetWrapModeNegative01(void)
422 TestApplication application;
423 Dali::Sampler instance;
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
433 DALI_TEST_CHECK(true); // We expect an assert
438 int UtcDaliSamplerSetWrapModeNegative02(void)
440 TestApplication application;
441 Dali::Sampler instance;
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
452 DALI_TEST_CHECK(true); // We expect an assert
457 int UtcDaliSamplerSetFilterModeNegative(void)
459 TestApplication application;
460 Dali::Sampler instance;
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
470 DALI_TEST_CHECK(true); // We expect an assert