John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //#define LOG_NDEBUG 0 |
Ana Krulec | 70d15b1b | 2020-12-01 10:05:15 -0800 | [diff] [blame] | 18 | #undef LOG_TAG |
| 19 | #define LOG_TAG "RenderEngine" |
| 20 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 21 | |
Alec Mouri | 4ce5ec0 | 2021-01-07 17:33:21 -0800 | [diff] [blame] | 22 | #include "SkiaGLRenderEngine.h" |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 23 | |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 24 | #include <EGL/egl.h> |
| 25 | #include <EGL/eglext.h> |
Ana Krulec | 1d12b3b | 2021-01-27 16:49:51 -0800 | [diff] [blame] | 26 | #include <android-base/stringprintf.h> |
Vishnu Nair | 40d8001 | 2024-07-13 23:25:06 +0000 | [diff] [blame] | 27 | #include <common/trace.h> |
Nolan Scobie | bc3f360 | 2024-08-30 13:51:37 -0400 | [diff] [blame] | 28 | #include <include/gpu/ganesh/GrContextOptions.h> |
| 29 | #include <include/gpu/ganesh/GrTypes.h> |
Kevin Lubick | d19875f | 2023-09-27 15:02:47 +0000 | [diff] [blame] | 30 | #include <include/gpu/ganesh/gl/GrGLDirectContext.h> |
Nolan Scobie | bc3f360 | 2024-08-30 13:51:37 -0400 | [diff] [blame] | 31 | #include <include/gpu/ganesh/gl/GrGLInterface.h> |
| 32 | #include <log/log_main.h> |
Alec Mouri | 4ce5ec0 | 2021-01-07 17:33:21 -0800 | [diff] [blame] | 33 | #include <sync/sync.h> |
Ana Krulec | 1d12b3b | 2021-01-27 16:49:51 -0800 | [diff] [blame] | 34 | #include <ui/DebugUtils.h> |
Alec Mouri | b577745 | 2020-09-28 11:32:42 -0700 | [diff] [blame] | 35 | |
| 36 | #include <cmath> |
Alec Mouri | 4ce5ec0 | 2021-01-07 17:33:21 -0800 | [diff] [blame] | 37 | #include <cstdint> |
| 38 | #include <memory> |
Alec Mouri | cdf6cbc | 2021-11-01 17:21:15 -0700 | [diff] [blame] | 39 | #include <numeric> |
Alec Mouri | 4ce5ec0 | 2021-01-07 17:33:21 -0800 | [diff] [blame] | 40 | |
Alec Mouri | e2b61c6 | 2023-08-15 19:04:54 +0000 | [diff] [blame] | 41 | #include "GLExtensions.h" |
Nolan Scobie | bc3f360 | 2024-08-30 13:51:37 -0400 | [diff] [blame] | 42 | #include "compat/SkiaGpuContext.h" |
Nathaniel Nifong | b9f27ef | 2021-04-01 16:44:12 -0400 | [diff] [blame] | 43 | |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 44 | namespace android { |
| 45 | namespace renderengine { |
| 46 | namespace skia { |
| 47 | |
Ana Krulec | 1d12b3b | 2021-01-27 16:49:51 -0800 | [diff] [blame] | 48 | using base::StringAppendF; |
| 49 | |
Alec Mouri | e2b61c6 | 2023-08-15 19:04:54 +0000 | [diff] [blame] | 50 | static bool checkGlError(const char* op, int lineNumber) { |
| 51 | bool errorFound = false; |
| 52 | GLint error = glGetError(); |
| 53 | while (error != GL_NO_ERROR) { |
| 54 | errorFound = true; |
| 55 | error = glGetError(); |
| 56 | ALOGV("after %s() (line # %d) glError (0x%x)\n", op, lineNumber, error); |
| 57 | } |
| 58 | return errorFound; |
| 59 | } |
| 60 | |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 61 | static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute, |
| 62 | EGLint wanted, EGLConfig* outConfig) { |
| 63 | EGLint numConfigs = -1, n = 0; |
| 64 | eglGetConfigs(dpy, nullptr, 0, &numConfigs); |
| 65 | std::vector<EGLConfig> configs(numConfigs, EGL_NO_CONFIG_KHR); |
| 66 | eglChooseConfig(dpy, attrs, configs.data(), configs.size(), &n); |
| 67 | configs.resize(n); |
| 68 | |
| 69 | if (!configs.empty()) { |
| 70 | if (attribute != EGL_NONE) { |
| 71 | for (EGLConfig config : configs) { |
| 72 | EGLint value = 0; |
| 73 | eglGetConfigAttrib(dpy, config, attribute, &value); |
| 74 | if (wanted == value) { |
| 75 | *outConfig = config; |
| 76 | return NO_ERROR; |
| 77 | } |
| 78 | } |
| 79 | } else { |
| 80 | // just pick the first one |
| 81 | *outConfig = configs[0]; |
| 82 | return NO_ERROR; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return NAME_NOT_FOUND; |
| 87 | } |
| 88 | |
| 89 | static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType, |
| 90 | EGLConfig* config) { |
| 91 | // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if |
| 92 | // it is to be used with WIFI displays |
| 93 | status_t err; |
| 94 | EGLint wantedAttribute; |
| 95 | EGLint wantedAttributeValue; |
| 96 | |
| 97 | std::vector<EGLint> attribs; |
| 98 | if (renderableType) { |
| 99 | const ui::PixelFormat pixelFormat = static_cast<ui::PixelFormat>(format); |
| 100 | const bool is1010102 = pixelFormat == ui::PixelFormat::RGBA_1010102; |
| 101 | |
| 102 | // Default to 8 bits per channel. |
| 103 | const EGLint tmpAttribs[] = { |
| 104 | EGL_RENDERABLE_TYPE, |
| 105 | renderableType, |
| 106 | EGL_RECORDABLE_ANDROID, |
| 107 | EGL_TRUE, |
| 108 | EGL_SURFACE_TYPE, |
| 109 | EGL_WINDOW_BIT | EGL_PBUFFER_BIT, |
| 110 | EGL_FRAMEBUFFER_TARGET_ANDROID, |
| 111 | EGL_TRUE, |
| 112 | EGL_RED_SIZE, |
| 113 | is1010102 ? 10 : 8, |
| 114 | EGL_GREEN_SIZE, |
| 115 | is1010102 ? 10 : 8, |
| 116 | EGL_BLUE_SIZE, |
| 117 | is1010102 ? 10 : 8, |
| 118 | EGL_ALPHA_SIZE, |
| 119 | is1010102 ? 2 : 8, |
| 120 | EGL_NONE, |
| 121 | }; |
| 122 | std::copy(tmpAttribs, tmpAttribs + (sizeof(tmpAttribs) / sizeof(EGLint)), |
| 123 | std::back_inserter(attribs)); |
| 124 | wantedAttribute = EGL_NONE; |
| 125 | wantedAttributeValue = EGL_NONE; |
| 126 | } else { |
| 127 | // if no renderable type specified, fallback to a simplified query |
| 128 | wantedAttribute = EGL_NATIVE_VISUAL_ID; |
| 129 | wantedAttributeValue = format; |
| 130 | } |
| 131 | |
| 132 | err = selectConfigForAttribute(display, attribs.data(), wantedAttribute, wantedAttributeValue, |
| 133 | config); |
| 134 | if (err == NO_ERROR) { |
| 135 | EGLint caveat; |
| 136 | if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat)) |
| 137 | ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!"); |
| 138 | } |
| 139 | |
| 140 | return err; |
| 141 | } |
| 142 | |
| 143 | std::unique_ptr<SkiaGLRenderEngine> SkiaGLRenderEngine::create( |
| 144 | const RenderEngineCreationArgs& args) { |
| 145 | // initialize EGL for the default display |
| 146 | EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 147 | if (!eglInitialize(display, nullptr, nullptr)) { |
| 148 | LOG_ALWAYS_FATAL("failed to initialize EGL"); |
| 149 | } |
| 150 | |
Yiwei Zhang | e265096 | 2020-12-01 23:27:58 +0000 | [diff] [blame] | 151 | const auto eglVersion = eglQueryString(display, EGL_VERSION); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 152 | if (!eglVersion) { |
| 153 | checkGlError(__FUNCTION__, __LINE__); |
Yiwei Zhang | e265096 | 2020-12-01 23:27:58 +0000 | [diff] [blame] | 154 | LOG_ALWAYS_FATAL("eglQueryString(EGL_VERSION) failed"); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Yiwei Zhang | e265096 | 2020-12-01 23:27:58 +0000 | [diff] [blame] | 157 | const auto eglExtensions = eglQueryString(display, EGL_EXTENSIONS); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 158 | if (!eglExtensions) { |
| 159 | checkGlError(__FUNCTION__, __LINE__); |
Yiwei Zhang | e265096 | 2020-12-01 23:27:58 +0000 | [diff] [blame] | 160 | LOG_ALWAYS_FATAL("eglQueryString(EGL_EXTENSIONS) failed"); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Alec Mouri | e2b61c6 | 2023-08-15 19:04:54 +0000 | [diff] [blame] | 163 | auto& extensions = GLExtensions::getInstance(); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 164 | extensions.initWithEGLStrings(eglVersion, eglExtensions); |
| 165 | |
| 166 | // The code assumes that ES2 or later is available if this extension is |
| 167 | // supported. |
| 168 | EGLConfig config = EGL_NO_CONFIG_KHR; |
| 169 | if (!extensions.hasNoConfigContext()) { |
| 170 | config = chooseEglConfig(display, args.pixelFormat, /*logConfig*/ true); |
| 171 | } |
| 172 | |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 173 | EGLContext protectedContext = EGL_NO_CONTEXT; |
Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 174 | const std::optional<RenderEngine::ContextPriority> priority = createContextPriority(args); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 175 | if (args.enableProtectedContext && extensions.hasProtectedContent()) { |
Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 176 | protectedContext = |
| 177 | createEglContext(display, config, nullptr, priority, Protection::PROTECTED); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 178 | ALOGE_IF(protectedContext == EGL_NO_CONTEXT, "Can't create protected context"); |
| 179 | } |
| 180 | |
Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 181 | EGLContext ctxt = |
| 182 | createEglContext(display, config, protectedContext, priority, Protection::UNPROTECTED); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 183 | |
| 184 | // if can't create a GL context, we can only abort. |
| 185 | LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed"); |
| 186 | |
| 187 | EGLSurface placeholder = EGL_NO_SURFACE; |
| 188 | if (!extensions.hasSurfacelessContext()) { |
| 189 | placeholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat, |
| 190 | Protection::UNPROTECTED); |
| 191 | LOG_ALWAYS_FATAL_IF(placeholder == EGL_NO_SURFACE, "can't create placeholder pbuffer"); |
| 192 | } |
| 193 | EGLBoolean success = eglMakeCurrent(display, placeholder, placeholder, ctxt); |
| 194 | LOG_ALWAYS_FATAL_IF(!success, "can't make placeholder pbuffer current"); |
| 195 | extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER), |
| 196 | glGetString(GL_VERSION), glGetString(GL_EXTENSIONS)); |
| 197 | |
| 198 | EGLSurface protectedPlaceholder = EGL_NO_SURFACE; |
| 199 | if (protectedContext != EGL_NO_CONTEXT && !extensions.hasSurfacelessContext()) { |
| 200 | protectedPlaceholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat, |
| 201 | Protection::PROTECTED); |
| 202 | ALOGE_IF(protectedPlaceholder == EGL_NO_SURFACE, |
| 203 | "can't create protected placeholder pbuffer"); |
| 204 | } |
| 205 | |
| 206 | // initialize the renderer while GL is current |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 207 | std::unique_ptr<SkiaGLRenderEngine> engine(new SkiaGLRenderEngine(args, display, ctxt, |
| 208 | placeholder, protectedContext, |
| 209 | protectedPlaceholder)); |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 210 | engine->ensureContextsCreated(); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 211 | |
| 212 | ALOGI("OpenGL ES informations:"); |
| 213 | ALOGI("vendor : %s", extensions.getVendor()); |
| 214 | ALOGI("renderer : %s", extensions.getRenderer()); |
| 215 | ALOGI("version : %s", extensions.getVersion()); |
| 216 | ALOGI("extensions: %s", extensions.getExtensions()); |
| 217 | ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize()); |
| 218 | ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims()); |
| 219 | |
| 220 | return engine; |
| 221 | } |
| 222 | |
| 223 | EGLConfig SkiaGLRenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) { |
| 224 | status_t err; |
| 225 | EGLConfig config; |
| 226 | |
| 227 | // First try to get an ES3 config |
| 228 | err = selectEGLConfig(display, format, EGL_OPENGL_ES3_BIT, &config); |
| 229 | if (err != NO_ERROR) { |
| 230 | // If ES3 fails, try to get an ES2 config |
| 231 | err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config); |
| 232 | if (err != NO_ERROR) { |
| 233 | // If ES2 still doesn't work, probably because we're on the emulator. |
| 234 | // try a simplified query |
| 235 | ALOGW("no suitable EGLConfig found, trying a simpler query"); |
| 236 | err = selectEGLConfig(display, format, 0, &config); |
| 237 | if (err != NO_ERROR) { |
| 238 | // this EGL is too lame for android |
Nolan Scobie | efa4f04 | 2023-12-14 13:24:57 -0500 | [diff] [blame] | 239 | LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up" |
| 240 | " (format: %d, vendor: %s, version: %s, extensions: %s, Client" |
| 241 | " API: %s)", |
| 242 | format, eglQueryString(display, EGL_VENDOR), |
| 243 | eglQueryString(display, EGL_VERSION), |
| 244 | eglQueryString(display, EGL_EXTENSIONS), |
| 245 | eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported"); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | if (logConfig) { |
| 251 | // print some debugging info |
| 252 | EGLint r, g, b, a; |
| 253 | eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r); |
| 254 | eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g); |
| 255 | eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b); |
| 256 | eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a); |
| 257 | ALOGI("EGL information:"); |
| 258 | ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR)); |
| 259 | ALOGI("version : %s", eglQueryString(display, EGL_VERSION)); |
| 260 | ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS)); |
| 261 | ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported"); |
| 262 | ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config); |
| 263 | } |
| 264 | |
| 265 | return config; |
| 266 | } |
| 267 | |
Lucas Dupin | f4cb4a0 | 2020-09-22 14:19:26 -0700 | [diff] [blame] | 268 | SkiaGLRenderEngine::SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display, |
Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 269 | EGLContext ctxt, EGLSurface placeholder, |
Lucas Dupin | f4cb4a0 | 2020-09-22 14:19:26 -0700 | [diff] [blame] | 270 | EGLContext protectedContext, EGLSurface protectedPlaceholder) |
Leon Scroggins III | 696bf93 | 2024-01-24 15:21:05 -0500 | [diff] [blame] | 271 | : SkiaRenderEngine(args.threaded, static_cast<PixelFormat>(args.pixelFormat), |
Robin Lee | 7338bd9 | 2024-04-04 14:05:07 +0000 | [diff] [blame] | 272 | args.blurAlgorithm), |
Alec Mouri | 0d99510 | 2021-02-24 16:53:38 -0800 | [diff] [blame] | 273 | mEGLDisplay(display), |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 274 | mEGLContext(ctxt), |
| 275 | mPlaceholderSurface(placeholder), |
| 276 | mProtectedEGLContext(protectedContext), |
Alec Mouri | 47bcb07 | 2023-08-15 02:02:49 +0000 | [diff] [blame] | 277 | mProtectedPlaceholderSurface(protectedPlaceholder) {} |
Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 278 | |
| 279 | SkiaGLRenderEngine::~SkiaGLRenderEngine() { |
Nolan Scobie | 2526b2f | 2024-04-16 15:12:22 -0400 | [diff] [blame] | 280 | finishRenderingAndAbandonContexts(); |
Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 281 | if (mPlaceholderSurface != EGL_NO_SURFACE) { |
| 282 | eglDestroySurface(mEGLDisplay, mPlaceholderSurface); |
| 283 | } |
| 284 | if (mProtectedPlaceholderSurface != EGL_NO_SURFACE) { |
| 285 | eglDestroySurface(mEGLDisplay, mProtectedPlaceholderSurface); |
| 286 | } |
| 287 | if (mEGLContext != EGL_NO_CONTEXT) { |
| 288 | eglDestroyContext(mEGLDisplay, mEGLContext); |
| 289 | } |
| 290 | if (mProtectedEGLContext != EGL_NO_CONTEXT) { |
| 291 | eglDestroyContext(mEGLDisplay, mProtectedEGLContext); |
| 292 | } |
| 293 | eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 294 | eglTerminate(mEGLDisplay); |
| 295 | eglReleaseThread(); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 298 | SkiaRenderEngine::Contexts SkiaGLRenderEngine::createContexts() { |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 299 | LOG_ALWAYS_FATAL_IF(isProtected(), |
| 300 | "Cannot setup contexts while already in protected mode"); |
| 301 | |
| 302 | sk_sp<const GrGLInterface> glInterface = GrGLMakeNativeInterface(); |
| 303 | |
| 304 | LOG_ALWAYS_FATAL_IF(!glInterface.get(), "GrGLMakeNativeInterface() failed"); |
| 305 | |
| 306 | SkiaRenderEngine::Contexts contexts; |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 307 | contexts.first = SkiaGpuContext::MakeGL_Ganesh(glInterface, mSkSLCacheMonitor); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 308 | if (supportsProtectedContentImpl()) { |
| 309 | useProtectedContextImpl(GrProtected::kYes); |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 310 | contexts.second = SkiaGpuContext::MakeGL_Ganesh(glInterface, mSkSLCacheMonitor); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 311 | useProtectedContextImpl(GrProtected::kNo); |
| 312 | } |
| 313 | |
| 314 | return contexts; |
| 315 | } |
| 316 | |
| 317 | bool SkiaGLRenderEngine::supportsProtectedContentImpl() const { |
Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 318 | return mProtectedEGLContext != EGL_NO_CONTEXT; |
| 319 | } |
| 320 | |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 321 | bool SkiaGLRenderEngine::useProtectedContextImpl(GrProtected isProtected) { |
Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 322 | const EGLSurface surface = |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 323 | (isProtected == GrProtected::kYes) ? |
| 324 | mProtectedPlaceholderSurface : mPlaceholderSurface; |
| 325 | const EGLContext context = (isProtected == GrProtected::kYes) ? |
| 326 | mProtectedEGLContext : mEGLContext; |
Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 327 | |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 328 | return eglMakeCurrent(mEGLDisplay, surface, surface, context) == EGL_TRUE; |
Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 331 | void SkiaGLRenderEngine::waitFence(SkiaGpuContext*, base::borrowed_fd fenceFd) { |
Alec Mouri | 4ee7b49 | 2021-08-11 10:36:55 -0700 | [diff] [blame] | 332 | if (fenceFd.get() >= 0 && !waitGpuFence(fenceFd)) { |
Vishnu Nair | 40d8001 | 2024-07-13 23:25:06 +0000 | [diff] [blame] | 333 | SFTRACE_NAME("SkiaGLRenderEngine::waitFence"); |
Alec Mouri | 4ee7b49 | 2021-08-11 10:36:55 -0700 | [diff] [blame] | 334 | sync_wait(fenceFd.get(), -1); |
| 335 | } |
| 336 | } |
| 337 | |
Nolan Scobie | 1e06f2d | 2024-03-21 14:56:38 -0400 | [diff] [blame] | 338 | base::unique_fd SkiaGLRenderEngine::flushAndSubmit(SkiaGpuContext* context, |
| 339 | sk_sp<SkSurface> dstSurface) { |
| 340 | sk_sp<GrDirectContext> grContext = context->grDirectContext(); |
| 341 | { |
Vishnu Nair | 40d8001 | 2024-07-13 23:25:06 +0000 | [diff] [blame] | 342 | SFTRACE_NAME("flush surface"); |
Nolan Scobie | 1e06f2d | 2024-03-21 14:56:38 -0400 | [diff] [blame] | 343 | grContext->flush(dstSurface.get()); |
| 344 | } |
| 345 | base::unique_fd drawFence = flushGL(); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 346 | |
| 347 | bool requireSync = drawFence.get() < 0; |
| 348 | if (requireSync) { |
Vishnu Nair | 40d8001 | 2024-07-13 23:25:06 +0000 | [diff] [blame] | 349 | SFTRACE_BEGIN("Submit(sync=true)"); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 350 | } else { |
Vishnu Nair | 40d8001 | 2024-07-13 23:25:06 +0000 | [diff] [blame] | 351 | SFTRACE_BEGIN("Submit(sync=false)"); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 352 | } |
Nolan Scobie | 1e06f2d | 2024-03-21 14:56:38 -0400 | [diff] [blame] | 353 | bool success = grContext->submit(requireSync ? GrSyncCpu::kYes : GrSyncCpu::kNo); |
Vishnu Nair | 40d8001 | 2024-07-13 23:25:06 +0000 | [diff] [blame] | 354 | SFTRACE_END(); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 355 | if (!success) { |
| 356 | ALOGE("Failed to flush RenderEngine commands"); |
| 357 | // Chances are, something illegal happened (Skia's internal GPU object |
| 358 | // doesn't exist, or the context was abandoned). |
| 359 | return drawFence; |
| 360 | } |
| 361 | |
| 362 | return drawFence; |
| 363 | } |
| 364 | |
Alec Mouri | 4ee7b49 | 2021-08-11 10:36:55 -0700 | [diff] [blame] | 365 | bool SkiaGLRenderEngine::waitGpuFence(base::borrowed_fd fenceFd) { |
Alec Mouri | e2b61c6 | 2023-08-15 19:04:54 +0000 | [diff] [blame] | 366 | if (!GLExtensions::getInstance().hasNativeFenceSync() || |
| 367 | !GLExtensions::getInstance().hasWaitSync()) { |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 368 | return false; |
| 369 | } |
| 370 | |
Alec Mouri | 4ee7b49 | 2021-08-11 10:36:55 -0700 | [diff] [blame] | 371 | // Duplicate the fence for passing to eglCreateSyncKHR. |
| 372 | base::unique_fd fenceDup(dup(fenceFd.get())); |
| 373 | if (fenceDup.get() < 0) { |
| 374 | ALOGE("failed to create duplicate fence fd: %d", fenceDup.get()); |
| 375 | return false; |
| 376 | } |
| 377 | |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 378 | // release the fd and transfer the ownership to EGLSync |
Alec Mouri | 4ee7b49 | 2021-08-11 10:36:55 -0700 | [diff] [blame] | 379 | EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceDup.release(), EGL_NONE}; |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 380 | EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs); |
| 381 | if (sync == EGL_NO_SYNC_KHR) { |
| 382 | ALOGE("failed to create EGL native fence sync: %#x", eglGetError()); |
| 383 | return false; |
| 384 | } |
| 385 | |
| 386 | // XXX: The spec draft is inconsistent as to whether this should return an |
| 387 | // EGLint or void. Ignore the return value for now, as it's not strictly |
| 388 | // needed. |
| 389 | eglWaitSyncKHR(mEGLDisplay, sync, 0); |
| 390 | EGLint error = eglGetError(); |
| 391 | eglDestroySyncKHR(mEGLDisplay, sync); |
| 392 | if (error != EGL_SUCCESS) { |
| 393 | ALOGE("failed to wait for EGL native fence sync: %#x", error); |
| 394 | return false; |
| 395 | } |
| 396 | |
| 397 | return true; |
| 398 | } |
| 399 | |
Nolan Scobie | 1e06f2d | 2024-03-21 14:56:38 -0400 | [diff] [blame] | 400 | base::unique_fd SkiaGLRenderEngine::flushGL() { |
Vishnu Nair | 40d8001 | 2024-07-13 23:25:06 +0000 | [diff] [blame] | 401 | SFTRACE_CALL(); |
Alec Mouri | e2b61c6 | 2023-08-15 19:04:54 +0000 | [diff] [blame] | 402 | if (!GLExtensions::getInstance().hasNativeFenceSync()) { |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 403 | return base::unique_fd(); |
John Reck | cdb4ed7 | 2021-02-04 13:39:33 -0500 | [diff] [blame] | 404 | } |
Nader Jawad | 2dfc98b | 2021-04-08 20:35:39 -0700 | [diff] [blame] | 405 | |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 406 | EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr); |
| 407 | if (sync == EGL_NO_SYNC_KHR) { |
| 408 | ALOGW("failed to create EGL native fence sync: %#x", eglGetError()); |
| 409 | return base::unique_fd(); |
Derek Sollenberger | 3f77aa4 | 2021-02-04 11:06:34 -0500 | [diff] [blame] | 410 | } |
| 411 | |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 412 | // native fence fd will not be populated until flush() is done. |
| 413 | glFlush(); |
Derek Sollenberger | 3f77aa4 | 2021-02-04 11:06:34 -0500 | [diff] [blame] | 414 | |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 415 | // get the fence fd |
| 416 | base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync)); |
| 417 | eglDestroySyncKHR(mEGLDisplay, sync); |
| 418 | if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) { |
| 419 | ALOGW("failed to dup EGL native fence sync: %#x", eglGetError()); |
Derek Sollenberger | 7c42bef | 2021-02-23 13:01:39 -0500 | [diff] [blame] | 420 | } |
| 421 | |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 422 | return fenceFd; |
Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 423 | } |
| 424 | |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 425 | EGLContext SkiaGLRenderEngine::createEglContext(EGLDisplay display, EGLConfig config, |
Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 426 | EGLContext shareContext, |
| 427 | std::optional<ContextPriority> contextPriority, |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 428 | Protection protection) { |
| 429 | EGLint renderableType = 0; |
| 430 | if (config == EGL_NO_CONFIG_KHR) { |
| 431 | renderableType = EGL_OPENGL_ES3_BIT; |
| 432 | } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) { |
| 433 | LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE"); |
| 434 | } |
| 435 | EGLint contextClientVersion = 0; |
| 436 | if (renderableType & EGL_OPENGL_ES3_BIT) { |
| 437 | contextClientVersion = 3; |
| 438 | } else if (renderableType & EGL_OPENGL_ES2_BIT) { |
| 439 | contextClientVersion = 2; |
| 440 | } else if (renderableType & EGL_OPENGL_ES_BIT) { |
| 441 | contextClientVersion = 1; |
| 442 | } else { |
| 443 | LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs"); |
| 444 | } |
| 445 | |
| 446 | std::vector<EGLint> contextAttributes; |
| 447 | contextAttributes.reserve(7); |
| 448 | contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION); |
| 449 | contextAttributes.push_back(contextClientVersion); |
Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 450 | if (contextPriority) { |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 451 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG); |
Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 452 | switch (*contextPriority) { |
| 453 | case ContextPriority::REALTIME: |
| 454 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_REALTIME_NV); |
| 455 | break; |
| 456 | case ContextPriority::MEDIUM: |
| 457 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_MEDIUM_IMG); |
| 458 | break; |
| 459 | case ContextPriority::LOW: |
| 460 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LOW_IMG); |
| 461 | break; |
| 462 | case ContextPriority::HIGH: |
| 463 | default: |
| 464 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG); |
| 465 | break; |
| 466 | } |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 467 | } |
| 468 | if (protection == Protection::PROTECTED) { |
| 469 | contextAttributes.push_back(EGL_PROTECTED_CONTENT_EXT); |
| 470 | contextAttributes.push_back(EGL_TRUE); |
| 471 | } |
| 472 | contextAttributes.push_back(EGL_NONE); |
| 473 | |
| 474 | EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data()); |
| 475 | |
| 476 | if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) { |
| 477 | // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus |
| 478 | // EGL_NO_CONTEXT so that we can abort. |
| 479 | if (config != EGL_NO_CONFIG_KHR) { |
| 480 | return context; |
| 481 | } |
| 482 | // If |config| is EGL_NO_CONFIG_KHR, we speculatively try to create GLES 3 context, so we |
| 483 | // should try to fall back to GLES 2. |
| 484 | contextAttributes[1] = 2; |
| 485 | context = eglCreateContext(display, config, shareContext, contextAttributes.data()); |
| 486 | } |
| 487 | |
| 488 | return context; |
| 489 | } |
| 490 | |
Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 491 | std::optional<RenderEngine::ContextPriority> SkiaGLRenderEngine::createContextPriority( |
| 492 | const RenderEngineCreationArgs& args) { |
Alec Mouri | e2b61c6 | 2023-08-15 19:04:54 +0000 | [diff] [blame] | 493 | if (!GLExtensions::getInstance().hasContextPriority()) { |
Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 494 | return std::nullopt; |
| 495 | } |
| 496 | |
| 497 | switch (args.contextPriority) { |
| 498 | case RenderEngine::ContextPriority::REALTIME: |
Alec Mouri | e2b61c6 | 2023-08-15 19:04:54 +0000 | [diff] [blame] | 499 | if (GLExtensions::getInstance().hasRealtimePriority()) { |
Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 500 | return RenderEngine::ContextPriority::REALTIME; |
| 501 | } else { |
| 502 | ALOGI("Realtime priority unsupported, degrading gracefully to high priority"); |
| 503 | return RenderEngine::ContextPriority::HIGH; |
| 504 | } |
| 505 | case RenderEngine::ContextPriority::HIGH: |
| 506 | case RenderEngine::ContextPriority::MEDIUM: |
| 507 | case RenderEngine::ContextPriority::LOW: |
| 508 | return args.contextPriority; |
| 509 | default: |
| 510 | return std::nullopt; |
| 511 | } |
| 512 | } |
| 513 | |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 514 | EGLSurface SkiaGLRenderEngine::createPlaceholderEglPbufferSurface(EGLDisplay display, |
| 515 | EGLConfig config, int hwcFormat, |
| 516 | Protection protection) { |
| 517 | EGLConfig placeholderConfig = config; |
| 518 | if (placeholderConfig == EGL_NO_CONFIG_KHR) { |
| 519 | placeholderConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true); |
| 520 | } |
| 521 | std::vector<EGLint> attributes; |
| 522 | attributes.reserve(7); |
| 523 | attributes.push_back(EGL_WIDTH); |
| 524 | attributes.push_back(1); |
| 525 | attributes.push_back(EGL_HEIGHT); |
| 526 | attributes.push_back(1); |
| 527 | if (protection == Protection::PROTECTED) { |
| 528 | attributes.push_back(EGL_PROTECTED_CONTENT_EXT); |
| 529 | attributes.push_back(EGL_TRUE); |
| 530 | } |
| 531 | attributes.push_back(EGL_NONE); |
| 532 | |
| 533 | return eglCreatePbufferSurface(display, placeholderConfig, attributes.data()); |
| 534 | } |
| 535 | |
Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 536 | int SkiaGLRenderEngine::getContextPriority() { |
| 537 | int value; |
| 538 | eglQueryContext(mEGLDisplay, mEGLContext, EGL_CONTEXT_PRIORITY_LEVEL_IMG, &value); |
| 539 | return value; |
| 540 | } |
| 541 | |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 542 | void SkiaGLRenderEngine::appendBackendSpecificInfoToDump(std::string& result) { |
Alec Mouri | e2b61c6 | 2023-08-15 19:04:54 +0000 | [diff] [blame] | 543 | const GLExtensions& extensions = GLExtensions::getInstance(); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 544 | StringAppendF(&result, "\n ------------RE GLES------------\n"); |
Ana Krulec | 1d12b3b | 2021-01-27 16:49:51 -0800 | [diff] [blame] | 545 | StringAppendF(&result, "EGL implementation : %s\n", extensions.getEGLVersion()); |
| 546 | StringAppendF(&result, "%s\n", extensions.getEGLExtensions()); |
| 547 | StringAppendF(&result, "GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(), |
| 548 | extensions.getVersion()); |
| 549 | StringAppendF(&result, "%s\n", extensions.getExtensions()); |
Ana Krulec | 1d12b3b | 2021-01-27 16:49:51 -0800 | [diff] [blame] | 550 | } |
| 551 | |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 552 | } // namespace skia |
| 553 | } // namespace renderengine |
Galia Peycheva | 6c46065 | 2020-11-03 19:42:42 +0100 | [diff] [blame] | 554 | } // namespace android |