| 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 | |
| 8 | #include "include/core/SkTypes.h" |
| 9 | |
| 10 | #if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26 |
| 11 | #define GL_GLEXT_PROTOTYPES |
| 12 | #define EGL_EGLEXT_PROTOTYPES |
| 13 | |
| 14 | |
| 15 | #include "src/gpu/GrAHardwareBufferImageGenerator.h" |
| 16 | |
| 17 | #include <android/hardware_buffer.h> |
| 18 | |
| 19 | #include "include/gpu/GrBackendSurface.h" |
| 20 | #include "include/gpu/GrContext.h" |
| 21 | #include "include/gpu/gl/GrGLTypes.h" |
| 22 | #include "include/private/GrRecordingContext.h" |
| 23 | #include "src/core/SkExchange.h" |
| 24 | #include "src/core/SkMessageBus.h" |
| 25 | #include "src/gpu/GrAHardwareBufferUtils.h" |
| 26 | #include "src/gpu/GrContextPriv.h" |
| 27 | #include "src/gpu/GrProxyProvider.h" |
| 28 | #include "src/gpu/GrRecordingContextPriv.h" |
| 29 | #include "src/gpu/GrResourceCache.h" |
| 30 | #include "src/gpu/GrResourceProvider.h" |
| 31 | #include "src/gpu/GrResourceProviderPriv.h" |
| 32 | #include "src/gpu/GrTexture.h" |
| 33 | #include "src/gpu/GrTextureProxy.h" |
| 34 | #include "src/gpu/SkGr.h" |
| 35 | #include "src/gpu/gl/GrGLDefines.h" |
| 36 | |
| 37 | #include <EGL/egl.h> |
| 38 | #include <EGL/eglext.h> |
| 39 | #include <GLES/gl.h> |
| 40 | #include <GLES/glext.h> |
| 41 | |
| 42 | #ifdef SK_VULKAN |
| 43 | #include "include/gpu/vk/GrVkExtensions.h" |
| 44 | #include "src/gpu/vk/GrVkGpu.h" |
| 45 | #endif |
| 46 | |
| 47 | #define PROT_CONTENT_EXT_STR "EGL_EXT_protected_content" |
| 48 | #define EGL_PROTECTED_CONTENT_EXT 0x32C0 |
| 49 | |
| 50 | std::unique_ptr<SkImageGenerator> GrAHardwareBufferImageGenerator::Make( |
| 51 | AHardwareBuffer* graphicBuffer, SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace, |
| 52 | GrSurfaceOrigin surfaceOrigin) { |
| 53 | AHardwareBuffer_Desc bufferDesc; |
| 54 | AHardwareBuffer_describe(graphicBuffer, &bufferDesc); |
| 55 | |
| 56 | SkColorType colorType = |
| 57 | GrAHardwareBufferUtils::GetSkColorTypeFromBufferFormat(bufferDesc.format); |
| 58 | SkImageInfo info = SkImageInfo::Make(bufferDesc.width, bufferDesc.height, colorType, |
| 59 | alphaType, std::move(colorSpace)); |
| 60 | |
| 61 | bool createProtectedImage = 0 != (bufferDesc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT); |
| 62 | return std::unique_ptr<SkImageGenerator>(new GrAHardwareBufferImageGenerator( |
| 63 | info, graphicBuffer, alphaType, createProtectedImage, |
| 64 | bufferDesc.format, surfaceOrigin)); |
| 65 | } |
| 66 | |
| 67 | GrAHardwareBufferImageGenerator::GrAHardwareBufferImageGenerator(const SkImageInfo& info, |
| 68 | AHardwareBuffer* hardwareBuffer, SkAlphaType alphaType, bool isProtectedContent, |
| 69 | uint32_t bufferFormat, GrSurfaceOrigin surfaceOrigin) |
| 70 | : INHERITED(info) |
| 71 | , fHardwareBuffer(hardwareBuffer) |
| 72 | , fBufferFormat(bufferFormat) |
| 73 | , fIsProtectedContent(isProtectedContent) |
| 74 | , fSurfaceOrigin(surfaceOrigin) { |
| 75 | AHardwareBuffer_acquire(fHardwareBuffer); |
| 76 | } |
| 77 | |
| 78 | GrAHardwareBufferImageGenerator::~GrAHardwareBufferImageGenerator() { |
| 79 | AHardwareBuffer_release(fHardwareBuffer); |
| 80 | } |
| 81 | |
| 82 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 83 | |
| 84 | GrSurfaceProxyView GrAHardwareBufferImageGenerator::makeView(GrRecordingContext* context) { |
| 85 | if (context->priv().abandoned()) { |
| 86 | return {}; |
| 87 | } |
| 88 | |
| 89 | auto direct = context->priv().asDirectContext(); |
| 90 | if (!direct) { |
| 91 | return {}; |
| 92 | } |
| 93 | |
| 94 | GrBackendFormat backendFormat = GrAHardwareBufferUtils::GetBackendFormat(direct, |
| 95 | fHardwareBuffer, |
| 96 | fBufferFormat, |
| 97 | false); |
| 98 | |
| 99 | GrColorType grColorType = SkColorTypeToGrColorType(this->getInfo().colorType()); |
| 100 | |
| 101 | int width = this->getInfo().width(); |
| 102 | int height = this->getInfo().height(); |
| 103 | |
| 104 | GrTextureType textureType = GrTextureType::k2D; |
| 105 | if (context->backend() == GrBackendApi::kOpenGL) { |
| 106 | textureType = GrTextureType::kExternal; |
| 107 | } else if (context->backend() == GrBackendApi::kVulkan) { |
| 108 | VkFormat format; |
| 109 | SkAssertResult(backendFormat.asVkFormat(&format)); |
| 110 | if (format == VK_FORMAT_UNDEFINED) { |
| 111 | textureType = GrTextureType::kExternal; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | auto proxyProvider = context->priv().proxyProvider(); |
| 116 | |
| 117 | AHardwareBuffer* hardwareBuffer = fHardwareBuffer; |
| 118 | AHardwareBuffer_acquire(hardwareBuffer); |
| 119 | |
| 120 | const bool isProtectedContent = fIsProtectedContent; |
| 121 | |
| 122 | class AutoAHBRelease { |
| 123 | public: |
| 124 | AutoAHBRelease(AHardwareBuffer* ahb) : fAhb(ahb) {} |
| 125 | // std::function() must be CopyConstructible, but ours should never actually be copied. |
| 126 | AutoAHBRelease(const AutoAHBRelease&) { SkASSERT(0); } |
| 127 | AutoAHBRelease(AutoAHBRelease&& that) : fAhb(that.fAhb) { that.fAhb = nullptr; } |
| 128 | ~AutoAHBRelease() { fAhb ? AHardwareBuffer_release(fAhb) : void(); } |
| 129 | |
| 130 | AutoAHBRelease& operator=(AutoAHBRelease&& that) { |
| 131 | fAhb = skstd::exchange(that.fAhb, nullptr); |
| 132 | return *this; |
| 133 | } |
| 134 | AutoAHBRelease& operator=(const AutoAHBRelease&) = delete; |
| 135 | |
| 136 | AHardwareBuffer* get() const { return fAhb; } |
| 137 | |
| 138 | private: |
| 139 | AHardwareBuffer* fAhb; |
| 140 | }; |
| 141 | |
| 142 | sk_sp<GrTextureProxy> texProxy = proxyProvider->createLazyProxy( |
| 143 | [direct, buffer = AutoAHBRelease(hardwareBuffer), width, height, isProtectedContent, |
| 144 | backendFormat]( |
| 145 | GrResourceProvider* resourceProvider) -> GrSurfaceProxy::LazyCallbackResult { |
| 146 | GrAHardwareBufferUtils::DeleteImageProc deleteImageProc = nullptr; |
| 147 | GrAHardwareBufferUtils::UpdateImageProc updateImageProc = nullptr; |
| 148 | GrAHardwareBufferUtils::TexImageCtx texImageCtx = nullptr; |
| 149 | |
| 150 | GrBackendTexture backendTex = |
| 151 | GrAHardwareBufferUtils::MakeBackendTexture(direct, buffer.get(), |
| 152 | width, height, |
| 153 | &deleteImageProc, |
| 154 | &updateImageProc, |
| 155 | &texImageCtx, |
| 156 | isProtectedContent, |
| 157 | backendFormat, |
| 158 | false); |
| 159 | if (!backendTex.isValid()) { |
| 160 | return {}; |
| 161 | } |
| 162 | SkASSERT(deleteImageProc && texImageCtx); |
| 163 | |
| 164 | // We make this texture cacheable to avoid recreating a GrTexture every time this |
| 165 | // is invoked. We know the owning SkIamge will send an invalidation message when the |
| 166 | // image is destroyed, so the texture will be removed at that time. |
| 167 | sk_sp<GrTexture> tex = resourceProvider->wrapBackendTexture( |
| 168 | backendTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kYes, kRead_GrIOType); |
| 169 | if (!tex) { |
| 170 | deleteImageProc(texImageCtx); |
| 171 | return {}; |
| 172 | } |
| 173 | |
| 174 | if (deleteImageProc) { |
| 175 | tex->setRelease(deleteImageProc, texImageCtx); |
| 176 | } |
| 177 | |
| 178 | return tex; |
| 179 | }, |
| 180 | backendFormat, {width, height}, GrRenderable::kNo, 1, GrMipMapped::kNo, |
| 181 | GrMipMapsStatus::kNotAllocated, GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, |
| 182 | SkBudgeted::kNo, GrProtected::kNo, GrSurfaceProxy::UseAllocator::kYes); |
| 183 | |
| 184 | GrSwizzle readSwizzle = context->priv().caps()->getReadSwizzle(backendFormat, grColorType); |
| 185 | |
| 186 | return GrSurfaceProxyView(std::move(texProxy), fSurfaceOrigin, readSwizzle); |
| 187 | } |
| 188 | |
| 189 | GrSurfaceProxyView GrAHardwareBufferImageGenerator::onGenerateTexture( |
| 190 | GrRecordingContext* context, |
| 191 | const SkImageInfo& info, |
| 192 | const SkIPoint& origin, |
| 193 | GrMipMapped mipMapped, |
| 194 | GrImageTexGenPolicy texGenPolicy) { |
| 195 | GrSurfaceProxyView texProxyView = this->makeView(context); |
| 196 | if (!texProxyView.proxy()) { |
| 197 | return {}; |
| 198 | } |
| 199 | SkASSERT(texProxyView.asTextureProxy()); |
| 200 | |
| 201 | if (texGenPolicy == GrImageTexGenPolicy::kDraw && origin.isZero() && |
| 202 | info.dimensions() == this->getInfo().dimensions() && mipMapped == GrMipMapped::kNo) { |
| 203 | // If the caller wants the full non-MIP mapped texture we're done. |
| 204 | return texProxyView; |
| 205 | } |
| 206 | // Otherwise, make a copy for the requested subset and/or MIP maps. |
| 207 | SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height()); |
| 208 | |
| 209 | SkBudgeted budgeted = texGenPolicy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted |
| 210 | ? SkBudgeted::kNo |
| 211 | : SkBudgeted::kYes; |
| 212 | |
| 213 | return GrSurfaceProxyView::Copy(context, std::move(texProxyView), mipMapped, subset, |
| 214 | SkBackingFit::kExact, budgeted); |
| 215 | } |
| 216 | |
| 217 | bool GrAHardwareBufferImageGenerator::onIsValid(GrContext* context) const { |
| 218 | if (nullptr == context) { |
| 219 | return false; //CPU backend is not supported, because hardware buffer can be swizzled |
| 220 | } |
| 221 | return GrBackendApi::kOpenGL == context->backend() || |
| 222 | GrBackendApi::kVulkan == context->backend(); |
| 223 | } |
| 224 | |
| 225 | #endif //SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 226 | |