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