1 /*
2  * Copyright (C) 2018 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 #pragma once
18 
19 #include <cstdint>
20 #include <vector>
21 
22 #include <EGL/egl.h>
23 #include <EGL/eglext.h>
24 
25 typedef EGLBoolean (*PFNEGLGETCONFIGATTRIB)(EGLDisplay, EGLConfig, EGLint, EGLint*);
26 
27 struct EglConfig {
28     static std::vector<EglConfig*> vec;
29 
30     // clang-format off
31     static constexpr EGLint kAttribs[] = {
32         EGL_DEPTH_SIZE,
33         EGL_STENCIL_SIZE,
34         EGL_RENDERABLE_TYPE,
35         EGL_SURFACE_TYPE,
36         EGL_CONFIG_ID,
37         EGL_BUFFER_SIZE,
38         EGL_ALPHA_SIZE,
39         EGL_BLUE_SIZE,
40         EGL_GREEN_SIZE,
41         EGL_RED_SIZE,
42         EGL_CONFIG_CAVEAT,
43         EGL_LEVEL,
44         EGL_MAX_PBUFFER_HEIGHT,
45         EGL_MAX_PBUFFER_PIXELS,
46         EGL_MAX_PBUFFER_WIDTH,
47         EGL_NATIVE_RENDERABLE,
48         EGL_NATIVE_VISUAL_ID,
49         EGL_NATIVE_VISUAL_TYPE,
50         0x3030, // EGL_PRESERVED_RESOURCES
51         EGL_SAMPLES,
52         EGL_SAMPLE_BUFFERS,
53         EGL_TRANSPARENT_TYPE,
54         EGL_TRANSPARENT_BLUE_VALUE,
55         EGL_TRANSPARENT_GREEN_VALUE,
56         EGL_TRANSPARENT_RED_VALUE,
57         EGL_BIND_TO_TEXTURE_RGB,
58         EGL_BIND_TO_TEXTURE_RGBA,
59         EGL_MIN_SWAP_INTERVAL,
60         EGL_MAX_SWAP_INTERVAL,
61         EGL_LUMINANCE_SIZE,
62         EGL_ALPHA_MASK_SIZE,
63         EGL_COLOR_BUFFER_TYPE,
64         //EGL_MATCH_NATIVE_PIXMAP,
65         EGL_RECORDABLE_ANDROID,
66         EGL_CONFORMANT
67     };
68     // clang-format on
69 
70     static constexpr size_t kNumAttribs = sizeof(kAttribs) / sizeof(kAttribs[0]);
71 
EglConfigEglConfig72     EglConfig(EGLDisplay dpy, EGLConfig config_, PFNEGLGETCONFIGATTRIB pfnEglGetConfigAttrib)
73         : config(config_) {
74         for (size_t a = 0; a < kNumAttribs; a++) {
75             if (!pfnEglGetConfigAttrib(dpy, config, kAttribs[a], &attribs[a]))
76                 attribs[a] = 0;
77         }
78         vec.push_back(this);
79     }
80 
~EglConfigEglConfig81     ~EglConfig() {
82         for (size_t i = 0; i < EglConfig::vec.size(); i++) {
83             if (vec[i] == this) {
84                 vec.erase(vec.begin() + i);
85                 break;
86             }
87         }
88     }
89 
90     EGLint attribs[kNumAttribs];
91     EGLConfig config;
92 };
93