blob: ff9a252f2e3e3e9ebcd76e6a9c18e611bc621948 [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
19#include <log/log.h>
Peiyong Linefefaac2018-08-17 12:27:51 -070020#include <ui/PixelFormat.h>
Peiyong Linf1bada92018-08-29 09:39:31 -070021#include "GLES20RenderEngine.h"
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080022
23namespace android {
Peiyong Lin833074a2018-08-28 11:53:54 -070024namespace renderengine {
Peiyong Linf1bada92018-08-29 09:39:31 -070025namespace gl {
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080026
Peiyong Linf1bada92018-08-29 09:39:31 -070027GLSurface::GLSurface(const GLES20RenderEngine& engine)
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080028 : mEGLDisplay(engine.getEGLDisplay()), mEGLConfig(engine.getEGLConfig()) {
29 // RE does not assume any config when EGL_KHR_no_config_context is supported
30 if (mEGLConfig == EGL_NO_CONFIG_KHR) {
Peiyong Linf1bada92018-08-29 09:39:31 -070031 mEGLConfig = GLES20RenderEngine::chooseEglConfig(mEGLDisplay,
32 PIXEL_FORMAT_RGBA_8888, false);
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080033 }
34}
35
Peiyong Linf1bada92018-08-29 09:39:31 -070036GLSurface::~GLSurface() {
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080037 setNativeWindow(nullptr);
38}
39
Peiyong Linf1bada92018-08-29 09:39:31 -070040void GLSurface::setNativeWindow(ANativeWindow* window) {
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080041 if (mEGLSurface != EGL_NO_SURFACE) {
42 eglDestroySurface(mEGLDisplay, mEGLSurface);
43 mEGLSurface = EGL_NO_SURFACE;
44 }
45
46 mWindow = window;
47 if (mWindow) {
48 mEGLSurface = eglCreateWindowSurface(mEGLDisplay, mEGLConfig, mWindow, nullptr);
49 }
50}
51
Peiyong Linf1bada92018-08-29 09:39:31 -070052void GLSurface::swapBuffers() const {
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080053 if (!eglSwapBuffers(mEGLDisplay, mEGLSurface)) {
54 EGLint error = eglGetError();
55
56 const char format[] = "eglSwapBuffers(%p, %p) failed with 0x%08x";
57 if (mCritical || error == EGL_CONTEXT_LOST) {
58 LOG_ALWAYS_FATAL(format, mEGLDisplay, mEGLSurface, error);
59 } else {
60 ALOGE(format, mEGLDisplay, mEGLSurface, error);
61 }
62 }
63}
64
Peiyong Linf1bada92018-08-29 09:39:31 -070065EGLint GLSurface::queryConfig(EGLint attrib) const {
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080066 EGLint value;
Chia-I Wu1e043612018-03-01 09:45:09 -080067 if (!eglGetConfigAttrib(mEGLDisplay, mEGLConfig, attrib, &value)) {
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080068 value = 0;
69 }
70
71 return value;
72}
73
Peiyong Linf1bada92018-08-29 09:39:31 -070074EGLint GLSurface::querySurface(EGLint attrib) const {
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080075 EGLint value;
76 if (!eglQuerySurface(mEGLDisplay, mEGLSurface, attrib, &value)) {
77 value = 0;
78 }
79
80 return value;
81}
82
Peiyong Linf1bada92018-08-29 09:39:31 -070083int32_t GLSurface::queryRedSize() const {
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080084 return queryConfig(EGL_RED_SIZE);
85}
86
Peiyong Linf1bada92018-08-29 09:39:31 -070087int32_t GLSurface::queryGreenSize() const {
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080088 return queryConfig(EGL_GREEN_SIZE);
89}
90
Peiyong Linf1bada92018-08-29 09:39:31 -070091int32_t GLSurface::queryBlueSize() const {
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080092 return queryConfig(EGL_BLUE_SIZE);
93}
94
Peiyong Linf1bada92018-08-29 09:39:31 -070095int32_t GLSurface::queryAlphaSize() const {
Chia-I Wu7e60ecc2017-11-09 11:04:45 -080096 return queryConfig(EGL_ALPHA_SIZE);
97}
98
Peiyong Linf1bada92018-08-29 09:39:31 -070099int32_t GLSurface::queryWidth() const {
Chia-I Wu7e60ecc2017-11-09 11:04:45 -0800100 return querySurface(EGL_WIDTH);
101}
102
Peiyong Linf1bada92018-08-29 09:39:31 -0700103int32_t GLSurface::queryHeight() const {
Chia-I Wu7e60ecc2017-11-09 11:04:45 -0800104 return querySurface(EGL_HEIGHT);
105}
106
Peiyong Linf1bada92018-08-29 09:39:31 -0700107} // namespace gl
Peiyong Lin833074a2018-08-28 11:53:54 -0700108} // namespace renderengine
109} // namespace android