1 /*
2 * Copyright (c) 2016-2017, 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()100 unsigned int EGLImageBuffer::getTexture()
101 //-----------------------------------------------------------------------------
102 {
103 if (textureID == 0) {
104 bindAsTexture();
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()122 void EGLImageBuffer::bindAsTexture()
123 //-----------------------------------------------------------------------------
124 {
125 if (textureID == 0) {
126 GL(glGenTextures(1, &textureID));
127 int target = 0x8D65;
128 GL(glBindTexture(target, textureID));
129 GL(glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
130 GL(glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
131 GL(glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
132 GL(glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
133
134 GL(glEGLImageTargetTexture2DOES(0x8D65, eglImageID));
135 }
136
137 GL(glBindTexture(0x8D65, textureID));
138 }
139
140 //-----------------------------------------------------------------------------
bindAsFramebuffer()141 void EGLImageBuffer::bindAsFramebuffer()
142 //-----------------------------------------------------------------------------
143 {
144 if (renderbufferID == 0) {
145 GL(glGenFramebuffers(1, &framebufferID));
146 GL(glGenRenderbuffers(1, &renderbufferID));
147
148 GL(glBindRenderbuffer(GL_RENDERBUFFER, renderbufferID));
149 GL(glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER, eglImageID));
150
151 GL(glBindFramebuffer(GL_FRAMEBUFFER, framebufferID));
152 GL(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
153 renderbufferID));
154 GLenum result = glCheckFramebufferStatus(GL_FRAMEBUFFER);
155 if (result != GL_FRAMEBUFFER_COMPLETE) {
156 ALOGI("%s Framebuffer Invalid***************", __FUNCTION__);
157 }
158 }
159
160 GL(glBindFramebuffer(GL_FRAMEBUFFER, framebufferID));
161 }
162