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