blob: 37f8e607467a7ca8676d689139cf9837fca20408 [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
17#define LOG_TAG "EglContext"
18
19#include "EglManager.h"
20
21#include <cutils/log.h>
22#include <cutils/properties.h>
23
24#include "../RenderState.h"
25#include "RenderThread.h"
26
27#define PROPERTY_RENDER_DIRTY_REGIONS "debug.hwui.render_dirty_regions"
28#define GLES_VERSION 2
29
30// Android-specific addition that is used to show when frames began in systrace
31EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface);
32
33namespace android {
34namespace uirenderer {
35namespace renderthread {
36
37#define ERROR_CASE(x) case x: return #x;
38static const char* egl_error_str(EGLint error) {
39 switch (error) {
40 ERROR_CASE(EGL_SUCCESS)
41 ERROR_CASE(EGL_NOT_INITIALIZED)
42 ERROR_CASE(EGL_BAD_ACCESS)
43 ERROR_CASE(EGL_BAD_ALLOC)
44 ERROR_CASE(EGL_BAD_ATTRIBUTE)
45 ERROR_CASE(EGL_BAD_CONFIG)
46 ERROR_CASE(EGL_BAD_CONTEXT)
47 ERROR_CASE(EGL_BAD_CURRENT_SURFACE)
48 ERROR_CASE(EGL_BAD_DISPLAY)
49 ERROR_CASE(EGL_BAD_MATCH)
50 ERROR_CASE(EGL_BAD_NATIVE_PIXMAP)
51 ERROR_CASE(EGL_BAD_NATIVE_WINDOW)
52 ERROR_CASE(EGL_BAD_PARAMETER)
53 ERROR_CASE(EGL_BAD_SURFACE)
54 ERROR_CASE(EGL_CONTEXT_LOST)
55 default:
56 return "Unknown error";
57 }
58}
59static const char* egl_error_str() {
60 return egl_error_str(eglGetError());
61}
62
63static bool load_dirty_regions_property() {
64 char buf[PROPERTY_VALUE_MAX];
65 int len = property_get(PROPERTY_RENDER_DIRTY_REGIONS, buf, "true");
66 return !strncasecmp("true", buf, len);
67}
68
69EglManager::EglManager(RenderThread& thread)
70 : mRenderThread(thread)
71 , mEglDisplay(EGL_NO_DISPLAY)
72 , mEglConfig(0)
73 , mEglContext(EGL_NO_CONTEXT)
74 , mPBufferSurface(EGL_NO_SURFACE)
75 , mRequestDirtyRegions(load_dirty_regions_property())
76 , mCurrentSurface(EGL_NO_SURFACE)
77 , mAtlasMap(NULL)
78 , mAtlasMapSize(0) {
79 mCanSetDirtyRegions = mRequestDirtyRegions;
80 ALOGD("Render dirty regions requested: %s", mRequestDirtyRegions ? "true" : "false");
81}
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
110 // We don't care *WHAT* surface is active, just that one is active to give
111 // us access to the GL context
112 if (mCurrentSurface == EGL_NO_SURFACE) {
113 usePBufferSurface();
114 }
115}
116
117void EglManager::loadConfig() {
118 EGLint swapBehavior = mCanSetDirtyRegions ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
119 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
136 if (mCanSetDirtyRegions) {
137 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
138 // Try again without dirty regions enabled
139 mCanSetDirtyRegions = false;
140 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);
257}
258
259void EglManager::swapBuffers(EGLSurface surface) {
260 eglSwapBuffers(mEglDisplay, surface);
261 EGLint err = eglGetError();
262 LOG_ALWAYS_FATAL_IF(err != EGL_SUCCESS,
263 "Encountered EGL error %d %s during rendering", err, egl_error_str(err));
264}
265
266bool EglManager::enableDirtyRegions(EGLSurface surface) {
267 if (!mRequestDirtyRegions) return false;
268
269 if (mCanSetDirtyRegions) {
270 if (!eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, EGL_BUFFER_PRESERVED)) {
271 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s",
272 (void*) surface, egl_error_str());
273 return false;
274 }
275 return true;
276 }
277 // Perhaps it is already enabled?
278 EGLint value;
279 if (!eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &value)) {
280 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p",
281 (void*) surface, egl_error_str());
282 return false;
283 }
284 return value == EGL_BUFFER_PRESERVED;
285}
286
287} /* namespace renderthread */
288} /* namespace uirenderer */
289} /* namespace android */