Hash :
2d3402eb
Author :
Date :
2021-02-18T09:05:02
Revert "EGL: Merge DisplayAndroid into DisplayEGL." This reverts commit 2cb6671534ddc6efe9664a2fb30be2b2fb13c8ef. Reason for revert: Make DisplayGbm fail to compile. See: https://ci.chromium.org/ui/p/chromium/builders/try/linux-chromeos-compile-dbg/636833/overview Original change's description: > EGL: Merge DisplayAndroid into DisplayEGL. > > These two classes have a lot of duplicated code for no reason. > > Bug: angleproject:5563 > Change-Id: Ia9d8efeb50f3e52ffb1df6a6e462efdb20fd4a9a > Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2636678 > Commit-Queue: Geoff Lang <geofflang@chromium.org> > Reviewed-by: Peng Huang <penghuang@chromium.org> > Reviewed-by: Jonah Ryan-Davis <jonahr@google.com> Bug: angleproject:5563 Change-Id: Ic39071130727b37cc2a168d4b8e3c50f3e7184a5 No-Presubmit: true No-Tree-Checks: true No-Try: true Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2704051 Reviewed-by: Corentin Wallez <cwallez@chromium.org> Commit-Queue: Corentin Wallez <cwallez@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
//
// Copyright 2016 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// DisplayAndroid.cpp: Android implementation of egl::Display
#include "libANGLE/renderer/gl/egl/android/DisplayAndroid.h"
#include <android/log.h>
#include <android/native_window.h>
#include "common/debug.h"
#include "libANGLE/Context.h"
#include "libANGLE/Display.h"
#include "libANGLE/Surface.h"
#include "libANGLE/renderer/gl/ContextGL.h"
#include "libANGLE/renderer/gl/RendererGL.h"
#include "libANGLE/renderer/gl/egl/ContextEGL.h"
#include "libANGLE/renderer/gl/egl/FunctionsEGLDL.h"
#include "libANGLE/renderer/gl/egl/RendererEGL.h"
#include "libANGLE/renderer/gl/egl/SurfaceEGL.h"
#include "libANGLE/renderer/gl/egl/android/NativeBufferImageSiblingAndroid.h"
#include "libANGLE/renderer/gl/renderergl_utils.h"
namespace
{
const char *GetEGLPath()
{
#if defined(__LP64__)
return "/system/lib64/libEGL.so";
#else
return "/system/lib/libEGL.so";
#endif
}
} // namespace
namespace rx
{
static constexpr bool kDefaultEGLVirtualizedContexts = true;
DisplayAndroid::DisplayAndroid(const egl::DisplayState &state)
: DisplayEGL(state),
mVirtualizedContexts(kDefaultEGLVirtualizedContexts),
mSupportsSurfaceless(false),
mMockPbuffer(EGL_NO_SURFACE)
{}
DisplayAndroid::~DisplayAndroid() {}
egl::Error DisplayAndroid::initialize(egl::Display *display)
{
mDisplayAttributes = display->getAttributeMap();
mVirtualizedContexts =
ShouldUseVirtualizedContexts(mDisplayAttributes, kDefaultEGLVirtualizedContexts);
FunctionsEGLDL *egl = new FunctionsEGLDL();
mEGL = egl;
void *eglHandle =
reinterpret_cast<void *>(mDisplayAttributes.get(EGL_PLATFORM_ANGLE_EGL_HANDLE_ANGLE, 0));
ANGLE_TRY(egl->initialize(display->getNativeDisplayId(), GetEGLPath(), eglHandle));
gl::Version eglVersion(mEGL->majorVersion, mEGL->minorVersion);
ASSERT(eglVersion >= gl::Version(1, 4));
std::vector<EGLint> renderableTypes;
static_assert(EGL_OPENGL_ES3_BIT == EGL_OPENGL_ES3_BIT_KHR, "Extension define must match core");
if (eglVersion >= gl::Version(1, 5) || mEGL->hasExtension("EGL_KHR_create_context"))
{
renderableTypes.push_back(EGL_OPENGL_ES3_BIT);
}
renderableTypes.push_back(EGL_OPENGL_ES2_BIT);
egl::AttributeMap baseConfigAttribs;
baseConfigAttribs.insert(EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER);
// Android doesn't support pixmaps
baseConfigAttribs.insert(EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PBUFFER_BIT);
egl::AttributeMap configAttribsWithFormat(baseConfigAttribs);
// Choose RGBA8888
configAttribsWithFormat.insert(EGL_RED_SIZE, 8);
configAttribsWithFormat.insert(EGL_GREEN_SIZE, 8);
configAttribsWithFormat.insert(EGL_BLUE_SIZE, 8);
configAttribsWithFormat.insert(EGL_ALPHA_SIZE, 8);
// Choose D24S8
// EGL1.5 spec Section 2.2 says that depth, multisample and stencil buffer depths
// must match for contexts to be compatible.
configAttribsWithFormat.insert(EGL_DEPTH_SIZE, 24);
configAttribsWithFormat.insert(EGL_STENCIL_SIZE, 8);
EGLConfig configWithFormat = EGL_NO_CONFIG_KHR;
for (EGLint renderableType : renderableTypes)
{
baseConfigAttribs.insert(EGL_RENDERABLE_TYPE, renderableType);
configAttribsWithFormat.insert(EGL_RENDERABLE_TYPE, renderableType);
std::vector<EGLint> attribVector = configAttribsWithFormat.toIntVector();
EGLint numConfig = 0;
if (mEGL->chooseConfig(attribVector.data(), &configWithFormat, 1, &numConfig) == EGL_TRUE)
{
break;
}
}
if (configWithFormat == EGL_NO_CONFIG_KHR)
{
return egl::EglNotInitialized()
<< "eglChooseConfig failed with " << egl::Error(mEGL->getError());
}
// A mock pbuffer is only needed if surfaceless contexts are not supported.
mSupportsSurfaceless = mEGL->hasExtension("EGL_KHR_surfaceless_context");
if (!mSupportsSurfaceless)
{
int mockPbufferAttribs[] = {
EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE,
};
mMockPbuffer = mEGL->createPbufferSurface(configWithFormat, mockPbufferAttribs);
if (mMockPbuffer == EGL_NO_SURFACE)
{
return egl::EglNotInitialized()
<< "eglCreatePbufferSurface failed with " << egl::Error(mEGL->getError());
}
}
// Create mMockPbuffer with a normal config, but create a no_config mContext, if possible
if (mEGL->hasExtension("EGL_KHR_no_config_context"))
{
mConfigAttribList = baseConfigAttribs.toIntVector();
mConfig = EGL_NO_CONFIG_KHR;
}
else
{
mConfigAttribList = configAttribsWithFormat.toIntVector();
mConfig = configWithFormat;
}
ANGLE_TRY(createRenderer(EGL_NO_CONTEXT, true, false, &mRenderer));
const gl::Version &maxVersion = mRenderer->getMaxSupportedESVersion();
if (maxVersion < gl::Version(2, 0))
{
return egl::EglNotInitialized() << "OpenGL ES 2.0 is not supportable.";
}
ANGLE_TRY(DisplayGL::initialize(display));
std::string rendererDescription = getRendererDescription();
__android_log_print(ANDROID_LOG_INFO, "ANGLE", "%s", rendererDescription.c_str());
return egl::NoError();
}
void DisplayAndroid::terminate()
{
DisplayGL::terminate();
EGLBoolean success = mEGL->makeCurrent(EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (success == EGL_FALSE)
{
ERR() << "eglMakeCurrent error " << egl::Error(mEGL->getError());
}
if (mMockPbuffer != EGL_NO_SURFACE)
{
success = mEGL->destroySurface(mMockPbuffer);
mMockPbuffer = EGL_NO_SURFACE;
if (success == EGL_FALSE)
{
ERR() << "eglDestroySurface error " << egl::Error(mEGL->getError());
}
}
mRenderer.reset();
egl::Error result = mEGL->terminate();
if (result.isError())
{
ERR() << "eglTerminate error " << result;
}
SafeDelete(mEGL);
}
ContextImpl *DisplayAndroid::createContext(const gl::State &state,
gl::ErrorSet *errorSet,
const egl::Config *configuration,
const gl::Context *shareContext,
const egl::AttributeMap &attribs)
{
std::shared_ptr<RendererEGL> renderer;
bool usingExternalContext = attribs.get(EGL_EXTERNAL_CONTEXT_ANGLE, EGL_FALSE) == EGL_TRUE;
if (mVirtualizedContexts && !usingExternalContext)
{
renderer = mRenderer;
}
else
{
EGLContext nativeShareContext = EGL_NO_CONTEXT;
if (usingExternalContext)
{
ASSERT(!shareContext);
}
else if (shareContext)
{
ContextEGL *shareContextEGL = GetImplAs<ContextEGL>(shareContext);
nativeShareContext = shareContextEGL->getContext();
}
// Create a new renderer for this context. It only needs to share with the user's requested
// share context because there are no internal resources in DisplayAndroid that are shared
// at the GL level.
egl::Error error =
createRenderer(nativeShareContext, false, usingExternalContext, &renderer);
if (error.isError())
{
ERR() << "Failed to create a shared renderer: " << error.getMessage();
return nullptr;
}
}
return new ContextEGL(state, errorSet, renderer,
RobustnessVideoMemoryPurgeStatus::NOT_REQUESTED);
}
class ExternalSurfaceEGL : public SurfaceEGL
{
public:
ExternalSurfaceEGL(const egl::SurfaceState &state,
const FunctionsEGL *egl,
EGLConfig config,
EGLint width,
EGLint height)
: SurfaceEGL(state, egl, config), mWidth(width), mHeight(height)
{}
~ExternalSurfaceEGL() override = default;
egl::Error initialize(const egl::Display *display) override { return egl::NoError(); }
EGLint getSwapBehavior() const override { return EGL_BUFFER_DESTROYED; }
EGLint getWidth() const override { return mWidth; }
EGLint getHeight() const override { return mHeight; }
bool isExternal() const override { return true; }
private:
const EGLint mWidth;
const EGLint mHeight;
};
SurfaceImpl *DisplayAndroid::createPbufferFromClientBuffer(const egl::SurfaceState &state,
EGLenum buftype,
EGLClientBuffer clientBuffer,
const egl::AttributeMap &attribs)
{
if (buftype == EGL_EXTERNAL_SURFACE_ANGLE)
{
ASSERT(clientBuffer == nullptr);
EGLint width = static_cast<EGLint>(attribs.get(EGL_WIDTH, 0));
EGLint height = static_cast<EGLint>(attribs.get(EGL_HEIGHT, 0));
// Use the ExternalSurfaceEGL, so ANGLE can know the framebuffer size.
return new ExternalSurfaceEGL(state, mEGL, EGL_NO_CONFIG_KHR, width, height);
}
return DisplayEGL::createPbufferFromClientBuffer(state, buftype, clientBuffer, attribs);
}
bool DisplayAndroid::isValidNativeWindow(EGLNativeWindowType window) const
{
return ANativeWindow_getFormat(window) >= 0;
}
egl::Error DisplayAndroid::validateClientBuffer(const egl::Config *configuration,
EGLenum buftype,
EGLClientBuffer clientBuffer,
const egl::AttributeMap &attribs) const
{
if (buftype == EGL_EXTERNAL_SURFACE_ANGLE)
{
ASSERT(clientBuffer == nullptr);
return egl::NoError();
}
return DisplayEGL::validateClientBuffer(configuration, buftype, clientBuffer, attribs);
}
egl::Error DisplayAndroid::validateImageClientBuffer(const gl::Context *context,
EGLenum target,
EGLClientBuffer clientBuffer,
const egl::AttributeMap &attribs) const
{
switch (target)
{
case EGL_NATIVE_BUFFER_ANDROID:
return egl::NoError();
default:
return DisplayEGL::validateImageClientBuffer(context, target, clientBuffer, attribs);
}
}
ExternalImageSiblingImpl *DisplayAndroid::createExternalImageSibling(
const gl::Context *context,
EGLenum target,
EGLClientBuffer buffer,
const egl::AttributeMap &attribs)
{
switch (target)
{
case EGL_NATIVE_BUFFER_ANDROID:
return new NativeBufferImageSiblingAndroid(buffer);
default:
return DisplayEGL::createExternalImageSibling(context, target, buffer, attribs);
}
}
egl::Error DisplayAndroid::makeCurrent(egl::Display *display,
egl::Surface *drawSurface,
egl::Surface *readSurface,
gl::Context *context)
{
CurrentNativeContext ¤tContext = mCurrentNativeContexts[std::this_thread::get_id()];
EGLSurface newSurface = EGL_NO_SURFACE;
if (drawSurface)
{
SurfaceEGL *drawSurfaceEGL = GetImplAs<SurfaceEGL>(drawSurface);
newSurface = drawSurfaceEGL->getSurface();
}
EGLContext newContext = EGL_NO_CONTEXT;
if (context)
{
ContextEGL *contextEGL = GetImplAs<ContextEGL>(context);
newContext = contextEGL->getContext();
}
if (currentContext.isExternalContext || (context && context->isExternal()))
{
ASSERT(currentContext.surface == EGL_NO_SURFACE);
if (!currentContext.isExternalContext)
{
// Switch to an ANGLE external context.
ASSERT(context);
ASSERT(currentContext.context == EGL_NO_CONTEXT);
currentContext.context = newContext;
currentContext.isExternalContext = true;
// We only support using external surface with external context.
ASSERT(GetImplAs<SurfaceEGL>(drawSurface)->isExternal());
ASSERT(GetImplAs<SurfaceEGL>(drawSurface)->getSurface() == EGL_NO_SURFACE);
}
else if (context)
{
// Switch surface but not context.
ASSERT(currentContext.context == newContext);
ASSERT(newSurface == EGL_NO_SURFACE);
ASSERT(newContext != EGL_NO_CONTEXT);
// We only support using external surface with external context.
ASSERT(GetImplAs<SurfaceEGL>(drawSurface)->isExternal());
ASSERT(GetImplAs<SurfaceEGL>(drawSurface)->getSurface() == EGL_NO_SURFACE);
}
else
{
// Release the ANGLE external context.
ASSERT(newSurface == EGL_NO_SURFACE);
ASSERT(newContext == EGL_NO_CONTEXT);
ASSERT(currentContext.context != EGL_NO_CONTEXT);
currentContext.context = EGL_NO_CONTEXT;
currentContext.isExternalContext = false;
}
// Do not need to call eglMakeCurrent(), since we don't support swtiching EGLSurface for
// external context.
return DisplayGL::makeCurrent(display, drawSurface, readSurface, context);
}
// The context should never change when context virtualization is being used unless binding a
// null context.
if (mVirtualizedContexts && newContext != EGL_NO_CONTEXT)
{
ASSERT(currentContext.context == EGL_NO_CONTEXT || newContext == currentContext.context);
newContext = mRenderer->getContext();
// If we know that we're only running on one thread (mVirtualizedContexts == true) and
// EGL_NO_SURFACE is going to be bound, we can optimize this case by not changing the
// surface binding and emulate the surfaceless extension in the frontend.
if (newSurface == EGL_NO_SURFACE)
{
newSurface = currentContext.surface;
}
// It's possible that no surface has been created yet and the driver doesn't support
// surfaceless, bind the mock pbuffer.
if (newSurface == EGL_NO_SURFACE && !mSupportsSurfaceless)
{
newSurface = mMockPbuffer;
ASSERT(newSurface != EGL_NO_SURFACE);
}
}
if (newSurface != currentContext.surface || newContext != currentContext.context)
{
if (mEGL->makeCurrent(newSurface, newContext) == EGL_FALSE)
{
return egl::Error(mEGL->getError(), "eglMakeCurrent failed");
}
currentContext.surface = newSurface;
currentContext.context = newContext;
}
return DisplayGL::makeCurrent(display, drawSurface, readSurface, context);
}
void DisplayAndroid::destroyNativeContext(EGLContext context)
{
DisplayEGL::destroyNativeContext(context);
}
void DisplayAndroid::generateExtensions(egl::DisplayExtensions *outExtensions) const
{
DisplayEGL::generateExtensions(outExtensions);
// Surfaceless can be support if the native driver supports it or we know that we are running on
// a single thread (mVirtualizedContexts == true)
outExtensions->surfacelessContext = mSupportsSurfaceless || mVirtualizedContexts;
outExtensions->externalContextAndSurface = true;
}
egl::Error DisplayAndroid::createRenderer(EGLContext shareContext,
bool makeNewContextCurrent,
bool isExternalContext,
std::shared_ptr<RendererEGL> *outRenderer)
{
EGLContext context = EGL_NO_CONTEXT;
native_egl::AttributeVector attribs;
// If isExternalContext is true, the external context is current, so we don't need to make the
// mMockPbuffer current.
if (isExternalContext)
{
ASSERT(shareContext == EGL_NO_CONTEXT);
ASSERT(!makeNewContextCurrent);
// Should we consider creating a share context to avoid querying and restoring GL context
// state?
context = mEGL->getCurrentContext();
ASSERT(context != EGL_NO_CONTEXT);
// TODO: get the version from the current context.
attribs = {EGL_CONTEXT_MAJOR_VERSION, 2, EGL_CONTEXT_MINOR_VERSION, 0, EGL_NONE};
}
else
{
ANGLE_TRY(initializeContext(shareContext, mDisplayAttributes, &context, &attribs));
if (mEGL->makeCurrent(mMockPbuffer, context) == EGL_FALSE)
{
return egl::EglNotInitialized()
<< "eglMakeCurrent failed with " << egl::Error(mEGL->getError());
}
}
std::unique_ptr<FunctionsGL> functionsGL(mEGL->makeFunctionsGL());
functionsGL->initialize(mDisplayAttributes);
outRenderer->reset(new RendererEGL(std::move(functionsGL), mDisplayAttributes, this, context,
attribs, isExternalContext));
CurrentNativeContext ¤tContext = mCurrentNativeContexts[std::this_thread::get_id()];
if (makeNewContextCurrent)
{
currentContext.surface = mMockPbuffer;
currentContext.context = context;
}
else if (!isExternalContext)
{
// Reset the current context back to the previous state
if (mEGL->makeCurrent(currentContext.surface, currentContext.context) == EGL_FALSE)
{
return egl::EglNotInitialized()
<< "eglMakeCurrent failed with " << egl::Error(mEGL->getError());
}
}
return egl::NoError();
}
class WorkerContextAndroid final : public WorkerContext
{
public:
WorkerContextAndroid(EGLContext context, FunctionsEGL *functions, EGLSurface pbuffer);
~WorkerContextAndroid() override;
bool makeCurrent() override;
void unmakeCurrent() override;
private:
EGLContext mContext;
FunctionsEGL *mFunctions;
EGLSurface mPbuffer;
};
WorkerContextAndroid::WorkerContextAndroid(EGLContext context,
FunctionsEGL *functions,
EGLSurface pbuffer)
: mContext(context), mFunctions(functions), mPbuffer(pbuffer)
{}
WorkerContextAndroid::~WorkerContextAndroid()
{
mFunctions->destroyContext(mContext);
}
bool WorkerContextAndroid::makeCurrent()
{
if (mFunctions->makeCurrent(mPbuffer, mContext) == EGL_FALSE)
{
ERR() << "Unable to make the EGL context current.";
return false;
}
return true;
}
void WorkerContextAndroid::unmakeCurrent()
{
mFunctions->makeCurrent(EGL_NO_SURFACE, EGL_NO_CONTEXT);
}
WorkerContext *DisplayAndroid::createWorkerContext(std::string *infoLog,
EGLContext sharedContext,
const native_egl::AttributeVector workerAttribs)
{
EGLContext context = mEGL->createContext(mConfig, sharedContext, workerAttribs.data());
if (context == EGL_NO_CONTEXT)
{
*infoLog += "Unable to create the EGL context.";
return nullptr;
}
return new WorkerContextAndroid(context, mEGL, mMockPbuffer);
}
} // namespace rx