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