blob: e49863de62c58c9068f499fd356828bd9790e44b [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
19#include <cutils/log.h>
20#include <cutils/properties.h>
21
Tom Hudson2dc236b2014-10-15 15:46:42 -040022#include "../Caches.h"
John Reck3b202512014-06-23 13:13:08 -070023#include "../RenderState.h"
24#include "RenderThread.h"
25
26#define PROPERTY_RENDER_DIRTY_REGIONS "debug.hwui.render_dirty_regions"
27#define GLES_VERSION 2
28
29// Android-specific addition that is used to show when frames began in systrace
30EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface);
31
32namespace android {
33namespace uirenderer {
34namespace renderthread {
35
36#define ERROR_CASE(x) case x: return #x;
37static const char* egl_error_str(EGLint error) {
38 switch (error) {
39 ERROR_CASE(EGL_SUCCESS)
40 ERROR_CASE(EGL_NOT_INITIALIZED)
41 ERROR_CASE(EGL_BAD_ACCESS)
42 ERROR_CASE(EGL_BAD_ALLOC)
43 ERROR_CASE(EGL_BAD_ATTRIBUTE)
44 ERROR_CASE(EGL_BAD_CONFIG)
45 ERROR_CASE(EGL_BAD_CONTEXT)
46 ERROR_CASE(EGL_BAD_CURRENT_SURFACE)
47 ERROR_CASE(EGL_BAD_DISPLAY)
48 ERROR_CASE(EGL_BAD_MATCH)
49 ERROR_CASE(EGL_BAD_NATIVE_PIXMAP)
50 ERROR_CASE(EGL_BAD_NATIVE_WINDOW)
51 ERROR_CASE(EGL_BAD_PARAMETER)
52 ERROR_CASE(EGL_BAD_SURFACE)
53 ERROR_CASE(EGL_CONTEXT_LOST)
54 default:
55 return "Unknown error";
56 }
57}
58static const char* egl_error_str() {
59 return egl_error_str(eglGetError());
60}
61
62static bool load_dirty_regions_property() {
63 char buf[PROPERTY_VALUE_MAX];
64 int len = property_get(PROPERTY_RENDER_DIRTY_REGIONS, buf, "true");
65 return !strncasecmp("true", buf, len);
66}
67
68EglManager::EglManager(RenderThread& thread)
69 : mRenderThread(thread)
70 , mEglDisplay(EGL_NO_DISPLAY)
71 , mEglConfig(0)
72 , mEglContext(EGL_NO_CONTEXT)
73 , mPBufferSurface(EGL_NO_SURFACE)
John Reck1125d1f2014-10-23 11:02:19 -070074 , mAllowPreserveBuffer(load_dirty_regions_property())
John Reck3b202512014-06-23 13:13:08 -070075 , mCurrentSurface(EGL_NO_SURFACE)
76 , mAtlasMap(NULL)
John Reck0e89e2b2014-10-31 14:49:06 -070077 , mAtlasMapSize(0)
78 , mInFrame(false) {
John Reck1125d1f2014-10-23 11:02:19 -070079 mCanSetPreserveBuffer = mAllowPreserveBuffer;
80 ALOGD("Use EGL_SWAP_BEHAVIOR_PRESERVED: %s", mAllowPreserveBuffer ? "true" : "false");
John Reck3b202512014-06-23 13:13:08 -070081}
82
83void EglManager::initialize() {
84 if (hasEglContext()) return;
85
86 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
87 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
88 "Failed to get EGL_DEFAULT_DISPLAY! err=%s", egl_error_str());
89
90 EGLint major, minor;
91 LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE,
92 "Failed to initialize display %p! err=%s", mEglDisplay, egl_error_str());
93
94 ALOGI("Initialized EGL, version %d.%d", (int)major, (int)minor);
95
96 loadConfig();
97 createContext();
98 usePBufferSurface();
99 mRenderThread.renderState().onGLContextCreated();
100 initAtlas();
101}
102
103bool EglManager::hasEglContext() {
104 return mEglDisplay != EGL_NO_DISPLAY;
105}
106
107void EglManager::requireGlContext() {
108 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, "No EGL context");
109
John Reck0e89e2b2014-10-31 14:49:06 -0700110 if (!mInFrame) {
111 // We can't be certain about the state of the current surface (whether
112 // or not it is destroyed, for example), so err on the side of using
113 // the pbuffer surface which we fully control
114 usePBufferSurface();
115 }
John Reck3b202512014-06-23 13:13:08 -0700116}
117
118void EglManager::loadConfig() {
John Reck1125d1f2014-10-23 11:02:19 -0700119 EGLint swapBehavior = mCanSetPreserveBuffer ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
John Reck3b202512014-06-23 13:13:08 -0700120 EGLint attribs[] = {
121 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
122 EGL_RED_SIZE, 8,
123 EGL_GREEN_SIZE, 8,
124 EGL_BLUE_SIZE, 8,
125 EGL_ALPHA_SIZE, 8,
126 EGL_DEPTH_SIZE, 0,
127 EGL_CONFIG_CAVEAT, EGL_NONE,
128 EGL_STENCIL_SIZE, Stencil::getStencilSize(),
129 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | swapBehavior,
130 EGL_NONE
131 };
132
133 EGLint num_configs = 1;
134 if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, num_configs, &num_configs)
135 || num_configs != 1) {
136 // Failed to get a valid config
John Reck1125d1f2014-10-23 11:02:19 -0700137 if (mCanSetPreserveBuffer) {
John Reck3b202512014-06-23 13:13:08 -0700138 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
139 // Try again without dirty regions enabled
John Reck1125d1f2014-10-23 11:02:19 -0700140 mCanSetPreserveBuffer = false;
John Reck3b202512014-06-23 13:13:08 -0700141 loadConfig();
142 } else {
143 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", egl_error_str());
144 }
145 }
146}
147
148void EglManager::createContext() {
149 EGLint attribs[] = { EGL_CONTEXT_CLIENT_VERSION, GLES_VERSION, EGL_NONE };
150 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, attribs);
151 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT,
152 "Failed to create context, error = %s", egl_error_str());
153}
154
155void EglManager::setTextureAtlas(const sp<GraphicBuffer>& buffer,
156 int64_t* map, size_t mapSize) {
157
158 // Already initialized
159 if (mAtlasBuffer.get()) {
160 ALOGW("Multiple calls to setTextureAtlas!");
161 delete map;
162 return;
163 }
164
165 mAtlasBuffer = buffer;
166 mAtlasMap = map;
167 mAtlasMapSize = mapSize;
168
169 if (hasEglContext()) {
170 usePBufferSurface();
171 initAtlas();
172 }
173}
174
175void EglManager::initAtlas() {
176 if (mAtlasBuffer.get()) {
177 Caches::getInstance().assetAtlas.init(mAtlasBuffer, mAtlasMap, mAtlasMapSize);
178 }
179}
180
181void EglManager::usePBufferSurface() {
182 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
183 "usePBufferSurface() called on uninitialized GlobalContext!");
184
185 if (mPBufferSurface == EGL_NO_SURFACE) {
186 EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
187 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs);
188 }
189 makeCurrent(mPBufferSurface);
190}
191
192EGLSurface EglManager::createSurface(EGLNativeWindowType window) {
193 initialize();
194 EGLSurface surface = eglCreateWindowSurface(mEglDisplay, mEglConfig, window, NULL);
195 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE,
196 "Failed to create EGLSurface for window %p, eglErr = %s",
197 (void*) window, egl_error_str());
198 return surface;
199}
200
201void EglManager::destroySurface(EGLSurface surface) {
202 if (isCurrent(surface)) {
203 makeCurrent(EGL_NO_SURFACE);
204 }
205 if (!eglDestroySurface(mEglDisplay, surface)) {
206 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, egl_error_str());
207 }
208}
209
210void EglManager::destroy() {
211 if (mEglDisplay == EGL_NO_DISPLAY) return;
212
213 usePBufferSurface();
214 if (Caches::hasInstance()) {
215 Caches::getInstance().terminate();
216 }
217
Chris Craik1d477422014-08-26 17:30:15 -0700218 mRenderThread.renderState().onGLContextDestroyed();
John Reck3b202512014-06-23 13:13:08 -0700219 eglDestroyContext(mEglDisplay, mEglContext);
220 eglDestroySurface(mEglDisplay, mPBufferSurface);
221 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
222 eglTerminate(mEglDisplay);
223 eglReleaseThread();
224
225 mEglDisplay = EGL_NO_DISPLAY;
226 mEglContext = EGL_NO_CONTEXT;
227 mPBufferSurface = EGL_NO_SURFACE;
228 mCurrentSurface = EGL_NO_SURFACE;
229}
230
231bool EglManager::makeCurrent(EGLSurface surface) {
232 if (isCurrent(surface)) return false;
233
234 if (surface == EGL_NO_SURFACE) {
235 // If we are setting EGL_NO_SURFACE we don't care about any of the potential
236 // return errors, which would only happen if mEglDisplay had already been
237 // destroyed in which case the current context is already NO_CONTEXT
238 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
239 } else if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
240 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s",
241 (void*)surface, egl_error_str());
242 }
243 mCurrentSurface = surface;
244 return true;
245}
246
247void EglManager::beginFrame(EGLSurface surface, EGLint* width, EGLint* height) {
248 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE,
249 "Tried to beginFrame on EGL_NO_SURFACE!");
250 makeCurrent(surface);
251 if (width) {
252 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, width);
253 }
254 if (height) {
255 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, height);
256 }
257 eglBeginFrame(mEglDisplay, surface);
John Reck0e89e2b2014-10-31 14:49:06 -0700258 mInFrame = true;
John Reck3b202512014-06-23 13:13:08 -0700259}
260
John Reck2cdbc7d2014-09-17 16:06:36 -0700261bool EglManager::swapBuffers(EGLSurface surface) {
John Reck0e89e2b2014-10-31 14:49:06 -0700262 mInFrame = false;
John Reck3b202512014-06-23 13:13:08 -0700263 eglSwapBuffers(mEglDisplay, surface);
264 EGLint err = eglGetError();
John Reck2cdbc7d2014-09-17 16:06:36 -0700265 if (CC_LIKELY(err == EGL_SUCCESS)) {
266 return true;
267 }
268 if (err == EGL_BAD_SURFACE) {
269 // For some reason our surface was destroyed out from under us
270 // This really shouldn't happen, but if it does we can recover easily
271 // by just not trying to use the surface anymore
272 ALOGW("swapBuffers encountered EGL_BAD_SURFACE on %p, halting rendering...", surface);
273 return false;
274 }
275 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering",
276 err, egl_error_str(err));
277 // Impossible to hit this, but the compiler doesn't know that
278 return false;
John Reck3b202512014-06-23 13:13:08 -0700279}
280
John Reck0e89e2b2014-10-31 14:49:06 -0700281void EglManager::cancelFrame() {
282 mInFrame = false;
283}
284
John Reck1125d1f2014-10-23 11:02:19 -0700285bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
286 if (CC_UNLIKELY(!mAllowPreserveBuffer)) return false;
John Reck3b202512014-06-23 13:13:08 -0700287
John Reck1125d1f2014-10-23 11:02:19 -0700288 bool preserved = false;
289 if (mCanSetPreserveBuffer) {
290 preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
291 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED);
292 if (CC_UNLIKELY(!preserved)) {
John Reck3b202512014-06-23 13:13:08 -0700293 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s",
294 (void*) surface, egl_error_str());
John Reck3b202512014-06-23 13:13:08 -0700295 }
John Reck3b202512014-06-23 13:13:08 -0700296 }
John Reck1125d1f2014-10-23 11:02:19 -0700297 if (CC_UNLIKELY(!preserved)) {
298 // Maybe it's already set?
299 EGLint swapBehavior;
300 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) {
301 preserved = (swapBehavior == EGL_BUFFER_PRESERVED);
302 } else {
303 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p",
304 (void*) surface, egl_error_str());
305 }
John Reck3b202512014-06-23 13:13:08 -0700306 }
John Reck1125d1f2014-10-23 11:02:19 -0700307
308 return preserved;
John Reck3b202512014-06-23 13:13:08 -0700309}
310
311} /* namespace renderthread */
312} /* namespace uirenderer */
313} /* namespace android */