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