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 <atomic> 26 #include <chrono> 27 #include <condition_variable> 28 #include <cstdint> 29 30 namespace android { 31 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(const std::function<void()>& listener); 63 64 // Update the texture with the most recent submitted buffer. 65 // Most be called on thread with EGL context. 66 // 67 // Returns EGL texture id of the texture. 68 GLuint updateTexture(); 69 70 // Returns EGL texture id of the underlying texture. 71 GLuint getTextureId() const; 72 73 // Returns 4x4 transformation matrix in column-major order, 74 // which should be applied to EGL texture coordinates 75 // before sampling from the texture backed by android native buffer, 76 // so the corresponding region of the underlying buffer is sampled. 77 // 78 // See SurfaceTexture.getTransformMatrix for more details. 79 std::array<float, 16> getTransformMatrix(); 80 81 // Retrieves the timestamp associated with the texture image 82 // set by the most recent call to updateTexture. 83 std::chrono::nanoseconds getTimestamp(); 84 85 // Returns true is a frame has ever been drawn on this surface. 86 bool isFirstFrameDrawn(); 87 88 class FrameAvailableListenerProxy 89 : public ConsumerBase::FrameAvailableListener { 90 public: 91 FrameAvailableListenerProxy(EglSurfaceTexture* surface); 92 93 void setCallback(const std::function<void()>& callback); 94 95 virtual void onFrameAvailable(const BufferItem&) override; 96 97 private: 98 EglSurfaceTexture& mSurface; 99 std::function<void()> mOnFrameAvailableCallback; 100 }; 101 102 private: 103 sp<GLConsumer> mGlConsumer; 104 sp<Surface> mSurface; 105 GLuint mTextureId; 106 const uint32_t mWidth; 107 const uint32_t mHeight; 108 std::atomic_long mLastWaitedFrame = 0; 109 sp<FrameAvailableListenerProxy> mFrameAvailableListenerProxy; 110 sp<ConsumerBase::FrameAvailableListener> mFrameAvailableListener; 111 std::condition_variable mFrameAvailableCondition; 112 std::mutex mWaitForFrameMutex; 113 }; 114 115 } // namespace virtualcamera 116 } // namespace companion 117 } // namespace android 118 119 #endif // ANDROID_COMPANION_VIRTUALCAMERA_EGLSURFACETEXTURE_H 120