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 );
173 TestGlAbstraction& gl = application.GetGlAbstraction();
175 /**************************************************************/
177 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
178 texParameterTrace.Reset();
179 texParameterTrace.Enable( true );
181 sampler.SetFilterMode( FilterMode::DEFAULT, FilterMode::DEFAULT );
182 application.SendNotification();
183 application.Render();
185 texParameterTrace.Enable( false );
189 // There are 4 calls to TexParameteri when the texture is first created
190 DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 4, TEST_LOCATION);
192 std::stringstream out;
193 out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_LINEAR;
194 DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
196 /**************************************************************/
198 texParameterTrace.Reset();
199 texParameterTrace.Enable( true );
201 sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
203 // Flush the queue and render once
204 application.SendNotification();
205 application.Render();
207 texParameterTrace.Enable( false );
211 // Should not make any calls when settings are the same (DEFAULT = LINEAR )
212 DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 0, TEST_LOCATION);
214 /**************************************************************/
216 texParameterTrace.Reset();
217 texParameterTrace.Enable( true );
219 sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
221 // Flush the queue and render once
222 application.SendNotification();
223 application.Render();
225 texParameterTrace.Enable( false );
227 // Verify actor gl state
228 DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 2, TEST_LOCATION);
231 out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_NEAREST;
232 DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
235 out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MAG_FILTER << ", " << GL_NEAREST;
236 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 << GL_TEXTURE_2D << ", " << GL_TEXTURE_MAG_FILTER << ", " << GL_LINEAR;
257 DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
259 /**************************************************************/
261 texParameterTrace.Reset();
262 texParameterTrace.Enable( true );
264 sampler.SetFilterMode( FilterMode::NONE, FilterMode::NONE );
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 << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_NEAREST_MIPMAP_LINEAR;
277 DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
282 int UtcSamplerSetWrapMode1(void)
284 TestApplication application;
286 Texture image = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
287 TextureSet textureSet = CreateTextureSet();
288 Sampler sampler = Sampler::New();
289 textureSet.SetTexture(0u, image);
290 textureSet.SetSampler( 0u, sampler );
292 Shader shader = CreateShader();
293 Geometry geometry = CreateQuadGeometry();
294 Renderer renderer = Renderer::New( geometry, shader );
295 renderer.SetTextures( textureSet );
297 Actor actor = Actor::New();
298 actor.AddRenderer(renderer);
299 actor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
300 actor.SetProperty( Actor::Property::SIZE, Vector2( 400.0f, 400.0f ) );
301 application.GetScene().Add( actor );
303 TestGlAbstraction& gl = application.GetGlAbstraction();
305 //****************************************
306 // CLAMP_TO_EDGE / CLAMP_TO_EDGE
307 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
308 texParameterTrace.Reset();
309 texParameterTrace.Enable( true );
311 application.SendNotification();
312 application.Render();
314 texParameterTrace.Enable( false );
318 // There are 4 calls to TexParameteri when the texture is first created
319 DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 4, TEST_LOCATION);
321 std::stringstream out;
322 out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_S << ", " << GL_CLAMP_TO_EDGE;
323 DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(2, "TexParameteri", out.str()), true, TEST_LOCATION);
326 out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_T << ", " << GL_CLAMP_TO_EDGE;
327 DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(3, "TexParameteri", out.str()), true, TEST_LOCATION);
329 texParameterTrace.Reset();
330 texParameterTrace.Enable( true );
332 sampler.SetWrapMode( WrapMode::DEFAULT, WrapMode::DEFAULT );
334 // Flush the queue and render once
335 application.SendNotification();
336 application.Render();
338 texParameterTrace.Enable( false );
342 // Should not make any calls when settings are the same
343 DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 0, TEST_LOCATION);
345 //Todo: Test the other wrap mode ( REPEAT, MIRRORED_REPEAT ) , currently not support!!
350 int UtcSamplerSetWrapMode2(void)
352 TestApplication application;
354 // Create a cube-map texture.
355 unsigned int width = 8u;
356 unsigned int height = 8u;
357 Texture texture = Texture::New( TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height );
359 // Create source image data.
360 unsigned int bufferSize( width * height * 4 );
361 unsigned char* buffer= new unsigned char[ bufferSize ];
362 memset( buffer, 0u, bufferSize );
364 PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::DELETE_ARRAY );
366 // Upload the source image data to all 6 sides of our cube-map.
367 texture.Upload( pixelData, CubeMapLayer::POSITIVE_X, 0u, 0u, 0u, width, height );
368 texture.Upload( pixelData, CubeMapLayer::NEGATIVE_X, 0u, 0u, 0u, width, height );
369 texture.Upload( pixelData, CubeMapLayer::POSITIVE_Y, 0u, 0u, 0u, width, height );
370 texture.Upload( pixelData, CubeMapLayer::NEGATIVE_Y, 0u, 0u, 0u, width, height );
371 texture.Upload( pixelData, CubeMapLayer::POSITIVE_Z, 0u, 0u, 0u, width, height );
372 texture.Upload( pixelData, CubeMapLayer::NEGATIVE_Z, 0u, 0u, 0u, width, height );
374 // Finalize the cube-map setup.
375 TextureSet textureSet = TextureSet::New();
376 textureSet.SetTexture( 0u, texture );
378 Sampler sampler = Sampler::New();
379 textureSet.SetSampler( 0u, sampler );
381 Shader shader = CreateShader();
382 Geometry geometry = CreateQuadGeometry();
383 Renderer renderer = Renderer::New( geometry, shader );
384 renderer.SetTextures( textureSet );
386 Actor actor = Actor::New();
387 actor.AddRenderer(renderer);
388 actor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
389 actor.SetProperty( Actor::Property::SIZE, Vector2( 400.0f, 400.0f ) );
390 application.GetScene().Add( actor );
392 TestGlAbstraction& gl = application.GetGlAbstraction();
394 application.SendNotification();
395 application.Render();
397 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
398 texParameterTrace.Reset();
399 texParameterTrace.Enable( true );
401 // Call the 3 dimensional wrap mode API.
402 sampler.SetWrapMode( WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE );
404 application.SendNotification();
405 application.Render();
407 // Verify that no TexParameteri calls occurred since wrap mode hasn't changed.
408 DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 0u, TEST_LOCATION );
411 sampler.SetWrapMode( WrapMode::REPEAT, WrapMode::REPEAT, WrapMode::REPEAT );
412 texParameterTrace.Reset();
413 application.SendNotification();
414 application.Render();
416 texParameterTrace.Enable( false );
418 // Verify that 3 TexParameteri calls occurred.
419 DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 3u, TEST_LOCATION );
423 int UtcDaliSamplerSetWrapModeNegative01(void)
425 TestApplication application;
426 Dali::Sampler instance;
429 Dali::WrapMode::Type arg1(static_cast<Dali::WrapMode::Type>(-1));
430 Dali::WrapMode::Type arg2(static_cast<Dali::WrapMode::Type>(-1));
431 instance.SetWrapMode(arg1,arg2);
432 DALI_TEST_CHECK(false); // Should not get here
436 DALI_TEST_CHECK(true); // We expect an assert
441 int UtcDaliSamplerSetWrapModeNegative02(void)
443 TestApplication application;
444 Dali::Sampler instance;
447 Dali::WrapMode::Type arg1(static_cast<Dali::WrapMode::Type>(-1));
448 Dali::WrapMode::Type arg2(static_cast<Dali::WrapMode::Type>(-1));
449 Dali::WrapMode::Type arg3(static_cast<Dali::WrapMode::Type>(-1));
450 instance.SetWrapMode(arg1,arg2,arg3);
451 DALI_TEST_CHECK(false); // Should not get here
455 DALI_TEST_CHECK(true); // We expect an assert
460 int UtcDaliSamplerSetFilterModeNegative(void)
462 TestApplication application;
463 Dali::Sampler instance;
466 Dali::FilterMode::Type arg1(static_cast<Dali::FilterMode::Type>(-1));
467 Dali::FilterMode::Type arg2(static_cast<Dali::FilterMode::Type>(-1));
468 instance.SetFilterMode(arg1,arg2);
469 DALI_TEST_CHECK(false); // Should not get here
473 DALI_TEST_CHECK(true); // We expect an assert