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 #ifndef ANDROID_COMPANION_VIRTUALCAMERA_EGLFRAMEBUFFER_H
18 #define ANDROID_COMPANION_VIRTUALCAMERA_EGLFRAMEBUFFER_H
19 
20 #define EGL_EGLEXT_PROTOTYPES
21 #define GL_GLEXT_PROTOTYPES
22 
23 #include <memory>
24 
25 #include "EGL/egl.h"
26 #include "EGL/eglext.h"
27 #include "GLES/gl.h"
28 
29 namespace android {
30 namespace companion {
31 namespace virtualcamera {
32 
33 // Encapsulates EGL Framebuffer backed by AHardwareBuffer instance.
34 //
35 // Note that the framebuffer is tied to EGLDisplay connection.
36 class EglFrameBuffer {
37  public:
38   EglFrameBuffer(EGLDisplay display, std::shared_ptr<AHardwareBuffer> hwBuffer);
39   virtual ~EglFrameBuffer();
40 
41   // Prepare for rendering into the framebuffer.
42   bool beforeDraw();
43 
44   // Finishes rendering into the framebuffer.
45   bool afterDraw();
46 
47   // Return width of framebuffer (in pixels).
48   int getWidth() const;
49 
50   // Return height of framebuffer (in pixels).
51   int getHeight() const;
52 
53   // Return underlying hardware buffer.
54   std::shared_ptr<AHardwareBuffer> getHardwareBuffer();
55 
56  private:
57   // Keeping shared_ptr to hardware buffer instance here prevents it from being
58   // freed while tied to EGL framebufer / EGL texture.
59   std::shared_ptr<AHardwareBuffer> mHardwareBuffer;
60   EGLDisplay mEglDisplay;
61   EGLImageKHR mEglImageKhr{EGL_NO_IMAGE_KHR};
62   GLuint mTextureId;
63   GLuint mFramebufferId;
64 
65   int mWidth;
66   int mHeight;
67 };
68 
69 }  // namespace virtualcamera
70 }  // namespace companion
71 }  // namespace android
72 
73 #endif  // ANDROID_COMPANION_VIRTUALCAMERA_EGLFRAMEBUFFER_H
74