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,bool allowWindowSurface,bool egl2egl)206 std::unique_ptr<EmulationGl> EmulationGl::create(uint32_t width, uint32_t height,
207 bool allowWindowSurface, bool egl2egl) {
208 // Loads the glestranslator function pointers.
209 if (!LazyLoadedEGLDispatch::get()) {
210 ERR("Failed to load EGL dispatch.");
211 return nullptr;
212 }
213 if (!LazyLoadedGLESv1Dispatch::get()) {
214 ERR("Failed to load GLESv1 dispatch.");
215 return nullptr;
216 }
217 if (!LazyLoadedGLESv2Dispatch::get()) {
218 ERR("Failed to load GLESv2 dispatch.");
219 return nullptr;
220 }
221
222 if (s_egl.eglUseOsEglApi) {
223 s_egl.eglUseOsEglApi(egl2egl, EGL_FALSE);
224 }
225
226 std::unique_ptr<EmulationGl> emulationGl(new EmulationGl());
227
228 emulationGl->mEglDisplay = s_egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
229 if (emulationGl->mEglDisplay == EGL_NO_DISPLAY) {
230 ERR("Failed to get EGL display.");
231 return nullptr;
232 }
233
234 GL_LOG("call eglInitialize");
235 if (!s_egl.eglInitialize(emulationGl->mEglDisplay,
236 &emulationGl->mEglVersionMajor,
237 &emulationGl->mEglVersionMinor)) {
238 ERR("Failed to eglInitialize.");
239 return nullptr;
240 }
241
242 s_egl.eglBindAPI(EGL_OPENGL_ES_API);
243
244 #ifdef ENABLE_GL_LOG
245 if (s_egl.eglDebugMessageControlKHR) {
246 const EGLAttrib controls[] = {
247 EGL_DEBUG_MSG_CRITICAL_KHR,
248 EGL_TRUE,
249 EGL_DEBUG_MSG_ERROR_KHR,
250 EGL_TRUE,
251 EGL_DEBUG_MSG_WARN_KHR,
252 EGL_TRUE,
253 EGL_DEBUG_MSG_INFO_KHR,
254 EGL_FALSE,
255 EGL_NONE,
256 EGL_NONE,
257 };
258
259 if (s_egl.eglDebugMessageControlKHR(&EglDebugCallback, controls) == EGL_SUCCESS) {
260 GL_LOG("Successfully set eglDebugMessageControlKHR");
261 } else {
262 GL_LOG("Failed to eglDebugMessageControlKHR");
263 }
264 } else {
265 GL_LOG("eglDebugMessageControlKHR not available");
266 }
267 #endif
268
269 emulationGl->mEglVendor = s_egl.eglQueryString(emulationGl->mEglDisplay, EGL_VENDOR);
270
271 const std::string eglExtensions = s_egl.eglQueryString(emulationGl->mEglDisplay, EGL_EXTENSIONS);
272 android::base::split<std::string>(eglExtensions, " ",
273 [&](const std::string& found) {
274 emulationGl->mEglExtensions.insert(found);
275 });
276
277 if (!emulationGl->hasEglExtension("EGL_KHR_gl_texture_2D_image")) {
278 ERR("Failed to find required EGL_KHR_gl_texture_2D_image extension.");
279 return nullptr;
280 }
281
282 emulationGl->mGlesDispatchMaxVersion
283 = calcMaxVersionFromDispatch(emulationGl->mEglDisplay);
284 if (s_egl.eglSetMaxGLESVersion) {
285 // eglSetMaxGLESVersion must be called before any context binding
286 // because it changes how we initialize the dispatcher table.
287 s_egl.eglSetMaxGLESVersion(emulationGl->mGlesDispatchMaxVersion);
288 }
289
290 int glesVersionMajor;
291 int glesVersionMinor;
292 emugl::getGlesVersion(&glesVersionMajor, &glesVersionMinor);
293 emulationGl->mGlesVersionMajor = glesVersionMajor;
294 emulationGl->mGlesVersionMinor = glesVersionMinor;
295
296 if (!validateGles2Context(emulationGl->mEglDisplay)) {
297 ERR("Failed to validate creating GLES 2.x context.");
298 return nullptr;
299 }
300
301 // TODO (b/207426737): Remove the Imagination-specific workaround.
302 const bool disableFastBlit =
303 emulationGl->mEglVendor.find("Imagination Technologies") != std::string::npos;
304
305 emulationGl->mFastBlitSupported =
306 (emulationGl->mGlesDispatchMaxVersion > GLES_DISPATCH_MAX_VERSION_2) &&
307 !disableFastBlit &&
308 (emugl::getRenderer() == SELECTED_RENDERER_HOST ||
309 emugl::getRenderer() == SELECTED_RENDERER_SWIFTSHADER_INDIRECT ||
310 emugl::getRenderer() == SELECTED_RENDERER_ANGLE_INDIRECT);
311
312 auto eglConfigOpt = getEmulationEglConfig(emulationGl->mEglDisplay, allowWindowSurface);
313 if (!eglConfigOpt) {
314 ERR("Failed to find config for emulation GL.");
315 return nullptr;
316 }
317 emulationGl->mEglConfig = *eglConfigOpt;
318
319 const GLint* maxContextAttribs = getGlesMaxContextAttribs();
320
321 emulationGl->mEglContext = s_egl.eglCreateContext(emulationGl->mEglDisplay,
322 emulationGl->mEglConfig,
323 EGL_NO_CONTEXT,
324 maxContextAttribs);
325 if (emulationGl->mEglContext == EGL_NO_CONTEXT) {
326 ERR("Failed to create context, error 0x%x.", s_egl.eglGetError());
327 return nullptr;
328 }
329
330 // Create another context which shares with the default context to be
331 // used when we bind the pbuffer. This prevents switching the drawable
332 // binding back and forth on the framebuffer context.
333 // The main purpose of it is to solve a "blanking" behaviour we see on
334 // on Mac platform when switching binded drawable for a context however
335 // it is more efficient on other platforms as well.
336 auto pbufferSurfaceGl = DisplaySurfaceGl::createPbufferSurface(emulationGl->mEglDisplay,
337 emulationGl->mEglConfig,
338 emulationGl->mEglContext,
339 maxContextAttribs,
340 /*width=*/1,
341 /*height=*/1);
342 if (!pbufferSurfaceGl) {
343 ERR("Failed to create pbuffer display surface.");
344 return nullptr;
345 }
346 auto* pbufferSurfaceGlPtr = pbufferSurfaceGl.get();
347
348 emulationGl->mPbufferSurface = std::make_unique<gfxstream::DisplaySurface>(
349 /*width=*/1,
350 /*height=*/1,
351 std::move(pbufferSurfaceGl));
352
353 auto fakeWindowSurfaceGl = DisplaySurfaceGl::createPbufferSurface(emulationGl->mEglDisplay,
354 emulationGl->mEglConfig,
355 emulationGl->mEglContext,
356 maxContextAttribs,
357 width,
358 height);
359 if (!fakeWindowSurfaceGl) {
360 ERR("Failed to create fake window display surface.");
361 return nullptr;
362 }
363 emulationGl->mFakeWindowSurface = std::make_unique<gfxstream::DisplaySurface>(
364 width,
365 height,
366 std::move(fakeWindowSurfaceGl));
367
368 emulationGl->mEmulatedEglConfigs =
369 std::make_unique<EmulatedEglConfigList>(emulationGl->mEglDisplay,
370 emulationGl->mGlesDispatchMaxVersion);
371 if (emulationGl->mEmulatedEglConfigs->empty()) {
372 ERR("Failed to initialize emulated configs.");
373 return nullptr;
374 }
375
376 const bool hasEsOrEs2Context =
377 std::any_of(emulationGl->mEmulatedEglConfigs->begin(),
378 emulationGl->mEmulatedEglConfigs->end(),
379 [](const EmulatedEglConfig& config) {
380 const GLint renderableType = config.getRenderableType();
381 return renderableType & (EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT);
382 });
383 if (!hasEsOrEs2Context) {
384 ERR("Failed to find any usable guest EGL configs.");
385 return nullptr;
386 }
387
388 RecursiveScopedContextBind contextBind(pbufferSurfaceGlPtr->getContextHelper());
389 if (!contextBind.isOk()) {
390 ERR("Failed to make pbuffer context and surface current");
391 return nullptr;
392 }
393
394 #ifdef ENABLE_GL_LOG
395 bool debugSetup = false;
396 if (s_gles2.glDebugMessageCallback) {
397 s_gles2.glEnable(GL_DEBUG_OUTPUT);
398 s_gles2.glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
399 s_gles2.glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE,
400 GL_DEBUG_SEVERITY_HIGH, 0, nullptr, GL_TRUE);
401 s_gles2.glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE,
402 GL_DEBUG_SEVERITY_MEDIUM, 0, nullptr, GL_TRUE);
403 s_gles2.glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE,
404 GL_DEBUG_SEVERITY_LOW, 0, nullptr, GL_TRUE);
405 s_gles2.glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE,
406 GL_DEBUG_SEVERITY_NOTIFICATION, 0, nullptr,
407 GL_TRUE);
408 s_gles2.glDebugMessageCallback(&GlDebugCallback, nullptr);
409 debugSetup = s_gles2.glGetError() == GL_NO_ERROR;
410 if (!debugSetup) {
411 ERR("Failed to set up glDebugMessageCallback");
412 } else {
413 GL_LOG("Successfully set up glDebugMessageCallback");
414 }
415 }
416 if (s_gles2.glDebugMessageCallbackKHR && !debugSetup) {
417 s_gles2.glDebugMessageControlKHR(GL_DONT_CARE, GL_DONT_CARE,
418 GL_DEBUG_SEVERITY_HIGH_KHR, 0, nullptr,
419 GL_TRUE);
420 s_gles2.glDebugMessageControlKHR(GL_DONT_CARE, GL_DONT_CARE,
421 GL_DEBUG_SEVERITY_MEDIUM_KHR, 0, nullptr,
422 GL_TRUE);
423 s_gles2.glDebugMessageControlKHR(GL_DONT_CARE, GL_DONT_CARE,
424 GL_DEBUG_SEVERITY_LOW_KHR, 0, nullptr,
425 GL_TRUE);
426 s_gles2.glDebugMessageControlKHR(GL_DONT_CARE, GL_DONT_CARE,
427 GL_DEBUG_SEVERITY_NOTIFICATION_KHR, 0, nullptr,
428 GL_TRUE);
429 s_gles2.glDebugMessageCallbackKHR(&GlDebugCallback, nullptr);
430 debugSetup = s_gles2.glGetError() == GL_NO_ERROR;
431 if (!debugSetup) {
432 ERR("Failed to set up glDebugMessageCallbackKHR");
433 } else {
434 GL_LOG("Successfully set up glDebugMessageCallbackKHR");
435 }
436 }
437 if (!debugSetup) {
438 GL_LOG("glDebugMessageCallback and glDebugMessageCallbackKHR not available");
439 }
440 #endif
441
442 emulationGl->mGlesVendor = (const char*)s_gles2.glGetString(GL_VENDOR);
443 emulationGl->mGlesRenderer = (const char*)s_gles2.glGetString(GL_RENDERER);
444 emulationGl->mGlesVersion = (const char*)s_gles2.glGetString(GL_VERSION);
445 emulationGl->mGlesExtensions = (const char*)s_gles2.glGetString(GL_EXTENSIONS);
446
447 s_gles2.glGetError();
448 GLint numDeviceUuids = 0;
449 s_gles2.glGetIntegerv(GL_NUM_DEVICE_UUIDS_EXT, &numDeviceUuids);
450 if (numDeviceUuids == 1) {
451 GlesUuid uuid{};
452 s_gles2.glGetUnsignedBytei_vEXT(GL_DEVICE_UUID_EXT, 0, uuid.data());
453 emulationGl->mGlesDeviceUuid = uuid;
454 }
455
456 emulationGl->mGlesVulkanInteropSupported = false;
457 if (s_egl.eglQueryVulkanInteropSupportANDROID) {
458 emulationGl->mGlesVulkanInteropSupported = s_egl.eglQueryVulkanInteropSupportANDROID();
459 }
460
461 emulationGl->mTextureDraw = std::make_unique<TextureDraw>();
462 if (!emulationGl->mTextureDraw) {
463 ERR("Failed to initialize TextureDraw.");
464 return nullptr;
465 }
466
467 emulationGl->mCompositorGl = std::make_unique<CompositorGl>(emulationGl->mTextureDraw.get());
468 emulationGl->mCompositorGl->bindToSurface(emulationGl->mFakeWindowSurface.get());
469
470 emulationGl->mDisplayGl = std::make_unique<DisplayGl>(emulationGl->mTextureDraw.get());
471 emulationGl->mDisplayGl->bindToSurface(emulationGl->mFakeWindowSurface.get());
472
473 {
474 auto surface1 = DisplaySurfaceGl::createPbufferSurface(emulationGl->mEglDisplay,
475 emulationGl->mEglConfig,
476 emulationGl->mEglContext,
477 getGlesMaxContextAttribs(),
478 /*width=*/1,
479 /*height=*/1);
480 if (!surface1) {
481 ERR("Failed to create pbuffer surface for ReadbackWorkerGl.");
482 return nullptr;
483 }
484
485 auto surface2 = DisplaySurfaceGl::createPbufferSurface(emulationGl->mEglDisplay,
486 emulationGl->mEglConfig,
487 emulationGl->mEglContext,
488 getGlesMaxContextAttribs(),
489 /*width=*/1,
490 /*height=*/1);
491 if (!surface2) {
492 ERR("Failed to create pbuffer surface for ReadbackWorkerGl.");
493 return nullptr;
494 }
495
496 emulationGl->mReadbackWorkerGl = std::make_unique<ReadbackWorkerGl>(std::move(surface1),
497 std::move(surface2));
498 }
499
500 return emulationGl;
501 }
502
~EmulationGl()503 EmulationGl::~EmulationGl() {
504 if (mCompositorGl) {
505 mCompositorGl->unbindFromSurface();
506 }
507 if (mDisplayGl) {
508 mDisplayGl->unbindFromSurface();
509 }
510
511 if (mPbufferSurface) {
512 // TODO(b/267349580): remove after Mac issue fixed.
513 mTextureDraw.release();
514 // const auto* displaySurfaceGl =
515 // reinterpret_cast<const DisplaySurfaceGl*>(mPbufferSurface->getImpl());
516 // RecursiveScopedContextBind contextBind(displaySurfaceGl->getContextHelper());
517 // if (contextBind.isOk()) {
518 // mTextureDraw.reset();
519 // } else {
520 // ERR("Failed to bind context for destroying TextureDraw.");
521 // }
522 }
523
524 if (mEglDisplay != EGL_NO_DISPLAY) {
525 s_egl.eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
526 if (mEglContext != EGL_NO_CONTEXT) {
527 s_egl.eglDestroyContext(mEglDisplay, mEglContext);
528 mEglContext = EGL_NO_CONTEXT;
529 }
530 mEglDisplay = EGL_NO_DISPLAY;
531 }
532 }
533
getGlesMaxContextAttribs()534 /*static*/ const GLint* EmulationGl::getGlesMaxContextAttribs() {
535 int glesMaj, glesMin;
536 emugl::getGlesVersion(&glesMaj, &glesMin);
537 if (shouldEnableCoreProfile()) {
538 if (glesMaj == 2) {
539 return kGles2ContextAttribsCoreGL;
540 } else {
541 return kGles3ContextAttribsCoreGL;
542 }
543 }
544 if (glesMaj == 2) {
545 return kGles2ContextAttribsESOrGLCompat;
546 } else {
547 return kGles3ContextAttribsESOrGLCompat;
548 }
549 }
550
getEglDispatch()551 const EGLDispatch* EmulationGl::getEglDispatch() {
552 return &s_egl;
553 }
554
getGles2Dispatch()555 const GLESv2Dispatch* EmulationGl::getGles2Dispatch() {
556 return &s_gles2;
557 }
558
getGlesMaxDispatchVersion() const559 GLESDispatchMaxVersion EmulationGl::getGlesMaxDispatchVersion() const {
560 return mGlesDispatchMaxVersion;
561 }
562
hasEglExtension(const std::string & ext) const563 bool EmulationGl::hasEglExtension(const std::string& ext) const {
564 return mEglExtensions.find(ext) != mEglExtensions.end();
565 }
566
getEglVersion(EGLint * major,EGLint * minor) const567 void EmulationGl::getEglVersion(EGLint* major, EGLint* minor) const {
568 if (major) {
569 *major = mEglVersionMajor;
570 }
571 if (minor) {
572 *minor = mEglVersionMinor;
573 }
574 }
575
getGlesVersion(GLint * major,GLint * minor) const576 void EmulationGl::getGlesVersion(GLint* major, GLint* minor) const {
577 if (major) {
578 *major = mGlesVersionMajor;
579 }
580 if (minor) {
581 *minor = mGlesVersionMinor;
582 }
583 }
584
isMesa() const585 bool EmulationGl::isMesa() const { return mGlesVersion.find("Mesa") != std::string::npos; }
586
isFastBlitSupported() const587 bool EmulationGl::isFastBlitSupported() const {
588 return mFastBlitSupported;
589 }
590
disableFastBlitForTesting()591 void EmulationGl::disableFastBlitForTesting() {
592 mFastBlitSupported = false;
593 }
594
isAsyncReadbackSupported() const595 bool EmulationGl::isAsyncReadbackSupported() const {
596 return mGlesVersionMajor > 2;
597 }
598
createWindowSurface(uint32_t width,uint32_t height,EGLNativeWindowType window)599 std::unique_ptr<DisplaySurface> EmulationGl::createWindowSurface(
600 uint32_t width,
601 uint32_t height,
602 EGLNativeWindowType window) {
603 auto surfaceGl = DisplaySurfaceGl::createWindowSurface(mEglDisplay,
604 mEglConfig,
605 mEglContext,
606 getGlesMaxContextAttribs(),
607 window);
608 if (!surfaceGl) {
609 ERR("Failed to create DisplaySurfaceGl.");
610 return nullptr;
611 }
612
613 return std::make_unique<DisplaySurface>(width,
614 height,
615 std::move(surfaceGl));
616 }
617
setUseBoundSurfaceContextForDisplay(bool use)618 void EmulationGl::setUseBoundSurfaceContextForDisplay(bool use) {
619 if (mDisplayGl) {
620 mDisplayGl->setUseBoundSurfaceContext(use);
621 }
622 if (mCompositorGl) {
623 mCompositorGl->setUseBoundSurfaceContext(use);
624 }
625 }
626
getColorBufferContextHelper()627 ContextHelper* EmulationGl::getColorBufferContextHelper() {
628 if (!mPbufferSurface) {
629 return nullptr;
630 }
631
632 const auto* surfaceGl = static_cast<const DisplaySurfaceGl*>(mPbufferSurface->getImpl());
633 return surfaceGl->getContextHelper();
634 }
635
createBuffer(uint64_t size,HandleType handle)636 std::unique_ptr<BufferGl> EmulationGl::createBuffer(uint64_t size, HandleType handle) {
637 return BufferGl::create(size, handle, getColorBufferContextHelper());
638 }
639
loadBuffer(android::base::Stream * stream)640 std::unique_ptr<BufferGl> EmulationGl::loadBuffer(android::base::Stream* stream) {
641 return BufferGl::onLoad(stream, getColorBufferContextHelper());
642 }
643
createColorBuffer(uint32_t width,uint32_t height,GLenum internalFormat,FrameworkFormat frameworkFormat,HandleType handle)644 std::unique_ptr<ColorBufferGl> EmulationGl::createColorBuffer(uint32_t width, uint32_t height,
645 GLenum internalFormat,
646 FrameworkFormat frameworkFormat,
647 HandleType handle) {
648 return ColorBufferGl::create(mEglDisplay, width, height, internalFormat, frameworkFormat,
649 handle, getColorBufferContextHelper(), mTextureDraw.get(),
650 isFastBlitSupported());
651 }
652
loadColorBuffer(android::base::Stream * stream)653 std::unique_ptr<ColorBufferGl> EmulationGl::loadColorBuffer(android::base::Stream* stream) {
654 return ColorBufferGl::onLoad(stream, mEglDisplay, getColorBufferContextHelper(),
655 mTextureDraw.get(), isFastBlitSupported());
656 }
657
createEmulatedEglContext(uint32_t emulatedEglConfigIndex,const EmulatedEglContext * sharedContext,GLESApi api,HandleType handle)658 std::unique_ptr<EmulatedEglContext> EmulationGl::createEmulatedEglContext(
659 uint32_t emulatedEglConfigIndex,
660 const EmulatedEglContext* sharedContext,
661 GLESApi api,
662 HandleType handle) {
663 if (!mEmulatedEglConfigs) {
664 ERR("EmulatedEglConfigs unavailable.");
665 return nullptr;
666 }
667
668 const EmulatedEglConfig* emulatedEglConfig = mEmulatedEglConfigs->get(emulatedEglConfigIndex);
669 if (!emulatedEglConfig) {
670 ERR("Failed to find emulated EGL config %d", emulatedEglConfigIndex);
671 return nullptr;
672 }
673
674 EGLConfig config = emulatedEglConfig->getHostEglConfig();
675 EGLContext share = sharedContext ? sharedContext->getEGLContext() : EGL_NO_CONTEXT;
676
677 return EmulatedEglContext::create(mEglDisplay, config, share, handle, api);
678 }
679
loadEmulatedEglContext(android::base::Stream * stream)680 std::unique_ptr<EmulatedEglContext> EmulationGl::loadEmulatedEglContext(
681 android::base::Stream* stream) {
682 return EmulatedEglContext::onLoad(stream, mEglDisplay);
683 }
684
createEmulatedEglFenceSync(EGLenum type,int destroyWhenSignaled)685 std::unique_ptr<EmulatedEglFenceSync> EmulationGl::createEmulatedEglFenceSync(
686 EGLenum type,
687 int destroyWhenSignaled) {
688 const bool hasNativeFence = type == EGL_SYNC_NATIVE_FENCE_ANDROID;
689 return EmulatedEglFenceSync::create(mEglDisplay,
690 hasNativeFence,
691 destroyWhenSignaled);
692
693 }
694
createEmulatedEglImage(EmulatedEglContext * context,EGLenum target,EGLClientBuffer buffer)695 std::unique_ptr<EmulatedEglImage> EmulationGl::createEmulatedEglImage(
696 EmulatedEglContext* context,
697 EGLenum target,
698 EGLClientBuffer buffer) {
699 EGLContext eglContext = context ? context->getEGLContext() : EGL_NO_CONTEXT;
700 return EmulatedEglImage::create(mEglDisplay, eglContext, target, buffer);
701 }
702
createEmulatedEglWindowSurface(uint32_t emulatedConfigIndex,uint32_t width,uint32_t height,HandleType handle)703 std::unique_ptr<EmulatedEglWindowSurface> EmulationGl::createEmulatedEglWindowSurface(
704 uint32_t emulatedConfigIndex,
705 uint32_t width,
706 uint32_t height,
707 HandleType handle) {
708 if (!mEmulatedEglConfigs) {
709 ERR("EmulatedEglConfigs unavailable.");
710 return nullptr;
711 }
712
713 const EmulatedEglConfig* emulatedEglConfig = mEmulatedEglConfigs->get(emulatedConfigIndex);
714 if (!emulatedEglConfig) {
715 ERR("Failed to find emulated EGL config %d", emulatedConfigIndex);
716 return nullptr;
717 }
718
719 EGLConfig config = emulatedEglConfig->getHostEglConfig();
720
721 return EmulatedEglWindowSurface::create(mEglDisplay, config, width, height, handle);
722 }
723
loadEmulatedEglWindowSurface(android::base::Stream * stream,const ColorBufferMap & colorBuffers,const EmulatedEglContextMap & contexts)724 std::unique_ptr<EmulatedEglWindowSurface> EmulationGl::loadEmulatedEglWindowSurface(
725 android::base::Stream* stream,
726 const ColorBufferMap& colorBuffers,
727 const EmulatedEglContextMap& contexts) {
728 return EmulatedEglWindowSurface::onLoad(stream, mEglDisplay, colorBuffers, contexts);
729 }
730
731 } // namespace gl
732 } // namespace gfxstream
733