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