Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 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 | |
Jan Sebechlebsky | b828267 | 2024-05-22 10:43:37 +0200 | [diff] [blame] | 20 | #include <chrono> |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 21 | #include <cstdint> |
| 22 | |
| 23 | #include "GLES/gl.h" |
| 24 | #include "gui/Surface.h" |
| 25 | #include "utils/RefBase.h" |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | class IGraphicBufferProducer; |
| 30 | class IGraphicBufferConsumer; |
| 31 | class GLConsumer; |
| 32 | |
| 33 | namespace companion { |
| 34 | namespace virtualcamera { |
| 35 | |
| 36 | // Encapsulates GLConsumer & Surface for rendering into EGL texture. |
| 37 | class 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 Sebechlebsky | b828267 | 2024-05-22 10:43:37 +0200 | [diff] [blame] | 55 | // 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 Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 61 | // 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 Sebechlebsky | 99492e3 | 2023-12-20 09:49:45 +0100 | [diff] [blame] | 67 | // 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 Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 78 | 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 |