• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "EglFramebuffer"
19 #include "EglFramebuffer.h"
20 
21 #include "EGL/eglext.h"
22 #include "EglUtil.h"
23 #include "GLES/gl.h"
24 #include "GLES2/gl2.h"
25 #include "GLES2/gl2ext.h"
26 #include "android/hardware_buffer.h"
27 #include "log/log.h"
28 
29 namespace android {
30 namespace companion {
31 namespace virtualcamera {
32 
EglFrameBuffer(EGLDisplay display,std::shared_ptr<AHardwareBuffer> hwBuffer)33 EglFrameBuffer::EglFrameBuffer(EGLDisplay display,
34                                std::shared_ptr<AHardwareBuffer> hwBuffer)
35     : mHardwareBuffer(hwBuffer), mEglDisplay(display) {
36   if (hwBuffer == nullptr) {
37     ALOGE("Cannot construct EglFramebuffer from null hwBuffer");
38     return;
39   }
40 
41   AHardwareBuffer_Desc hwBufferDesc;
42   AHardwareBuffer_describe(hwBuffer.get(), &hwBufferDesc);
43   mWidth = hwBufferDesc.width;
44   mHeight = hwBufferDesc.height;
45 
46   EGLClientBuffer clientBuffer = eglGetNativeClientBufferANDROID(hwBuffer.get());
47   mEglImageKhr = eglCreateImageKHR(display, EGL_NO_CONTEXT,
48                                    EGL_NATIVE_BUFFER_ANDROID, clientBuffer, 0);
49   if (checkEglError("eglCreateImageKHR")) {
50     return;
51   }
52 
53   // Create texture backed by the hardware buffer.
54   glGenTextures(1, &mTextureId);
55   glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureId);
56   glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES,
57                                (GLeglImageOES)mEglImageKhr);
58   if (checkEglError("configure external texture")) {
59     return;
60   }
61 
62   // Create framebuffer backed by the texture.
63   glGenFramebuffers(1, &mFramebufferId);
64   glBindFramebuffer(GL_FRAMEBUFFER, mFramebufferId);
65   glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
66                          GL_TEXTURE_EXTERNAL_OES, mTextureId, 0);
67   GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
68   if (status != GL_FRAMEBUFFER_COMPLETE) {
69     ALOGE("Failed to configure framebuffer for texture");
70     return;  // false;
71   }
72   if (checkEglError("glCheckFramebufferStatus")) {
73     return;  // false;
74   }
75 }
76 
~EglFrameBuffer()77 EglFrameBuffer::~EglFrameBuffer() {
78   if (mFramebufferId != 0) {
79     glDeleteFramebuffers(1, &mFramebufferId);
80   }
81   if (mTextureId != 0) {
82     glDeleteTextures(1, &mTextureId);
83   }
84   if (mEglImageKhr != EGL_NO_IMAGE_KHR) {
85     eglDestroyImageKHR(mEglDisplay, mEglDisplay);
86   }
87 }
88 
beforeDraw()89 bool EglFrameBuffer::beforeDraw() {
90   glBindFramebuffer(GL_FRAMEBUFFER, mFramebufferId);
91   if (checkEglError("glBindFramebuffer")) {
92     return false;
93   }
94 
95   glViewport(0, 0, mWidth, mHeight);
96 
97   return true;
98 }
99 
afterDraw()100 bool EglFrameBuffer::afterDraw() {
101   glFinish();
102   glBindFramebuffer(GL_FRAMEBUFFER, 0);
103   glBindTexture(GL_TEXTURE_EXTERNAL_OES, 0);
104   return true;
105 }
106 
getWidth() const107 int EglFrameBuffer::getWidth() const {
108   return mWidth;
109 }
110 
getHeight() const111 int EglFrameBuffer::getHeight() const {
112   return mHeight;
113 }
114 
getHardwareBuffer()115 std::shared_ptr<AHardwareBuffer> EglFrameBuffer::getHardwareBuffer() {
116   return mHardwareBuffer;
117 }
118 
119 }  // namespace virtualcamera
120 }  // namespace companion
121 }  // namespace android
122