• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2022 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "EmulationGl.h"
16 
17 #include <algorithm>
18 #include <cstring>
19 #include <optional>
20 #include <vector>
21 
22 #include "DisplaySurfaceGl.h"
23 #include "GLESVersionDetector.h"
24 #include "OpenGLESDispatch/DispatchTables.h"
25 #include "OpenGLESDispatch/EGLDispatch.h"
26 #include "OpenGLESDispatch/GLESv2Dispatch.h"
27 #include "OpenGLESDispatch/OpenGLDispatchLoader.h"
28 #include "RenderThreadInfoGl.h"
29 #include "aemu/base/misc/StringUtils.h"
30 #include "host-common/GfxstreamFatalError.h"
31 #include "host-common/feature_control.h"
32 #include "host-common/logging.h"
33 #include "host-common/opengl/misc.h"
34 
35 namespace gfxstream {
36 namespace gl {
37 namespace {
38 
EglDebugCallback(EGLenum error,const char * command,EGLint messageType,EGLLabelKHR threadLabel,EGLLabelKHR objectLabel,const char * message)39 static void EGLAPIENTRY EglDebugCallback(EGLenum error,
40                                          const char *command,
41                                          EGLint messageType,
42                                          EGLLabelKHR threadLabel,
43                                          EGLLabelKHR objectLabel,
44                                          const char *message) {
45     GL_LOG("command:%s message:%s", command, message);
46 }
47 
GlDebugCallback(GLenum source,GLenum type,GLuint id,GLenum severity,GLsizei length,const GLchar * message,const void * userParam)48 static void GL_APIENTRY GlDebugCallback(GLenum source,
49                                         GLenum type,
50                                         GLuint id,
51                                         GLenum severity,
52                                         GLsizei length,
53                                         const GLchar *message,
54                                         const void *userParam) {
55     GL_LOG("message:%s", message);
56 }
57 
58 static const GLint kGles2ContextAttribsESOrGLCompat[] = {
59     EGL_CONTEXT_CLIENT_VERSION, 2,  //
60     EGL_NONE,                       //
61 };
62 
63 static const GLint kGles2ContextAttribsCoreGL[] = {
64     EGL_CONTEXT_CLIENT_VERSION, 2,                                                 //
65     EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR,  //
66     EGL_NONE,                                                                      //
67 };
68 
69 static const GLint kGles3ContextAttribsESOrGLCompat[] = {
70     EGL_CONTEXT_CLIENT_VERSION, 3,  //
71     EGL_NONE,                       //
72 };
73 
74 static const GLint kGles3ContextAttribsCoreGL[] = {
75     EGL_CONTEXT_CLIENT_VERSION, 3,                                                 //
76     EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR,  //
77     EGL_NONE,                                                                      //
78 };
79 
validateGles2Context(EGLDisplay display)80 static bool validateGles2Context(EGLDisplay display) {
81     const GLint configAttribs[] = {
82         EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
83         EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
84         EGL_NONE,
85     };
86 
87     EGLint numConfigs = 0;
88     EGLConfig config;
89     if (!s_egl.eglChooseConfig(display, configAttribs, &config, 1, &numConfigs)) {
90         ERR("Failed to find GLES 2.x config.");
91         return false;
92     }
93     if (numConfigs != 1) {
94         ERR("Failed to find exactly 1 GLES 2.x config: found %d.", numConfigs);
95         return false;
96     }
97 
98     const EGLint surfaceAttribs[] = {
99         EGL_WIDTH, 1,
100         EGL_HEIGHT, 1,
101         EGL_NONE,
102     };
103 
104     EGLSurface surface = s_egl.eglCreatePbufferSurface(display, config, surfaceAttribs);
105     if (surface == EGL_NO_SURFACE) {
106         ERR("Failed to create GLES 2.x pbuffer surface.");
107         return false;
108     }
109 
110     const GLint* contextAttribs = EmulationGl::getGlesMaxContextAttribs();
111     EGLContext context = s_egl.eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
112     if (context == EGL_NO_CONTEXT) {
113         ERR("Failed to create GLES 2.x context.");
114         s_egl.eglDestroySurface(display, surface);
115         return false;
116     }
117 
118     if (!s_egl.eglMakeCurrent(display, surface, surface, context)) {
119         ERR("Failed to make GLES 2.x context current.");
120         s_egl.eglDestroySurface(display, surface);
121         s_egl.eglDestroyContext(display, context);
122         return false;
123     }
124 
125     const char* extensions = (const char*)s_gles2.glGetString(GL_EXTENSIONS);
126     if (extensions == nullptr) {
127         ERR("Failed to query GLES 2.x context extensions.");
128         s_egl.eglDestroySurface(display, surface);
129         s_egl.eglDestroyContext(display, context);
130         return false;
131     }
132 
133     // It is rare but some drivers actually fail this...
134     if (!s_egl.eglMakeCurrent(display, EGL_NO_CONTEXT, EGL_NO_SURFACE, EGL_NO_SURFACE)) {
135         ERR("Failed to unbind GLES 2.x context.");
136         s_egl.eglDestroySurface(display, surface);
137         s_egl.eglDestroyContext(display, context);
138         return false;
139     }
140 
141     s_egl.eglDestroyContext(display, context);
142     s_egl.eglDestroySurface(display, surface);
143     return true;
144 }
145 
getEmulationEglConfig(EGLDisplay display,bool allowWindowSurface)146 static std::optional<EGLConfig> getEmulationEglConfig(EGLDisplay display, bool allowWindowSurface) {
147     GLint surfaceType = EGL_PBUFFER_BIT;
148 
149     if (allowWindowSurface) {
150         surfaceType |= EGL_WINDOW_BIT;
151     }
152 
153     // On Linux, we need RGB888 exactly, or eglMakeCurrent will fail,
154     // as glXMakeContextCurrent needs to match the format of the
155     // native pixmap.
156     constexpr const EGLint kWantedRedSize = 8;
157     constexpr const EGLint kWantedGreenSize = 8;
158     constexpr const EGLint kWantedBlueSize = 8;
159 
160     const GLint configAttribs[] = {
161         EGL_RED_SIZE, kWantedRedSize,             //
162         EGL_GREEN_SIZE, kWantedGreenSize,         //
163         EGL_BLUE_SIZE, kWantedBlueSize,           //
164         EGL_SURFACE_TYPE, surfaceType,            //
165         EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,  //
166         EGL_NONE,                                 //
167     };
168 
169     EGLint numConfigs = 0;
170     s_egl.eglGetConfigs(display, nullptr, 0, &numConfigs);
171 
172     std::vector<EGLConfig> configs(numConfigs);
173 
174     EGLint numMatchedConfigs = 0;
175     s_egl.eglChooseConfig(display, configAttribs, configs.data(), numConfigs, &numMatchedConfigs);
176 
177     configs.resize(numMatchedConfigs);
178 
179     for (EGLConfig config : configs) {
180         EGLint foundRedSize = 0;
181         s_egl.eglGetConfigAttrib(display, config, EGL_RED_SIZE, &foundRedSize);
182         if (foundRedSize != kWantedRedSize) {
183             continue;
184         }
185 
186         EGLint foundGreenSize = 0;
187         s_egl.eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &foundGreenSize);
188         if (foundGreenSize != kWantedGreenSize) {
189             continue;
190         }
191 
192         EGLint foundBlueSize = 0;
193         s_egl.eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &foundBlueSize);
194         if (foundBlueSize != kWantedBlueSize) {
195             continue;
196         }
197 
198         return config;
199     }
200 
201     return std::nullopt;
202 }
203 
204 }  // namespace
205 
create(uint32_t width,uint32_t height,gfxstream::host::FeatureSet features,bool allowWindowSurface,bool egl2egl)206 std::unique_ptr<EmulationGl> EmulationGl::create(uint32_t width, uint32_t height,
207                                                  gfxstream::host::FeatureSet features,
208                                                  bool allowWindowSurface, bool egl2egl) {
209     // Loads the glestranslator function pointers.
210     if (!LazyLoadedEGLDispatch::get()) {
211         ERR("Failed to load EGL dispatch.");
212         return nullptr;
213     }
214     if (!LazyLoadedGLESv1Dispatch::get()) {
215         ERR("Failed to load GLESv1 dispatch.");
216         return nullptr;
217     }
218     if (!LazyLoadedGLESv2Dispatch::get()) {
219         ERR("Failed to load GLESv2 dispatch.");
220         return nullptr;
221     }
222 
223     if (s_egl.eglUseOsEglApi) {
224         s_egl.eglUseOsEglApi(egl2egl, EGL_FALSE);
225     }
226 
227 
228     std::unique_ptr<EmulationGl> emulationGl(new EmulationGl());
229 
230     emulationGl->mFeatures = features;
231     emulationGl->mWidth = width;
232     emulationGl->mHeight = height;
233 
234     emulationGl->mEglDisplay = s_egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
235     if (emulationGl->mEglDisplay == EGL_NO_DISPLAY) {
236         ERR("Failed to get EGL display.");
237         return nullptr;
238     }
239 
240     GL_LOG("call eglInitialize");
241     if (!s_egl.eglInitialize(emulationGl->mEglDisplay,
242                              &emulationGl->mEglVersionMajor,
243                              &emulationGl->mEglVersionMinor)) {
244         ERR("Failed to eglInitialize.");
245         return nullptr;
246     }
247 
248     if (s_egl.eglSetNativeTextureDecompressionEnabledANDROID) {
249         s_egl.eglSetNativeTextureDecompressionEnabledANDROID(
250             emulationGl->mEglDisplay,
251             emulationGl->mFeatures.NativeTextureDecompression.enabled);
252     }
253 
254     s_egl.eglBindAPI(EGL_OPENGL_ES_API);
255 
256 #ifdef ENABLE_GL_LOG
257     if (s_egl.eglDebugMessageControlKHR) {
258         const EGLAttrib controls[] = {
259             EGL_DEBUG_MSG_CRITICAL_KHR,
260             EGL_TRUE,
261             EGL_DEBUG_MSG_ERROR_KHR,
262             EGL_TRUE,
263             EGL_DEBUG_MSG_WARN_KHR,
264             EGL_TRUE,
265             EGL_DEBUG_MSG_INFO_KHR,
266             EGL_FALSE,
267             EGL_NONE,
268             EGL_NONE,
269         };
270 
271         if (s_egl.eglDebugMessageControlKHR(&EglDebugCallback, controls) == EGL_SUCCESS) {
272             GL_LOG("Successfully set eglDebugMessageControlKHR");
273         } else {
274             GL_LOG("Failed to eglDebugMessageControlKHR");
275         }
276     } else {
277         GL_LOG("eglDebugMessageControlKHR not available");
278     }
279 #endif
280 
281     emulationGl->mEglVendor = s_egl.eglQueryString(emulationGl->mEglDisplay, EGL_VENDOR);
282 
283     const std::string eglExtensions = s_egl.eglQueryString(emulationGl->mEglDisplay, EGL_EXTENSIONS);
284     android::base::split<std::string>(eglExtensions, " ",
285                                       [&](const std::string& found) {
286                                         emulationGl->mEglExtensions.insert(found);
287                                       });
288 
289     if (!emulationGl->hasEglExtension("EGL_KHR_gl_texture_2D_image")) {
290         ERR("Failed to find required EGL_KHR_gl_texture_2D_image extension.");
291         return nullptr;
292     }
293 
294     emulationGl->mGlesDispatchMaxVersion
295         = calcMaxVersionFromDispatch(emulationGl->mFeatures, emulationGl->mEglDisplay);
296     if (s_egl.eglSetMaxGLESVersion) {
297         // eglSetMaxGLESVersion must be called before any context binding
298         // because it changes how we initialize the dispatcher table.
299         s_egl.eglSetMaxGLESVersion(emulationGl->mGlesDispatchMaxVersion);
300     }
301 
302     int glesVersionMajor;
303     int glesVersionMinor;
304     emugl::getGlesVersion(&glesVersionMajor, &glesVersionMinor);
305     emulationGl->mGlesVersionMajor = glesVersionMajor;
306     emulationGl->mGlesVersionMinor = glesVersionMinor;
307 
308     if (!validateGles2Context(emulationGl->mEglDisplay)) {
309         ERR("Failed to validate creating GLES 2.x context.");
310         return nullptr;
311     }
312 
313     // TODO (b/207426737): Remove the Imagination-specific workaround.
314     const bool disableFastBlit =
315         emulationGl->mEglVendor.find("Imagination Technologies") != std::string::npos;
316 
317     emulationGl->mFastBlitSupported =
318         (emulationGl->mGlesDispatchMaxVersion > GLES_DISPATCH_MAX_VERSION_2) &&
319         !disableFastBlit &&
320         (emugl::getRenderer() == SELECTED_RENDERER_HOST ||
321          emugl::getRenderer() == SELECTED_RENDERER_SWIFTSHADER_INDIRECT ||
322          emugl::getRenderer() == SELECTED_RENDERER_ANGLE_INDIRECT);
323 
324     auto eglConfigOpt = getEmulationEglConfig(emulationGl->mEglDisplay, allowWindowSurface);
325     if (!eglConfigOpt) {
326         ERR("Failed to find config for emulation GL.");
327         return nullptr;
328     }
329     emulationGl->mEglConfig = *eglConfigOpt;
330 
331     const GLint* maxContextAttribs = getGlesMaxContextAttribs();
332 
333     emulationGl->mEglContext = s_egl.eglCreateContext(emulationGl->mEglDisplay,
334                                                       emulationGl->mEglConfig,
335                                                       EGL_NO_CONTEXT,
336                                                       maxContextAttribs);
337     if (emulationGl->mEglContext == EGL_NO_CONTEXT) {
338         ERR("Failed to create context, error 0x%x.", s_egl.eglGetError());
339         return nullptr;
340     }
341 
342     // Create another context which shares with the default context to be
343     // used when we bind the pbuffer. This prevents switching the drawable
344     // binding back and forth on the framebuffer context.
345     // The main purpose of it is to solve a "blanking" behaviour we see on
346     // on Mac platform when switching binded drawable for a context however
347     // it is more efficient on other platforms as well.
348     auto pbufferSurfaceGl = DisplaySurfaceGl::createPbufferSurface(emulationGl->mEglDisplay,
349                                                                    emulationGl->mEglConfig,
350                                                                    emulationGl->mEglContext,
351                                                                    maxContextAttribs,
352                                                                    /*width=*/1,
353                                                                    /*height=*/1);
354     if (!pbufferSurfaceGl) {
355         ERR("Failed to create pbuffer display surface.");
356         return nullptr;
357     }
358     auto* pbufferSurfaceGlPtr = pbufferSurfaceGl.get();
359 
360     emulationGl->mPbufferSurface = std::make_unique<gfxstream::DisplaySurface>(
361         /*width=*/1,
362         /*height=*/1,
363         std::move(pbufferSurfaceGl));
364 
365     emulationGl->mEmulatedEglConfigs =
366         std::make_unique<EmulatedEglConfigList>(emulationGl->mEglDisplay,
367                                                 emulationGl->mGlesDispatchMaxVersion,
368                                                 emulationGl->mFeatures);
369     if (emulationGl->mEmulatedEglConfigs->empty()) {
370         ERR("Failed to initialize emulated configs.");
371         return nullptr;
372     }
373 
374     const bool hasEsOrEs2Context =
375         std::any_of(emulationGl->mEmulatedEglConfigs->begin(),
376                     emulationGl->mEmulatedEglConfigs->end(),
377                     [](const EmulatedEglConfig& config) {
378                         const GLint renderableType = config.getRenderableType();
379                         return renderableType & (EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT);
380                     });
381     if (!hasEsOrEs2Context) {
382         ERR("Failed to find any usable guest EGL configs.");
383         return nullptr;
384     }
385 
386     RecursiveScopedContextBind contextBind(pbufferSurfaceGlPtr->getContextHelper());
387     if (!contextBind.isOk()) {
388         ERR("Failed to make pbuffer context and surface current");
389         return nullptr;
390     }
391 
392 #ifdef ENABLE_GL_LOG
393     bool debugSetup = false;
394     if (s_gles2.glDebugMessageCallback) {
395         s_gles2.glEnable(GL_DEBUG_OUTPUT);
396         s_gles2.glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
397         s_gles2.glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE,
398                                       GL_DEBUG_SEVERITY_HIGH, 0, nullptr, GL_TRUE);
399         s_gles2.glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE,
400                                       GL_DEBUG_SEVERITY_MEDIUM, 0, nullptr, GL_TRUE);
401         s_gles2.glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE,
402                                       GL_DEBUG_SEVERITY_LOW, 0, nullptr, GL_TRUE);
403         s_gles2.glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE,
404                                       GL_DEBUG_SEVERITY_NOTIFICATION, 0, nullptr,
405                                       GL_TRUE);
406         s_gles2.glDebugMessageCallback(&GlDebugCallback, nullptr);
407         debugSetup = s_gles2.glGetError() == GL_NO_ERROR;
408         if (!debugSetup) {
409             ERR("Failed to set up glDebugMessageCallback");
410         } else {
411             GL_LOG("Successfully set up glDebugMessageCallback");
412         }
413     }
414     if (s_gles2.glDebugMessageCallbackKHR && !debugSetup) {
415         s_gles2.glDebugMessageControlKHR(GL_DONT_CARE, GL_DONT_CARE,
416                                          GL_DEBUG_SEVERITY_HIGH_KHR, 0, nullptr,
417                                          GL_TRUE);
418         s_gles2.glDebugMessageControlKHR(GL_DONT_CARE, GL_DONT_CARE,
419                                          GL_DEBUG_SEVERITY_MEDIUM_KHR, 0, nullptr,
420                                          GL_TRUE);
421         s_gles2.glDebugMessageControlKHR(GL_DONT_CARE, GL_DONT_CARE,
422                                          GL_DEBUG_SEVERITY_LOW_KHR, 0, nullptr,
423                                          GL_TRUE);
424         s_gles2.glDebugMessageControlKHR(GL_DONT_CARE, GL_DONT_CARE,
425                                          GL_DEBUG_SEVERITY_NOTIFICATION_KHR, 0, nullptr,
426                                          GL_TRUE);
427         s_gles2.glDebugMessageCallbackKHR(&GlDebugCallback, nullptr);
428         debugSetup = s_gles2.glGetError() == GL_NO_ERROR;
429         if (!debugSetup) {
430             ERR("Failed to set up glDebugMessageCallbackKHR");
431         } else {
432             GL_LOG("Successfully set up glDebugMessageCallbackKHR");
433         }
434     }
435     if (!debugSetup) {
436         GL_LOG("glDebugMessageCallback and glDebugMessageCallbackKHR not available");
437     }
438 #endif
439 
440     emulationGl->mGlesVendor = (const char*)s_gles2.glGetString(GL_VENDOR);
441     emulationGl->mGlesRenderer = (const char*)s_gles2.glGetString(GL_RENDERER);
442     emulationGl->mGlesVersion = (const char*)s_gles2.glGetString(GL_VERSION);
443     emulationGl->mGlesExtensions = (const char*)s_gles2.glGetString(GL_EXTENSIONS);
444 
445     s_gles2.glGetError();
446     GLint numDeviceUuids = 0;
447     s_gles2.glGetIntegerv(GL_NUM_DEVICE_UUIDS_EXT, &numDeviceUuids);
448     if (numDeviceUuids == 1) {
449         GlesUuid uuid{};
450         s_gles2.glGetUnsignedBytei_vEXT(GL_DEVICE_UUID_EXT, 0, uuid.data());
451         emulationGl->mGlesDeviceUuid = uuid;
452     }
453 
454     emulationGl->mGlesVulkanInteropSupported = false;
455     if (s_egl.eglQueryVulkanInteropSupportANDROID) {
456         emulationGl->mGlesVulkanInteropSupported = s_egl.eglQueryVulkanInteropSupportANDROID();
457     }
458 
459     emulationGl->mTextureDraw = std::make_unique<TextureDraw>();
460     if (!emulationGl->mTextureDraw) {
461         ERR("Failed to initialize TextureDraw.");
462         return nullptr;
463     }
464 
465     emulationGl->mCompositorGl = std::make_unique<CompositorGl>(emulationGl->mTextureDraw.get());
466 
467     emulationGl->mDisplayGl = std::make_unique<DisplayGl>(emulationGl->mTextureDraw.get());
468 
469     {
470         auto surface1 = DisplaySurfaceGl::createPbufferSurface(emulationGl->mEglDisplay,
471                                                                emulationGl->mEglConfig,
472                                                                emulationGl->mEglContext,
473                                                                getGlesMaxContextAttribs(),
474                                                                /*width=*/1,
475                                                                /*height=*/1);
476         if (!surface1) {
477             ERR("Failed to create pbuffer surface for ReadbackWorkerGl.");
478             return nullptr;
479         }
480 
481         auto surface2 = DisplaySurfaceGl::createPbufferSurface(emulationGl->mEglDisplay,
482                                                                emulationGl->mEglConfig,
483                                                                emulationGl->mEglContext,
484                                                                getGlesMaxContextAttribs(),
485                                                                /*width=*/1,
486                                                                /*height=*/1);
487         if (!surface2) {
488             ERR("Failed to create pbuffer surface for ReadbackWorkerGl.");
489             return nullptr;
490         }
491 
492         emulationGl->mReadbackWorkerGl = std::make_unique<ReadbackWorkerGl>(std::move(surface1),
493                                                                             std::move(surface2));
494     }
495 
496     return emulationGl;
497 }
498 
~EmulationGl()499 EmulationGl::~EmulationGl() {
500     if (mPbufferSurface) {
501         // TODO(b/267349580): remove after Mac issue fixed.
502         mTextureDraw.release();
503         // const auto* displaySurfaceGl =
504         //    reinterpret_cast<const DisplaySurfaceGl*>(mPbufferSurface->getImpl());
505         // RecursiveScopedContextBind contextBind(displaySurfaceGl->getContextHelper());
506         // if (contextBind.isOk()) {
507         //     mTextureDraw.reset();
508         // } else {
509         //     ERR("Failed to bind context for destroying TextureDraw.");
510         // }
511     }
512 
513     if (mEglDisplay != EGL_NO_DISPLAY) {
514         s_egl.eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
515         if (mEglContext != EGL_NO_CONTEXT) {
516             s_egl.eglDestroyContext(mEglDisplay, mEglContext);
517             mEglContext = EGL_NO_CONTEXT;
518         }
519         mEglDisplay = EGL_NO_DISPLAY;
520     }
521 }
522 
createFakeWindowSurface()523 std::unique_ptr<gfxstream::DisplaySurface> EmulationGl::createFakeWindowSurface() {
524     return std::make_unique<gfxstream::DisplaySurface>(
525         mWidth, mHeight,
526         std::move(DisplaySurfaceGl::createPbufferSurface(
527             mEglDisplay, mEglConfig, mEglContext, getGlesMaxContextAttribs(), mWidth, mHeight)));
528 }
529 
getGlesMaxContextAttribs()530 /*static*/ const GLint* EmulationGl::getGlesMaxContextAttribs() {
531     int glesMaj, glesMin;
532     emugl::getGlesVersion(&glesMaj, &glesMin);
533     if (shouldEnableCoreProfile()) {
534         if (glesMaj == 2) {
535             return kGles2ContextAttribsCoreGL;
536         } else {
537             return kGles3ContextAttribsCoreGL;
538         }
539     }
540     if (glesMaj == 2) {
541         return kGles2ContextAttribsESOrGLCompat;
542     } else {
543         return kGles3ContextAttribsESOrGLCompat;
544     }
545 }
546 
getEglDispatch()547 const EGLDispatch* EmulationGl::getEglDispatch() {
548     return &s_egl;
549 }
550 
getGles2Dispatch()551 const GLESv2Dispatch* EmulationGl::getGles2Dispatch() {
552     return &s_gles2;
553 }
554 
getGlesMaxDispatchVersion() const555 GLESDispatchMaxVersion EmulationGl::getGlesMaxDispatchVersion() const {
556     return mGlesDispatchMaxVersion;
557 }
558 
hasEglExtension(const std::string & ext) const559 bool EmulationGl::hasEglExtension(const std::string& ext) const {
560     return mEglExtensions.find(ext) != mEglExtensions.end();
561 }
562 
getEglVersion(EGLint * major,EGLint * minor) const563 void EmulationGl::getEglVersion(EGLint* major, EGLint* minor) const {
564     if (major) {
565         *major = mEglVersionMajor;
566     }
567     if (minor) {
568         *minor = mEglVersionMinor;
569     }
570 }
571 
getGlesVersion(GLint * major,GLint * minor) const572 void EmulationGl::getGlesVersion(GLint* major, GLint* minor) const {
573     if (major) {
574         *major = mGlesVersionMajor;
575     }
576     if (minor) {
577         *minor = mGlesVersionMinor;
578     }
579 }
580 
isMesa() const581 bool EmulationGl::isMesa() const { return mGlesVersion.find("Mesa") != std::string::npos; }
582 
isFastBlitSupported() const583 bool EmulationGl::isFastBlitSupported() const {
584     return mFastBlitSupported;
585 }
586 
disableFastBlitForTesting()587 void EmulationGl::disableFastBlitForTesting() {
588     mFastBlitSupported = false;
589 }
590 
isAsyncReadbackSupported() const591 bool EmulationGl::isAsyncReadbackSupported() const {
592     return mGlesVersionMajor > 2;
593 }
594 
createWindowSurface(uint32_t width,uint32_t height,EGLNativeWindowType window)595 std::unique_ptr<DisplaySurface> EmulationGl::createWindowSurface(
596         uint32_t width,
597         uint32_t height,
598         EGLNativeWindowType window) {
599     auto surfaceGl = DisplaySurfaceGl::createWindowSurface(mEglDisplay,
600                                                            mEglConfig,
601                                                            mEglContext,
602                                                            getGlesMaxContextAttribs(),
603                                                            window);
604     if (!surfaceGl) {
605         ERR("Failed to create DisplaySurfaceGl.");
606         return nullptr;
607     }
608 
609     return std::make_unique<DisplaySurface>(width,
610                                             height,
611                                             std::move(surfaceGl));
612 }
613 
getColorBufferContextHelper()614 ContextHelper* EmulationGl::getColorBufferContextHelper() {
615     if (!mPbufferSurface) {
616         return nullptr;
617     }
618 
619     const auto* surfaceGl = static_cast<const DisplaySurfaceGl*>(mPbufferSurface->getImpl());
620     return surfaceGl->getContextHelper();
621 }
622 
createBuffer(uint64_t size,HandleType handle)623 std::unique_ptr<BufferGl> EmulationGl::createBuffer(uint64_t size, HandleType handle) {
624     return BufferGl::create(size, handle, getColorBufferContextHelper());
625 }
626 
loadBuffer(android::base::Stream * stream)627 std::unique_ptr<BufferGl> EmulationGl::loadBuffer(android::base::Stream* stream) {
628     return BufferGl::onLoad(stream, getColorBufferContextHelper());
629 }
630 
createColorBuffer(uint32_t width,uint32_t height,GLenum internalFormat,FrameworkFormat frameworkFormat,HandleType handle)631 std::unique_ptr<ColorBufferGl> EmulationGl::createColorBuffer(uint32_t width, uint32_t height,
632                                                               GLenum internalFormat,
633                                                               FrameworkFormat frameworkFormat,
634                                                               HandleType handle) {
635     return ColorBufferGl::create(mEglDisplay, width, height, internalFormat, frameworkFormat,
636                                  handle, getColorBufferContextHelper(), mTextureDraw.get(),
637                                  isFastBlitSupported(), mFeatures);
638 }
639 
loadColorBuffer(android::base::Stream * stream)640 std::unique_ptr<ColorBufferGl> EmulationGl::loadColorBuffer(android::base::Stream* stream) {
641     return ColorBufferGl::onLoad(stream, mEglDisplay, getColorBufferContextHelper(),
642                                  mTextureDraw.get(), isFastBlitSupported(), mFeatures);
643 }
644 
createEmulatedEglContext(uint32_t emulatedEglConfigIndex,const EmulatedEglContext * sharedContext,GLESApi api,HandleType handle)645 std::unique_ptr<EmulatedEglContext> EmulationGl::createEmulatedEglContext(
646         uint32_t emulatedEglConfigIndex,
647         const EmulatedEglContext* sharedContext,
648         GLESApi api,
649         HandleType handle) {
650     if (!mEmulatedEglConfigs) {
651         ERR("EmulatedEglConfigs unavailable.");
652         return nullptr;
653     }
654 
655     const EmulatedEglConfig* emulatedEglConfig = mEmulatedEglConfigs->get(emulatedEglConfigIndex);
656     if (!emulatedEglConfig) {
657         ERR("Failed to find emulated EGL config %d", emulatedEglConfigIndex);
658         return nullptr;
659     }
660 
661     EGLConfig config = emulatedEglConfig->getHostEglConfig();
662     EGLContext share = sharedContext ? sharedContext->getEGLContext() : EGL_NO_CONTEXT;
663 
664     return EmulatedEglContext::create(mEglDisplay, config, share, handle, api);
665 }
666 
loadEmulatedEglContext(android::base::Stream * stream)667 std::unique_ptr<EmulatedEglContext> EmulationGl::loadEmulatedEglContext(
668         android::base::Stream* stream) {
669     return EmulatedEglContext::onLoad(stream, mEglDisplay);
670 }
671 
createEmulatedEglFenceSync(EGLenum type,int destroyWhenSignaled)672 std::unique_ptr<EmulatedEglFenceSync> EmulationGl::createEmulatedEglFenceSync(
673         EGLenum type,
674         int destroyWhenSignaled) {
675     const bool hasNativeFence = type == EGL_SYNC_NATIVE_FENCE_ANDROID;
676     return EmulatedEglFenceSync::create(mEglDisplay,
677                                         hasNativeFence,
678                                         destroyWhenSignaled);
679 
680 }
681 
createEmulatedEglImage(EmulatedEglContext * context,EGLenum target,EGLClientBuffer buffer)682 std::unique_ptr<EmulatedEglImage> EmulationGl::createEmulatedEglImage(
683         EmulatedEglContext* context,
684         EGLenum target,
685         EGLClientBuffer buffer) {
686     EGLContext eglContext = context ? context->getEGLContext() : EGL_NO_CONTEXT;
687     return EmulatedEglImage::create(mEglDisplay, eglContext, target, buffer);
688 }
689 
createEmulatedEglWindowSurface(uint32_t emulatedConfigIndex,uint32_t width,uint32_t height,HandleType handle)690 std::unique_ptr<EmulatedEglWindowSurface> EmulationGl::createEmulatedEglWindowSurface(
691         uint32_t emulatedConfigIndex,
692         uint32_t width,
693         uint32_t height,
694         HandleType handle) {
695     if (!mEmulatedEglConfigs) {
696         ERR("EmulatedEglConfigs unavailable.");
697         return nullptr;
698     }
699 
700     const EmulatedEglConfig* emulatedEglConfig = mEmulatedEglConfigs->get(emulatedConfigIndex);
701     if (!emulatedEglConfig) {
702         ERR("Failed to find emulated EGL config %d", emulatedConfigIndex);
703         return nullptr;
704     }
705 
706     EGLConfig config = emulatedEglConfig->getHostEglConfig();
707 
708     return EmulatedEglWindowSurface::create(mEglDisplay, config, width, height, handle);
709 }
710 
loadEmulatedEglWindowSurface(android::base::Stream * stream,const ColorBufferMap & colorBuffers,const EmulatedEglContextMap & contexts)711 std::unique_ptr<EmulatedEglWindowSurface> EmulationGl::loadEmulatedEglWindowSurface(
712         android::base::Stream* stream,
713         const ColorBufferMap& colorBuffers,
714         const EmulatedEglContextMap& contexts) {
715     return EmulatedEglWindowSurface::onLoad(stream, mEglDisplay, colorBuffers, contexts);
716 }
717 
718 }  // namespace gl
719 }  // namespace gfxstream
720