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 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
|
/*
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
#include "egl_display.h"
#include <SurfaceFlingerProperties.h>
#include <android-base/properties.h>
#include <android/dlext.h>
#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
#include <configstore/Utils.h>
#include <dlfcn.h>
#include <graphicsenv/GraphicsEnv.h>
#include "../egl_impl.h"
#include "EGL/eglext_angle.h"
#include "Loader.h"
#include "egl_angle_platform.h"
#include "egl_cache.h"
#include "egl_object.h"
#include "egl_tls.h"
#include "private/EGL/display.h"
using namespace android::hardware::configstore;
using namespace android::hardware::configstore::V1_0;
namespace android {
static const char* const sVendorString = "Android";
static const char* const sVersionString14 = "1.4 Android META-EGL";
static const char* const sVersionString15 = "1.5 Android META-EGL";
static const char* const sClientApiString = "OpenGL_ES";
extern const char* const gBuiltinExtensionString;
extern const char* const gExtensionString;
extern void setGLHooksThreadSpecific(gl_hooks_t const* value);
bool findExtension(const char* exts, const char* name, size_t nameLen) {
if (exts) {
if (!nameLen) {
nameLen = strlen(name);
}
for (const char* match = strstr(exts, name); match; match = strstr(match + nameLen, name)) {
if (match[nameLen] == '\0' || match[nameLen] == ' ') {
return true;
}
}
}
return false;
}
bool needsAndroidPEglMitigation() {
static const int32_t vndk_version = base::GetIntProperty("ro.vndk.version", -1);
return vndk_version <= 28;
}
int egl_get_init_count(EGLDisplay dpy) {
egl_display_t* eglDisplay = egl_display_t::get(dpy);
return eglDisplay ? eglDisplay->getRefsCount() : 0;
}
std::map<EGLDisplay, std::unique_ptr<egl_display_t>> egl_display_t::displayMap;
std::mutex egl_display_t::displayMapLock;
egl_display_t::egl_display_t()
: magic('_dpy'),
finishOnSwap(false),
traceGpuCompletion(false),
refs(0),
eglIsInitialized(false) {}
egl_display_t::~egl_display_t() {
magic = 0;
egl_cache_t::get()->terminate();
}
egl_display_t* egl_display_t::get(EGLDisplay dpy) {
if (uintptr_t(dpy) == 0) {
return nullptr;
}
const std::lock_guard<std::mutex> lock(displayMapLock);
auto search = displayMap.find(dpy);
if (search == displayMap.end() || !search->second->isValid()) {
return nullptr;
}
return search->second.get();
}
void egl_display_t::addObject(egl_object_t* object) {
std::lock_guard<std::mutex> _l(lock);
objects.insert(object);
}
void egl_display_t::removeObject(egl_object_t* object) {
std::lock_guard<std::mutex> _l(lock);
objects.erase(object);
}
bool egl_display_t::getObject(egl_object_t* object) const {
std::lock_guard<std::mutex> _l(lock);
if (objects.find(object) != objects.end()) {
if (object->getDisplay() == this) {
object->incRef();
return true;
}
}
return false;
}
EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp,
const EGLAttrib* attrib_list) {
if (uintptr_t(disp) >= NUM_DISPLAYS) return nullptr;
return getPlatformDisplay(disp, attrib_list);
}
static EGLDisplay getPlatformDisplayAngle(EGLNativeDisplayType display, egl_connection_t* const cnx,
const EGLAttrib* attrib_list, EGLint* error) {
EGLDisplay dpy = EGL_NO_DISPLAY;
*error = EGL_NONE;
if (cnx->egl.eglGetPlatformDisplay) {
std::vector<EGLAttrib> attrs;
if (attrib_list) {
for (const EGLAttrib* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
attrs.push_back(attr[0]);
attrs.push_back(attr[1]);
}
}
const auto& eglFeatures = GraphicsEnv::getInstance().getAngleEglFeatures();
std::vector<const char*> features;
if (eglFeatures.size() > 0) {
for (const std::string& eglFeature : eglFeatures) {
features.push_back(eglFeature.c_str());
}
features.push_back(0);
attrs.push_back(EGL_FEATURE_OVERRIDES_ENABLED_ANGLE);
attrs.push_back(reinterpret_cast<EGLAttrib>(features.data()));
}
attrs.push_back(EGL_PLATFORM_ANGLE_TYPE_ANGLE);
attrs.push_back(EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE);
attrs.push_back(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE);
attrs.push_back(base::GetBoolProperty("debug.angle.validation", false));
attrs.push_back(EGL_NONE);
dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANGLE_ANGLE,
reinterpret_cast<void*>(EGL_DEFAULT_DISPLAY),
attrs.data());
if (dpy == EGL_NO_DISPLAY) {
ALOGE("eglGetPlatformDisplay failed!");
} else {
if (!angle::initializeAnglePlatform(dpy)) {
ALOGE("initializeAnglePlatform failed!");
}
}
} else {
ALOGE("eglGetDisplay(%p) failed: Unable to look up eglGetPlatformDisplay from ANGLE",
display);
}
return dpy;
}
EGLDisplay egl_display_t::getPlatformDisplay(EGLNativeDisplayType display,
const EGLAttrib* attrib_list) {
ATRACE_CALL();
// get our driver loader
Loader& loader(Loader::getInstance());
egl_connection_t* const cnx = &gEGLImpl;
if (cnx->dso) {
EGLDisplay dpy = EGL_NO_DISPLAY;
if (cnx->useAngle) {
EGLint error;
dpy = getPlatformDisplayAngle(display, cnx, attrib_list, &error);
if (error != EGL_NONE) {
return setError(error, dpy);
}
}
if (dpy == EGL_NO_DISPLAY) {
// NOTE: eglGetPlatformDisplay with a empty attribute list
// behaves the same as eglGetDisplay
if (cnx->egl.eglGetPlatformDisplay) {
dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANDROID_KHR, display,
attrib_list);
}
// It is possible that eglGetPlatformDisplay does not have a
// working implementation for Android platform; in that case,
// one last fallback to eglGetDisplay
if (dpy == EGL_NO_DISPLAY) {
if (attrib_list) {
ALOGW("getPlatformDisplay: unexpected attribute list, attributes ignored");
}
dpy = cnx->egl.eglGetDisplay(display);
}
}
if (dpy == EGL_NO_DISPLAY) {
loader.close(cnx);
} else {
const std::lock_guard<std::mutex> lock(displayMapLock);
if (displayMap.find(dpy) == displayMap.end()) {
auto d = std::make_unique<egl_display_t>();
d->disp.dpy = dpy;
displayMap[dpy] = std::move(d);
}
return dpy;
}
}
return nullptr;
}
EGLBoolean egl_display_t::initialize(EGLint* major, EGLint* minor) {
{ // scope for refLock
std::unique_lock<std::mutex> _l(refLock);
refs++;
if (refs > 1) {
// We don't know what to report until we know what the
// driver supports. Make sure we are initialized before
// returning the version info.
while (!eglIsInitialized) {
refCond.wait(_l);
}
egl_connection_t* const cnx = &gEGLImpl;
// TODO: If device doesn't provide 1.4 or 1.5 then we'll be
// changing the behavior from the past where we always advertise
// version 1.4. May need to check that revision is valid
// before using cnx->major & cnx->minor
if (major != nullptr) *major = cnx->major;
if (minor != nullptr) *minor = cnx->minor;
return EGL_TRUE;
}
while (eglIsInitialized) {
refCond.wait(_l);
}
}
{ // scope for lock
std::lock_guard<std::mutex> _l(lock);
setGLHooksThreadSpecific(&gHooksNoContext);
// initialize each EGL and
// build our own extension string first, based on the extension we know
// and the extension supported by our client implementation
egl_connection_t* const cnx = &gEGLImpl;
cnx->major = -1;
cnx->minor = -1;
if (cnx->dso) {
EGLDisplay idpy = disp.dpy;
if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
// ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
// idpy, cnx->major, cnx->minor, cnx);
// display is now initialized
disp.state = egl_display_t::INITIALIZED;
// get the query-strings for this display for each implementation
disp.queryString.vendor = cnx->egl.eglQueryString(idpy, EGL_VENDOR);
disp.queryString.version = cnx->egl.eglQueryString(idpy, EGL_VERSION);
disp.queryString.extensions = cnx->egl.eglQueryString(idpy, EGL_EXTENSIONS);
disp.queryString.clientApi = cnx->egl.eglQueryString(idpy, EGL_CLIENT_APIS);
} else {
ALOGW("eglInitialize(%p) failed (%s)", idpy,
egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
}
}
if (cnx->minor == 5) {
// full list in egl_entries.in
if (!cnx->egl.eglCreateImage || !cnx->egl.eglDestroyImage ||
!cnx->egl.eglGetPlatformDisplay || !cnx->egl.eglCreatePlatformWindowSurface ||
!cnx->egl.eglCreatePlatformPixmapSurface || !cnx->egl.eglCreateSync ||
!cnx->egl.eglDestroySync || !cnx->egl.eglClientWaitSync ||
!cnx->egl.eglGetSyncAttrib || !cnx->egl.eglWaitSync) {
ALOGE("Driver indicates EGL 1.5 support, but does not have "
"a critical API");
cnx->minor = 4;
}
}
// the query strings are per-display
mVendorString = sVendorString;
mVersionString.clear();
cnx->driverVersion = EGL_MAKE_VERSION(1, 4, 0);
mVersionString = sVersionString14;
if ((cnx->major == 1) && (cnx->minor == 5)) {
mVersionString = sVersionString15;
cnx->driverVersion = EGL_MAKE_VERSION(1, 5, 0);
}
if (mVersionString.empty()) {
ALOGW("Unexpected driver version: %d.%d, want 1.4 or 1.5", cnx->major, cnx->minor);
mVersionString = sVersionString14;
}
mClientApiString = sClientApiString;
mExtensionString = gBuiltinExtensionString;
// b/269060366 Conditionally enabled EGL_ANDROID_get_frame_timestamps extension if the
// device's present timestamps are reliable (which may not be the case on emulators).
if (cnx->useAngle) {
if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) {
mExtensionString.append("EGL_ANDROID_get_frame_timestamps ");
}
} else {
mExtensionString.append("EGL_ANDROID_get_frame_timestamps ");
}
hasColorSpaceSupport = findExtension(disp.queryString.extensions, "EGL_KHR_gl_colorspace");
// Note: CDD requires that devices supporting wide color and/or HDR color also support
// the EGL_KHR_gl_colorspace extension.
bool wideColorBoardConfig = android::sysprop::has_wide_color_display(false);
// Add wide-color extensions if device can support wide-color
if (wideColorBoardConfig && hasColorSpaceSupport) {
mExtensionString.append(
"EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear "
"EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 "
"EGL_EXT_gl_colorspace_display_p3_passthrough ");
}
bool hasHdrBoardConfig = android::sysprop::has_HDR_display(false);
if (hasHdrBoardConfig && hasColorSpaceSupport) {
// hasHDRBoardConfig indicates the system is capable of supporting HDR content.
// Typically that means there is an HDR capable display attached, but could be
// support for attaching an HDR display. In either case, advertise support for
// HDR color spaces.
mExtensionString.append(
"EGL_EXT_gl_colorspace_bt2020_linear EGL_EXT_gl_colorspace_bt2020_pq ");
}
char const* start = gExtensionString;
do {
// length of the extension name
size_t len = strcspn(start, " ");
if (len) {
// NOTE: we could avoid the copy if we had strnstr.
const std::string ext(start, len);
// Mitigation for Android P vendor partitions: Adreno 530 driver shipped on
// some Android P vendor partitions this extension under the draft KHR name,
// but during Khronos review it was decided to demote it to EXT.
if (needsAndroidPEglMitigation() && ext == "EGL_EXT_image_gl_colorspace" &&
findExtension(disp.queryString.extensions, "EGL_KHR_image_gl_colorspace")) {
mExtensionString.append("EGL_EXT_image_gl_colorspace ");
}
if (findExtension(disp.queryString.extensions, ext.c_str(), len)) {
mExtensionString.append(ext + " ");
}
// advance to the next extension name, skipping the space.
start += len;
start += (*start == ' ') ? 1 : 0;
}
} while (*start != '\0');
egl_cache_t::get()->initialize(this);
finishOnSwap = base::GetBoolProperty("debug.egl.finish", false);
traceGpuCompletion = base::GetBoolProperty("debug.egl.traceGpuCompletion", false);
// TODO: If device doesn't provide 1.4 or 1.5 then we'll be
// changing the behavior from the past where we always advertise
// version 1.4. May need to check that revision is valid
// before using cnx->major & cnx->minor
if (major != nullptr) *major = cnx->major;
if (minor != nullptr) *minor = cnx->minor;
}
{ // scope for refLock
std::unique_lock<std::mutex> _l(refLock);
eglIsInitialized = true;
refCond.notify_all();
}
return EGL_TRUE;
}
EGLBoolean egl_display_t::terminate() {
{ // scope for refLock
std::unique_lock<std::mutex> _rl(refLock);
if (refs == 0) {
/*
* From the EGL spec (3.2):
* "Termination of a display that has already been terminated,
* (...), is allowed, but the only effect of such a call is
* to return EGL_TRUE (...)
*/
return EGL_TRUE;
}
// this is specific to Android, display termination is ref-counted.
refs--;
if (refs > 0) {
return EGL_TRUE;
}
}
EGLBoolean res = EGL_FALSE;
{ // scope for lock
std::lock_guard<std::mutex> _l(lock);
egl_connection_t* const cnx = &gEGLImpl;
if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
// If we're using ANGLE reset any custom DisplayPlatform
if (cnx->useAngle) {
angle::resetAnglePlatform(disp.dpy);
}
if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
}
// REVISIT: it's unclear what to do if eglTerminate() fails
disp.state = egl_display_t::TERMINATED;
res = EGL_TRUE;
}
// Reset the extension string since it will be regenerated if we get
// reinitialized.
mExtensionString.clear();
// Mark all objects remaining in the list as terminated, unless
// there are no reference to them, it which case, we're free to
// delete them.
size_t count = objects.size();
ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
for (auto o : objects) {
o->destroy();
}
// this marks all object handles are "terminated"
objects.clear();
}
{ // scope for refLock
std::unique_lock<std::mutex> _rl(refLock);
eglIsInitialized = false;
refCond.notify_all();
}
return res;
}
void egl_display_t::loseCurrent(egl_context_t* cur_c) {
if (cur_c) {
egl_display_t* display = cur_c->getDisplay();
if (display) {
display->loseCurrentImpl(cur_c);
}
}
}
void egl_display_t::loseCurrentImpl(egl_context_t* cur_c) {
// by construction, these are either 0 or valid (possibly terminated)
// it should be impossible for these to be invalid
ContextRef _cur_c(cur_c);
SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
{ // scope for the lock
std::lock_guard<std::mutex> _l(lock);
cur_c->onLooseCurrent();
}
// This cannot be called with the lock held because it might end-up
// calling back into EGL (in particular when a surface is destroyed
// it calls ANativeWindow::disconnect
_cur_c.release();
_cur_r.release();
_cur_d.release();
}
EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c, EGLSurface draw,
EGLSurface read, EGLContext /*ctx*/, EGLSurface impl_draw,
EGLSurface impl_read, EGLContext impl_ctx) {
EGLBoolean result;
// by construction, these are either 0 or valid (possibly terminated)
// it should be impossible for these to be invalid
ContextRef _cur_c(cur_c);
SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
{ // scope for the lock
std::lock_guard<std::mutex> _l(lock);
if (c) {
result = c->cnx->egl.eglMakeCurrent(disp.dpy, impl_draw, impl_read, impl_ctx);
if (result == EGL_TRUE) {
c->onMakeCurrent(draw, read);
}
} else {
result = cur_c->cnx->egl.eglMakeCurrent(disp.dpy, impl_draw, impl_read, impl_ctx);
if (result == EGL_TRUE) {
cur_c->onLooseCurrent();
}
}
}
if (result == EGL_TRUE) {
// This cannot be called with the lock held because it might end-up
// calling back into EGL (in particular when a surface is destroyed
// it calls ANativeWindow::disconnect
_cur_c.release();
_cur_r.release();
_cur_d.release();
}
return result;
}
bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
if (!nameLen) {
nameLen = strlen(name);
}
return findExtension(mExtensionString.c_str(), name, nameLen);
}
egl_display_t* validate_display(EGLDisplay dpy) {
egl_display_t* const dp = get_display(dpy);
if (!dp) return setError(EGL_BAD_DISPLAY, (egl_display_t*)nullptr);
if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (egl_display_t*)nullptr);
return dp;
}
egl_display_t* validate_display_connection(EGLDisplay dpy, egl_connection_t** outCnx) {
*outCnx = nullptr;
egl_display_t* dp = validate_display(dpy);
if (!dp) return dp;
*outCnx = &gEGLImpl;
if ((*outCnx)->dso == nullptr) {
return setError(EGL_BAD_CONFIG, (egl_display_t*)nullptr);
}
return dp;
}
}; // namespace android
|