blob: ac3cf7d352709d7d35df93e893c3923b2e888fb6 [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"
Jan Sebechlebsky18ac32c2024-06-07 09:53:53 +020024#include "gui/ConsumerBase.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010025#include "gui/Surface.h"
26#include "utils/RefBase.h"
27
28namespace android {
29
30class IGraphicBufferProducer;
31class IGraphicBufferConsumer;
32class GLConsumer;
33
34namespace companion {
35namespace virtualcamera {
36
37// Encapsulates GLConsumer & Surface for rendering into EGL texture.
38class 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
Jan Sebechlebskyb8282672024-05-22 10:43:37 +020056 // 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
Jan Sebechlebsky18ac32c2024-06-07 09:53:53 +020062 void setFrameAvailableListener(
63 const wp<ConsumerBase::FrameAvailableListener>& listener);
64
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010065 // 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
Jan Sebechlebsky99492e32023-12-20 09:49:45 +010071 // 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
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010082 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