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