blob: 99bc9a75e94bd065e8ecd036a5a43330afd59283 [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
Mark Salyzyn96bf5982016-09-28 16:15:30 -070019#include <string>
20
21#include "utils/StringUtils.h"
22#include <cutils/properties.h>
23#include <log/log.h>
24
John Reckd04794a2015-05-08 10:04:36 -070025#include "Caches.h"
John Reck704bed02015-11-05 09:22:17 -080026#include "DeviceInfo.h"
John Reckd04794a2015-05-08 10:04:36 -070027#include "Properties.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080028#include "RenderThread.h"
John Reckd04794a2015-05-08 10:04:36 -070029#include "renderstate/RenderState.h"
John Reck55156372015-01-21 07:46:37 -080030#include <EGL/eglext.h>
John Reck149173d2015-08-10 09:52:29 -070031
John Reck3b202512014-06-23 13:13:08 -070032#define GLES_VERSION 2
33
34// Android-specific addition that is used to show when frames began in systrace
35EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface);
36
37namespace android {
38namespace uirenderer {
39namespace renderthread {
40
41#define ERROR_CASE(x) case x: return #x;
42static const char* egl_error_str(EGLint error) {
43 switch (error) {
44 ERROR_CASE(EGL_SUCCESS)
45 ERROR_CASE(EGL_NOT_INITIALIZED)
46 ERROR_CASE(EGL_BAD_ACCESS)
47 ERROR_CASE(EGL_BAD_ALLOC)
48 ERROR_CASE(EGL_BAD_ATTRIBUTE)
49 ERROR_CASE(EGL_BAD_CONFIG)
50 ERROR_CASE(EGL_BAD_CONTEXT)
51 ERROR_CASE(EGL_BAD_CURRENT_SURFACE)
52 ERROR_CASE(EGL_BAD_DISPLAY)
53 ERROR_CASE(EGL_BAD_MATCH)
54 ERROR_CASE(EGL_BAD_NATIVE_PIXMAP)
55 ERROR_CASE(EGL_BAD_NATIVE_WINDOW)
56 ERROR_CASE(EGL_BAD_PARAMETER)
57 ERROR_CASE(EGL_BAD_SURFACE)
58 ERROR_CASE(EGL_CONTEXT_LOST)
59 default:
60 return "Unknown error";
61 }
62}
63static const char* egl_error_str() {
64 return egl_error_str(eglGetError());
65}
66
John Reck149173d2015-08-10 09:52:29 -070067static struct {
68 bool bufferAge = false;
69 bool setDamage = false;
70} EglExtensions;
71
72void Frame::map(const SkRect& in, EGLint* out) const {
73 /* The rectangles are specified relative to the bottom-left of the surface
74 * and the x and y components of each rectangle specify the bottom-left
75 * position of that rectangle.
76 *
77 * HWUI does everything with 0,0 being top-left, so need to map
78 * the rect
79 */
80 SkIRect idirty;
81 in.roundOut(&idirty);
82 EGLint y = mHeight - (idirty.y() + idirty.height());
83 // layout: {x, y, width, height}
84 out[0] = idirty.x();
85 out[1] = y;
86 out[2] = idirty.width();
87 out[3] = idirty.height();
John Reck3b202512014-06-23 13:13:08 -070088}
89
90EglManager::EglManager(RenderThread& thread)
91 : mRenderThread(thread)
92 , mEglDisplay(EGL_NO_DISPLAY)
Chris Craikd41c4d82015-01-05 15:51:13 -080093 , mEglConfig(nullptr)
John Reck3b202512014-06-23 13:13:08 -070094 , mEglContext(EGL_NO_CONTEXT)
95 , mPBufferSurface(EGL_NO_SURFACE)
John Reck3b202512014-06-23 13:13:08 -070096 , mCurrentSurface(EGL_NO_SURFACE)
Chris Craikd41c4d82015-01-05 15:51:13 -080097 , mAtlasMap(nullptr)
John Reckd7db4d72015-05-20 07:18:55 -070098 , mAtlasMapSize(0) {
John Reck3b202512014-06-23 13:13:08 -070099}
100
101void EglManager::initialize() {
102 if (hasEglContext()) return;
103
John Reckfbc8df02014-11-14 16:18:41 -0800104 ATRACE_NAME("Creating EGLContext");
105
John Reck3b202512014-06-23 13:13:08 -0700106 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
107 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
108 "Failed to get EGL_DEFAULT_DISPLAY! err=%s", egl_error_str());
109
110 EGLint major, minor;
111 LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE,
112 "Failed to initialize display %p! err=%s", mEglDisplay, egl_error_str());
113
114 ALOGI("Initialized EGL, version %d.%d", (int)major, (int)minor);
115
John Reck149173d2015-08-10 09:52:29 -0700116 initExtensions();
Season Li13d1b4a2015-07-29 17:16:19 -0700117
John Reck149173d2015-08-10 09:52:29 -0700118 // Now that extensions are loaded, pick a swap behavior
119 if (Properties::enablePartialUpdates) {
120 if (Properties::useBufferAge && EglExtensions.bufferAge) {
121 mSwapBehavior = SwapBehavior::BufferAge;
122 } else {
123 mSwapBehavior = SwapBehavior::Preserved;
124 }
125 }
126
127 loadConfig();
John Reck3b202512014-06-23 13:13:08 -0700128 createContext();
John Reckd7db4d72015-05-20 07:18:55 -0700129 createPBufferSurface();
130 makeCurrent(mPBufferSurface);
John Reck704bed02015-11-05 09:22:17 -0800131 DeviceInfo::initialize();
John Reck3b202512014-06-23 13:13:08 -0700132 mRenderThread.renderState().onGLContextCreated();
133 initAtlas();
134}
135
John Reck149173d2015-08-10 09:52:29 -0700136void EglManager::initExtensions() {
John Reck704bed02015-11-05 09:22:17 -0800137 auto extensions = StringUtils::split(
138 eglQueryString(mEglDisplay, EGL_EXTENSIONS));
John Reck1deac992016-09-27 14:45:28 -0700139 // For our purposes we don't care if EGL_BUFFER_AGE is a result of
140 // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered
141 // under EGL_KHR_partial_update and we don't need the expanded scope
142 // that EGL_EXT_buffer_age provides.
143 EglExtensions.bufferAge = extensions.has("EGL_EXT_buffer_age")
144 || extensions.has("EGL_KHR_partial_update");
Chris Craik6e6646c2015-09-14 15:54:12 -0700145 EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update");
John Reck708b6682015-10-22 13:11:00 -0700146 LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"),
147 "Missing required extension EGL_KHR_swap_buffers_with_damage");
John Reck149173d2015-08-10 09:52:29 -0700148}
149
John Reck3b202512014-06-23 13:13:08 -0700150bool EglManager::hasEglContext() {
151 return mEglDisplay != EGL_NO_DISPLAY;
152}
153
John Reck149173d2015-08-10 09:52:29 -0700154void EglManager::loadConfig() {
155 ALOGD("Swap behavior %d", static_cast<int>(mSwapBehavior));
156 EGLint swapBehavior = (mSwapBehavior == SwapBehavior::Preserved)
157 ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
John Reck3b202512014-06-23 13:13:08 -0700158 EGLint attribs[] = {
159 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
160 EGL_RED_SIZE, 8,
161 EGL_GREEN_SIZE, 8,
162 EGL_BLUE_SIZE, 8,
163 EGL_ALPHA_SIZE, 8,
164 EGL_DEPTH_SIZE, 0,
165 EGL_CONFIG_CAVEAT, EGL_NONE,
166 EGL_STENCIL_SIZE, Stencil::getStencilSize(),
167 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | swapBehavior,
168 EGL_NONE
169 };
170
171 EGLint num_configs = 1;
172 if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, num_configs, &num_configs)
173 || num_configs != 1) {
John Reck149173d2015-08-10 09:52:29 -0700174 if (mSwapBehavior == SwapBehavior::Preserved) {
John Reck3b202512014-06-23 13:13:08 -0700175 // Try again without dirty regions enabled
John Reck149173d2015-08-10 09:52:29 -0700176 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
177 mSwapBehavior = SwapBehavior::Discard;
178 loadConfig();
John Reck3b202512014-06-23 13:13:08 -0700179 } else {
John Reck149173d2015-08-10 09:52:29 -0700180 // Failed to get a valid config
John Reck3b202512014-06-23 13:13:08 -0700181 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", egl_error_str());
182 }
183 }
184}
185
186void EglManager::createContext() {
John Reck682573c2015-10-30 10:37:35 -0700187 EGLint attribs[] = {
188 EGL_CONTEXT_CLIENT_VERSION, GLES_VERSION,
189 EGL_NONE
190 };
John Reck3b202512014-06-23 13:13:08 -0700191 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, attribs);
192 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT,
193 "Failed to create context, error = %s", egl_error_str());
194}
195
196void EglManager::setTextureAtlas(const sp<GraphicBuffer>& buffer,
197 int64_t* map, size_t mapSize) {
198
199 // Already initialized
200 if (mAtlasBuffer.get()) {
201 ALOGW("Multiple calls to setTextureAtlas!");
202 delete map;
203 return;
204 }
205
206 mAtlasBuffer = buffer;
207 mAtlasMap = map;
208 mAtlasMapSize = mapSize;
209
210 if (hasEglContext()) {
John Reck3b202512014-06-23 13:13:08 -0700211 initAtlas();
212 }
213}
214
215void EglManager::initAtlas() {
216 if (mAtlasBuffer.get()) {
John Reckebd52612014-12-10 16:47:36 -0800217 mRenderThread.renderState().assetAtlas().init(mAtlasBuffer,
218 mAtlasMap, mAtlasMapSize);
John Reck3b202512014-06-23 13:13:08 -0700219 }
220}
221
John Reckd7db4d72015-05-20 07:18:55 -0700222void EglManager::createPBufferSurface() {
John Reck3b202512014-06-23 13:13:08 -0700223 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
224 "usePBufferSurface() called on uninitialized GlobalContext!");
225
226 if (mPBufferSurface == EGL_NO_SURFACE) {
227 EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
228 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs);
229 }
John Reck3b202512014-06-23 13:13:08 -0700230}
231
232EGLSurface EglManager::createSurface(EGLNativeWindowType window) {
233 initialize();
Chris Craikd41c4d82015-01-05 15:51:13 -0800234 EGLSurface surface = eglCreateWindowSurface(mEglDisplay, mEglConfig, window, nullptr);
John Reck3b202512014-06-23 13:13:08 -0700235 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE,
236 "Failed to create EGLSurface for window %p, eglErr = %s",
237 (void*) window, egl_error_str());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000238
239 if (mSwapBehavior != SwapBehavior::Preserved) {
240 LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED) == EGL_FALSE,
241 "Failed to set swap behavior to destroyed for window %p, eglErr = %s",
242 (void*) window, egl_error_str());
243 }
244
John Reck3b202512014-06-23 13:13:08 -0700245 return surface;
246}
247
248void EglManager::destroySurface(EGLSurface surface) {
249 if (isCurrent(surface)) {
250 makeCurrent(EGL_NO_SURFACE);
251 }
252 if (!eglDestroySurface(mEglDisplay, surface)) {
253 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, egl_error_str());
254 }
255}
256
257void EglManager::destroy() {
258 if (mEglDisplay == EGL_NO_DISPLAY) return;
259
Chris Craik1d477422014-08-26 17:30:15 -0700260 mRenderThread.renderState().onGLContextDestroyed();
John Reck3b202512014-06-23 13:13:08 -0700261 eglDestroyContext(mEglDisplay, mEglContext);
262 eglDestroySurface(mEglDisplay, mPBufferSurface);
263 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
264 eglTerminate(mEglDisplay);
265 eglReleaseThread();
266
267 mEglDisplay = EGL_NO_DISPLAY;
268 mEglContext = EGL_NO_CONTEXT;
269 mPBufferSurface = EGL_NO_SURFACE;
270 mCurrentSurface = EGL_NO_SURFACE;
271}
272
John Reckf2dcc2a2015-07-16 09:17:59 -0700273bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut) {
John Reck3b202512014-06-23 13:13:08 -0700274 if (isCurrent(surface)) return false;
275
276 if (surface == EGL_NO_SURFACE) {
John Reckd7db4d72015-05-20 07:18:55 -0700277 // Ensure we always have a valid surface & context
278 surface = mPBufferSurface;
279 }
280 if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
John Reckf2dcc2a2015-07-16 09:17:59 -0700281 if (errOut) {
282 *errOut = eglGetError();
283 ALOGW("Failed to make current on surface %p, error=%s",
284 (void*)surface, egl_error_str(*errOut));
285 } else {
286 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s",
287 (void*)surface, egl_error_str());
288 }
John Reck3b202512014-06-23 13:13:08 -0700289 }
290 mCurrentSurface = surface;
291 return true;
292}
293
John Reck149173d2015-08-10 09:52:29 -0700294EGLint EglManager::queryBufferAge(EGLSurface surface) {
295 switch (mSwapBehavior) {
296 case SwapBehavior::Discard:
297 return 0;
298 case SwapBehavior::Preserved:
299 return 1;
300 case SwapBehavior::BufferAge:
301 EGLint bufferAge;
302 eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge);
303 return bufferAge;
304 }
305 return 0;
306}
307
308Frame EglManager::beginFrame(EGLSurface surface) {
John Reck3b202512014-06-23 13:13:08 -0700309 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE,
310 "Tried to beginFrame on EGL_NO_SURFACE!");
311 makeCurrent(surface);
John Reck149173d2015-08-10 09:52:29 -0700312 Frame frame;
313 frame.mSurface = surface;
314 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth);
315 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight);
316 frame.mBufferAge = queryBufferAge(surface);
John Reck3b202512014-06-23 13:13:08 -0700317 eglBeginFrame(mEglDisplay, surface);
John Reck149173d2015-08-10 09:52:29 -0700318 return frame;
John Reck3b202512014-06-23 13:13:08 -0700319}
320
John Reck149173d2015-08-10 09:52:29 -0700321void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) {
322#ifdef EGL_KHR_partial_update
323 if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) {
324 EGLint rects[4];
325 frame.map(dirty, rects);
326 if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) {
327 LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s",
328 (void*)frame.mSurface, egl_error_str());
329 }
330 }
331#endif
332}
333
John Reckc96955d2016-02-26 14:56:44 -0800334bool EglManager::damageRequiresSwap() {
335 return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge;
336}
337
John Reck149173d2015-08-10 09:52:29 -0700338bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) {
John Reck55156372015-01-21 07:46:37 -0800339
John Reck682573c2015-10-30 10:37:35 -0700340 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
John Reck55156372015-01-21 07:46:37 -0800341 ATRACE_NAME("Finishing GPU work");
342 fence();
343 }
John Reck55156372015-01-21 07:46:37 -0800344
John Recka672f6b2015-10-22 09:53:26 -0700345 EGLint rects[4];
346 frame.map(screenDirty, rects);
347 eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects,
348 screenDirty.isEmpty() ? 0 : 1);
John Reckd04794a2015-05-08 10:04:36 -0700349
John Reck3b202512014-06-23 13:13:08 -0700350 EGLint err = eglGetError();
John Reck2cdbc7d2014-09-17 16:06:36 -0700351 if (CC_LIKELY(err == EGL_SUCCESS)) {
352 return true;
353 }
John Reckc2547fa2015-10-26 13:52:52 -0700354 if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) {
John Reck2cdbc7d2014-09-17 16:06:36 -0700355 // For some reason our surface was destroyed out from under us
356 // This really shouldn't happen, but if it does we can recover easily
357 // by just not trying to use the surface anymore
John Reckd1ddcf12016-02-05 15:59:55 -0800358 ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...",
359 err, frame.mSurface);
John Reck2cdbc7d2014-09-17 16:06:36 -0700360 return false;
361 }
362 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering",
363 err, egl_error_str(err));
364 // Impossible to hit this, but the compiler doesn't know that
365 return false;
John Reck3b202512014-06-23 13:13:08 -0700366}
367
John Reck55156372015-01-21 07:46:37 -0800368void EglManager::fence() {
369 EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL);
370 eglClientWaitSyncKHR(mEglDisplay, fence,
371 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
372 eglDestroySyncKHR(mEglDisplay, fence);
373}
374
John Reck1125d1f2014-10-23 11:02:19 -0700375bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
John Reck149173d2015-08-10 09:52:29 -0700376 if (mSwapBehavior != SwapBehavior::Preserved) return false;
John Reck3b202512014-06-23 13:13:08 -0700377
John Reck149173d2015-08-10 09:52:29 -0700378 bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
379 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED);
380 if (!preserved) {
381 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s",
382 (void*) surface, egl_error_str());
John Reck1125d1f2014-10-23 11:02:19 -0700383 // Maybe it's already set?
384 EGLint swapBehavior;
385 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) {
386 preserved = (swapBehavior == EGL_BUFFER_PRESERVED);
387 } else {
388 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p",
389 (void*) surface, egl_error_str());
390 }
John Reck3b202512014-06-23 13:13:08 -0700391 }
John Reck1125d1f2014-10-23 11:02:19 -0700392
393 return preserved;
John Reck3b202512014-06-23 13:13:08 -0700394}
395
John Reck3b202512014-06-23 13:13:08 -0700396} /* namespace renderthread */
397} /* namespace uirenderer */
398} /* namespace android */