blob: 6255f5ef5b147f773ec494f7d231324b3eded68b [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 Reck3b202512014-06-23 13:13:08 -070028#define PROPERTY_RENDER_DIRTY_REGIONS "debug.hwui.render_dirty_regions"
29#define GLES_VERSION 2
30
John Reck55156372015-01-21 07:46:37 -080031#define WAIT_FOR_GPU_COMPLETION 0
32
John Reck3b202512014-06-23 13:13:08 -070033// 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}
62static const char* egl_error_str() {
63 return egl_error_str(eglGetError());
64}
65
66static bool load_dirty_regions_property() {
67 char buf[PROPERTY_VALUE_MAX];
68 int len = property_get(PROPERTY_RENDER_DIRTY_REGIONS, buf, "true");
69 return !strncasecmp("true", buf, len);
70}
71
72EglManager::EglManager(RenderThread& thread)
73 : mRenderThread(thread)
74 , mEglDisplay(EGL_NO_DISPLAY)
Chris Craikd41c4d82015-01-05 15:51:13 -080075 , mEglConfig(nullptr)
John Reck3b202512014-06-23 13:13:08 -070076 , mEglContext(EGL_NO_CONTEXT)
77 , mPBufferSurface(EGL_NO_SURFACE)
John Reck1125d1f2014-10-23 11:02:19 -070078 , mAllowPreserveBuffer(load_dirty_regions_property())
John Reck3b202512014-06-23 13:13:08 -070079 , mCurrentSurface(EGL_NO_SURFACE)
Chris Craikd41c4d82015-01-05 15:51:13 -080080 , mAtlasMap(nullptr)
John Reck0e89e2b2014-10-31 14:49:06 -070081 , mAtlasMapSize(0)
82 , mInFrame(false) {
John Reck1125d1f2014-10-23 11:02:19 -070083 mCanSetPreserveBuffer = mAllowPreserveBuffer;
84 ALOGD("Use EGL_SWAP_BEHAVIOR_PRESERVED: %s", mAllowPreserveBuffer ? "true" : "false");
John Reck3b202512014-06-23 13:13:08 -070085}
86
87void EglManager::initialize() {
88 if (hasEglContext()) return;
89
John Reckfbc8df02014-11-14 16:18:41 -080090 ATRACE_NAME("Creating EGLContext");
91
John Reck3b202512014-06-23 13:13:08 -070092 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
93 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
94 "Failed to get EGL_DEFAULT_DISPLAY! err=%s", egl_error_str());
95
96 EGLint major, minor;
97 LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE,
98 "Failed to initialize display %p! err=%s", mEglDisplay, egl_error_str());
99
100 ALOGI("Initialized EGL, version %d.%d", (int)major, (int)minor);
101
102 loadConfig();
103 createContext();
104 usePBufferSurface();
105 mRenderThread.renderState().onGLContextCreated();
106 initAtlas();
107}
108
109bool EglManager::hasEglContext() {
110 return mEglDisplay != EGL_NO_DISPLAY;
111}
112
113void EglManager::requireGlContext() {
114 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, "No EGL context");
115
John Reck0e89e2b2014-10-31 14:49:06 -0700116 if (!mInFrame) {
117 // We can't be certain about the state of the current surface (whether
118 // or not it is destroyed, for example), so err on the side of using
119 // the pbuffer surface which we fully control
120 usePBufferSurface();
121 }
John Reck3b202512014-06-23 13:13:08 -0700122}
123
124void EglManager::loadConfig() {
John Reck1125d1f2014-10-23 11:02:19 -0700125 EGLint swapBehavior = mCanSetPreserveBuffer ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
John Reck3b202512014-06-23 13:13:08 -0700126 EGLint attribs[] = {
127 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
128 EGL_RED_SIZE, 8,
129 EGL_GREEN_SIZE, 8,
130 EGL_BLUE_SIZE, 8,
131 EGL_ALPHA_SIZE, 8,
132 EGL_DEPTH_SIZE, 0,
133 EGL_CONFIG_CAVEAT, EGL_NONE,
134 EGL_STENCIL_SIZE, Stencil::getStencilSize(),
135 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | swapBehavior,
136 EGL_NONE
137 };
138
139 EGLint num_configs = 1;
140 if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, num_configs, &num_configs)
141 || num_configs != 1) {
142 // Failed to get a valid config
John Reck1125d1f2014-10-23 11:02:19 -0700143 if (mCanSetPreserveBuffer) {
John Reck3b202512014-06-23 13:13:08 -0700144 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
145 // Try again without dirty regions enabled
John Reck1125d1f2014-10-23 11:02:19 -0700146 mCanSetPreserveBuffer = false;
John Reck3b202512014-06-23 13:13:08 -0700147 loadConfig();
148 } else {
149 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", egl_error_str());
150 }
151 }
152}
153
154void EglManager::createContext() {
155 EGLint attribs[] = { EGL_CONTEXT_CLIENT_VERSION, GLES_VERSION, EGL_NONE };
156 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, attribs);
157 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT,
158 "Failed to create context, error = %s", egl_error_str());
159}
160
161void EglManager::setTextureAtlas(const sp<GraphicBuffer>& buffer,
162 int64_t* map, size_t mapSize) {
163
164 // Already initialized
165 if (mAtlasBuffer.get()) {
166 ALOGW("Multiple calls to setTextureAtlas!");
167 delete map;
168 return;
169 }
170
171 mAtlasBuffer = buffer;
172 mAtlasMap = map;
173 mAtlasMapSize = mapSize;
174
175 if (hasEglContext()) {
176 usePBufferSurface();
177 initAtlas();
178 }
179}
180
181void EglManager::initAtlas() {
182 if (mAtlasBuffer.get()) {
John Reckebd52612014-12-10 16:47:36 -0800183 mRenderThread.renderState().assetAtlas().init(mAtlasBuffer,
184 mAtlasMap, mAtlasMapSize);
John Reck3b202512014-06-23 13:13:08 -0700185 }
186}
187
188void EglManager::usePBufferSurface() {
189 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
190 "usePBufferSurface() called on uninitialized GlobalContext!");
191
192 if (mPBufferSurface == EGL_NO_SURFACE) {
193 EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
194 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs);
195 }
196 makeCurrent(mPBufferSurface);
197}
198
199EGLSurface EglManager::createSurface(EGLNativeWindowType window) {
200 initialize();
Chris Craikd41c4d82015-01-05 15:51:13 -0800201 EGLSurface surface = eglCreateWindowSurface(mEglDisplay, mEglConfig, window, nullptr);
John Reck3b202512014-06-23 13:13:08 -0700202 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE,
203 "Failed to create EGLSurface for window %p, eglErr = %s",
204 (void*) window, egl_error_str());
205 return surface;
206}
207
208void EglManager::destroySurface(EGLSurface surface) {
209 if (isCurrent(surface)) {
210 makeCurrent(EGL_NO_SURFACE);
211 }
212 if (!eglDestroySurface(mEglDisplay, surface)) {
213 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, egl_error_str());
214 }
215}
216
217void EglManager::destroy() {
218 if (mEglDisplay == EGL_NO_DISPLAY) return;
219
220 usePBufferSurface();
John Reck3b202512014-06-23 13:13:08 -0700221
Chris Craik1d477422014-08-26 17:30:15 -0700222 mRenderThread.renderState().onGLContextDestroyed();
John Reck3b202512014-06-23 13:13:08 -0700223 eglDestroyContext(mEglDisplay, mEglContext);
224 eglDestroySurface(mEglDisplay, mPBufferSurface);
225 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
226 eglTerminate(mEglDisplay);
227 eglReleaseThread();
228
229 mEglDisplay = EGL_NO_DISPLAY;
230 mEglContext = EGL_NO_CONTEXT;
231 mPBufferSurface = EGL_NO_SURFACE;
232 mCurrentSurface = EGL_NO_SURFACE;
233}
234
235bool EglManager::makeCurrent(EGLSurface surface) {
236 if (isCurrent(surface)) return false;
237
238 if (surface == EGL_NO_SURFACE) {
239 // If we are setting EGL_NO_SURFACE we don't care about any of the potential
240 // return errors, which would only happen if mEglDisplay had already been
241 // destroyed in which case the current context is already NO_CONTEXT
242 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
243 } else if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
244 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s",
245 (void*)surface, egl_error_str());
246 }
247 mCurrentSurface = surface;
248 return true;
249}
250
251void EglManager::beginFrame(EGLSurface surface, EGLint* width, EGLint* height) {
252 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE,
253 "Tried to beginFrame on EGL_NO_SURFACE!");
254 makeCurrent(surface);
255 if (width) {
256 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, width);
257 }
258 if (height) {
259 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, height);
260 }
261 eglBeginFrame(mEglDisplay, surface);
John Reck0e89e2b2014-10-31 14:49:06 -0700262 mInFrame = true;
John Reck3b202512014-06-23 13:13:08 -0700263}
264
John Reckd04794a2015-05-08 10:04:36 -0700265bool EglManager::swapBuffers(EGLSurface surface, const SkRect& dirty,
266 EGLint width, EGLint height) {
John Reck0e89e2b2014-10-31 14:49:06 -0700267 mInFrame = false;
John Reck55156372015-01-21 07:46:37 -0800268
269#if WAIT_FOR_GPU_COMPLETION
270 {
271 ATRACE_NAME("Finishing GPU work");
272 fence();
273 }
274#endif
275
John Reckd04794a2015-05-08 10:04:36 -0700276#ifdef EGL_KHR_swap_buffers_with_damage
277 if (CC_UNLIKELY(Properties::swapBuffersWithDamage)) {
278 SkIRect idirty;
279 dirty.roundOut(&idirty);
280 /*
281 * EGL_KHR_swap_buffers_with_damage spec states:
282 *
283 * The rectangles are specified relative to the bottom-left of the surface
284 * and the x and y components of each rectangle specify the bottom-left
285 * position of that rectangle.
286 *
287 * HWUI does everything with 0,0 being top-left, so need to map
288 * the rect
289 */
290 EGLint y = height - (idirty.y() + idirty.height());
291 // layout: {x, y, width, height}
292 EGLint rects[4] = { idirty.x(), y, idirty.width(), idirty.height() };
293 EGLint numrects = dirty.isEmpty() ? 0 : 1;
294 // TODO: Remove prior to enabling this path by default
295 ALOGD("Swap buffers with damage %d: %d, %d, %d, %d (src="
296 RECT_STRING ")",
297 dirty.isEmpty() ? 0 : 1, rects[0], rects[1], rects[2], rects[3],
298 SK_RECT_ARGS(dirty));
299 eglSwapBuffersWithDamageKHR(mEglDisplay, surface, rects, numrects);
300 } else {
301 eglSwapBuffers(mEglDisplay, surface);
302 }
303#else
John Reck3b202512014-06-23 13:13:08 -0700304 eglSwapBuffers(mEglDisplay, surface);
John Reckd04794a2015-05-08 10:04:36 -0700305#endif
306
John Reck3b202512014-06-23 13:13:08 -0700307 EGLint err = eglGetError();
John Reck2cdbc7d2014-09-17 16:06:36 -0700308 if (CC_LIKELY(err == EGL_SUCCESS)) {
309 return true;
310 }
311 if (err == EGL_BAD_SURFACE) {
312 // For some reason our surface was destroyed out from under us
313 // This really shouldn't happen, but if it does we can recover easily
314 // by just not trying to use the surface anymore
315 ALOGW("swapBuffers encountered EGL_BAD_SURFACE on %p, halting rendering...", surface);
316 return false;
317 }
318 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering",
319 err, egl_error_str(err));
320 // Impossible to hit this, but the compiler doesn't know that
321 return false;
John Reck3b202512014-06-23 13:13:08 -0700322}
323
John Reck55156372015-01-21 07:46:37 -0800324void EglManager::fence() {
325 EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL);
326 eglClientWaitSyncKHR(mEglDisplay, fence,
327 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
328 eglDestroySyncKHR(mEglDisplay, fence);
329}
330
John Reck0e89e2b2014-10-31 14:49:06 -0700331void EglManager::cancelFrame() {
332 mInFrame = false;
333}
334
John Reck1125d1f2014-10-23 11:02:19 -0700335bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
336 if (CC_UNLIKELY(!mAllowPreserveBuffer)) return false;
John Reck3b202512014-06-23 13:13:08 -0700337
John Reck1125d1f2014-10-23 11:02:19 -0700338 bool preserved = false;
339 if (mCanSetPreserveBuffer) {
340 preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
341 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED);
342 if (CC_UNLIKELY(!preserved)) {
John Reck3b202512014-06-23 13:13:08 -0700343 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s",
344 (void*) surface, egl_error_str());
John Reck3b202512014-06-23 13:13:08 -0700345 }
John Reck3b202512014-06-23 13:13:08 -0700346 }
John Reck1125d1f2014-10-23 11:02:19 -0700347 if (CC_UNLIKELY(!preserved)) {
348 // Maybe it's already set?
349 EGLint swapBehavior;
350 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) {
351 preserved = (swapBehavior == EGL_BUFFER_PRESERVED);
352 } else {
353 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p",
354 (void*) surface, egl_error_str());
355 }
John Reck3b202512014-06-23 13:13:08 -0700356 }
John Reck1125d1f2014-10-23 11:02:19 -0700357
358 return preserved;
John Reck3b202512014-06-23 13:13:08 -0700359}
360
361} /* namespace renderthread */
362} /* namespace uirenderer */
363} /* namespace android */