1eada6c2b744042829413531d7709cd43e32fc40
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Texture.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <dali/public-api/dali-core.h>
19 #include <dali-test-suite-utils.h>
20 #include <test-native-image.h>
21
22 using namespace Dali;
23
24 #include <mesh-builder.h>
25
26 void texture_set_startup(void)
27 {
28   test_return_value = TET_UNDEF;
29 }
30
31 void texture_set_cleanup(void)
32 {
33   test_return_value = TET_PASS;
34 }
35
36 int UtcDaliTextureNew01(void)
37 {
38   TestApplication application;
39
40   unsigned int width(64);
41   unsigned int height(64);
42   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
43
44   DALI_TEST_CHECK( texture );
45   END_TEST;
46 }
47
48 int UtcDaliTextureNew02(void)
49 {
50   TestApplication application;
51   Texture texture;
52   DALI_TEST_CHECK( !texture );
53   END_TEST;
54 }
55
56 int UtcDaliTextureNew03(void)
57 {
58   TestApplication application;
59
60   // Create a native image source.
61   TestNativeImageNoExtPointer testNativeImage = TestNativeImageNoExt::New( 64u, 64u );
62
63   // Create a texture from the native image source.
64   Texture nativeTexture = Texture::New( *testNativeImage );
65
66   // Check the texture was created OK.
67   DALI_TEST_CHECK( nativeTexture );
68
69   END_TEST;
70 }
71
72 int UtcDaliTextureCopyConstructor(void)
73 {
74   TestApplication application;
75
76   unsigned int width(64);
77   unsigned int height(64);
78   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
79
80   Texture textureCopy(texture);
81
82   DALI_TEST_CHECK( textureCopy );
83
84   END_TEST;
85 }
86
87 int UtcDaliTextureAssignmentOperator(void)
88 {
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 );
93
94   Texture texture2;
95   DALI_TEST_CHECK( !texture2 );
96
97   texture2 = texture;
98   DALI_TEST_CHECK( texture2 );
99
100   END_TEST;
101 }
102
103 int UtcDaliTextureDownCast01(void)
104 {
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 );
109
110   BaseHandle handle(texture);
111   Texture texture2 = Texture::DownCast(handle);
112   DALI_TEST_CHECK( texture2 );
113
114   END_TEST;
115 }
116
117 int UtcDaliTextureDownCast02(void)
118 {
119   TestApplication application;
120
121   Handle handle = Handle::New(); // Create a custom object
122   Texture texture = Texture::DownCast(handle);
123   DALI_TEST_CHECK( !texture );
124   END_TEST;
125 }
126
127 int UtcDaliTextureUpload01(void)
128 {
129   TestApplication application;
130
131   //Create the texture
132   unsigned int width(64);
133   unsigned int height(64);
134   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
135
136   application.GetGlAbstraction().EnableTextureCallTrace(true);
137
138   application.SendNotification();
139   application.Render();
140
141   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
142
143   //TexImage2D should be called with a null pointer to reserve storage for the texture in the gpu
144   {
145     std::stringstream out;
146     out << GL_TEXTURE_2D <<", "<< 0u << ", " << width <<", "<< height;
147     DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
148   }
149
150   //Upload data to the texture
151   callStack.Reset();
152
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();
159
160   //TexImage2D should be called to upload the data
161   {
162     std::stringstream out;
163     out << GL_TEXTURE_2D <<", "<< 0u << ", " << width <<", "<< height;
164     DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
165   }
166
167   //Upload part of the texture
168   callStack.Reset();
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();
175
176   //TexSubImage2D should be called to upload the data
177   {
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() ) );
181   }
182
183
184   END_TEST;
185 }
186
187 int UtcDaliTextureUpload02(void)
188 {
189   TestApplication application;
190
191   //Create the texture
192   unsigned int width(64);
193   unsigned int height(64);
194   Texture texture = Texture::New( TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height );
195
196   application.GetGlAbstraction().EnableTextureCallTrace(true);
197
198   application.SendNotification();
199   application.Render();
200
201   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
202
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 )
205   {
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() ) );
209   }
210
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 );
214
215   //Upload data to the POSITIVE_X face of the texture
216   {
217     callStack.Reset();
218
219     texture.Upload( pixelData, CubeMapLayer::POSITIVE_X, 0u, 0u, 0u, width, height );
220     application.SendNotification();
221     application.Render();
222
223     //TexImage2D should be called to upload the data to the POSITIVE_X face
224     {
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() ) );
228     }
229   }
230
231   //Upload data to the NEGATIVE_X face of the texture
232   {
233     callStack.Reset();
234
235     texture.Upload( pixelData, CubeMapLayer::NEGATIVE_X, 0u, 0u, 0u, width, height );
236     application.SendNotification();
237     application.Render();
238
239     //TexImage2D should be called to upload the data to the NEGATIVE_X face
240     {
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() ) );
244     }
245   }
246
247   //Upload data to the POSITIVE_Y face of the texture
248   {
249     callStack.Reset();
250     texture.Upload( pixelData, CubeMapLayer::POSITIVE_Y, 0u, 0u, 0u, width, height );
251     application.SendNotification();
252     application.Render();
253
254     //TexImage2D should be called to upload the data to the POSITIVE_Y face
255     {
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() ) );
259     }
260   }
261
262   //Upload data to the NEGATIVE_Y face of the texture
263   {
264     callStack.Reset();
265     texture.Upload( pixelData, CubeMapLayer::NEGATIVE_Y, 0u, 0u, 0u, width, height );
266     application.SendNotification();
267     application.Render();
268
269     //TexImage2D should be called to upload the data to the NEGATIVE_Y face
270     {
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() ) );
274     }
275   }
276
277   //Upload data to the POSITIVE_Z face of the texture
278   {
279     callStack.Reset();
280     texture.Upload( pixelData, CubeMapLayer::POSITIVE_Z, 0u, 0u, 0u, width, height );
281     application.SendNotification();
282     application.Render();
283
284     //TexImage2D should be called to upload the data to the POSITIVE_Z face
285     {
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() ) );
289     }
290   }
291
292   //Upload data to the NEGATIVE_Z face of the texture
293   {
294     callStack.Reset();
295     texture.Upload( pixelData, CubeMapLayer::NEGATIVE_Z, 0u, 0u, 0u, width, height );
296     application.SendNotification();
297     application.Render();
298
299     //TexImage2D should be called to upload the data to the NEGATIVE_Z face
300     {
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() ) );
304     }
305   }
306
307   END_TEST;
308 }
309
310 int UtcDaliTextureUpload03(void)
311 {
312   TestApplication application;
313
314   //Create the texture
315   unsigned int width(64);
316   unsigned int height(64);
317   unsigned int widthMipmap1(32);
318   unsigned int heightMipmap1(32);
319
320   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
321
322   application.GetGlAbstraction().EnableTextureCallTrace(true);
323
324   application.SendNotification();
325   application.Render();
326
327   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
328
329   //TexImage2D should be called with a null pointer to reserve storage for the texture in the gpu
330   {
331     std::stringstream out;
332     out << GL_TEXTURE_2D <<", "<< 0u << ", " << width <<", "<< height;
333     DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
334   }
335
336   //Upload data to the texture mipmap 0 and mipmap 1
337   callStack.Reset();
338
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 );
343
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();
350
351   //TexImage2D should be called to upload the data to mipmaps 0 and 1
352   {
353     std::stringstream out;
354     out << GL_TEXTURE_2D <<", "<< 0u << ", " << width <<", "<< height;
355     DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
356   }
357   {
358     std::stringstream out;
359     out << GL_TEXTURE_2D <<", "<< 1u << ", " << widthMipmap1 <<", "<< heightMipmap1;
360     DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
361   }
362
363   END_TEST;
364 }
365
366 int UtcDaliTextureUpload04(void)
367 {
368   TestApplication application;
369
370   //Create the texture
371   unsigned int width(64);
372   unsigned int height(64);
373   unsigned int widthMipmap1(32);
374   unsigned int heightMipmap1(32);
375
376   Texture texture = Texture::New( TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height );
377
378   application.GetGlAbstraction().EnableTextureCallTrace(true);
379   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
380
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 );
386
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();
393
394   //TexImage2D should be called to upload the data to mipmaps 0 and 1
395   {
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() ) );
399   }
400   {
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() ) );
404   }
405
406   END_TEST;
407 }
408
409 int UtcDaliTextureUpload05(void)
410 {
411   TestApplication application;
412
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 );
417
418   application.GetGlAbstraction().EnableTextureCallTrace(true);
419
420   application.SendNotification();
421   application.Render();
422
423   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
424
425   //CompressedTexImage2D should be called with a null pointer to reserve storage for the texture in the gpu
426   {
427     std::stringstream out;
428     out << GL_TEXTURE_2D <<", "<< 0u << ", " << width <<", "<< height;
429     DALI_TEST_CHECK( callStack.FindMethodAndParams("CompressedTexImage2D", out.str().c_str() ) );
430   }
431
432   //Upload data to the texture
433   callStack.Reset();
434
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();
441
442   //CompressedTexImage2D should be called to upload the data
443   {
444     std::stringstream out;
445     out << GL_TEXTURE_2D <<", "<< 0u << ", " << width <<", "<< height;
446     DALI_TEST_CHECK( callStack.FindMethodAndParams("CompressedTexImage2D", out.str().c_str() ) );
447   }
448
449   //Upload part of the texture
450   callStack.Reset();
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();
457
458   //CompressedTexSubImage2D should be called to upload the data
459   {
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() ) );
463   }
464
465
466   END_TEST;
467 }
468
469 int UtcDaliTextureGenerateMipmaps(void)
470 {
471   TestApplication application;
472   unsigned int width(64);
473   unsigned int height(64);
474
475   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
476   texture.GenerateMipmaps();
477
478   Texture textureCubemap = Texture::New( TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height );
479   textureCubemap.GenerateMipmaps();
480
481   application.GetGlAbstraction().EnableTextureCallTrace(true);
482   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
483   application.SendNotification();
484   application.Render();
485
486   {
487     std::stringstream out;
488     out << GL_TEXTURE_2D;
489     DALI_TEST_CHECK( callStack.FindMethodAndParams("GenerateMipmap", out.str().c_str() ) );
490   }
491   {
492     std::stringstream out;
493     out << GL_TEXTURE_CUBE_MAP;
494     DALI_TEST_CHECK( callStack.FindMethodAndParams("GenerateMipmap", out.str().c_str() ) );
495   }
496
497   END_TEST;
498 }
499
500 int UtcDaliTextureGetWidth(void)
501 {
502   TestApplication application;
503   unsigned int width(64);
504   unsigned int height(64);
505
506   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
507   DALI_TEST_EQUALS( texture.GetWidth(), width, TEST_LOCATION );
508   END_TEST;
509 }
510
511 int UtcDaliTextureGetHeight(void)
512 {
513   TestApplication application;
514   unsigned int width(64);
515   unsigned int height(64);
516
517   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
518   DALI_TEST_EQUALS( texture.GetHeight(), height, TEST_LOCATION );
519
520   END_TEST;
521 }
522