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