blob: 86731c9581be9d5c43b455801371adf38a8a3090 [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"
John Reck704bed02015-11-05 09:22:17 -080020#include "DeviceInfo.h"
John Reckd04794a2015-05-08 10:04:36 -070021#include "Properties.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080022#include "RenderThread.h"
John Reckd04794a2015-05-08 10:04:36 -070023#include "renderstate/RenderState.h"
Chris Craik6e6646c2015-09-14 15:54:12 -070024#include "utils/StringUtils.h"
John Reck3b202512014-06-23 13:13:08 -070025#include <cutils/log.h>
26#include <cutils/properties.h>
John Reck55156372015-01-21 07:46:37 -080027#include <EGL/eglext.h>
John Reck149173d2015-08-10 09:52:29 -070028#include <string>
29
John Reck3b202512014-06-23 13:13:08 -070030#define GLES_VERSION 2
31
32// Android-specific addition that is used to show when frames began in systrace
33EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface);
34
35namespace android {
36namespace uirenderer {
37namespace renderthread {
38
39#define ERROR_CASE(x) case x: return #x;
40static const char* egl_error_str(EGLint error) {
41 switch (error) {
42 ERROR_CASE(EGL_SUCCESS)
43 ERROR_CASE(EGL_NOT_INITIALIZED)
44 ERROR_CASE(EGL_BAD_ACCESS)
45 ERROR_CASE(EGL_BAD_ALLOC)
46 ERROR_CASE(EGL_BAD_ATTRIBUTE)
47 ERROR_CASE(EGL_BAD_CONFIG)
48 ERROR_CASE(EGL_BAD_CONTEXT)
49 ERROR_CASE(EGL_BAD_CURRENT_SURFACE)
50 ERROR_CASE(EGL_BAD_DISPLAY)
51 ERROR_CASE(EGL_BAD_MATCH)
52 ERROR_CASE(EGL_BAD_NATIVE_PIXMAP)
53 ERROR_CASE(EGL_BAD_NATIVE_WINDOW)
54 ERROR_CASE(EGL_BAD_PARAMETER)
55 ERROR_CASE(EGL_BAD_SURFACE)
56 ERROR_CASE(EGL_CONTEXT_LOST)
57 default:
58 return "Unknown error";
59 }
60}
61static const char* egl_error_str() {
62 return egl_error_str(eglGetError());
63}
64
John Reck149173d2015-08-10 09:52:29 -070065static struct {
66 bool bufferAge = false;
67 bool setDamage = false;
68} EglExtensions;
69
70void Frame::map(const SkRect& in, EGLint* out) const {
71 /* The rectangles are specified relative to the bottom-left of the surface
72 * and the x and y components of each rectangle specify the bottom-left
73 * position of that rectangle.
74 *
75 * HWUI does everything with 0,0 being top-left, so need to map
76 * the rect
77 */
78 SkIRect idirty;
79 in.roundOut(&idirty);
80 EGLint y = mHeight - (idirty.y() + idirty.height());
81 // layout: {x, y, width, height}
82 out[0] = idirty.x();
83 out[1] = y;
84 out[2] = idirty.width();
85 out[3] = idirty.height();
John Reck3b202512014-06-23 13:13:08 -070086}
87
88EglManager::EglManager(RenderThread& thread)
89 : mRenderThread(thread)
90 , mEglDisplay(EGL_NO_DISPLAY)
Chris Craikd41c4d82015-01-05 15:51:13 -080091 , mEglConfig(nullptr)
John Reck3b202512014-06-23 13:13:08 -070092 , mEglContext(EGL_NO_CONTEXT)
93 , mPBufferSurface(EGL_NO_SURFACE)
John Reck3b202512014-06-23 13:13:08 -070094 , mCurrentSurface(EGL_NO_SURFACE)
Chris Craikd41c4d82015-01-05 15:51:13 -080095 , mAtlasMap(nullptr)
John Reckd7db4d72015-05-20 07:18:55 -070096 , mAtlasMapSize(0) {
John Reck3b202512014-06-23 13:13:08 -070097}
98
99void EglManager::initialize() {
100 if (hasEglContext()) return;
101
John Reckfbc8df02014-11-14 16:18:41 -0800102 ATRACE_NAME("Creating EGLContext");
103
John Reck3b202512014-06-23 13:13:08 -0700104 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
105 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
106 "Failed to get EGL_DEFAULT_DISPLAY! err=%s", egl_error_str());
107
108 EGLint major, minor;
109 LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE,
110 "Failed to initialize display %p! err=%s", mEglDisplay, egl_error_str());
111
112 ALOGI("Initialized EGL, version %d.%d", (int)major, (int)minor);
113
John Reck149173d2015-08-10 09:52:29 -0700114 initExtensions();
Season Li13d1b4a2015-07-29 17:16:19 -0700115
John Reck149173d2015-08-10 09:52:29 -0700116 // Now that extensions are loaded, pick a swap behavior
117 if (Properties::enablePartialUpdates) {
118 if (Properties::useBufferAge && EglExtensions.bufferAge) {
119 mSwapBehavior = SwapBehavior::BufferAge;
120 } else {
121 mSwapBehavior = SwapBehavior::Preserved;
122 }
123 }
124
125 loadConfig();
John Reck3b202512014-06-23 13:13:08 -0700126 createContext();
John Reckd7db4d72015-05-20 07:18:55 -0700127 createPBufferSurface();
128 makeCurrent(mPBufferSurface);
John Reck704bed02015-11-05 09:22:17 -0800129 DeviceInfo::initialize();
John Reck3b202512014-06-23 13:13:08 -0700130 mRenderThread.renderState().onGLContextCreated();
131 initAtlas();
132}
133
John Reck149173d2015-08-10 09:52:29 -0700134void EglManager::initExtensions() {
John Reck704bed02015-11-05 09:22:17 -0800135 auto extensions = StringUtils::split(
136 eglQueryString(mEglDisplay, EGL_EXTENSIONS));
John Reck1deac992016-09-27 14:45:28 -0700137 // For our purposes we don't care if EGL_BUFFER_AGE is a result of
138 // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered
139 // under EGL_KHR_partial_update and we don't need the expanded scope
140 // that EGL_EXT_buffer_age provides.
141 EglExtensions.bufferAge = extensions.has("EGL_EXT_buffer_age")
142 || extensions.has("EGL_KHR_partial_update");
Chris Craik6e6646c2015-09-14 15:54:12 -0700143 EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update");
John Reck708b6682015-10-22 13:11:00 -0700144 LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"),
145 "Missing required extension EGL_KHR_swap_buffers_with_damage");
John Reck149173d2015-08-10 09:52:29 -0700146}
147
John Reck3b202512014-06-23 13:13:08 -0700148bool EglManager::hasEglContext() {
149 return mEglDisplay != EGL_NO_DISPLAY;
150}
151
John Reck149173d2015-08-10 09:52:29 -0700152void EglManager::loadConfig() {
153 ALOGD("Swap behavior %d", static_cast<int>(mSwapBehavior));
154 EGLint swapBehavior = (mSwapBehavior == SwapBehavior::Preserved)
155 ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
John Reck3b202512014-06-23 13:13:08 -0700156 EGLint attribs[] = {
157 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
158 EGL_RED_SIZE, 8,
159 EGL_GREEN_SIZE, 8,
160 EGL_BLUE_SIZE, 8,
161 EGL_ALPHA_SIZE, 8,
162 EGL_DEPTH_SIZE, 0,
163 EGL_CONFIG_CAVEAT, EGL_NONE,
164 EGL_STENCIL_SIZE, Stencil::getStencilSize(),
165 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | swapBehavior,
166 EGL_NONE
167 };
168
169 EGLint num_configs = 1;
170 if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, num_configs, &num_configs)
171 || num_configs != 1) {
John Reck149173d2015-08-10 09:52:29 -0700172 if (mSwapBehavior == SwapBehavior::Preserved) {
John Reck3b202512014-06-23 13:13:08 -0700173 // Try again without dirty regions enabled
John Reck149173d2015-08-10 09:52:29 -0700174 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
175 mSwapBehavior = SwapBehavior::Discard;
176 loadConfig();
John Reck3b202512014-06-23 13:13:08 -0700177 } else {
John Reck149173d2015-08-10 09:52:29 -0700178 // Failed to get a valid config
John Reck3b202512014-06-23 13:13:08 -0700179 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", egl_error_str());
180 }
181 }
182}
183
184void EglManager::createContext() {
John Reck682573c2015-10-30 10:37:35 -0700185 EGLint attribs[] = {
186 EGL_CONTEXT_CLIENT_VERSION, GLES_VERSION,
187 EGL_NONE
188 };
John Reck3b202512014-06-23 13:13:08 -0700189 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, attribs);
190 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT,
191 "Failed to create context, error = %s", egl_error_str());
192}
193
194void EglManager::setTextureAtlas(const sp<GraphicBuffer>& buffer,
195 int64_t* map, size_t mapSize) {
196
197 // Already initialized
198 if (mAtlasBuffer.get()) {
199 ALOGW("Multiple calls to setTextureAtlas!");
200 delete map;
201 return;
202 }
203
204 mAtlasBuffer = buffer;
205 mAtlasMap = map;
206 mAtlasMapSize = mapSize;
207
208 if (hasEglContext()) {
John Reck3b202512014-06-23 13:13:08 -0700209 initAtlas();
210 }
211}
212
213void EglManager::initAtlas() {
214 if (mAtlasBuffer.get()) {
John Reckebd52612014-12-10 16:47:36 -0800215 mRenderThread.renderState().assetAtlas().init(mAtlasBuffer,
216 mAtlasMap, mAtlasMapSize);
John Reck3b202512014-06-23 13:13:08 -0700217 }
218}
219
John Reckd7db4d72015-05-20 07:18:55 -0700220void EglManager::createPBufferSurface() {
John Reck3b202512014-06-23 13:13:08 -0700221 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
222 "usePBufferSurface() called on uninitialized GlobalContext!");
223
224 if (mPBufferSurface == EGL_NO_SURFACE) {
225 EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
226 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs);
227 }
John Reck3b202512014-06-23 13:13:08 -0700228}
229
230EGLSurface EglManager::createSurface(EGLNativeWindowType window) {
231 initialize();
Chris Craikd41c4d82015-01-05 15:51:13 -0800232 EGLSurface surface = eglCreateWindowSurface(mEglDisplay, mEglConfig, window, nullptr);
John Reck3b202512014-06-23 13:13:08 -0700233 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE,
234 "Failed to create EGLSurface for window %p, eglErr = %s",
235 (void*) window, egl_error_str());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000236
237 if (mSwapBehavior != SwapBehavior::Preserved) {
238 LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED) == EGL_FALSE,
239 "Failed to set swap behavior to destroyed for window %p, eglErr = %s",
240 (void*) window, egl_error_str());
241 }
242
John Reck3b202512014-06-23 13:13:08 -0700243 return surface;
244}
245
246void EglManager::destroySurface(EGLSurface surface) {
247 if (isCurrent(surface)) {
248 makeCurrent(EGL_NO_SURFACE);
249 }
250 if (!eglDestroySurface(mEglDisplay, surface)) {
251 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, egl_error_str());
252 }
253}
254
255void EglManager::destroy() {
256 if (mEglDisplay == EGL_NO_DISPLAY) return;
257
Chris Craik1d477422014-08-26 17:30:15 -0700258 mRenderThread.renderState().onGLContextDestroyed();
John Reck3b202512014-06-23 13:13:08 -0700259 eglDestroyContext(mEglDisplay, mEglContext);
260 eglDestroySurface(mEglDisplay, mPBufferSurface);
261 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
262 eglTerminate(mEglDisplay);
263 eglReleaseThread();
264
265 mEglDisplay = EGL_NO_DISPLAY;
266 mEglContext = EGL_NO_CONTEXT;
267 mPBufferSurface = EGL_NO_SURFACE;
268 mCurrentSurface = EGL_NO_SURFACE;
269}
270
John Reckf2dcc2a2015-07-16 09:17:59 -0700271bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut) {
John Reck3b202512014-06-23 13:13:08 -0700272 if (isCurrent(surface)) return false;
273
274 if (surface == EGL_NO_SURFACE) {
John Reckd7db4d72015-05-20 07:18:55 -0700275 // Ensure we always have a valid surface & context
276 surface = mPBufferSurface;
277 }
278 if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
John Reckf2dcc2a2015-07-16 09:17:59 -0700279 if (errOut) {
280 *errOut = eglGetError();
281 ALOGW("Failed to make current on surface %p, error=%s",
282 (void*)surface, egl_error_str(*errOut));
283 } else {
284 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s",
285 (void*)surface, egl_error_str());
286 }
John Reck3b202512014-06-23 13:13:08 -0700287 }
288 mCurrentSurface = surface;
289 return true;
290}
291
John Reck149173d2015-08-10 09:52:29 -0700292EGLint EglManager::queryBufferAge(EGLSurface surface) {
293 switch (mSwapBehavior) {
294 case SwapBehavior::Discard:
295 return 0;
296 case SwapBehavior::Preserved:
297 return 1;
298 case SwapBehavior::BufferAge:
299 EGLint bufferAge;
300 eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge);
301 return bufferAge;
302 }
303 return 0;
304}
305
306Frame EglManager::beginFrame(EGLSurface surface) {
John Reck3b202512014-06-23 13:13:08 -0700307 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE,
308 "Tried to beginFrame on EGL_NO_SURFACE!");
309 makeCurrent(surface);
John Reck149173d2015-08-10 09:52:29 -0700310 Frame frame;
311 frame.mSurface = surface;
312 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth);
313 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight);
314 frame.mBufferAge = queryBufferAge(surface);
John Reck3b202512014-06-23 13:13:08 -0700315 eglBeginFrame(mEglDisplay, surface);
John Reck149173d2015-08-10 09:52:29 -0700316 return frame;
John Reck3b202512014-06-23 13:13:08 -0700317}
318
John Reck149173d2015-08-10 09:52:29 -0700319void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) {
320#ifdef EGL_KHR_partial_update
321 if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) {
322 EGLint rects[4];
323 frame.map(dirty, rects);
324 if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) {
325 LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s",
326 (void*)frame.mSurface, egl_error_str());
327 }
328 }
329#endif
330}
331
John Reckc96955d2016-02-26 14:56:44 -0800332bool EglManager::damageRequiresSwap() {
333 return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge;
334}
335
John Reck149173d2015-08-10 09:52:29 -0700336bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) {
John Reck55156372015-01-21 07:46:37 -0800337
John Reck682573c2015-10-30 10:37:35 -0700338 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
John Reck55156372015-01-21 07:46:37 -0800339 ATRACE_NAME("Finishing GPU work");
340 fence();
341 }
John Reck55156372015-01-21 07:46:37 -0800342
John Recka672f6b2015-10-22 09:53:26 -0700343 EGLint rects[4];
344 frame.map(screenDirty, rects);
345 eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects,
346 screenDirty.isEmpty() ? 0 : 1);
John Reckd04794a2015-05-08 10:04:36 -0700347
John Reck3b202512014-06-23 13:13:08 -0700348 EGLint err = eglGetError();
John Reck2cdbc7d2014-09-17 16:06:36 -0700349 if (CC_LIKELY(err == EGL_SUCCESS)) {
350 return true;
351 }
John Reckc2547fa2015-10-26 13:52:52 -0700352 if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) {
John Reck2cdbc7d2014-09-17 16:06:36 -0700353 // For some reason our surface was destroyed out from under us
354 // This really shouldn't happen, but if it does we can recover easily
355 // by just not trying to use the surface anymore
John Reckd1ddcf12016-02-05 15:59:55 -0800356 ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...",
357 err, frame.mSurface);
John Reck2cdbc7d2014-09-17 16:06:36 -0700358 return false;
359 }
360 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering",
361 err, egl_error_str(err));
362 // Impossible to hit this, but the compiler doesn't know that
363 return false;
John Reck3b202512014-06-23 13:13:08 -0700364}
365
John Reck55156372015-01-21 07:46:37 -0800366void EglManager::fence() {
367 EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL);
368 eglClientWaitSyncKHR(mEglDisplay, fence,
369 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
370 eglDestroySyncKHR(mEglDisplay, fence);
371}
372
John Reck1125d1f2014-10-23 11:02:19 -0700373bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
John Reck149173d2015-08-10 09:52:29 -0700374 if (mSwapBehavior != SwapBehavior::Preserved) return false;
John Reck3b202512014-06-23 13:13:08 -0700375
John Reck149173d2015-08-10 09:52:29 -0700376 bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
377 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED);
378 if (!preserved) {
379 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s",
380 (void*) surface, egl_error_str());
John Reck1125d1f2014-10-23 11:02:19 -0700381 // Maybe it's already set?
382 EGLint swapBehavior;
383 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) {
384 preserved = (swapBehavior == EGL_BUFFER_PRESERVED);
385 } else {
386 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p",
387 (void*) surface, egl_error_str());
388 }
John Reck3b202512014-06-23 13:13:08 -0700389 }
John Reck1125d1f2014-10-23 11:02:19 -0700390
391 return preserved;
John Reck3b202512014-06-23 13:13:08 -0700392}
393
John Reck3b202512014-06-23 13:13:08 -0700394} /* namespace renderthread */
395} /* namespace uirenderer */
396} /* namespace android */