blob: ac92bc4871da7a9ffac1581921a73269f2587574 [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
18#include <memory>
19#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);
51}
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
67GLuint EglSurfaceTexture::updateTexture() {
68 mGlConsumer->updateTexImage();
69 return mTextureId;
70}
71
72uint32_t EglSurfaceTexture::getWidth() const {
73 return mWidth;
74}
75
76uint32_t EglSurfaceTexture::getHeight() const {
77 return mHeight;
78}
79
80} // namespace virtualcamera
81} // namespace companion
82} // namespace android