Fix the screen rotation issue
[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;
210   buffer                      = reinterpret_cast<unsigned char*>(malloc(bufferSize));
211   PixelData pixelDataSubImage = PixelData::New(buffer, bufferSize, width / 2, height / 2, 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, widthMipmap1, heightMipmap1, 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, widthMipmap1, heightMipmap1, 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;
538     buffer                      = reinterpret_cast<unsigned char*>(malloc(bufferSize));
539     PixelData pixelDataSubImage = PixelData::New(buffer, bufferSize, width / 2, height / 2, 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 UtcDaliTextureUploadSubPixelData01(void)
658 {
659   TestApplication application;
660
661   //Create the texture
662   unsigned int width(64);
663   unsigned int height(64);
664   Texture      texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
665
666   application.GetGlAbstraction().EnableTextureCallTrace(true);
667
668   application.SendNotification();
669   application.Render();
670
671   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
672
673   //Upload data to the texture
674   callStack.Reset();
675
676   uint32_t bufferWidth   = width * 2;
677   uint32_t bufferHeight  = height * 2;
678   uint32_t bufferXOffset = width;
679   uint32_t bufferYOffset = height;
680
681   unsigned int   bufferSize(bufferWidth * bufferHeight * 4);
682   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
683   PixelData      pixelData = PixelData::New(buffer, bufferSize, bufferWidth, bufferHeight, Pixel::RGBA8888, PixelData::FREE);
684   DevelTexture::UploadSubPixelData(texture, pixelData, bufferXOffset, bufferYOffset, width, height);
685   application.SendNotification();
686   application.Render();
687
688   //TexImage2D should be called to upload the data
689   {
690     std::stringstream out;
691     out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
692     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
693   }
694
695   //Upload part of the texture
696   callStack.Reset();
697   DevelTexture::UploadSubPixelData(texture, pixelData, bufferXOffset, bufferYOffset, width / 2, height / 2, 0u, 0u, width / 2, height / 2, width / 2, height / 2);
698   application.SendNotification();
699   application.Render();
700
701   //TexSubImage2D should be called to upload the data
702   {
703     std::stringstream out;
704     out << GL_TEXTURE_2D << ", " << 0u << ", " << width / 2 << ", " << height / 2 << ", " << width / 2 << ", " << height / 2;
705     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexSubImage2D", out.str().c_str()));
706   }
707
708   END_TEST;
709 }
710
711 int UtcDaliTextureUploadSubPixelData02(void)
712 {
713   TestApplication application;
714
715   //Create the texture
716   unsigned int width(64);
717   unsigned int height(64);
718   Texture      texture = CreateTexture(TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height);
719
720   application.GetGlAbstraction().EnableTextureCallTrace(true);
721
722   application.SendNotification();
723   application.Render();
724
725   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
726
727   tet_infoline("TexImage2D should be called six times with a null pointer to reserve storage for the six textures of the cube map");
728   for(unsigned int i(0); i < 6; ++i)
729   {
730     std::stringstream out;
731     out << GL_TEXTURE_CUBE_MAP_POSITIVE_X + i << ", " << 0u << ", " << width << ", " << height;
732     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
733   }
734
735   uint32_t bufferWidth   = width * 2;
736   uint32_t bufferHeight  = height * 2;
737   uint32_t bufferXOffset = width;
738   uint32_t bufferYOffset = height;
739
740   unsigned int   bufferSize(bufferWidth * bufferHeight * 4);
741   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
742   PixelData      pixelData = PixelData::New(buffer, bufferSize, bufferWidth, bufferHeight, Pixel::RGBA8888, PixelData::FREE);
743
744   //Upload data to the POSITIVE_X face of the texture
745   {
746     callStack.Reset();
747
748     DevelTexture::UploadSubPixelData(texture, pixelData, bufferXOffset, bufferYOffset, width, height, CubeMapLayer::POSITIVE_X, 0u, 0u, 0u, width, height);
749     application.SendNotification();
750     application.Render();
751
752     //TexImage2D should be called to upload the data to the POSITIVE_X face
753     {
754       std::stringstream out;
755       out << GL_TEXTURE_CUBE_MAP_POSITIVE_X << ", " << 0u << ", " << width << ", " << height;
756       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
757     }
758   }
759
760   //Upload data to the NEGATIVE_X face of the texture
761   {
762     callStack.Reset();
763
764     DevelTexture::UploadSubPixelData(texture, pixelData, bufferXOffset, bufferYOffset, width, height, CubeMapLayer::NEGATIVE_X, 0u, 0u, 0u, width, height);
765     application.SendNotification();
766     application.Render();
767
768     //TexImage2D should be called to upload the data to the NEGATIVE_X face
769     {
770       std::stringstream out;
771       out << GL_TEXTURE_CUBE_MAP_NEGATIVE_X << ", " << 0u << ", " << width << ", " << height;
772       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
773     }
774   }
775
776   //Upload data to the POSITIVE_Y face of the texture
777   {
778     callStack.Reset();
779     DevelTexture::UploadSubPixelData(texture, pixelData, bufferXOffset, bufferYOffset, width, height, CubeMapLayer::POSITIVE_Y, 0u, 0u, 0u, width, height);
780     application.SendNotification();
781     application.Render();
782
783     //TexImage2D should be called to upload the data to the POSITIVE_Y face
784     {
785       std::stringstream out;
786       out << GL_TEXTURE_CUBE_MAP_POSITIVE_Y << ", " << 0u << ", " << width << ", " << height;
787       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
788     }
789   }
790
791   //Upload data to the NEGATIVE_Y face of the texture
792   {
793     callStack.Reset();
794     DevelTexture::UploadSubPixelData(texture, pixelData, bufferXOffset, bufferYOffset, width, height, CubeMapLayer::NEGATIVE_Y, 0u, 0u, 0u, width, height);
795     application.SendNotification();
796     application.Render();
797
798     //TexImage2D should be called to upload the data to the NEGATIVE_Y face
799     {
800       std::stringstream out;
801       out << GL_TEXTURE_CUBE_MAP_NEGATIVE_Y << ", " << 0u << ", " << width << ", " << height;
802       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
803     }
804   }
805
806   //Upload data to the POSITIVE_Z face of the texture
807   {
808     callStack.Reset();
809     DevelTexture::UploadSubPixelData(texture, pixelData, bufferXOffset, bufferYOffset, width, height, CubeMapLayer::POSITIVE_Z, 0u, 0u, 0u, width, height);
810     application.SendNotification();
811     application.Render();
812
813     //TexImage2D should be called to upload the data to the POSITIVE_Z face
814     {
815       std::stringstream out;
816       out << GL_TEXTURE_CUBE_MAP_POSITIVE_Z << ", " << 0u << ", " << width << ", " << height;
817       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
818     }
819   }
820
821   //Upload data to the NEGATIVE_Z face of the texture
822   {
823     callStack.Reset();
824     DevelTexture::UploadSubPixelData(texture, pixelData, bufferXOffset, bufferYOffset, width, height, CubeMapLayer::NEGATIVE_Z, 0u, 0u, 0u, width, height);
825     application.SendNotification();
826     application.Render();
827
828     //TexImage2D should be called to upload the data to the NEGATIVE_Z face
829     {
830       std::stringstream out;
831       out << GL_TEXTURE_CUBE_MAP_NEGATIVE_Z << ", " << 0u << ", " << width << ", " << height;
832       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
833     }
834   }
835
836   END_TEST;
837 }
838
839 int UtcDaliTextureUploadPixelFormats(void)
840 {
841   TestApplication application;
842   application.GetGlAbstraction().EnableTextureCallTrace(true);
843
844   //Create the texture
845   unsigned int width(64);
846   unsigned int height(64);
847
848   std::vector<Pixel::Format> formats =
849     {
850       Pixel::A8,
851       Pixel::L8,
852       Pixel::LA88,
853       Pixel::RGB565,
854       Pixel::BGR565,
855       Pixel::RGBA4444,
856       Pixel::BGRA4444,
857       Pixel::RGBA5551,
858       Pixel::BGRA5551,
859       Pixel::RGB888,
860       Pixel::RGB8888,
861       Pixel::BGR8888,
862       Pixel::RGBA8888,
863       Pixel::BGRA8888,
864       Pixel::DEPTH_UNSIGNED_INT,
865       Pixel::DEPTH_FLOAT,
866       Pixel::DEPTH_STENCIL,
867       Pixel::RGB16F,
868       Pixel::RGB32F,
869       Pixel::R11G11B10F,
870       Pixel::CHROMINANCE_U,
871       Pixel::CHROMINANCE_V};
872
873   for(auto format : formats)
874   {
875     tet_infoline("Creating a Texure with a new or recent format");
876     Texture texture = CreateTexture(TextureType::TEXTURE_2D, format, width, height);
877
878     application.SendNotification();
879     application.Render();
880
881     TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
882
883     tet_infoline("TexImage2D should be called twice per texture");
884     DALI_TEST_EQUALS(callStack.CountMethod("TexImage2D"), 2, TEST_LOCATION);
885     {
886       std::stringstream out;
887       out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
888       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
889     }
890     callStack.Reset();
891   }
892
893   END_TEST;
894 }
895
896 int UtcDaliTextureUploadSmallerThanSize(void)
897 {
898   TestApplication application;
899
900   //Create the texture
901   unsigned int width(64);
902   unsigned int height(64);
903   Texture      texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
904
905   application.GetGlAbstraction().EnableTextureCallTrace(true);
906
907   application.SendNotification();
908   application.Render();
909
910   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
911   callStack.EnableLogging(true);
912   TraceCallStack& texParamCallStack = application.GetGlAbstraction().GetTexParameterTrace();
913   texParamCallStack.EnableLogging(true);
914
915   tet_infoline("TexImage2D should be called with a null pointer to reserve storage for the texture in the gpu");
916   {
917     std::stringstream out;
918     out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
919     std::string params;
920     DALI_TEST_CHECK(callStack.FindMethodAndGetParameters("TexImage2D", params));
921     DALI_TEST_EQUALS(out.str(), params, TEST_LOCATION);
922   }
923
924   //Upload data to the texture
925   callStack.Reset();
926
927   unsigned int   bufferSize(width * height * 4);
928   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
929   PixelData      pixelData = PixelData::New(buffer, bufferSize, width / 2, height / 2, Pixel::RGBA8888, PixelData::FREE);
930   texture.Upload(pixelData);
931   application.SendNotification();
932   application.Render();
933
934   //TexSubImage2D should be called to upload the data
935   {
936     std::stringstream out;
937     out << GL_TEXTURE_2D << ", " << 0u << ", " << 0u << ", " << 0u << ", " << width / 2 << ", " << height / 2;
938     std::string params;
939     DALI_TEST_CHECK(callStack.FindMethodAndGetParameters("TexSubImage2D", params));
940     DALI_TEST_EQUALS(out.str(), params, TEST_LOCATION);
941   }
942   END_TEST;
943 }
944
945 int UtcDaliTextureGenerateMipmaps(void)
946 {
947   TestApplication application;
948   unsigned int    width(64);
949   unsigned int    height(64);
950
951   Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
952   texture.GenerateMipmaps();
953
954   Texture textureCubemap = CreateTexture(TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height);
955   textureCubemap.GenerateMipmaps();
956
957   application.GetGlAbstraction().EnableTextureCallTrace(true);
958   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
959   application.SendNotification();
960   application.Render();
961
962   {
963     std::stringstream out;
964     out << GL_TEXTURE_2D;
965     DALI_TEST_CHECK(callStack.FindMethodAndParams("GenerateMipmap", out.str().c_str()));
966   }
967   {
968     std::stringstream out;
969     out << GL_TEXTURE_CUBE_MAP;
970     DALI_TEST_CHECK(callStack.FindMethodAndParams("GenerateMipmap", out.str().c_str()));
971   }
972
973   END_TEST;
974 }
975
976 int UtcDaliTextureGetWidth(void)
977 {
978   TestApplication application;
979   unsigned int    width(64);
980   unsigned int    height(64);
981
982   Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
983   DALI_TEST_EQUALS(texture.GetWidth(), width, TEST_LOCATION);
984   END_TEST;
985 }
986
987 int UtcDaliTextureGetHeight(void)
988 {
989   TestApplication application;
990   unsigned int    width(64);
991   unsigned int    height(64);
992
993   Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
994   DALI_TEST_EQUALS(texture.GetHeight(), height, TEST_LOCATION);
995
996   END_TEST;
997 }
998
999 int UtcDaliTextureContextLoss(void)
1000 {
1001   tet_infoline("UtcDaliTextureContextLoss\n");
1002   TestApplication application;
1003
1004   //Create the texture
1005   unsigned int width(64);
1006   unsigned int height(64);
1007   Texture      texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
1008   DALI_TEST_CHECK(texture);
1009
1010   application.SendNotification();
1011   application.Render(16);
1012
1013   // Lose & regain context (in render 'thread')
1014   application.ResetContext();
1015   DALI_TEST_CHECK(texture);
1016
1017   END_TEST;
1018 }
1019
1020 int UtcDaliNativeImageTexture01(void)
1021 {
1022   TestApplication application;
1023   tet_infoline("UtcDaliNativeImageTexture01");
1024
1025   TestNativeImagePointer imageInterface = TestNativeImage::New(16, 16);
1026   {
1027     Texture texture = Texture::New(*(imageInterface.Get()));
1028     Actor   actor   = CreateRenderableActor(texture, "", "");
1029     application.GetScene().Add(actor);
1030
1031     DALI_TEST_CHECK(texture);
1032
1033     application.SendNotification();
1034     application.Render(16);
1035
1036     DALI_TEST_EQUALS(imageInterface->mExtensionCreateCalls, 1, TEST_LOCATION);
1037     DALI_TEST_EQUALS(imageInterface->mExtensionDestroyCalls, 0, TEST_LOCATION);
1038     DALI_TEST_EQUALS(actor.GetProperty(Actor::Property::SIZE), Property::Value(Vector3(16, 16, 0)), TEST_LOCATION);
1039
1040     UnparentAndReset(actor);
1041
1042     application.SendNotification();
1043     application.Render(16);
1044   }
1045   application.SendNotification();
1046   application.Render(16);
1047
1048   DALI_TEST_EQUALS(imageInterface->mExtensionCreateCalls, 1, TEST_LOCATION);
1049   DALI_TEST_EQUALS(imageInterface->mExtensionDestroyCalls, 1, TEST_LOCATION);
1050
1051   END_TEST;
1052 }
1053
1054 int UtcDaliNativeImageTexture02(void)
1055 {
1056   TestApplication application;
1057   tet_infoline("UtcDaliNativeImageTexture02 - test error on TargetTexture");
1058
1059   TestNativeImagePointer imageInterface = TestNativeImage::New(16, 16);
1060   imageInterface->mTargetTextureError   = 1u;
1061   {
1062     Texture texture = Texture::New(*(imageInterface.Get()));
1063     Actor   actor   = CreateRenderableActor(texture, "", "");
1064     application.GetScene().Add(actor);
1065
1066     DALI_TEST_CHECK(texture);
1067
1068     application.SendNotification();
1069     application.Render(16);
1070
1071     // Expect 2 attempts to create the texture - once when adding the texture
1072     // to the scene-graph, and again since that failed, during the Bind.
1073     // The second one succeeds (TargetTexture only errors once)
1074     DALI_TEST_EQUALS(imageInterface->mExtensionCreateCalls, 2, TEST_LOCATION);
1075     DALI_TEST_EQUALS(imageInterface->mExtensionDestroyCalls, 1, TEST_LOCATION);
1076
1077     UnparentAndReset(actor);
1078
1079     application.SendNotification();
1080     application.Render(16);
1081   }
1082   application.SendNotification();
1083   application.Render(16);
1084
1085   // Expect that there are no further calls to create/destroy resource
1086   DALI_TEST_EQUALS(imageInterface->mExtensionCreateCalls, 2, TEST_LOCATION);
1087   DALI_TEST_EQUALS(imageInterface->mExtensionDestroyCalls, 2, TEST_LOCATION);
1088
1089   END_TEST;
1090 }
1091
1092 int UtcDaliTextureGenerateMipmapsNegative(void)
1093 {
1094   TestApplication application;
1095   Dali::Texture   instance;
1096   try
1097   {
1098     instance.GenerateMipmaps();
1099     DALI_TEST_CHECK(false); // Should not get here
1100   }
1101   catch(...)
1102   {
1103     DALI_TEST_CHECK(true); // We expect an assert
1104   }
1105   END_TEST;
1106 }
1107
1108 int UtcDaliTextureUploadNegative01(void)
1109 {
1110   TestApplication application;
1111   Dali::Texture   instance;
1112   try
1113   {
1114     Dali::PixelData arg1;
1115     instance.Upload(arg1);
1116     DALI_TEST_CHECK(false); // Should not get here
1117   }
1118   catch(...)
1119   {
1120     DALI_TEST_CHECK(true); // We expect an assert
1121   }
1122   END_TEST;
1123 }
1124
1125 int UtcDaliTextureUploadNegative02(void)
1126 {
1127   TestApplication application;
1128   Dali::Texture   instance;
1129   try
1130   {
1131     Dali::PixelData arg1;
1132     unsigned int    arg2(0u);
1133     unsigned int    arg3(0u);
1134     unsigned int    arg4(0u);
1135     unsigned int    arg5(0u);
1136     unsigned int    arg6(0u);
1137     unsigned int    arg7(0u);
1138     instance.Upload(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
1139     DALI_TEST_CHECK(false); // Should not get here
1140   }
1141   catch(...)
1142   {
1143     DALI_TEST_CHECK(true); // We expect an assert
1144   }
1145   END_TEST;
1146 }
1147
1148 int UtcDaliTextureGetWidthNegative(void)
1149 {
1150   TestApplication application;
1151   Dali::Texture   instance;
1152   try
1153   {
1154     instance.GetWidth();
1155     DALI_TEST_CHECK(false); // Should not get here
1156   }
1157   catch(...)
1158   {
1159     DALI_TEST_CHECK(true); // We expect an assert
1160   }
1161   END_TEST;
1162 }
1163
1164 int UtcDaliTextureGetHeightNegative(void)
1165 {
1166   TestApplication application;
1167   Dali::Texture   instance;
1168   try
1169   {
1170     instance.GetHeight();
1171     DALI_TEST_CHECK(false); // Should not get here
1172   }
1173   catch(...)
1174   {
1175     DALI_TEST_CHECK(true); // We expect an assert
1176   }
1177   END_TEST;
1178 }
1179
1180 int UtcDaliTextureCheckNativeP(void)
1181 {
1182   TestApplication        application;
1183   TestNativeImagePointer testNativeImage = TestNativeImage::New(64u, 64u);
1184   Texture                nativeTexture   = Texture::New(*testNativeImage);
1185
1186   DALI_TEST_CHECK(nativeTexture);
1187   DALI_TEST_CHECK(DevelTexture::IsNative(nativeTexture));
1188   END_TEST;
1189 }
1190
1191 int UtcDaliTextureCheckNativeN1(void)
1192 {
1193   TestApplication application;
1194   unsigned int    width(64);
1195   unsigned int    height(64);
1196   Texture         texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
1197
1198   DALI_TEST_CHECK(texture);
1199   DALI_TEST_CHECK(!DevelTexture::IsNative(texture));
1200   END_TEST;
1201 }
1202
1203 int UtcDaliTextureCheckNativeN2(void)
1204 {
1205   TestApplication application;
1206   Texture         texture;
1207   try
1208   {
1209     bool native = DevelTexture::IsNative(texture);
1210     DALI_TEST_CHECK(native != native);
1211   }
1212   catch(...)
1213   {
1214     DALI_TEST_CHECK(true);
1215   }
1216   END_TEST;
1217 }
1218
1219 int UtcDaliTextureApplyFragShaderP1(void)
1220 {
1221   TestApplication        application;
1222   TestNativeImagePointer testNativeImage = TestNativeImage::New(64u, 64u);
1223   Texture                nativeTexture   = Texture::New(*testNativeImage);
1224   DALI_TEST_CHECK(nativeTexture);
1225
1226   const std::string baseFragShader =
1227     "varying mediump vec4 uColor;\n"
1228     "void main(){\n"
1229     "  gl_FragColor=uColor;\n"
1230     "}\n";
1231   std::string fragShader = baseFragShader;
1232   bool        applied    = DevelTexture::ApplyNativeFragmentShader(nativeTexture, fragShader);
1233
1234   std::string fragPrefix = "#extension GL_OES_EGL_image_external:require\n";
1235
1236   DALI_TEST_CHECK(applied);
1237   DALI_TEST_CHECK(baseFragShader.compare(fragShader));
1238   DALI_TEST_CHECK(fragShader.compare(fragPrefix + baseFragShader) == 0);
1239   DALI_TEST_CHECK(!fragShader.empty());
1240   END_TEST;
1241 }
1242
1243 int UtcDaliTextureApplyFragShaderP2(void)
1244 {
1245   TestApplication        application;
1246   TestNativeImagePointer testNativeImage = TestNativeImage::New(64u, 64u);
1247   Texture                nativeTexture   = Texture::New(*testNativeImage);
1248   DALI_TEST_CHECK(nativeTexture);
1249
1250   const std::string baseFragShader =
1251     "varying mediump vec4 uColor;\n"
1252     "varying vec2 vTexCoord;\n"
1253     "uniform sampler2D uNative;\n"
1254     "void main(){\n"
1255     "  gl_FragColor=uColor*texture2D(uNative, vTexCoord);\n"
1256     "}\n";
1257   std::string fragShader = baseFragShader;
1258   bool        applied    = DevelTexture::ApplyNativeFragmentShader(nativeTexture, fragShader);
1259
1260   DALI_TEST_CHECK(applied);
1261   DALI_TEST_CHECK(baseFragShader.compare(fragShader));
1262   DALI_TEST_CHECK(!fragShader.empty());
1263   DALI_TEST_CHECK(fragShader.find("samplerExternalOES") < fragShader.length());
1264   END_TEST;
1265 }
1266
1267 int UtcDaliTextureApplyFragShaderN1(void)
1268 {
1269   TestApplication        application;
1270   TestNativeImagePointer testNativeImage = TestNativeImage::New(64u, 64u);
1271   Texture                nativeTexture   = Texture::New(*testNativeImage);
1272   DALI_TEST_CHECK(nativeTexture);
1273
1274   std::string fragShader;
1275   bool        applied = DevelTexture::ApplyNativeFragmentShader(nativeTexture, fragShader);
1276
1277   DALI_TEST_CHECK(!applied);
1278   DALI_TEST_CHECK(fragShader.empty());
1279   END_TEST;
1280 }
1281
1282 int UtcDaliTextureApplyFragShaderN2(void)
1283 {
1284   TestApplication application;
1285   unsigned int    width(64);
1286   unsigned int    height(64);
1287   Texture         texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
1288
1289   const std::string baseFragShader =
1290     "varying mediump vec4 uColor;\n"
1291     "void main(){\n"
1292     "  gl_FragColor=uColor;\n"
1293     "}\n";
1294   std::string fragShader = baseFragShader;
1295   bool        applied    = DevelTexture::ApplyNativeFragmentShader(texture, fragShader);
1296
1297   DALI_TEST_CHECK(!applied);
1298   DALI_TEST_CHECK(!baseFragShader.compare(fragShader));
1299   END_TEST;
1300 }
1301
1302 int UtcDaliTextureGetPixelFormat(void)
1303 {
1304   TestApplication application;
1305   uint32_t        width(64);
1306   uint32_t        height(64);
1307
1308   Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
1309   DALI_TEST_EQUALS(texture.GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION);
1310
1311   texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGB888, width, height);
1312   DALI_TEST_EQUALS(texture.GetPixelFormat(), Pixel::RGB888, TEST_LOCATION);
1313
1314   texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::L8, width, height);
1315   DALI_TEST_EQUALS(texture.GetPixelFormat(), Pixel::L8, TEST_LOCATION);
1316
1317   texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::CHROMINANCE_U, width, height);
1318   DALI_TEST_EQUALS(texture.GetPixelFormat(), Pixel::CHROMINANCE_U, TEST_LOCATION);
1319
1320   END_TEST;
1321 }
1322
1323 int utcDaliTexturePartialUpdate01(void)
1324 {
1325   TestApplication application(
1326     TestApplication::DEFAULT_SURFACE_WIDTH,
1327     TestApplication::DEFAULT_SURFACE_HEIGHT,
1328     TestApplication::DEFAULT_HORIZONTAL_DPI,
1329     TestApplication::DEFAULT_VERTICAL_DPI,
1330     true,
1331     true);
1332
1333   tet_infoline("Check the damaged rect with partial update and texture change");
1334
1335   const TestGlAbstraction::ScissorParams& glScissorParams(application.GetGlAbstraction().GetScissorParams());
1336
1337   std::vector<Rect<int32_t>> damagedRects;
1338   Rect<int32_t>              clippingRect;
1339
1340   Geometry geometry = CreateQuadGeometry();
1341   Shader   shader   = Shader::New("vertexSrc", "fragmentSrc");
1342   Renderer renderer = Renderer::New(geometry, shader);
1343
1344   uint32_t   width(4);
1345   uint32_t   height(4);
1346   Texture    texture    = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
1347   TextureSet textureSet = TextureSet::New();
1348   textureSet.SetTexture(0u, texture);
1349   renderer.SetTextures(textureSet);
1350
1351   Actor actor = Actor::New();
1352   actor.AddRenderer(renderer);
1353
1354   actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
1355   actor.SetProperty(Actor::Property::POSITION, Vector3(16.0f, 16.0f, 0.0f));
1356   actor.SetProperty(Actor::Property::SIZE, Vector3(16.0f, 16.0f, 0.0f));
1357   application.GetScene().Add(actor);
1358
1359   damagedRects.clear();
1360   application.SendNotification();
1361   application.PreRenderWithPartialUpdate(TestApplication::RENDER_FRAME_INTERVAL, nullptr, damagedRects);
1362   DALI_TEST_EQUALS(damagedRects.size(), 1, TEST_LOCATION);
1363
1364   // Aligned by 16
1365   clippingRect = Rect<int32_t>(16, 768, 32, 32); // in screen coordinates
1366   DALI_TEST_EQUALS<Rect<int32_t>>(clippingRect, damagedRects[0], TEST_LOCATION);
1367   application.RenderWithPartialUpdate(damagedRects, clippingRect);
1368   DALI_TEST_EQUALS(clippingRect.x, glScissorParams.x, TEST_LOCATION);
1369   DALI_TEST_EQUALS(clippingRect.y, glScissorParams.y, TEST_LOCATION);
1370   DALI_TEST_EQUALS(clippingRect.width, glScissorParams.width, TEST_LOCATION);
1371   DALI_TEST_EQUALS(clippingRect.height, glScissorParams.height, TEST_LOCATION);
1372
1373   damagedRects.clear();
1374   application.PreRenderWithPartialUpdate(TestApplication::RENDER_FRAME_INTERVAL, nullptr, damagedRects);
1375   application.RenderWithPartialUpdate(damagedRects, clippingRect);
1376
1377   // Ensure the damaged rect is empty
1378   DALI_TEST_EQUALS(damagedRects.size(), 0, TEST_LOCATION);
1379
1380   // Upload texture
1381   uint32_t  bufferSize(width * height * 4);
1382   uint8_t*  buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
1383   PixelData pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE);
1384   texture.Upload(pixelData);
1385
1386   damagedRects.clear();
1387   application.SendNotification();
1388   application.PreRenderWithPartialUpdate(TestApplication::RENDER_FRAME_INTERVAL, nullptr, damagedRects);
1389   DALI_TEST_EQUALS(damagedRects.size(), 1, TEST_LOCATION);
1390
1391   // Aligned by 16
1392   clippingRect = Rect<int32_t>(16, 768, 32, 32); // in screen coordinates
1393   DALI_TEST_EQUALS<Rect<int32_t>>(clippingRect, damagedRects[0], TEST_LOCATION);
1394   application.RenderWithPartialUpdate(damagedRects, clippingRect);
1395   DALI_TEST_EQUALS(clippingRect.x, glScissorParams.x, TEST_LOCATION);
1396   DALI_TEST_EQUALS(clippingRect.y, glScissorParams.y, TEST_LOCATION);
1397   DALI_TEST_EQUALS(clippingRect.width, glScissorParams.width, TEST_LOCATION);
1398   DALI_TEST_EQUALS(clippingRect.height, glScissorParams.height, TEST_LOCATION);
1399
1400   damagedRects.clear();
1401   application.PreRenderWithPartialUpdate(TestApplication::RENDER_FRAME_INTERVAL, nullptr, damagedRects);
1402   application.RenderWithPartialUpdate(damagedRects, clippingRect);
1403
1404   // Ensure the damaged rect is empty
1405   DALI_TEST_EQUALS(damagedRects.size(), 0, TEST_LOCATION);
1406
1407   END_TEST;
1408 }
1409
1410 int utcDaliTexturePartialUpdate02(void)
1411 {
1412   TestApplication application(
1413     TestApplication::DEFAULT_SURFACE_WIDTH,
1414     TestApplication::DEFAULT_SURFACE_HEIGHT,
1415     TestApplication::DEFAULT_HORIZONTAL_DPI,
1416     TestApplication::DEFAULT_VERTICAL_DPI,
1417     true,
1418     true);
1419
1420   tet_infoline("Check the damaged rect with partial update and texture change");
1421
1422   const TestGlAbstraction::ScissorParams& glScissorParams(application.GetGlAbstraction().GetScissorParams());
1423
1424   std::vector<Rect<int32_t>> damagedRects;
1425   Rect<int32_t>              clippingRect;
1426
1427   Geometry geometry = CreateQuadGeometry();
1428   Shader   shader   = Shader::New("vertexSrc", "fragmentSrc");
1429   Renderer renderer = Renderer::New(geometry, shader);
1430
1431   uint32_t   width(4);
1432   uint32_t   height(4);
1433   Texture    texture1   = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
1434   Texture    texture2   = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
1435   TextureSet textureSet = TextureSet::New();
1436   textureSet.SetTexture(0u, texture1);
1437   renderer.SetTextures(textureSet);
1438
1439   Actor actor = Actor::New();
1440   actor.AddRenderer(renderer);
1441
1442   actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
1443   actor.SetProperty(Actor::Property::POSITION, Vector3(16.0f, 16.0f, 0.0f));
1444   actor.SetProperty(Actor::Property::SIZE, Vector3(16.0f, 16.0f, 0.0f));
1445   application.GetScene().Add(actor);
1446
1447   damagedRects.clear();
1448   application.SendNotification();
1449   application.PreRenderWithPartialUpdate(TestApplication::RENDER_FRAME_INTERVAL, nullptr, damagedRects);
1450   DALI_TEST_EQUALS(damagedRects.size(), 1, TEST_LOCATION);
1451
1452   // Aligned by 16
1453   clippingRect = Rect<int32_t>(16, 768, 32, 32); // in screen coordinates, includes 3 last frames updates
1454   DALI_TEST_EQUALS<Rect<int32_t>>(clippingRect, damagedRects[0], TEST_LOCATION);
1455   application.RenderWithPartialUpdate(damagedRects, clippingRect);
1456   DALI_TEST_EQUALS(clippingRect.x, glScissorParams.x, TEST_LOCATION);
1457   DALI_TEST_EQUALS(clippingRect.y, glScissorParams.y, TEST_LOCATION);
1458   DALI_TEST_EQUALS(clippingRect.width, glScissorParams.width, TEST_LOCATION);
1459   DALI_TEST_EQUALS(clippingRect.height, glScissorParams.height, TEST_LOCATION);
1460
1461   damagedRects.clear();
1462   application.PreRenderWithPartialUpdate(TestApplication::RENDER_FRAME_INTERVAL, nullptr, damagedRects);
1463   application.RenderWithPartialUpdate(damagedRects, clippingRect);
1464
1465   // Ensure the damaged rect is empty
1466   DALI_TEST_EQUALS(damagedRects.size(), 0, TEST_LOCATION);
1467
1468   // Set another texture
1469   textureSet.SetTexture(0u, texture2);
1470
1471   damagedRects.clear();
1472   application.SendNotification();
1473   application.PreRenderWithPartialUpdate(TestApplication::RENDER_FRAME_INTERVAL, nullptr, damagedRects);
1474   DALI_TEST_EQUALS(damagedRects.size(), 1, TEST_LOCATION);
1475
1476   // Aligned by 16
1477   clippingRect = Rect<int32_t>(16, 768, 32, 32); // in screen coordinates, includes 3 last frames updates
1478   DALI_TEST_EQUALS<Rect<int32_t>>(clippingRect, damagedRects[0], TEST_LOCATION);
1479   application.RenderWithPartialUpdate(damagedRects, clippingRect);
1480   DALI_TEST_EQUALS(clippingRect.x, glScissorParams.x, TEST_LOCATION);
1481   DALI_TEST_EQUALS(clippingRect.y, glScissorParams.y, TEST_LOCATION);
1482   DALI_TEST_EQUALS(clippingRect.width, glScissorParams.width, TEST_LOCATION);
1483   DALI_TEST_EQUALS(clippingRect.height, glScissorParams.height, TEST_LOCATION);
1484
1485   damagedRects.clear();
1486   application.PreRenderWithPartialUpdate(TestApplication::RENDER_FRAME_INTERVAL, nullptr, damagedRects);
1487   application.RenderWithPartialUpdate(damagedRects, clippingRect);
1488
1489   // Ensure the damaged rect is empty
1490   DALI_TEST_EQUALS(damagedRects.size(), 0, TEST_LOCATION);
1491
1492   END_TEST;
1493 }