Debian Codegraph @

src/gpu/ganesh/gl/AHardwareBufferGL.cpp

raw
1 /*
2 * Copyright 2023 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "include/android/GrAHardwareBufferUtils.h"
9
10 #if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26
11 #define GL_GLEXT_PROTOTYPES
12 #define EGL_EGLEXT_PROTOTYPES
13
14 #include "include/gpu/ganesh/GrBackendSurface.h"
15 #include "include/gpu/ganesh/GrDirectContext.h"
16 #include "include/gpu/ganesh/gl/GrGLBackendSurface.h"
17 #include "include/gpu/ganesh/gl/GrGLTypes.h"
18 #include "src/gpu/ganesh/GrDirectContextPriv.h"
19 #include "src/gpu/ganesh/gl/GrGLDefines.h"
20 #include "src/gpu/ganesh/gl/GrGLUtil.h"
21
22 #include <android/hardware_buffer.h>
23 #include <EGL/egl.h>
24 #include <EGL/eglext.h>
25 #include <GLES/gl.h>
26 #include <GLES/glext.h>
27
28 #define PROT_CONTENT_EXT_STR "EGL_EXT_protected_content"
29 #define EGL_PROTECTED_CONTENT_EXT 0x32C0
30
31 namespace GrAHardwareBufferUtils {
32
33 GrBackendFormat GetGLBackendFormat(GrDirectContext* dContext,
34 uint32_t bufferFormat, bool requireKnownFormat) {
35 GrBackendApi backend = dContext->backend();
36 if (backend != GrBackendApi::kOpenGL) {
37 return GrBackendFormat();
38 }
39 switch (bufferFormat) {
40 case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
41 return GrBackendFormats::MakeGL(GR_GL_RGBA8);
42 case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
43 // According to https://developer.android.com/ndk/reference/group/a-hardware-buffer,
44 // the GL format equivalent is GL_RGB8 but that is considered to be 24bpp throughout
45 // Ganesh. For now keep it as an external format until we figure out the RGB vs RGBA
46 // compatibility.
47 return GrBackendFormats::MakeGLExternal();
48 case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT:
49 return GrBackendFormats::MakeGL(GR_GL_RGBA16F);
50 case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
51 return GrBackendFormats::MakeGL(GR_GL_RGB565);
52 case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM:
53 return GrBackendFormats::MakeGL(GR_GL_RGB10_A2);
54 case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
55 return GrBackendFormats::MakeGL(GR_GL_RGB8);
56 #if __ANDROID_API__ >= 33
57 case AHARDWAREBUFFER_FORMAT_R8_UNORM:
58 return GrBackendFormats::MakeGL(GR_GL_R8);
59 #endif
60 #if __ANDROID_API__ >= 34
61 case AHARDWAREBUFFER_FORMAT_R10G10B10A10_UNORM:
62 // There is no GLFormat enum that corresponds to this texture format, but it is known
63 // (GrColorType and SkColorType can describe it).
64 return GrBackendFormats::MakeGLExternal();
65 #endif
66 default:
67 if (requireKnownFormat) {
68 return GrBackendFormat();
69 } else {
70 return GrBackendFormats::MakeGLExternal();
71 }
72 }
73 SkUNREACHABLE;
74 }
75
76 class GLTextureHelper {
77 public:
78 GLTextureHelper(GrGLuint texID, EGLImageKHR image, EGLDisplay display, GrGLuint texTarget)
79 : fTexID(texID)
80 , fImage(image)
81 , fDisplay(display)
82 , fTexTarget(texTarget) { }
83 ~GLTextureHelper() {
84 glDeleteTextures(1, &fTexID);
85 // eglDestroyImageKHR will remove a ref from the AHardwareBuffer
86 eglDestroyImageKHR(fDisplay, fImage);
87 }
88 void rebind(GrDirectContext*);
89
90 private:
91 GrGLuint fTexID;
92 EGLImageKHR fImage;
93 EGLDisplay fDisplay;
94 GrGLuint fTexTarget;
95 };
96
97 void GLTextureHelper::rebind(GrDirectContext* dContext) {
98 glBindTexture(fTexTarget, fTexID);
99 GLenum status = GL_NO_ERROR;
100 if ((status = glGetError()) != GL_NO_ERROR) {
101 SkDebugf("glBindTexture(%#x, %d) failed (%#x)", (int) fTexTarget,
102 (int) fTexID, (int) status);
103 return;
104 }
105 glEGLImageTargetTexture2DOES(fTexTarget, fImage);
106 if ((status = glGetError()) != GL_NO_ERROR) {
107 SkDebugf("glEGLImageTargetTexture2DOES failed (%#x)", (int) status);
108 return;
109 }
110 dContext->resetContext(kTextureBinding_GrGLBackendState);
111 }
112
113 void delete_gl_texture(void* context) {
114 GLTextureHelper* cleanupHelper = static_cast<GLTextureHelper*>(context);
115 delete cleanupHelper;
116 }
117
118 void update_gl_texture(void* context, GrDirectContext* dContext) {
119 GLTextureHelper* cleanupHelper = static_cast<GLTextureHelper*>(context);
120 cleanupHelper->rebind(dContext);
121 }
122
123 static GrBackendTexture make_gl_backend_texture(
124 GrDirectContext* dContext,
125 AHardwareBuffer* hardwareBuffer,
126 int width, int height,
127 DeleteImageProc* deleteProc,
128 UpdateImageProc* updateProc,
129 TexImageCtx* imageCtx,
130 bool isProtectedContent,
131 const GrBackendFormat& backendFormat) {
132 while (GL_NO_ERROR != glGetError()) {} //clear GL errors
133
134 auto textureType = backendFormat.textureType();
135 if (textureType != GrTextureType::k2D && textureType != GrTextureType::kExternal) {
136 SkDebugf("Unsupported texture target type: %d\n", (int) textureType);
137 return GrBackendTexture();
138 }
139
140 EGLClientBuffer clientBuffer = eglGetNativeClientBufferANDROID(hardwareBuffer);
141 EGLint attribs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
142 isProtectedContent ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE,
143 isProtectedContent ? EGL_TRUE : EGL_NONE,
144 EGL_NONE };
145 EGLDisplay display = eglGetCurrentDisplay();
146 // eglCreateImageKHR will add a ref to the AHardwareBuffer
147 EGLImageKHR image = eglCreateImageKHR(display, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
148 clientBuffer, attribs);
149 if (EGL_NO_IMAGE_KHR == image) {
150 SkDebugf("Could not create EGL image, err = (%#x)", (int) eglGetError() );
151 return GrBackendTexture();
152 }
153
154 GrGLuint texID;
155 glGenTextures(1, &texID);
156 if (!texID) {
157 eglDestroyImageKHR(display, image);
158 return GrBackendTexture();
159 }
160
161 GrGLuint target = textureType == GrTextureType::kExternal ? GR_GL_TEXTURE_EXTERNAL
162 : GR_GL_TEXTURE_2D;
163
164 if (!dContext->priv().caps()->shaderCaps()->fExternalTextureSupport &&
165 textureType == GrTextureType::kExternal) {
166 // The extension OES_EGL_image_external is mandatory for an
167 // Android-compatible device. See
168 // https://source.android.com/docs/core/graphics/implement-opengl-es
169 //
170 // This path exists to support a RenderDoc fork which supports
171 // OES_EGL_image but not OES_EGL_image_external.
172 target = GR_GL_TEXTURE_2D;
173 }
174
175 glBindTexture(target, texID);
176 GLenum status = GL_NO_ERROR;
177 if ((status = glGetError()) != GL_NO_ERROR) {
178 SkDebugf("glBindTexture failed (%#x)", (int) status);
179 glDeleteTextures(1, &texID);
180 eglDestroyImageKHR(display, image);
181 return GrBackendTexture();
182 }
183 glEGLImageTargetTexture2DOES(target, image);
184 if ((status = glGetError()) != GL_NO_ERROR) {
185 SkDebugf("glEGLImageTargetTexture2DOES failed (%#x)", (int) status);
186 glDeleteTextures(1, &texID);
187 eglDestroyImageKHR(display, image);
188 return GrBackendTexture();
189 }
190 dContext->resetContext(kTextureBinding_GrGLBackendState);
191
192 GrGLTextureInfo textureInfo;
193 textureInfo.fID = texID;
194 SkASSERT(backendFormat.isValid());
195 textureInfo.fTarget = target;
196 textureInfo.fFormat = GrBackendFormats::AsGLFormatEnum(backendFormat);
197 textureInfo.fProtected = skgpu::Protected(isProtectedContent);
198
199 *deleteProc = delete_gl_texture;
200 *updateProc = update_gl_texture;
201 *imageCtx = new GLTextureHelper(texID, image, display, target);
202
203 return GrBackendTextures::MakeGL(width, height, skgpu::Mipmapped::kNo, textureInfo);
204 }
205
206 static bool can_import_protected_content_eglimpl() {
207 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
208 const char* exts = eglQueryString(dpy, EGL_EXTENSIONS);
209 size_t cropExtLen = strlen(PROT_CONTENT_EXT_STR);
210 size_t extsLen = strlen(exts);
211 bool equal = !strcmp(PROT_CONTENT_EXT_STR, exts);
212 bool atStart = !strncmp(PROT_CONTENT_EXT_STR " ", exts, cropExtLen+1);
213 bool atEnd = (cropExtLen+1) < extsLen
214 && !strcmp(" " PROT_CONTENT_EXT_STR,
215 exts + extsLen - (cropExtLen+1));
216 bool inMiddle = strstr(exts, " " PROT_CONTENT_EXT_STR " ");
217 return equal || atStart || atEnd || inMiddle;
218 }
219
220 static bool can_import_protected_content(GrDirectContext* dContext) {
221 SkASSERT(GrBackendApi::kOpenGL == dContext->backend());
222 // Only compute whether the extension is present once the first time this
223 // function is called.
224 static bool hasIt = can_import_protected_content_eglimpl();
225 return hasIt;
226 }
227
228 GrBackendTexture MakeGLBackendTexture(GrDirectContext* dContext,
229 AHardwareBuffer* hardwareBuffer,
230 int width, int height,
231 DeleteImageProc* deleteProc,
232 UpdateImageProc* updateProc,
233 TexImageCtx* imageCtx,
234 bool isProtectedContent,
235 const GrBackendFormat& backendFormat,
236 bool isRenderable) {
237 SkASSERT(dContext);
238 if (!dContext || dContext->abandoned()) {
239 return GrBackendTexture();
240 }
241
242 if (GrBackendApi::kOpenGL != dContext->backend()) {
243 return GrBackendTexture();
244 }
245
246 if (isProtectedContent && !can_import_protected_content(dContext)) {
247 return GrBackendTexture();
248 }
249
250 if (isRenderable &&
251 !dContext->priv().caps()->isFormatRenderable(backendFormat, /*sampleCount=*/1)) {
252 return GrBackendTexture();
253 }
254
255 return make_gl_backend_texture(dContext, hardwareBuffer, width, height, deleteProc,
256 updateProc, imageCtx, isProtectedContent, backendFormat);
257 }
258
259 } // namespace GrAHardwareBufferUtils
260
261 #endif