xref: /aosp_15_r20/frameworks/av/services/camera/virtualcamera/util/EglSurfaceTexture.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
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_EGLSURFACETEXTURE_H
18 #define ANDROID_COMPANION_VIRTUALCAMERA_EGLSURFACETEXTURE_H
19 
20 #include <GLES/gl.h>
21 #include <gui/ConsumerBase.h>
22 #include <gui/Surface.h>
23 #include <utils/RefBase.h>
24 
25 #include <chrono>
26 #include <cstdint>
27 
28 namespace android {
29 
30 #if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
31 class IGraphicBufferProducer;
32 class IGraphicBufferConsumer;
33 #endif  // !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
34 
35 class GLConsumer;
36 
37 namespace companion {
38 namespace virtualcamera {
39 
40 // Encapsulates GLConsumer & Surface for rendering into EGL texture.
41 class EglSurfaceTexture {
42  public:
43   // Create new EGL Texture with specified size.
44   EglSurfaceTexture(uint32_t width, uint32_t height);
45   ~EglSurfaceTexture();
46 
47   // Get Surface backing up the texture.
48   sp<Surface> getSurface();
49 
50   // Get GraphicBuffer backing the current texture.
51   sp<GraphicBuffer> getCurrentBuffer();
52 
53   // Get width of surface / texture.
54   uint32_t getWidth() const;
55 
56   // Get height of surface / texture.
57   uint32_t getHeight() const;
58 
59   // Wait for next frame to be available in the surface
60   // until timeout.
61   //
62   // Returns false on timeout, true if new frame was received before timeout.
63   bool waitForNextFrame(std::chrono::nanoseconds timeout);
64 
65   void setFrameAvailableListener(const std::function<void()>& listener);
66 
67   // Update the texture with the most recent submitted buffer.
68   // Most be called on thread with EGL context.
69   //
70   // Returns EGL texture id of the texture.
71   GLuint updateTexture();
72 
73   // Returns EGL texture id of the underlying texture.
74   GLuint getTextureId() const;
75 
76   // Returns 4x4 transformation matrix in column-major order,
77   // which should be applied to EGL texture coordinates
78   // before sampling from the texture backed by android native buffer,
79   // so the corresponding region of the underlying buffer is sampled.
80   //
81   // See SurfaceTexture.getTransformMatrix for more details.
82   std::array<float, 16> getTransformMatrix();
83 
84   // Retrieves the timestamp associated with the texture image
85   // set by the most recent call to updateTexture.
86   std::chrono::nanoseconds getTimestamp();
87 
88   // Returns true is a frame has ever been drawn on this surface.
89   bool isFirstFrameDrawn();
90 
91  private:
92 #if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
93   sp<IGraphicBufferProducer> mBufferProducer;
94   sp<IGraphicBufferConsumer> mBufferConsumer;
95 #endif  // !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
96   sp<GLConsumer> mGlConsumer;
97   sp<Surface> mSurface;
98   GLuint mTextureId;
99   const uint32_t mWidth;
100   const uint32_t mHeight;
101   std::atomic_bool mIsFirstFrameDrawn = false;
102   sp<ConsumerBase::FrameAvailableListener> mFrameAvailableListener;
103 };
104 
105 }  // namespace virtualcamera
106 }  // namespace companion
107 }  // namespace android
108 
109 #endif  // ANDROID_COMPANION_VIRTUALCAMERA_EGLSURFACETEXTURE_H
110