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 <nativebase/nativebase.h>
26 #include <utils/Trace.h>
27 #include "GLESRenderEngine.h"
28 
29 namespace android {
30 namespace renderengine {
31 namespace gl {
32 
GLFramebuffer(GLESRenderEngine & engine)33 GLFramebuffer::GLFramebuffer(GLESRenderEngine& engine)
34       : mEngine(engine), mEGLDisplay(engine.getEGLDisplay()), mEGLImage(EGL_NO_IMAGE_KHR) {
35     glGenTextures(1, &mTextureName);
36     glGenFramebuffers(1, &mFramebufferName);
37 }
38 
~GLFramebuffer()39 GLFramebuffer::~GLFramebuffer() {
40     glDeleteFramebuffers(1, &mFramebufferName);
41     glDeleteTextures(1, &mTextureName);
42 }
43 
setNativeWindowBuffer(ANativeWindowBuffer * nativeBuffer,bool isProtected,const bool useFramebufferCache)44 bool GLFramebuffer::setNativeWindowBuffer(ANativeWindowBuffer* nativeBuffer, bool isProtected,
45                                           const bool useFramebufferCache) {
46     ATRACE_CALL();
47     if (mEGLImage != EGL_NO_IMAGE_KHR) {
48         if (!usingFramebufferCache) {
49             eglDestroyImageKHR(mEGLDisplay, mEGLImage);
50         }
51         mEGLImage = EGL_NO_IMAGE_KHR;
52         mBufferWidth = 0;
53         mBufferHeight = 0;
54     }
55 
56     if (nativeBuffer) {
57         mEGLImage = mEngine.createFramebufferImageIfNeeded(nativeBuffer, isProtected,
58                                                            useFramebufferCache);
59         if (mEGLImage == EGL_NO_IMAGE_KHR) {
60             return false;
61         }
62         usingFramebufferCache = useFramebufferCache;
63         mBufferWidth = nativeBuffer->width;
64         mBufferHeight = nativeBuffer->height;
65     }
66     return true;
67 }
68 
allocateBuffers(uint32_t width,uint32_t height,void * data)69 void GLFramebuffer::allocateBuffers(uint32_t width, uint32_t height, void* data) {
70     ATRACE_CALL();
71 
72     glBindTexture(GL_TEXTURE_2D, mTextureName);
73     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
74     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
75     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
76     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
77     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
78 
79     mBufferHeight = height;
80     mBufferWidth = width;
81     mEngine.checkErrors("Allocating Fbo texture");
82 
83     bind();
84     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mTextureName, 0);
85     mStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
86     unbind();
87     glBindTexture(GL_TEXTURE_2D, 0);
88 
89     if (mStatus != GL_FRAMEBUFFER_COMPLETE) {
90         ALOGE("Frame buffer is not complete. Error %d", mStatus);
91     }
92 }
93 
bind() const94 void GLFramebuffer::bind() const {
95     glBindFramebuffer(GL_FRAMEBUFFER, mFramebufferName);
96 }
97 
bindAsReadBuffer() const98 void GLFramebuffer::bindAsReadBuffer() const {
99     glBindFramebuffer(GL_READ_FRAMEBUFFER, mFramebufferName);
100 }
101 
bindAsDrawBuffer() const102 void GLFramebuffer::bindAsDrawBuffer() const {
103     glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebufferName);
104 }
105 
unbind() const106 void GLFramebuffer::unbind() const {
107     glBindFramebuffer(GL_FRAMEBUFFER, 0);
108 }
109 
110 } // namespace gl
111 } // namespace renderengine
112 } // namespace android
113