Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2023 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 | // #define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "EglDisplayContext" |
| 19 | #define EGL_EGLEXT_PROTOTYPES |
| 20 | #define GL_GLEXT_PROTOTYPES |
| 21 | |
| 22 | #include "EglDisplayContext.h" |
| 23 | |
| 24 | #include "EGL/egl.h" |
| 25 | #include "EglDisplayContext.h" |
| 26 | #include "EglFramebuffer.h" |
| 27 | #include "log/log.h" |
| 28 | |
| 29 | namespace android { |
| 30 | namespace companion { |
| 31 | namespace virtualcamera { |
| 32 | |
| 33 | EglDisplayContext::EglDisplayContext() |
| 34 | : mEglDisplay(EGL_NO_DISPLAY), |
| 35 | mEglContext(EGL_NO_CONTEXT), |
| 36 | mEglConfig(nullptr) { |
| 37 | EGLBoolean result; |
| 38 | |
| 39 | mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 40 | if (mEglDisplay == EGL_NO_DISPLAY) { |
| 41 | ALOGE("eglGetDisplay failed: %#x", eglGetError()); |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | EGLint majorVersion, minorVersion; |
| 46 | result = eglInitialize(mEglDisplay, &majorVersion, &minorVersion); |
| 47 | if (result != EGL_TRUE) { |
| 48 | ALOGE("eglInitialize failed: %#x", eglGetError()); |
| 49 | return; |
| 50 | } |
| 51 | ALOGV("Initialized EGL v%d.%d", majorVersion, minorVersion); |
| 52 | |
| 53 | EGLint numConfigs = 0; |
| 54 | EGLint configAttribs[] = { |
| 55 | EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, EGL_RENDERABLE_TYPE, |
| 56 | EGL_OPENGL_ES2_BIT, EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, |
| 57 | // no alpha |
| 58 | EGL_NONE}; |
| 59 | |
| 60 | result = |
| 61 | eglChooseConfig(mEglDisplay, configAttribs, &mEglConfig, 1, &numConfigs); |
| 62 | if (result != EGL_TRUE) { |
| 63 | ALOGE("eglChooseConfig error: %#x", eglGetError()); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | EGLint contextAttribs[] = {EGL_CONTEXT_MAJOR_VERSION_KHR, 3, EGL_NONE}; |
| 68 | mEglContext = |
| 69 | eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, contextAttribs); |
| 70 | if (mEglContext == EGL_NO_CONTEXT) { |
| 71 | ALOGE("eglCreateContext error: %#x", eglGetError()); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | if (!makeCurrent()) { |
| 76 | ALOGE( |
| 77 | "Failed to set newly initialized EGLContext and EGLDisplay connection " |
| 78 | "as current."); |
| 79 | } else { |
| 80 | ALOGV("EGL successfully initialized."); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | EglDisplayContext::~EglDisplayContext() { |
| 85 | if (mEglDisplay != EGL_NO_DISPLAY) { |
| 86 | eglTerminate(mEglDisplay); |
| 87 | } |
| 88 | if (mEglContext != EGL_NO_CONTEXT) { |
| 89 | eglDestroyContext(mEglDisplay, mEglContext); |
| 90 | } |
| 91 | eglReleaseThread(); |
| 92 | } |
| 93 | |
| 94 | EGLDisplay EglDisplayContext::getEglDisplay() const { |
| 95 | return mEglDisplay; |
| 96 | } |
| 97 | |
| 98 | bool EglDisplayContext::isInitialized() const { |
| 99 | return mEglContext != EGL_NO_CONTEXT && mEglDisplay != EGL_NO_DISPLAY; |
| 100 | } |
| 101 | |
| 102 | bool EglDisplayContext::makeCurrent() { |
| 103 | if (!eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, mEglContext)) { |
| 104 | ALOGE("eglMakeCurrent failed: %#x", eglGetError()); |
| 105 | return false; |
| 106 | } |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | } // namespace virtualcamera |
| 111 | } // namespace companion |
| 112 | } // namespace android |