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; |
| 76 | default: |
| 77 | return nullptr; |
| 78 | } |
| 79 | SkImageInfo info = SkImageInfo::Make(bufferDesc.width, bufferDesc.height, colorType, |
| 80 | alphaType, std::move(colorSpace)); |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame^] | 81 | bool createProtectedImage = 0 != (bufferDesc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 82 | return std::unique_ptr<SkImageGenerator>(new GrAHardwareBufferImageGenerator(info, graphicBuffer, |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame^] | 83 | alphaType, createProtectedImage)); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | GrAHardwareBufferImageGenerator::GrAHardwareBufferImageGenerator(const SkImageInfo& info, |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame^] | 87 | AHardwareBuffer* hardwareBuffer, SkAlphaType alphaType, bool isProtectedContent) |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 88 | : INHERITED(info) |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame^] | 89 | , fHardwareBuffer(hardwareBuffer) |
| 90 | , fIsProtectedContent(isProtectedContent) { |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 91 | AHardwareBuffer_acquire(fHardwareBuffer); |
| 92 | } |
| 93 | |
| 94 | void GrAHardwareBufferImageGenerator::releaseTextureRef() { |
| 95 | // We must release our ref on the proxy before we send the free message to the actual texture so |
| 96 | // that we make sure the last ref (if it is owned by this class) is released on the owning |
| 97 | // context. |
| 98 | fCachedProxy.reset(); |
| 99 | if (fOwnedTexture) { |
| 100 | SkASSERT(fOwningContextID != SK_InvalidGenID); |
| 101 | // Notify the original cache that it can free the last ref, so it happens on the correct |
| 102 | // thread. |
| 103 | GrGpuResourceFreedMessage msg { fOwnedTexture, fOwningContextID }; |
| 104 | SkMessageBus<GrGpuResourceFreedMessage>::Post(msg); |
| 105 | fOwnedTexture = nullptr; |
| 106 | fOwningContextID = SK_InvalidGenID; |
| 107 | } |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | GrAHardwareBufferImageGenerator::~GrAHardwareBufferImageGenerator() { |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 111 | this->releaseTextureRef(); |
| 112 | AHardwareBuffer_release(fHardwareBuffer); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 113 | } |
| 114 | |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 115 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 116 | |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 117 | sk_sp<GrTextureProxy> GrAHardwareBufferImageGenerator::onGenerateTexture( |
Brian Osman | c87cfb6 | 2018-07-11 09:08:46 -0400 | [diff] [blame] | 118 | GrContext* context, const SkImageInfo& info, const SkIPoint& origin, bool willNeedMipMaps) { |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 119 | this->makeProxy(context); |
| 120 | if (!fCachedProxy) { |
Stan Iliev | dbba55d | 2017-06-28 13:24:41 -0400 | [diff] [blame] | 121 | return nullptr; |
| 122 | } |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 123 | |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 124 | bool makingASubset = true; |
Stan Iliev | dbba55d | 2017-06-28 13:24:41 -0400 | [diff] [blame] | 125 | if (0 == origin.fX && 0 == origin.fY && |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 126 | info.width() == this->getInfo().width() && info.height() == this->getInfo().height()) { |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 127 | makingASubset = false; |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 128 | if (!willNeedMipMaps || GrMipMapped::kYes == fCachedProxy->mipMapped()) { |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 129 | // If the caller wants the full texture and we have the correct mip support, we're done |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 130 | return fCachedProxy; |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 131 | } |
Stan Iliev | dbba55d | 2017-06-28 13:24:41 -0400 | [diff] [blame] | 132 | } |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 133 | // Otherwise, make a copy for the requested subset or for mip maps. |
| 134 | SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height()); |
| 135 | |
| 136 | GrMipMapped mipMapped = willNeedMipMaps ? GrMipMapped::kYes : GrMipMapped::kNo; |
| 137 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 138 | sk_sp<GrTextureProxy> texProxy = GrSurfaceProxy::Copy(context, fCachedProxy.get(), mipMapped, |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 139 | subset, SkBudgeted::kYes); |
| 140 | if (!makingASubset && texProxy) { |
| 141 | // We are in this case if we wanted the full texture, but we will be mip mapping the |
| 142 | // texture. Therefore we want to update the cached texture so that we point to the |
| 143 | // mipped version instead of the old one. |
| 144 | SkASSERT(willNeedMipMaps); |
| 145 | SkASSERT(GrMipMapped::kYes == texProxy->mipMapped()); |
| 146 | |
| 147 | // The only way we should get into here is if we just made a new texture in makeProxy or |
| 148 | // we found a cached texture in the same context. Thus the current and cached contexts |
| 149 | // should match. |
| 150 | SkASSERT(context->uniqueID() == fOwningContextID); |
| 151 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 152 | // Since we no longer will be caching the and reusing the texture that actually wraps the |
| 153 | // hardware buffer, we can release our refs on it. |
| 154 | this->releaseTextureRef(); |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 155 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 156 | fCachedProxy = texProxy; |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 157 | } |
| 158 | return texProxy; |
Stan Iliev | dbba55d | 2017-06-28 13:24:41 -0400 | [diff] [blame] | 159 | } |
Stan Iliev | dbba55d | 2017-06-28 13:24:41 -0400 | [diff] [blame] | 160 | |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 161 | class BufferCleanupHelper { |
| 162 | public: |
| 163 | BufferCleanupHelper(EGLImageKHR image, EGLDisplay display) |
| 164 | : fImage(image) |
| 165 | , fDisplay(display) { } |
| 166 | ~BufferCleanupHelper() { |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 167 | // eglDestroyImageKHR will remove a ref from the AHardwareBuffer |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 168 | eglDestroyImageKHR(fDisplay, fImage); |
| 169 | } |
| 170 | private: |
| 171 | EGLImageKHR fImage; |
| 172 | EGLDisplay fDisplay; |
| 173 | }; |
| 174 | |
| 175 | |
| 176 | void GrAHardwareBufferImageGenerator::DeleteEGLImage(void* context) { |
| 177 | BufferCleanupHelper* cleanupHelper = static_cast<BufferCleanupHelper*>(context); |
| 178 | delete cleanupHelper; |
| 179 | } |
| 180 | |
| 181 | static GrBackendTexture make_gl_backend_texture( |
| 182 | GrContext* context, AHardwareBuffer* hardwareBuffer, |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 183 | int width, int height, GrPixelConfig config, |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 184 | GrAHardwareBufferImageGenerator::DeleteImageProc* deleteProc, |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame^] | 185 | GrAHardwareBufferImageGenerator::DeleteImageCtx* deleteCtx, |
| 186 | bool isProtectedContent) { |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 187 | while (GL_NO_ERROR != glGetError()) {} //clear GL errors |
| 188 | |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame^] | 189 | EGLClientBuffer clientBuffer = eglGetNativeClientBufferANDROID(hardwareBuffer); |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 190 | EGLint attribs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame^] | 191 | isProtectedContent ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE, |
| 192 | isProtectedContent ? EGL_TRUE : EGL_NONE, |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 193 | EGL_NONE }; |
| 194 | EGLDisplay display = eglGetCurrentDisplay(); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 195 | // eglCreateImageKHR will add a ref to the AHardwareBuffer |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 196 | EGLImageKHR image = eglCreateImageKHR(display, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, |
| 197 | clientBuffer, attribs); |
| 198 | if (EGL_NO_IMAGE_KHR == image) { |
| 199 | SkDebugf("Could not create EGL image, err = (%#x)", (int) eglGetError() ); |
| 200 | return GrBackendTexture(); |
| 201 | } |
| 202 | |
| 203 | GrGLuint texID; |
| 204 | glGenTextures(1, &texID); |
| 205 | if (!texID) { |
| 206 | eglDestroyImageKHR(display, image); |
| 207 | return GrBackendTexture(); |
| 208 | } |
| 209 | glBindTexture(GL_TEXTURE_EXTERNAL_OES, texID); |
| 210 | GLenum status = GL_NO_ERROR; |
| 211 | if ((status = glGetError()) != GL_NO_ERROR) { |
| 212 | SkDebugf("glBindTexture failed (%#x)", (int) status); |
| 213 | glDeleteTextures(1, &texID); |
| 214 | eglDestroyImageKHR(display, image); |
| 215 | return GrBackendTexture(); |
| 216 | } |
| 217 | glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, image); |
| 218 | if ((status = glGetError()) != GL_NO_ERROR) { |
| 219 | SkDebugf("glEGLImageTargetTexture2DOES failed (%#x)", (int) status); |
| 220 | glDeleteTextures(1, &texID); |
| 221 | eglDestroyImageKHR(display, image); |
| 222 | return GrBackendTexture(); |
| 223 | } |
| 224 | context->resetContext(kTextureBinding_GrGLBackendState); |
| 225 | |
| 226 | GrGLTextureInfo textureInfo; |
| 227 | textureInfo.fTarget = GL_TEXTURE_EXTERNAL_OES; |
| 228 | textureInfo.fID = texID; |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 229 | switch (config) { |
| 230 | case kRGBA_8888_GrPixelConfig: |
| 231 | textureInfo.fFormat = GR_GL_RGBA8; |
| 232 | break; |
| 233 | case kRGBA_half_GrPixelConfig: |
| 234 | textureInfo.fFormat = GR_GL_RGBA16F; |
| 235 | break; |
| 236 | case kRGB_565_GrPixelConfig: |
| 237 | textureInfo.fFormat = GR_GL_RGB565; |
| 238 | break; |
| 239 | default: |
| 240 | SkASSERT(false); |
| 241 | } |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 242 | |
| 243 | *deleteProc = GrAHardwareBufferImageGenerator::DeleteEGLImage; |
| 244 | *deleteCtx = new BufferCleanupHelper(image, display); |
| 245 | |
| 246 | return GrBackendTexture(width, height, GrMipMapped::kNo, textureInfo); |
| 247 | } |
| 248 | |
| 249 | static GrBackendTexture make_backend_texture( |
| 250 | GrContext* context, AHardwareBuffer* hardwareBuffer, |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 251 | int width, int height, GrPixelConfig config, |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 252 | GrAHardwareBufferImageGenerator::DeleteImageProc* deleteProc, |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame^] | 253 | GrAHardwareBufferImageGenerator::DeleteImageCtx* deleteCtx, |
| 254 | bool isProtectedContent) { |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 255 | if (context->abandoned() || kOpenGL_GrBackend != context->contextPriv().getBackend()) { |
| 256 | // Check if GrContext is not abandoned and the backend is GL. |
| 257 | return GrBackendTexture(); |
| 258 | } |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame^] | 259 | bool createProtectedImage = isProtectedContent && can_import_protected_content(context); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 260 | return make_gl_backend_texture(context, hardwareBuffer, width, height, config, deleteProc, |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame^] | 261 | deleteCtx, createProtectedImage); |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | static void free_backend_texture(GrBackendTexture* backendTexture) { |
| 265 | SkASSERT(backendTexture && backendTexture->isValid()); |
| 266 | |
| 267 | switch (backendTexture->backend()) { |
| 268 | case kOpenGL_GrBackend: { |
| 269 | GrGLTextureInfo texInfo; |
| 270 | SkAssertResult(backendTexture->getGLTextureInfo(&texInfo)); |
| 271 | glDeleteTextures(1, &texInfo.fID); |
| 272 | return; |
| 273 | } |
| 274 | case kVulkan_GrBackend: // fall through |
| 275 | default: |
| 276 | return; |
| 277 | } |
| 278 | } |
| 279 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 280 | void GrAHardwareBufferImageGenerator::makeProxy(GrContext* context) { |
Khushal | c421ca1 | 2018-06-26 14:38:34 -0700 | [diff] [blame] | 281 | if (context->abandoned() || kOpenGL_GrBackend != context->contextPriv().getBackend()) { |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 282 | // Check if GrContext is not abandoned and the backend is GL. |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 283 | return; |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 284 | } |
| 285 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 286 | if (SK_InvalidGenID != fOwningContextID) { |
| 287 | SkASSERT(fCachedProxy); |
| 288 | if (context->uniqueID() != fOwningContextID) { |
| 289 | this->releaseTextureRef(); |
| 290 | } else { |
| 291 | return; |
| 292 | } |
Stan Iliev | dbba55d | 2017-06-28 13:24:41 -0400 | [diff] [blame] | 293 | } |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 294 | SkASSERT(!fCachedProxy); |
| 295 | |
| 296 | fOwningContextID = context->uniqueID(); |
Stan Iliev | dbba55d | 2017-06-28 13:24:41 -0400 | [diff] [blame] | 297 | |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 298 | GrPixelConfig pixelConfig; |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 299 | switch (this->getInfo().colorType()) { |
| 300 | case kRGBA_8888_SkColorType: |
| 301 | pixelConfig = kRGBA_8888_GrPixelConfig; |
| 302 | break; |
| 303 | case kRGBA_F16_SkColorType: |
| 304 | pixelConfig = kRGBA_half_GrPixelConfig; |
| 305 | break; |
| 306 | case kRGB_565_SkColorType: |
| 307 | pixelConfig = kRGB_565_GrPixelConfig; |
| 308 | break; |
| 309 | default: |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 310 | return; |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 311 | } |
| 312 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 313 | int width = this->getInfo().width(); |
| 314 | int height = this->getInfo().height(); |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 315 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 316 | GrSurfaceDesc desc; |
| 317 | desc.fWidth = width; |
| 318 | desc.fHeight = height; |
| 319 | desc.fConfig = pixelConfig; |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 320 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 321 | GrTextureType textureType = GrTextureType::k2D; |
| 322 | if (context->contextPriv().getBackend() == kOpenGL_GrBackend) { |
| 323 | textureType = GrTextureType::kExternal; |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 324 | } |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 325 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 326 | auto proxyProvider = context->contextPriv().proxyProvider(); |
| 327 | |
| 328 | AHardwareBuffer* hardwareBuffer = fHardwareBuffer; |
| 329 | AHardwareBuffer_acquire(hardwareBuffer); |
| 330 | |
| 331 | GrTexture** ownedTexturePtr = &fOwnedTexture; |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame^] | 332 | const bool isProtectedContent = fIsProtectedContent; |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 333 | |
| 334 | fCachedProxy = proxyProvider->createLazyProxy( |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame^] | 335 | [context, hardwareBuffer, width, height, pixelConfig, ownedTexturePtr, |
| 336 | isProtectedContent] |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 337 | (GrResourceProvider* resourceProvider) { |
| 338 | if (!resourceProvider) { |
| 339 | AHardwareBuffer_release(hardwareBuffer); |
| 340 | return sk_sp<GrTexture>(); |
| 341 | } |
| 342 | |
| 343 | SkASSERT(!*ownedTexturePtr); |
| 344 | |
| 345 | DeleteImageProc deleteImageProc = nullptr; |
| 346 | DeleteImageCtx deleteImageCtx = nullptr; |
| 347 | |
| 348 | GrBackendTexture backendTex = make_backend_texture(context, hardwareBuffer, |
| 349 | width, height, pixelConfig, |
| 350 | &deleteImageProc, |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame^] | 351 | &deleteImageCtx, |
| 352 | isProtectedContent); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 353 | if (!backendTex.isValid()) { |
| 354 | return sk_sp<GrTexture>(); |
| 355 | } |
| 356 | SkASSERT(deleteImageProc && deleteImageCtx); |
| 357 | |
| 358 | backendTex.fConfig = pixelConfig; |
| 359 | sk_sp<GrTexture> tex = resourceProvider->wrapBackendTexture( |
| 360 | backendTex, kAdopt_GrWrapOwnership); |
| 361 | if (!tex) { |
| 362 | free_backend_texture(&backendTex); |
| 363 | deleteImageProc(deleteImageCtx); |
| 364 | return sk_sp<GrTexture>(); |
| 365 | } |
| 366 | |
| 367 | sk_sp<GrReleaseProcHelper> releaseProcHelper( |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 368 | new GrReleaseProcHelper(deleteImageProc, deleteImageCtx)); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 369 | tex->setRelease(releaseProcHelper); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 370 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 371 | *ownedTexturePtr = tex.get(); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 372 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 373 | // Attach our texture to this context's resource cache. This ensures that deletion |
| 374 | // will happen in the correct thread/context. This adds the only ref to the texture |
| 375 | // that will persist from this point. To trigger GrTexture deletion a message is |
| 376 | // sent by generator dtor or by makeProxy when it is invoked with a different |
| 377 | // context. |
| 378 | context->contextPriv().getResourceCache()->insertCrossContextGpuResource(tex.get()); |
| 379 | return tex; |
| 380 | }, |
| 381 | desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, textureType, SkBackingFit::kExact, |
| 382 | SkBudgeted::kNo); |
| 383 | |
| 384 | |
| 385 | if (!fCachedProxy) { |
| 386 | AHardwareBuffer_release(hardwareBuffer); |
| 387 | return; |
| 388 | } |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 389 | } |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 390 | |
| 391 | bool GrAHardwareBufferImageGenerator::onIsValid(GrContext* context) const { |
| 392 | if (nullptr == context) { |
| 393 | return false; //CPU backend is not supported, because hardware buffer can be swizzled |
| 394 | } |
| 395 | // TODO: add Vulkan support |
| 396 | return kOpenGL_GrBackend == context->contextPriv().getBackend(); |
| 397 | } |
| 398 | |
| 399 | #endif //SK_BUILD_FOR_ANDROID_FRAMEWORK |