blob: 22b28402f150a6be47b3faa33f986b649031e2e0 [file] [log] [blame]
Stan Iliev7e910df2017-06-02 10:29:21 -04001/*
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 Iliev7e910df2017-06-02 10:29:21 -04007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTypes.h"
Derek Sollenberger7a869872017-06-27 15:37:25 -04009
10#if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26
Stan Iliev7e910df2017-06-02 10:29:21 -040011#define GL_GLEXT_PROTOTYPES
12#define EGL_EGLEXT_PROTOTYPES
Greg Daniel54bfb182018-11-20 17:12:36 -050013
Greg Daniel54bfb182018-11-20 17:12:36 -050014
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrAHardwareBufferImageGenerator.h"
Stan Iliev7e910df2017-06-02 10:29:21 -040016
Derek Sollenberger7a869872017-06-27 15:37:25 -040017#include <android/hardware_buffer.h>
18
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/gpu/GrBackendSurface.h"
20#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/gpu/gl/GrGLTypes.h"
22#include "include/private/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/core/SkExchange.h"
Ben Wagner21bca282019-05-15 10:15:52 -040024#include "src/core/SkMessageBus.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#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"
Greg Daniel456f9b52020-03-05 19:14:18 +000032#include "src/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040033#include "src/gpu/GrTextureProxy.h"
Brian Salomonbc074a62020-03-18 10:06:13 -040034#include "src/gpu/SkGr.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/gl/GrGLDefines.h"
Stan Iliev7e910df2017-06-02 10:29:21 -040036
37#include <EGL/egl.h>
38#include <EGL/eglext.h>
39#include <GLES/gl.h>
40#include <GLES/glext.h>
41
Greg Daniel637c06a2018-09-12 09:44:25 -040042#ifdef SK_VULKAN
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "include/gpu/vk/GrVkExtensions.h"
44#include "src/gpu/vk/GrVkGpu.h"
Greg Daniel637c06a2018-09-12 09:44:25 -040045#endif
46
Stan Ilievc01b5c72018-08-28 10:18:19 -040047#define PROT_CONTENT_EXT_STR "EGL_EXT_protected_content"
48#define EGL_PROTECTED_CONTENT_EXT 0x32C0
49
Stan Iliev7e910df2017-06-02 10:29:21 -040050std::unique_ptr<SkImageGenerator> GrAHardwareBufferImageGenerator::Make(
Stan Iliev505dd572018-09-13 14:20:03 -040051 AHardwareBuffer* graphicBuffer, SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace,
52 GrSurfaceOrigin surfaceOrigin) {
Stan Iliev7e910df2017-06-02 10:29:21 -040053 AHardwareBuffer_Desc bufferDesc;
54 AHardwareBuffer_describe(graphicBuffer, &bufferDesc);
Greg Daniel173464d2019-02-06 15:30:34 -050055
56 SkColorType colorType =
57 GrAHardwareBufferUtils::GetSkColorTypeFromBufferFormat(bufferDesc.format);
Stan Iliev7e910df2017-06-02 10:29:21 -040058 SkImageInfo info = SkImageInfo::Make(bufferDesc.width, bufferDesc.height, colorType,
59 alphaType, std::move(colorSpace));
Greg Daniel173464d2019-02-06 15:30:34 -050060
Stan Ilievc01b5c72018-08-28 10:18:19 -040061 bool createProtectedImage = 0 != (bufferDesc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT);
Stan Iliev505dd572018-09-13 14:20:03 -040062 return std::unique_ptr<SkImageGenerator>(new GrAHardwareBufferImageGenerator(
63 info, graphicBuffer, alphaType, createProtectedImage,
64 bufferDesc.format, surfaceOrigin));
Stan Iliev7e910df2017-06-02 10:29:21 -040065}
66
67GrAHardwareBufferImageGenerator::GrAHardwareBufferImageGenerator(const SkImageInfo& info,
Stan Iliev114b0912018-08-31 14:02:55 -040068 AHardwareBuffer* hardwareBuffer, SkAlphaType alphaType, bool isProtectedContent,
Stan Iliev505dd572018-09-13 14:20:03 -040069 uint32_t bufferFormat, GrSurfaceOrigin surfaceOrigin)
Stan Iliev7e910df2017-06-02 10:29:21 -040070 : INHERITED(info)
Stan Ilievc01b5c72018-08-28 10:18:19 -040071 , fHardwareBuffer(hardwareBuffer)
Stan Iliev114b0912018-08-31 14:02:55 -040072 , fBufferFormat(bufferFormat)
Stan Iliev505dd572018-09-13 14:20:03 -040073 , fIsProtectedContent(isProtectedContent)
74 , fSurfaceOrigin(surfaceOrigin) {
Greg Danielf1251112018-08-27 09:55:03 -040075 AHardwareBuffer_acquire(fHardwareBuffer);
76}
77
Stan Iliev7e910df2017-06-02 10:29:21 -040078GrAHardwareBufferImageGenerator::~GrAHardwareBufferImageGenerator() {
Greg Danielf1251112018-08-27 09:55:03 -040079 AHardwareBuffer_release(fHardwareBuffer);
Stan Iliev7e910df2017-06-02 10:29:21 -040080}
81
Stan Iliev7e910df2017-06-02 10:29:21 -040082///////////////////////////////////////////////////////////////////////////////////////////////////
83
Greg Daniel40903af2020-01-30 14:55:05 -050084GrSurfaceProxyView GrAHardwareBufferImageGenerator::makeView(GrRecordingContext* context) {
Robert Phillips9338c602019-02-19 12:52:29 -050085 if (context->priv().abandoned()) {
Greg Daniel40903af2020-01-30 14:55:05 -050086 return {};
Stan Iliev7e910df2017-06-02 10:29:21 -040087 }
88
Robert Phillips9338c602019-02-19 12:52:29 -050089 auto direct = context->priv().asDirectContext();
90 if (!direct) {
Greg Daniel40903af2020-01-30 14:55:05 -050091 return {};
Robert Phillips9338c602019-02-19 12:52:29 -050092 }
93
94 GrBackendFormat backendFormat = GrAHardwareBufferUtils::GetBackendFormat(direct,
Greg Daniel173464d2019-02-06 15:30:34 -050095 fHardwareBuffer,
96 fBufferFormat,
97 false);
98
Robert Phillipsc80b0e92019-07-23 10:27:09 -040099 GrColorType grColorType = SkColorTypeToGrColorType(this->getInfo().colorType());
Stan Iliev7e910df2017-06-02 10:29:21 -0400100
Greg Danielf1251112018-08-27 09:55:03 -0400101 int width = this->getInfo().width();
102 int height = this->getInfo().height();
Greg Daniel9af948d2018-08-27 09:53:51 -0400103
Greg Danielf1251112018-08-27 09:55:03 -0400104 GrTextureType textureType = GrTextureType::k2D;
Robert Phillips4217ea72019-01-30 13:08:28 -0500105 if (context->backend() == GrBackendApi::kOpenGL) {
Greg Danielf1251112018-08-27 09:55:03 -0400106 textureType = GrTextureType::kExternal;
Robert Phillips4217ea72019-01-30 13:08:28 -0500107 } else if (context->backend() == GrBackendApi::kVulkan) {
Brian Salomond4764a12019-08-08 12:08:24 -0400108 VkFormat format;
109 SkAssertResult(backendFormat.asVkFormat(&format));
110 if (format == VK_FORMAT_UNDEFINED) {
Greg Daniel14c55c22018-12-04 11:25:03 -0500111 textureType = GrTextureType::kExternal;
112 }
Stan Iliev7e910df2017-06-02 10:29:21 -0400113 }
Greg Daniel6a0176b2018-01-30 09:28:44 -0500114
Robert Phillips9da87e02019-02-04 13:26:26 -0500115 auto proxyProvider = context->priv().proxyProvider();
Greg Danielf1251112018-08-27 09:55:03 -0400116
117 AHardwareBuffer* hardwareBuffer = fHardwareBuffer;
118 AHardwareBuffer_acquire(hardwareBuffer);
119
Stan Ilievc01b5c72018-08-28 10:18:19 -0400120 const bool isProtectedContent = fIsProtectedContent;
Greg Danielf1251112018-08-27 09:55:03 -0400121
Brian Salomon67f01952019-02-14 13:05:25 -0500122 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(); }
Greg Danielf1251112018-08-27 09:55:03 -0400129
Brian Salomon67f01952019-02-14 13:05:25 -0500130 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(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400143 [direct, buffer = AutoAHBRelease(hardwareBuffer), width, height, isProtectedContent,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400144 backendFormat](
Brian Salomonbeb7f522019-08-30 16:19:42 -0400145 GrResourceProvider* resourceProvider) -> GrSurfaceProxy::LazyCallbackResult {
Greg Daniel173464d2019-02-06 15:30:34 -0500146 GrAHardwareBufferUtils::DeleteImageProc deleteImageProc = nullptr;
Stan Ilieva56b04a2019-08-01 14:22:34 -0400147 GrAHardwareBufferUtils::UpdateImageProc updateImageProc = nullptr;
148 GrAHardwareBufferUtils::TexImageCtx texImageCtx = nullptr;
Greg Danielf1251112018-08-27 09:55:03 -0400149
Greg Daniel173464d2019-02-06 15:30:34 -0500150 GrBackendTexture backendTex =
Robert Phillips9338c602019-02-19 12:52:29 -0500151 GrAHardwareBufferUtils::MakeBackendTexture(direct, buffer.get(),
Greg Daniel173464d2019-02-06 15:30:34 -0500152 width, height,
Greg Danielf1251112018-08-27 09:55:03 -0400153 &deleteImageProc,
Stan Ilieva56b04a2019-08-01 14:22:34 -0400154 &updateImageProc,
155 &texImageCtx,
Stan Iliev114b0912018-08-31 14:02:55 -0400156 isProtectedContent,
Greg Daniel173464d2019-02-06 15:30:34 -0500157 backendFormat,
158 false);
Greg Danielf1251112018-08-27 09:55:03 -0400159 if (!backendTex.isValid()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400160 return {};
Greg Danielf1251112018-08-27 09:55:03 -0400161 }
Stan Ilieva56b04a2019-08-01 14:22:34 -0400162 SkASSERT(deleteImageProc && texImageCtx);
Greg Danielf1251112018-08-27 09:55:03 -0400163
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500164 // 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.
Brian Salomonc67c31c2018-12-06 10:00:03 -0500167 sk_sp<GrTexture> tex = resourceProvider->wrapBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400168 backendTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kYes, kRead_GrIOType);
Greg Danielf1251112018-08-27 09:55:03 -0400169 if (!tex) {
Stan Ilieva56b04a2019-08-01 14:22:34 -0400170 deleteImageProc(texImageCtx);
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400171 return {};
Greg Danielf1251112018-08-27 09:55:03 -0400172 }
173
Greg Daniel637c06a2018-09-12 09:44:25 -0400174 if (deleteImageProc) {
Stan Ilieva56b04a2019-08-01 14:22:34 -0400175 tex->setRelease(deleteImageProc, texImageCtx);
Greg Daniel637c06a2018-09-12 09:44:25 -0400176 }
Stan Iliev7e910df2017-06-02 10:29:21 -0400177
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400178 return tex;
Greg Danielf1251112018-08-27 09:55:03 -0400179 },
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400180 backendFormat, {width, height}, GrRenderable::kNo, 1, GrMipMapped::kNo,
Greg Daniel3a365112020-02-14 10:47:18 -0500181 GrMipMapsStatus::kNotAllocated, GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact,
182 SkBudgeted::kNo, GrProtected::kNo, GrSurfaceProxy::UseAllocator::kYes);
Greg Danielf1251112018-08-27 09:55:03 -0400183
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400184 GrSwizzle readSwizzle = context->priv().caps()->getReadSwizzle(backendFormat, grColorType);
185
Greg Daniel40903af2020-01-30 14:55:05 -0500186 return GrSurfaceProxyView(std::move(texProxy), fSurfaceOrigin, readSwizzle);
Greg Daniel3860cfd12018-09-07 09:13:54 -0400187}
188
Brian Salomonbc074a62020-03-18 10:06:13 -0400189GrSurfaceProxyView GrAHardwareBufferImageGenerator::onGenerateTexture(
190 GrRecordingContext* context,
191 const SkImageInfo& info,
192 const SkIPoint& origin,
193 GrMipMapped mipMapped,
194 GrImageTexGenPolicy texGenPolicy) {
Greg Daniel40903af2020-01-30 14:55:05 -0500195 GrSurfaceProxyView texProxyView = this->makeView(context);
196 if (!texProxyView.proxy()) {
Greg Danielcc104db2020-02-03 14:17:08 -0500197 return {};
Greg Daniel3860cfd12018-09-07 09:13:54 -0400198 }
Greg Danielcc104db2020-02-03 14:17:08 -0500199 SkASSERT(texProxyView.asTextureProxy());
Greg Daniel3860cfd12018-09-07 09:13:54 -0400200
Brian Salomonbc074a62020-03-18 10:06:13 -0400201 if (texGenPolicy == GrImageTexGenPolicy::kDraw && origin.isZero() &&
202 info.dimensions() == this->getInfo().dimensions() && mipMapped == GrMipMapped::kNo) {
Brian Salomona54af922020-03-03 16:56:22 -0500203 // If the caller wants the full non-MIP mapped texture we're done.
Greg Danielcc104db2020-02-03 14:17:08 -0500204 return texProxyView;
Greg Daniel3860cfd12018-09-07 09:13:54 -0400205 }
Brian Salomona54af922020-03-03 16:56:22 -0500206 // Otherwise, make a copy for the requested subset and/or MIP maps.
Greg Daniel3860cfd12018-09-07 09:13:54 -0400207 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height());
208
Brian Salomonbc074a62020-03-18 10:06:13 -0400209 SkBudgeted budgeted = texGenPolicy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted
210 ? SkBudgeted::kNo
211 : SkBudgeted::kYes;
212
Greg Daniel3155f7f2020-01-16 16:54:26 -0500213 GrColorType grColorType = SkColorTypeToGrColorType(this->getInfo().colorType());
Greg Daniel40903af2020-01-30 14:55:05 -0500214 return GrSurfaceProxy::Copy(context, texProxyView.proxy(), texProxyView.origin(), grColorType,
Brian Salomonbc074a62020-03-18 10:06:13 -0400215 mipMapped, subset, SkBackingFit::kExact, budgeted);
Stan Iliev7e910df2017-06-02 10:29:21 -0400216}
Stan Iliev7e910df2017-06-02 10:29:21 -0400217
218bool GrAHardwareBufferImageGenerator::onIsValid(GrContext* context) const {
219 if (nullptr == context) {
220 return false; //CPU backend is not supported, because hardware buffer can be swizzled
221 }
Robert Phillips4217ea72019-01-30 13:08:28 -0500222 return GrBackendApi::kOpenGL == context->backend() ||
223 GrBackendApi::kVulkan == context->backend();
Stan Iliev7e910df2017-06-02 10:29:21 -0400224}
225
226#endif //SK_BUILD_FOR_ANDROID_FRAMEWORK