blob: 2d694e9124def4c89410d2500bf38c86b193bce9 [file] [log] [blame]
Alec Mourif1d19c72018-11-15 00:00:50 +00001/*
2 * Copyright 2018 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#include "GLSurface.h"
18
19#include <android/native_window.h>
20#include <log/log.h>
21#include <ui/PixelFormat.h>
22#include "GLES20RenderEngine.h"
23
24namespace android {
25namespace renderengine {
26namespace gl {
27
28GLSurface::GLSurface(const GLES20RenderEngine& engine)
29 : mEGLDisplay(engine.getEGLDisplay()), mEGLConfig(engine.getEGLConfig()) {
30 // RE does not assume any config when EGL_KHR_no_config_context is supported
31 if (mEGLConfig == EGL_NO_CONFIG_KHR) {
32 mEGLConfig =
33 GLES20RenderEngine::chooseEglConfig(mEGLDisplay, PIXEL_FORMAT_RGBA_8888, false);
34 }
35}
36
37GLSurface::~GLSurface() {
38 setNativeWindow(nullptr);
39}
40
41void GLSurface::setNativeWindow(ANativeWindow* window) {
42 if (mEGLSurface != EGL_NO_SURFACE) {
43 eglDestroySurface(mEGLDisplay, mEGLSurface);
44 mEGLSurface = EGL_NO_SURFACE;
45 mSurfaceWidth = 0;
46 mSurfaceHeight = 0;
47 }
48
49 mWindow = window;
50 if (mWindow) {
51 mEGLSurface = eglCreateWindowSurface(mEGLDisplay, mEGLConfig, mWindow, nullptr);
52 mSurfaceWidth = ANativeWindow_getWidth(window);
53 mSurfaceHeight = ANativeWindow_getHeight(window);
54 }
55}
56
57void GLSurface::swapBuffers() const {
58 if (!eglSwapBuffers(mEGLDisplay, mEGLSurface)) {
59 EGLint error = eglGetError();
60
61 const char format[] = "eglSwapBuffers(%p, %p) failed with 0x%08x";
62 if (mCritical || error == EGL_CONTEXT_LOST) {
63 LOG_ALWAYS_FATAL(format, mEGLDisplay, mEGLSurface, error);
64 } else {
65 ALOGE(format, mEGLDisplay, mEGLSurface, error);
66 }
67 }
68}
69
70EGLint GLSurface::queryConfig(EGLint attrib) const {
71 EGLint value;
72 if (!eglGetConfigAttrib(mEGLDisplay, mEGLConfig, attrib, &value)) {
73 value = 0;
74 }
75
76 return value;
77}
78
79int32_t GLSurface::queryRedSize() const {
80 return queryConfig(EGL_RED_SIZE);
81}
82
83int32_t GLSurface::queryGreenSize() const {
84 return queryConfig(EGL_GREEN_SIZE);
85}
86
87int32_t GLSurface::queryBlueSize() const {
88 return queryConfig(EGL_BLUE_SIZE);
89}
90
91int32_t GLSurface::queryAlphaSize() const {
92 return queryConfig(EGL_ALPHA_SIZE);
93}
94
95int32_t GLSurface::getWidth() const {
96 return mSurfaceWidth;
97}
98
99int32_t GLSurface::getHeight() const {
100 return mSurfaceHeight;
101}
102
103} // namespace gl
104} // namespace renderengine
105} // namespace android