blob: cf0af6f4fda17ba099bb9a706ca1e4a383c078bf [file] [log] [blame]
John Reck3b202512014-06-23 13:13:08 -07001/*
2 * Copyright (C) 2014 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
John Reck3b202512014-06-23 13:13:08 -070017#include "EglManager.h"
18
sergeyv694d4992016-10-27 10:23:13 -070019#include "Texture.h"
John Reckd04794a2015-05-08 10:04:36 -070020#include "Caches.h"
John Reck704bed02015-11-05 09:22:17 -080021#include "DeviceInfo.h"
John Reckd04794a2015-05-08 10:04:36 -070022#include "Properties.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080023#include "RenderThread.h"
John Reckd04794a2015-05-08 10:04:36 -070024#include "renderstate/RenderState.h"
Chris Craik6e6646c2015-09-14 15:54:12 -070025#include "utils/StringUtils.h"
John Reck3b202512014-06-23 13:13:08 -070026#include <cutils/log.h>
27#include <cutils/properties.h>
John Reck55156372015-01-21 07:46:37 -080028#include <EGL/eglext.h>
John Reck149173d2015-08-10 09:52:29 -070029#include <string>
30
John Reck3b202512014-06-23 13:13:08 -070031#define GLES_VERSION 2
32
33// Android-specific addition that is used to show when frames began in systrace
34EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface);
35
36namespace android {
37namespace uirenderer {
38namespace renderthread {
39
40#define ERROR_CASE(x) case x: return #x;
41static const char* egl_error_str(EGLint error) {
42 switch (error) {
43 ERROR_CASE(EGL_SUCCESS)
44 ERROR_CASE(EGL_NOT_INITIALIZED)
45 ERROR_CASE(EGL_BAD_ACCESS)
46 ERROR_CASE(EGL_BAD_ALLOC)
47 ERROR_CASE(EGL_BAD_ATTRIBUTE)
48 ERROR_CASE(EGL_BAD_CONFIG)
49 ERROR_CASE(EGL_BAD_CONTEXT)
50 ERROR_CASE(EGL_BAD_CURRENT_SURFACE)
51 ERROR_CASE(EGL_BAD_DISPLAY)
52 ERROR_CASE(EGL_BAD_MATCH)
53 ERROR_CASE(EGL_BAD_NATIVE_PIXMAP)
54 ERROR_CASE(EGL_BAD_NATIVE_WINDOW)
55 ERROR_CASE(EGL_BAD_PARAMETER)
56 ERROR_CASE(EGL_BAD_SURFACE)
57 ERROR_CASE(EGL_CONTEXT_LOST)
58 default:
59 return "Unknown error";
60 }
61}
sergeyv694d4992016-10-27 10:23:13 -070062const char* EglManager::eglErrorString() {
John Reck3b202512014-06-23 13:13:08 -070063 return egl_error_str(eglGetError());
64}
65
John Reck149173d2015-08-10 09:52:29 -070066static struct {
67 bool bufferAge = false;
68 bool setDamage = false;
69} EglExtensions;
70
71void Frame::map(const SkRect& in, EGLint* out) const {
72 /* The rectangles are specified relative to the bottom-left of the surface
73 * and the x and y components of each rectangle specify the bottom-left
74 * position of that rectangle.
75 *
76 * HWUI does everything with 0,0 being top-left, so need to map
77 * the rect
78 */
79 SkIRect idirty;
80 in.roundOut(&idirty);
81 EGLint y = mHeight - (idirty.y() + idirty.height());
82 // layout: {x, y, width, height}
83 out[0] = idirty.x();
84 out[1] = y;
85 out[2] = idirty.width();
86 out[3] = idirty.height();
John Reck3b202512014-06-23 13:13:08 -070087}
88
89EglManager::EglManager(RenderThread& thread)
90 : mRenderThread(thread)
91 , mEglDisplay(EGL_NO_DISPLAY)
Chris Craikd41c4d82015-01-05 15:51:13 -080092 , mEglConfig(nullptr)
John Reck3b202512014-06-23 13:13:08 -070093 , mEglContext(EGL_NO_CONTEXT)
94 , mPBufferSurface(EGL_NO_SURFACE)
Romain Guy253f2c22016-09-28 17:34:42 -070095 , mCurrentSurface(EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070096}
97
98void EglManager::initialize() {
99 if (hasEglContext()) return;
100
John Reckfbc8df02014-11-14 16:18:41 -0800101 ATRACE_NAME("Creating EGLContext");
102
John Reck3b202512014-06-23 13:13:08 -0700103 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
104 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
sergeyv694d4992016-10-27 10:23:13 -0700105 "Failed to get EGL_DEFAULT_DISPLAY! err=%s", eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700106
107 EGLint major, minor;
108 LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE,
sergeyv694d4992016-10-27 10:23:13 -0700109 "Failed to initialize display %p! err=%s", mEglDisplay, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700110
111 ALOGI("Initialized EGL, version %d.%d", (int)major, (int)minor);
112
John Reck149173d2015-08-10 09:52:29 -0700113 initExtensions();
Season Li13d1b4a2015-07-29 17:16:19 -0700114
John Reck149173d2015-08-10 09:52:29 -0700115 // Now that extensions are loaded, pick a swap behavior
116 if (Properties::enablePartialUpdates) {
117 if (Properties::useBufferAge && EglExtensions.bufferAge) {
118 mSwapBehavior = SwapBehavior::BufferAge;
119 } else {
120 mSwapBehavior = SwapBehavior::Preserved;
121 }
122 }
123
124 loadConfig();
John Reck3b202512014-06-23 13:13:08 -0700125 createContext();
John Reckd7db4d72015-05-20 07:18:55 -0700126 createPBufferSurface();
127 makeCurrent(mPBufferSurface);
John Reck704bed02015-11-05 09:22:17 -0800128 DeviceInfo::initialize();
John Reck3b202512014-06-23 13:13:08 -0700129 mRenderThread.renderState().onGLContextCreated();
John Reck3b202512014-06-23 13:13:08 -0700130}
131
John Reck149173d2015-08-10 09:52:29 -0700132void EglManager::initExtensions() {
John Reck704bed02015-11-05 09:22:17 -0800133 auto extensions = StringUtils::split(
134 eglQueryString(mEglDisplay, EGL_EXTENSIONS));
John Reck8733bff2016-09-27 14:45:28 -0700135 // For our purposes we don't care if EGL_BUFFER_AGE is a result of
136 // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered
137 // under EGL_KHR_partial_update and we don't need the expanded scope
138 // that EGL_EXT_buffer_age provides.
139 EglExtensions.bufferAge = extensions.has("EGL_EXT_buffer_age")
140 || extensions.has("EGL_KHR_partial_update");
Chris Craik6e6646c2015-09-14 15:54:12 -0700141 EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update");
John Reck708b6682015-10-22 13:11:00 -0700142 LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"),
143 "Missing required extension EGL_KHR_swap_buffers_with_damage");
John Reck149173d2015-08-10 09:52:29 -0700144}
145
John Reck3b202512014-06-23 13:13:08 -0700146bool EglManager::hasEglContext() {
147 return mEglDisplay != EGL_NO_DISPLAY;
148}
149
John Reck149173d2015-08-10 09:52:29 -0700150void EglManager::loadConfig() {
151 ALOGD("Swap behavior %d", static_cast<int>(mSwapBehavior));
152 EGLint swapBehavior = (mSwapBehavior == SwapBehavior::Preserved)
153 ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
John Reck3b202512014-06-23 13:13:08 -0700154 EGLint attribs[] = {
155 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
156 EGL_RED_SIZE, 8,
157 EGL_GREEN_SIZE, 8,
158 EGL_BLUE_SIZE, 8,
159 EGL_ALPHA_SIZE, 8,
160 EGL_DEPTH_SIZE, 0,
161 EGL_CONFIG_CAVEAT, EGL_NONE,
162 EGL_STENCIL_SIZE, Stencil::getStencilSize(),
163 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | swapBehavior,
164 EGL_NONE
165 };
166
167 EGLint num_configs = 1;
168 if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, num_configs, &num_configs)
169 || num_configs != 1) {
John Reck149173d2015-08-10 09:52:29 -0700170 if (mSwapBehavior == SwapBehavior::Preserved) {
John Reck3b202512014-06-23 13:13:08 -0700171 // Try again without dirty regions enabled
John Reck149173d2015-08-10 09:52:29 -0700172 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
173 mSwapBehavior = SwapBehavior::Discard;
174 loadConfig();
John Reck3b202512014-06-23 13:13:08 -0700175 } else {
John Reck149173d2015-08-10 09:52:29 -0700176 // Failed to get a valid config
sergeyv694d4992016-10-27 10:23:13 -0700177 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700178 }
179 }
180}
181
182void EglManager::createContext() {
John Reck682573c2015-10-30 10:37:35 -0700183 EGLint attribs[] = {
184 EGL_CONTEXT_CLIENT_VERSION, GLES_VERSION,
185 EGL_NONE
186 };
John Reck3b202512014-06-23 13:13:08 -0700187 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, attribs);
188 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT,
sergeyv694d4992016-10-27 10:23:13 -0700189 "Failed to create context, error = %s", eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700190}
191
John Reckd7db4d72015-05-20 07:18:55 -0700192void EglManager::createPBufferSurface() {
John Reck3b202512014-06-23 13:13:08 -0700193 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
194 "usePBufferSurface() called on uninitialized GlobalContext!");
195
196 if (mPBufferSurface == EGL_NO_SURFACE) {
197 EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
198 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs);
199 }
John Reck3b202512014-06-23 13:13:08 -0700200}
201
202EGLSurface EglManager::createSurface(EGLNativeWindowType window) {
203 initialize();
Romain Guy253f2c22016-09-28 17:34:42 -0700204
205 EGLint attribs[] = {
206#ifdef ANDROID_ENABLE_LINEAR_BLENDING
207 EGL_GL_COLORSPACE_KHR, EGL_GL_COLORSPACE_SRGB_KHR,
208 EGL_COLORSPACE, EGL_COLORSPACE_sRGB,
209#endif
210 EGL_NONE
211 };
212
213 EGLSurface surface = eglCreateWindowSurface(mEglDisplay, mEglConfig, window, attribs);
John Reck3b202512014-06-23 13:13:08 -0700214 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE,
215 "Failed to create EGLSurface for window %p, eglErr = %s",
sergeyv694d4992016-10-27 10:23:13 -0700216 (void*) window, eglErrorString());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000217
218 if (mSwapBehavior != SwapBehavior::Preserved) {
219 LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED) == EGL_FALSE,
220 "Failed to set swap behavior to destroyed for window %p, eglErr = %s",
sergeyv694d4992016-10-27 10:23:13 -0700221 (void*) window, eglErrorString());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000222 }
223
John Reck3b202512014-06-23 13:13:08 -0700224 return surface;
225}
226
227void EglManager::destroySurface(EGLSurface surface) {
228 if (isCurrent(surface)) {
229 makeCurrent(EGL_NO_SURFACE);
230 }
231 if (!eglDestroySurface(mEglDisplay, surface)) {
sergeyv694d4992016-10-27 10:23:13 -0700232 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700233 }
234}
235
236void EglManager::destroy() {
237 if (mEglDisplay == EGL_NO_DISPLAY) return;
238
Chris Craik1d477422014-08-26 17:30:15 -0700239 mRenderThread.renderState().onGLContextDestroyed();
John Reck3b202512014-06-23 13:13:08 -0700240 eglDestroyContext(mEglDisplay, mEglContext);
241 eglDestroySurface(mEglDisplay, mPBufferSurface);
242 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
243 eglTerminate(mEglDisplay);
244 eglReleaseThread();
245
246 mEglDisplay = EGL_NO_DISPLAY;
247 mEglContext = EGL_NO_CONTEXT;
248 mPBufferSurface = EGL_NO_SURFACE;
249 mCurrentSurface = EGL_NO_SURFACE;
250}
251
John Reckf2dcc2a2015-07-16 09:17:59 -0700252bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut) {
John Reck3b202512014-06-23 13:13:08 -0700253 if (isCurrent(surface)) return false;
254
255 if (surface == EGL_NO_SURFACE) {
John Reckd7db4d72015-05-20 07:18:55 -0700256 // Ensure we always have a valid surface & context
257 surface = mPBufferSurface;
258 }
259 if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
John Reckf2dcc2a2015-07-16 09:17:59 -0700260 if (errOut) {
261 *errOut = eglGetError();
262 ALOGW("Failed to make current on surface %p, error=%s",
263 (void*)surface, egl_error_str(*errOut));
264 } else {
265 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s",
sergeyv694d4992016-10-27 10:23:13 -0700266 (void*)surface, eglErrorString());
John Reckf2dcc2a2015-07-16 09:17:59 -0700267 }
John Reck3b202512014-06-23 13:13:08 -0700268 }
269 mCurrentSurface = surface;
270 return true;
271}
272
John Reck149173d2015-08-10 09:52:29 -0700273EGLint EglManager::queryBufferAge(EGLSurface surface) {
274 switch (mSwapBehavior) {
275 case SwapBehavior::Discard:
276 return 0;
277 case SwapBehavior::Preserved:
278 return 1;
279 case SwapBehavior::BufferAge:
280 EGLint bufferAge;
281 eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge);
282 return bufferAge;
283 }
284 return 0;
285}
286
287Frame EglManager::beginFrame(EGLSurface surface) {
John Reck3b202512014-06-23 13:13:08 -0700288 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE,
289 "Tried to beginFrame on EGL_NO_SURFACE!");
290 makeCurrent(surface);
John Reck149173d2015-08-10 09:52:29 -0700291 Frame frame;
292 frame.mSurface = surface;
293 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth);
294 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight);
295 frame.mBufferAge = queryBufferAge(surface);
John Reck3b202512014-06-23 13:13:08 -0700296 eglBeginFrame(mEglDisplay, surface);
John Reck149173d2015-08-10 09:52:29 -0700297 return frame;
John Reck3b202512014-06-23 13:13:08 -0700298}
299
John Reck149173d2015-08-10 09:52:29 -0700300void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) {
301#ifdef EGL_KHR_partial_update
302 if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) {
303 EGLint rects[4];
304 frame.map(dirty, rects);
305 if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) {
306 LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s",
sergeyv694d4992016-10-27 10:23:13 -0700307 (void*)frame.mSurface, eglErrorString());
John Reck149173d2015-08-10 09:52:29 -0700308 }
309 }
310#endif
311}
312
John Reckc96955d2016-02-26 14:56:44 -0800313bool EglManager::damageRequiresSwap() {
314 return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge;
315}
316
John Reck149173d2015-08-10 09:52:29 -0700317bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) {
John Reck55156372015-01-21 07:46:37 -0800318
John Reck682573c2015-10-30 10:37:35 -0700319 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
John Reck55156372015-01-21 07:46:37 -0800320 ATRACE_NAME("Finishing GPU work");
321 fence();
322 }
John Reck55156372015-01-21 07:46:37 -0800323
John Recka672f6b2015-10-22 09:53:26 -0700324 EGLint rects[4];
325 frame.map(screenDirty, rects);
326 eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects,
327 screenDirty.isEmpty() ? 0 : 1);
John Reckd04794a2015-05-08 10:04:36 -0700328
John Reck3b202512014-06-23 13:13:08 -0700329 EGLint err = eglGetError();
John Reck2cdbc7d2014-09-17 16:06:36 -0700330 if (CC_LIKELY(err == EGL_SUCCESS)) {
331 return true;
332 }
John Reckc2547fa2015-10-26 13:52:52 -0700333 if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) {
John Reck2cdbc7d2014-09-17 16:06:36 -0700334 // For some reason our surface was destroyed out from under us
335 // This really shouldn't happen, but if it does we can recover easily
336 // by just not trying to use the surface anymore
John Reckd1ddcf12016-02-05 15:59:55 -0800337 ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...",
338 err, frame.mSurface);
John Reck2cdbc7d2014-09-17 16:06:36 -0700339 return false;
340 }
341 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering",
342 err, egl_error_str(err));
343 // Impossible to hit this, but the compiler doesn't know that
344 return false;
John Reck3b202512014-06-23 13:13:08 -0700345}
346
John Reck55156372015-01-21 07:46:37 -0800347void EglManager::fence() {
348 EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL);
349 eglClientWaitSyncKHR(mEglDisplay, fence,
350 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
351 eglDestroySyncKHR(mEglDisplay, fence);
352}
353
John Reck1125d1f2014-10-23 11:02:19 -0700354bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
John Reck149173d2015-08-10 09:52:29 -0700355 if (mSwapBehavior != SwapBehavior::Preserved) return false;
John Reck3b202512014-06-23 13:13:08 -0700356
John Reck149173d2015-08-10 09:52:29 -0700357 bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
358 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED);
359 if (!preserved) {
360 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s",
sergeyv694d4992016-10-27 10:23:13 -0700361 (void*) surface, eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700362 // Maybe it's already set?
363 EGLint swapBehavior;
364 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) {
365 preserved = (swapBehavior == EGL_BUFFER_PRESERVED);
366 } else {
367 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p",
sergeyv694d4992016-10-27 10:23:13 -0700368 (void*) surface, eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700369 }
John Reck3b202512014-06-23 13:13:08 -0700370 }
John Reck1125d1f2014-10-23 11:02:19 -0700371
372 return preserved;
John Reck3b202512014-06-23 13:13:08 -0700373}
374
John Reck3b202512014-06-23 13:13:08 -0700375} /* namespace renderthread */
376} /* namespace uirenderer */
377} /* namespace android */