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