1 /*
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
17 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
19 #include "GLFramebuffer.h"
20
21 #include <GLES/gl.h>
22 #include <GLES/glext.h>
23 #include <GLES2/gl2ext.h>
24 #include <GLES3/gl3.h>
25 #include <gui/DebugEGLImageTracker.h>
26 #include <nativebase/nativebase.h>
27 #include <utils/Trace.h>
28 #include "GLESRenderEngine.h"
29
30 namespace android {
31 namespace renderengine {
32 namespace gl {
33
GLFramebuffer(GLESRenderEngine & engine)34 GLFramebuffer::GLFramebuffer(GLESRenderEngine& engine)
35 : mEngine(engine), mEGLDisplay(engine.getEGLDisplay()), mEGLImage(EGL_NO_IMAGE_KHR) {
36 glGenTextures(1, &mTextureName);
37 glGenFramebuffers(1, &mFramebufferName);
38 }
39
~GLFramebuffer()40 GLFramebuffer::~GLFramebuffer() {
41 glDeleteFramebuffers(1, &mFramebufferName);
42 glDeleteTextures(1, &mTextureName);
43 }
44
setNativeWindowBuffer(ANativeWindowBuffer * nativeBuffer,bool isProtected,const bool useFramebufferCache)45 bool GLFramebuffer::setNativeWindowBuffer(ANativeWindowBuffer* nativeBuffer, bool isProtected,
46 const bool useFramebufferCache) {
47 ATRACE_CALL();
48 if (mEGLImage != EGL_NO_IMAGE_KHR) {
49 if (!usingFramebufferCache) {
50 eglDestroyImageKHR(mEGLDisplay, mEGLImage);
51 DEBUG_EGL_IMAGE_TRACKER_DESTROY();
52 }
53 mEGLImage = EGL_NO_IMAGE_KHR;
54 mBufferWidth = 0;
55 mBufferHeight = 0;
56 }
57
58 if (nativeBuffer) {
59 mEGLImage = mEngine.createFramebufferImageIfNeeded(nativeBuffer, isProtected,
60 useFramebufferCache);
61 if (mEGLImage == EGL_NO_IMAGE_KHR) {
62 return false;
63 }
64 usingFramebufferCache = useFramebufferCache;
65 mBufferWidth = nativeBuffer->width;
66 mBufferHeight = nativeBuffer->height;
67 }
68 return true;
69 }
70
allocateBuffers(uint32_t width,uint32_t height,void * data)71 void GLFramebuffer::allocateBuffers(uint32_t width, uint32_t height, void* data) {
72 ATRACE_CALL();
73
74 glBindTexture(GL_TEXTURE_2D, mTextureName);
75 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
76 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
81 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);
87 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
bind() const96 void GLFramebuffer::bind() const {
97 glBindFramebuffer(GL_FRAMEBUFFER, mFramebufferName);
98 }
99
bindAsReadBuffer() const100 void GLFramebuffer::bindAsReadBuffer() const {
101 glBindFramebuffer(GL_READ_FRAMEBUFFER, mFramebufferName);
102 }
103
bindAsDrawBuffer() const104 void GLFramebuffer::bindAsDrawBuffer() const {
105 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebufferName);
106 }
107
unbind() const108 void GLFramebuffer::unbind() const {
109 glBindFramebuffer(GL_FRAMEBUFFER, 0);
110 }
111
112 } // namespace gl
113 } // namespace renderengine
114 } // namespace android
115