blob: 9f26e19846c6b2939756e9a6f36faf290777fd0c [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 Sebechlebsky5cb39962023-11-22 17:33:07 +010018#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
30namespace android {
31namespace companion {
32namespace virtualcamera {
33
34EglSurfaceTexture::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 Sebechlebsky5cb39962023-11-22 17:33:07 +010050}
51
52EglSurfaceTexture::~EglSurfaceTexture() {
53 if (mTextureId != 0) {
54 glDeleteTextures(1, &mTextureId);
55 }
56}
57
58sp<Surface> EglSurfaceTexture::getSurface() {
59 return mSurface;
60}
61
62sp<GraphicBuffer> EglSurfaceTexture::getCurrentBuffer() {
63 return mGlConsumer->getCurrentBuffer();
64}
65
66GLuint EglSurfaceTexture::updateTexture() {
67 mGlConsumer->updateTexImage();
68 return mTextureId;
69}
70
Jan Sebechlebsky99492e32023-12-20 09:49:45 +010071GLuint EglSurfaceTexture::getTextureId() const {
72 return mTextureId;
73}
74
75std::array<float, 16> EglSurfaceTexture::getTransformMatrix() {
76 std::array<float, 16> matrix;
77 mGlConsumer->getTransformMatrix(matrix.data());
78 return matrix;
79}
80
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010081uint32_t EglSurfaceTexture::getWidth() const {
82 return mWidth;
83}
84
85uint32_t EglSurfaceTexture::getHeight() const {
86 return mHeight;
87}
88
89} // namespace virtualcamera
90} // namespace companion
91} // namespace android