blob: 364d4dda1e7fd382fe5a59eb2524a0542afc54b2 [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));
Chris Craik6e6646c2015-09-14 15:54:12 -0700137 EglExtensions.bufferAge = extensions.has("EGL_EXT_buffer_age");
138 EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update");
John Reck708b6682015-10-22 13:11:00 -0700139 LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"),
140 "Missing required extension EGL_KHR_swap_buffers_with_damage");
John Reck149173d2015-08-10 09:52:29 -0700141}
142
John Reck3b202512014-06-23 13:13:08 -0700143bool EglManager::hasEglContext() {
144 return mEglDisplay != EGL_NO_DISPLAY;
145}
146
John Reck149173d2015-08-10 09:52:29 -0700147void EglManager::loadConfig() {
148 ALOGD("Swap behavior %d", static_cast<int>(mSwapBehavior));
149 EGLint swapBehavior = (mSwapBehavior == SwapBehavior::Preserved)
150 ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
John Reck3b202512014-06-23 13:13:08 -0700151 EGLint attribs[] = {
152 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
153 EGL_RED_SIZE, 8,
154 EGL_GREEN_SIZE, 8,
155 EGL_BLUE_SIZE, 8,
156 EGL_ALPHA_SIZE, 8,
157 EGL_DEPTH_SIZE, 0,
158 EGL_CONFIG_CAVEAT, EGL_NONE,
159 EGL_STENCIL_SIZE, Stencil::getStencilSize(),
160 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | swapBehavior,
161 EGL_NONE
162 };
163
164 EGLint num_configs = 1;
165 if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, num_configs, &num_configs)
166 || num_configs != 1) {
John Reck149173d2015-08-10 09:52:29 -0700167 if (mSwapBehavior == SwapBehavior::Preserved) {
John Reck3b202512014-06-23 13:13:08 -0700168 // Try again without dirty regions enabled
John Reck149173d2015-08-10 09:52:29 -0700169 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
170 mSwapBehavior = SwapBehavior::Discard;
171 loadConfig();
John Reck3b202512014-06-23 13:13:08 -0700172 } else {
John Reck149173d2015-08-10 09:52:29 -0700173 // Failed to get a valid config
John Reck3b202512014-06-23 13:13:08 -0700174 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", egl_error_str());
175 }
176 }
177}
178
179void EglManager::createContext() {
John Reck682573c2015-10-30 10:37:35 -0700180 EGLint attribs[] = {
181 EGL_CONTEXT_CLIENT_VERSION, GLES_VERSION,
182 EGL_NONE
183 };
John Reck3b202512014-06-23 13:13:08 -0700184 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, attribs);
185 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT,
186 "Failed to create context, error = %s", egl_error_str());
187}
188
189void EglManager::setTextureAtlas(const sp<GraphicBuffer>& buffer,
190 int64_t* map, size_t mapSize) {
191
192 // Already initialized
193 if (mAtlasBuffer.get()) {
194 ALOGW("Multiple calls to setTextureAtlas!");
195 delete map;
196 return;
197 }
198
199 mAtlasBuffer = buffer;
200 mAtlasMap = map;
201 mAtlasMapSize = mapSize;
202
203 if (hasEglContext()) {
John Reck3b202512014-06-23 13:13:08 -0700204 initAtlas();
205 }
206}
207
208void EglManager::initAtlas() {
209 if (mAtlasBuffer.get()) {
John Reckebd52612014-12-10 16:47:36 -0800210 mRenderThread.renderState().assetAtlas().init(mAtlasBuffer,
211 mAtlasMap, mAtlasMapSize);
John Reck3b202512014-06-23 13:13:08 -0700212 }
213}
214
John Reckd7db4d72015-05-20 07:18:55 -0700215void EglManager::createPBufferSurface() {
John Reck3b202512014-06-23 13:13:08 -0700216 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
217 "usePBufferSurface() called on uninitialized GlobalContext!");
218
219 if (mPBufferSurface == EGL_NO_SURFACE) {
220 EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
221 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs);
222 }
John Reck3b202512014-06-23 13:13:08 -0700223}
224
225EGLSurface EglManager::createSurface(EGLNativeWindowType window) {
226 initialize();
Chris Craikd41c4d82015-01-05 15:51:13 -0800227 EGLSurface surface = eglCreateWindowSurface(mEglDisplay, mEglConfig, window, nullptr);
John Reck3b202512014-06-23 13:13:08 -0700228 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE,
229 "Failed to create EGLSurface for window %p, eglErr = %s",
230 (void*) window, egl_error_str());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000231
232 if (mSwapBehavior != SwapBehavior::Preserved) {
233 LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED) == EGL_FALSE,
234 "Failed to set swap behavior to destroyed for window %p, eglErr = %s",
235 (void*) window, egl_error_str());
236 }
237
John Reck3b202512014-06-23 13:13:08 -0700238 return surface;
239}
240
241void EglManager::destroySurface(EGLSurface surface) {
242 if (isCurrent(surface)) {
243 makeCurrent(EGL_NO_SURFACE);
244 }
245 if (!eglDestroySurface(mEglDisplay, surface)) {
246 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, egl_error_str());
247 }
248}
249
250void EglManager::destroy() {
251 if (mEglDisplay == EGL_NO_DISPLAY) return;
252
Chris Craik1d477422014-08-26 17:30:15 -0700253 mRenderThread.renderState().onGLContextDestroyed();
John Reck3b202512014-06-23 13:13:08 -0700254 eglDestroyContext(mEglDisplay, mEglContext);
255 eglDestroySurface(mEglDisplay, mPBufferSurface);
256 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
257 eglTerminate(mEglDisplay);
258 eglReleaseThread();
259
260 mEglDisplay = EGL_NO_DISPLAY;
261 mEglContext = EGL_NO_CONTEXT;
262 mPBufferSurface = EGL_NO_SURFACE;
263 mCurrentSurface = EGL_NO_SURFACE;
264}
265
John Reckf2dcc2a2015-07-16 09:17:59 -0700266bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut) {
John Reck3b202512014-06-23 13:13:08 -0700267 if (isCurrent(surface)) return false;
268
269 if (surface == EGL_NO_SURFACE) {
John Reckd7db4d72015-05-20 07:18:55 -0700270 // Ensure we always have a valid surface & context
271 surface = mPBufferSurface;
272 }
273 if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
John Reckf2dcc2a2015-07-16 09:17:59 -0700274 if (errOut) {
275 *errOut = eglGetError();
276 ALOGW("Failed to make current on surface %p, error=%s",
277 (void*)surface, egl_error_str(*errOut));
278 } else {
279 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s",
280 (void*)surface, egl_error_str());
281 }
John Reck3b202512014-06-23 13:13:08 -0700282 }
283 mCurrentSurface = surface;
284 return true;
285}
286
John Reck149173d2015-08-10 09:52:29 -0700287EGLint EglManager::queryBufferAge(EGLSurface surface) {
288 switch (mSwapBehavior) {
289 case SwapBehavior::Discard:
290 return 0;
291 case SwapBehavior::Preserved:
292 return 1;
293 case SwapBehavior::BufferAge:
294 EGLint bufferAge;
295 eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge);
296 return bufferAge;
297 }
298 return 0;
299}
300
301Frame EglManager::beginFrame(EGLSurface surface) {
John Reck3b202512014-06-23 13:13:08 -0700302 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE,
303 "Tried to beginFrame on EGL_NO_SURFACE!");
304 makeCurrent(surface);
John Reck149173d2015-08-10 09:52:29 -0700305 Frame frame;
306 frame.mSurface = surface;
307 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth);
308 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight);
309 frame.mBufferAge = queryBufferAge(surface);
John Reck3b202512014-06-23 13:13:08 -0700310 eglBeginFrame(mEglDisplay, surface);
John Reck149173d2015-08-10 09:52:29 -0700311 return frame;
John Reck3b202512014-06-23 13:13:08 -0700312}
313
John Reck149173d2015-08-10 09:52:29 -0700314void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) {
315#ifdef EGL_KHR_partial_update
316 if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) {
317 EGLint rects[4];
318 frame.map(dirty, rects);
319 if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) {
320 LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s",
321 (void*)frame.mSurface, egl_error_str());
322 }
323 }
324#endif
325}
326
327bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) {
John Reck55156372015-01-21 07:46:37 -0800328
John Reck682573c2015-10-30 10:37:35 -0700329 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
John Reck55156372015-01-21 07:46:37 -0800330 ATRACE_NAME("Finishing GPU work");
331 fence();
332 }
John Reck55156372015-01-21 07:46:37 -0800333
John Recka672f6b2015-10-22 09:53:26 -0700334 EGLint rects[4];
335 frame.map(screenDirty, rects);
336 eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects,
337 screenDirty.isEmpty() ? 0 : 1);
John Reckd04794a2015-05-08 10:04:36 -0700338
John Reck3b202512014-06-23 13:13:08 -0700339 EGLint err = eglGetError();
John Reck2cdbc7d2014-09-17 16:06:36 -0700340 if (CC_LIKELY(err == EGL_SUCCESS)) {
341 return true;
342 }
John Reckc2547fa2015-10-26 13:52:52 -0700343 if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) {
John Reck2cdbc7d2014-09-17 16:06:36 -0700344 // For some reason our surface was destroyed out from under us
345 // This really shouldn't happen, but if it does we can recover easily
346 // by just not trying to use the surface anymore
John Reckd1ddcf12016-02-05 15:59:55 -0800347 ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...",
348 err, frame.mSurface);
John Reck2cdbc7d2014-09-17 16:06:36 -0700349 return false;
350 }
351 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering",
352 err, egl_error_str(err));
353 // Impossible to hit this, but the compiler doesn't know that
354 return false;
John Reck3b202512014-06-23 13:13:08 -0700355}
356
John Reck55156372015-01-21 07:46:37 -0800357void EglManager::fence() {
358 EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL);
359 eglClientWaitSyncKHR(mEglDisplay, fence,
360 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
361 eglDestroySyncKHR(mEglDisplay, fence);
362}
363
John Reck1125d1f2014-10-23 11:02:19 -0700364bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
John Reck149173d2015-08-10 09:52:29 -0700365 if (mSwapBehavior != SwapBehavior::Preserved) return false;
John Reck3b202512014-06-23 13:13:08 -0700366
John Reck149173d2015-08-10 09:52:29 -0700367 bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
368 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED);
369 if (!preserved) {
370 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s",
371 (void*) surface, egl_error_str());
John Reck1125d1f2014-10-23 11:02:19 -0700372 // Maybe it's already set?
373 EGLint swapBehavior;
374 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) {
375 preserved = (swapBehavior == EGL_BUFFER_PRESERVED);
376 } else {
377 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p",
378 (void*) surface, egl_error_str());
379 }
John Reck3b202512014-06-23 13:13:08 -0700380 }
John Reck1125d1f2014-10-23 11:02:19 -0700381
382 return preserved;
John Reck3b202512014-06-23 13:13:08 -0700383}
384
John Reck3b202512014-06-23 13:13:08 -0700385} /* namespace renderthread */
386} /* namespace uirenderer */
387} /* namespace android */