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