Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 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 | #include "SkTypes.h" |
| 8 | |
Derek Sollenberger | 7a86987 | 2017-06-27 15:37:25 -0400 | [diff] [blame] | 9 | |
| 10 | #if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26 |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 11 | #define GL_GLEXT_PROTOTYPES |
| 12 | #define EGL_EGLEXT_PROTOTYPES |
| 13 | #include "GrAHardwareBufferImageGenerator.h" |
| 14 | |
Derek Sollenberger | 7a86987 | 2017-06-27 15:37:25 -0400 | [diff] [blame] | 15 | #include <android/hardware_buffer.h> |
| 16 | |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 17 | #include "GrBackendSurface.h" |
| 18 | #include "GrContext.h" |
| 19 | #include "GrContextPriv.h" |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 20 | #include "GrProxyProvider.h" |
Stan Iliev | dbba55d | 2017-06-28 13:24:41 -0400 | [diff] [blame] | 21 | #include "GrResourceCache.h" |
Robert Phillips | 0001828 | 2017-06-15 15:35:16 -0400 | [diff] [blame] | 22 | #include "GrResourceProvider.h" |
Robert Phillips | 847d4c5 | 2017-06-13 18:21:44 -0400 | [diff] [blame] | 23 | #include "GrTexture.h" |
Robert Phillips | ade9f61 | 2017-06-16 07:32:43 -0400 | [diff] [blame] | 24 | #include "GrTextureProxy.h" |
Stan Iliev | dbba55d | 2017-06-28 13:24:41 -0400 | [diff] [blame] | 25 | #include "SkMessageBus.h" |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 26 | #include "gl/GrGLDefines.h" |
| 27 | #include "gl/GrGLTypes.h" |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 28 | |
| 29 | #include <EGL/egl.h> |
| 30 | #include <EGL/eglext.h> |
| 31 | #include <GLES/gl.h> |
| 32 | #include <GLES/glext.h> |
| 33 | |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 34 | #define PROT_CONTENT_EXT_STR "EGL_EXT_protected_content" |
| 35 | #define EGL_PROTECTED_CONTENT_EXT 0x32C0 |
| 36 | |
| 37 | static bool can_import_protected_content_eglimpl() { |
| 38 | EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 39 | const char* exts = eglQueryString(dpy, EGL_EXTENSIONS); |
| 40 | size_t cropExtLen = strlen(PROT_CONTENT_EXT_STR); |
| 41 | size_t extsLen = strlen(exts); |
| 42 | bool equal = !strcmp(PROT_CONTENT_EXT_STR, exts); |
| 43 | bool atStart = !strncmp(PROT_CONTENT_EXT_STR " ", exts, cropExtLen+1); |
| 44 | bool atEnd = (cropExtLen+1) < extsLen |
| 45 | && !strcmp(" " PROT_CONTENT_EXT_STR, |
| 46 | exts + extsLen - (cropExtLen+1)); |
| 47 | bool inMiddle = strstr(exts, " " PROT_CONTENT_EXT_STR " "); |
| 48 | return equal || atStart || atEnd || inMiddle; |
| 49 | } |
| 50 | |
| 51 | static bool can_import_protected_content(GrContext* context) { |
| 52 | if (kOpenGL_GrBackend == context->contextPriv().getBackend()) { |
| 53 | // Only compute whether the extension is present once the first time this |
| 54 | // function is called. |
| 55 | static bool hasIt = can_import_protected_content_eglimpl(); |
| 56 | return hasIt; |
| 57 | } |
| 58 | return false; |
| 59 | } |
| 60 | |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 61 | std::unique_ptr<SkImageGenerator> GrAHardwareBufferImageGenerator::Make( |
| 62 | AHardwareBuffer* graphicBuffer, SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace) { |
| 63 | AHardwareBuffer_Desc bufferDesc; |
| 64 | AHardwareBuffer_describe(graphicBuffer, &bufferDesc); |
| 65 | SkColorType colorType; |
| 66 | switch (bufferDesc.format) { |
| 67 | case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM: |
| 68 | colorType = kRGBA_8888_SkColorType; |
| 69 | break; |
| 70 | case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT: |
| 71 | colorType = kRGBA_F16_SkColorType; |
| 72 | break; |
| 73 | case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM: |
| 74 | colorType = kRGB_565_SkColorType; |
| 75 | break; |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 76 | case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM: |
| 77 | case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM: |
| 78 | colorType = kRGB_888x_SkColorType; |
| 79 | break; |
| 80 | case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM: |
| 81 | colorType = kRGBA_1010102_SkColorType; |
| 82 | break; |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 83 | default: |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 84 | // Given that we only use this texture as a source, colorType will not impact how Skia uses |
| 85 | // the texture. The only potential affect this is anticipated to have is that for some |
| 86 | // format types if we are not bound as an OES texture we may get invalid results for SKP |
| 87 | // capture if we read back the texture. |
| 88 | colorType = kRGBA_8888_SkColorType; |
| 89 | break; |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 90 | } |
| 91 | SkImageInfo info = SkImageInfo::Make(bufferDesc.width, bufferDesc.height, colorType, |
| 92 | alphaType, std::move(colorSpace)); |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 93 | bool createProtectedImage = 0 != (bufferDesc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 94 | return std::unique_ptr<SkImageGenerator>(new GrAHardwareBufferImageGenerator(info, graphicBuffer, |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 95 | alphaType, createProtectedImage, bufferDesc.format)); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | GrAHardwareBufferImageGenerator::GrAHardwareBufferImageGenerator(const SkImageInfo& info, |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 99 | AHardwareBuffer* hardwareBuffer, SkAlphaType alphaType, bool isProtectedContent, |
| 100 | uint32_t bufferFormat) |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 101 | : INHERITED(info) |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 102 | , fHardwareBuffer(hardwareBuffer) |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 103 | , fBufferFormat(bufferFormat) |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 104 | , fIsProtectedContent(isProtectedContent) { |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 105 | AHardwareBuffer_acquire(fHardwareBuffer); |
| 106 | } |
| 107 | |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 108 | GrAHardwareBufferImageGenerator::~GrAHardwareBufferImageGenerator() { |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 109 | AHardwareBuffer_release(fHardwareBuffer); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 110 | } |
| 111 | |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 112 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 113 | |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame^] | 114 | class GLCleanupHelper { |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 115 | public: |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame^] | 116 | GLCleanupHelper(GrGLuint texID, EGLImageKHR image, EGLDisplay display) |
| 117 | : fTexID(texID) |
| 118 | , fImage(image) |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 119 | , fDisplay(display) { } |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame^] | 120 | ~GLCleanupHelper() { |
| 121 | glDeleteTextures(1, &fTexID); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 122 | // eglDestroyImageKHR will remove a ref from the AHardwareBuffer |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 123 | eglDestroyImageKHR(fDisplay, fImage); |
| 124 | } |
| 125 | private: |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame^] | 126 | GrGLuint fTexID; |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 127 | EGLImageKHR fImage; |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame^] | 128 | EGLDisplay fDisplay; |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame^] | 132 | void GrAHardwareBufferImageGenerator::DeleteGLTexture(void* context) { |
| 133 | GLCleanupHelper* cleanupHelper = static_cast<GLCleanupHelper*>(context); |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 134 | delete cleanupHelper; |
| 135 | } |
| 136 | |
| 137 | static GrBackendTexture make_gl_backend_texture( |
| 138 | GrContext* context, AHardwareBuffer* hardwareBuffer, |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 139 | int width, int height, GrPixelConfig config, |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 140 | GrAHardwareBufferImageGenerator::DeleteImageProc* deleteProc, |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 141 | GrAHardwareBufferImageGenerator::DeleteImageCtx* deleteCtx, |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 142 | bool isProtectedContent, |
| 143 | const GrBackendFormat& backendFormat) { |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 144 | while (GL_NO_ERROR != glGetError()) {} //clear GL errors |
| 145 | |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 146 | EGLClientBuffer clientBuffer = eglGetNativeClientBufferANDROID(hardwareBuffer); |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 147 | EGLint attribs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 148 | isProtectedContent ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE, |
| 149 | isProtectedContent ? EGL_TRUE : EGL_NONE, |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 150 | EGL_NONE }; |
| 151 | EGLDisplay display = eglGetCurrentDisplay(); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 152 | // eglCreateImageKHR will add a ref to the AHardwareBuffer |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 153 | EGLImageKHR image = eglCreateImageKHR(display, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, |
| 154 | clientBuffer, attribs); |
| 155 | if (EGL_NO_IMAGE_KHR == image) { |
| 156 | SkDebugf("Could not create EGL image, err = (%#x)", (int) eglGetError() ); |
| 157 | return GrBackendTexture(); |
| 158 | } |
| 159 | |
| 160 | GrGLuint texID; |
| 161 | glGenTextures(1, &texID); |
| 162 | if (!texID) { |
| 163 | eglDestroyImageKHR(display, image); |
| 164 | return GrBackendTexture(); |
| 165 | } |
| 166 | glBindTexture(GL_TEXTURE_EXTERNAL_OES, texID); |
| 167 | GLenum status = GL_NO_ERROR; |
| 168 | if ((status = glGetError()) != GL_NO_ERROR) { |
| 169 | SkDebugf("glBindTexture failed (%#x)", (int) status); |
| 170 | glDeleteTextures(1, &texID); |
| 171 | eglDestroyImageKHR(display, image); |
| 172 | return GrBackendTexture(); |
| 173 | } |
| 174 | glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, image); |
| 175 | if ((status = glGetError()) != GL_NO_ERROR) { |
| 176 | SkDebugf("glEGLImageTargetTexture2DOES failed (%#x)", (int) status); |
| 177 | glDeleteTextures(1, &texID); |
| 178 | eglDestroyImageKHR(display, image); |
| 179 | return GrBackendTexture(); |
| 180 | } |
| 181 | context->resetContext(kTextureBinding_GrGLBackendState); |
| 182 | |
| 183 | GrGLTextureInfo textureInfo; |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 184 | textureInfo.fID = texID; |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 185 | SkASSERT(backendFormat.isValid()); |
| 186 | textureInfo.fTarget = *backendFormat.getGLTarget(); |
| 187 | textureInfo.fFormat = *backendFormat.getGLFormat(); |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 188 | |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame^] | 189 | *deleteProc = GrAHardwareBufferImageGenerator::DeleteGLTexture; |
| 190 | *deleteCtx = new GLCleanupHelper(texID, image, display); |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 191 | |
| 192 | return GrBackendTexture(width, height, GrMipMapped::kNo, textureInfo); |
| 193 | } |
| 194 | |
| 195 | static GrBackendTexture make_backend_texture( |
| 196 | GrContext* context, AHardwareBuffer* hardwareBuffer, |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 197 | int width, int height, GrPixelConfig config, |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 198 | GrAHardwareBufferImageGenerator::DeleteImageProc* deleteProc, |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 199 | GrAHardwareBufferImageGenerator::DeleteImageCtx* deleteCtx, |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 200 | bool isProtectedContent, |
| 201 | const GrBackendFormat& backendFormat) { |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 202 | if (context->abandoned() || kOpenGL_GrBackend != context->contextPriv().getBackend()) { |
| 203 | // Check if GrContext is not abandoned and the backend is GL. |
| 204 | return GrBackendTexture(); |
| 205 | } |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 206 | bool createProtectedImage = isProtectedContent && can_import_protected_content(context); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 207 | return make_gl_backend_texture(context, hardwareBuffer, width, height, config, deleteProc, |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 208 | deleteCtx, createProtectedImage, backendFormat); |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 209 | } |
| 210 | |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 211 | GrBackendFormat get_backend_format(GrBackend backend, uint32_t bufferFormat) { |
| 212 | if (backend == kOpenGL_GrBackend) { |
| 213 | switch (bufferFormat) { |
| 214 | //TODO: find out if we can detect, which graphic buffers support GR_GL_TEXTURE_2D |
| 215 | case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM: |
| 216 | return GrBackendFormat::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_EXTERNAL); |
| 217 | case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT: |
| 218 | return GrBackendFormat::MakeGL(GR_GL_RGBA16F, GR_GL_TEXTURE_EXTERNAL); |
| 219 | case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM: |
| 220 | return GrBackendFormat::MakeGL(GR_GL_RGB565, GR_GL_TEXTURE_EXTERNAL); |
| 221 | case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM: |
| 222 | return GrBackendFormat::MakeGL(GR_GL_RGB10_A2, GR_GL_TEXTURE_EXTERNAL); |
| 223 | case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM: |
| 224 | case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM: |
| 225 | return GrBackendFormat::MakeGL(GR_GL_RGB8, GR_GL_TEXTURE_EXTERNAL); |
| 226 | default: |
| 227 | return GrBackendFormat::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_EXTERNAL); |
| 228 | } |
| 229 | } |
| 230 | return GrBackendFormat(); |
| 231 | } |
| 232 | |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame^] | 233 | sk_sp<GrTextureProxy> GrAHardwareBufferImageGenerator::makeProxy(GrContext* context) { |
Khushal | c421ca1 | 2018-06-26 14:38:34 -0700 | [diff] [blame] | 234 | if (context->abandoned() || kOpenGL_GrBackend != context->contextPriv().getBackend()) { |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 235 | // Check if GrContext is not abandoned and the backend is GL. |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame^] | 236 | return nullptr; |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 237 | } |
| 238 | |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 239 | GrPixelConfig pixelConfig; |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 240 | GrBackendFormat backendFormat = get_backend_format(context->contextPriv().getBackend(), |
| 241 | fBufferFormat); |
| 242 | if (!context->contextPriv().caps()->getConfigFromBackendFormat( |
| 243 | backendFormat, this->getInfo().colorType(), &pixelConfig)) { |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame^] | 244 | return nullptr; |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 245 | } |
| 246 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 247 | int width = this->getInfo().width(); |
| 248 | int height = this->getInfo().height(); |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 249 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 250 | GrSurfaceDesc desc; |
| 251 | desc.fWidth = width; |
| 252 | desc.fHeight = height; |
| 253 | desc.fConfig = pixelConfig; |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 254 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 255 | GrTextureType textureType = GrTextureType::k2D; |
| 256 | if (context->contextPriv().getBackend() == kOpenGL_GrBackend) { |
| 257 | textureType = GrTextureType::kExternal; |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 258 | } |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 259 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 260 | auto proxyProvider = context->contextPriv().proxyProvider(); |
| 261 | |
| 262 | AHardwareBuffer* hardwareBuffer = fHardwareBuffer; |
| 263 | AHardwareBuffer_acquire(hardwareBuffer); |
| 264 | |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 265 | const bool isProtectedContent = fIsProtectedContent; |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 266 | |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame^] | 267 | sk_sp<GrTextureProxy> texProxy = proxyProvider->createLazyProxy( |
| 268 | [context, hardwareBuffer, width, height, pixelConfig, isProtectedContent, backendFormat] |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 269 | (GrResourceProvider* resourceProvider) { |
| 270 | if (!resourceProvider) { |
| 271 | AHardwareBuffer_release(hardwareBuffer); |
| 272 | return sk_sp<GrTexture>(); |
| 273 | } |
| 274 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 275 | DeleteImageProc deleteImageProc = nullptr; |
| 276 | DeleteImageCtx deleteImageCtx = nullptr; |
| 277 | |
| 278 | GrBackendTexture backendTex = make_backend_texture(context, hardwareBuffer, |
| 279 | width, height, pixelConfig, |
| 280 | &deleteImageProc, |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 281 | &deleteImageCtx, |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 282 | isProtectedContent, |
| 283 | backendFormat); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 284 | if (!backendTex.isValid()) { |
| 285 | return sk_sp<GrTexture>(); |
| 286 | } |
| 287 | SkASSERT(deleteImageProc && deleteImageCtx); |
| 288 | |
| 289 | backendTex.fConfig = pixelConfig; |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame^] | 290 | sk_sp<GrTexture> tex = resourceProvider->wrapBackendTexture(backendTex); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 291 | if (!tex) { |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 292 | deleteImageProc(deleteImageCtx); |
| 293 | return sk_sp<GrTexture>(); |
| 294 | } |
| 295 | |
| 296 | sk_sp<GrReleaseProcHelper> releaseProcHelper( |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 297 | new GrReleaseProcHelper(deleteImageProc, deleteImageCtx)); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 298 | tex->setRelease(releaseProcHelper); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 299 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 300 | return tex; |
| 301 | }, |
| 302 | desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, textureType, SkBackingFit::kExact, |
| 303 | SkBudgeted::kNo); |
| 304 | |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame^] | 305 | if (!texProxy) { |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 306 | AHardwareBuffer_release(hardwareBuffer); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 307 | } |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame^] | 308 | return texProxy; |
| 309 | } |
| 310 | |
| 311 | sk_sp<GrTextureProxy> GrAHardwareBufferImageGenerator::onGenerateTexture( |
| 312 | GrContext* context, const SkImageInfo& info, const SkIPoint& origin, bool willNeedMipMaps) { |
| 313 | sk_sp<GrTextureProxy> texProxy = this->makeProxy(context); |
| 314 | if (!texProxy) { |
| 315 | return nullptr; |
| 316 | } |
| 317 | |
| 318 | if (0 == origin.fX && 0 == origin.fY && |
| 319 | info.width() == this->getInfo().width() && info.height() == this->getInfo().height()) { |
| 320 | // If the caller wants the full texture we're done. The caller will handle making a copy for |
| 321 | // mip maps if that is required. |
| 322 | return texProxy; |
| 323 | } |
| 324 | // Otherwise, make a copy for the requested subset. |
| 325 | SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height()); |
| 326 | |
| 327 | GrMipMapped mipMapped = willNeedMipMaps ? GrMipMapped::kYes : GrMipMapped::kNo; |
| 328 | |
| 329 | return GrSurfaceProxy::Copy(context, texProxy.get(), mipMapped, subset, SkBudgeted::kYes); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 330 | } |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 331 | |
| 332 | bool GrAHardwareBufferImageGenerator::onIsValid(GrContext* context) const { |
| 333 | if (nullptr == context) { |
| 334 | return false; //CPU backend is not supported, because hardware buffer can be swizzled |
| 335 | } |
| 336 | // TODO: add Vulkan support |
| 337 | return kOpenGL_GrBackend == context->contextPriv().getBackend(); |
| 338 | } |
| 339 | |
| 340 | #endif //SK_BUILD_FOR_ANDROID_FRAMEWORK |