Add CHROMINANCE pixel format
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Texture.cpp
1 /*
2  * Copyright (c) 2022 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-test-suite-utils.h>
19 #include <dali/devel-api/images/pixel-data-devel.h>
20 #include <dali/devel-api/rendering/texture-devel.h>
21 #include <dali/public-api/dali-core.h>
22 #include <test-native-image.h>
23
24 using namespace Dali;
25
26 #include <mesh-builder.h>
27
28 void texture_set_startup(void)
29 {
30   test_return_value = TET_UNDEF;
31 }
32
33 void texture_set_cleanup(void)
34 {
35   test_return_value = TET_PASS;
36 }
37
38 int UtcDaliTextureNew01(void)
39 {
40   TestApplication application;
41
42   unsigned int width(64);
43   unsigned int height(64);
44   Texture      texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
45
46   DALI_TEST_CHECK(texture);
47   END_TEST;
48 }
49
50 int UtcDaliTextureNew02(void)
51 {
52   TestApplication application;
53   Texture         texture;
54   DALI_TEST_CHECK(!texture);
55   END_TEST;
56 }
57
58 int UtcDaliTextureNew03(void)
59 {
60   TestApplication application;
61
62   // Create a native image source.
63   TestNativeImagePointer testNativeImage = TestNativeImage::New(64u, 64u);
64
65   // Create a texture from the native image source.
66   Texture nativeTexture = Texture::New(*testNativeImage);
67
68   // Check the texture was created OK.
69   DALI_TEST_CHECK(nativeTexture);
70
71   END_TEST;
72 }
73
74 int UtcDaliTextureCopyConstructor(void)
75 {
76   TestApplication application;
77
78   unsigned int width(64);
79   unsigned int height(64);
80   Texture      texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
81
82   Texture textureCopy(texture);
83
84   DALI_TEST_CHECK(textureCopy);
85
86   END_TEST;
87 }
88
89 int UtcDaliTextureAssignmentOperator(void)
90 {
91   TestApplication application;
92   unsigned int    width(64);
93   unsigned int    height(64);
94   Texture         texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
95
96   Texture texture2;
97   DALI_TEST_CHECK(!texture2);
98
99   texture2 = texture;
100   DALI_TEST_CHECK(texture2);
101
102   END_TEST;
103 }
104
105 int UtcDaliTextureMoveConstructor(void)
106 {
107   TestApplication application;
108
109   uint32_t width   = 64;
110   uint32_t height  = 64;
111   Texture  texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
112   DALI_TEST_CHECK(texture);
113   DALI_TEST_EQUALS(1, texture.GetBaseObject().ReferenceCount(), TEST_LOCATION);
114   DALI_TEST_EQUALS(texture.GetWidth(), width, TEST_LOCATION);
115   DALI_TEST_EQUALS(texture.GetHeight(), height, TEST_LOCATION);
116
117   Texture move = std::move(texture);
118   DALI_TEST_CHECK(move);
119   DALI_TEST_EQUALS(1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION);
120   DALI_TEST_EQUALS(move.GetWidth(), width, TEST_LOCATION);
121   DALI_TEST_EQUALS(move.GetHeight(), height, TEST_LOCATION);
122   DALI_TEST_CHECK(!texture);
123
124   END_TEST;
125 }
126
127 int UtcDaliTextureMoveAssignment(void)
128 {
129   TestApplication application;
130
131   uint32_t width   = 64;
132   uint32_t height  = 64;
133   Texture  texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
134   DALI_TEST_CHECK(texture);
135   DALI_TEST_EQUALS(1, texture.GetBaseObject().ReferenceCount(), TEST_LOCATION);
136   DALI_TEST_EQUALS(texture.GetWidth(), width, TEST_LOCATION);
137   DALI_TEST_EQUALS(texture.GetHeight(), height, TEST_LOCATION);
138
139   Texture move;
140   move = std::move(texture);
141   DALI_TEST_CHECK(move);
142   DALI_TEST_EQUALS(1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION);
143   DALI_TEST_EQUALS(move.GetWidth(), width, TEST_LOCATION);
144   DALI_TEST_EQUALS(move.GetHeight(), height, TEST_LOCATION);
145   DALI_TEST_CHECK(!texture);
146
147   END_TEST;
148 }
149
150 int UtcDaliTextureDownCast01(void)
151 {
152   TestApplication application;
153   unsigned int    width(64);
154   unsigned int    height(64);
155   Texture         texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
156
157   BaseHandle handle(texture);
158   Texture    texture2 = Texture::DownCast(handle);
159   DALI_TEST_CHECK(texture2);
160
161   END_TEST;
162 }
163
164 int UtcDaliTextureDownCast02(void)
165 {
166   TestApplication application;
167
168   Handle  handle  = Handle::New(); // Create a custom object
169   Texture texture = Texture::DownCast(handle);
170   DALI_TEST_CHECK(!texture);
171   END_TEST;
172 }
173
174 int UtcDaliTextureUpload01(void)
175 {
176   TestApplication application;
177
178   //Create the texture
179   unsigned int width(64);
180   unsigned int height(64);
181   Texture      texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
182
183   application.GetGlAbstraction().EnableTextureCallTrace(true);
184
185   application.SendNotification();
186   application.Render();
187
188   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
189
190   //Upload data to the texture
191   callStack.Reset();
192
193   unsigned int   bufferSize(width * height * 4);
194   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
195   PixelData      pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE);
196   texture.Upload(pixelData);
197   application.SendNotification();
198   application.Render();
199
200   //TexImage2D should be called to upload the data
201   {
202     std::stringstream out;
203     out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
204     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
205   }
206
207   //Upload part of the texture
208   callStack.Reset();
209   bufferSize                  = width * height * 2;
210   buffer                      = reinterpret_cast<unsigned char*>(malloc(bufferSize));
211   PixelData pixelDataSubImage = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE);
212   texture.Upload(pixelDataSubImage, 0u, 0u, width / 2, height / 2, width / 2, height / 2);
213   application.SendNotification();
214   application.Render();
215
216   //TexSubImage2D should be called to upload the data
217   {
218     std::stringstream out;
219     out << GL_TEXTURE_2D << ", " << 0u << ", " << width / 2 << ", " << height / 2 << ", " << width / 2 << ", " << height / 2;
220     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexSubImage2D", out.str().c_str()));
221   }
222
223   END_TEST;
224 }
225
226 int UtcDaliTextureUpload02(void)
227 {
228   TestApplication application;
229
230   //Create the texture
231   unsigned int width(64);
232   unsigned int height(64);
233   Texture      texture = CreateTexture(TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height);
234
235   application.GetGlAbstraction().EnableTextureCallTrace(true);
236
237   application.SendNotification();
238   application.Render();
239
240   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
241
242   tet_infoline("TexImage2D should be called six times with a null pointer to reserve storage for the six textures of the cube map");
243   for(unsigned int i(0); i < 6; ++i)
244   {
245     std::stringstream out;
246     out << GL_TEXTURE_CUBE_MAP_POSITIVE_X + i << ", " << 0u << ", " << width << ", " << height;
247     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
248   }
249
250   unsigned int   bufferSize(width * height * 4);
251   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
252   PixelData      pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE);
253
254   //Upload data to the POSITIVE_X face of the texture
255   {
256     callStack.Reset();
257
258     texture.Upload(pixelData, CubeMapLayer::POSITIVE_X, 0u, 0u, 0u, width, height);
259     application.SendNotification();
260     application.Render();
261
262     //TexImage2D should be called to upload the data to the POSITIVE_X face
263     {
264       std::stringstream out;
265       out << GL_TEXTURE_CUBE_MAP_POSITIVE_X << ", " << 0u << ", " << width << ", " << height;
266       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
267     }
268   }
269
270   //Upload data to the NEGATIVE_X face of the texture
271   {
272     callStack.Reset();
273
274     texture.Upload(pixelData, CubeMapLayer::NEGATIVE_X, 0u, 0u, 0u, width, height);
275     application.SendNotification();
276     application.Render();
277
278     //TexImage2D should be called to upload the data to the NEGATIVE_X face
279     {
280       std::stringstream out;
281       out << GL_TEXTURE_CUBE_MAP_NEGATIVE_X << ", " << 0u << ", " << width << ", " << height;
282       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
283     }
284   }
285
286   //Upload data to the POSITIVE_Y face of the texture
287   {
288     callStack.Reset();
289     texture.Upload(pixelData, CubeMapLayer::POSITIVE_Y, 0u, 0u, 0u, width, height);
290     application.SendNotification();
291     application.Render();
292
293     //TexImage2D should be called to upload the data to the POSITIVE_Y face
294     {
295       std::stringstream out;
296       out << GL_TEXTURE_CUBE_MAP_POSITIVE_Y << ", " << 0u << ", " << width << ", " << height;
297       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
298     }
299   }
300
301   //Upload data to the NEGATIVE_Y face of the texture
302   {
303     callStack.Reset();
304     texture.Upload(pixelData, CubeMapLayer::NEGATIVE_Y, 0u, 0u, 0u, width, height);
305     application.SendNotification();
306     application.Render();
307
308     //TexImage2D should be called to upload the data to the NEGATIVE_Y face
309     {
310       std::stringstream out;
311       out << GL_TEXTURE_CUBE_MAP_NEGATIVE_Y << ", " << 0u << ", " << width << ", " << height;
312       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
313     }
314   }
315
316   //Upload data to the POSITIVE_Z face of the texture
317   {
318     callStack.Reset();
319     texture.Upload(pixelData, CubeMapLayer::POSITIVE_Z, 0u, 0u, 0u, width, height);
320     application.SendNotification();
321     application.Render();
322
323     //TexImage2D should be called to upload the data to the POSITIVE_Z face
324     {
325       std::stringstream out;
326       out << GL_TEXTURE_CUBE_MAP_POSITIVE_Z << ", " << 0u << ", " << width << ", " << height;
327       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
328     }
329   }
330
331   //Upload data to the NEGATIVE_Z face of the texture
332   {
333     callStack.Reset();
334     texture.Upload(pixelData, CubeMapLayer::NEGATIVE_Z, 0u, 0u, 0u, width, height);
335     application.SendNotification();
336     application.Render();
337
338     //TexImage2D should be called to upload the data to the NEGATIVE_Z face
339     {
340       std::stringstream out;
341       out << GL_TEXTURE_CUBE_MAP_NEGATIVE_Z << ", " << 0u << ", " << width << ", " << height;
342       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
343     }
344   }
345
346   END_TEST;
347 }
348
349 int UtcDaliTextureUpload03(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 = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
360
361   application.GetGlAbstraction().EnableTextureCallTrace(true);
362
363   application.SendNotification();
364   application.Render();
365
366   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
367
368   tet_infoline("TexImage2D should be called with a null pointer to reserve storage for the texture in the gpu");
369   {
370     std::stringstream out;
371     out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
372     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
373   }
374
375   //Upload data to the texture mipmap 0 and mipmap 1
376   callStack.Reset();
377
378   unsigned int   bufferSize(width * height * 4);
379   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
380   PixelData      pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE);
381   texture.Upload(pixelData, 0u, 0u, 0u, 0u, width, height);
382
383   bufferSize                 = widthMipmap1 * heightMipmap1 * 4;
384   buffer                     = reinterpret_cast<unsigned char*>(malloc(bufferSize));
385   PixelData pixelDataMipmap1 = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE);
386   texture.Upload(pixelDataMipmap1, 0u, 1u, 0u, 0u, widthMipmap1, heightMipmap1);
387   application.SendNotification();
388   application.Render();
389
390   //TexImage2D should be called to upload the data to mipmaps 0 and 1
391   {
392     std::stringstream out;
393     out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
394     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
395   }
396   {
397     std::stringstream out;
398     out << GL_TEXTURE_2D << ", " << 1u << ", " << widthMipmap1 << ", " << heightMipmap1;
399     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
400   }
401
402   END_TEST;
403 }
404
405 int UtcDaliTextureUpload04(void)
406 {
407   TestApplication application;
408
409   //Create the texture
410   unsigned int width(64);
411   unsigned int height(64);
412   unsigned int widthMipmap1(32);
413   unsigned int heightMipmap1(32);
414
415   Texture texture = CreateTexture(TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height);
416
417   application.GetGlAbstraction().EnableTextureCallTrace(true);
418   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
419
420   //Upload data to the NEGATIVE_X face mipmap 0 and mipmap 1
421   unsigned int   bufferSize(width * height * 4);
422   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
423   PixelData      pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE);
424   texture.Upload(pixelData, CubeMapLayer::NEGATIVE_X, 0u, 0u, 0u, width, height);
425
426   bufferSize                 = widthMipmap1 * heightMipmap1 * 4;
427   buffer                     = reinterpret_cast<unsigned char*>(malloc(bufferSize));
428   PixelData pixelDataMipmap1 = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE);
429   texture.Upload(pixelDataMipmap1, CubeMapLayer::NEGATIVE_X, 1u, 0u, 0u, widthMipmap1, heightMipmap1);
430   application.SendNotification();
431   application.Render();
432
433   //TexImage2D should be called to upload the data to mipmaps 0 and 1
434   {
435     std::stringstream out;
436     out << GL_TEXTURE_CUBE_MAP_NEGATIVE_X << ", " << 0u << ", " << width << ", " << height;
437     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
438   }
439   {
440     std::stringstream out;
441     out << GL_TEXTURE_CUBE_MAP_NEGATIVE_X << ", " << 1u << ", " << widthMipmap1 << ", " << heightMipmap1;
442     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
443   }
444
445   END_TEST;
446 }
447
448 int UtcDaliTextureUpload05(void)
449 {
450   Pixel::Format COMPRESSED_PIXEL_FORMATS[] =
451     {
452       Pixel::COMPRESSED_R11_EAC,
453       Pixel::COMPRESSED_SIGNED_R11_EAC,
454       Pixel::COMPRESSED_RG11_EAC,
455       Pixel::COMPRESSED_SIGNED_RG11_EAC,
456       Pixel::COMPRESSED_RGB8_ETC2,
457       Pixel::COMPRESSED_SRGB8_ETC2,
458       Pixel::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2,
459       Pixel::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2,
460       Pixel::COMPRESSED_RGBA8_ETC2_EAC,
461       Pixel::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC,
462       Pixel::COMPRESSED_RGB8_ETC1,
463       Pixel::COMPRESSED_RGB_PVRTC_4BPPV1,
464       Pixel::COMPRESSED_RGBA_ASTC_4x4_KHR,
465       Pixel::COMPRESSED_RGBA_ASTC_5x4_KHR,
466       Pixel::COMPRESSED_RGBA_ASTC_5x5_KHR,
467       Pixel::COMPRESSED_RGBA_ASTC_6x5_KHR,
468       Pixel::COMPRESSED_RGBA_ASTC_6x6_KHR,
469       Pixel::COMPRESSED_RGBA_ASTC_8x5_KHR,
470       Pixel::COMPRESSED_RGBA_ASTC_8x6_KHR,
471       Pixel::COMPRESSED_RGBA_ASTC_8x8_KHR,
472       Pixel::COMPRESSED_RGBA_ASTC_10x5_KHR,
473       Pixel::COMPRESSED_RGBA_ASTC_10x6_KHR,
474       Pixel::COMPRESSED_RGBA_ASTC_10x8_KHR,
475       Pixel::COMPRESSED_RGBA_ASTC_10x10_KHR,
476       Pixel::COMPRESSED_RGBA_ASTC_12x10_KHR,
477       Pixel::COMPRESSED_RGBA_ASTC_12x12_KHR,
478       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR,
479       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR,
480       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR,
481       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR,
482       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR,
483       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR,
484       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR,
485       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR,
486       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR,
487       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR,
488       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR,
489       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR,
490       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR,
491       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR,
492     };
493   const unsigned int NUMBER_OF_COMPRESSED_PIXEL_FORMATS = sizeof(COMPRESSED_PIXEL_FORMATS) / sizeof(Pixel::Format);
494
495   for(unsigned int index = 0; index < NUMBER_OF_COMPRESSED_PIXEL_FORMATS; ++index)
496   {
497     TestApplication application;
498
499     //Create a texture with a compressed format
500     unsigned int width(64);
501     unsigned int height(64);
502     Texture      texture = CreateTexture(TextureType::TEXTURE_2D, COMPRESSED_PIXEL_FORMATS[index], width, height);
503
504     application.GetGlAbstraction().EnableTextureCallTrace(true);
505
506     application.SendNotification();
507     application.Render();
508
509     TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
510
511     tet_infoline("CompressedTexImage2D should be called with a null pointer to reserve storage for the texture in the gpu");
512     {
513       std::stringstream out;
514       out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
515       DALI_TEST_CHECK(callStack.FindMethodAndParams("CompressedTexImage2D", out.str().c_str()));
516     }
517
518     //Upload data to the texture
519     callStack.Reset();
520
521     unsigned int   bufferSize(width * height * 4);
522     unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
523     PixelData      pixelData = PixelData::New(buffer, bufferSize, width, height, COMPRESSED_PIXEL_FORMATS[index], PixelData::FREE);
524     texture.Upload(pixelData);
525     application.SendNotification();
526     application.Render();
527
528     //CompressedTexImage2D should be called to upload the data
529     {
530       std::stringstream out;
531       out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
532       DALI_TEST_CHECK(callStack.FindMethodAndParams("CompressedTexImage2D", out.str().c_str()));
533     }
534
535     //Upload part of the texture
536     callStack.Reset();
537     bufferSize                  = width * height * 2;
538     buffer                      = reinterpret_cast<unsigned char*>(malloc(bufferSize));
539     PixelData pixelDataSubImage = PixelData::New(buffer, bufferSize, width, height, COMPRESSED_PIXEL_FORMATS[index], PixelData::FREE);
540     texture.Upload(pixelDataSubImage, 0u, 0u, width / 2, height / 2, width / 2, height / 2);
541     application.SendNotification();
542     application.Render();
543
544     //CompressedTexSubImage2D should be called to upload the data
545     {
546       std::stringstream out;
547       out << GL_TEXTURE_2D << ", " << 0u << ", " << width / 2 << ", " << height / 2 << ", " << width / 2 << ", " << height / 2;
548       DALI_TEST_CHECK(callStack.FindMethodAndParams("CompressedTexSubImage2D", out.str().c_str()));
549     }
550
551     application.GetGlAbstraction().ResetTextureCallStack();
552   }
553
554   END_TEST;
555 }
556
557 int UtcDaliTextureUpload06(void)
558 {
559   TestApplication application;
560
561   //Create the texture
562   unsigned int width(64);
563   unsigned int height(64);
564   tet_infoline("Creating a Texure with an alpha channel");
565   Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
566
567   application.GetGlAbstraction().EnableTextureCallTrace(true);
568
569   application.SendNotification();
570   application.Render();
571
572   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
573
574   tet_infoline("TexImage2D should be called with a null pointer to reserve storage for the texture in the gpu");
575   {
576     std::stringstream out;
577     out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
578     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
579   }
580
581   tet_infoline("Upload data to the texture");
582   callStack.Reset();
583
584   tet_infoline("Creating a RGB pixel buffer and adding that to the texture to ensure it is handled correctly");
585   unsigned int   bufferSize(width * height * 3);
586   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
587   PixelData      pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGB888, PixelData::FREE);
588   texture.Upload(pixelData);
589   application.SendNotification();
590   application.Render();
591
592   tet_infoline("TexImage2D should be called to upload the data");
593   {
594     std::stringstream out;
595     out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
596     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
597   }
598
599   END_TEST;
600 }
601
602 int UtcDaliTextureUpload07(void)
603 {
604   Pixel::Format FLOATING_POINT_PIXEL_FORMATS[] =
605     {
606       Pixel::RGB16F,
607       Pixel::RGB32F,
608     };
609   const unsigned int NUMBER_OF_FLOATING_POINT_PIXEL_FORMATS = sizeof(FLOATING_POINT_PIXEL_FORMATS) / sizeof(Pixel::Format);
610
611   for(unsigned int index = 0; index < NUMBER_OF_FLOATING_POINT_PIXEL_FORMATS; ++index)
612   {
613     TestApplication application;
614
615     //Create the texture
616     unsigned int width(64);
617     unsigned int height(64);
618     tet_infoline("Creating a floating point texture");
619     Texture texture = CreateTexture(TextureType::TEXTURE_2D, FLOATING_POINT_PIXEL_FORMATS[index], width, height);
620
621     application.GetGlAbstraction().EnableTextureCallTrace(true);
622
623     application.SendNotification();
624     application.Render();
625
626     TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
627
628     tet_infoline("TexImage2D should be called with a null pointer to reserve storage for the texture in the gpu");
629     {
630       std::stringstream out;
631       out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
632       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
633     }
634
635     tet_infoline("Upload data to the texture");
636     callStack.Reset();
637
638     tet_infoline("Creating a RGB pixel buffer and adding that to the texture to ensure it is handled correctly");
639     unsigned int   bufferSize(width * height * 3);
640     unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
641     PixelData      pixelData = PixelData::New(buffer, bufferSize, width, height, FLOATING_POINT_PIXEL_FORMATS[index], PixelData::FREE);
642     texture.Upload(pixelData);
643     application.SendNotification();
644     application.Render();
645
646     tet_infoline("TexImage2D should be called to upload the data");
647     {
648       std::stringstream out;
649       out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
650       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
651     }
652   }
653
654   END_TEST;
655 }
656
657 int UtcDaliTextureUploadPixelFormats(void)
658 {
659   TestApplication application;
660   application.GetGlAbstraction().EnableTextureCallTrace(true);
661
662   //Create the texture
663   unsigned int width(64);
664   unsigned int height(64);
665
666   std::vector<Pixel::Format> formats =
667     {
668       Pixel::A8,
669       Pixel::L8,
670       Pixel::LA88,
671       Pixel::RGB565,
672       Pixel::BGR565,
673       Pixel::RGBA4444,
674       Pixel::BGRA4444,
675       Pixel::RGBA5551,
676       Pixel::BGRA5551,
677       Pixel::RGB888,
678       Pixel::RGB8888,
679       Pixel::BGR8888,
680       Pixel::RGBA8888,
681       Pixel::BGRA8888,
682       Pixel::DEPTH_UNSIGNED_INT,
683       Pixel::DEPTH_FLOAT,
684       Pixel::DEPTH_STENCIL,
685       Pixel::RGB16F,
686       Pixel::RGB32F,
687       Pixel::R11G11B10F,
688       Pixel::CHROMINANCE_U,
689       Pixel::CHROMINANCE_V};
690
691   for(auto format : formats)
692   {
693     tet_infoline("Creating a Texure with a new or recent format");
694     Texture texture = CreateTexture(TextureType::TEXTURE_2D, format, width, height);
695
696     application.SendNotification();
697     application.Render();
698
699     TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
700
701     tet_infoline("TexImage2D should be called twice per texture");
702     DALI_TEST_EQUALS(callStack.CountMethod("TexImage2D"), 2, TEST_LOCATION);
703     {
704       std::stringstream out;
705       out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
706       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
707     }
708     callStack.Reset();
709   }
710
711   END_TEST;
712 }
713
714 int UtcDaliTextureUploadSmallerThanSize(void)
715 {
716   TestApplication application;
717
718   //Create the texture
719   unsigned int width(64);
720   unsigned int height(64);
721   Texture      texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
722
723   application.GetGlAbstraction().EnableTextureCallTrace(true);
724
725   application.SendNotification();
726   application.Render();
727
728   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
729   callStack.EnableLogging(true);
730   TraceCallStack& texParamCallStack = application.GetGlAbstraction().GetTexParameterTrace();
731   texParamCallStack.EnableLogging(true);
732
733   tet_infoline("TexImage2D should be called with a null pointer to reserve storage for the texture in the gpu");
734   {
735     std::stringstream out;
736     out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
737     std::string params;
738     DALI_TEST_CHECK(callStack.FindMethodAndGetParameters("TexImage2D", params));
739     DALI_TEST_EQUALS(out.str(), params, TEST_LOCATION);
740   }
741
742   //Upload data to the texture
743   callStack.Reset();
744
745   unsigned int   bufferSize(width * height * 4);
746   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
747   PixelData      pixelData = PixelData::New(buffer, bufferSize, width / 2, height / 2, Pixel::RGBA8888, PixelData::FREE);
748   texture.Upload(pixelData);
749   application.SendNotification();
750   application.Render();
751
752   //TexSubImage2D should be called to upload the data
753   {
754     std::stringstream out;
755     out << GL_TEXTURE_2D << ", " << 0u << ", " << 0u << ", " << 0u << ", " << width / 2 << ", " << height / 2;
756     std::string params;
757     DALI_TEST_CHECK(callStack.FindMethodAndGetParameters("TexSubImage2D", params));
758     DALI_TEST_EQUALS(out.str(), params, TEST_LOCATION);
759   }
760   END_TEST;
761 }
762
763 int UtcDaliTextureGenerateMipmaps(void)
764 {
765   TestApplication application;
766   unsigned int    width(64);
767   unsigned int    height(64);
768
769   Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
770   texture.GenerateMipmaps();
771
772   Texture textureCubemap = CreateTexture(TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height);
773   textureCubemap.GenerateMipmaps();
774
775   application.GetGlAbstraction().EnableTextureCallTrace(true);
776   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
777   application.SendNotification();
778   application.Render();
779
780   {
781     std::stringstream out;
782     out << GL_TEXTURE_2D;
783     DALI_TEST_CHECK(callStack.FindMethodAndParams("GenerateMipmap", out.str().c_str()));
784   }
785   {
786     std::stringstream out;
787     out << GL_TEXTURE_CUBE_MAP;
788     DALI_TEST_CHECK(callStack.FindMethodAndParams("GenerateMipmap", out.str().c_str()));
789   }
790
791   END_TEST;
792 }
793
794 int UtcDaliTextureGetWidth(void)
795 {
796   TestApplication application;
797   unsigned int    width(64);
798   unsigned int    height(64);
799
800   Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
801   DALI_TEST_EQUALS(texture.GetWidth(), width, TEST_LOCATION);
802   END_TEST;
803 }
804
805 int UtcDaliTextureGetHeight(void)
806 {
807   TestApplication application;
808   unsigned int    width(64);
809   unsigned int    height(64);
810
811   Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
812   DALI_TEST_EQUALS(texture.GetHeight(), height, TEST_LOCATION);
813
814   END_TEST;
815 }
816
817 int UtcDaliTextureContextLoss(void)
818 {
819   tet_infoline("UtcDaliTextureContextLoss\n");
820   TestApplication application;
821
822   //Create the texture
823   unsigned int width(64);
824   unsigned int height(64);
825   Texture      texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
826   DALI_TEST_CHECK(texture);
827
828   application.SendNotification();
829   application.Render(16);
830
831   // Lose & regain context (in render 'thread')
832   application.ResetContext();
833   DALI_TEST_CHECK(texture);
834
835   END_TEST;
836 }
837
838 int UtcDaliNativeImageTexture01(void)
839 {
840   TestApplication application;
841   tet_infoline("UtcDaliNativeImageTexture01");
842
843   TestNativeImagePointer imageInterface = TestNativeImage::New(16, 16);
844   {
845     Texture texture = Texture::New(*(imageInterface.Get()));
846     Actor   actor   = CreateRenderableActor(texture, "", "");
847     application.GetScene().Add(actor);
848
849     DALI_TEST_CHECK(texture);
850
851     application.SendNotification();
852     application.Render(16);
853
854     DALI_TEST_EQUALS(imageInterface->mExtensionCreateCalls, 1, TEST_LOCATION);
855     DALI_TEST_EQUALS(imageInterface->mExtensionDestroyCalls, 0, TEST_LOCATION);
856     DALI_TEST_EQUALS(actor.GetProperty(Actor::Property::SIZE), Property::Value(Vector3(16, 16, 0)), TEST_LOCATION);
857
858     UnparentAndReset(actor);
859
860     application.SendNotification();
861     application.Render(16);
862   }
863   application.SendNotification();
864   application.Render(16);
865
866   DALI_TEST_EQUALS(imageInterface->mExtensionCreateCalls, 1, TEST_LOCATION);
867   DALI_TEST_EQUALS(imageInterface->mExtensionDestroyCalls, 1, TEST_LOCATION);
868
869   END_TEST;
870 }
871
872 int UtcDaliNativeImageTexture02(void)
873 {
874   TestApplication application;
875   tet_infoline("UtcDaliNativeImageTexture02 - test error on TargetTexture");
876
877   TestNativeImagePointer imageInterface = TestNativeImage::New(16, 16);
878   imageInterface->mTargetTextureError   = 1u;
879   {
880     Texture texture = Texture::New(*(imageInterface.Get()));
881     Actor   actor   = CreateRenderableActor(texture, "", "");
882     application.GetScene().Add(actor);
883
884     DALI_TEST_CHECK(texture);
885
886     application.SendNotification();
887     application.Render(16);
888
889     // Expect 2 attempts to create the texture - once when adding the texture
890     // to the scene-graph, and again since that failed, during the Bind.
891     // The second one succeeds (TargetTexture only errors once)
892     DALI_TEST_EQUALS(imageInterface->mExtensionCreateCalls, 2, TEST_LOCATION);
893     DALI_TEST_EQUALS(imageInterface->mExtensionDestroyCalls, 1, TEST_LOCATION);
894
895     UnparentAndReset(actor);
896
897     application.SendNotification();
898     application.Render(16);
899   }
900   application.SendNotification();
901   application.Render(16);
902
903   // Expect that there are no further calls to create/destroy resource
904   DALI_TEST_EQUALS(imageInterface->mExtensionCreateCalls, 2, TEST_LOCATION);
905   DALI_TEST_EQUALS(imageInterface->mExtensionDestroyCalls, 2, TEST_LOCATION);
906
907   END_TEST;
908 }
909
910 int UtcDaliTextureGenerateMipmapsNegative(void)
911 {
912   TestApplication application;
913   Dali::Texture   instance;
914   try
915   {
916     instance.GenerateMipmaps();
917     DALI_TEST_CHECK(false); // Should not get here
918   }
919   catch(...)
920   {
921     DALI_TEST_CHECK(true); // We expect an assert
922   }
923   END_TEST;
924 }
925
926 int UtcDaliTextureUploadNegative01(void)
927 {
928   TestApplication application;
929   Dali::Texture   instance;
930   try
931   {
932     Dali::PixelData arg1;
933     instance.Upload(arg1);
934     DALI_TEST_CHECK(false); // Should not get here
935   }
936   catch(...)
937   {
938     DALI_TEST_CHECK(true); // We expect an assert
939   }
940   END_TEST;
941 }
942
943 int UtcDaliTextureUploadNegative02(void)
944 {
945   TestApplication application;
946   Dali::Texture   instance;
947   try
948   {
949     Dali::PixelData arg1;
950     unsigned int    arg2(0u);
951     unsigned int    arg3(0u);
952     unsigned int    arg4(0u);
953     unsigned int    arg5(0u);
954     unsigned int    arg6(0u);
955     unsigned int    arg7(0u);
956     instance.Upload(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
957     DALI_TEST_CHECK(false); // Should not get here
958   }
959   catch(...)
960   {
961     DALI_TEST_CHECK(true); // We expect an assert
962   }
963   END_TEST;
964 }
965
966 int UtcDaliTextureGetWidthNegative(void)
967 {
968   TestApplication application;
969   Dali::Texture   instance;
970   try
971   {
972     instance.GetWidth();
973     DALI_TEST_CHECK(false); // Should not get here
974   }
975   catch(...)
976   {
977     DALI_TEST_CHECK(true); // We expect an assert
978   }
979   END_TEST;
980 }
981
982 int UtcDaliTextureGetHeightNegative(void)
983 {
984   TestApplication application;
985   Dali::Texture   instance;
986   try
987   {
988     instance.GetHeight();
989     DALI_TEST_CHECK(false); // Should not get here
990   }
991   catch(...)
992   {
993     DALI_TEST_CHECK(true); // We expect an assert
994   }
995   END_TEST;
996 }
997
998 int UtcDaliTextureCheckNativeP(void)
999 {
1000   TestApplication        application;
1001   TestNativeImagePointer testNativeImage = TestNativeImage::New(64u, 64u);
1002   Texture                nativeTexture   = Texture::New(*testNativeImage);
1003
1004   DALI_TEST_CHECK(nativeTexture);
1005   DALI_TEST_CHECK(DevelTexture::IsNative(nativeTexture));
1006   END_TEST;
1007 }
1008
1009 int UtcDaliTextureCheckNativeN1(void)
1010 {
1011   TestApplication application;
1012   unsigned int    width(64);
1013   unsigned int    height(64);
1014   Texture         texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
1015
1016   DALI_TEST_CHECK(texture);
1017   DALI_TEST_CHECK(!DevelTexture::IsNative(texture));
1018   END_TEST;
1019 }
1020
1021 int UtcDaliTextureCheckNativeN2(void)
1022 {
1023   TestApplication application;
1024   Texture         texture;
1025   try
1026   {
1027     bool native = DevelTexture::IsNative(texture);
1028     DALI_TEST_CHECK(native != native);
1029   }
1030   catch(...)
1031   {
1032     DALI_TEST_CHECK(true);
1033   }
1034   END_TEST;
1035 }
1036
1037 int UtcDaliTextureApplyFragShaderP1(void)
1038 {
1039   TestApplication        application;
1040   TestNativeImagePointer testNativeImage = TestNativeImage::New(64u, 64u);
1041   Texture                nativeTexture   = Texture::New(*testNativeImage);
1042   DALI_TEST_CHECK(nativeTexture);
1043
1044   const std::string baseFragShader =
1045     "varying mediump vec4 uColor;\n"
1046     "void main(){\n"
1047     "  gl_FragColor=uColor;\n"
1048     "}\n";
1049   std::string fragShader = baseFragShader;
1050   bool        applied    = DevelTexture::ApplyNativeFragmentShader(nativeTexture, fragShader);
1051
1052   std::string fragPrefix = "#extension GL_OES_EGL_image_external:require\n";
1053
1054   DALI_TEST_CHECK(applied);
1055   DALI_TEST_CHECK(baseFragShader.compare(fragShader));
1056   DALI_TEST_CHECK(fragShader.compare(fragPrefix + baseFragShader) == 0);
1057   DALI_TEST_CHECK(!fragShader.empty());
1058   END_TEST;
1059 }
1060
1061 int UtcDaliTextureApplyFragShaderP2(void)
1062 {
1063   TestApplication        application;
1064   TestNativeImagePointer testNativeImage = TestNativeImage::New(64u, 64u);
1065   Texture                nativeTexture   = Texture::New(*testNativeImage);
1066   DALI_TEST_CHECK(nativeTexture);
1067
1068   const std::string baseFragShader =
1069     "varying mediump vec4 uColor;\n"
1070     "varying vec2 vTexCoord;\n"
1071     "uniform sampler2D uNative;\n"
1072     "void main(){\n"
1073     "  gl_FragColor=uColor*texture2D(uNative, vTexCoord);\n"
1074     "}\n";
1075   std::string fragShader = baseFragShader;
1076   bool        applied    = DevelTexture::ApplyNativeFragmentShader(nativeTexture, fragShader);
1077
1078   DALI_TEST_CHECK(applied);
1079   DALI_TEST_CHECK(baseFragShader.compare(fragShader));
1080   DALI_TEST_CHECK(!fragShader.empty());
1081   DALI_TEST_CHECK(fragShader.find("samplerExternalOES") < fragShader.length());
1082   END_TEST;
1083 }
1084
1085 int UtcDaliTextureApplyFragShaderN1(void)
1086 {
1087   TestApplication        application;
1088   TestNativeImagePointer testNativeImage = TestNativeImage::New(64u, 64u);
1089   Texture                nativeTexture   = Texture::New(*testNativeImage);
1090   DALI_TEST_CHECK(nativeTexture);
1091
1092   std::string fragShader;
1093   bool        applied = DevelTexture::ApplyNativeFragmentShader(nativeTexture, fragShader);
1094
1095   DALI_TEST_CHECK(!applied);
1096   DALI_TEST_CHECK(fragShader.empty());
1097   END_TEST;
1098 }
1099
1100 int UtcDaliTextureApplyFragShaderN2(void)
1101 {
1102   TestApplication application;
1103   unsigned int    width(64);
1104   unsigned int    height(64);
1105   Texture         texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
1106
1107   const std::string baseFragShader =
1108     "varying mediump vec4 uColor;\n"
1109     "void main(){\n"
1110     "  gl_FragColor=uColor;\n"
1111     "}\n";
1112   std::string fragShader = baseFragShader;
1113   bool        applied    = DevelTexture::ApplyNativeFragmentShader(texture, fragShader);
1114
1115   DALI_TEST_CHECK(!applied);
1116   DALI_TEST_CHECK(!baseFragShader.compare(fragShader));
1117   END_TEST;
1118 }