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 | // #define LOG_NDEBUG 0 |
Vadim Caen | b79f4e3 | 2024-08-30 16:35:33 +0200 | [diff] [blame] | 18 | #include <chrono> |
| 19 | |
Jan Sebechlebsky | b828267 | 2024-05-22 10:43:37 +0200 | [diff] [blame] | 20 | #include "utils/Timers.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 21 | #define LOG_TAG "EglSurfaceTexture" |
| 22 | |
Jim Shargo | 2e0202f | 2024-07-30 19:54:43 +0000 | [diff] [blame] | 23 | #include <GLES/gl.h> |
| 24 | #include <com_android_graphics_libgui_flags.h> |
| 25 | #include <gui/BufferQueue.h> |
| 26 | #include <gui/GLConsumer.h> |
| 27 | #include <gui/IGraphicBufferProducer.h> |
| 28 | #include <hardware/gralloc.h> |
| 29 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 30 | #include <cstdint> |
| 31 | |
| 32 | #include "EglSurfaceTexture.h" |
| 33 | #include "EglUtil.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 34 | |
| 35 | namespace android { |
| 36 | namespace companion { |
| 37 | namespace virtualcamera { |
Jan Sebechlebsky | 759f0a8 | 2024-07-02 15:33:16 +0200 | [diff] [blame] | 38 | namespace { |
| 39 | |
| 40 | // Maximal number of buffers producer can dequeue without blocking. |
| 41 | constexpr int kBufferProducerMaxDequeueBufferCount = 64; |
| 42 | |
| 43 | } // namespace |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 44 | |
| 45 | EglSurfaceTexture::EglSurfaceTexture(const uint32_t width, const uint32_t height) |
| 46 | : mWidth(width), mHeight(height) { |
| 47 | glGenTextures(1, &mTextureId); |
| 48 | if (checkEglError("EglSurfaceTexture(): glGenTextures")) { |
| 49 | ALOGE("Failed to generate texture"); |
| 50 | return; |
| 51 | } |
Jim Shargo | 2e0202f | 2024-07-30 19:54:43 +0000 | [diff] [blame] | 52 | |
| 53 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) |
| 54 | mGlConsumer = sp<GLConsumer>::make(mTextureId, GLConsumer::TEXTURE_EXTERNAL, |
| 55 | false, false); |
| 56 | mGlConsumer->setName(String8("VirtualCameraEglSurfaceTexture")); |
| 57 | mGlConsumer->setDefaultBufferSize(mWidth, mHeight); |
| 58 | mGlConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_TEXTURE); |
| 59 | mGlConsumer->setDefaultBufferFormat(AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420); |
| 60 | |
| 61 | mSurface = mGlConsumer->getSurface(); |
| 62 | mSurface->setMaxDequeuedBufferCount(kBufferProducerMaxDequeueBufferCount); |
| 63 | #else |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 64 | BufferQueue::createBufferQueue(&mBufferProducer, &mBufferConsumer); |
Jan Sebechlebsky | 759f0a8 | 2024-07-02 15:33:16 +0200 | [diff] [blame] | 65 | // Set max dequeue buffer count for producer to maximal value to prevent |
| 66 | // blocking when dequeuing input buffers. |
| 67 | mBufferProducer->setMaxDequeuedBufferCount( |
| 68 | kBufferProducerMaxDequeueBufferCount); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 69 | mGlConsumer = sp<GLConsumer>::make( |
| 70 | mBufferConsumer, mTextureId, GLConsumer::TEXTURE_EXTERNAL, false, false); |
| 71 | mGlConsumer->setName(String8("VirtualCameraEglSurfaceTexture")); |
| 72 | mGlConsumer->setDefaultBufferSize(mWidth, mHeight); |
| 73 | mGlConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_TEXTURE); |
| 74 | mGlConsumer->setDefaultBufferFormat(AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420); |
| 75 | |
| 76 | mSurface = sp<Surface>::make(mBufferProducer); |
Jim Shargo | 2e0202f | 2024-07-30 19:54:43 +0000 | [diff] [blame] | 77 | #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | EglSurfaceTexture::~EglSurfaceTexture() { |
| 81 | if (mTextureId != 0) { |
| 82 | glDeleteTextures(1, &mTextureId); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | sp<Surface> EglSurfaceTexture::getSurface() { |
| 87 | return mSurface; |
| 88 | } |
| 89 | |
| 90 | sp<GraphicBuffer> EglSurfaceTexture::getCurrentBuffer() { |
| 91 | return mGlConsumer->getCurrentBuffer(); |
| 92 | } |
| 93 | |
Jan Sebechlebsky | 18ac32c | 2024-06-07 09:53:53 +0200 | [diff] [blame] | 94 | void EglSurfaceTexture::setFrameAvailableListener( |
| 95 | const wp<ConsumerBase::FrameAvailableListener>& listener) { |
| 96 | mGlConsumer->setFrameAvailableListener(listener); |
| 97 | } |
| 98 | |
Jan Sebechlebsky | b828267 | 2024-05-22 10:43:37 +0200 | [diff] [blame] | 99 | bool EglSurfaceTexture::waitForNextFrame(const std::chrono::nanoseconds timeout) { |
| 100 | return mSurface->waitForNextFrame(mGlConsumer->getFrameNumber(), |
| 101 | static_cast<nsecs_t>(timeout.count())); |
| 102 | } |
| 103 | |
Vadim Caen | b79f4e3 | 2024-08-30 16:35:33 +0200 | [diff] [blame] | 104 | std::chrono::nanoseconds EglSurfaceTexture::getTimestamp() { |
| 105 | return std::chrono::nanoseconds(mGlConsumer->getTimestamp()); |
| 106 | } |
| 107 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 108 | GLuint EglSurfaceTexture::updateTexture() { |
Jan Sebechlebsky | 759f0a8 | 2024-07-02 15:33:16 +0200 | [diff] [blame] | 109 | int previousFrameId; |
| 110 | int framesAdvance = 0; |
| 111 | // Consume buffers one at the time. |
| 112 | // Contrary to the code comments in GLConsumer, the GLConsumer acquires |
| 113 | // next queued buffer (not the most recently queued buffer). |
| 114 | while (true) { |
| 115 | previousFrameId = mGlConsumer->getFrameNumber(); |
| 116 | mGlConsumer->updateTexImage(); |
| 117 | int currentFrameId = mGlConsumer->getFrameNumber(); |
| 118 | if (previousFrameId == currentFrameId) { |
| 119 | // Frame number didn't change after updating the texture, |
| 120 | // this means we're at the end of the queue and current attached |
| 121 | // buffer is the most recent buffer. |
| 122 | break; |
| 123 | } |
| 124 | |
| 125 | framesAdvance++; |
| 126 | previousFrameId = currentFrameId; |
| 127 | } |
| 128 | ALOGV("%s: Advanced %d frames", __func__, framesAdvance); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 129 | return mTextureId; |
| 130 | } |
| 131 | |
Jan Sebechlebsky | 99492e3 | 2023-12-20 09:49:45 +0100 | [diff] [blame] | 132 | GLuint EglSurfaceTexture::getTextureId() const { |
| 133 | return mTextureId; |
| 134 | } |
| 135 | |
| 136 | std::array<float, 16> EglSurfaceTexture::getTransformMatrix() { |
| 137 | std::array<float, 16> matrix; |
| 138 | mGlConsumer->getTransformMatrix(matrix.data()); |
| 139 | return matrix; |
| 140 | } |
| 141 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 142 | uint32_t EglSurfaceTexture::getWidth() const { |
| 143 | return mWidth; |
| 144 | } |
| 145 | |
| 146 | uint32_t EglSurfaceTexture::getHeight() const { |
| 147 | return mHeight; |
| 148 | } |
| 149 | |
| 150 | } // namespace virtualcamera |
| 151 | } // namespace companion |
| 152 | } // namespace android |