Merge branch 'devel/master' into tizen
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Texture.cpp
1 /*
2  * Copyright (c) 2020 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 = Texture::New(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   //TexImage2D should be called with a null pointer to reserve storage for the texture in the gpu
191   {
192     std::stringstream out;
193     out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
194     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
195   }
196
197   //Upload data to the texture
198   callStack.Reset();
199
200   unsigned int   bufferSize(width * height * 4);
201   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
202   PixelData      pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE);
203   texture.Upload(pixelData);
204   application.SendNotification();
205   application.Render();
206
207   //TexImage2D should be called to upload the data
208   {
209     std::stringstream out;
210     out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
211     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
212   }
213
214   //Upload part of the texture
215   callStack.Reset();
216   bufferSize                  = width * height * 2;
217   buffer                      = reinterpret_cast<unsigned char*>(malloc(bufferSize));
218   PixelData pixelDataSubImage = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE);
219   texture.Upload(pixelDataSubImage, 0u, 0u, width / 2, height / 2, width / 2, height / 2);
220   application.SendNotification();
221   application.Render();
222
223   //TexSubImage2D should be called to upload the data
224   {
225     std::stringstream out;
226     out << GL_TEXTURE_2D << ", " << 0u << ", " << width / 2 << ", " << height / 2 << ", " << width / 2 << ", " << height / 2;
227     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexSubImage2D", out.str().c_str()));
228   }
229
230   END_TEST;
231 }
232
233 int UtcDaliTextureUpload02(void)
234 {
235   TestApplication application;
236
237   //Create the texture
238   unsigned int width(64);
239   unsigned int height(64);
240   Texture      texture = Texture::New(TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height);
241
242   application.GetGlAbstraction().EnableTextureCallTrace(true);
243
244   application.SendNotification();
245   application.Render();
246
247   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
248
249   //TexImage2D should be called six times with a null pointer to reserve storage for the six textures of the cube map
250   for(unsigned int i(0); i < 6; ++i)
251   {
252     std::stringstream out;
253     out << GL_TEXTURE_CUBE_MAP_POSITIVE_X + i << ", " << 0u << ", " << width << ", " << height;
254     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
255   }
256
257   unsigned int   bufferSize(width * height * 4);
258   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
259   PixelData      pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE);
260
261   //Upload data to the POSITIVE_X face of the texture
262   {
263     callStack.Reset();
264
265     texture.Upload(pixelData, CubeMapLayer::POSITIVE_X, 0u, 0u, 0u, width, height);
266     application.SendNotification();
267     application.Render();
268
269     //TexImage2D should be called to upload the data to the POSITIVE_X face
270     {
271       std::stringstream out;
272       out << GL_TEXTURE_CUBE_MAP_POSITIVE_X << ", " << 0u << ", " << width << ", " << height;
273       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
274     }
275   }
276
277   //Upload data to the NEGATIVE_X face of the texture
278   {
279     callStack.Reset();
280
281     texture.Upload(pixelData, CubeMapLayer::NEGATIVE_X, 0u, 0u, 0u, width, height);
282     application.SendNotification();
283     application.Render();
284
285     //TexImage2D should be called to upload the data to the NEGATIVE_X face
286     {
287       std::stringstream out;
288       out << GL_TEXTURE_CUBE_MAP_NEGATIVE_X << ", " << 0u << ", " << width << ", " << height;
289       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
290     }
291   }
292
293   //Upload data to the POSITIVE_Y face of the texture
294   {
295     callStack.Reset();
296     texture.Upload(pixelData, CubeMapLayer::POSITIVE_Y, 0u, 0u, 0u, width, height);
297     application.SendNotification();
298     application.Render();
299
300     //TexImage2D should be called to upload the data to the POSITIVE_Y face
301     {
302       std::stringstream out;
303       out << GL_TEXTURE_CUBE_MAP_POSITIVE_Y << ", " << 0u << ", " << width << ", " << height;
304       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
305     }
306   }
307
308   //Upload data to the NEGATIVE_Y face of the texture
309   {
310     callStack.Reset();
311     texture.Upload(pixelData, CubeMapLayer::NEGATIVE_Y, 0u, 0u, 0u, width, height);
312     application.SendNotification();
313     application.Render();
314
315     //TexImage2D should be called to upload the data to the NEGATIVE_Y face
316     {
317       std::stringstream out;
318       out << GL_TEXTURE_CUBE_MAP_NEGATIVE_Y << ", " << 0u << ", " << width << ", " << height;
319       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
320     }
321   }
322
323   //Upload data to the POSITIVE_Z face of the texture
324   {
325     callStack.Reset();
326     texture.Upload(pixelData, CubeMapLayer::POSITIVE_Z, 0u, 0u, 0u, width, height);
327     application.SendNotification();
328     application.Render();
329
330     //TexImage2D should be called to upload the data to the POSITIVE_Z face
331     {
332       std::stringstream out;
333       out << GL_TEXTURE_CUBE_MAP_POSITIVE_Z << ", " << 0u << ", " << width << ", " << height;
334       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
335     }
336   }
337
338   //Upload data to the NEGATIVE_Z face of the texture
339   {
340     callStack.Reset();
341     texture.Upload(pixelData, CubeMapLayer::NEGATIVE_Z, 0u, 0u, 0u, width, height);
342     application.SendNotification();
343     application.Render();
344
345     //TexImage2D should be called to upload the data to the NEGATIVE_Z face
346     {
347       std::stringstream out;
348       out << GL_TEXTURE_CUBE_MAP_NEGATIVE_Z << ", " << 0u << ", " << width << ", " << height;
349       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
350     }
351   }
352
353   END_TEST;
354 }
355
356 int UtcDaliTextureUpload03(void)
357 {
358   TestApplication application;
359
360   //Create the texture
361   unsigned int width(64);
362   unsigned int height(64);
363   unsigned int widthMipmap1(32);
364   unsigned int heightMipmap1(32);
365
366   Texture texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
367
368   application.GetGlAbstraction().EnableTextureCallTrace(true);
369
370   application.SendNotification();
371   application.Render();
372
373   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
374
375   //TexImage2D should be called with a null pointer to reserve storage for the texture in the gpu
376   {
377     std::stringstream out;
378     out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
379     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
380   }
381
382   //Upload data to the texture mipmap 0 and mipmap 1
383   callStack.Reset();
384
385   unsigned int   bufferSize(width * height * 4);
386   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
387   PixelData      pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE);
388   texture.Upload(pixelData, 0u, 0u, 0u, 0u, width, height);
389
390   bufferSize                 = widthMipmap1 * heightMipmap1 * 4;
391   buffer                     = reinterpret_cast<unsigned char*>(malloc(bufferSize));
392   PixelData pixelDataMipmap1 = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE);
393   texture.Upload(pixelDataMipmap1, 0u, 1u, 0u, 0u, widthMipmap1, heightMipmap1);
394   application.SendNotification();
395   application.Render();
396
397   //TexImage2D should be called to upload the data to mipmaps 0 and 1
398   {
399     std::stringstream out;
400     out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
401     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
402   }
403   {
404     std::stringstream out;
405     out << GL_TEXTURE_2D << ", " << 1u << ", " << widthMipmap1 << ", " << heightMipmap1;
406     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
407   }
408
409   END_TEST;
410 }
411
412 int UtcDaliTextureUpload04(void)
413 {
414   TestApplication application;
415
416   //Create the texture
417   unsigned int width(64);
418   unsigned int height(64);
419   unsigned int widthMipmap1(32);
420   unsigned int heightMipmap1(32);
421
422   Texture texture = Texture::New(TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height);
423
424   application.GetGlAbstraction().EnableTextureCallTrace(true);
425   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
426
427   //Upload data to the NEGATIVE_X face mipmap 0 and mipmap 1
428   unsigned int   bufferSize(width * height * 4);
429   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
430   PixelData      pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE);
431   texture.Upload(pixelData, CubeMapLayer::NEGATIVE_X, 0u, 0u, 0u, width, height);
432
433   bufferSize                 = widthMipmap1 * heightMipmap1 * 4;
434   buffer                     = reinterpret_cast<unsigned char*>(malloc(bufferSize));
435   PixelData pixelDataMipmap1 = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE);
436   texture.Upload(pixelDataMipmap1, CubeMapLayer::NEGATIVE_X, 1u, 0u, 0u, widthMipmap1, heightMipmap1);
437   application.SendNotification();
438   application.Render();
439
440   //TexImage2D should be called to upload the data to mipmaps 0 and 1
441   {
442     std::stringstream out;
443     out << GL_TEXTURE_CUBE_MAP_NEGATIVE_X << ", " << 0u << ", " << width << ", " << height;
444     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
445   }
446   {
447     std::stringstream out;
448     out << GL_TEXTURE_CUBE_MAP_NEGATIVE_X << ", " << 1u << ", " << widthMipmap1 << ", " << heightMipmap1;
449     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
450   }
451
452   END_TEST;
453 }
454
455 int UtcDaliTextureUpload05(void)
456 {
457   Pixel::Format COMPRESSED_PIXEL_FORMATS[] =
458     {
459       Pixel::COMPRESSED_R11_EAC,
460       Pixel::COMPRESSED_SIGNED_R11_EAC,
461       Pixel::COMPRESSED_RG11_EAC,
462       Pixel::COMPRESSED_SIGNED_RG11_EAC,
463       Pixel::COMPRESSED_RGB8_ETC2,
464       Pixel::COMPRESSED_SRGB8_ETC2,
465       Pixel::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2,
466       Pixel::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2,
467       Pixel::COMPRESSED_RGBA8_ETC2_EAC,
468       Pixel::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC,
469       Pixel::COMPRESSED_RGB8_ETC1,
470       Pixel::COMPRESSED_RGB_PVRTC_4BPPV1,
471       Pixel::COMPRESSED_RGBA_ASTC_4x4_KHR,
472       Pixel::COMPRESSED_RGBA_ASTC_5x4_KHR,
473       Pixel::COMPRESSED_RGBA_ASTC_5x5_KHR,
474       Pixel::COMPRESSED_RGBA_ASTC_6x5_KHR,
475       Pixel::COMPRESSED_RGBA_ASTC_6x6_KHR,
476       Pixel::COMPRESSED_RGBA_ASTC_8x5_KHR,
477       Pixel::COMPRESSED_RGBA_ASTC_8x6_KHR,
478       Pixel::COMPRESSED_RGBA_ASTC_8x8_KHR,
479       Pixel::COMPRESSED_RGBA_ASTC_10x5_KHR,
480       Pixel::COMPRESSED_RGBA_ASTC_10x6_KHR,
481       Pixel::COMPRESSED_RGBA_ASTC_10x8_KHR,
482       Pixel::COMPRESSED_RGBA_ASTC_10x10_KHR,
483       Pixel::COMPRESSED_RGBA_ASTC_12x10_KHR,
484       Pixel::COMPRESSED_RGBA_ASTC_12x12_KHR,
485       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR,
486       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR,
487       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR,
488       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR,
489       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR,
490       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR,
491       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR,
492       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR,
493       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR,
494       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR,
495       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR,
496       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR,
497       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR,
498       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR,
499     };
500   const unsigned int NUMBER_OF_COMPRESSED_PIXEL_FORMATS = sizeof(COMPRESSED_PIXEL_FORMATS) / sizeof(Pixel::Format);
501
502   for(unsigned int index = 0; index < NUMBER_OF_COMPRESSED_PIXEL_FORMATS; ++index)
503   {
504     TestApplication application;
505
506     //Create a texture with a compressed format
507     unsigned int width(64);
508     unsigned int height(64);
509     Texture      texture = Texture::New(TextureType::TEXTURE_2D, COMPRESSED_PIXEL_FORMATS[index], width, height);
510
511     application.GetGlAbstraction().EnableTextureCallTrace(true);
512
513     application.SendNotification();
514     application.Render();
515
516     TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
517
518     //CompressedTexImage2D should be called with a null pointer to reserve storage for the texture in the gpu
519     {
520       std::stringstream out;
521       out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
522       DALI_TEST_CHECK(callStack.FindMethodAndParams("CompressedTexImage2D", out.str().c_str()));
523     }
524
525     //Upload data to the texture
526     callStack.Reset();
527
528     unsigned int   bufferSize(width * height * 4);
529     unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
530     PixelData      pixelData = PixelData::New(buffer, bufferSize, width, height, COMPRESSED_PIXEL_FORMATS[index], PixelData::FREE);
531     texture.Upload(pixelData);
532     application.SendNotification();
533     application.Render();
534
535     //CompressedTexImage2D should be called to upload the data
536     {
537       std::stringstream out;
538       out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
539       DALI_TEST_CHECK(callStack.FindMethodAndParams("CompressedTexImage2D", out.str().c_str()));
540     }
541
542     //Upload part of the texture
543     callStack.Reset();
544     bufferSize                  = width * height * 2;
545     buffer                      = reinterpret_cast<unsigned char*>(malloc(bufferSize));
546     PixelData pixelDataSubImage = PixelData::New(buffer, bufferSize, width, height, COMPRESSED_PIXEL_FORMATS[index], PixelData::FREE);
547     texture.Upload(pixelDataSubImage, 0u, 0u, width / 2, height / 2, width / 2, height / 2);
548     application.SendNotification();
549     application.Render();
550
551     //CompressedTexSubImage2D should be called to upload the data
552     {
553       std::stringstream out;
554       out << GL_TEXTURE_2D << ", " << 0u << ", " << width / 2 << ", " << height / 2 << ", " << width / 2 << ", " << height / 2;
555       DALI_TEST_CHECK(callStack.FindMethodAndParams("CompressedTexSubImage2D", out.str().c_str()));
556     }
557
558     application.GetGlAbstraction().ResetTextureCallStack();
559   }
560
561   END_TEST;
562 }
563
564 int UtcDaliTextureUpload06(void)
565 {
566   TestApplication application;
567
568   //Create the texture
569   unsigned int width(64);
570   unsigned int height(64);
571   tet_infoline("Creating a Texure with an alpha channel");
572   Texture texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
573
574   application.GetGlAbstraction().EnableTextureCallTrace(true);
575
576   application.SendNotification();
577   application.Render();
578
579   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
580
581   tet_infoline("TexImage2D should be called with a null pointer to reserve storage for the texture in the gpu");
582   {
583     std::stringstream out;
584     out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
585     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
586   }
587
588   tet_infoline("Upload data to the texture");
589   callStack.Reset();
590
591   tet_infoline("Creating a RGB pixel buffer and adding that to the texture to ensure it is handled correctly");
592   unsigned int   bufferSize(width * height * 3);
593   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
594   PixelData      pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGB888, PixelData::FREE);
595   texture.Upload(pixelData);
596   application.SendNotification();
597   application.Render();
598
599   tet_infoline("TexImage2D should be called to upload the data");
600   {
601     std::stringstream out;
602     out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
603     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
604   }
605
606   END_TEST;
607 }
608
609 int UtcDaliTextureUpload07(void)
610 {
611   Pixel::Format FLOATING_POINT_PIXEL_FORMATS[] =
612     {
613       Pixel::RGB16F,
614       Pixel::RGB32F,
615     };
616   const unsigned int NUMBER_OF_FLOATING_POINT_PIXEL_FORMATS = sizeof(FLOATING_POINT_PIXEL_FORMATS) / sizeof(Pixel::Format);
617
618   for(unsigned int index = 0; index < NUMBER_OF_FLOATING_POINT_PIXEL_FORMATS; ++index)
619   {
620     TestApplication application;
621
622     //Create the texture
623     unsigned int width(64);
624     unsigned int height(64);
625     tet_infoline("Creating a floating point texture");
626     Texture texture = Texture::New(TextureType::TEXTURE_2D, FLOATING_POINT_PIXEL_FORMATS[index], width, height);
627
628     application.GetGlAbstraction().EnableTextureCallTrace(true);
629
630     application.SendNotification();
631     application.Render();
632
633     TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
634
635     tet_infoline("TexImage2D should be called with a null pointer to reserve storage for the texture in the gpu");
636     {
637       std::stringstream out;
638       out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
639       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
640     }
641
642     tet_infoline("Upload data to the texture");
643     callStack.Reset();
644
645     tet_infoline("Creating a RGB pixel buffer and adding that to the texture to ensure it is handled correctly");
646     unsigned int   bufferSize(width * height * 3);
647     unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
648     PixelData      pixelData = PixelData::New(buffer, bufferSize, width, height, FLOATING_POINT_PIXEL_FORMATS[index], PixelData::FREE);
649     texture.Upload(pixelData);
650     application.SendNotification();
651     application.Render();
652
653     tet_infoline("TexImage2D should be called to upload the data");
654     {
655       std::stringstream out;
656       out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
657       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
658     }
659   }
660
661   END_TEST;
662 }
663
664 int UtcDaliTextureUploadSmallerThanSize(void)
665 {
666   TestApplication application;
667
668   //Create the texture
669   unsigned int width(64);
670   unsigned int height(64);
671   Texture      texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
672
673   application.GetGlAbstraction().EnableTextureCallTrace(true);
674
675   application.SendNotification();
676   application.Render();
677
678   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
679
680   //TexImage2D should be called with a null pointer to reserve storage for the texture in the gpu
681   {
682     std::stringstream out;
683     out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
684     std::string params;
685     DALI_TEST_CHECK(callStack.FindMethodAndGetParameters("TexImage2D", params));
686     DALI_TEST_EQUALS(out.str(), params, TEST_LOCATION);
687   }
688
689   //Upload data to the texture
690   callStack.Reset();
691
692   unsigned int   bufferSize(width * height * 4);
693   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
694   PixelData      pixelData = PixelData::New(buffer, bufferSize, width / 2, height / 2, Pixel::RGBA8888, PixelData::FREE);
695   texture.Upload(pixelData);
696   application.SendNotification();
697   application.Render();
698
699   //TexImage2D should be called to upload the data
700   {
701     std::stringstream out;
702     out << GL_TEXTURE_2D << ", " << 0u << ", " << 0u << ", " << 0u << ", " << width / 2 << ", " << height / 2;
703     std::string params;
704     DALI_TEST_CHECK(callStack.FindMethodAndGetParameters("TexSubImage2D", params));
705     DALI_TEST_EQUALS(out.str(), params, TEST_LOCATION);
706   }
707
708   END_TEST;
709 }
710
711 int UtcDaliTextureGenerateMipmaps(void)
712 {
713   TestApplication application;
714   unsigned int    width(64);
715   unsigned int    height(64);
716
717   Texture texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
718   texture.GenerateMipmaps();
719
720   Texture textureCubemap = Texture::New(TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height);
721   textureCubemap.GenerateMipmaps();
722
723   application.GetGlAbstraction().EnableTextureCallTrace(true);
724   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
725   application.SendNotification();
726   application.Render();
727
728   {
729     std::stringstream out;
730     out << GL_TEXTURE_2D;
731     DALI_TEST_CHECK(callStack.FindMethodAndParams("GenerateMipmap", out.str().c_str()));
732   }
733   {
734     std::stringstream out;
735     out << GL_TEXTURE_CUBE_MAP;
736     DALI_TEST_CHECK(callStack.FindMethodAndParams("GenerateMipmap", out.str().c_str()));
737   }
738
739   END_TEST;
740 }
741
742 int UtcDaliTextureGetWidth(void)
743 {
744   TestApplication application;
745   unsigned int    width(64);
746   unsigned int    height(64);
747
748   Texture texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
749   DALI_TEST_EQUALS(texture.GetWidth(), width, TEST_LOCATION);
750   END_TEST;
751 }
752
753 int UtcDaliTextureGetHeight(void)
754 {
755   TestApplication application;
756   unsigned int    width(64);
757   unsigned int    height(64);
758
759   Texture texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
760   DALI_TEST_EQUALS(texture.GetHeight(), height, TEST_LOCATION);
761
762   END_TEST;
763 }
764
765 int UtcDaliTextureContextLoss(void)
766 {
767   tet_infoline("UtcDaliTextureContextLoss\n");
768   TestApplication application;
769
770   //Create the texture
771   unsigned int width(64);
772   unsigned int height(64);
773   Texture      texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
774   DALI_TEST_CHECK(texture);
775
776   application.SendNotification();
777   application.Render(16);
778
779   // Lose & regain context (in render 'thread')
780   application.ResetContext();
781   DALI_TEST_CHECK(texture);
782
783   END_TEST;
784 }
785
786 int UtcDaliNativeImageTexture01(void)
787 {
788   TestApplication application;
789   tet_infoline("UtcDaliNativeImageTexture01");
790
791   TestNativeImagePointer imageInterface = TestNativeImage::New(16, 16);
792   {
793     Texture texture = Texture::New(*(imageInterface.Get()));
794     Actor   actor   = CreateRenderableActor(texture, "", "");
795     application.GetScene().Add(actor);
796
797     DALI_TEST_CHECK(texture);
798
799     application.SendNotification();
800     application.Render(16);
801
802     DALI_TEST_EQUALS(imageInterface->mExtensionCreateCalls, 1, TEST_LOCATION);
803     DALI_TEST_EQUALS(imageInterface->mExtensionDestroyCalls, 0, TEST_LOCATION);
804     DALI_TEST_EQUALS(actor.GetProperty(Actor::Property::SIZE), Property::Value(Vector3(16, 16, 0)), TEST_LOCATION);
805
806     UnparentAndReset(actor);
807
808     application.SendNotification();
809     application.Render(16);
810   }
811   application.SendNotification();
812   application.Render(16);
813
814   DALI_TEST_EQUALS(imageInterface->mExtensionCreateCalls, 1, TEST_LOCATION);
815   DALI_TEST_EQUALS(imageInterface->mExtensionDestroyCalls, 1, TEST_LOCATION);
816
817   END_TEST;
818 }
819
820 int UtcDaliNativeImageTexture02(void)
821 {
822   TestApplication application;
823   tet_infoline("UtcDaliNativeImageTexture02 - test error on TargetTexture");
824
825   TestNativeImagePointer imageInterface = TestNativeImage::New(16, 16);
826   imageInterface->mTargetTextureError   = 1u;
827   {
828     Texture texture = Texture::New(*(imageInterface.Get()));
829     Actor   actor   = CreateRenderableActor(texture, "", "");
830     application.GetScene().Add(actor);
831
832     DALI_TEST_CHECK(texture);
833
834     application.SendNotification();
835     application.Render(16);
836
837     // Expect 2 attempts to create the texture - once when adding the texture
838     // to the scene-graph, and again since that failed, during the Bind.
839     DALI_TEST_EQUALS(imageInterface->mExtensionCreateCalls, 2, TEST_LOCATION);
840     DALI_TEST_EQUALS(imageInterface->mExtensionDestroyCalls, 2, TEST_LOCATION);
841
842     UnparentAndReset(actor);
843
844     application.SendNotification();
845     application.Render(16);
846   }
847   application.SendNotification();
848   application.Render(16);
849
850   // Expect that there are no further calls to create/destroy resource
851   DALI_TEST_EQUALS(imageInterface->mExtensionCreateCalls, 2, TEST_LOCATION);
852   DALI_TEST_EQUALS(imageInterface->mExtensionDestroyCalls, 2, TEST_LOCATION);
853
854   END_TEST;
855 }
856
857 int UtcDaliTextureGenerateMipmapsNegative(void)
858 {
859   TestApplication application;
860   Dali::Texture   instance;
861   try
862   {
863     instance.GenerateMipmaps();
864     DALI_TEST_CHECK(false); // Should not get here
865   }
866   catch(...)
867   {
868     DALI_TEST_CHECK(true); // We expect an assert
869   }
870   END_TEST;
871 }
872
873 int UtcDaliTextureUploadNegative01(void)
874 {
875   TestApplication application;
876   Dali::Texture   instance;
877   try
878   {
879     Dali::PixelData arg1;
880     instance.Upload(arg1);
881     DALI_TEST_CHECK(false); // Should not get here
882   }
883   catch(...)
884   {
885     DALI_TEST_CHECK(true); // We expect an assert
886   }
887   END_TEST;
888 }
889
890 int UtcDaliTextureUploadNegative02(void)
891 {
892   TestApplication application;
893   Dali::Texture   instance;
894   try
895   {
896     Dali::PixelData arg1;
897     unsigned int    arg2(0u);
898     unsigned int    arg3(0u);
899     unsigned int    arg4(0u);
900     unsigned int    arg5(0u);
901     unsigned int    arg6(0u);
902     unsigned int    arg7(0u);
903     instance.Upload(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
904     DALI_TEST_CHECK(false); // Should not get here
905   }
906   catch(...)
907   {
908     DALI_TEST_CHECK(true); // We expect an assert
909   }
910   END_TEST;
911 }
912
913 int UtcDaliTextureGetWidthNegative(void)
914 {
915   TestApplication application;
916   Dali::Texture   instance;
917   try
918   {
919     instance.GetWidth();
920     DALI_TEST_CHECK(false); // Should not get here
921   }
922   catch(...)
923   {
924     DALI_TEST_CHECK(true); // We expect an assert
925   }
926   END_TEST;
927 }
928
929 int UtcDaliTextureGetHeightNegative(void)
930 {
931   TestApplication application;
932   Dali::Texture   instance;
933   try
934   {
935     instance.GetHeight();
936     DALI_TEST_CHECK(false); // Should not get here
937   }
938   catch(...)
939   {
940     DALI_TEST_CHECK(true); // We expect an assert
941   }
942   END_TEST;
943 }
944
945 int UtcDaliTextureCheckNativeP(void)
946 {
947   TestApplication        application;
948   TestNativeImagePointer testNativeImage = TestNativeImage::New(64u, 64u);
949   Texture                nativeTexture   = Texture::New(*testNativeImage);
950
951   DALI_TEST_CHECK(nativeTexture);
952   DALI_TEST_CHECK(DevelTexture::IsNative(nativeTexture));
953   END_TEST;
954 }
955
956 int UtcDaliTextureCheckNativeN1(void)
957 {
958   TestApplication application;
959   unsigned int    width(64);
960   unsigned int    height(64);
961   Texture         texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
962
963   DALI_TEST_CHECK(texture);
964   DALI_TEST_CHECK(!DevelTexture::IsNative(texture));
965   END_TEST;
966 }
967
968 int UtcDaliTextureCheckNativeN2(void)
969 {
970   TestApplication application;
971   Texture         texture;
972   try
973   {
974     bool native = DevelTexture::IsNative(texture);
975     DALI_TEST_CHECK(native != native);
976   }
977   catch(...)
978   {
979     DALI_TEST_CHECK(true);
980   }
981   END_TEST;
982 }
983
984 int UtcDaliTextureApplyFragShaderP1(void)
985 {
986   TestApplication        application;
987   TestNativeImagePointer testNativeImage = TestNativeImage::New(64u, 64u);
988   Texture                nativeTexture   = Texture::New(*testNativeImage);
989   DALI_TEST_CHECK(nativeTexture);
990
991   const std::string baseFragShader =
992     "varying mediump vec4 uColor;\n"
993     "void main(){\n"
994     "  gl_FragColor=uColor;\n"
995     "}\n";
996   std::string fragShader = baseFragShader;
997   bool        applied    = DevelTexture::ApplyNativeFragmentShader(nativeTexture, fragShader);
998
999   DALI_TEST_CHECK(applied);
1000   DALI_TEST_CHECK(baseFragShader.compare(fragShader));
1001   DALI_TEST_CHECK(!fragShader.empty());
1002   END_TEST;
1003 }
1004
1005 int UtcDaliTextureApplyFragShaderP2(void)
1006 {
1007   TestApplication        application;
1008   TestNativeImagePointer testNativeImage = TestNativeImage::New(64u, 64u);
1009   Texture                nativeTexture   = Texture::New(*testNativeImage);
1010   DALI_TEST_CHECK(nativeTexture);
1011
1012   const std::string baseFragShader =
1013     "varying mediump vec4 uColor;\n"
1014     "varying vec2 vTexCoord;\n"
1015     "uniform sampler2D uNative;\n"
1016     "void main(){\n"
1017     "  gl_FragColor=uColor*texture2D(uNative, vTexCoord);\n"
1018     "}\n";
1019   std::string fragShader = baseFragShader;
1020   bool        applied    = DevelTexture::ApplyNativeFragmentShader(nativeTexture, fragShader);
1021
1022   DALI_TEST_CHECK(applied);
1023   DALI_TEST_CHECK(baseFragShader.compare(fragShader));
1024   DALI_TEST_CHECK(!fragShader.empty());
1025   DALI_TEST_CHECK(fragShader.find("samplerExternalOES") < fragShader.length());
1026   END_TEST;
1027 }
1028
1029 int UtcDaliTextureApplyFragShaderN1(void)
1030 {
1031   TestApplication        application;
1032   TestNativeImagePointer testNativeImage = TestNativeImage::New(64u, 64u);
1033   Texture                nativeTexture   = Texture::New(*testNativeImage);
1034   DALI_TEST_CHECK(nativeTexture);
1035
1036   std::string fragShader;
1037   bool        applied = DevelTexture::ApplyNativeFragmentShader(nativeTexture, fragShader);
1038
1039   DALI_TEST_CHECK(!applied);
1040   DALI_TEST_CHECK(fragShader.empty());
1041   END_TEST;
1042 }
1043
1044 int UtcDaliTextureApplyFragShaderN2(void)
1045 {
1046   TestApplication application;
1047   unsigned int    width(64);
1048   unsigned int    height(64);
1049   Texture         texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
1050
1051   const std::string baseFragShader =
1052     "varying mediump vec4 uColor;\n"
1053     "void main(){\n"
1054     "  gl_FragColor=uColor;\n"
1055     "}\n";
1056   std::string fragShader = baseFragShader;
1057   bool        applied    = DevelTexture::ApplyNativeFragmentShader(texture, fragShader);
1058
1059   DALI_TEST_CHECK(!applied);
1060   DALI_TEST_CHECK(!baseFragShader.compare(fragShader));
1061   END_TEST;
1062 }