blob: 104af563b2d91ae785948c9d620fedfbdf3db284 [file] [log] [blame]
Chia-I Wu7e60ecc2017-11-09 11:04:45 -08001/*
Peiyong Linf1bada92018-08-29 09:39:31 -07002 * Copyright 2018 The Android Open Source Project
Chia-I Wu7e60ecc2017-11-09 11:04:45 -08003 *
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
Peiyong Linf1bada92018-08-29 09:39:31 -070017#include "GLSurface.h"
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080018
Alec Mouri05483a02018-09-10 21:03:42 +000019#include <android/native_window.h>
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080020#include <log/log.h>
Peiyong Linefefaac2018-08-17 12:27:51 -070021#include <ui/PixelFormat.h>
Peiyong Linf1bada92018-08-29 09:39:31 -070022#include "GLES20RenderEngine.h"
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080023
24namespace android {
Peiyong Lin833074a2018-08-28 11:53:54 -070025namespace renderengine {
Peiyong Linf1bada92018-08-29 09:39:31 -070026namespace gl {
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080027
Peiyong Linf1bada92018-08-29 09:39:31 -070028GLSurface::GLSurface(const GLES20RenderEngine& engine)
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080029 : 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) {
Peiyong Linf1bada92018-08-29 09:39:31 -070032 mEGLConfig = GLES20RenderEngine::chooseEglConfig(mEGLDisplay,
33 PIXEL_FORMAT_RGBA_8888, false);
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080034 }
35}
36
Peiyong Linf1bada92018-08-29 09:39:31 -070037GLSurface::~GLSurface() {
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080038 setNativeWindow(nullptr);
39}
40
Peiyong Linf1bada92018-08-29 09:39:31 -070041void GLSurface::setNativeWindow(ANativeWindow* window) {
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080042 if (mEGLSurface != EGL_NO_SURFACE) {
43 eglDestroySurface(mEGLDisplay, mEGLSurface);
44 mEGLSurface = EGL_NO_SURFACE;
Alec Mouri05483a02018-09-10 21:03:42 +000045 mSurfaceWidth = 0;
46 mSurfaceHeight = 0;
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080047 }
48
49 mWindow = window;
50 if (mWindow) {
51 mEGLSurface = eglCreateWindowSurface(mEGLDisplay, mEGLConfig, mWindow, nullptr);
Alec Mouri05483a02018-09-10 21:03:42 +000052 mSurfaceWidth = ANativeWindow_getWidth(window);
53 mSurfaceHeight = ANativeWindow_getHeight(window);
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080054 }
55}
56
Peiyong Linf1bada92018-08-29 09:39:31 -070057void GLSurface::swapBuffers() const {
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080058 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
Peiyong Linf1bada92018-08-29 09:39:31 -070070EGLint GLSurface::queryConfig(EGLint attrib) const {
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080071 EGLint value;
Chia-I Wu1e043612018-03-01 09:45:09 -080072 if (!eglGetConfigAttrib(mEGLDisplay, mEGLConfig, attrib, &value)) {
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080073 value = 0;
74 }
75
76 return value;
77}
78
Peiyong Linf1bada92018-08-29 09:39:31 -070079int32_t GLSurface::queryRedSize() const {
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080080 return queryConfig(EGL_RED_SIZE);
81}
82
Peiyong Linf1bada92018-08-29 09:39:31 -070083int32_t GLSurface::queryGreenSize() const {
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080084 return queryConfig(EGL_GREEN_SIZE);
85}
86
Peiyong Linf1bada92018-08-29 09:39:31 -070087int32_t GLSurface::queryBlueSize() const {
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080088 return queryConfig(EGL_BLUE_SIZE);
89}
90
Peiyong Linf1bada92018-08-29 09:39:31 -070091int32_t GLSurface::queryAlphaSize() const {
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080092 return queryConfig(EGL_ALPHA_SIZE);
93}
94
Alec Mouri05483a02018-09-10 21:03:42 +000095int32_t GLSurface::getWidth() const {
96 return mSurfaceWidth;
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080097}
98
Alec Mouri05483a02018-09-10 21:03:42 +000099int32_t GLSurface::getHeight() const {
100 return mSurfaceHeight;
Chia-I Wu7e60ecc2017-11-09 11:04:45 -0800101}
102
Peiyong Linf1bada92018-08-29 09:39:31 -0700103} // namespace gl
Peiyong Lin833074a2018-08-28 11:53:54 -0700104} // namespace renderengine
105} // namespace android