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 | */ |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 7 | |
Greg Daniel | 54bfb18 | 2018-11-20 17:12:36 -0500 | [diff] [blame] | 8 | #include "SkTypes.h" |
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 |
Greg Daniel | 54bfb18 | 2018-11-20 17:12:36 -0500 | [diff] [blame] | 13 | |
Greg Daniel | 54bfb18 | 2018-11-20 17:12:36 -0500 | [diff] [blame] | 14 | |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 15 | #include "GrAHardwareBufferImageGenerator.h" |
| 16 | |
Derek Sollenberger | 7a86987 | 2017-06-27 15:37:25 -0400 | [diff] [blame] | 17 | #include <android/hardware_buffer.h> |
| 18 | |
Greg Daniel | 173464d | 2019-02-06 15:30:34 -0500 | [diff] [blame] | 19 | #include "GrAHardwareBufferUtils.h" |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 20 | #include "GrBackendSurface.h" |
| 21 | #include "GrContext.h" |
| 22 | #include "GrContextPriv.h" |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 23 | #include "GrProxyProvider.h" |
Stan Iliev | dbba55d | 2017-06-28 13:24:41 -0400 | [diff] [blame] | 24 | #include "GrResourceCache.h" |
Robert Phillips | 0001828 | 2017-06-15 15:35:16 -0400 | [diff] [blame] | 25 | #include "GrResourceProvider.h" |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 26 | #include "GrResourceProviderPriv.h" |
Robert Phillips | 847d4c5 | 2017-06-13 18:21:44 -0400 | [diff] [blame] | 27 | #include "GrTexture.h" |
Robert Phillips | ade9f61 | 2017-06-16 07:32:43 -0400 | [diff] [blame] | 28 | #include "GrTextureProxy.h" |
Stan Iliev | dbba55d | 2017-06-28 13:24:41 -0400 | [diff] [blame] | 29 | #include "SkMessageBus.h" |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 30 | #include "gl/GrGLDefines.h" |
| 31 | #include "gl/GrGLTypes.h" |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 32 | |
| 33 | #include <EGL/egl.h> |
| 34 | #include <EGL/eglext.h> |
| 35 | #include <GLES/gl.h> |
| 36 | #include <GLES/glext.h> |
| 37 | |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 38 | #ifdef SK_VULKAN |
| 39 | #include "vk/GrVkExtensions.h" |
| 40 | #include "vk/GrVkGpu.h" |
| 41 | #endif |
| 42 | |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 43 | #define PROT_CONTENT_EXT_STR "EGL_EXT_protected_content" |
| 44 | #define EGL_PROTECTED_CONTENT_EXT 0x32C0 |
| 45 | |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 46 | std::unique_ptr<SkImageGenerator> GrAHardwareBufferImageGenerator::Make( |
Stan Iliev | 505dd57 | 2018-09-13 14:20:03 -0400 | [diff] [blame] | 47 | AHardwareBuffer* graphicBuffer, SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace, |
| 48 | GrSurfaceOrigin surfaceOrigin) { |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 49 | AHardwareBuffer_Desc bufferDesc; |
| 50 | AHardwareBuffer_describe(graphicBuffer, &bufferDesc); |
Greg Daniel | 173464d | 2019-02-06 15:30:34 -0500 | [diff] [blame] | 51 | |
| 52 | SkColorType colorType = |
| 53 | GrAHardwareBufferUtils::GetSkColorTypeFromBufferFormat(bufferDesc.format); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 54 | SkImageInfo info = SkImageInfo::Make(bufferDesc.width, bufferDesc.height, colorType, |
| 55 | alphaType, std::move(colorSpace)); |
Greg Daniel | 173464d | 2019-02-06 15:30:34 -0500 | [diff] [blame] | 56 | |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 57 | bool createProtectedImage = 0 != (bufferDesc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT); |
Stan Iliev | 505dd57 | 2018-09-13 14:20:03 -0400 | [diff] [blame] | 58 | return std::unique_ptr<SkImageGenerator>(new GrAHardwareBufferImageGenerator( |
| 59 | info, graphicBuffer, alphaType, createProtectedImage, |
| 60 | bufferDesc.format, surfaceOrigin)); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | GrAHardwareBufferImageGenerator::GrAHardwareBufferImageGenerator(const SkImageInfo& info, |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 64 | AHardwareBuffer* hardwareBuffer, SkAlphaType alphaType, bool isProtectedContent, |
Stan Iliev | 505dd57 | 2018-09-13 14:20:03 -0400 | [diff] [blame] | 65 | uint32_t bufferFormat, GrSurfaceOrigin surfaceOrigin) |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 66 | : INHERITED(info) |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 67 | , fHardwareBuffer(hardwareBuffer) |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 68 | , fBufferFormat(bufferFormat) |
Stan Iliev | 505dd57 | 2018-09-13 14:20:03 -0400 | [diff] [blame] | 69 | , fIsProtectedContent(isProtectedContent) |
| 70 | , fSurfaceOrigin(surfaceOrigin) { |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 71 | AHardwareBuffer_acquire(fHardwareBuffer); |
| 72 | } |
| 73 | |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 74 | GrAHardwareBufferImageGenerator::~GrAHardwareBufferImageGenerator() { |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 75 | AHardwareBuffer_release(fHardwareBuffer); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 76 | } |
| 77 | |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 78 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 79 | |
Greg Daniel | 3860cfd1 | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 80 | sk_sp<GrTextureProxy> GrAHardwareBufferImageGenerator::makeProxy(GrContext* context) { |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 81 | if (context->abandoned()) { |
Greg Daniel | 3860cfd1 | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 82 | return nullptr; |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 83 | } |
| 84 | |
Greg Daniel | 173464d | 2019-02-06 15:30:34 -0500 | [diff] [blame] | 85 | GrBackendFormat backendFormat = GrAHardwareBufferUtils::GetBackendFormat(context, |
| 86 | fHardwareBuffer, |
| 87 | fBufferFormat, |
| 88 | false); |
| 89 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 90 | GrPixelConfig pixelConfig = context->priv().caps()->getConfigFromBackendFormat( |
Brian Salomon | f391d0f | 2018-12-14 09:18:50 -0500 | [diff] [blame] | 91 | backendFormat, this->getInfo().colorType()); |
| 92 | |
| 93 | if (pixelConfig == kUnknown_GrPixelConfig) { |
Greg Daniel | 3860cfd1 | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 94 | return nullptr; |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 95 | } |
| 96 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 97 | int width = this->getInfo().width(); |
| 98 | int height = this->getInfo().height(); |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 99 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 100 | GrSurfaceDesc desc; |
| 101 | desc.fWidth = width; |
| 102 | desc.fHeight = height; |
| 103 | desc.fConfig = pixelConfig; |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 104 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 105 | GrTextureType textureType = GrTextureType::k2D; |
Robert Phillips | 4217ea7 | 2019-01-30 13:08:28 -0500 | [diff] [blame] | 106 | if (context->backend() == GrBackendApi::kOpenGL) { |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 107 | textureType = GrTextureType::kExternal; |
Robert Phillips | 4217ea7 | 2019-01-30 13:08:28 -0500 | [diff] [blame] | 108 | } else if (context->backend() == GrBackendApi::kVulkan) { |
Greg Daniel | 14c55c2 | 2018-12-04 11:25:03 -0500 | [diff] [blame] | 109 | const VkFormat* format = backendFormat.getVkFormat(); |
| 110 | SkASSERT(format); |
| 111 | if (*format == VK_FORMAT_UNDEFINED) { |
| 112 | textureType = GrTextureType::kExternal; |
| 113 | } |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 114 | } |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 115 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 116 | auto proxyProvider = context->priv().proxyProvider(); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 117 | |
| 118 | AHardwareBuffer* hardwareBuffer = fHardwareBuffer; |
| 119 | AHardwareBuffer_acquire(hardwareBuffer); |
| 120 | |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 121 | const bool isProtectedContent = fIsProtectedContent; |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 122 | |
Greg Daniel | 3860cfd1 | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 123 | sk_sp<GrTextureProxy> texProxy = proxyProvider->createLazyProxy( |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 124 | [context, hardwareBuffer, width, height, pixelConfig, isProtectedContent, |
| 125 | backendFormat](GrResourceProvider* resourceProvider) { |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 126 | if (!resourceProvider) { |
| 127 | AHardwareBuffer_release(hardwareBuffer); |
| 128 | return sk_sp<GrTexture>(); |
| 129 | } |
| 130 | |
Greg Daniel | 173464d | 2019-02-06 15:30:34 -0500 | [diff] [blame] | 131 | GrAHardwareBufferUtils::DeleteImageProc deleteImageProc = nullptr; |
| 132 | GrAHardwareBufferUtils::DeleteImageCtx deleteImageCtx = nullptr; |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 133 | |
Greg Daniel | 173464d | 2019-02-06 15:30:34 -0500 | [diff] [blame] | 134 | GrBackendTexture backendTex = |
| 135 | GrAHardwareBufferUtils::MakeBackendTexture(context, hardwareBuffer, |
| 136 | width, height, |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 137 | &deleteImageProc, |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 138 | &deleteImageCtx, |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 139 | isProtectedContent, |
Greg Daniel | 173464d | 2019-02-06 15:30:34 -0500 | [diff] [blame] | 140 | backendFormat, |
| 141 | false); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 142 | if (!backendTex.isValid()) { |
| 143 | return sk_sp<GrTexture>(); |
| 144 | } |
| 145 | SkASSERT(deleteImageProc && deleteImageCtx); |
| 146 | |
| 147 | backendTex.fConfig = pixelConfig; |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 148 | // We make this texture cacheable to avoid recreating a GrTexture every time this |
| 149 | // is invoked. We know the owning SkIamge will send an invalidation message when the |
| 150 | // image is destroyed, so the texture will be removed at that time. |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 151 | sk_sp<GrTexture> tex = resourceProvider->wrapBackendTexture( |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 152 | backendTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kYes, kRead_GrIOType); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 153 | if (!tex) { |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 154 | deleteImageProc(deleteImageCtx); |
| 155 | return sk_sp<GrTexture>(); |
| 156 | } |
| 157 | |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 158 | if (deleteImageProc) { |
| 159 | sk_sp<GrReleaseProcHelper> releaseProcHelper( |
| 160 | new GrReleaseProcHelper(deleteImageProc, deleteImageCtx)); |
| 161 | tex->setRelease(releaseProcHelper); |
| 162 | } |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 163 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 164 | return tex; |
| 165 | }, |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 166 | backendFormat, desc, fSurfaceOrigin, GrMipMapped::kNo, |
| 167 | GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, SkBudgeted::kNo); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 168 | |
Greg Daniel | 3860cfd1 | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 169 | if (!texProxy) { |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 170 | AHardwareBuffer_release(hardwareBuffer); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 171 | } |
Greg Daniel | 3860cfd1 | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 172 | return texProxy; |
| 173 | } |
| 174 | |
| 175 | sk_sp<GrTextureProxy> GrAHardwareBufferImageGenerator::onGenerateTexture( |
| 176 | GrContext* context, const SkImageInfo& info, const SkIPoint& origin, bool willNeedMipMaps) { |
| 177 | sk_sp<GrTextureProxy> texProxy = this->makeProxy(context); |
| 178 | if (!texProxy) { |
| 179 | return nullptr; |
| 180 | } |
| 181 | |
| 182 | if (0 == origin.fX && 0 == origin.fY && |
| 183 | info.width() == this->getInfo().width() && info.height() == this->getInfo().height()) { |
| 184 | // If the caller wants the full texture we're done. The caller will handle making a copy for |
| 185 | // mip maps if that is required. |
| 186 | return texProxy; |
| 187 | } |
| 188 | // Otherwise, make a copy for the requested subset. |
| 189 | SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height()); |
| 190 | |
| 191 | GrMipMapped mipMapped = willNeedMipMaps ? GrMipMapped::kYes : GrMipMapped::kNo; |
| 192 | |
Brian Salomon | fee3f9b | 2018-12-19 12:34:12 -0500 | [diff] [blame] | 193 | return GrSurfaceProxy::Copy(context, texProxy.get(), mipMapped, subset, SkBackingFit::kExact, |
| 194 | SkBudgeted::kYes); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 195 | } |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 196 | |
| 197 | bool GrAHardwareBufferImageGenerator::onIsValid(GrContext* context) const { |
| 198 | if (nullptr == context) { |
| 199 | return false; //CPU backend is not supported, because hardware buffer can be swizzled |
| 200 | } |
Robert Phillips | 4217ea7 | 2019-01-30 13:08:28 -0500 | [diff] [blame] | 201 | return GrBackendApi::kOpenGL == context->backend() || |
| 202 | GrBackendApi::kVulkan == context->backend(); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | #endif //SK_BUILD_FOR_ANDROID_FRAMEWORK |