Send PixelData instead of raw buffer
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Texture.cpp
1 /*
2  * Copyright (c) 2023 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/rendering/texture-devel.h>
20 #include <dali/public-api/dali-core.h>
21 #include <test-native-image.h>
22
23 using namespace Dali;
24
25 #include <mesh-builder.h>
26
27 void texture_set_startup(void)
28 {
29   test_return_value = TET_UNDEF;
30 }
31
32 void texture_set_cleanup(void)
33 {
34   test_return_value = TET_PASS;
35 }
36
37 int UtcDaliTextureNew01(void)
38 {
39   TestApplication application;
40
41   unsigned int width(64);
42   unsigned int height(64);
43   Texture      texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
44
45   DALI_TEST_CHECK(texture);
46   END_TEST;
47 }
48
49 int UtcDaliTextureNew02(void)
50 {
51   TestApplication application;
52   Texture         texture;
53   DALI_TEST_CHECK(!texture);
54   END_TEST;
55 }
56
57 int UtcDaliTextureNew03(void)
58 {
59   TestApplication application;
60
61   // Create a native image source.
62   TestNativeImagePointer testNativeImage = TestNativeImage::New(64u, 64u);
63
64   // Create a texture from the native image source.
65   Texture nativeTexture = Texture::New(*testNativeImage);
66
67   // Check the texture was created OK.
68   DALI_TEST_CHECK(nativeTexture);
69
70   END_TEST;
71 }
72
73 int UtcDaliTextureCopyConstructor(void)
74 {
75   TestApplication application;
76
77   unsigned int width(64);
78   unsigned int height(64);
79   Texture      texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
80
81   Texture textureCopy(texture);
82
83   DALI_TEST_CHECK(textureCopy);
84
85   END_TEST;
86 }
87
88 int UtcDaliTextureAssignmentOperator(void)
89 {
90   TestApplication application;
91   unsigned int    width(64);
92   unsigned int    height(64);
93   Texture         texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
94
95   Texture texture2;
96   DALI_TEST_CHECK(!texture2);
97
98   texture2 = texture;
99   DALI_TEST_CHECK(texture2);
100
101   END_TEST;
102 }
103
104 int UtcDaliTextureMoveConstructor(void)
105 {
106   TestApplication application;
107
108   uint32_t width   = 64;
109   uint32_t height  = 64;
110   Texture  texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
111   DALI_TEST_CHECK(texture);
112   DALI_TEST_EQUALS(1, texture.GetBaseObject().ReferenceCount(), TEST_LOCATION);
113   DALI_TEST_EQUALS(texture.GetWidth(), width, TEST_LOCATION);
114   DALI_TEST_EQUALS(texture.GetHeight(), height, TEST_LOCATION);
115
116   Texture move = std::move(texture);
117   DALI_TEST_CHECK(move);
118   DALI_TEST_EQUALS(1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION);
119   DALI_TEST_EQUALS(move.GetWidth(), width, TEST_LOCATION);
120   DALI_TEST_EQUALS(move.GetHeight(), height, TEST_LOCATION);
121   DALI_TEST_CHECK(!texture);
122
123   END_TEST;
124 }
125
126 int UtcDaliTextureMoveAssignment(void)
127 {
128   TestApplication application;
129
130   uint32_t width   = 64;
131   uint32_t height  = 64;
132   Texture  texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
133   DALI_TEST_CHECK(texture);
134   DALI_TEST_EQUALS(1, texture.GetBaseObject().ReferenceCount(), TEST_LOCATION);
135   DALI_TEST_EQUALS(texture.GetWidth(), width, TEST_LOCATION);
136   DALI_TEST_EQUALS(texture.GetHeight(), height, TEST_LOCATION);
137
138   Texture move;
139   move = std::move(texture);
140   DALI_TEST_CHECK(move);
141   DALI_TEST_EQUALS(1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION);
142   DALI_TEST_EQUALS(move.GetWidth(), width, TEST_LOCATION);
143   DALI_TEST_EQUALS(move.GetHeight(), height, TEST_LOCATION);
144   DALI_TEST_CHECK(!texture);
145
146   END_TEST;
147 }
148
149 int UtcDaliTextureDownCast01(void)
150 {
151   TestApplication application;
152   unsigned int    width(64);
153   unsigned int    height(64);
154   Texture         texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
155
156   BaseHandle handle(texture);
157   Texture    texture2 = Texture::DownCast(handle);
158   DALI_TEST_CHECK(texture2);
159
160   END_TEST;
161 }
162
163 int UtcDaliTextureDownCast02(void)
164 {
165   TestApplication application;
166
167   Handle  handle  = Handle::New(); // Create a custom object
168   Texture texture = Texture::DownCast(handle);
169   DALI_TEST_CHECK(!texture);
170   END_TEST;
171 }
172
173 int UtcDaliTextureUpload01(void)
174 {
175   TestApplication application;
176
177   //Create the texture
178   unsigned int width(64);
179   unsigned int height(64);
180   Texture      texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
181
182   application.GetGlAbstraction().EnableTextureCallTrace(true);
183
184   application.SendNotification();
185   application.Render();
186
187   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
188
189   //Upload data to the texture
190   callStack.Reset();
191
192   unsigned int   bufferSize(width * height * 4);
193   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
194   PixelData      pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE);
195   texture.Upload(pixelData);
196   application.SendNotification();
197   application.Render();
198
199   //TexImage2D should be called to upload the data
200   {
201     std::stringstream out;
202     out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
203     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
204   }
205
206   //Upload part of the texture
207   callStack.Reset();
208   bufferSize                  = width * height;
209   buffer                      = reinterpret_cast<unsigned char*>(malloc(bufferSize));
210   PixelData pixelDataSubImage = PixelData::New(buffer, bufferSize, width / 2, height / 2, Pixel::RGBA8888, PixelData::FREE);
211   texture.Upload(pixelDataSubImage, 0u, 0u, width / 2, height / 2, width / 2, height / 2);
212   application.SendNotification();
213   application.Render();
214
215   //TexSubImage2D should be called to upload the data
216   {
217     std::stringstream out;
218     out << GL_TEXTURE_2D << ", " << 0u << ", " << width / 2 << ", " << height / 2 << ", " << width / 2 << ", " << height / 2;
219     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexSubImage2D", out.str().c_str()));
220   }
221
222   END_TEST;
223 }
224
225 int UtcDaliTextureUpload02(void)
226 {
227   TestApplication application;
228
229   //Create the texture
230   unsigned int width(64);
231   unsigned int height(64);
232   Texture      texture = CreateTexture(TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height);
233
234   application.GetGlAbstraction().EnableTextureCallTrace(true);
235
236   application.SendNotification();
237   application.Render();
238
239   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
240
241   tet_infoline("TexImage2D should be called six times with a null pointer to reserve storage for the six textures of the cube map");
242   for(unsigned int i(0); i < 6; ++i)
243   {
244     std::stringstream out;
245     out << GL_TEXTURE_CUBE_MAP_POSITIVE_X + i << ", " << 0u << ", " << width << ", " << height;
246     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
247   }
248
249   unsigned int   bufferSize(width * height * 4);
250   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
251   PixelData      pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE);
252
253   //Upload data to the POSITIVE_X face of the texture
254   {
255     callStack.Reset();
256
257     texture.Upload(pixelData, CubeMapLayer::POSITIVE_X, 0u, 0u, 0u, width, height);
258     application.SendNotification();
259     application.Render();
260
261     //TexImage2D should be called to upload the data to the POSITIVE_X face
262     {
263       std::stringstream out;
264       out << GL_TEXTURE_CUBE_MAP_POSITIVE_X << ", " << 0u << ", " << width << ", " << height;
265       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
266     }
267   }
268
269   //Upload data to the NEGATIVE_X face of the texture
270   {
271     callStack.Reset();
272
273     texture.Upload(pixelData, CubeMapLayer::NEGATIVE_X, 0u, 0u, 0u, width, height);
274     application.SendNotification();
275     application.Render();
276
277     //TexImage2D should be called to upload the data to the NEGATIVE_X face
278     {
279       std::stringstream out;
280       out << GL_TEXTURE_CUBE_MAP_NEGATIVE_X << ", " << 0u << ", " << width << ", " << height;
281       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
282     }
283   }
284
285   //Upload data to the POSITIVE_Y face of the texture
286   {
287     callStack.Reset();
288     texture.Upload(pixelData, CubeMapLayer::POSITIVE_Y, 0u, 0u, 0u, width, height);
289     application.SendNotification();
290     application.Render();
291
292     //TexImage2D should be called to upload the data to the POSITIVE_Y face
293     {
294       std::stringstream out;
295       out << GL_TEXTURE_CUBE_MAP_POSITIVE_Y << ", " << 0u << ", " << width << ", " << height;
296       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
297     }
298   }
299
300   //Upload data to the NEGATIVE_Y face of the texture
301   {
302     callStack.Reset();
303     texture.Upload(pixelData, CubeMapLayer::NEGATIVE_Y, 0u, 0u, 0u, width, height);
304     application.SendNotification();
305     application.Render();
306
307     //TexImage2D should be called to upload the data to the NEGATIVE_Y face
308     {
309       std::stringstream out;
310       out << GL_TEXTURE_CUBE_MAP_NEGATIVE_Y << ", " << 0u << ", " << width << ", " << height;
311       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
312     }
313   }
314
315   //Upload data to the POSITIVE_Z face of the texture
316   {
317     callStack.Reset();
318     texture.Upload(pixelData, CubeMapLayer::POSITIVE_Z, 0u, 0u, 0u, width, height);
319     application.SendNotification();
320     application.Render();
321
322     //TexImage2D should be called to upload the data to the POSITIVE_Z face
323     {
324       std::stringstream out;
325       out << GL_TEXTURE_CUBE_MAP_POSITIVE_Z << ", " << 0u << ", " << width << ", " << height;
326       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
327     }
328   }
329
330   //Upload data to the NEGATIVE_Z face of the texture
331   {
332     callStack.Reset();
333     texture.Upload(pixelData, CubeMapLayer::NEGATIVE_Z, 0u, 0u, 0u, width, height);
334     application.SendNotification();
335     application.Render();
336
337     //TexImage2D should be called to upload the data to the NEGATIVE_Z face
338     {
339       std::stringstream out;
340       out << GL_TEXTURE_CUBE_MAP_NEGATIVE_Z << ", " << 0u << ", " << width << ", " << height;
341       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
342     }
343   }
344
345   END_TEST;
346 }
347
348 int UtcDaliTextureUpload03(void)
349 {
350   TestApplication application;
351
352   //Create the texture
353   unsigned int width(64);
354   unsigned int height(64);
355   unsigned int widthMipmap1(32);
356   unsigned int heightMipmap1(32);
357
358   Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
359
360   application.GetGlAbstraction().EnableTextureCallTrace(true);
361
362   application.SendNotification();
363   application.Render();
364
365   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
366
367   tet_infoline("TexImage2D should be called with a null pointer to reserve storage for the texture in the gpu");
368   {
369     std::stringstream out;
370     out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
371     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
372   }
373
374   //Upload data to the texture mipmap 0 and mipmap 1
375   callStack.Reset();
376
377   unsigned int   bufferSize(width * height * 4);
378   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
379   PixelData      pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE);
380   texture.Upload(pixelData, 0u, 0u, 0u, 0u, width, height);
381
382   bufferSize                 = widthMipmap1 * heightMipmap1 * 4;
383   buffer                     = reinterpret_cast<unsigned char*>(malloc(bufferSize));
384   PixelData pixelDataMipmap1 = PixelData::New(buffer, bufferSize, widthMipmap1, heightMipmap1, Pixel::RGBA8888, PixelData::FREE);
385   texture.Upload(pixelDataMipmap1, 0u, 1u, 0u, 0u, widthMipmap1, heightMipmap1);
386   application.SendNotification();
387   application.Render();
388
389   //TexImage2D should be called to upload the data to mipmaps 0 and 1
390   {
391     std::stringstream out;
392     out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
393     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
394   }
395   {
396     std::stringstream out;
397     out << GL_TEXTURE_2D << ", " << 1u << ", " << widthMipmap1 << ", " << heightMipmap1;
398     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
399   }
400
401   END_TEST;
402 }
403
404 int UtcDaliTextureUpload04(void)
405 {
406   TestApplication application;
407
408   //Create the texture
409   unsigned int width(64);
410   unsigned int height(64);
411   unsigned int widthMipmap1(32);
412   unsigned int heightMipmap1(32);
413
414   Texture texture = CreateTexture(TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height);
415
416   application.GetGlAbstraction().EnableTextureCallTrace(true);
417   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
418
419   //Upload data to the NEGATIVE_X face mipmap 0 and mipmap 1
420   unsigned int   bufferSize(width * height * 4);
421   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
422   PixelData      pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE);
423   texture.Upload(pixelData, CubeMapLayer::NEGATIVE_X, 0u, 0u, 0u, width, height);
424
425   bufferSize                 = widthMipmap1 * heightMipmap1 * 4;
426   buffer                     = reinterpret_cast<unsigned char*>(malloc(bufferSize));
427   PixelData pixelDataMipmap1 = PixelData::New(buffer, bufferSize, widthMipmap1, heightMipmap1, Pixel::RGBA8888, PixelData::FREE);
428   texture.Upload(pixelDataMipmap1, CubeMapLayer::NEGATIVE_X, 1u, 0u, 0u, widthMipmap1, heightMipmap1);
429   application.SendNotification();
430   application.Render();
431
432   //TexImage2D should be called to upload the data to mipmaps 0 and 1
433   {
434     std::stringstream out;
435     out << GL_TEXTURE_CUBE_MAP_NEGATIVE_X << ", " << 0u << ", " << width << ", " << height;
436     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
437   }
438   {
439     std::stringstream out;
440     out << GL_TEXTURE_CUBE_MAP_NEGATIVE_X << ", " << 1u << ", " << widthMipmap1 << ", " << heightMipmap1;
441     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
442   }
443
444   END_TEST;
445 }
446
447 int UtcDaliTextureUpload05(void)
448 {
449   Pixel::Format COMPRESSED_PIXEL_FORMATS[] =
450     {
451       Pixel::COMPRESSED_R11_EAC,
452       Pixel::COMPRESSED_SIGNED_R11_EAC,
453       Pixel::COMPRESSED_RG11_EAC,
454       Pixel::COMPRESSED_SIGNED_RG11_EAC,
455       Pixel::COMPRESSED_RGB8_ETC2,
456       Pixel::COMPRESSED_SRGB8_ETC2,
457       Pixel::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2,
458       Pixel::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2,
459       Pixel::COMPRESSED_RGBA8_ETC2_EAC,
460       Pixel::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC,
461       Pixel::COMPRESSED_RGB8_ETC1,
462       Pixel::COMPRESSED_RGB_PVRTC_4BPPV1,
463       Pixel::COMPRESSED_RGBA_ASTC_4x4_KHR,
464       Pixel::COMPRESSED_RGBA_ASTC_5x4_KHR,
465       Pixel::COMPRESSED_RGBA_ASTC_5x5_KHR,
466       Pixel::COMPRESSED_RGBA_ASTC_6x5_KHR,
467       Pixel::COMPRESSED_RGBA_ASTC_6x6_KHR,
468       Pixel::COMPRESSED_RGBA_ASTC_8x5_KHR,
469       Pixel::COMPRESSED_RGBA_ASTC_8x6_KHR,
470       Pixel::COMPRESSED_RGBA_ASTC_8x8_KHR,
471       Pixel::COMPRESSED_RGBA_ASTC_10x5_KHR,
472       Pixel::COMPRESSED_RGBA_ASTC_10x6_KHR,
473       Pixel::COMPRESSED_RGBA_ASTC_10x8_KHR,
474       Pixel::COMPRESSED_RGBA_ASTC_10x10_KHR,
475       Pixel::COMPRESSED_RGBA_ASTC_12x10_KHR,
476       Pixel::COMPRESSED_RGBA_ASTC_12x12_KHR,
477       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR,
478       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR,
479       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR,
480       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR,
481       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR,
482       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR,
483       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR,
484       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR,
485       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR,
486       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR,
487       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR,
488       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR,
489       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR,
490       Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR,
491     };
492   const unsigned int NUMBER_OF_COMPRESSED_PIXEL_FORMATS = sizeof(COMPRESSED_PIXEL_FORMATS) / sizeof(Pixel::Format);
493
494   for(unsigned int index = 0; index < NUMBER_OF_COMPRESSED_PIXEL_FORMATS; ++index)
495   {
496     TestApplication application;
497
498     //Create a texture with a compressed format
499     unsigned int width(64);
500     unsigned int height(64);
501     Texture      texture = CreateTexture(TextureType::TEXTURE_2D, COMPRESSED_PIXEL_FORMATS[index], width, height);
502
503     application.GetGlAbstraction().EnableTextureCallTrace(true);
504
505     application.SendNotification();
506     application.Render();
507
508     TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
509
510     tet_infoline("CompressedTexImage2D should be called with a null pointer to reserve storage for the texture in the gpu");
511     {
512       std::stringstream out;
513       out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
514       DALI_TEST_CHECK(callStack.FindMethodAndParams("CompressedTexImage2D", out.str().c_str()));
515     }
516
517     //Upload data to the texture
518     callStack.Reset();
519
520     unsigned int   bufferSize(width * height * 4);
521     unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
522     PixelData      pixelData = PixelData::New(buffer, bufferSize, width, height, COMPRESSED_PIXEL_FORMATS[index], PixelData::FREE);
523     texture.Upload(pixelData);
524     application.SendNotification();
525     application.Render();
526
527     //CompressedTexImage2D should be called to upload the data
528     {
529       std::stringstream out;
530       out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
531       DALI_TEST_CHECK(callStack.FindMethodAndParams("CompressedTexImage2D", out.str().c_str()));
532     }
533
534     //Upload part of the texture
535     callStack.Reset();
536     bufferSize                  = width * height;
537     buffer                      = reinterpret_cast<unsigned char*>(malloc(bufferSize));
538     PixelData pixelDataSubImage = PixelData::New(buffer, bufferSize, width / 2, height / 2, COMPRESSED_PIXEL_FORMATS[index], PixelData::FREE);
539     texture.Upload(pixelDataSubImage, 0u, 0u, width / 2, height / 2, width / 2, height / 2);
540     application.SendNotification();
541     application.Render();
542
543     //CompressedTexSubImage2D should be called to upload the data
544     {
545       std::stringstream out;
546       out << GL_TEXTURE_2D << ", " << 0u << ", " << width / 2 << ", " << height / 2 << ", " << width / 2 << ", " << height / 2;
547       DALI_TEST_CHECK(callStack.FindMethodAndParams("CompressedTexSubImage2D", out.str().c_str()));
548     }
549
550     application.GetGlAbstraction().ResetTextureCallStack();
551   }
552
553   END_TEST;
554 }
555
556 int UtcDaliTextureUpload06(void)
557 {
558   TestApplication application;
559
560   //Create the texture
561   unsigned int width(64);
562   unsigned int height(64);
563   tet_infoline("Creating a Texure with an alpha channel");
564   Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
565
566   application.GetGlAbstraction().EnableTextureCallTrace(true);
567
568   application.SendNotification();
569   application.Render();
570
571   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
572
573   tet_infoline("TexImage2D should be called with a null pointer to reserve storage for the texture in the gpu");
574   {
575     std::stringstream out;
576     out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
577     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
578   }
579
580   tet_infoline("Upload data to the texture");
581   callStack.Reset();
582
583   tet_infoline("Creating a RGB pixel buffer and adding that to the texture to ensure it is handled correctly");
584   unsigned int   bufferSize(width * height * 3);
585   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
586   PixelData      pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGB888, PixelData::FREE);
587   texture.Upload(pixelData);
588   application.SendNotification();
589   application.Render();
590
591   tet_infoline("TexImage2D should be called to upload the data");
592   {
593     std::stringstream out;
594     out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
595     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
596   }
597
598   END_TEST;
599 }
600
601 int UtcDaliTextureUpload07(void)
602 {
603   Pixel::Format FLOATING_POINT_PIXEL_FORMATS[] =
604     {
605       Pixel::RGB16F,
606       Pixel::RGB32F,
607     };
608   const unsigned int NUMBER_OF_FLOATING_POINT_PIXEL_FORMATS = sizeof(FLOATING_POINT_PIXEL_FORMATS) / sizeof(Pixel::Format);
609
610   for(unsigned int index = 0; index < NUMBER_OF_FLOATING_POINT_PIXEL_FORMATS; ++index)
611   {
612     TestApplication application;
613
614     //Create the texture
615     unsigned int width(64);
616     unsigned int height(64);
617     tet_infoline("Creating a floating point texture");
618     Texture texture = CreateTexture(TextureType::TEXTURE_2D, FLOATING_POINT_PIXEL_FORMATS[index], width, height);
619
620     application.GetGlAbstraction().EnableTextureCallTrace(true);
621
622     application.SendNotification();
623     application.Render();
624
625     TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
626
627     tet_infoline("TexImage2D should be called with a null pointer to reserve storage for the texture in the gpu");
628     {
629       std::stringstream out;
630       out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
631       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
632     }
633
634     tet_infoline("Upload data to the texture");
635     callStack.Reset();
636
637     tet_infoline("Creating a RGB pixel buffer and adding that to the texture to ensure it is handled correctly");
638     unsigned int   bufferSize(width * height * 3);
639     unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
640     PixelData      pixelData = PixelData::New(buffer, bufferSize, width, height, FLOATING_POINT_PIXEL_FORMATS[index], PixelData::FREE);
641     texture.Upload(pixelData);
642     application.SendNotification();
643     application.Render();
644
645     tet_infoline("TexImage2D should be called to upload the data");
646     {
647       std::stringstream out;
648       out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
649       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
650     }
651   }
652
653   END_TEST;
654 }
655
656 int UtcDaliTextureUploadSubPixelData01(void)
657 {
658   TestApplication application;
659
660   //Create the texture
661   unsigned int width(64);
662   unsigned int height(64);
663   Texture      texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
664
665   application.GetGlAbstraction().EnableTextureCallTrace(true);
666
667   application.SendNotification();
668   application.Render();
669
670   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
671
672   //Upload data to the texture
673   callStack.Reset();
674
675   uint32_t bufferWidth   = width * 2;
676   uint32_t bufferHeight  = height * 2;
677   uint32_t bufferXOffset = width;
678   uint32_t bufferYOffset = height;
679
680   unsigned int   bufferSize(bufferWidth * bufferHeight * 4);
681   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
682   PixelData      pixelData = PixelData::New(buffer, bufferSize, bufferWidth, bufferHeight, Pixel::RGBA8888, PixelData::FREE);
683   DevelTexture::UploadSubPixelData(texture, pixelData, bufferXOffset, bufferYOffset, width, height);
684   application.SendNotification();
685   application.Render();
686
687   //TexImage2D should be called to upload the data
688   {
689     std::stringstream out;
690     out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
691     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
692   }
693
694   //Upload part of the texture
695   callStack.Reset();
696   DevelTexture::UploadSubPixelData(texture, pixelData, bufferXOffset, bufferYOffset, width / 2, height / 2, 0u, 0u, width / 2, height / 2, width / 2, height / 2);
697   application.SendNotification();
698   application.Render();
699
700   //TexSubImage2D should be called to upload the data
701   {
702     std::stringstream out;
703     out << GL_TEXTURE_2D << ", " << 0u << ", " << width / 2 << ", " << height / 2 << ", " << width / 2 << ", " << height / 2;
704     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexSubImage2D", out.str().c_str()));
705   }
706
707   END_TEST;
708 }
709
710 int UtcDaliTextureUploadSubPixelData02(void)
711 {
712   TestApplication application;
713
714   //Create the texture
715   unsigned int width(64);
716   unsigned int height(64);
717   Texture      texture = CreateTexture(TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height);
718
719   application.GetGlAbstraction().EnableTextureCallTrace(true);
720
721   application.SendNotification();
722   application.Render();
723
724   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
725
726   tet_infoline("TexImage2D should be called six times with a null pointer to reserve storage for the six textures of the cube map");
727   for(unsigned int i(0); i < 6; ++i)
728   {
729     std::stringstream out;
730     out << GL_TEXTURE_CUBE_MAP_POSITIVE_X + i << ", " << 0u << ", " << width << ", " << height;
731     DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
732   }
733
734   uint32_t bufferWidth   = width * 2;
735   uint32_t bufferHeight  = height * 2;
736   uint32_t bufferXOffset = width;
737   uint32_t bufferYOffset = height;
738
739   unsigned int   bufferSize(bufferWidth * bufferHeight * 4);
740   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
741   PixelData      pixelData = PixelData::New(buffer, bufferSize, bufferWidth, bufferHeight, Pixel::RGBA8888, PixelData::FREE);
742
743   //Upload data to the POSITIVE_X face of the texture
744   {
745     callStack.Reset();
746
747     DevelTexture::UploadSubPixelData(texture, pixelData, bufferXOffset, bufferYOffset, width, height, CubeMapLayer::POSITIVE_X, 0u, 0u, 0u, width, height);
748     application.SendNotification();
749     application.Render();
750
751     //TexImage2D should be called to upload the data to the POSITIVE_X face
752     {
753       std::stringstream out;
754       out << GL_TEXTURE_CUBE_MAP_POSITIVE_X << ", " << 0u << ", " << width << ", " << height;
755       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
756     }
757   }
758
759   //Upload data to the NEGATIVE_X face of the texture
760   {
761     callStack.Reset();
762
763     DevelTexture::UploadSubPixelData(texture, pixelData, bufferXOffset, bufferYOffset, width, height, CubeMapLayer::NEGATIVE_X, 0u, 0u, 0u, width, height);
764     application.SendNotification();
765     application.Render();
766
767     //TexImage2D should be called to upload the data to the NEGATIVE_X face
768     {
769       std::stringstream out;
770       out << GL_TEXTURE_CUBE_MAP_NEGATIVE_X << ", " << 0u << ", " << width << ", " << height;
771       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
772     }
773   }
774
775   //Upload data to the POSITIVE_Y face of the texture
776   {
777     callStack.Reset();
778     DevelTexture::UploadSubPixelData(texture, pixelData, bufferXOffset, bufferYOffset, width, height, CubeMapLayer::POSITIVE_Y, 0u, 0u, 0u, width, height);
779     application.SendNotification();
780     application.Render();
781
782     //TexImage2D should be called to upload the data to the POSITIVE_Y face
783     {
784       std::stringstream out;
785       out << GL_TEXTURE_CUBE_MAP_POSITIVE_Y << ", " << 0u << ", " << width << ", " << height;
786       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
787     }
788   }
789
790   //Upload data to the NEGATIVE_Y face of the texture
791   {
792     callStack.Reset();
793     DevelTexture::UploadSubPixelData(texture, pixelData, bufferXOffset, bufferYOffset, width, height, CubeMapLayer::NEGATIVE_Y, 0u, 0u, 0u, width, height);
794     application.SendNotification();
795     application.Render();
796
797     //TexImage2D should be called to upload the data to the NEGATIVE_Y face
798     {
799       std::stringstream out;
800       out << GL_TEXTURE_CUBE_MAP_NEGATIVE_Y << ", " << 0u << ", " << width << ", " << height;
801       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
802     }
803   }
804
805   //Upload data to the POSITIVE_Z face of the texture
806   {
807     callStack.Reset();
808     DevelTexture::UploadSubPixelData(texture, pixelData, bufferXOffset, bufferYOffset, width, height, CubeMapLayer::POSITIVE_Z, 0u, 0u, 0u, width, height);
809     application.SendNotification();
810     application.Render();
811
812     //TexImage2D should be called to upload the data to the POSITIVE_Z face
813     {
814       std::stringstream out;
815       out << GL_TEXTURE_CUBE_MAP_POSITIVE_Z << ", " << 0u << ", " << width << ", " << height;
816       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
817     }
818   }
819
820   //Upload data to the NEGATIVE_Z face of the texture
821   {
822     callStack.Reset();
823     DevelTexture::UploadSubPixelData(texture, pixelData, bufferXOffset, bufferYOffset, width, height, CubeMapLayer::NEGATIVE_Z, 0u, 0u, 0u, width, height);
824     application.SendNotification();
825     application.Render();
826
827     //TexImage2D should be called to upload the data to the NEGATIVE_Z face
828     {
829       std::stringstream out;
830       out << GL_TEXTURE_CUBE_MAP_NEGATIVE_Z << ", " << 0u << ", " << width << ", " << height;
831       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
832     }
833   }
834
835   END_TEST;
836 }
837
838 int UtcDaliTextureUploadPixelFormats(void)
839 {
840   TestApplication application;
841   application.GetGlAbstraction().EnableTextureCallTrace(true);
842
843   //Create the texture
844   unsigned int width(64);
845   unsigned int height(64);
846
847   std::vector<Pixel::Format> formats =
848     {
849       Pixel::A8,
850       Pixel::L8,
851       Pixel::LA88,
852       Pixel::RGB565,
853       Pixel::BGR565,
854       Pixel::RGBA4444,
855       Pixel::BGRA4444,
856       Pixel::RGBA5551,
857       Pixel::BGRA5551,
858       Pixel::RGB888,
859       Pixel::RGB8888,
860       Pixel::BGR8888,
861       Pixel::RGBA8888,
862       Pixel::BGRA8888,
863       Pixel::DEPTH_UNSIGNED_INT,
864       Pixel::DEPTH_FLOAT,
865       Pixel::DEPTH_STENCIL,
866       Pixel::RGB16F,
867       Pixel::RGB32F,
868       Pixel::R11G11B10F,
869       Pixel::CHROMINANCE_U,
870       Pixel::CHROMINANCE_V};
871
872   for(auto format : formats)
873   {
874     tet_infoline("Creating a Texure with a new or recent format");
875     Texture texture = CreateTexture(TextureType::TEXTURE_2D, format, width, height);
876
877     application.SendNotification();
878     application.Render();
879
880     TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
881
882     tet_infoline("TexImage2D should be called twice per texture");
883     DALI_TEST_EQUALS(callStack.CountMethod("TexImage2D"), 2, TEST_LOCATION);
884     {
885       std::stringstream out;
886       out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
887       DALI_TEST_CHECK(callStack.FindMethodAndParams("TexImage2D", out.str().c_str()));
888     }
889     callStack.Reset();
890   }
891
892   END_TEST;
893 }
894
895 int UtcDaliTextureUploadSmallerThanSize(void)
896 {
897   TestApplication application;
898
899   //Create the texture
900   unsigned int width(64);
901   unsigned int height(64);
902   Texture      texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
903
904   application.GetGlAbstraction().EnableTextureCallTrace(true);
905
906   application.SendNotification();
907   application.Render();
908
909   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
910   callStack.EnableLogging(true);
911   TraceCallStack& texParamCallStack = application.GetGlAbstraction().GetTexParameterTrace();
912   texParamCallStack.EnableLogging(true);
913
914   tet_infoline("TexImage2D should be called with a null pointer to reserve storage for the texture in the gpu");
915   {
916     std::stringstream out;
917     out << GL_TEXTURE_2D << ", " << 0u << ", " << width << ", " << height;
918     std::string params;
919     DALI_TEST_CHECK(callStack.FindMethodAndGetParameters("TexImage2D", params));
920     DALI_TEST_EQUALS(out.str(), params, TEST_LOCATION);
921   }
922
923   //Upload data to the texture
924   callStack.Reset();
925
926   unsigned int   bufferSize(width * height * 4);
927   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
928   PixelData      pixelData = PixelData::New(buffer, bufferSize, width / 2, height / 2, Pixel::RGBA8888, PixelData::FREE);
929   texture.Upload(pixelData);
930   application.SendNotification();
931   application.Render();
932
933   //TexSubImage2D should be called to upload the data
934   {
935     std::stringstream out;
936     out << GL_TEXTURE_2D << ", " << 0u << ", " << 0u << ", " << 0u << ", " << width / 2 << ", " << height / 2;
937     std::string params;
938     DALI_TEST_CHECK(callStack.FindMethodAndGetParameters("TexSubImage2D", params));
939     DALI_TEST_EQUALS(out.str(), params, TEST_LOCATION);
940   }
941   END_TEST;
942 }
943
944 int UtcDaliTextureGenerateMipmaps(void)
945 {
946   TestApplication application;
947   unsigned int    width(64);
948   unsigned int    height(64);
949
950   Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
951   texture.GenerateMipmaps();
952
953   Texture textureCubemap = CreateTexture(TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height);
954   textureCubemap.GenerateMipmaps();
955
956   application.GetGlAbstraction().EnableTextureCallTrace(true);
957   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
958   application.SendNotification();
959   application.Render();
960
961   {
962     std::stringstream out;
963     out << GL_TEXTURE_2D;
964     DALI_TEST_CHECK(callStack.FindMethodAndParams("GenerateMipmap", out.str().c_str()));
965   }
966   {
967     std::stringstream out;
968     out << GL_TEXTURE_CUBE_MAP;
969     DALI_TEST_CHECK(callStack.FindMethodAndParams("GenerateMipmap", out.str().c_str()));
970   }
971
972   END_TEST;
973 }
974
975 int UtcDaliTextureGetWidth(void)
976 {
977   TestApplication application;
978   unsigned int    width(64);
979   unsigned int    height(64);
980
981   Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
982   DALI_TEST_EQUALS(texture.GetWidth(), width, TEST_LOCATION);
983   END_TEST;
984 }
985
986 int UtcDaliTextureGetHeight(void)
987 {
988   TestApplication application;
989   unsigned int    width(64);
990   unsigned int    height(64);
991
992   Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
993   DALI_TEST_EQUALS(texture.GetHeight(), height, TEST_LOCATION);
994
995   END_TEST;
996 }
997
998 int UtcDaliTextureContextLoss(void)
999 {
1000   tet_infoline("UtcDaliTextureContextLoss\n");
1001   TestApplication application;
1002
1003   //Create the texture
1004   unsigned int width(64);
1005   unsigned int height(64);
1006   Texture      texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
1007   DALI_TEST_CHECK(texture);
1008
1009   application.SendNotification();
1010   application.Render(16);
1011
1012   // Lose & regain context (in render 'thread')
1013   application.ResetContext();
1014   DALI_TEST_CHECK(texture);
1015
1016   END_TEST;
1017 }
1018
1019 int UtcDaliNativeImageTexture01(void)
1020 {
1021   TestApplication application;
1022   tet_infoline("UtcDaliNativeImageTexture01");
1023
1024   TestNativeImagePointer imageInterface = TestNativeImage::New(16, 16);
1025   {
1026     Texture texture = Texture::New(*(imageInterface.Get()));
1027     Actor   actor   = CreateRenderableActor(texture, "", "");
1028     application.GetScene().Add(actor);
1029
1030     DALI_TEST_CHECK(texture);
1031
1032     application.SendNotification();
1033     application.Render(16);
1034
1035     DALI_TEST_EQUALS(imageInterface->mExtensionCreateCalls, 1, TEST_LOCATION);
1036     DALI_TEST_EQUALS(imageInterface->mExtensionDestroyCalls, 0, TEST_LOCATION);
1037     DALI_TEST_EQUALS(actor.GetProperty(Actor::Property::SIZE), Property::Value(Vector3(16, 16, 0)), TEST_LOCATION);
1038
1039     UnparentAndReset(actor);
1040
1041     application.SendNotification();
1042     application.Render(16);
1043   }
1044   application.SendNotification();
1045   application.Render(16);
1046
1047   DALI_TEST_EQUALS(imageInterface->mExtensionCreateCalls, 1, TEST_LOCATION);
1048   DALI_TEST_EQUALS(imageInterface->mExtensionDestroyCalls, 1, TEST_LOCATION);
1049
1050   END_TEST;
1051 }
1052
1053 int UtcDaliNativeImageTexture02(void)
1054 {
1055   TestApplication application;
1056   tet_infoline("UtcDaliNativeImageTexture02 - test error on TargetTexture");
1057
1058   TestNativeImagePointer imageInterface = TestNativeImage::New(16, 16);
1059   imageInterface->mTargetTextureError   = 1u;
1060   {
1061     Texture texture = Texture::New(*(imageInterface.Get()));
1062     Actor   actor   = CreateRenderableActor(texture, "", "");
1063     application.GetScene().Add(actor);
1064
1065     DALI_TEST_CHECK(texture);
1066
1067     application.SendNotification();
1068     application.Render(16);
1069
1070     // Expect 2 attempts to create the texture - once when adding the texture
1071     // to the scene-graph, and again since that failed, during the Bind.
1072     // The second one succeeds (TargetTexture only errors once)
1073     DALI_TEST_EQUALS(imageInterface->mExtensionCreateCalls, 2, TEST_LOCATION);
1074     DALI_TEST_EQUALS(imageInterface->mExtensionDestroyCalls, 1, TEST_LOCATION);
1075
1076     UnparentAndReset(actor);
1077
1078     application.SendNotification();
1079     application.Render(16);
1080   }
1081   application.SendNotification();
1082   application.Render(16);
1083
1084   // Expect that there are no further calls to create/destroy resource
1085   DALI_TEST_EQUALS(imageInterface->mExtensionCreateCalls, 2, TEST_LOCATION);
1086   DALI_TEST_EQUALS(imageInterface->mExtensionDestroyCalls, 2, TEST_LOCATION);
1087
1088   END_TEST;
1089 }
1090
1091 int UtcDaliTextureGenerateMipmapsNegative(void)
1092 {
1093   TestApplication application;
1094   Dali::Texture   instance;
1095   try
1096   {
1097     instance.GenerateMipmaps();
1098     DALI_TEST_CHECK(false); // Should not get here
1099   }
1100   catch(...)
1101   {
1102     DALI_TEST_CHECK(true); // We expect an assert
1103   }
1104   END_TEST;
1105 }
1106
1107 int UtcDaliTextureUploadNegative01(void)
1108 {
1109   TestApplication application;
1110   Dali::Texture   instance;
1111   try
1112   {
1113     Dali::PixelData arg1;
1114     instance.Upload(arg1);
1115     DALI_TEST_CHECK(false); // Should not get here
1116   }
1117   catch(...)
1118   {
1119     DALI_TEST_CHECK(true); // We expect an assert
1120   }
1121   END_TEST;
1122 }
1123
1124 int UtcDaliTextureUploadNegative02(void)
1125 {
1126   TestApplication application;
1127   Dali::Texture   instance;
1128   try
1129   {
1130     Dali::PixelData arg1;
1131     unsigned int    arg2(0u);
1132     unsigned int    arg3(0u);
1133     unsigned int    arg4(0u);
1134     unsigned int    arg5(0u);
1135     unsigned int    arg6(0u);
1136     unsigned int    arg7(0u);
1137     instance.Upload(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
1138     DALI_TEST_CHECK(false); // Should not get here
1139   }
1140   catch(...)
1141   {
1142     DALI_TEST_CHECK(true); // We expect an assert
1143   }
1144   END_TEST;
1145 }
1146
1147 int UtcDaliTextureGetWidthNegative(void)
1148 {
1149   TestApplication application;
1150   Dali::Texture   instance;
1151   try
1152   {
1153     instance.GetWidth();
1154     DALI_TEST_CHECK(false); // Should not get here
1155   }
1156   catch(...)
1157   {
1158     DALI_TEST_CHECK(true); // We expect an assert
1159   }
1160   END_TEST;
1161 }
1162
1163 int UtcDaliTextureGetHeightNegative(void)
1164 {
1165   TestApplication application;
1166   Dali::Texture   instance;
1167   try
1168   {
1169     instance.GetHeight();
1170     DALI_TEST_CHECK(false); // Should not get here
1171   }
1172   catch(...)
1173   {
1174     DALI_TEST_CHECK(true); // We expect an assert
1175   }
1176   END_TEST;
1177 }
1178
1179 int UtcDaliTextureCheckNativeP(void)
1180 {
1181   TestApplication        application;
1182   TestNativeImagePointer testNativeImage = TestNativeImage::New(64u, 64u);
1183   Texture                nativeTexture   = Texture::New(*testNativeImage);
1184
1185   DALI_TEST_CHECK(nativeTexture);
1186   DALI_TEST_CHECK(DevelTexture::IsNative(nativeTexture));
1187   END_TEST;
1188 }
1189
1190 int UtcDaliTextureCheckNativeN1(void)
1191 {
1192   TestApplication application;
1193   unsigned int    width(64);
1194   unsigned int    height(64);
1195   Texture         texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
1196
1197   DALI_TEST_CHECK(texture);
1198   DALI_TEST_CHECK(!DevelTexture::IsNative(texture));
1199   END_TEST;
1200 }
1201
1202 int UtcDaliTextureCheckNativeN2(void)
1203 {
1204   TestApplication application;
1205   Texture         texture;
1206   try
1207   {
1208     bool native = DevelTexture::IsNative(texture);
1209     DALI_TEST_CHECK(native != native);
1210   }
1211   catch(...)
1212   {
1213     DALI_TEST_CHECK(true);
1214   }
1215   END_TEST;
1216 }
1217
1218 int UtcDaliTextureApplyFragShaderP1(void)
1219 {
1220   TestApplication        application;
1221   TestNativeImagePointer testNativeImage = TestNativeImage::New(64u, 64u);
1222   Texture                nativeTexture   = Texture::New(*testNativeImage);
1223   DALI_TEST_CHECK(nativeTexture);
1224
1225   const std::string baseFragShader =
1226     "varying mediump vec4 uColor;\n"
1227     "void main(){\n"
1228     "  gl_FragColor=uColor;\n"
1229     "}\n";
1230   std::string fragShader = baseFragShader;
1231   bool        applied    = DevelTexture::ApplyNativeFragmentShader(nativeTexture, fragShader);
1232
1233   std::string fragPrefix = "#extension GL_OES_EGL_image_external:require\n";
1234
1235   DALI_TEST_CHECK(applied);
1236   DALI_TEST_CHECK(baseFragShader.compare(fragShader));
1237   DALI_TEST_CHECK(fragShader.compare(fragPrefix + baseFragShader) == 0);
1238   DALI_TEST_CHECK(!fragShader.empty());
1239   END_TEST;
1240 }
1241
1242 int UtcDaliTextureApplyFragShaderP2(void)
1243 {
1244   TestApplication        application;
1245   TestNativeImagePointer testNativeImage = TestNativeImage::New(64u, 64u);
1246   Texture                nativeTexture   = Texture::New(*testNativeImage);
1247   DALI_TEST_CHECK(nativeTexture);
1248
1249   const std::string baseFragShader =
1250     "varying mediump vec4 uColor;\n"
1251     "varying vec2 vTexCoord;\n"
1252     "uniform sampler2D uNative;\n"
1253     "void main(){\n"
1254     "  gl_FragColor=uColor*texture2D(uNative, vTexCoord);\n"
1255     "}\n";
1256   std::string fragShader = baseFragShader;
1257   bool        applied    = DevelTexture::ApplyNativeFragmentShader(nativeTexture, fragShader);
1258
1259   DALI_TEST_CHECK(applied);
1260   DALI_TEST_CHECK(baseFragShader.compare(fragShader));
1261   DALI_TEST_CHECK(!fragShader.empty());
1262   DALI_TEST_CHECK(fragShader.find("samplerExternalOES") < fragShader.length());
1263   END_TEST;
1264 }
1265
1266 int UtcDaliTextureApplyFragShaderN1(void)
1267 {
1268   TestApplication        application;
1269   TestNativeImagePointer testNativeImage = TestNativeImage::New(64u, 64u);
1270   Texture                nativeTexture   = Texture::New(*testNativeImage);
1271   DALI_TEST_CHECK(nativeTexture);
1272
1273   std::string fragShader;
1274   bool        applied = DevelTexture::ApplyNativeFragmentShader(nativeTexture, fragShader);
1275
1276   DALI_TEST_CHECK(!applied);
1277   DALI_TEST_CHECK(fragShader.empty());
1278   END_TEST;
1279 }
1280
1281 int UtcDaliTextureApplyFragShaderN2(void)
1282 {
1283   TestApplication application;
1284   unsigned int    width(64);
1285   unsigned int    height(64);
1286   Texture         texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
1287
1288   const std::string baseFragShader =
1289     "varying mediump vec4 uColor;\n"
1290     "void main(){\n"
1291     "  gl_FragColor=uColor;\n"
1292     "}\n";
1293   std::string fragShader = baseFragShader;
1294   bool        applied    = DevelTexture::ApplyNativeFragmentShader(texture, fragShader);
1295
1296   DALI_TEST_CHECK(!applied);
1297   DALI_TEST_CHECK(!baseFragShader.compare(fragShader));
1298   END_TEST;
1299 }
1300
1301 int UtcDaliTextureGetPixelFormat(void)
1302 {
1303   TestApplication application;
1304   uint32_t        width(64);
1305   uint32_t        height(64);
1306
1307   Texture texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
1308   DALI_TEST_EQUALS(texture.GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION);
1309
1310   texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGB888, width, height);
1311   DALI_TEST_EQUALS(texture.GetPixelFormat(), Pixel::RGB888, TEST_LOCATION);
1312
1313   texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::L8, width, height);
1314   DALI_TEST_EQUALS(texture.GetPixelFormat(), Pixel::L8, TEST_LOCATION);
1315
1316   texture = CreateTexture(TextureType::TEXTURE_2D, Pixel::CHROMINANCE_U, width, height);
1317   DALI_TEST_EQUALS(texture.GetPixelFormat(), Pixel::CHROMINANCE_U, TEST_LOCATION);
1318
1319   END_TEST;
1320 }
1321
1322 int utcDaliTexturePartialUpdate01(void)
1323 {
1324   TestApplication application(
1325     TestApplication::DEFAULT_SURFACE_WIDTH,
1326     TestApplication::DEFAULT_SURFACE_HEIGHT,
1327     TestApplication::DEFAULT_HORIZONTAL_DPI,
1328     TestApplication::DEFAULT_VERTICAL_DPI,
1329     true,
1330     true);
1331
1332   tet_infoline("Check the damaged rect with partial update and texture change");
1333
1334   const TestGlAbstraction::ScissorParams& glScissorParams(application.GetGlAbstraction().GetScissorParams());
1335
1336   std::vector<Rect<int32_t>> damagedRects;
1337   Rect<int32_t>              clippingRect;
1338
1339   Geometry geometry = CreateQuadGeometry();
1340   Shader   shader   = Shader::New("vertexSrc", "fragmentSrc");
1341   Renderer renderer = Renderer::New(geometry, shader);
1342
1343   uint32_t   width(4);
1344   uint32_t   height(4);
1345   Texture    texture    = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
1346   TextureSet textureSet = TextureSet::New();
1347   textureSet.SetTexture(0u, texture);
1348   renderer.SetTextures(textureSet);
1349
1350   Actor actor = Actor::New();
1351   actor.AddRenderer(renderer);
1352
1353   actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
1354   actor.SetProperty(Actor::Property::POSITION, Vector3(16.0f, 16.0f, 0.0f));
1355   actor.SetProperty(Actor::Property::SIZE, Vector3(16.0f, 16.0f, 0.0f));
1356   application.GetScene().Add(actor);
1357
1358   damagedRects.clear();
1359   application.SendNotification();
1360   application.PreRenderWithPartialUpdate(TestApplication::RENDER_FRAME_INTERVAL, nullptr, damagedRects);
1361   DALI_TEST_EQUALS(damagedRects.size(), 1, TEST_LOCATION);
1362
1363   // Aligned by 16
1364   clippingRect = Rect<int32_t>(16, 768, 32, 32); // in screen coordinates
1365   DALI_TEST_EQUALS<Rect<int32_t>>(clippingRect, damagedRects[0], TEST_LOCATION);
1366   application.RenderWithPartialUpdate(damagedRects, clippingRect);
1367   DALI_TEST_EQUALS(clippingRect.x, glScissorParams.x, TEST_LOCATION);
1368   DALI_TEST_EQUALS(clippingRect.y, glScissorParams.y, TEST_LOCATION);
1369   DALI_TEST_EQUALS(clippingRect.width, glScissorParams.width, TEST_LOCATION);
1370   DALI_TEST_EQUALS(clippingRect.height, glScissorParams.height, TEST_LOCATION);
1371
1372   damagedRects.clear();
1373   application.PreRenderWithPartialUpdate(TestApplication::RENDER_FRAME_INTERVAL, nullptr, damagedRects);
1374   application.RenderWithPartialUpdate(damagedRects, clippingRect);
1375
1376   // Ensure the damaged rect is empty
1377   DALI_TEST_EQUALS(damagedRects.size(), 0, TEST_LOCATION);
1378
1379   // Upload texture
1380   uint32_t  bufferSize(width * height * 4);
1381   uint8_t*  buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
1382   PixelData pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE);
1383   texture.Upload(pixelData);
1384
1385   damagedRects.clear();
1386   application.SendNotification();
1387   application.PreRenderWithPartialUpdate(TestApplication::RENDER_FRAME_INTERVAL, nullptr, damagedRects);
1388   DALI_TEST_EQUALS(damagedRects.size(), 1, TEST_LOCATION);
1389
1390   // Aligned by 16
1391   clippingRect = Rect<int32_t>(16, 768, 32, 32); // in screen coordinates
1392   DALI_TEST_EQUALS<Rect<int32_t>>(clippingRect, damagedRects[0], TEST_LOCATION);
1393   application.RenderWithPartialUpdate(damagedRects, clippingRect);
1394   DALI_TEST_EQUALS(clippingRect.x, glScissorParams.x, TEST_LOCATION);
1395   DALI_TEST_EQUALS(clippingRect.y, glScissorParams.y, TEST_LOCATION);
1396   DALI_TEST_EQUALS(clippingRect.width, glScissorParams.width, TEST_LOCATION);
1397   DALI_TEST_EQUALS(clippingRect.height, glScissorParams.height, TEST_LOCATION);
1398
1399   damagedRects.clear();
1400   application.PreRenderWithPartialUpdate(TestApplication::RENDER_FRAME_INTERVAL, nullptr, damagedRects);
1401   application.RenderWithPartialUpdate(damagedRects, clippingRect);
1402
1403   // Ensure the damaged rect is empty
1404   DALI_TEST_EQUALS(damagedRects.size(), 0, TEST_LOCATION);
1405
1406   END_TEST;
1407 }
1408
1409 int utcDaliTexturePartialUpdate02(void)
1410 {
1411   TestApplication application(
1412     TestApplication::DEFAULT_SURFACE_WIDTH,
1413     TestApplication::DEFAULT_SURFACE_HEIGHT,
1414     TestApplication::DEFAULT_HORIZONTAL_DPI,
1415     TestApplication::DEFAULT_VERTICAL_DPI,
1416     true,
1417     true);
1418
1419   tet_infoline("Check the damaged rect with partial update and texture change");
1420
1421   const TestGlAbstraction::ScissorParams& glScissorParams(application.GetGlAbstraction().GetScissorParams());
1422
1423   std::vector<Rect<int32_t>> damagedRects;
1424   Rect<int32_t>              clippingRect;
1425
1426   Geometry geometry = CreateQuadGeometry();
1427   Shader   shader   = Shader::New("vertexSrc", "fragmentSrc");
1428   Renderer renderer = Renderer::New(geometry, shader);
1429
1430   uint32_t   width(4);
1431   uint32_t   height(4);
1432   Texture    texture1   = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
1433   Texture    texture2   = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
1434   TextureSet textureSet = TextureSet::New();
1435   textureSet.SetTexture(0u, texture1);
1436   renderer.SetTextures(textureSet);
1437
1438   Actor actor = Actor::New();
1439   actor.AddRenderer(renderer);
1440
1441   actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
1442   actor.SetProperty(Actor::Property::POSITION, Vector3(16.0f, 16.0f, 0.0f));
1443   actor.SetProperty(Actor::Property::SIZE, Vector3(16.0f, 16.0f, 0.0f));
1444   application.GetScene().Add(actor);
1445
1446   damagedRects.clear();
1447   application.SendNotification();
1448   application.PreRenderWithPartialUpdate(TestApplication::RENDER_FRAME_INTERVAL, nullptr, damagedRects);
1449   DALI_TEST_EQUALS(damagedRects.size(), 1, TEST_LOCATION);
1450
1451   // Aligned by 16
1452   clippingRect = Rect<int32_t>(16, 768, 32, 32); // in screen coordinates, includes 3 last frames updates
1453   DALI_TEST_EQUALS<Rect<int32_t>>(clippingRect, damagedRects[0], TEST_LOCATION);
1454   application.RenderWithPartialUpdate(damagedRects, clippingRect);
1455   DALI_TEST_EQUALS(clippingRect.x, glScissorParams.x, TEST_LOCATION);
1456   DALI_TEST_EQUALS(clippingRect.y, glScissorParams.y, TEST_LOCATION);
1457   DALI_TEST_EQUALS(clippingRect.width, glScissorParams.width, TEST_LOCATION);
1458   DALI_TEST_EQUALS(clippingRect.height, glScissorParams.height, TEST_LOCATION);
1459
1460   damagedRects.clear();
1461   application.PreRenderWithPartialUpdate(TestApplication::RENDER_FRAME_INTERVAL, nullptr, damagedRects);
1462   application.RenderWithPartialUpdate(damagedRects, clippingRect);
1463
1464   // Ensure the damaged rect is empty
1465   DALI_TEST_EQUALS(damagedRects.size(), 0, TEST_LOCATION);
1466
1467   // Set another texture
1468   textureSet.SetTexture(0u, texture2);
1469
1470   damagedRects.clear();
1471   application.SendNotification();
1472   application.PreRenderWithPartialUpdate(TestApplication::RENDER_FRAME_INTERVAL, nullptr, damagedRects);
1473   DALI_TEST_EQUALS(damagedRects.size(), 1, TEST_LOCATION);
1474
1475   // Aligned by 16
1476   clippingRect = Rect<int32_t>(16, 768, 32, 32); // in screen coordinates, includes 3 last frames updates
1477   DALI_TEST_EQUALS<Rect<int32_t>>(clippingRect, damagedRects[0], TEST_LOCATION);
1478   application.RenderWithPartialUpdate(damagedRects, clippingRect);
1479   DALI_TEST_EQUALS(clippingRect.x, glScissorParams.x, TEST_LOCATION);
1480   DALI_TEST_EQUALS(clippingRect.y, glScissorParams.y, TEST_LOCATION);
1481   DALI_TEST_EQUALS(clippingRect.width, glScissorParams.width, TEST_LOCATION);
1482   DALI_TEST_EQUALS(clippingRect.height, glScissorParams.height, TEST_LOCATION);
1483
1484   damagedRects.clear();
1485   application.PreRenderWithPartialUpdate(TestApplication::RENDER_FRAME_INTERVAL, nullptr, damagedRects);
1486   application.RenderWithPartialUpdate(damagedRects, clippingRect);
1487
1488   // Ensure the damaged rect is empty
1489   DALI_TEST_EQUALS(damagedRects.size(), 0, TEST_LOCATION);
1490
1491   END_TEST;
1492 }