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 |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 18 | #define LOG_TAG "EglSurfaceTexture" |
| 19 | |
| 20 | #include <cstdint> |
| 21 | |
| 22 | #include "EglSurfaceTexture.h" |
| 23 | #include "EglUtil.h" |
| 24 | #include "GLES/gl.h" |
| 25 | #include "gui/BufferQueue.h" |
| 26 | #include "gui/GLConsumer.h" |
| 27 | #include "gui/IGraphicBufferProducer.h" |
| 28 | #include "hardware/gralloc.h" |
| 29 | |
| 30 | namespace android { |
| 31 | namespace companion { |
| 32 | namespace virtualcamera { |
| 33 | |
| 34 | EglSurfaceTexture::EglSurfaceTexture(const uint32_t width, const uint32_t height) |
| 35 | : mWidth(width), mHeight(height) { |
| 36 | glGenTextures(1, &mTextureId); |
| 37 | if (checkEglError("EglSurfaceTexture(): glGenTextures")) { |
| 38 | ALOGE("Failed to generate texture"); |
| 39 | return; |
| 40 | } |
| 41 | BufferQueue::createBufferQueue(&mBufferProducer, &mBufferConsumer); |
| 42 | mGlConsumer = sp<GLConsumer>::make( |
| 43 | mBufferConsumer, mTextureId, GLConsumer::TEXTURE_EXTERNAL, false, false); |
| 44 | mGlConsumer->setName(String8("VirtualCameraEglSurfaceTexture")); |
| 45 | mGlConsumer->setDefaultBufferSize(mWidth, mHeight); |
| 46 | mGlConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_TEXTURE); |
| 47 | mGlConsumer->setDefaultBufferFormat(AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420); |
| 48 | |
| 49 | mSurface = sp<Surface>::make(mBufferProducer); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | EglSurfaceTexture::~EglSurfaceTexture() { |
| 53 | if (mTextureId != 0) { |
| 54 | glDeleteTextures(1, &mTextureId); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | sp<Surface> EglSurfaceTexture::getSurface() { |
| 59 | return mSurface; |
| 60 | } |
| 61 | |
| 62 | sp<GraphicBuffer> EglSurfaceTexture::getCurrentBuffer() { |
| 63 | return mGlConsumer->getCurrentBuffer(); |
| 64 | } |
| 65 | |
| 66 | GLuint EglSurfaceTexture::updateTexture() { |
| 67 | mGlConsumer->updateTexImage(); |
| 68 | return mTextureId; |
| 69 | } |
| 70 | |
| 71 | uint32_t EglSurfaceTexture::getWidth() const { |
| 72 | return mWidth; |
| 73 | } |
| 74 | |
| 75 | uint32_t EglSurfaceTexture::getHeight() const { |
| 76 | return mHeight; |
| 77 | } |
| 78 | |
| 79 | } // namespace virtualcamera |
| 80 | } // namespace companion |
| 81 | } // namespace android |