[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-graphics-texture.cpp
1 /*
2  * Copyright (c) 2024 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 #include "test-graphics-texture.h"
18 #include <dali/integration-api/pixel-data-integ.h>
19 #include <iostream>
20 #include <sstream>
21
22 namespace
23 {
24 // These match the GL specification
25 const GLint GL_MINIFY_DEFAULT  = GL_NEAREST_MIPMAP_LINEAR;
26 const GLint GL_MAGNIFY_DEFAULT = GL_LINEAR;
27 const GLint GL_WRAP_DEFAULT    = GL_CLAMP_TO_EDGE;
28
29 // These are the Dali defaults
30 const GLint DALI_MINIFY_DEFAULT  = GL_LINEAR;
31 const GLint DALI_MAGNIFY_DEFAULT = GL_LINEAR;
32
33 GLuint GetTextureTarget(Graphics::TextureType type)
34 {
35   GLuint target{GL_TEXTURE_2D};
36
37   switch(type)
38   {
39     case Graphics::TextureType::TEXTURE_2D:
40       target = GL_TEXTURE_2D; // Native texture may override this with GL_TEXTURE_EXTERNAL_OES
41       break;
42     case Graphics::TextureType::TEXTURE_3D:
43       target = GL_TEXTURE_3D;
44       break;
45     case Graphics::TextureType::TEXTURE_CUBEMAP:
46       target = GL_TEXTURE_CUBE_MAP;
47       break;
48   }
49   return target;
50 }
51
52 /**
53  * @brief Whether specified pixel format is compressed.
54  *
55  * @param [in] pixelformat Pixel format
56  * @return true if format is compressed, false otherwise
57  */
58 bool IsCompressedFormat(Graphics::Format pixelFormat)
59 {
60   switch(pixelFormat)
61   {
62     case Graphics::Format::UNDEFINED:
63     case Graphics::Format::L8:
64     case Graphics::Format::L8A8:
65     case Graphics::Format::R4G4_UNORM_PACK8:
66     case Graphics::Format::R4G4B4A4_UNORM_PACK16:
67     case Graphics::Format::B4G4R4A4_UNORM_PACK16:
68     case Graphics::Format::R5G6B5_UNORM_PACK16:
69     case Graphics::Format::B5G6R5_UNORM_PACK16:
70     case Graphics::Format::R5G5B5A1_UNORM_PACK16:
71     case Graphics::Format::B5G5R5A1_UNORM_PACK16:
72     case Graphics::Format::A1R5G5B5_UNORM_PACK16:
73     case Graphics::Format::R8_UNORM:
74     case Graphics::Format::R8_SNORM:
75     case Graphics::Format::R8_USCALED:
76     case Graphics::Format::R8_SSCALED:
77     case Graphics::Format::R8_UINT:
78     case Graphics::Format::R8_SINT:
79     case Graphics::Format::R8_SRGB:
80     case Graphics::Format::R8G8_UNORM:
81     case Graphics::Format::R8G8_SNORM:
82     case Graphics::Format::R8G8_USCALED:
83     case Graphics::Format::R8G8_SSCALED:
84     case Graphics::Format::R8G8_UINT:
85     case Graphics::Format::R8G8_SINT:
86     case Graphics::Format::R8G8_SRGB:
87     case Graphics::Format::R8G8B8_UNORM:
88     case Graphics::Format::R8G8B8_SNORM:
89     case Graphics::Format::R8G8B8_USCALED:
90     case Graphics::Format::R8G8B8_SSCALED:
91     case Graphics::Format::R8G8B8_UINT:
92     case Graphics::Format::R8G8B8_SINT:
93     case Graphics::Format::R8G8B8_SRGB:
94     case Graphics::Format::B8G8R8_UNORM:
95     case Graphics::Format::B8G8R8_SNORM:
96     case Graphics::Format::B8G8R8_USCALED:
97     case Graphics::Format::B8G8R8_SSCALED:
98     case Graphics::Format::B8G8R8_UINT:
99     case Graphics::Format::B8G8R8_SINT:
100     case Graphics::Format::B8G8R8_SRGB:
101     case Graphics::Format::R8G8B8A8_UNORM:
102     case Graphics::Format::R8G8B8A8_SNORM:
103     case Graphics::Format::R8G8B8A8_USCALED:
104     case Graphics::Format::R8G8B8A8_SSCALED:
105     case Graphics::Format::R8G8B8A8_UINT:
106     case Graphics::Format::R8G8B8A8_SINT:
107     case Graphics::Format::R8G8B8A8_SRGB:
108     case Graphics::Format::B8G8R8A8_UNORM:
109     case Graphics::Format::B8G8R8A8_SNORM:
110     case Graphics::Format::B8G8R8A8_USCALED:
111     case Graphics::Format::B8G8R8A8_SSCALED:
112     case Graphics::Format::B8G8R8A8_UINT:
113     case Graphics::Format::B8G8R8A8_SINT:
114     case Graphics::Format::B8G8R8A8_SRGB:
115     case Graphics::Format::A8B8G8R8_UNORM_PACK32:
116     case Graphics::Format::A8B8G8R8_SNORM_PACK32:
117     case Graphics::Format::A8B8G8R8_USCALED_PACK32:
118     case Graphics::Format::A8B8G8R8_SSCALED_PACK32:
119     case Graphics::Format::A8B8G8R8_UINT_PACK32:
120     case Graphics::Format::A8B8G8R8_SINT_PACK32:
121     case Graphics::Format::A8B8G8R8_SRGB_PACK32:
122     case Graphics::Format::A2R10G10B10_UNORM_PACK32:
123     case Graphics::Format::A2R10G10B10_SNORM_PACK32:
124     case Graphics::Format::A2R10G10B10_USCALED_PACK32:
125     case Graphics::Format::A2R10G10B10_SSCALED_PACK32:
126     case Graphics::Format::A2R10G10B10_UINT_PACK32:
127     case Graphics::Format::A2R10G10B10_SINT_PACK32:
128     case Graphics::Format::A2B10G10R10_UNORM_PACK32:
129     case Graphics::Format::A2B10G10R10_SNORM_PACK32:
130     case Graphics::Format::A2B10G10R10_USCALED_PACK32:
131     case Graphics::Format::A2B10G10R10_SSCALED_PACK32:
132     case Graphics::Format::A2B10G10R10_UINT_PACK32:
133     case Graphics::Format::A2B10G10R10_SINT_PACK32:
134     case Graphics::Format::R16_UNORM:
135     case Graphics::Format::R16_SNORM:
136     case Graphics::Format::R16_USCALED:
137     case Graphics::Format::R16_SSCALED:
138     case Graphics::Format::R16_UINT:
139     case Graphics::Format::R16_SINT:
140     case Graphics::Format::R16_SFLOAT:
141     case Graphics::Format::R16G16_UNORM:
142     case Graphics::Format::R16G16_SNORM:
143     case Graphics::Format::R16G16_USCALED:
144     case Graphics::Format::R16G16_SSCALED:
145     case Graphics::Format::R16G16_UINT:
146     case Graphics::Format::R16G16_SINT:
147     case Graphics::Format::R16G16_SFLOAT:
148     case Graphics::Format::R16G16B16_UNORM:
149     case Graphics::Format::R16G16B16_SNORM:
150     case Graphics::Format::R16G16B16_USCALED:
151     case Graphics::Format::R16G16B16_SSCALED:
152     case Graphics::Format::R16G16B16_UINT:
153     case Graphics::Format::R16G16B16_SINT:
154     case Graphics::Format::R16G16B16_SFLOAT:
155     case Graphics::Format::R16G16B16A16_UNORM:
156     case Graphics::Format::R16G16B16A16_SNORM:
157     case Graphics::Format::R16G16B16A16_USCALED:
158     case Graphics::Format::R16G16B16A16_SSCALED:
159     case Graphics::Format::R16G16B16A16_UINT:
160     case Graphics::Format::R16G16B16A16_SINT:
161     case Graphics::Format::R16G16B16A16_SFLOAT:
162     case Graphics::Format::R32_UINT:
163     case Graphics::Format::R32_SINT:
164     case Graphics::Format::R32_SFLOAT:
165     case Graphics::Format::R32G32_UINT:
166     case Graphics::Format::R32G32_SINT:
167     case Graphics::Format::R32G32_SFLOAT:
168     case Graphics::Format::R32G32B32_UINT:
169     case Graphics::Format::R32G32B32_SINT:
170     case Graphics::Format::R32G32B32_SFLOAT:
171     case Graphics::Format::R32G32B32A32_UINT:
172     case Graphics::Format::R32G32B32A32_SINT:
173     case Graphics::Format::R32G32B32A32_SFLOAT:
174     case Graphics::Format::R64_UINT:
175     case Graphics::Format::R64_SINT:
176     case Graphics::Format::R64_SFLOAT:
177     case Graphics::Format::R64G64_UINT:
178     case Graphics::Format::R64G64_SINT:
179     case Graphics::Format::R64G64_SFLOAT:
180     case Graphics::Format::R64G64B64_UINT:
181     case Graphics::Format::R64G64B64_SINT:
182     case Graphics::Format::R64G64B64_SFLOAT:
183     case Graphics::Format::R64G64B64A64_UINT:
184     case Graphics::Format::R64G64B64A64_SINT:
185     case Graphics::Format::R64G64B64A64_SFLOAT:
186     case Graphics::Format::B10G11R11_UFLOAT_PACK32:
187     case Graphics::Format::R11G11B10_UFLOAT_PACK32:
188     case Graphics::Format::E5B9G9R9_UFLOAT_PACK32:
189     case Graphics::Format::D16_UNORM:
190     case Graphics::Format::X8_D24_UNORM_PACK32:
191     case Graphics::Format::D32_SFLOAT:
192     case Graphics::Format::S8_UINT:
193     case Graphics::Format::D16_UNORM_S8_UINT:
194     case Graphics::Format::D24_UNORM_S8_UINT:
195     case Graphics::Format::D32_SFLOAT_S8_UINT:
196     case Graphics::Format::BC1_RGB_UNORM_BLOCK:
197     case Graphics::Format::BC1_RGB_SRGB_BLOCK:
198     case Graphics::Format::BC1_RGBA_UNORM_BLOCK:
199     case Graphics::Format::BC1_RGBA_SRGB_BLOCK:
200     case Graphics::Format::BC2_UNORM_BLOCK:
201     case Graphics::Format::BC2_SRGB_BLOCK:
202     case Graphics::Format::BC3_UNORM_BLOCK:
203     case Graphics::Format::BC3_SRGB_BLOCK:
204     case Graphics::Format::BC4_UNORM_BLOCK:
205     case Graphics::Format::BC4_SNORM_BLOCK:
206     case Graphics::Format::BC5_UNORM_BLOCK:
207     case Graphics::Format::BC5_SNORM_BLOCK:
208     case Graphics::Format::BC6H_UFLOAT_BLOCK:
209     case Graphics::Format::BC6H_SFLOAT_BLOCK:
210     case Graphics::Format::BC7_UNORM_BLOCK:
211     case Graphics::Format::BC7_SRGB_BLOCK:
212     {
213       return false;
214     }
215
216     case Graphics::Format::ETC2_R8G8B8_UNORM_BLOCK:
217     case Graphics::Format::ETC2_R8G8B8_SRGB_BLOCK:
218     case Graphics::Format::ETC2_R8G8B8A1_UNORM_BLOCK:
219     case Graphics::Format::ETC2_R8G8B8A1_SRGB_BLOCK:
220     case Graphics::Format::ETC2_R8G8B8A8_UNORM_BLOCK:
221     case Graphics::Format::ETC2_R8G8B8A8_SRGB_BLOCK:
222     case Graphics::Format::EAC_R11_UNORM_BLOCK:
223     case Graphics::Format::EAC_R11_SNORM_BLOCK:
224     case Graphics::Format::EAC_R11G11_UNORM_BLOCK:
225     case Graphics::Format::EAC_R11G11_SNORM_BLOCK:
226     case Graphics::Format::ASTC_4x4_UNORM_BLOCK:
227     case Graphics::Format::ASTC_4x4_SRGB_BLOCK:
228     case Graphics::Format::ASTC_5x4_UNORM_BLOCK:
229     case Graphics::Format::ASTC_5x4_SRGB_BLOCK:
230     case Graphics::Format::ASTC_5x5_UNORM_BLOCK:
231     case Graphics::Format::ASTC_5x5_SRGB_BLOCK:
232     case Graphics::Format::ASTC_6x5_UNORM_BLOCK:
233     case Graphics::Format::ASTC_6x5_SRGB_BLOCK:
234     case Graphics::Format::ASTC_6x6_UNORM_BLOCK:
235     case Graphics::Format::ASTC_6x6_SRGB_BLOCK:
236     case Graphics::Format::ASTC_8x5_UNORM_BLOCK:
237     case Graphics::Format::ASTC_8x5_SRGB_BLOCK:
238     case Graphics::Format::ASTC_8x6_UNORM_BLOCK:
239     case Graphics::Format::ASTC_8x6_SRGB_BLOCK:
240     case Graphics::Format::ASTC_8x8_UNORM_BLOCK:
241     case Graphics::Format::ASTC_8x8_SRGB_BLOCK:
242     case Graphics::Format::ASTC_10x5_UNORM_BLOCK:
243     case Graphics::Format::ASTC_10x5_SRGB_BLOCK:
244     case Graphics::Format::ASTC_10x6_UNORM_BLOCK:
245     case Graphics::Format::ASTC_10x6_SRGB_BLOCK:
246     case Graphics::Format::ASTC_10x8_UNORM_BLOCK:
247     case Graphics::Format::ASTC_10x8_SRGB_BLOCK:
248     case Graphics::Format::ASTC_10x10_UNORM_BLOCK:
249     case Graphics::Format::ASTC_10x10_SRGB_BLOCK:
250     case Graphics::Format::ASTC_12x10_UNORM_BLOCK:
251     case Graphics::Format::ASTC_12x10_SRGB_BLOCK:
252     case Graphics::Format::ASTC_12x12_UNORM_BLOCK:
253     case Graphics::Format::ASTC_12x12_SRGB_BLOCK:
254     case Graphics::Format::PVRTC1_2BPP_UNORM_BLOCK_IMG:
255     case Graphics::Format::PVRTC1_4BPP_UNORM_BLOCK_IMG:
256     case Graphics::Format::PVRTC2_2BPP_UNORM_BLOCK_IMG:
257     case Graphics::Format::PVRTC2_4BPP_UNORM_BLOCK_IMG:
258     case Graphics::Format::PVRTC1_2BPP_SRGB_BLOCK_IMG:
259     case Graphics::Format::PVRTC1_4BPP_SRGB_BLOCK_IMG:
260     case Graphics::Format::PVRTC2_2BPP_SRGB_BLOCK_IMG:
261     case Graphics::Format::PVRTC2_4BPP_SRGB_BLOCK_IMG:
262     {
263       return true;
264     }
265   }
266
267   return false;
268 }
269
270 /**
271  * @brief Retrives the GL format, GL internal format and pixel data type from a Graphics::Format
272  * @param[in] pixelFormat The pixel format.
273  * @param[out] glFormat The gl format.
274  * @param[out] glInternalFormat The gl internal format.
275  * @param[out] pixelDataType The data type of the pixel data.
276  */
277 void PixelFormatToGl(Graphics::Format pixelFormat, GLenum& glFormat, GLint& glInternalFormat, GLenum& pixelDataType)
278 {
279   // Compressed textures have no pixelDataType, so init to an invalid value:
280   pixelDataType = -1;
281
282   switch(pixelFormat)
283   {
284     case Graphics::Format::R8_UNORM:
285     {
286       pixelDataType = GL_UNSIGNED_BYTE;
287       glFormat      = GL_ALPHA;
288       break;
289     }
290
291     case Graphics::Format::L8:
292     {
293       pixelDataType = GL_UNSIGNED_BYTE;
294       glFormat      = GL_LUMINANCE;
295       break;
296     }
297
298     case Graphics::Format::L8A8:
299     {
300       pixelDataType = GL_UNSIGNED_BYTE;
301       glFormat      = GL_LUMINANCE_ALPHA;
302       break;
303     }
304
305     case Graphics::Format::R5G6B5_UNORM_PACK16:
306     {
307       pixelDataType = GL_UNSIGNED_SHORT_5_6_5;
308       glFormat      = GL_RGB;
309       break;
310     }
311
312     case Graphics::Format::B5G6R5_UNORM_PACK16:
313     {
314       pixelDataType = GL_UNSIGNED_SHORT_5_6_5;
315 #ifdef _ARCH_ARM_
316       glFormat = GL_BGRA_EXT; // alpha is reserved but not used
317 #else
318       glFormat = GL_RGBA; // alpha is reserved but not used
319 #endif
320       break;
321     }
322
323     case Graphics::Format::R4G4B4A4_UNORM_PACK16:
324     {
325       pixelDataType = GL_UNSIGNED_SHORT_4_4_4_4;
326       glFormat      = GL_RGBA;
327       break;
328     }
329
330     case Graphics::Format::B4G4R4A4_UNORM_PACK16:
331     {
332       pixelDataType = GL_UNSIGNED_SHORT_4_4_4_4;
333 #ifdef _ARCH_ARM_
334       glFormat = GL_BGRA_EXT; // alpha is reserved but not used
335 #else
336       glFormat = GL_RGBA; // alpha is reserved but not used
337 #endif
338       break;
339     }
340
341     case Graphics::Format::R5G5B5A1_UNORM_PACK16:
342     {
343       pixelDataType = GL_UNSIGNED_SHORT_5_5_5_1;
344       glFormat      = GL_RGBA;
345       break;
346     }
347
348     case Graphics::Format::B5G5R5A1_UNORM_PACK16:
349     {
350       pixelDataType = GL_UNSIGNED_SHORT_5_5_5_1;
351 #ifdef _ARCH_ARM_
352       glFormat = GL_BGRA_EXT; // alpha is reserved but not used
353 #else
354       glFormat = GL_RGBA; // alpha is reserved but not used
355 #endif
356       break;
357     }
358
359     case Graphics::Format::R8G8B8_UNORM:
360     {
361       pixelDataType = GL_UNSIGNED_BYTE;
362       glFormat      = GL_RGB;
363       break;
364     }
365
366     case Graphics::Format::R8G8B8A8_UNORM:
367     {
368       pixelDataType = GL_UNSIGNED_BYTE;
369       glFormat      = GL_RGBA; // alpha is reserved but not used
370       break;
371     }
372
373     case Graphics::Format::B8G8R8A8_UNORM:
374     {
375       pixelDataType = GL_UNSIGNED_BYTE;
376 #ifdef GL_BGRA_EXT
377       glFormat = GL_BGRA_EXT; // alpha is reserved but not used
378 #else
379       glFormat = GL_RGBA; // alpha is reserved but not used
380 #endif
381       break;
382     }
383
384     case Graphics::Format::ETC2_R8G8B8_UNORM_BLOCK:
385     {
386       glFormat = GL_COMPRESSED_RGB8_ETC2;
387       break;
388     }
389
390     case Graphics::Format::PVRTC1_4BPP_UNORM_BLOCK_IMG:
391     {
392       glFormat = 0x8C00; ///! < Hardcoded so we can test before we move to GLES 3.0 or greater.
393       break;
394     }
395
396     // GLES 3.0 standard compressed formats:
397     case Graphics::Format::EAC_R11_UNORM_BLOCK:
398     {
399       glFormat = GL_COMPRESSED_R11_EAC;
400       break;
401     }
402     case Graphics::Format::EAC_R11_SNORM_BLOCK:
403     {
404       glFormat = GL_COMPRESSED_SIGNED_R11_EAC;
405       break;
406     }
407     case Graphics::Format::EAC_R11G11_UNORM_BLOCK:
408     {
409       glFormat = GL_COMPRESSED_RG11_EAC;
410       break;
411     }
412     case Graphics::Format::EAC_R11G11_SNORM_BLOCK:
413     {
414       glFormat = GL_COMPRESSED_SIGNED_RG11_EAC;
415       break;
416     }
417     case Graphics::Format::ETC2_R8G8B8_SRGB_BLOCK:
418     {
419       glFormat = GL_COMPRESSED_SRGB8_ETC2;
420       break;
421     }
422     case Graphics::Format::ETC2_R8G8B8A1_UNORM_BLOCK:
423     {
424       glFormat = GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
425       break;
426     }
427     case Graphics::Format::ETC2_R8G8B8A1_SRGB_BLOCK:
428     {
429       glFormat = GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
430       break;
431     }
432     case Graphics::Format::ETC2_R8G8B8A8_SRGB_BLOCK:
433     {
434       glFormat = GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
435       break;
436     }
437
438     // GLES 3.1 extension compressed formats:
439     case Graphics::Format::ASTC_4x4_UNORM_BLOCK:
440     {
441       glFormat = GL_COMPRESSED_RGBA_ASTC_4x4_KHR;
442       break;
443     }
444     case Graphics::Format::ASTC_5x4_UNORM_BLOCK:
445     {
446       glFormat = GL_COMPRESSED_RGBA_ASTC_5x4_KHR;
447       break;
448     }
449     case Graphics::Format::ASTC_5x5_UNORM_BLOCK:
450     {
451       glFormat = GL_COMPRESSED_RGBA_ASTC_5x5_KHR;
452       break;
453     }
454     case Graphics::Format::ASTC_6x5_UNORM_BLOCK:
455     {
456       glFormat = GL_COMPRESSED_RGBA_ASTC_6x5_KHR;
457       break;
458     }
459     case Graphics::Format::ASTC_6x6_UNORM_BLOCK:
460     {
461       glFormat = GL_COMPRESSED_RGBA_ASTC_6x6_KHR;
462       break;
463     }
464     case Graphics::Format::ASTC_8x5_UNORM_BLOCK:
465     {
466       glFormat = GL_COMPRESSED_RGBA_ASTC_8x5_KHR;
467       break;
468     }
469     case Graphics::Format::ASTC_8x6_UNORM_BLOCK:
470     {
471       glFormat = GL_COMPRESSED_RGBA_ASTC_8x6_KHR;
472       break;
473     }
474     case Graphics::Format::ASTC_8x8_UNORM_BLOCK:
475     {
476       glFormat = GL_COMPRESSED_RGBA_ASTC_8x8_KHR;
477       break;
478     }
479     case Graphics::Format::ASTC_10x5_UNORM_BLOCK:
480     {
481       glFormat = GL_COMPRESSED_RGBA_ASTC_10x5_KHR;
482       break;
483     }
484     case Graphics::Format::ASTC_10x6_UNORM_BLOCK:
485     {
486       glFormat = GL_COMPRESSED_RGBA_ASTC_10x6_KHR;
487       break;
488     }
489     case Graphics::Format::ASTC_10x8_UNORM_BLOCK:
490     {
491       glFormat = GL_COMPRESSED_RGBA_ASTC_10x8_KHR;
492       break;
493     }
494     case Graphics::Format::ASTC_10x10_UNORM_BLOCK:
495     {
496       glFormat = GL_COMPRESSED_RGBA_ASTC_10x10_KHR;
497       break;
498     }
499     case Graphics::Format::ASTC_12x10_UNORM_BLOCK:
500     {
501       glFormat = GL_COMPRESSED_RGBA_ASTC_12x10_KHR;
502       break;
503     }
504     case Graphics::Format::ASTC_12x12_UNORM_BLOCK:
505     {
506       glFormat = GL_COMPRESSED_RGBA_ASTC_12x12_KHR;
507       break;
508     }
509
510     case Graphics::Format::ASTC_4x4_SRGB_BLOCK:
511     {
512       glFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR;
513       break;
514     }
515     case Graphics::Format::ASTC_5x4_SRGB_BLOCK:
516     {
517       glFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR;
518       break;
519     }
520     case Graphics::Format::ASTC_5x5_SRGB_BLOCK:
521     {
522       glFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR;
523       break;
524     }
525     case Graphics::Format::ASTC_6x5_SRGB_BLOCK:
526     {
527       glFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR;
528       break;
529     }
530     case Graphics::Format::ASTC_6x6_SRGB_BLOCK:
531     {
532       glFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR;
533       break;
534     }
535     case Graphics::Format::ASTC_8x5_SRGB_BLOCK:
536     {
537       glFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR;
538       break;
539     }
540     case Graphics::Format::ASTC_8x6_SRGB_BLOCK:
541     {
542       glFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR;
543       break;
544     }
545     case Graphics::Format::ASTC_8x8_SRGB_BLOCK:
546     {
547       glFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR;
548       break;
549     }
550     case Graphics::Format::ASTC_10x5_SRGB_BLOCK:
551     {
552       glFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR;
553       break;
554     }
555     case Graphics::Format::ASTC_10x6_SRGB_BLOCK:
556     {
557       glFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR;
558       break;
559     }
560     case Graphics::Format::ASTC_10x8_SRGB_BLOCK:
561     {
562       glFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR;
563       break;
564     }
565     case Graphics::Format::ASTC_10x10_SRGB_BLOCK:
566     {
567       glFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR;
568       break;
569     }
570     case Graphics::Format::ASTC_12x10_SRGB_BLOCK:
571     {
572       glFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR;
573       break;
574     }
575     case Graphics::Format::ASTC_12x12_SRGB_BLOCK:
576     {
577       glFormat = GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR;
578       break;
579     }
580
581     // GLES 3.0 floating point formats.
582     case Graphics::Format::R16G16B16_SFLOAT:
583     {
584       glFormat      = GL_RGB;
585       pixelDataType = GL_HALF_FLOAT;
586       break;
587     }
588     case Graphics::Format::R32G32B32_SFLOAT:
589     {
590       glFormat      = GL_RGB;
591       pixelDataType = GL_FLOAT;
592       break;
593     }
594
595     // GLES 3.0 depth and stencil formats
596     case Graphics::Format::D16_UNORM:
597     {
598       glFormat      = GL_DEPTH_COMPONENT;
599       pixelDataType = GL_UNSIGNED_INT;
600       break;
601     }
602
603     case Graphics::Format::D32_SFLOAT:
604     {
605       glFormat      = GL_DEPTH_COMPONENT;
606       pixelDataType = GL_FLOAT;
607       break;
608     }
609
610     case Graphics::Format::D24_UNORM_S8_UINT:
611     {
612       glFormat      = GL_DEPTH_STENCIL;
613       pixelDataType = GL_UNSIGNED_INT_24_8;
614       break;
615     }
616
617     case Graphics::Format::UNDEFINED:
618     {
619       //DALI_LOG_ERROR( "Invalid pixel format for bitmap\n" );
620       glFormat = 0;
621       break;
622     }
623     case Graphics::Format::R11G11B10_UFLOAT_PACK32:
624     {
625       glFormat      = GL_RGB;
626       pixelDataType = GL_FLOAT;
627       break;
628     }
629
630     case Graphics::Format::R4G4_UNORM_PACK8:
631     case Graphics::Format::A1R5G5B5_UNORM_PACK16:
632     case Graphics::Format::R8_SNORM:
633     case Graphics::Format::R8_USCALED:
634     case Graphics::Format::R8_SSCALED:
635     case Graphics::Format::R8_UINT:
636     case Graphics::Format::R8_SINT:
637     case Graphics::Format::R8_SRGB:
638     case Graphics::Format::R8G8_UNORM:
639     case Graphics::Format::R8G8_SNORM:
640     case Graphics::Format::R8G8_USCALED:
641     case Graphics::Format::R8G8_SSCALED:
642     case Graphics::Format::R8G8_UINT:
643     case Graphics::Format::R8G8_SINT:
644     case Graphics::Format::R8G8_SRGB:
645     case Graphics::Format::R8G8B8_SNORM:
646     case Graphics::Format::R8G8B8_USCALED:
647     case Graphics::Format::R8G8B8_SSCALED:
648     case Graphics::Format::R8G8B8_UINT:
649     case Graphics::Format::R8G8B8_SINT:
650     case Graphics::Format::R8G8B8_SRGB:
651     case Graphics::Format::B8G8R8_UNORM:
652     case Graphics::Format::B8G8R8_SNORM:
653     case Graphics::Format::B8G8R8_USCALED:
654     case Graphics::Format::B8G8R8_SSCALED:
655     case Graphics::Format::B8G8R8_UINT:
656     case Graphics::Format::B8G8R8_SINT:
657     case Graphics::Format::B8G8R8_SRGB:
658     case Graphics::Format::R8G8B8A8_SNORM:
659     case Graphics::Format::R8G8B8A8_USCALED:
660     case Graphics::Format::R8G8B8A8_SSCALED:
661     case Graphics::Format::R8G8B8A8_UINT:
662     case Graphics::Format::R8G8B8A8_SINT:
663     case Graphics::Format::R8G8B8A8_SRGB:
664     case Graphics::Format::B8G8R8A8_SNORM:
665     case Graphics::Format::B8G8R8A8_USCALED:
666     case Graphics::Format::B8G8R8A8_SSCALED:
667     case Graphics::Format::B8G8R8A8_UINT:
668     case Graphics::Format::B8G8R8A8_SINT:
669     case Graphics::Format::B8G8R8A8_SRGB:
670     case Graphics::Format::A8B8G8R8_UNORM_PACK32:
671     case Graphics::Format::A8B8G8R8_SNORM_PACK32:
672     case Graphics::Format::A8B8G8R8_USCALED_PACK32:
673     case Graphics::Format::A8B8G8R8_SSCALED_PACK32:
674     case Graphics::Format::A8B8G8R8_UINT_PACK32:
675     case Graphics::Format::A8B8G8R8_SINT_PACK32:
676     case Graphics::Format::A8B8G8R8_SRGB_PACK32:
677     case Graphics::Format::A2R10G10B10_UNORM_PACK32:
678     case Graphics::Format::A2R10G10B10_SNORM_PACK32:
679     case Graphics::Format::A2R10G10B10_USCALED_PACK32:
680     case Graphics::Format::A2R10G10B10_SSCALED_PACK32:
681     case Graphics::Format::A2R10G10B10_UINT_PACK32:
682     case Graphics::Format::A2R10G10B10_SINT_PACK32:
683     case Graphics::Format::A2B10G10R10_UNORM_PACK32:
684     case Graphics::Format::A2B10G10R10_SNORM_PACK32:
685     case Graphics::Format::A2B10G10R10_USCALED_PACK32:
686     case Graphics::Format::A2B10G10R10_SSCALED_PACK32:
687     case Graphics::Format::A2B10G10R10_UINT_PACK32:
688     case Graphics::Format::A2B10G10R10_SINT_PACK32:
689     case Graphics::Format::R16_UNORM:
690     case Graphics::Format::R16_SNORM:
691     case Graphics::Format::R16_USCALED:
692     case Graphics::Format::R16_SSCALED:
693     case Graphics::Format::R16_UINT:
694     case Graphics::Format::R16_SINT:
695     case Graphics::Format::R16_SFLOAT:
696     case Graphics::Format::R16G16_UNORM:
697     case Graphics::Format::R16G16_SNORM:
698     case Graphics::Format::R16G16_USCALED:
699     case Graphics::Format::R16G16_SSCALED:
700     case Graphics::Format::R16G16_UINT:
701     case Graphics::Format::R16G16_SINT:
702     case Graphics::Format::R16G16_SFLOAT:
703     case Graphics::Format::R16G16B16_UNORM:
704     case Graphics::Format::R16G16B16_SNORM:
705     case Graphics::Format::R16G16B16_USCALED:
706     case Graphics::Format::R16G16B16_SSCALED:
707     case Graphics::Format::R16G16B16_UINT:
708     case Graphics::Format::R16G16B16_SINT:
709     case Graphics::Format::R16G16B16A16_UNORM:
710     case Graphics::Format::R16G16B16A16_SNORM:
711     case Graphics::Format::R16G16B16A16_USCALED:
712     case Graphics::Format::R16G16B16A16_SSCALED:
713     case Graphics::Format::R16G16B16A16_UINT:
714     case Graphics::Format::R16G16B16A16_SINT:
715     case Graphics::Format::R16G16B16A16_SFLOAT:
716     case Graphics::Format::R32_UINT:
717     case Graphics::Format::R32_SINT:
718     case Graphics::Format::R32_SFLOAT:
719     case Graphics::Format::R32G32_UINT:
720     case Graphics::Format::R32G32_SINT:
721     case Graphics::Format::R32G32_SFLOAT:
722     case Graphics::Format::R32G32B32_UINT:
723     case Graphics::Format::R32G32B32_SINT:
724     case Graphics::Format::R32G32B32A32_UINT:
725     case Graphics::Format::R32G32B32A32_SINT:
726     case Graphics::Format::R32G32B32A32_SFLOAT:
727     case Graphics::Format::R64_UINT:
728     case Graphics::Format::R64_SINT:
729     case Graphics::Format::R64_SFLOAT:
730     case Graphics::Format::R64G64_UINT:
731     case Graphics::Format::R64G64_SINT:
732     case Graphics::Format::R64G64_SFLOAT:
733     case Graphics::Format::R64G64B64_UINT:
734     case Graphics::Format::R64G64B64_SINT:
735     case Graphics::Format::R64G64B64_SFLOAT:
736     case Graphics::Format::R64G64B64A64_UINT:
737     case Graphics::Format::R64G64B64A64_SINT:
738     case Graphics::Format::R64G64B64A64_SFLOAT:
739     case Graphics::Format::B10G11R11_UFLOAT_PACK32:
740     case Graphics::Format::E5B9G9R9_UFLOAT_PACK32:
741     case Graphics::Format::X8_D24_UNORM_PACK32:
742     case Graphics::Format::S8_UINT:
743     case Graphics::Format::D16_UNORM_S8_UINT:
744     case Graphics::Format::D32_SFLOAT_S8_UINT:
745     case Graphics::Format::BC1_RGB_UNORM_BLOCK:
746     case Graphics::Format::BC1_RGB_SRGB_BLOCK:
747     case Graphics::Format::BC1_RGBA_UNORM_BLOCK:
748     case Graphics::Format::BC1_RGBA_SRGB_BLOCK:
749     case Graphics::Format::BC2_UNORM_BLOCK:
750     case Graphics::Format::BC2_SRGB_BLOCK:
751     case Graphics::Format::BC3_UNORM_BLOCK:
752     case Graphics::Format::BC3_SRGB_BLOCK:
753     case Graphics::Format::BC4_UNORM_BLOCK:
754     case Graphics::Format::BC4_SNORM_BLOCK:
755     case Graphics::Format::BC5_UNORM_BLOCK:
756     case Graphics::Format::BC5_SNORM_BLOCK:
757     case Graphics::Format::BC6H_UFLOAT_BLOCK:
758     case Graphics::Format::BC6H_SFLOAT_BLOCK:
759     case Graphics::Format::BC7_UNORM_BLOCK:
760     case Graphics::Format::BC7_SRGB_BLOCK:
761     case Graphics::Format::ETC2_R8G8B8A8_UNORM_BLOCK:
762     case Graphics::Format::PVRTC1_2BPP_UNORM_BLOCK_IMG:
763     case Graphics::Format::PVRTC2_2BPP_UNORM_BLOCK_IMG:
764     case Graphics::Format::PVRTC2_4BPP_UNORM_BLOCK_IMG:
765     case Graphics::Format::PVRTC1_2BPP_SRGB_BLOCK_IMG:
766     case Graphics::Format::PVRTC1_4BPP_SRGB_BLOCK_IMG:
767     case Graphics::Format::PVRTC2_2BPP_SRGB_BLOCK_IMG:
768     case Graphics::Format::PVRTC2_4BPP_SRGB_BLOCK_IMG:
769     {
770       glFormat = 0;
771       break;
772     }
773   }
774
775   switch(pixelFormat)
776   {
777     case Graphics::Format::R16G16B16A16_SFLOAT:
778     case Graphics::Format::R32G32B32A32_SFLOAT:
779     case Graphics::Format::R11G11B10_UFLOAT_PACK32:
780     {
781       glInternalFormat = GL_R11F_G11F_B10F;
782       break;
783     }
784     case Graphics::Format::D32_SFLOAT:
785     {
786       glInternalFormat = GL_DEPTH_COMPONENT32F;
787       break;
788     }
789     case Graphics::Format::D24_UNORM_S8_UINT:
790     {
791       glInternalFormat = GL_DEPTH24_STENCIL8;
792       break;
793     }
794     default:
795     {
796       glInternalFormat = glFormat;
797     }
798   }
799 }
800
801 } // namespace
802
803 TestGraphicsTexture::TestGraphicsTexture(TestGlAbstraction& glAbstraction, const Graphics::TextureCreateInfo& createInfo)
804 : mGlAbstraction(glAbstraction),
805   mCreateInfo(createInfo),
806   mIsCompressed(IsCompressedFormat(createInfo.format))
807 {
808   GLuint target = GetTarget();
809   if(mCreateInfo.nativeImagePtr)
810   {
811     InitializeNativeImage(target);
812   }
813   else
814   {
815     Initialize(target);
816
817     if(mCreateInfo.textureType == Graphics::TextureType::TEXTURE_2D)
818     {
819       if(!mIsCompressed)
820       {
821         glAbstraction.TexImage2D(target, 0, mGlInternalFormat, createInfo.size.width, createInfo.size.height, 0, mGlFormat, mPixelDataType, nullptr);
822       }
823       else
824       {
825         glAbstraction.CompressedTexImage2D(target, 0, mGlInternalFormat, createInfo.size.width, createInfo.size.height, 0, 0, nullptr);
826       }
827     }
828     else if(mCreateInfo.textureType == Graphics::TextureType::TEXTURE_CUBEMAP)
829     {
830       if(!mIsCompressed)
831       {
832         for(uint32_t i(0); i < 6; ++i)
833         {
834           glAbstraction.TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, mGlInternalFormat, createInfo.size.width, createInfo.size.height, 0, mGlFormat, mPixelDataType, nullptr);
835         }
836       }
837       else
838       {
839         for(uint32_t i(0); i < 6; ++i)
840         {
841           glAbstraction.CompressedTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, mGlInternalFormat, createInfo.size.width, createInfo.size.height, 0, 0, nullptr);
842         }
843       }
844       TestGraphicsSampler::SetTexParameter(glAbstraction, target, GL_TEXTURE_WRAP_R, GL_WRAP_DEFAULT);
845     }
846   }
847 }
848
849 TestGraphicsTexture::~TestGraphicsTexture()
850 {
851   mGlAbstraction.DeleteTextures(1, &mId);
852   if(mCreateInfo.nativeImagePtr)
853   {
854     mCreateInfo.nativeImagePtr->DestroyResource();
855   }
856 }
857
858 void TestGraphicsTexture::Initialize(GLuint target)
859 {
860   PixelFormatToGl(mCreateInfo.format,
861                   mGlFormat,
862                   mGlInternalFormat,
863                   mPixelDataType);
864
865   mGlAbstraction.GenTextures(1, &mId);
866   mGlAbstraction.BindTexture(target, mId);
867   mGlAbstraction.PixelStorei(GL_UNPACK_ALIGNMENT, 1); // We always use tightly packed data
868
869   //Apply default sampling parameters
870   TestGraphicsSampler::SetTexParameter(mGlAbstraction, target, GL_TEXTURE_MIN_FILTER, DALI_MINIFY_DEFAULT);
871   TestGraphicsSampler::SetTexParameter(mGlAbstraction, target, GL_TEXTURE_MAG_FILTER, DALI_MAGNIFY_DEFAULT);
872   TestGraphicsSampler::SetTexParameter(mGlAbstraction, target, GL_TEXTURE_WRAP_S, GL_WRAP_DEFAULT);
873   TestGraphicsSampler::SetTexParameter(mGlAbstraction, target, GL_TEXTURE_WRAP_T, GL_WRAP_DEFAULT);
874 }
875
876 void TestGraphicsTexture::InitializeNativeImage(GLuint target)
877 {
878   mCreateInfo.nativeImagePtr->CreateResource();
879   Initialize(target);
880
881   if(mCreateInfo.nativeImagePtr->TargetTexture() != 0u) // This can definitely fail
882   {
883     mGlAbstraction.DeleteTextures(1, &mId);
884     mCreateInfo.nativeImagePtr->DestroyResource();
885     mId = 0u;
886   }
887 }
888
889 GLuint TestGraphicsTexture::GetTarget()
890 {
891   GLuint target;
892   if(mCreateInfo.nativeImagePtr)
893   {
894     target = mCreateInfo.nativeImagePtr->GetTextureTarget(); // Could be GL_TEXTURE_2D or GL_TEXTURE_EXTERNAL_OES
895   }
896   else
897   {
898     target = GetTextureTarget(mCreateInfo.textureType);
899   }
900   return target;
901 }
902
903 void TestGraphicsTexture::Bind(uint32_t textureUnit)
904 {
905   if(mCreateInfo.nativeImagePtr)
906   {
907     if(mId == 0)
908     {
909       InitializeNativeImage(GetTarget());
910     }
911   }
912   mGlAbstraction.ActiveTexture(textureUnit + GL_TEXTURE0);
913   mGlAbstraction.BindTexture(GetTarget(), mId);
914 }
915
916 void TestGraphicsTexture::Prepare()
917 {
918   if(mCreateInfo.nativeImagePtr)
919   {
920     // Ensure the native image is up-to-date
921     mCreateInfo.nativeImagePtr->PrepareTexture();
922   }
923 }
924
925 void TestGraphicsTexture::Update(Graphics::TextureUpdateInfo updateInfo, Graphics::TextureUpdateSourceInfo source)
926 {
927   GLenum target{GetTarget()};
928   if(mCreateInfo.textureType == Graphics::TextureType::TEXTURE_CUBEMAP)
929   {
930     target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + updateInfo.layer;
931   }
932
933   mGlAbstraction.PixelStorei(GL_UNPACK_ALIGNMENT, 1); // We always use tightly packed data
934
935   const bool isSubImage(updateInfo.dstOffset2D.x != 0 || updateInfo.dstOffset2D.y != 0 ||
936                         updateInfo.srcExtent2D.width != (mCreateInfo.size.width / (1 << updateInfo.level)) ||
937                         updateInfo.srcExtent2D.height != (mCreateInfo.size.height / (1 << updateInfo.level)));
938
939   uint8_t* pixels        = nullptr;
940   bool     releasePixels = false;
941
942   switch(source.sourceType)
943   {
944     case Graphics::TextureUpdateSourceInfo::Type::PIXEL_DATA:
945     {
946       auto pixelDataBuffer = Dali::Integration::GetPixelDataBuffer(source.pixelDataSource.pixelData);
947
948       pixels        = pixelDataBuffer.buffer;
949       releasePixels = Dali::Integration::IsPixelDataReleaseAfterUpload(source.pixelDataSource.pixelData) && updateInfo.srcOffset == 0u;
950       break;
951     }
952     case Graphics::TextureUpdateSourceInfo::Type::MEMORY:
953     {
954       pixels        = reinterpret_cast<uint8_t*>(source.memorySource.memory);
955       releasePixels = true;
956       break;
957     }
958     default:
959     {
960       // TODO : Implement here
961       break;
962     }
963   }
964
965   if(!isSubImage)
966   {
967     if(!mIsCompressed)
968     {
969       mGlAbstraction.TexImage2D(target, updateInfo.level, mGlInternalFormat, updateInfo.srcExtent2D.width, updateInfo.srcExtent2D.height, 0, mGlFormat, mPixelDataType, pixels);
970     }
971     else
972     {
973       mGlAbstraction.CompressedTexImage2D(target, updateInfo.level, mGlInternalFormat, updateInfo.srcExtent2D.width, updateInfo.srcExtent2D.height, 0, updateInfo.srcSize, pixels);
974     }
975   }
976   else
977   {
978     if(!mIsCompressed)
979     {
980       mGlAbstraction.TexSubImage2D(target, updateInfo.level, updateInfo.dstOffset2D.x, updateInfo.dstOffset2D.y, updateInfo.srcExtent2D.width, updateInfo.srcExtent2D.height, mGlFormat, mPixelDataType, pixels);
981     }
982     else
983     {
984       mGlAbstraction.CompressedTexSubImage2D(target, updateInfo.level, updateInfo.dstOffset2D.x, updateInfo.dstOffset2D.y, updateInfo.srcExtent2D.width, updateInfo.srcExtent2D.height, mGlFormat, updateInfo.srcSize, pixels);
985     }
986   }
987
988   if(releasePixels && pixels != nullptr)
989   {
990     switch(source.sourceType)
991     {
992       case Graphics::TextureUpdateSourceInfo::Type::PIXEL_DATA:
993       {
994         Dali::Integration::ReleasePixelDataBuffer(source.pixelDataSource.pixelData);
995         break;
996       }
997       case Graphics::TextureUpdateSourceInfo::Type::MEMORY:
998       {
999         free(reinterpret_cast<void*>(pixels));
1000         break;
1001       }
1002       default:
1003       {
1004         // TODO : Implement here
1005         break;
1006       }
1007     }
1008   }
1009 }