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" |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 24 | #include "GrRecordingContext.h" |
| 25 | #include "GrRecordingContextPriv.h" |
Stan Iliev | dbba55d | 2017-06-28 13:24:41 -0400 | [diff] [blame] | 26 | #include "GrResourceCache.h" |
Robert Phillips | 0001828 | 2017-06-15 15:35:16 -0400 | [diff] [blame] | 27 | #include "GrResourceProvider.h" |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 28 | #include "GrResourceProviderPriv.h" |
Robert Phillips | 847d4c5 | 2017-06-13 18:21:44 -0400 | [diff] [blame] | 29 | #include "GrTexture.h" |
Robert Phillips | ade9f61 | 2017-06-16 07:32:43 -0400 | [diff] [blame] | 30 | #include "GrTextureProxy.h" |
Brian Salomon | 67f0195 | 2019-02-14 13:05:25 -0500 | [diff] [blame] | 31 | #include "SkExchange.h" |
Stan Iliev | dbba55d | 2017-06-28 13:24:41 -0400 | [diff] [blame] | 32 | #include "SkMessageBus.h" |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 33 | #include "gl/GrGLDefines.h" |
| 34 | #include "gl/GrGLTypes.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 |
| 42 | #include "vk/GrVkExtensions.h" |
| 43 | #include "vk/GrVkGpu.h" |
| 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 | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 98 | GrPixelConfig pixelConfig = context->priv().caps()->getConfigFromBackendFormat( |
Brian Salomon | f391d0f | 2018-12-14 09:18:50 -0500 | [diff] [blame] | 99 | backendFormat, this->getInfo().colorType()); |
| 100 | |
| 101 | if (pixelConfig == kUnknown_GrPixelConfig) { |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 102 | return nullptr; |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 103 | } |
| 104 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 105 | int width = this->getInfo().width(); |
| 106 | int height = this->getInfo().height(); |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 107 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 108 | GrSurfaceDesc desc; |
| 109 | desc.fWidth = width; |
| 110 | desc.fHeight = height; |
| 111 | desc.fConfig = pixelConfig; |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 112 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 113 | GrTextureType textureType = GrTextureType::k2D; |
Robert Phillips | 4217ea7 | 2019-01-30 13:08:28 -0500 | [diff] [blame] | 114 | if (context->backend() == GrBackendApi::kOpenGL) { |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 115 | textureType = GrTextureType::kExternal; |
Robert Phillips | 4217ea7 | 2019-01-30 13:08:28 -0500 | [diff] [blame] | 116 | } else if (context->backend() == GrBackendApi::kVulkan) { |
Greg Daniel | 14c55c2 | 2018-12-04 11:25:03 -0500 | [diff] [blame] | 117 | const VkFormat* format = backendFormat.getVkFormat(); |
| 118 | SkASSERT(format); |
| 119 | if (*format == VK_FORMAT_UNDEFINED) { |
| 120 | textureType = GrTextureType::kExternal; |
| 121 | } |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 122 | } |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 123 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 124 | auto proxyProvider = context->priv().proxyProvider(); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 125 | |
| 126 | AHardwareBuffer* hardwareBuffer = fHardwareBuffer; |
| 127 | AHardwareBuffer_acquire(hardwareBuffer); |
| 128 | |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 129 | const bool isProtectedContent = fIsProtectedContent; |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 130 | |
Brian Salomon | 67f0195 | 2019-02-14 13:05:25 -0500 | [diff] [blame] | 131 | class AutoAHBRelease { |
| 132 | public: |
| 133 | AutoAHBRelease(AHardwareBuffer* ahb) : fAhb(ahb) {} |
| 134 | // std::function() must be CopyConstructible, but ours should never actually be copied. |
| 135 | AutoAHBRelease(const AutoAHBRelease&) { SkASSERT(0); } |
| 136 | AutoAHBRelease(AutoAHBRelease&& that) : fAhb(that.fAhb) { that.fAhb = nullptr; } |
| 137 | ~AutoAHBRelease() { fAhb ? AHardwareBuffer_release(fAhb) : void(); } |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 138 | |
Brian Salomon | 67f0195 | 2019-02-14 13:05:25 -0500 | [diff] [blame] | 139 | AutoAHBRelease& operator=(AutoAHBRelease&& that) { |
| 140 | fAhb = skstd::exchange(that.fAhb, nullptr); |
| 141 | return *this; |
| 142 | } |
| 143 | AutoAHBRelease& operator=(const AutoAHBRelease&) = delete; |
| 144 | |
| 145 | AHardwareBuffer* get() const { return fAhb; } |
| 146 | |
| 147 | private: |
| 148 | AHardwareBuffer* fAhb; |
| 149 | }; |
| 150 | |
| 151 | sk_sp<GrTextureProxy> texProxy = proxyProvider->createLazyProxy( |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 152 | [direct, buffer = AutoAHBRelease(hardwareBuffer), width, height, pixelConfig, |
Brian Salomon | 67f0195 | 2019-02-14 13:05:25 -0500 | [diff] [blame] | 153 | isProtectedContent, backendFormat](GrResourceProvider* resourceProvider) { |
Greg Daniel | 173464d | 2019-02-06 15:30:34 -0500 | [diff] [blame] | 154 | GrAHardwareBufferUtils::DeleteImageProc deleteImageProc = nullptr; |
| 155 | GrAHardwareBufferUtils::DeleteImageCtx deleteImageCtx = nullptr; |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 156 | |
Greg Daniel | 173464d | 2019-02-06 15:30:34 -0500 | [diff] [blame] | 157 | GrBackendTexture backendTex = |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 158 | GrAHardwareBufferUtils::MakeBackendTexture(direct, buffer.get(), |
Greg Daniel | 173464d | 2019-02-06 15:30:34 -0500 | [diff] [blame] | 159 | width, height, |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 160 | &deleteImageProc, |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 161 | &deleteImageCtx, |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 162 | isProtectedContent, |
Greg Daniel | 173464d | 2019-02-06 15:30:34 -0500 | [diff] [blame] | 163 | backendFormat, |
| 164 | false); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 165 | if (!backendTex.isValid()) { |
| 166 | return sk_sp<GrTexture>(); |
| 167 | } |
| 168 | SkASSERT(deleteImageProc && deleteImageCtx); |
| 169 | |
| 170 | backendTex.fConfig = pixelConfig; |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 171 | // We make this texture cacheable to avoid recreating a GrTexture every time this |
| 172 | // is invoked. We know the owning SkIamge will send an invalidation message when the |
| 173 | // image is destroyed, so the texture will be removed at that time. |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 174 | sk_sp<GrTexture> tex = resourceProvider->wrapBackendTexture( |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 175 | backendTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kYes, kRead_GrIOType); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 176 | if (!tex) { |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 177 | deleteImageProc(deleteImageCtx); |
| 178 | return sk_sp<GrTexture>(); |
| 179 | } |
| 180 | |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 181 | if (deleteImageProc) { |
Brian Salomon | b2c5dae | 2019-03-04 10:25:17 -0500 | [diff] [blame] | 182 | tex->setRelease(deleteImageProc, deleteImageCtx); |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 183 | } |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 184 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 185 | return tex; |
| 186 | }, |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 187 | backendFormat, desc, fSurfaceOrigin, GrMipMapped::kNo, |
| 188 | GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, SkBudgeted::kNo); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 189 | |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 190 | return texProxy; |
| 191 | } |
| 192 | |
| 193 | sk_sp<GrTextureProxy> GrAHardwareBufferImageGenerator::onGenerateTexture( |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 194 | GrRecordingContext* context, const SkImageInfo& info, |
| 195 | const SkIPoint& origin, bool willNeedMipMaps) { |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 196 | sk_sp<GrTextureProxy> texProxy = this->makeProxy(context); |
| 197 | if (!texProxy) { |
| 198 | return nullptr; |
| 199 | } |
| 200 | |
| 201 | if (0 == origin.fX && 0 == origin.fY && |
| 202 | info.width() == this->getInfo().width() && info.height() == this->getInfo().height()) { |
| 203 | // If the caller wants the full texture we're done. The caller will handle making a copy for |
| 204 | // mip maps if that is required. |
| 205 | return texProxy; |
| 206 | } |
| 207 | // Otherwise, make a copy for the requested subset. |
| 208 | SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height()); |
| 209 | |
| 210 | GrMipMapped mipMapped = willNeedMipMaps ? GrMipMapped::kYes : GrMipMapped::kNo; |
| 211 | |
Brian Salomon | fee3f9b | 2018-12-19 12:34:12 -0500 | [diff] [blame] | 212 | return GrSurfaceProxy::Copy(context, texProxy.get(), mipMapped, subset, SkBackingFit::kExact, |
| 213 | SkBudgeted::kYes); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 214 | } |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 215 | |
| 216 | bool GrAHardwareBufferImageGenerator::onIsValid(GrContext* context) const { |
| 217 | if (nullptr == context) { |
| 218 | return false; //CPU backend is not supported, because hardware buffer can be swizzled |
| 219 | } |
Robert Phillips | 4217ea7 | 2019-01-30 13:08:28 -0500 | [diff] [blame] | 220 | return GrBackendApi::kOpenGL == context->backend() || |
| 221 | GrBackendApi::kVulkan == context->backend(); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | #endif //SK_BUILD_FOR_ANDROID_FRAMEWORK |