1 /*
2  * Copyright 2021 The Android Open Source Project
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 package androidx.graphics.opengl.egl
18 
19 import android.opengl.EGL14
20 import androidx.graphics.opengl.egl.EGLConfigAttributes.Companion.EGL_COLOR_COMPONENT_TYPE_EXT
21 import androidx.graphics.opengl.egl.EGLConfigAttributes.Companion.EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT
22 import androidx.test.ext.junit.runners.AndroidJUnit4
23 import androidx.test.filters.SmallTest
24 import org.junit.Assert.assertEquals
25 import org.junit.Assert.assertTrue
26 import org.junit.Test
27 import org.junit.runner.RunWith
28 
29 @SmallTest
30 @RunWith(AndroidJUnit4::class)
31 class EGLConfigAttributesTest {
32 
33     @Test
testConfig8888null34     fun testConfig8888() {
35         with(EGLConfigAttributes.RGBA_8888.attrs) {
36             assertTrue(find(EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT))
37             assertTrue(find(EGL14.EGL_RED_SIZE, 8))
38             assertTrue(find(EGL14.EGL_GREEN_SIZE, 8))
39             assertTrue(find(EGL14.EGL_BLUE_SIZE, 8))
40             assertTrue(find(EGL14.EGL_ALPHA_SIZE, 8))
41             assertTrue(find(EGL14.EGL_DEPTH_SIZE, 0))
42             assertTrue(find(EGL14.EGL_CONFIG_CAVEAT, EGL14.EGL_NONE))
43             assertTrue(find(EGL14.EGL_STENCIL_SIZE, 0))
44             assertTrue(find(EGL14.EGL_SURFACE_TYPE, EGL14.EGL_WINDOW_BIT))
45             assertEquals(this[size - 1], EGL14.EGL_NONE)
46             assertEquals(19, size)
47         }
48     }
49 
50     @Test
testConfig1010102null51     fun testConfig1010102() {
52         with(EGLConfigAttributes.RGBA_1010102.attrs) {
53             assertTrue(find(EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT))
54             assertTrue(find(EGL14.EGL_RED_SIZE, 10))
55             assertTrue(find(EGL14.EGL_GREEN_SIZE, 10))
56             assertTrue(find(EGL14.EGL_BLUE_SIZE, 10))
57             assertTrue(find(EGL14.EGL_ALPHA_SIZE, 2))
58             assertTrue(find(EGL14.EGL_DEPTH_SIZE, 0))
59             assertTrue(find(EGL14.EGL_STENCIL_SIZE, 0))
60             assertTrue(find(EGL14.EGL_SURFACE_TYPE, EGL14.EGL_WINDOW_BIT))
61             assertEquals(this[size - 1], EGL14.EGL_NONE)
62             assertEquals(17, size)
63         }
64     }
65 
66     @Test
testConfigF16null67     fun testConfigF16() {
68         with(EGLConfigAttributes.RGBA_F16.attrs) {
69             assertTrue(find(EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT))
70             assertTrue(find(EGL_COLOR_COMPONENT_TYPE_EXT, EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT))
71             assertTrue(find(EGL14.EGL_RED_SIZE, 16))
72             assertTrue(find(EGL14.EGL_GREEN_SIZE, 16))
73             assertTrue(find(EGL14.EGL_BLUE_SIZE, 16))
74             assertTrue(find(EGL14.EGL_ALPHA_SIZE, 16))
75             assertTrue(find(EGL14.EGL_DEPTH_SIZE, 0))
76             assertTrue(find(EGL14.EGL_STENCIL_SIZE, 0))
77             assertTrue(find(EGL14.EGL_SURFACE_TYPE, EGL14.EGL_WINDOW_BIT))
78             assertEquals(this[size - 1], EGL14.EGL_NONE)
79             assertEquals(19, size)
80         }
81     }
82 
83     @Test
testIncludenull84     fun testInclude() {
85         // Verify that custom config that uses an include initially and overwrites
86         // individual values is handled appropriately even if the config is technically invalid
87         val customConfig = EGLConfigAttributes {
88             include(EGLConfigAttributes.RGBA_8888)
89             EGL14.EGL_RED_SIZE to 27
90             EGL_COLOR_COMPONENT_TYPE_EXT to EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT
91             EGL14.EGL_STENCIL_SIZE to 32
92         }
93 
94         with(customConfig.attrs) {
95             assertTrue(find(EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT))
96             assertTrue(find(EGL14.EGL_RED_SIZE, 27))
97             assertTrue(find(EGL_COLOR_COMPONENT_TYPE_EXT, EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT))
98             assertTrue(find(EGL14.EGL_STENCIL_SIZE, 32))
99             assertEquals(this[size - 1], EGL14.EGL_NONE)
100             assertEquals(21, size)
101         }
102     }
103 
IntArraynull104     private fun IntArray.find(key: Int, value: Int): Boolean {
105         // size - 1 to skip trailing EGL_NONE
106         for (i in 0 until this.size - 1 step 2) {
107             if (this[i] == key) {
108                 return this[i + 1] == value
109             }
110         }
111         return false
112     }
113 }
114