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 three calls to TexParameteri when the texture is first created
158 // Texture mag filter is not called as the first time set it uses the system default
159 DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 3, TEST_LOCATION);
161 std::stringstream out;
162 out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_LINEAR;
163 DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(2, "TexParameteri", out.str()), true, TEST_LOCATION);
165 /**************************************************************/
167 texParameterTrace.Reset();
168 texParameterTrace.Enable( true );
170 sampler.SetFilterMode( FilterMode::DEFAULT, FilterMode::DEFAULT );
172 // Flush the queue and render once
173 application.SendNotification();
174 application.Render();
176 texParameterTrace.Enable( false );
180 // Should not make any calls when settings are the same
181 DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 0, TEST_LOCATION);
183 /**************************************************************/
185 texParameterTrace.Reset();
186 texParameterTrace.Enable( true );
188 sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
190 // Flush the queue and render once
191 application.SendNotification();
192 application.Render();
194 texParameterTrace.Enable( false );
196 // Verify actor gl state
197 DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 2, TEST_LOCATION);
200 out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_NEAREST;
201 DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
204 out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MAG_FILTER << ", " << GL_NEAREST;
205 DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(1, "TexParameteri", out.str()), true, TEST_LOCATION);
208 /**************************************************************/
210 texParameterTrace.Reset();
211 texParameterTrace.Enable( true );
213 sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::LINEAR );
215 // Flush the queue and render once
216 application.SendNotification();
217 application.Render();
219 texParameterTrace.Enable( false );
221 // Verify actor gl state
222 DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 1, TEST_LOCATION);
225 out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MAG_FILTER << ", " << GL_LINEAR;
226 DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
228 /**************************************************************/
230 texParameterTrace.Reset();
231 texParameterTrace.Enable( true );
233 sampler.SetFilterMode( FilterMode::NONE, FilterMode::NONE );
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" ), 1, TEST_LOCATION);
245 out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_NEAREST_MIPMAP_LINEAR;
246 DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
251 int UtcSamplerSetWrapMode1(void)
253 TestApplication application;
255 Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
256 TextureSet textureSet = CreateTextureSet();
257 Sampler sampler = Sampler::New();
258 TextureSetImage( textureSet, 0u, image );
259 textureSet.SetSampler( 0u, sampler );
261 Shader shader = CreateShader();
262 Geometry geometry = CreateQuadGeometry();
263 Renderer renderer = Renderer::New( geometry, shader );
264 renderer.SetTextures( textureSet );
266 Actor actor = Actor::New();
267 actor.AddRenderer(renderer);
268 actor.SetParentOrigin( ParentOrigin::CENTER );
269 actor.SetSize(400, 400);
270 Stage::GetCurrent().Add( actor );
272 TestGlAbstraction& gl = application.GetGlAbstraction();
274 //****************************************
275 // CLAMP_TO_EDGE / CLAMP_TO_EDGE
276 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
277 texParameterTrace.Reset();
278 texParameterTrace.Enable( true );
280 application.SendNotification();
281 application.Render();
283 texParameterTrace.Enable( false );
287 // There are three calls to TexParameteri when the texture is first created
288 // Texture mag filter is not called as the first time set it uses the system default
289 DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 3, TEST_LOCATION);
291 std::stringstream out;
292 out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_S << ", " << GL_CLAMP_TO_EDGE;
293 DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
296 out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_T << ", " << GL_CLAMP_TO_EDGE;
297 DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(1, "TexParameteri", out.str()), true, TEST_LOCATION);
299 texParameterTrace.Reset();
300 texParameterTrace.Enable( true );
302 sampler.SetWrapMode( WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE );
304 // Flush the queue and render once
305 application.SendNotification();
306 application.Render();
308 texParameterTrace.Enable( false );
312 // Should not make any calls when settings are the same
313 DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 0, TEST_LOCATION);
315 //Todo: Test the other wrap mode ( REPEAT, MIRRORED_REPEAT ) , currently not support!!
320 int UtcSamplerSetWrapMode2(void)
322 TestApplication application;
324 // Create a cube-map texture.
325 unsigned int width = 8u;
326 unsigned int height = 8u;
327 Texture texture = Texture::New( TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height );
329 // Create source image data.
330 unsigned int bufferSize( width * height * 4 );
331 unsigned char* buffer= new unsigned char[ bufferSize ];
332 memset( buffer, 0u, bufferSize );
334 PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::DELETE_ARRAY );
336 // Upload the source image data to all 6 sides of our cube-map.
337 texture.Upload( pixelData, CubeMapLayer::POSITIVE_X, 0u, 0u, 0u, width, height );
338 texture.Upload( pixelData, CubeMapLayer::NEGATIVE_X, 0u, 0u, 0u, width, height );
339 texture.Upload( pixelData, CubeMapLayer::POSITIVE_Y, 0u, 0u, 0u, width, height );
340 texture.Upload( pixelData, CubeMapLayer::NEGATIVE_Y, 0u, 0u, 0u, width, height );
341 texture.Upload( pixelData, CubeMapLayer::POSITIVE_Z, 0u, 0u, 0u, width, height );
342 texture.Upload( pixelData, CubeMapLayer::NEGATIVE_Z, 0u, 0u, 0u, width, height );
344 // Finalize the cube-map setup.
345 TextureSet textureSet = TextureSet::New();
346 textureSet.SetTexture( 0u, texture );
348 Sampler sampler = Sampler::New();
349 textureSet.SetSampler( 0u, sampler );
351 Shader shader = CreateShader();
352 Geometry geometry = CreateQuadGeometry();
353 Renderer renderer = Renderer::New( geometry, shader );
354 renderer.SetTextures( textureSet );
356 Actor actor = Actor::New();
357 actor.AddRenderer(renderer);
358 actor.SetParentOrigin( ParentOrigin::CENTER );
359 actor.SetSize( 400, 400 );
360 Stage::GetCurrent().Add( actor );
362 TestGlAbstraction& gl = application.GetGlAbstraction();
364 application.SendNotification();
365 application.Render();
367 TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
368 texParameterTrace.Reset();
369 texParameterTrace.Enable( true );
371 // Call the 3 dimensional wrap mode API.
372 sampler.SetWrapMode( WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE );
374 application.SendNotification();
375 application.Render();
377 texParameterTrace.Enable( false );
379 // Verify that 3 TexParameteri calls occurred.
380 DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 3u, TEST_LOCATION );