1 /*
2  * Copyright (c) 2016-2017, 2019 The Linux Foundation. All rights reserved.
3  * Not a Contribution.
4  *
5  * Copyright 2015 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #include "EGLImageBuffer.h"
21 #include <cutils/native_handle.h>
22 #include <gralloc_priv.h>
23 #include <ui/GraphicBuffer.h>
24 #include <map>
25 #include "EGLImageWrapper.h"
26 #include "glengine.h"
27 
28 //-----------------------------------------------------------------------------
create_eglImage(android::sp<android::GraphicBuffer> graphicBuffer)29 EGLImageKHR create_eglImage(android::sp<android::GraphicBuffer> graphicBuffer)
30 //-----------------------------------------------------------------------------
31 {
32   bool isProtected = (graphicBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
33   EGLint attrs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
34                     isProtected ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE,
35                     isProtected ? EGL_TRUE : EGL_NONE, EGL_NONE};
36 
37   EGLImageKHR eglImage = eglCreateImageKHR(
38       eglGetCurrentDisplay(), (EGLContext)EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
39       (EGLClientBuffer)(graphicBuffer->getNativeBuffer()), attrs);
40 
41   return eglImage;
42 }
43 
44 //-----------------------------------------------------------------------------
EGLImageBuffer(android::sp<android::GraphicBuffer> graphicBuffer)45 EGLImageBuffer::EGLImageBuffer(android::sp<android::GraphicBuffer> graphicBuffer)
46 //-----------------------------------------------------------------------------
47 {
48   // this->graphicBuffer = graphicBuffer;
49   this->eglImageID = create_eglImage(graphicBuffer);
50   this->width = graphicBuffer->getWidth();
51   this->height = graphicBuffer->getHeight();
52 
53   textureID = 0;
54   renderbufferID = 0;
55   framebufferID = 0;
56 }
57 
58 //-----------------------------------------------------------------------------
~EGLImageBuffer()59 EGLImageBuffer::~EGLImageBuffer()
60 //-----------------------------------------------------------------------------
61 {
62   if (textureID != 0) {
63     GL(glDeleteTextures(1, &textureID));
64     textureID = 0;
65   }
66 
67   if (renderbufferID != 0) {
68     GL(glDeleteRenderbuffers(1, &renderbufferID));
69     renderbufferID = 0;
70   }
71 
72   if (framebufferID != 0) {
73     GL(glDeleteFramebuffers(1, &framebufferID));
74     framebufferID = 0;
75   }
76 
77   // Delete the eglImage
78   if (eglImageID != 0)
79   {
80       eglDestroyImageKHR(eglGetCurrentDisplay(), eglImageID);
81       eglImageID = 0;
82   }
83 }
84 
85 //-----------------------------------------------------------------------------
getWidth()86 int EGLImageBuffer::getWidth()
87 //-----------------------------------------------------------------------------
88 {
89   return width;
90 }
91 
92 //-----------------------------------------------------------------------------
getHeight()93 int EGLImageBuffer::getHeight()
94 //-----------------------------------------------------------------------------
95 {
96   return height;
97 }
98 
99 //-----------------------------------------------------------------------------
getTexture(int target)100 unsigned int EGLImageBuffer::getTexture(int target)
101 //-----------------------------------------------------------------------------
102 {
103   if (textureID == 0) {
104     bindAsTexture(target);
105   }
106 
107   return textureID;
108 }
109 
110 //-----------------------------------------------------------------------------
getFramebuffer()111 unsigned int EGLImageBuffer::getFramebuffer()
112 //-----------------------------------------------------------------------------
113 {
114   if (framebufferID == 0) {
115     bindAsFramebuffer();
116   }
117 
118   return framebufferID;
119 }
120 
121 //-----------------------------------------------------------------------------
bindAsTexture(int target)122 void EGLImageBuffer::bindAsTexture(int target)
123 //-----------------------------------------------------------------------------
124 {
125   if (textureID == 0) {
126     GL(glGenTextures(1, &textureID));
127     GL(glBindTexture(target, textureID));
128     GL(glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
129     GL(glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
130     GL(glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
131     GL(glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
132 
133     GL(glEGLImageTargetTexture2DOES(target, eglImageID));
134   }
135 
136   GL(glBindTexture(target, textureID));
137 }
138 
139 //-----------------------------------------------------------------------------
bindAsFramebuffer()140 void EGLImageBuffer::bindAsFramebuffer()
141 //-----------------------------------------------------------------------------
142 {
143   if (renderbufferID == 0) {
144     GL(glGenFramebuffers(1, &framebufferID));
145     GL(glGenRenderbuffers(1, &renderbufferID));
146 
147     GL(glBindRenderbuffer(GL_RENDERBUFFER, renderbufferID));
148     GL(glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER, eglImageID));
149 
150     GL(glBindFramebuffer(GL_FRAMEBUFFER, framebufferID));
151     GL(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
152                                  renderbufferID));
153     GLenum result = glCheckFramebufferStatus(GL_FRAMEBUFFER);
154     if (result != GL_FRAMEBUFFER_COMPLETE) {
155       ALOGI("%s Framebuffer Invalid***************", __FUNCTION__);
156     }
157   }
158 
159   GL(glBindFramebuffer(GL_FRAMEBUFFER, framebufferID));
160 }
161