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