blob: 153935b6781ef728019d505986f470d1d56dc014 [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)
Lucas Dupin19c8f0e2019-11-25 17:55:44 -080035 : GLFramebuffer(engine, false /* multiTarget */) {}
36
37GLFramebuffer::GLFramebuffer(GLESRenderEngine& engine, bool multiTarget)
Alec Mourida4cf3b2019-02-12 15:33:01 -080038 : mEngine(engine), mEGLDisplay(engine.getEGLDisplay()), mEGLImage(EGL_NO_IMAGE_KHR) {
Peiyong Line5a9a7f2018-08-30 15:32:13 -070039 glGenTextures(1, &mTextureName);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -080040 if (multiTarget) {
41 glGenTextures(1, &mSecondaryTextureName);
42 }
Peiyong Line5a9a7f2018-08-30 15:32:13 -070043 glGenFramebuffers(1, &mFramebufferName);
44}
45
46GLFramebuffer::~GLFramebuffer() {
47 glDeleteFramebuffers(1, &mFramebufferName);
48 glDeleteTextures(1, &mTextureName);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -080049 if (mSecondaryTextureName != -1) {
50 glDeleteTextures(1, &mSecondaryTextureName);
51 }
Peiyong Line5a9a7f2018-08-30 15:32:13 -070052}
53
Alec Mourife0d72b2019-03-21 14:05:56 -070054bool GLFramebuffer::setNativeWindowBuffer(ANativeWindowBuffer* nativeBuffer, bool isProtected,
55 const bool useFramebufferCache) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +000056 ATRACE_CALL();
Peiyong Line5a9a7f2018-08-30 15:32:13 -070057 if (mEGLImage != EGL_NO_IMAGE_KHR) {
Alec Mourife0d72b2019-03-21 14:05:56 -070058 if (!usingFramebufferCache) {
59 eglDestroyImageKHR(mEGLDisplay, mEGLImage);
Ady Abrahama3b08ef2019-07-15 18:43:10 -070060 DEBUG_EGL_IMAGE_TRACKER_DESTROY();
Alec Mourife0d72b2019-03-21 14:05:56 -070061 }
Peiyong Line5a9a7f2018-08-30 15:32:13 -070062 mEGLImage = EGL_NO_IMAGE_KHR;
Alec Mouri05483a02018-09-10 21:03:42 +000063 mBufferWidth = 0;
64 mBufferHeight = 0;
Peiyong Line5a9a7f2018-08-30 15:32:13 -070065 }
66
67 if (nativeBuffer) {
Alec Mourife0d72b2019-03-21 14:05:56 -070068 mEGLImage = mEngine.createFramebufferImageIfNeeded(nativeBuffer, isProtected,
69 useFramebufferCache);
Peiyong Line5a9a7f2018-08-30 15:32:13 -070070 if (mEGLImage == EGL_NO_IMAGE_KHR) {
71 return false;
72 }
Alec Mourife0d72b2019-03-21 14:05:56 -070073 usingFramebufferCache = useFramebufferCache;
Alec Mouri05483a02018-09-10 21:03:42 +000074 mBufferWidth = nativeBuffer->width;
75 mBufferHeight = nativeBuffer->height;
Peiyong Line5a9a7f2018-08-30 15:32:13 -070076 }
77 return true;
78}
79
Lucas Dupin19c8f0e2019-11-25 17:55:44 -080080void GLFramebuffer::allocateBuffers(uint32_t width, uint32_t height) {
81 ATRACE_CALL();
82
83 glBindTexture(GL_TEXTURE_2D, mTextureName);
84 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
85 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
86 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
87 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
88 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
89
90 const bool multiTarget = mSecondaryTextureName != -1;
91 if (multiTarget) {
92 glBindTexture(GL_TEXTURE_2D, mSecondaryTextureName);
93 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
94 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
95 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
96 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
97 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
98 }
99
100 mBufferHeight = height;
101 mBufferWidth = width;
102 mEngine.checkErrors("Allocating Fbo texture");
103
104 bind();
105 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mTextureName, 0);
106 if (multiTarget) {
107 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D,
108 mSecondaryTextureName, 0);
109 GLenum buffers[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT};
110 glDrawBuffers(2, buffers);
111 }
112 mStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
113 unbind();
114 glBindTexture(GL_TEXTURE_2D, 0);
115
116 if (mStatus != GL_FRAMEBUFFER_COMPLETE) {
117 ALOGE("Frame buffer is not complete. Error %d", mStatus);
118 }
119}
120
121void GLFramebuffer::bind() const {
122 glBindFramebuffer(GL_FRAMEBUFFER, mFramebufferName);
123}
124
Lucas Dupinf82ed8f2020-01-17 12:11:07 -0800125void GLFramebuffer::bindAsReadBuffer() const {
126 glBindFramebuffer(GL_READ_FRAMEBUFFER, mFramebufferName);
127}
128
129void GLFramebuffer::bindAsDrawBuffer() const {
130 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebufferName);
131}
132
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800133void GLFramebuffer::unbind() const {
134 glBindFramebuffer(GL_FRAMEBUFFER, 0);
135}
136
Peiyong Lin46080ef2018-10-26 18:43:14 -0700137} // namespace gl
138} // namespace renderengine
139} // namespace android