2 * Copyright (c) 2015 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.
18 #include <dali/public-api/dali-core.h>
19 #include <dali-test-suite-utils.h>
20 #include <test-native-image.h>
24 #include <mesh-builder.h>
26 void texture_set_startup(void)
28 test_return_value = TET_UNDEF;
31 void texture_set_cleanup(void)
33 test_return_value = TET_PASS;
36 int UtcDaliTextureNew01(void)
38 TestApplication application;
40 unsigned int width(64);
41 unsigned int height(64);
42 Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
44 DALI_TEST_CHECK( texture );
48 int UtcDaliTextureNew02(void)
50 TestApplication application;
52 DALI_TEST_CHECK( !texture );
56 int UtcDaliTextureNew03(void)
58 TestApplication application;
60 // Create a native image source.
61 TestNativeImageNoExtPointer testNativeImage = TestNativeImageNoExt::New( 64u, 64u );
63 // Create a texture from the native image source.
64 Texture nativeTexture = Texture::New( *testNativeImage );
66 // Check the texture was created OK.
67 DALI_TEST_CHECK( nativeTexture );
72 int UtcDaliTextureCopyConstructor(void)
74 TestApplication application;
76 unsigned int width(64);
77 unsigned int height(64);
78 Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
80 Texture textureCopy(texture);
82 DALI_TEST_CHECK( textureCopy );
87 int UtcDaliTextureAssignmentOperator(void)
89 TestApplication application;
90 unsigned int width(64);
91 unsigned int height(64);
92 Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
95 DALI_TEST_CHECK( !texture2 );
98 DALI_TEST_CHECK( texture2 );
103 int UtcDaliTextureDownCast01(void)
105 TestApplication application;
106 unsigned int width(64);
107 unsigned int height(64);
108 Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
110 BaseHandle handle(texture);
111 Texture texture2 = Texture::DownCast(handle);
112 DALI_TEST_CHECK( texture2 );
117 int UtcDaliTextureDownCast02(void)
119 TestApplication application;
121 Handle handle = Handle::New(); // Create a custom object
122 Texture texture = Texture::DownCast(handle);
123 DALI_TEST_CHECK( !texture );
127 int UtcDaliTextureUpload01(void)
129 TestApplication application;
132 unsigned int width(64);
133 unsigned int height(64);
134 Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
136 application.GetGlAbstraction().EnableTextureCallTrace(true);
138 application.SendNotification();
139 application.Render();
141 TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
143 //TexImage2D should be called with a null pointer to reserve storage for the texture in the gpu
145 std::stringstream out;
146 out << GL_TEXTURE_2D <<", "<< 0u << ", " << width <<", "<< height;
147 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
150 //Upload data to the texture
153 unsigned int bufferSize( width * height * 4 );
154 unsigned char* buffer= reinterpret_cast<unsigned char*>( malloc( bufferSize ) );
155 PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE );
156 texture.Upload( pixelData );
157 application.SendNotification();
158 application.Render();
160 //TexImage2D should be called to upload the data
162 std::stringstream out;
163 out << GL_TEXTURE_2D <<", "<< 0u << ", " << width <<", "<< height;
164 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
167 //Upload part of the texture
169 bufferSize = width * height * 2;
170 buffer = reinterpret_cast<unsigned char*>( malloc( bufferSize ) );
171 PixelData pixelDataSubImage = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE );
172 texture.Upload( pixelDataSubImage, 0u, 0u, width/2, height/2, width/2, height/2 );
173 application.SendNotification();
174 application.Render();
176 //TexSubImage2D should be called to upload the data
178 std::stringstream out;
179 out << GL_TEXTURE_2D <<", "<< 0u << ", " << width/2 << ", " << height/2 << ", " << width/2 << ", " << height/2;
180 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", out.str().c_str() ) );
187 int UtcDaliTextureUpload02(void)
189 TestApplication application;
192 unsigned int width(64);
193 unsigned int height(64);
194 Texture texture = Texture::New( TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height );
196 application.GetGlAbstraction().EnableTextureCallTrace(true);
198 application.SendNotification();
199 application.Render();
201 TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
203 //TexImage2D should be called six times with a null pointer to reserve storage for the six textures of the cube map
204 for( unsigned int i(0); i<6; ++i )
206 std::stringstream out;
207 out << GL_TEXTURE_CUBE_MAP_POSITIVE_X + i <<", "<< 0u << ", " << width <<", "<< height;
208 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
211 unsigned int bufferSize( width * height * 4 );
212 unsigned char* buffer= reinterpret_cast<unsigned char*>( malloc( bufferSize ) );
213 PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE );
215 //Upload data to the POSITIVE_X face of the texture
219 texture.Upload( pixelData, CubeMapLayer::POSITIVE_X, 0u, 0u, 0u, width, height );
220 application.SendNotification();
221 application.Render();
223 //TexImage2D should be called to upload the data to the POSITIVE_X face
225 std::stringstream out;
226 out << GL_TEXTURE_CUBE_MAP_POSITIVE_X <<", "<< 0u << ", " << width <<", "<< height;
227 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
231 //Upload data to the NEGATIVE_X face of the texture
235 texture.Upload( pixelData, CubeMapLayer::NEGATIVE_X, 0u, 0u, 0u, width, height );
236 application.SendNotification();
237 application.Render();
239 //TexImage2D should be called to upload the data to the NEGATIVE_X face
241 std::stringstream out;
242 out << GL_TEXTURE_CUBE_MAP_NEGATIVE_X <<", "<< 0u << ", " << width <<", "<< height;
243 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
247 //Upload data to the POSITIVE_Y face of the texture
250 texture.Upload( pixelData, CubeMapLayer::POSITIVE_Y, 0u, 0u, 0u, width, height );
251 application.SendNotification();
252 application.Render();
254 //TexImage2D should be called to upload the data to the POSITIVE_Y face
256 std::stringstream out;
257 out << GL_TEXTURE_CUBE_MAP_POSITIVE_Y <<", "<< 0u << ", " << width <<", "<< height;
258 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
262 //Upload data to the NEGATIVE_Y face of the texture
265 texture.Upload( pixelData, CubeMapLayer::NEGATIVE_Y, 0u, 0u, 0u, width, height );
266 application.SendNotification();
267 application.Render();
269 //TexImage2D should be called to upload the data to the NEGATIVE_Y face
271 std::stringstream out;
272 out << GL_TEXTURE_CUBE_MAP_NEGATIVE_Y <<", "<< 0u << ", " << width <<", "<< height;
273 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
277 //Upload data to the POSITIVE_Z face of the texture
280 texture.Upload( pixelData, CubeMapLayer::POSITIVE_Z, 0u, 0u, 0u, width, height );
281 application.SendNotification();
282 application.Render();
284 //TexImage2D should be called to upload the data to the POSITIVE_Z face
286 std::stringstream out;
287 out << GL_TEXTURE_CUBE_MAP_POSITIVE_Z <<", "<< 0u << ", " << width <<", "<< height;
288 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
292 //Upload data to the NEGATIVE_Z face of the texture
295 texture.Upload( pixelData, CubeMapLayer::NEGATIVE_Z, 0u, 0u, 0u, width, height );
296 application.SendNotification();
297 application.Render();
299 //TexImage2D should be called to upload the data to the NEGATIVE_Z face
301 std::stringstream out;
302 out << GL_TEXTURE_CUBE_MAP_NEGATIVE_Z <<", "<< 0u << ", " << width <<", "<< height;
303 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
310 int UtcDaliTextureUpload03(void)
312 TestApplication application;
315 unsigned int width(64);
316 unsigned int height(64);
317 unsigned int widthMipmap1(32);
318 unsigned int heightMipmap1(32);
320 Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
322 application.GetGlAbstraction().EnableTextureCallTrace(true);
324 application.SendNotification();
325 application.Render();
327 TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
329 //TexImage2D should be called with a null pointer to reserve storage for the texture in the gpu
331 std::stringstream out;
332 out << GL_TEXTURE_2D <<", "<< 0u << ", " << width <<", "<< height;
333 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
336 //Upload data to the texture mipmap 0 and mipmap 1
339 unsigned int bufferSize( width * height * 4 );
340 unsigned char* buffer= reinterpret_cast<unsigned char*>( malloc( bufferSize ) );
341 PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE );
342 texture.Upload( pixelData, 0u, 0u, 0u, 0u, width, height );
344 bufferSize = widthMipmap1 * heightMipmap1 * 4;
345 buffer = reinterpret_cast<unsigned char*>( malloc( bufferSize ) );
346 PixelData pixelDataMipmap1 = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE );
347 texture.Upload( pixelDataMipmap1, 0u, 1u, 0u, 0u, widthMipmap1, heightMipmap1 );
348 application.SendNotification();
349 application.Render();
351 //TexImage2D should be called to upload the data to mipmaps 0 and 1
353 std::stringstream out;
354 out << GL_TEXTURE_2D <<", "<< 0u << ", " << width <<", "<< height;
355 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
358 std::stringstream out;
359 out << GL_TEXTURE_2D <<", "<< 1u << ", " << widthMipmap1 <<", "<< heightMipmap1;
360 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
366 int UtcDaliTextureUpload04(void)
368 TestApplication application;
371 unsigned int width(64);
372 unsigned int height(64);
373 unsigned int widthMipmap1(32);
374 unsigned int heightMipmap1(32);
376 Texture texture = Texture::New( TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height );
378 application.GetGlAbstraction().EnableTextureCallTrace(true);
379 TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
381 //Upload data to the NEGATIVE_X face mipmap 0 and mipmap 1
382 unsigned int bufferSize( width * height * 4 );
383 unsigned char* buffer= reinterpret_cast<unsigned char*>( malloc( bufferSize ) );
384 PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE );
385 texture.Upload( pixelData, CubeMapLayer::NEGATIVE_X, 0u, 0u, 0u, width, height );
387 bufferSize = widthMipmap1 * heightMipmap1 * 4;
388 buffer = reinterpret_cast<unsigned char*>( malloc( bufferSize ) );
389 PixelData pixelDataMipmap1 = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE );
390 texture.Upload( pixelDataMipmap1, CubeMapLayer::NEGATIVE_X, 1u, 0u, 0u, widthMipmap1, heightMipmap1 );
391 application.SendNotification();
392 application.Render();
394 //TexImage2D should be called to upload the data to mipmaps 0 and 1
396 std::stringstream out;
397 out << GL_TEXTURE_CUBE_MAP_NEGATIVE_X <<", "<< 0u << ", " << width <<", "<< height;
398 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
401 std::stringstream out;
402 out << GL_TEXTURE_CUBE_MAP_NEGATIVE_X <<", "<< 1u << ", " << widthMipmap1 <<", "<< heightMipmap1;
403 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
409 int UtcDaliTextureUpload05(void)
411 TestApplication application;
413 //Create a texture with a compressed format
414 unsigned int width(64);
415 unsigned int height(64);
416 Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::COMPRESSED_RGBA_ASTC_4x4_KHR, width, height );
418 application.GetGlAbstraction().EnableTextureCallTrace(true);
420 application.SendNotification();
421 application.Render();
423 TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
425 //CompressedTexImage2D should be called with a null pointer to reserve storage for the texture in the gpu
427 std::stringstream out;
428 out << GL_TEXTURE_2D <<", "<< 0u << ", " << width <<", "<< height;
429 DALI_TEST_CHECK( callStack.FindMethodAndParams("CompressedTexImage2D", out.str().c_str() ) );
432 //Upload data to the texture
435 unsigned int bufferSize( width * height * 4 );
436 unsigned char* buffer= reinterpret_cast<unsigned char*>( malloc( bufferSize ) );
437 PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::COMPRESSED_RGBA_ASTC_4x4_KHR, PixelData::FREE );
438 texture.Upload( pixelData );
439 application.SendNotification();
440 application.Render();
442 //CompressedTexImage2D should be called to upload the data
444 std::stringstream out;
445 out << GL_TEXTURE_2D <<", "<< 0u << ", " << width <<", "<< height;
446 DALI_TEST_CHECK( callStack.FindMethodAndParams("CompressedTexImage2D", out.str().c_str() ) );
449 //Upload part of the texture
451 bufferSize = width * height * 2;
452 buffer = reinterpret_cast<unsigned char*>( malloc( bufferSize ) );
453 PixelData pixelDataSubImage = PixelData::New( buffer, bufferSize, width, height, Pixel::COMPRESSED_RGBA_ASTC_4x4_KHR, PixelData::FREE );
454 texture.Upload( pixelDataSubImage, 0u, 0u, width/2, height/2, width/2, height/2 );
455 application.SendNotification();
456 application.Render();
458 //CompressedTexSubImage2D should be called to upload the data
460 std::stringstream out;
461 out << GL_TEXTURE_2D <<", "<< 0u << ", " << width/2 << ", " << height/2 << ", " << width/2 << ", " << height/2;
462 DALI_TEST_CHECK( callStack.FindMethodAndParams("CompressedTexSubImage2D", out.str().c_str() ) );
469 int UtcDaliTextureGenerateMipmaps(void)
471 TestApplication application;
472 unsigned int width(64);
473 unsigned int height(64);
475 Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
476 texture.GenerateMipmaps();
478 Texture textureCubemap = Texture::New( TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height );
479 textureCubemap.GenerateMipmaps();
481 application.GetGlAbstraction().EnableTextureCallTrace(true);
482 TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
483 application.SendNotification();
484 application.Render();
487 std::stringstream out;
488 out << GL_TEXTURE_2D;
489 DALI_TEST_CHECK( callStack.FindMethodAndParams("GenerateMipmap", out.str().c_str() ) );
492 std::stringstream out;
493 out << GL_TEXTURE_CUBE_MAP;
494 DALI_TEST_CHECK( callStack.FindMethodAndParams("GenerateMipmap", out.str().c_str() ) );
500 int UtcDaliTextureGetWidth(void)
502 TestApplication application;
503 unsigned int width(64);
504 unsigned int height(64);
506 Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
507 DALI_TEST_EQUALS( texture.GetWidth(), width, TEST_LOCATION );
511 int UtcDaliTextureGetHeight(void)
513 TestApplication application;
514 unsigned int width(64);
515 unsigned int height(64);
517 Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
518 DALI_TEST_EQUALS( texture.GetHeight(), height, TEST_LOCATION );