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