Moved UTC tests for public API to dali directory
[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
21 using namespace Dali;
22
23 #include <mesh-builder.h>
24
25 void texture_set_startup(void)
26 {
27   test_return_value = TET_UNDEF;
28 }
29
30 void texture_set_cleanup(void)
31 {
32   test_return_value = TET_PASS;
33 }
34
35 int UtcDaliTextureNew01(void)
36 {
37   TestApplication application;
38
39   unsigned int width(64);
40   unsigned int height(64);
41   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
42
43   DALI_TEST_CHECK( texture );
44   END_TEST;
45 }
46
47 int UtcDaliTextureNew02(void)
48 {
49   TestApplication application;
50   Texture texture;
51   DALI_TEST_CHECK( !texture );
52   END_TEST;
53 }
54
55 int UtcDaliTextureCopyConstructor(void)
56 {
57   TestApplication application;
58
59   unsigned int width(64);
60   unsigned int height(64);
61   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
62
63   Texture textureCopy(texture);
64
65   DALI_TEST_CHECK( textureCopy );
66
67   END_TEST;
68 }
69
70 int UtcDaliTextureAssignmentOperator(void)
71 {
72   TestApplication application;
73   unsigned int width(64);
74   unsigned int height(64);
75   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
76
77   Texture texture2;
78   DALI_TEST_CHECK( !texture2 );
79
80   texture2 = texture;
81   DALI_TEST_CHECK( texture2 );
82
83   END_TEST;
84 }
85
86 int UtcDaliTextureDownCast01(void)
87 {
88   TestApplication application;
89   unsigned int width(64);
90   unsigned int height(64);
91   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
92
93   BaseHandle handle(texture);
94   Texture texture2 = Texture::DownCast(handle);
95   DALI_TEST_CHECK( texture2 );
96
97   END_TEST;
98 }
99
100 int UtcDaliTextureDownCast02(void)
101 {
102   TestApplication application;
103
104   Handle handle = Handle::New(); // Create a custom object
105   Texture texture = Texture::DownCast(handle);
106   DALI_TEST_CHECK( !texture );
107   END_TEST;
108 }
109
110 int UtcDaliTextureUpload01(void)
111 {
112   TestApplication application;
113
114   //Create the texture
115   unsigned int width(64);
116   unsigned int height(64);
117   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
118
119   application.GetGlAbstraction().EnableTextureCallTrace(true);
120
121   application.SendNotification();
122   application.Render();
123
124   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
125
126   //TexImage2D should be called with a null pointer to reserve storage for the texture in the gpu
127   {
128     std::stringstream out;
129     out << GL_TEXTURE_2D <<", "<< 0u << ", " << width <<", "<< height;
130     DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
131   }
132
133   //Upload data to the texture
134   callStack.Reset();
135
136   unsigned int bufferSize( width * height * 4 );
137   unsigned char* buffer= reinterpret_cast<unsigned char*>( malloc( bufferSize ) );
138   PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE );
139   texture.Upload( pixelData );
140   application.SendNotification();
141   application.Render();
142
143   //TexImage2D should be called to upload the data
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 part of the texture
151   callStack.Reset();
152   bufferSize =  width * height * 2;
153   buffer = reinterpret_cast<unsigned char*>( malloc( bufferSize ) );
154   PixelData pixelDataSubImage = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE );
155   texture.Upload( pixelDataSubImage, 0u, 0u, width/2, height/2, width/2, height/2 );
156   application.SendNotification();
157   application.Render();
158
159   //TexSubImage2D should be called to upload the data
160   {
161     std::stringstream out;
162     out << GL_TEXTURE_2D <<", "<< 0u << ", " << width/2 << ", " <<  height/2 << ", " << width/2 << ", " <<  height/2;
163     DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", out.str().c_str() ) );
164   }
165
166
167   END_TEST;
168 }
169
170 int UtcDaliTextureUpload02(void)
171 {
172   TestApplication application;
173
174   //Create the texture
175   unsigned int width(64);
176   unsigned int height(64);
177   Texture texture = Texture::New( TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height );
178
179   application.GetGlAbstraction().EnableTextureCallTrace(true);
180
181   application.SendNotification();
182   application.Render();
183
184   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
185
186   //TexImage2D should be called six times with a null pointer to reserve storage for the six textures of the cube map
187   for( unsigned int i(0); i<6; ++i )
188   {
189     std::stringstream out;
190     out << GL_TEXTURE_CUBE_MAP_POSITIVE_X + i <<", "<< 0u << ", " << width <<", "<< height;
191     DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
192   }
193
194   unsigned int bufferSize( width * height * 4 );
195   unsigned char* buffer= reinterpret_cast<unsigned char*>( malloc( bufferSize ) );
196   PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE );
197
198   //Upload data to the POSITIVE_X face of the texture
199   {
200     callStack.Reset();
201
202     texture.Upload( pixelData, CubeMapLayer::POSITIVE_X, 0u, 0u, 0u, width, height );
203     application.SendNotification();
204     application.Render();
205
206     //TexImage2D should be called to upload the data to the POSITIVE_X face
207     {
208       std::stringstream out;
209       out << GL_TEXTURE_CUBE_MAP_POSITIVE_X <<", "<< 0u << ", " << width <<", "<< height;
210       DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
211     }
212   }
213
214   //Upload data to the NEGATIVE_X face of the texture
215   {
216     callStack.Reset();
217
218     texture.Upload( pixelData, CubeMapLayer::NEGATIVE_X, 0u, 0u, 0u, width, height );
219     application.SendNotification();
220     application.Render();
221
222     //TexImage2D should be called to upload the data to the NEGATIVE_X face
223     {
224       std::stringstream out;
225       out << GL_TEXTURE_CUBE_MAP_NEGATIVE_X <<", "<< 0u << ", " << width <<", "<< height;
226       DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
227     }
228   }
229
230   //Upload data to the POSITIVE_Y face of the texture
231   {
232     callStack.Reset();
233     texture.Upload( pixelData, CubeMapLayer::POSITIVE_Y, 0u, 0u, 0u, width, height );
234     application.SendNotification();
235     application.Render();
236
237     //TexImage2D should be called to upload the data to the POSITIVE_Y face
238     {
239       std::stringstream out;
240       out << GL_TEXTURE_CUBE_MAP_POSITIVE_Y <<", "<< 0u << ", " << width <<", "<< height;
241       DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
242     }
243   }
244
245   //Upload data to the NEGATIVE_Y face of the texture
246   {
247     callStack.Reset();
248     texture.Upload( pixelData, CubeMapLayer::NEGATIVE_Y, 0u, 0u, 0u, width, height );
249     application.SendNotification();
250     application.Render();
251
252     //TexImage2D should be called to upload the data to the NEGATIVE_Y face
253     {
254       std::stringstream out;
255       out << GL_TEXTURE_CUBE_MAP_NEGATIVE_Y <<", "<< 0u << ", " << width <<", "<< height;
256       DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
257     }
258   }
259
260   //Upload data to the POSITIVE_Z face of the texture
261   {
262     callStack.Reset();
263     texture.Upload( pixelData, CubeMapLayer::POSITIVE_Z, 0u, 0u, 0u, width, height );
264     application.SendNotification();
265     application.Render();
266
267     //TexImage2D should be called to upload the data to the POSITIVE_Z face
268     {
269       std::stringstream out;
270       out << GL_TEXTURE_CUBE_MAP_POSITIVE_Z <<", "<< 0u << ", " << width <<", "<< height;
271       DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
272     }
273   }
274
275   //Upload data to the NEGATIVE_Z face of the texture
276   {
277     callStack.Reset();
278     texture.Upload( pixelData, CubeMapLayer::NEGATIVE_Z, 0u, 0u, 0u, width, height );
279     application.SendNotification();
280     application.Render();
281
282     //TexImage2D should be called to upload the data to the NEGATIVE_Z face
283     {
284       std::stringstream out;
285       out << GL_TEXTURE_CUBE_MAP_NEGATIVE_Z <<", "<< 0u << ", " << width <<", "<< height;
286       DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
287     }
288   }
289
290   END_TEST;
291 }
292
293 int UtcDaliTextureUpload03(void)
294 {
295   TestApplication application;
296
297   //Create the texture
298   unsigned int width(64);
299   unsigned int height(64);
300   unsigned int widthMipmap1(32);
301   unsigned int heightMipmap1(32);
302
303   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
304
305   application.GetGlAbstraction().EnableTextureCallTrace(true);
306
307   application.SendNotification();
308   application.Render();
309
310   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
311
312   //TexImage2D should be called with a null pointer to reserve storage for the texture in the gpu
313   {
314     std::stringstream out;
315     out << GL_TEXTURE_2D <<", "<< 0u << ", " << width <<", "<< height;
316     DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
317   }
318
319   //Upload data to the texture mipmap 0 and mipmap 1
320   callStack.Reset();
321
322   unsigned int bufferSize( width * height * 4 );
323   unsigned char* buffer= reinterpret_cast<unsigned char*>( malloc(  bufferSize ) );
324   PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE );
325   texture.Upload( pixelData, 0u, 0u, 0u, 0u, width, height );
326
327   bufferSize = widthMipmap1 * heightMipmap1 * 4;
328   buffer = reinterpret_cast<unsigned char*>( malloc( bufferSize ) );
329   PixelData pixelDataMipmap1 = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE );
330   texture.Upload( pixelDataMipmap1, 0u, 1u, 0u, 0u, widthMipmap1, heightMipmap1 );
331   application.SendNotification();
332   application.Render();
333
334   //TexImage2D should be called to upload the data to mipmaps 0 and 1
335   {
336     std::stringstream out;
337     out << GL_TEXTURE_2D <<", "<< 0u << ", " << width <<", "<< height;
338     DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
339   }
340   {
341     std::stringstream out;
342     out << GL_TEXTURE_2D <<", "<< 1u << ", " << widthMipmap1 <<", "<< heightMipmap1;
343     DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
344   }
345
346   END_TEST;
347 }
348
349 int UtcDaliTextureUpload04(void)
350 {
351   TestApplication application;
352
353   //Create the texture
354   unsigned int width(64);
355   unsigned int height(64);
356   unsigned int widthMipmap1(32);
357   unsigned int heightMipmap1(32);
358
359   Texture texture = Texture::New( TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height );
360
361   application.GetGlAbstraction().EnableTextureCallTrace(true);
362   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
363
364   //Upload data to the NEGATIVE_X face mipmap 0 and mipmap 1
365   unsigned int bufferSize( width * height * 4 );
366   unsigned char* buffer= reinterpret_cast<unsigned char*>( malloc( bufferSize ) );
367   PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE );
368   texture.Upload( pixelData, CubeMapLayer::NEGATIVE_X, 0u, 0u, 0u, width, height );
369
370   bufferSize = widthMipmap1 * heightMipmap1 * 4;
371   buffer = reinterpret_cast<unsigned char*>( malloc( bufferSize ) );
372   PixelData pixelDataMipmap1 = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE );
373   texture.Upload( pixelDataMipmap1, CubeMapLayer::NEGATIVE_X, 1u, 0u, 0u, widthMipmap1, heightMipmap1 );
374   application.SendNotification();
375   application.Render();
376
377   //TexImage2D should be called to upload the data to mipmaps 0 and 1
378   {
379     std::stringstream out;
380     out << GL_TEXTURE_CUBE_MAP_NEGATIVE_X <<", "<< 0u << ", " << width <<", "<< height;
381     DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
382   }
383   {
384     std::stringstream out;
385     out << GL_TEXTURE_CUBE_MAP_NEGATIVE_X <<", "<< 1u << ", " << widthMipmap1 <<", "<< heightMipmap1;
386     DALI_TEST_CHECK( callStack.FindMethodAndParams("TexImage2D", out.str().c_str() ) );
387   }
388
389   END_TEST;
390 }
391
392 int UtcDaliTextureUpload05(void)
393 {
394   TestApplication application;
395
396   //Create a texture with a compressed format
397   unsigned int width(64);
398   unsigned int height(64);
399   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::COMPRESSED_RGBA_ASTC_4x4_KHR, width, height );
400
401   application.GetGlAbstraction().EnableTextureCallTrace(true);
402
403   application.SendNotification();
404   application.Render();
405
406   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
407
408   //CompressedTexImage2D should be called with a null pointer to reserve storage for the texture in the gpu
409   {
410     std::stringstream out;
411     out << GL_TEXTURE_2D <<", "<< 0u << ", " << width <<", "<< height;
412     DALI_TEST_CHECK( callStack.FindMethodAndParams("CompressedTexImage2D", out.str().c_str() ) );
413   }
414
415   //Upload data to the texture
416   callStack.Reset();
417
418   unsigned int bufferSize( width * height * 4 );
419   unsigned char* buffer= reinterpret_cast<unsigned char*>( malloc( bufferSize ) );
420   PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::COMPRESSED_RGBA_ASTC_4x4_KHR, PixelData::FREE );
421   texture.Upload( pixelData );
422   application.SendNotification();
423   application.Render();
424
425   //CompressedTexImage2D should be called to upload the data
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 part of the texture
433   callStack.Reset();
434   bufferSize =  width * height * 2;
435   buffer = reinterpret_cast<unsigned char*>( malloc( bufferSize ) );
436   PixelData pixelDataSubImage = PixelData::New( buffer, bufferSize, width, height, Pixel::COMPRESSED_RGBA_ASTC_4x4_KHR, PixelData::FREE );
437   texture.Upload( pixelDataSubImage, 0u, 0u, width/2, height/2, width/2, height/2 );
438   application.SendNotification();
439   application.Render();
440
441   //CompressedTexSubImage2D should be called to upload the data
442   {
443     std::stringstream out;
444     out << GL_TEXTURE_2D <<", "<< 0u << ", " << width/2 << ", " <<  height/2 << ", " << width/2 << ", " <<  height/2;
445     DALI_TEST_CHECK( callStack.FindMethodAndParams("CompressedTexSubImage2D", out.str().c_str() ) );
446   }
447
448
449   END_TEST;
450 }
451
452 int UtcDaliTextureGenerateMipmaps(void)
453 {
454   TestApplication application;
455   unsigned int width(64);
456   unsigned int height(64);
457
458   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
459   texture.GenerateMipmaps();
460
461   Texture textureCubemap = Texture::New( TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height );
462   textureCubemap.GenerateMipmaps();
463
464   application.GetGlAbstraction().EnableTextureCallTrace(true);
465   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
466   application.SendNotification();
467   application.Render();
468
469   {
470     std::stringstream out;
471     out << GL_TEXTURE_2D;
472     DALI_TEST_CHECK( callStack.FindMethodAndParams("GenerateMipmap", out.str().c_str() ) );
473   }
474   {
475     std::stringstream out;
476     out << GL_TEXTURE_CUBE_MAP;
477     DALI_TEST_CHECK( callStack.FindMethodAndParams("GenerateMipmap", out.str().c_str() ) );
478   }
479
480   END_TEST;
481 }
482
483 int UtcDaliTextureGetWidth(void)
484 {
485   TestApplication application;
486   unsigned int width(64);
487   unsigned int height(64);
488
489   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
490   DALI_TEST_EQUALS( texture.GetWidth(), width, TEST_LOCATION );
491   END_TEST;
492 }
493
494 int UtcDaliTextureGetHeight(void)
495 {
496   TestApplication application;
497   unsigned int width(64);
498   unsigned int height(64);
499
500   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
501   DALI_TEST_EQUALS( texture.GetHeight(), height, TEST_LOCATION );
502
503   END_TEST;
504 }
505