2 * Copyright (c) 2016 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>
20 #include <dali/devel-api/images/texture-set-image.h>
25 #include <dali-test-suite-utils.h>
26 #include <mesh-builder.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 UtcDaliSamplerDownCast01(void)
80 TestApplication application;
81 Sampler sampler = Sampler::New();
83 BaseHandle handle(sampler);
84 Sampler sampler2 = Sampler::DownCast(handle);
85 DALI_TEST_EQUALS( (bool)sampler2, true, TEST_LOCATION );
89 int UtcDaliSamplerDownCast02(void)
91 TestApplication application;
94 Sampler sampler = Sampler::DownCast(handle);
95 DALI_TEST_EQUALS( (bool)sampler, false, TEST_LOCATION );
99 int UtcDaliSamplerAssignmentOperator(void)
101 TestApplication application;
102 Sampler sampler1 = Sampler::New();
106 DALI_TEST_CHECK(!(sampler1 == sampler2));
110 DALI_TEST_CHECK(sampler1 == sampler2);
112 sampler2 = Sampler::New();
114 DALI_TEST_CHECK(!(sampler1 == sampler2));
119 int UtcSamplerSetFilterMode(void)
121 TestApplication application;
123 Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
124 Sampler sampler = Sampler::New();
126 TextureSet textureSet = CreateTextureSet();
127 TextureSetImage( textureSet, 0u, image );
128 textureSet.SetSampler( 0u, sampler );
130 Shader shader = CreateShader();
131 Geometry geometry = CreateQuadGeometry();
132 Renderer renderer = Renderer::New( geometry, shader );
133 renderer.SetTextures( textureSet );
134 Actor actor = Actor::New();
135 actor.AddRenderer(renderer);
136 actor.SetParentOrigin( ParentOrigin::CENTER );
137 actor.SetSize(400, 400);
138 Stage::GetCurrent().Add( actor );
141 TestGlAbstraction& gl = application.GetGlAbstraction();
143 /**************************************************************/
145 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
146 texParameterTrace.Reset();
147 texParameterTrace.Enable( true );
149 sampler.SetFilterMode( FilterMode::DEFAULT, FilterMode::DEFAULT );
150 application.SendNotification();
151 application.Render();
153 texParameterTrace.Enable( false );
157 // There are 4 calls to TexParameteri when the texture is first created
158 DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 4, TEST_LOCATION);
160 std::stringstream out;
161 out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_LINEAR;
162 DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
164 /**************************************************************/
166 texParameterTrace.Reset();
167 texParameterTrace.Enable( true );
169 sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
171 // Flush the queue and render once
172 application.SendNotification();
173 application.Render();
175 texParameterTrace.Enable( false );
179 // Should not make any calls when settings are the same (DEFAULT = LINEAR )
180 DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 0, TEST_LOCATION);
182 /**************************************************************/
184 texParameterTrace.Reset();
185 texParameterTrace.Enable( true );
187 sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
189 // Flush the queue and render once
190 application.SendNotification();
191 application.Render();
193 texParameterTrace.Enable( false );
195 // Verify actor gl state
196 DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 2, TEST_LOCATION);
199 out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_NEAREST;
200 DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
203 out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MAG_FILTER << ", " << GL_NEAREST;
204 DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(1, "TexParameteri", out.str()), true, TEST_LOCATION);
207 /**************************************************************/
209 texParameterTrace.Reset();
210 texParameterTrace.Enable( true );
212 sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::LINEAR );
214 // Flush the queue and render once
215 application.SendNotification();
216 application.Render();
218 texParameterTrace.Enable( false );
220 // Verify actor gl state
221 DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 1, TEST_LOCATION);
224 out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MAG_FILTER << ", " << GL_LINEAR;
225 DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
227 /**************************************************************/
229 texParameterTrace.Reset();
230 texParameterTrace.Enable( true );
232 sampler.SetFilterMode( FilterMode::NONE, FilterMode::NONE );
234 // Flush the queue and render once
235 application.SendNotification();
236 application.Render();
238 texParameterTrace.Enable( false );
240 // Verify actor gl state
241 DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 1, TEST_LOCATION);
244 out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_NEAREST_MIPMAP_LINEAR;
245 DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
250 int UtcSamplerSetWrapMode1(void)
252 TestApplication application;
254 Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
255 TextureSet textureSet = CreateTextureSet();
256 Sampler sampler = Sampler::New();
257 TextureSetImage( textureSet, 0u, image );
258 textureSet.SetSampler( 0u, sampler );
260 Shader shader = CreateShader();
261 Geometry geometry = CreateQuadGeometry();
262 Renderer renderer = Renderer::New( geometry, shader );
263 renderer.SetTextures( textureSet );
265 Actor actor = Actor::New();
266 actor.AddRenderer(renderer);
267 actor.SetParentOrigin( ParentOrigin::CENTER );
268 actor.SetSize(400, 400);
269 Stage::GetCurrent().Add( actor );
271 TestGlAbstraction& gl = application.GetGlAbstraction();
273 //****************************************
274 // CLAMP_TO_EDGE / CLAMP_TO_EDGE
275 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
276 texParameterTrace.Reset();
277 texParameterTrace.Enable( true );
279 application.SendNotification();
280 application.Render();
282 texParameterTrace.Enable( false );
286 // There are 4 calls to TexParameteri when the texture is first created
287 DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 4, TEST_LOCATION);
289 std::stringstream out;
290 out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_S << ", " << GL_CLAMP_TO_EDGE;
291 DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(2, "TexParameteri", out.str()), true, TEST_LOCATION);
294 out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_T << ", " << GL_CLAMP_TO_EDGE;
295 DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(3, "TexParameteri", out.str()), true, TEST_LOCATION);
297 texParameterTrace.Reset();
298 texParameterTrace.Enable( true );
300 sampler.SetWrapMode( WrapMode::DEFAULT, WrapMode::DEFAULT );
302 // Flush the queue and render once
303 application.SendNotification();
304 application.Render();
306 texParameterTrace.Enable( false );
310 // Should not make any calls when settings are the same
311 DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 0, TEST_LOCATION);
313 //Todo: Test the other wrap mode ( REPEAT, MIRRORED_REPEAT ) , currently not support!!
318 int UtcSamplerSetWrapMode2(void)
320 TestApplication application;
322 // Create a cube-map texture.
323 unsigned int width = 8u;
324 unsigned int height = 8u;
325 Texture texture = Texture::New( TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height );
327 // Create source image data.
328 unsigned int bufferSize( width * height * 4 );
329 unsigned char* buffer= new unsigned char[ bufferSize ];
330 memset( buffer, 0u, bufferSize );
332 PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::DELETE_ARRAY );
334 // Upload the source image data to all 6 sides of our cube-map.
335 texture.Upload( pixelData, CubeMapLayer::POSITIVE_X, 0u, 0u, 0u, width, height );
336 texture.Upload( pixelData, CubeMapLayer::NEGATIVE_X, 0u, 0u, 0u, width, height );
337 texture.Upload( pixelData, CubeMapLayer::POSITIVE_Y, 0u, 0u, 0u, width, height );
338 texture.Upload( pixelData, CubeMapLayer::NEGATIVE_Y, 0u, 0u, 0u, width, height );
339 texture.Upload( pixelData, CubeMapLayer::POSITIVE_Z, 0u, 0u, 0u, width, height );
340 texture.Upload( pixelData, CubeMapLayer::NEGATIVE_Z, 0u, 0u, 0u, width, height );
342 // Finalize the cube-map setup.
343 TextureSet textureSet = TextureSet::New();
344 textureSet.SetTexture( 0u, texture );
346 Sampler sampler = Sampler::New();
347 textureSet.SetSampler( 0u, sampler );
349 Shader shader = CreateShader();
350 Geometry geometry = CreateQuadGeometry();
351 Renderer renderer = Renderer::New( geometry, shader );
352 renderer.SetTextures( textureSet );
354 Actor actor = Actor::New();
355 actor.AddRenderer(renderer);
356 actor.SetParentOrigin( ParentOrigin::CENTER );
357 actor.SetSize( 400, 400 );
358 Stage::GetCurrent().Add( actor );
360 TestGlAbstraction& gl = application.GetGlAbstraction();
362 application.SendNotification();
363 application.Render();
365 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
366 texParameterTrace.Reset();
367 texParameterTrace.Enable( true );
369 // Call the 3 dimensional wrap mode API.
370 sampler.SetWrapMode( WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE );
372 application.SendNotification();
373 application.Render();
375 // Verify that no TexParameteri calls occurred since wrap mode hasn't changed.
376 DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 0u, TEST_LOCATION );
379 sampler.SetWrapMode( WrapMode::REPEAT, WrapMode::REPEAT, WrapMode::REPEAT );
380 texParameterTrace.Reset();
381 application.SendNotification();
382 application.Render();
384 texParameterTrace.Enable( false );
386 // Verify that 3 TexParameteri calls occurred.
387 DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 3u, TEST_LOCATION );