blob: 383486b6b8245017b5c848c21b8bdf5f520094c7 [file] [log] [blame]
Peiyong Line5a9a7f2018-08-30 15:32:13 -07001/*
2 * Copyright 2018 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
Alec Mourie7d1d4a2019-02-05 01:13:46 +000017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Peiyong Line5a9a7f2018-08-30 15:32:13 -070019#include "GLFramebuffer.h"
20
21#include <GLES/gl.h>
22#include <GLES/glext.h>
Peiyong Line5a9a7f2018-08-30 15:32:13 -070023#include <GLES2/gl2ext.h>
Lucas Dupin19c8f0e2019-11-25 17:55:44 -080024#include <GLES3/gl3.h>
Ady Abrahama3b08ef2019-07-15 18:43:10 -070025#include <gui/DebugEGLImageTracker.h>
Alec Mouri05483a02018-09-10 21:03:42 +000026#include <nativebase/nativebase.h>
Alec Mourie7d1d4a2019-02-05 01:13:46 +000027#include <utils/Trace.h>
Peiyong Lin7e219eb2018-12-03 05:40:42 -080028#include "GLESRenderEngine.h"
Peiyong Line5a9a7f2018-08-30 15:32:13 -070029
30namespace android {
31namespace renderengine {
32namespace gl {
33
Alec Mourida4cf3b2019-02-12 15:33:01 -080034GLFramebuffer::GLFramebuffer(GLESRenderEngine& engine)
35 : mEngine(engine), mEGLDisplay(engine.getEGLDisplay()), mEGLImage(EGL_NO_IMAGE_KHR) {
Peiyong Line5a9a7f2018-08-30 15:32:13 -070036 glGenTextures(1, &mTextureName);
37 glGenFramebuffers(1, &mFramebufferName);
38}
39
40GLFramebuffer::~GLFramebuffer() {
41 glDeleteFramebuffers(1, &mFramebufferName);
42 glDeleteTextures(1, &mTextureName);
Peiyong Line5a9a7f2018-08-30 15:32:13 -070043}
44
Alec Mourife0d72b2019-03-21 14:05:56 -070045bool GLFramebuffer::setNativeWindowBuffer(ANativeWindowBuffer* nativeBuffer, bool isProtected,
46 const bool useFramebufferCache) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +000047 ATRACE_CALL();
Peiyong Line5a9a7f2018-08-30 15:32:13 -070048 if (mEGLImage != EGL_NO_IMAGE_KHR) {
Alec Mourife0d72b2019-03-21 14:05:56 -070049 if (!usingFramebufferCache) {
50 eglDestroyImageKHR(mEGLDisplay, mEGLImage);
Ady Abrahama3b08ef2019-07-15 18:43:10 -070051 DEBUG_EGL_IMAGE_TRACKER_DESTROY();
Alec Mourife0d72b2019-03-21 14:05:56 -070052 }
Peiyong Line5a9a7f2018-08-30 15:32:13 -070053 mEGLImage = EGL_NO_IMAGE_KHR;
Alec Mouri05483a02018-09-10 21:03:42 +000054 mBufferWidth = 0;
55 mBufferHeight = 0;
Peiyong Line5a9a7f2018-08-30 15:32:13 -070056 }
57
58 if (nativeBuffer) {
Alec Mourife0d72b2019-03-21 14:05:56 -070059 mEGLImage = mEngine.createFramebufferImageIfNeeded(nativeBuffer, isProtected,
60 useFramebufferCache);
Peiyong Line5a9a7f2018-08-30 15:32:13 -070061 if (mEGLImage == EGL_NO_IMAGE_KHR) {
62 return false;
63 }
Alec Mourife0d72b2019-03-21 14:05:56 -070064 usingFramebufferCache = useFramebufferCache;
Alec Mouri05483a02018-09-10 21:03:42 +000065 mBufferWidth = nativeBuffer->width;
66 mBufferHeight = nativeBuffer->height;
Peiyong Line5a9a7f2018-08-30 15:32:13 -070067 }
68 return true;
69}
70
Alec Mouri4dde1782019-09-30 17:27:13 -070071void GLFramebuffer::allocateBuffers(uint32_t width, uint32_t height, void* data) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -080072 ATRACE_CALL();
73
74 glBindTexture(GL_TEXTURE_2D, mTextureName);
Alec Mouri4dde1782019-09-30 17:27:13 -070075 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -080076 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
77 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
78 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
79 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
80
Lucas Dupin19c8f0e2019-11-25 17:55:44 -080081 mBufferHeight = height;
82 mBufferWidth = width;
83 mEngine.checkErrors("Allocating Fbo texture");
84
85 bind();
86 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mTextureName, 0);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -080087 mStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
88 unbind();
89 glBindTexture(GL_TEXTURE_2D, 0);
90
91 if (mStatus != GL_FRAMEBUFFER_COMPLETE) {
92 ALOGE("Frame buffer is not complete. Error %d", mStatus);
93 }
94}
95
96void GLFramebuffer::bind() const {
97 glBindFramebuffer(GL_FRAMEBUFFER, mFramebufferName);
98}
99
Lucas Dupinf82ed8f2020-01-17 12:11:07 -0800100void GLFramebuffer::bindAsReadBuffer() const {
101 glBindFramebuffer(GL_READ_FRAMEBUFFER, mFramebufferName);
102}
103
104void GLFramebuffer::bindAsDrawBuffer() const {
105 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebufferName);
106}
107
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800108void GLFramebuffer::unbind() const {
109 glBindFramebuffer(GL_FRAMEBUFFER, 0);
110}
111
Peiyong Lin46080ef2018-10-26 18:43:14 -0700112} // namespace gl
113} // namespace renderengine
114} // namespace android