| Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 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 |  | 
| Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS | 
|  | 18 |  | 
| Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 19 | #include "GLFramebuffer.h" | 
|  | 20 |  | 
|  | 21 | #include <GLES/gl.h> | 
|  | 22 | #include <GLES/glext.h> | 
| Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 23 | #include <GLES2/gl2ext.h> | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 24 | #include <GLES3/gl3.h> | 
| Ady Abraham | a3b08ef | 2019-07-15 18:43:10 -0700 | [diff] [blame] | 25 | #include <gui/DebugEGLImageTracker.h> | 
| Alec Mouri | 05483a0 | 2018-09-10 21:03:42 +0000 | [diff] [blame] | 26 | #include <nativebase/nativebase.h> | 
| Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 27 | #include <utils/Trace.h> | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 28 | #include "GLESRenderEngine.h" | 
| Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 29 |  | 
|  | 30 | namespace android { | 
|  | 31 | namespace renderengine { | 
|  | 32 | namespace gl { | 
|  | 33 |  | 
| Alec Mouri | da4cf3b | 2019-02-12 15:33:01 -0800 | [diff] [blame] | 34 | GLFramebuffer::GLFramebuffer(GLESRenderEngine& engine) | 
|  | 35 | : mEngine(engine), mEGLDisplay(engine.getEGLDisplay()), mEGLImage(EGL_NO_IMAGE_KHR) { | 
| Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 36 | glGenTextures(1, &mTextureName); | 
|  | 37 | glGenFramebuffers(1, &mFramebufferName); | 
|  | 38 | } | 
|  | 39 |  | 
|  | 40 | GLFramebuffer::~GLFramebuffer() { | 
| Alec Mouri | 2725c09 | 2020-11-25 09:53:56 -0800 | [diff] [blame] | 41 | setNativeWindowBuffer(nullptr, false, false); | 
| Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 42 | glDeleteFramebuffers(1, &mFramebufferName); | 
|  | 43 | glDeleteTextures(1, &mTextureName); | 
| Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 44 | } | 
|  | 45 |  | 
| Alec Mouri | fe0d72b | 2019-03-21 14:05:56 -0700 | [diff] [blame] | 46 | bool GLFramebuffer::setNativeWindowBuffer(ANativeWindowBuffer* nativeBuffer, bool isProtected, | 
|  | 47 | const bool useFramebufferCache) { | 
| Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 48 | ATRACE_CALL(); | 
| Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 49 | if (mEGLImage != EGL_NO_IMAGE_KHR) { | 
| Alec Mouri | fe0d72b | 2019-03-21 14:05:56 -0700 | [diff] [blame] | 50 | if (!usingFramebufferCache) { | 
|  | 51 | eglDestroyImageKHR(mEGLDisplay, mEGLImage); | 
| Ady Abraham | a3b08ef | 2019-07-15 18:43:10 -0700 | [diff] [blame] | 52 | DEBUG_EGL_IMAGE_TRACKER_DESTROY(); | 
| Alec Mouri | fe0d72b | 2019-03-21 14:05:56 -0700 | [diff] [blame] | 53 | } | 
| Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 54 | mEGLImage = EGL_NO_IMAGE_KHR; | 
| Alec Mouri | 05483a0 | 2018-09-10 21:03:42 +0000 | [diff] [blame] | 55 | mBufferWidth = 0; | 
|  | 56 | mBufferHeight = 0; | 
| Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 57 | } | 
|  | 58 |  | 
|  | 59 | if (nativeBuffer) { | 
| Alec Mouri | fe0d72b | 2019-03-21 14:05:56 -0700 | [diff] [blame] | 60 | mEGLImage = mEngine.createFramebufferImageIfNeeded(nativeBuffer, isProtected, | 
|  | 61 | useFramebufferCache); | 
| Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 62 | if (mEGLImage == EGL_NO_IMAGE_KHR) { | 
|  | 63 | return false; | 
|  | 64 | } | 
| Alec Mouri | fe0d72b | 2019-03-21 14:05:56 -0700 | [diff] [blame] | 65 | usingFramebufferCache = useFramebufferCache; | 
| Alec Mouri | 05483a0 | 2018-09-10 21:03:42 +0000 | [diff] [blame] | 66 | mBufferWidth = nativeBuffer->width; | 
|  | 67 | mBufferHeight = nativeBuffer->height; | 
| Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 68 | } | 
|  | 69 | return true; | 
|  | 70 | } | 
|  | 71 |  | 
| Lingfeng Yang | 2e4fef6 | 2020-04-24 14:48:43 +0000 | [diff] [blame] | 72 | void GLFramebuffer::allocateBuffers(uint32_t width, uint32_t height, void* data) { | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 73 | ATRACE_CALL(); | 
|  | 74 |  | 
|  | 75 | glBindTexture(GL_TEXTURE_2D, mTextureName); | 
| Lingfeng Yang | 2e4fef6 | 2020-04-24 14:48:43 +0000 | [diff] [blame] | 76 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 77 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 
|  | 78 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | 
|  | 79 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); | 
|  | 80 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); | 
|  | 81 |  | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 82 | mBufferHeight = height; | 
|  | 83 | mBufferWidth = width; | 
|  | 84 | mEngine.checkErrors("Allocating Fbo texture"); | 
|  | 85 |  | 
|  | 86 | bind(); | 
|  | 87 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mTextureName, 0); | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 88 | mStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER); | 
|  | 89 | unbind(); | 
|  | 90 | glBindTexture(GL_TEXTURE_2D, 0); | 
|  | 91 |  | 
|  | 92 | if (mStatus != GL_FRAMEBUFFER_COMPLETE) { | 
|  | 93 | ALOGE("Frame buffer is not complete. Error %d", mStatus); | 
|  | 94 | } | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | void GLFramebuffer::bind() const { | 
|  | 98 | glBindFramebuffer(GL_FRAMEBUFFER, mFramebufferName); | 
|  | 99 | } | 
|  | 100 |  | 
| Lucas Dupin | f82ed8f | 2020-01-17 12:11:07 -0800 | [diff] [blame] | 101 | void GLFramebuffer::bindAsReadBuffer() const { | 
|  | 102 | glBindFramebuffer(GL_READ_FRAMEBUFFER, mFramebufferName); | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | void GLFramebuffer::bindAsDrawBuffer() const { | 
|  | 106 | glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebufferName); | 
|  | 107 | } | 
|  | 108 |  | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 109 | void GLFramebuffer::unbind() const { | 
|  | 110 | glBindFramebuffer(GL_FRAMEBUFFER, 0); | 
|  | 111 | } | 
|  | 112 |  | 
| Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 113 | } // namespace gl | 
|  | 114 | } // namespace renderengine | 
|  | 115 | } // namespace android |