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 | |
Alec Mouri | 05483a0 | 2018-09-10 21:03:42 +0000 | [diff] [blame] | 19 | #include <android/native_window.h> |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 20 | #include <log/log.h> |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 21 | #include <ui/PixelFormat.h> |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 22 | #include "GLES20RenderEngine.h" |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 23 | |
| 24 | namespace android { |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 25 | namespace renderengine { |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 26 | namespace gl { |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 27 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 28 | GLSurface::GLSurface(const GLES20RenderEngine& engine) |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 29 | : mEGLDisplay(engine.getEGLDisplay()), mEGLConfig(engine.getEGLConfig()) { |
| 30 | // RE does not assume any config when EGL_KHR_no_config_context is supported |
| 31 | if (mEGLConfig == EGL_NO_CONFIG_KHR) { |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 32 | mEGLConfig = GLES20RenderEngine::chooseEglConfig(mEGLDisplay, |
| 33 | PIXEL_FORMAT_RGBA_8888, false); |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 37 | GLSurface::~GLSurface() { |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 38 | setNativeWindow(nullptr); |
| 39 | } |
| 40 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 41 | void GLSurface::setNativeWindow(ANativeWindow* window) { |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 42 | if (mEGLSurface != EGL_NO_SURFACE) { |
| 43 | eglDestroySurface(mEGLDisplay, mEGLSurface); |
| 44 | mEGLSurface = EGL_NO_SURFACE; |
Alec Mouri | 05483a0 | 2018-09-10 21:03:42 +0000 | [diff] [blame] | 45 | mSurfaceWidth = 0; |
| 46 | mSurfaceHeight = 0; |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | mWindow = window; |
| 50 | if (mWindow) { |
| 51 | mEGLSurface = eglCreateWindowSurface(mEGLDisplay, mEGLConfig, mWindow, nullptr); |
Alec Mouri | 05483a0 | 2018-09-10 21:03:42 +0000 | [diff] [blame] | 52 | mSurfaceWidth = ANativeWindow_getWidth(window); |
| 53 | mSurfaceHeight = ANativeWindow_getHeight(window); |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 57 | void GLSurface::swapBuffers() const { |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 58 | if (!eglSwapBuffers(mEGLDisplay, mEGLSurface)) { |
| 59 | EGLint error = eglGetError(); |
| 60 | |
| 61 | const char format[] = "eglSwapBuffers(%p, %p) failed with 0x%08x"; |
| 62 | if (mCritical || error == EGL_CONTEXT_LOST) { |
| 63 | LOG_ALWAYS_FATAL(format, mEGLDisplay, mEGLSurface, error); |
| 64 | } else { |
| 65 | ALOGE(format, mEGLDisplay, mEGLSurface, error); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 70 | EGLint GLSurface::queryConfig(EGLint attrib) const { |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 71 | EGLint value; |
Chia-I Wu | 1e04361 | 2018-03-01 09:45:09 -0800 | [diff] [blame] | 72 | if (!eglGetConfigAttrib(mEGLDisplay, mEGLConfig, attrib, &value)) { |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 73 | value = 0; |
| 74 | } |
| 75 | |
| 76 | return value; |
| 77 | } |
| 78 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 79 | int32_t GLSurface::queryRedSize() const { |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 80 | return queryConfig(EGL_RED_SIZE); |
| 81 | } |
| 82 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 83 | int32_t GLSurface::queryGreenSize() const { |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 84 | return queryConfig(EGL_GREEN_SIZE); |
| 85 | } |
| 86 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 87 | int32_t GLSurface::queryBlueSize() const { |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 88 | return queryConfig(EGL_BLUE_SIZE); |
| 89 | } |
| 90 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 91 | int32_t GLSurface::queryAlphaSize() const { |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 92 | return queryConfig(EGL_ALPHA_SIZE); |
| 93 | } |
| 94 | |
Alec Mouri | 05483a0 | 2018-09-10 21:03:42 +0000 | [diff] [blame] | 95 | int32_t GLSurface::getWidth() const { |
| 96 | return mSurfaceWidth; |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Alec Mouri | 05483a0 | 2018-09-10 21:03:42 +0000 | [diff] [blame] | 99 | int32_t GLSurface::getHeight() const { |
| 100 | return mSurfaceHeight; |
Chia-I Wu | 7e60ecc | 2017-11-09 11:04:45 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 103 | } // namespace gl |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 104 | } // namespace renderengine |
| 105 | } // namespace android |