blob: 7de5020f95c19e087c798ed84ae68a862c274418 [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// #define LOG_NDEBUG 0
Jan Sebechlebskyb8282672024-05-22 10:43:37 +020018#include "utils/Timers.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010019#define LOG_TAG "EglSurfaceTexture"
20
21#include <cstdint>
22
23#include "EglSurfaceTexture.h"
24#include "EglUtil.h"
25#include "GLES/gl.h"
26#include "gui/BufferQueue.h"
27#include "gui/GLConsumer.h"
28#include "gui/IGraphicBufferProducer.h"
29#include "hardware/gralloc.h"
30
31namespace android {
32namespace companion {
33namespace virtualcamera {
34
35EglSurfaceTexture::EglSurfaceTexture(const uint32_t width, const uint32_t height)
36 : mWidth(width), mHeight(height) {
37 glGenTextures(1, &mTextureId);
38 if (checkEglError("EglSurfaceTexture(): glGenTextures")) {
39 ALOGE("Failed to generate texture");
40 return;
41 }
42 BufferQueue::createBufferQueue(&mBufferProducer, &mBufferConsumer);
43 mGlConsumer = sp<GLConsumer>::make(
44 mBufferConsumer, mTextureId, GLConsumer::TEXTURE_EXTERNAL, false, false);
45 mGlConsumer->setName(String8("VirtualCameraEglSurfaceTexture"));
46 mGlConsumer->setDefaultBufferSize(mWidth, mHeight);
47 mGlConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_TEXTURE);
48 mGlConsumer->setDefaultBufferFormat(AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420);
49
50 mSurface = sp<Surface>::make(mBufferProducer);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010051}
52
53EglSurfaceTexture::~EglSurfaceTexture() {
54 if (mTextureId != 0) {
55 glDeleteTextures(1, &mTextureId);
56 }
57}
58
59sp<Surface> EglSurfaceTexture::getSurface() {
60 return mSurface;
61}
62
63sp<GraphicBuffer> EglSurfaceTexture::getCurrentBuffer() {
64 return mGlConsumer->getCurrentBuffer();
65}
66
Jan Sebechlebskyb8282672024-05-22 10:43:37 +020067bool EglSurfaceTexture::waitForNextFrame(const std::chrono::nanoseconds timeout) {
68 return mSurface->waitForNextFrame(mGlConsumer->getFrameNumber(),
69 static_cast<nsecs_t>(timeout.count()));
70}
71
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010072GLuint EglSurfaceTexture::updateTexture() {
73 mGlConsumer->updateTexImage();
74 return mTextureId;
75}
76
Jan Sebechlebsky99492e32023-12-20 09:49:45 +010077GLuint EglSurfaceTexture::getTextureId() const {
78 return mTextureId;
79}
80
81std::array<float, 16> EglSurfaceTexture::getTransformMatrix() {
82 std::array<float, 16> matrix;
83 mGlConsumer->getTransformMatrix(matrix.data());
84 return matrix;
85}
86
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010087uint32_t EglSurfaceTexture::getWidth() const {
88 return mWidth;
89}
90
91uint32_t EglSurfaceTexture::getHeight() const {
92 return mHeight;
93}
94
95} // namespace virtualcamera
96} // namespace companion
97} // namespace android