1 /*
2 * Copyright (C) 2016 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 #include "GLESVersionDetector.h"
18
19 #include "OpenGLESDispatch/EGLDispatch.h"
20
21 #include "aemu/base/system/System.h"
22 #include "aemu/base/misc/StringUtils.h"
23 #include "host-common/feature_control.h"
24 #include "host-common/opengl/misc.h"
25
26 #include <algorithm>
27
28 namespace gfxstream {
29 namespace gl {
30
31 // Config + context attributes to query the underlying OpenGL if it is
32 // a OpenGL ES backend. Only try for OpenGL ES 3, and assume OpenGL ES 2
33 // exists (if it doesn't, this is the least of our problems).
34 static const EGLint gles3ConfigAttribs[] =
35 { EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
36 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT | EGL_OPENGL_ES3_BIT, EGL_NONE };
37
38 static const EGLint pbufAttribs[] =
39 { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
40
41 static const EGLint gles31Attribs[] =
42 { EGL_CONTEXT_MAJOR_VERSION_KHR, 3,
43 EGL_CONTEXT_MINOR_VERSION_KHR, 1, EGL_NONE };
44
45 static const EGLint gles30Attribs[] =
46 { EGL_CONTEXT_MAJOR_VERSION_KHR, 3,
47 EGL_CONTEXT_MINOR_VERSION_KHR, 0, EGL_NONE };
48
sTryContextCreation(EGLDisplay dpy,GLESDispatchMaxVersion ver)49 static bool sTryContextCreation(EGLDisplay dpy, GLESDispatchMaxVersion ver) {
50 EGLConfig config;
51 EGLSurface surface;
52
53 const EGLint* contextAttribs = nullptr;
54
55 // Assume ES2 capable.
56 if (ver == GLES_DISPATCH_MAX_VERSION_2) return true;
57
58 switch (ver) {
59 case GLES_DISPATCH_MAX_VERSION_3_0:
60 contextAttribs = gles30Attribs;
61 break;
62 case GLES_DISPATCH_MAX_VERSION_3_1:
63 contextAttribs = gles31Attribs;
64 break;
65 default:
66 break;
67 }
68
69 if (!contextAttribs) return false;
70
71 int numConfigs;
72 if (!s_egl.eglChooseConfig(
73 dpy, gles3ConfigAttribs, &config, 1, &numConfigs) ||
74 numConfigs == 0) {
75 return false;
76 }
77
78 surface = s_egl.eglCreatePbufferSurface(dpy, config, pbufAttribs);
79 if (surface == EGL_NO_SURFACE) {
80 return false;
81 }
82
83 EGLContext ctx = s_egl.eglCreateContext(dpy, config, EGL_NO_CONTEXT,
84 contextAttribs);
85
86 if (ctx == EGL_NO_CONTEXT) {
87 s_egl.eglDestroySurface(dpy, surface);
88 return false;
89 } else {
90 s_egl.eglDestroyContext(dpy, ctx);
91 s_egl.eglDestroySurface(dpy, surface);
92 return true;
93 }
94 }
95
calcMaxVersionFromDispatch(const gfxstream::host::FeatureSet & features,EGLDisplay dpy)96 GLESDispatchMaxVersion calcMaxVersionFromDispatch(const gfxstream::host::FeatureSet& features,
97 EGLDisplay dpy) {
98 // TODO: 3.1 is the highest
99 // b/360208429: CTS conformance for OpenGL ES 3.1
100 GLESDispatchMaxVersion maxVersion =
101 GLES_DISPATCH_MAX_VERSION_3_1;
102
103 if (emugl::getRenderer() == SELECTED_RENDERER_HOST
104 || emugl::getRenderer() == SELECTED_RENDERER_SWIFTSHADER_INDIRECT
105 || emugl::getRenderer() == SELECTED_RENDERER_ANGLE_INDIRECT
106 || emugl::getRenderer() == SELECTED_RENDERER_ANGLE9_INDIRECT) {
107 if (s_egl.eglGetMaxGLESVersion) {
108 maxVersion =
109 (GLESDispatchMaxVersion)s_egl.eglGetMaxGLESVersion(dpy);
110 }
111 } else {
112 if (!sTryContextCreation(dpy, GLES_DISPATCH_MAX_VERSION_3_1)) {
113 maxVersion = GLES_DISPATCH_MAX_VERSION_3_0;
114 if (!sTryContextCreation(dpy, GLES_DISPATCH_MAX_VERSION_3_0)) {
115 maxVersion = GLES_DISPATCH_MAX_VERSION_2;
116 }
117 }
118 }
119
120 int maj = 2; int min = 0;
121 switch (maxVersion) {
122 case GLES_DISPATCH_MAX_VERSION_2:
123 maj = 2; min = 0; break;
124 case GLES_DISPATCH_MAX_VERSION_3_0:
125 maj = 3; min = 0; break;
126 case GLES_DISPATCH_MAX_VERSION_3_1:
127 maj = 3; min = 1; break;
128 case GLES_DISPATCH_MAX_VERSION_3_2:
129 maj = 3; min = 2; break;
130 default:
131 break;
132 }
133
134 emugl::setGlesVersion(maj, min);
135 return maxVersion;
136 }
137
138 // For determining whether or not to use core profile OpenGL.
139 // (Note: This does not affect the detection of possible core profile configs,
140 // just whether to use them)
shouldEnableCoreProfile()141 bool shouldEnableCoreProfile() {
142 int dispatchMaj, dispatchMin;
143
144 emugl::getGlesVersion(&dispatchMaj, &dispatchMin);
145 return emugl::getRenderer() == SELECTED_RENDERER_HOST &&
146 dispatchMaj > 2;
147 }
148
sAddExtensionIfSupported(GLESDispatchMaxVersion currVersion,const std::string & from,GLESDispatchMaxVersion extVersion,const std::string & ext,std::string & to)149 void sAddExtensionIfSupported(GLESDispatchMaxVersion currVersion,
150 const std::string& from,
151 GLESDispatchMaxVersion extVersion,
152 const std::string& ext,
153 std::string& to) {
154 // If we chose a GLES version less than or equal to
155 // the |extVersion| the extension |ext| is tagged with,
156 // filter it according to the whitelist.
157 if (emugl::hasExtension(from.c_str(), ext.c_str()) &&
158 currVersion > extVersion) {
159 to += ext;
160 to += " ";
161 }
162 }
163
sWhitelistedExtensionsGLES2(const std::string & hostExt)164 static bool sWhitelistedExtensionsGLES2(const std::string& hostExt) {
165
166 #define WHITELIST(ext) \
167 if (hostExt == #ext) return true; \
168
169 WHITELIST(GL_OES_compressed_ETC1_RGB8_texture)
170 WHITELIST(GL_OES_depth24)
171 WHITELIST(GL_OES_depth32)
172 WHITELIST(GL_OES_depth_texture)
173 WHITELIST(GL_OES_depth_texture_cube_map)
174 WHITELIST(GL_OES_EGL_image)
175 WHITELIST(GL_OES_EGL_image_external)
176 WHITELIST(GL_OES_EGL_sync)
177 WHITELIST(GL_OES_element_index_uint)
178 WHITELIST(GL_OES_framebuffer_object)
179 WHITELIST(GL_OES_packed_depth_stencil)
180 WHITELIST(GL_OES_rgb8_rgba8)
181 WHITELIST(GL_OES_standard_derivatives)
182 WHITELIST(GL_OES_texture_float)
183 WHITELIST(GL_OES_texture_float_linear)
184 WHITELIST(GL_OES_texture_half_float)
185 WHITELIST(GL_OES_texture_half_float_linear)
186 WHITELIST(GL_OES_texture_npot)
187 WHITELIST(GL_OES_texture_3D)
188 WHITELIST(GL_OVR_multiview2)
189 WHITELIST(GL_EXT_multiview_texture_multisample)
190 WHITELIST(GL_EXT_blend_minmax)
191 WHITELIST(GL_EXT_color_buffer_half_float)
192 WHITELIST(GL_EXT_draw_buffers)
193 WHITELIST(GL_EXT_instanced_arrays)
194 WHITELIST(GL_EXT_occlusion_query_boolean)
195 WHITELIST(GL_EXT_read_format_bgra)
196 WHITELIST(GL_EXT_texture_compression_rgtc)
197 WHITELIST(GL_EXT_texture_filter_anisotropic)
198 WHITELIST(GL_EXT_texture_format_BGRA8888)
199 WHITELIST(GL_EXT_texture_rg)
200 WHITELIST(GL_ANGLE_framebuffer_blit)
201 WHITELIST(GL_ANGLE_framebuffer_multisample)
202 WHITELIST(GL_ANGLE_instanced_arrays)
203 WHITELIST(GL_CHROMIUM_texture_filtering_hint)
204 WHITELIST(GL_NV_fence)
205 WHITELIST(GL_NV_framebuffer_blit)
206 WHITELIST(GL_NV_read_depth)
207
208 #if defined(__linux__)
209 WHITELIST(GL_EXT_texture_compression_bptc)
210 WHITELIST(GL_EXT_texture_compression_s3tc)
211 #endif
212
213 #undef WHITELIST
214
215 return false;
216 }
217
filterExtensionsBasedOnMaxVersion(const gfxstream::host::FeatureSet & features,GLESDispatchMaxVersion ver,const std::string & exts)218 std::string filterExtensionsBasedOnMaxVersion(const gfxstream::host::FeatureSet& features,
219 GLESDispatchMaxVersion ver,
220 const std::string& exts) {
221 // We need to advertise ES 2 extensions if:
222 // a. the dispatch version on the host is ES 2
223 // b. the guest image is not updated for ES 3+
224 // (GLESDynamicVersion is disabled)
225 if (ver > GLES_DISPATCH_MAX_VERSION_2 && features.GlesDynamicVersion.enabled) {
226 return exts;
227 }
228
229 std::string filteredExtensions;
230 filteredExtensions.reserve(4096);
231 auto add = [&filteredExtensions](const std::string& hostExt) {
232 if (!hostExt.empty() &&
233 sWhitelistedExtensionsGLES2(hostExt)) {
234 filteredExtensions += hostExt;
235 filteredExtensions += " ";
236 }
237 };
238
239 android::base::split<std::string>(exts, " ", add);
240
241 return filteredExtensions;
242 }
243
244 } // namespace gl
245 } // namespace gfxstream
246