Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 1 | /* |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame^] | 2 | * Copyright 2018 The Android Open Source Project |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 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 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame^] | 17 | #include "GLSurface.h" |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 18 | |
| 19 | #include <log/log.h> |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 20 | #include <ui/PixelFormat.h> |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame^] | 21 | #include "GLES20RenderEngine.h" |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 22 | |
| 23 | namespace android { |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 24 | namespace renderengine { |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame^] | 25 | namespace gl { |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 26 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame^] | 27 | GLSurface::GLSurface(const GLES20RenderEngine& engine) |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 28 | : 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 Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame^] | 31 | mEGLConfig = GLES20RenderEngine::chooseEglConfig(mEGLDisplay, |
| 32 | PIXEL_FORMAT_RGBA_8888, false); |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame^] | 36 | GLSurface::~GLSurface() { |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 37 | setNativeWindow(nullptr); |
| 38 | } |
| 39 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame^] | 40 | void GLSurface::setNativeWindow(ANativeWindow* window) { |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 41 | 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 Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame^] | 52 | void GLSurface::swapBuffers() const { |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 53 | 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 Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame^] | 65 | EGLint GLSurface::queryConfig(EGLint attrib) const { |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 66 | EGLint value; |
Chia-I Wu | 1e04361 | 2018-03-01 09:45:09 -0800 | [diff] [blame] | 67 | if (!eglGetConfigAttrib(mEGLDisplay, mEGLConfig, attrib, &value)) { |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 68 | value = 0; |
| 69 | } |
| 70 | |
| 71 | return value; |
| 72 | } |
| 73 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame^] | 74 | EGLint GLSurface::querySurface(EGLint attrib) const { |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 75 | EGLint value; |
| 76 | if (!eglQuerySurface(mEGLDisplay, mEGLSurface, attrib, &value)) { |
| 77 | value = 0; |
| 78 | } |
| 79 | |
| 80 | return value; |
| 81 | } |
| 82 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame^] | 83 | int32_t GLSurface::queryRedSize() const { |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 84 | return queryConfig(EGL_RED_SIZE); |
| 85 | } |
| 86 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame^] | 87 | int32_t GLSurface::queryGreenSize() const { |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 88 | return queryConfig(EGL_GREEN_SIZE); |
| 89 | } |
| 90 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame^] | 91 | int32_t GLSurface::queryBlueSize() const { |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 92 | return queryConfig(EGL_BLUE_SIZE); |
| 93 | } |
| 94 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame^] | 95 | int32_t GLSurface::queryAlphaSize() const { |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 96 | return queryConfig(EGL_ALPHA_SIZE); |
| 97 | } |
| 98 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame^] | 99 | int32_t GLSurface::queryWidth() const { |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 100 | return querySurface(EGL_WIDTH); |
| 101 | } |
| 102 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame^] | 103 | int32_t GLSurface::queryHeight() const { |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 104 | return querySurface(EGL_HEIGHT); |
| 105 | } |
| 106 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame^] | 107 | } // namespace gl |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 108 | } // namespace renderengine |
| 109 | } // namespace android |