• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(EGLDisplay dpy)96 GLESDispatchMaxVersion calcMaxVersionFromDispatch(EGLDisplay dpy) {
97 
98     // TODO: 3.1 is the highest
99     GLESDispatchMaxVersion maxVersion =
100        GLES_DISPATCH_MAX_VERSION_3_1;
101 
102     // TODO: CTS conformance for OpenGL ES 3.1
103     bool playStoreImage = feature_is_enabled(
104             kFeature_PlayStoreImage);
105 
106     if (emugl::getRenderer() == SELECTED_RENDERER_HOST
107         || emugl::getRenderer() == SELECTED_RENDERER_SWIFTSHADER_INDIRECT
108         || emugl::getRenderer() == SELECTED_RENDERER_ANGLE_INDIRECT
109         || emugl::getRenderer() == SELECTED_RENDERER_ANGLE9_INDIRECT) {
110         if (s_egl.eglGetMaxGLESVersion) {
111             maxVersion =
112                 (GLESDispatchMaxVersion)s_egl.eglGetMaxGLESVersion(dpy);
113         }
114     } else {
115         if (playStoreImage ||
116             !sTryContextCreation(dpy, GLES_DISPATCH_MAX_VERSION_3_1)) {
117             maxVersion = GLES_DISPATCH_MAX_VERSION_3_0;
118             if (!sTryContextCreation(dpy, GLES_DISPATCH_MAX_VERSION_3_0)) {
119                 maxVersion = GLES_DISPATCH_MAX_VERSION_2;
120             }
121         }
122     }
123 
124     if (playStoreImage) {
125         maxVersion =
126             std::min(maxVersion,
127                      GLES_DISPATCH_MAX_VERSION_3_0);
128     }
129 
130     int maj = 2; int min = 0;
131     switch (maxVersion) {
132         case GLES_DISPATCH_MAX_VERSION_2:
133             maj = 2; min = 0; break;
134         case GLES_DISPATCH_MAX_VERSION_3_0:
135             maj = 3; min = 0; break;
136         case GLES_DISPATCH_MAX_VERSION_3_1:
137             maj = 3; min = 1; break;
138         case GLES_DISPATCH_MAX_VERSION_3_2:
139             maj = 3; min = 2; break;
140         default:
141             break;
142     }
143 
144     emugl::setGlesVersion(maj, min);
145 
146     return maxVersion;
147 }
148 
149 // For determining whether or not to use core profile OpenGL.
150 // (Note: This does not affect the detection of possible core profile configs,
151 // just whether to use them)
shouldEnableCoreProfile()152 bool shouldEnableCoreProfile() {
153     int dispatchMaj, dispatchMin;
154 
155     emugl::getGlesVersion(&dispatchMaj, &dispatchMin);
156     return emugl::getRenderer() == SELECTED_RENDERER_HOST &&
157            dispatchMaj > 2;
158 }
159 
sAddExtensionIfSupported(GLESDispatchMaxVersion currVersion,const std::string & from,GLESDispatchMaxVersion extVersion,const std::string & ext,std::string & to)160 void sAddExtensionIfSupported(GLESDispatchMaxVersion currVersion,
161                               const std::string& from,
162                               GLESDispatchMaxVersion extVersion,
163                               const std::string& ext,
164                               std::string& to) {
165     // If we chose a GLES version less than or equal to
166     // the |extVersion| the extension |ext| is tagged with,
167     // filter it according to the whitelist.
168     if (emugl::hasExtension(from.c_str(), ext.c_str()) &&
169         currVersion > extVersion) {
170         to += ext;
171         to += " ";
172     }
173 }
174 
sWhitelistedExtensionsGLES2(const std::string & hostExt)175 static bool sWhitelistedExtensionsGLES2(const std::string& hostExt) {
176 
177 #define WHITELIST(ext) \
178     if (hostExt == #ext) return true; \
179 
180 WHITELIST(GL_OES_compressed_ETC1_RGB8_texture)
181 WHITELIST(GL_OES_depth24)
182 WHITELIST(GL_OES_depth32)
183 WHITELIST(GL_OES_depth_texture)
184 WHITELIST(GL_OES_depth_texture_cube_map)
185 WHITELIST(GL_OES_EGL_image)
186 WHITELIST(GL_OES_EGL_image_external)
187 WHITELIST(GL_OES_EGL_sync)
188 WHITELIST(GL_OES_element_index_uint)
189 WHITELIST(GL_OES_framebuffer_object)
190 WHITELIST(GL_OES_packed_depth_stencil)
191 WHITELIST(GL_OES_rgb8_rgba8)
192 WHITELIST(GL_OES_standard_derivatives)
193 WHITELIST(GL_OES_texture_float)
194 WHITELIST(GL_OES_texture_float_linear)
195 WHITELIST(GL_OES_texture_half_float)
196 WHITELIST(GL_OES_texture_half_float_linear)
197 WHITELIST(GL_OES_texture_npot)
198 WHITELIST(GL_OES_texture_3D)
199 WHITELIST(GL_EXT_blend_minmax)
200 WHITELIST(GL_EXT_color_buffer_half_float)
201 WHITELIST(GL_EXT_draw_buffers)
202 WHITELIST(GL_EXT_instanced_arrays)
203 WHITELIST(GL_EXT_occlusion_query_boolean)
204 WHITELIST(GL_EXT_read_format_bgra)
205 WHITELIST(GL_EXT_texture_compression_rgtc)
206 WHITELIST(GL_EXT_texture_filter_anisotropic)
207 WHITELIST(GL_EXT_texture_format_BGRA8888)
208 WHITELIST(GL_EXT_texture_rg)
209 WHITELIST(GL_ANGLE_framebuffer_blit)
210 WHITELIST(GL_ANGLE_framebuffer_multisample)
211 WHITELIST(GL_ANGLE_instanced_arrays)
212 WHITELIST(GL_CHROMIUM_texture_filtering_hint)
213 WHITELIST(GL_NV_fence)
214 WHITELIST(GL_NV_framebuffer_blit)
215 WHITELIST(GL_NV_read_depth)
216 
217 #if defined(__linux__)
218 WHITELIST(GL_EXT_texture_compression_bptc)
219 WHITELIST(GL_EXT_texture_compression_s3tc)
220 #endif
221 
222 #undef WHITELIST
223 
224     return false;
225 }
226 
filterExtensionsBasedOnMaxVersion(GLESDispatchMaxVersion ver,const std::string & exts)227 std::string filterExtensionsBasedOnMaxVersion(GLESDispatchMaxVersion ver,
228                                               const std::string& exts) {
229     // We need to advertise ES 2 extensions if:
230     // a. the dispatch version on the host is ES 2
231     // b. the guest image is not updated for ES 3+
232     // (GLESDynamicVersion is disabled)
233     if (ver > GLES_DISPATCH_MAX_VERSION_2 &&
234         feature_is_enabled(
235             kFeature_GLESDynamicVersion)) {
236         return exts;
237     }
238 
239     std::string filteredExtensions;
240     filteredExtensions.reserve(4096);
241     auto add = [&filteredExtensions](const std::string& hostExt) {
242         if (!hostExt.empty() &&
243             sWhitelistedExtensionsGLES2(hostExt)) {
244             filteredExtensions += hostExt;
245             filteredExtensions += " ";
246         }
247     };
248 
249     android::base::split<std::string>(exts, " ", add);
250 
251     return filteredExtensions;
252 }
253 
254 }  // namespace gl
255 }  // namespace gfxstream
256