blob: d2ce49f800fc42b79b6f658d944c1dbf32e54e39 [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
John Reckd04794a2015-05-08 10:04:36 -070019#include "Caches.h"
20#include "Properties.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080021#include "RenderThread.h"
John Reckd04794a2015-05-08 10:04:36 -070022#include "renderstate/RenderState.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080023
John Reck3b202512014-06-23 13:13:08 -070024#include <cutils/log.h>
25#include <cutils/properties.h>
John Reck55156372015-01-21 07:46:37 -080026#include <EGL/eglext.h>
John Reck3b202512014-06-23 13:13:08 -070027
John Reck149173d2015-08-10 09:52:29 -070028#include <string>
29
John Reck3b202512014-06-23 13:13:08 -070030#define GLES_VERSION 2
31
John Reck55156372015-01-21 07:46:37 -080032#define WAIT_FOR_GPU_COMPLETION 0
33
John Reck3b202512014-06-23 13:13:08 -070034// 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 Reck3b202512014-06-23 13:13:08 -0700131 mRenderThread.renderState().onGLContextCreated();
132 initAtlas();
133}
134
John Reck149173d2015-08-10 09:52:29 -0700135void EglManager::initExtensions() {
136 std::string extensions(eglQueryString(mEglDisplay, EGL_EXTENSIONS));
137 auto has = [&](const char* ext) {
138 return extensions.find(ext) != std::string::npos;
139 };
140 EglExtensions.bufferAge = has("EGL_EXT_buffer_age");
141 EglExtensions.setDamage = has("EGL_KHR_partial_update");
142}
143
John Reck3b202512014-06-23 13:13:08 -0700144bool EglManager::hasEglContext() {
145 return mEglDisplay != EGL_NO_DISPLAY;
146}
147
John Reck149173d2015-08-10 09:52:29 -0700148void EglManager::loadConfig() {
149 ALOGD("Swap behavior %d", static_cast<int>(mSwapBehavior));
150 EGLint swapBehavior = (mSwapBehavior == SwapBehavior::Preserved)
151 ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
John Reck3b202512014-06-23 13:13:08 -0700152 EGLint attribs[] = {
153 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
154 EGL_RED_SIZE, 8,
155 EGL_GREEN_SIZE, 8,
156 EGL_BLUE_SIZE, 8,
157 EGL_ALPHA_SIZE, 8,
158 EGL_DEPTH_SIZE, 0,
159 EGL_CONFIG_CAVEAT, EGL_NONE,
160 EGL_STENCIL_SIZE, Stencil::getStencilSize(),
161 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | swapBehavior,
162 EGL_NONE
163 };
164
165 EGLint num_configs = 1;
166 if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, num_configs, &num_configs)
167 || num_configs != 1) {
John Reck149173d2015-08-10 09:52:29 -0700168 if (mSwapBehavior == SwapBehavior::Preserved) {
John Reck3b202512014-06-23 13:13:08 -0700169 // Try again without dirty regions enabled
John Reck149173d2015-08-10 09:52:29 -0700170 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
171 mSwapBehavior = SwapBehavior::Discard;
172 loadConfig();
John Reck3b202512014-06-23 13:13:08 -0700173 } else {
John Reck149173d2015-08-10 09:52:29 -0700174 // Failed to get a valid config
John Reck3b202512014-06-23 13:13:08 -0700175 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", egl_error_str());
176 }
177 }
178}
179
180void EglManager::createContext() {
181 EGLint attribs[] = { EGL_CONTEXT_CLIENT_VERSION, GLES_VERSION, EGL_NONE };
182 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, attribs);
183 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT,
184 "Failed to create context, error = %s", egl_error_str());
185}
186
187void EglManager::setTextureAtlas(const sp<GraphicBuffer>& buffer,
188 int64_t* map, size_t mapSize) {
189
190 // Already initialized
191 if (mAtlasBuffer.get()) {
192 ALOGW("Multiple calls to setTextureAtlas!");
193 delete map;
194 return;
195 }
196
197 mAtlasBuffer = buffer;
198 mAtlasMap = map;
199 mAtlasMapSize = mapSize;
200
201 if (hasEglContext()) {
John Reck3b202512014-06-23 13:13:08 -0700202 initAtlas();
203 }
204}
205
206void EglManager::initAtlas() {
207 if (mAtlasBuffer.get()) {
John Reckebd52612014-12-10 16:47:36 -0800208 mRenderThread.renderState().assetAtlas().init(mAtlasBuffer,
209 mAtlasMap, mAtlasMapSize);
John Reck3b202512014-06-23 13:13:08 -0700210 }
211}
212
John Reckd7db4d72015-05-20 07:18:55 -0700213void EglManager::createPBufferSurface() {
John Reck3b202512014-06-23 13:13:08 -0700214 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
215 "usePBufferSurface() called on uninitialized GlobalContext!");
216
217 if (mPBufferSurface == EGL_NO_SURFACE) {
218 EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
219 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs);
220 }
John Reck3b202512014-06-23 13:13:08 -0700221}
222
223EGLSurface EglManager::createSurface(EGLNativeWindowType window) {
224 initialize();
Chris Craikd41c4d82015-01-05 15:51:13 -0800225 EGLSurface surface = eglCreateWindowSurface(mEglDisplay, mEglConfig, window, nullptr);
John Reck3b202512014-06-23 13:13:08 -0700226 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE,
227 "Failed to create EGLSurface for window %p, eglErr = %s",
228 (void*) window, egl_error_str());
229 return surface;
230}
231
232void EglManager::destroySurface(EGLSurface surface) {
233 if (isCurrent(surface)) {
234 makeCurrent(EGL_NO_SURFACE);
235 }
236 if (!eglDestroySurface(mEglDisplay, surface)) {
237 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, egl_error_str());
238 }
239}
240
241void EglManager::destroy() {
242 if (mEglDisplay == EGL_NO_DISPLAY) return;
243
Chris Craik1d477422014-08-26 17:30:15 -0700244 mRenderThread.renderState().onGLContextDestroyed();
John Reck3b202512014-06-23 13:13:08 -0700245 eglDestroyContext(mEglDisplay, mEglContext);
246 eglDestroySurface(mEglDisplay, mPBufferSurface);
247 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
248 eglTerminate(mEglDisplay);
249 eglReleaseThread();
250
251 mEglDisplay = EGL_NO_DISPLAY;
252 mEglContext = EGL_NO_CONTEXT;
253 mPBufferSurface = EGL_NO_SURFACE;
254 mCurrentSurface = EGL_NO_SURFACE;
255}
256
John Reckf2dcc2a2015-07-16 09:17:59 -0700257bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut) {
John Reck3b202512014-06-23 13:13:08 -0700258 if (isCurrent(surface)) return false;
259
260 if (surface == EGL_NO_SURFACE) {
John Reckd7db4d72015-05-20 07:18:55 -0700261 // Ensure we always have a valid surface & context
262 surface = mPBufferSurface;
263 }
264 if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
John Reckf2dcc2a2015-07-16 09:17:59 -0700265 if (errOut) {
266 *errOut = eglGetError();
267 ALOGW("Failed to make current on surface %p, error=%s",
268 (void*)surface, egl_error_str(*errOut));
269 } else {
270 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s",
271 (void*)surface, egl_error_str());
272 }
John Reck3b202512014-06-23 13:13:08 -0700273 }
274 mCurrentSurface = surface;
275 return true;
276}
277
John Reck149173d2015-08-10 09:52:29 -0700278EGLint EglManager::queryBufferAge(EGLSurface surface) {
279 switch (mSwapBehavior) {
280 case SwapBehavior::Discard:
281 return 0;
282 case SwapBehavior::Preserved:
283 return 1;
284 case SwapBehavior::BufferAge:
285 EGLint bufferAge;
286 eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge);
287 return bufferAge;
288 }
289 return 0;
290}
291
292Frame EglManager::beginFrame(EGLSurface surface) {
John Reck3b202512014-06-23 13:13:08 -0700293 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE,
294 "Tried to beginFrame on EGL_NO_SURFACE!");
295 makeCurrent(surface);
John Reck149173d2015-08-10 09:52:29 -0700296 Frame frame;
297 frame.mSurface = surface;
298 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth);
299 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight);
300 frame.mBufferAge = queryBufferAge(surface);
John Reck3b202512014-06-23 13:13:08 -0700301 eglBeginFrame(mEglDisplay, surface);
John Reck149173d2015-08-10 09:52:29 -0700302 return frame;
John Reck3b202512014-06-23 13:13:08 -0700303}
304
John Reck149173d2015-08-10 09:52:29 -0700305void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) {
306#ifdef EGL_KHR_partial_update
307 if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) {
308 EGLint rects[4];
309 frame.map(dirty, rects);
310 if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) {
311 LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s",
312 (void*)frame.mSurface, egl_error_str());
313 }
314 }
315#endif
316}
317
318bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) {
John Reck55156372015-01-21 07:46:37 -0800319
320#if WAIT_FOR_GPU_COMPLETION
321 {
322 ATRACE_NAME("Finishing GPU work");
323 fence();
324 }
325#endif
326
John Reckd04794a2015-05-08 10:04:36 -0700327#ifdef EGL_KHR_swap_buffers_with_damage
John Reck4cd44f82015-05-27 10:26:10 -0700328 if (CC_LIKELY(Properties::swapBuffersWithDamage)) {
John Reck149173d2015-08-10 09:52:29 -0700329 EGLint rects[4];
330 frame.map(screenDirty, rects);
331 eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects,
332 screenDirty.isEmpty() ? 0 : 1);
John Reckd04794a2015-05-08 10:04:36 -0700333 } else {
John Reck149173d2015-08-10 09:52:29 -0700334 eglSwapBuffers(mEglDisplay, frame.mSurface);
John Reckd04794a2015-05-08 10:04:36 -0700335 }
336#else
John Reck149173d2015-08-10 09:52:29 -0700337 eglSwapBuffers(mEglDisplay, frame.mSurface);
John Reckd04794a2015-05-08 10:04:36 -0700338#endif
339
John Reck3b202512014-06-23 13:13:08 -0700340 EGLint err = eglGetError();
John Reck2cdbc7d2014-09-17 16:06:36 -0700341 if (CC_LIKELY(err == EGL_SUCCESS)) {
342 return true;
343 }
344 if (err == EGL_BAD_SURFACE) {
345 // For some reason our surface was destroyed out from under us
346 // This really shouldn't happen, but if it does we can recover easily
347 // by just not trying to use the surface anymore
John Reck149173d2015-08-10 09:52:29 -0700348 ALOGW("swapBuffers encountered EGL_BAD_SURFACE on %p, halting rendering...",
349 frame.mSurface);
John Reck2cdbc7d2014-09-17 16:06:36 -0700350 return false;
351 }
352 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering",
353 err, egl_error_str(err));
354 // Impossible to hit this, but the compiler doesn't know that
355 return false;
John Reck3b202512014-06-23 13:13:08 -0700356}
357
John Reck55156372015-01-21 07:46:37 -0800358void EglManager::fence() {
359 EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL);
360 eglClientWaitSyncKHR(mEglDisplay, fence,
361 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
362 eglDestroySyncKHR(mEglDisplay, fence);
363}
364
John Reck1125d1f2014-10-23 11:02:19 -0700365bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
John Reck149173d2015-08-10 09:52:29 -0700366 if (mSwapBehavior != SwapBehavior::Preserved) return false;
John Reck3b202512014-06-23 13:13:08 -0700367
John Reck149173d2015-08-10 09:52:29 -0700368 bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
369 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED);
370 if (!preserved) {
371 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s",
372 (void*) surface, egl_error_str());
John Reck1125d1f2014-10-23 11:02:19 -0700373 // Maybe it's already set?
374 EGLint swapBehavior;
375 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) {
376 preserved = (swapBehavior == EGL_BUFFER_PRESERVED);
377 } else {
378 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p",
379 (void*) surface, egl_error_str());
380 }
John Reck3b202512014-06-23 13:13:08 -0700381 }
John Reck1125d1f2014-10-23 11:02:19 -0700382
383 return preserved;
John Reck3b202512014-06-23 13:13:08 -0700384}
385
John Reck3b202512014-06-23 13:13:08 -0700386} /* namespace renderthread */
387} /* namespace uirenderer */
388} /* namespace android */