blob: 33e482a4a86ec1f71d95d74fd31318f3e8841879 [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"
21#include "include/gpu/GrTexture.h"
22#include "include/gpu/gl/GrGLTypes.h"
23#include "include/private/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/core/SkExchange.h"
Ben Wagner21bca282019-05-15 10:15:52 -040025#include "src/core/SkMessageBus.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#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 Danielf91aeb22019-06-18 09:58:02 -040033#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/gl/GrGLDefines.h"
Stan Iliev7e910df2017-06-02 10:29:21 -040035
36#include <EGL/egl.h>
37#include <EGL/eglext.h>
38#include <GLES/gl.h>
39#include <GLES/glext.h>
40
Greg Daniel637c06a2018-09-12 09:44:25 -040041#ifdef SK_VULKAN
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "include/gpu/vk/GrVkExtensions.h"
43#include "src/gpu/vk/GrVkGpu.h"
Greg Daniel637c06a2018-09-12 09:44:25 -040044#endif
45
Stan Ilievc01b5c72018-08-28 10:18:19 -040046#define PROT_CONTENT_EXT_STR "EGL_EXT_protected_content"
47#define EGL_PROTECTED_CONTENT_EXT 0x32C0
48
Stan Iliev7e910df2017-06-02 10:29:21 -040049std::unique_ptr<SkImageGenerator> GrAHardwareBufferImageGenerator::Make(
Stan Iliev505dd572018-09-13 14:20:03 -040050 AHardwareBuffer* graphicBuffer, SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace,
51 GrSurfaceOrigin surfaceOrigin) {
Stan Iliev7e910df2017-06-02 10:29:21 -040052 AHardwareBuffer_Desc bufferDesc;
53 AHardwareBuffer_describe(graphicBuffer, &bufferDesc);
Greg Daniel173464d2019-02-06 15:30:34 -050054
55 SkColorType colorType =
56 GrAHardwareBufferUtils::GetSkColorTypeFromBufferFormat(bufferDesc.format);
Stan Iliev7e910df2017-06-02 10:29:21 -040057 SkImageInfo info = SkImageInfo::Make(bufferDesc.width, bufferDesc.height, colorType,
58 alphaType, std::move(colorSpace));
Greg Daniel173464d2019-02-06 15:30:34 -050059
Stan Ilievc01b5c72018-08-28 10:18:19 -040060 bool createProtectedImage = 0 != (bufferDesc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT);
Stan Iliev505dd572018-09-13 14:20:03 -040061 return std::unique_ptr<SkImageGenerator>(new GrAHardwareBufferImageGenerator(
62 info, graphicBuffer, alphaType, createProtectedImage,
63 bufferDesc.format, surfaceOrigin));
Stan Iliev7e910df2017-06-02 10:29:21 -040064}
65
66GrAHardwareBufferImageGenerator::GrAHardwareBufferImageGenerator(const SkImageInfo& info,
Stan Iliev114b0912018-08-31 14:02:55 -040067 AHardwareBuffer* hardwareBuffer, SkAlphaType alphaType, bool isProtectedContent,
Stan Iliev505dd572018-09-13 14:20:03 -040068 uint32_t bufferFormat, GrSurfaceOrigin surfaceOrigin)
Stan Iliev7e910df2017-06-02 10:29:21 -040069 : INHERITED(info)
Stan Ilievc01b5c72018-08-28 10:18:19 -040070 , fHardwareBuffer(hardwareBuffer)
Stan Iliev114b0912018-08-31 14:02:55 -040071 , fBufferFormat(bufferFormat)
Stan Iliev505dd572018-09-13 14:20:03 -040072 , fIsProtectedContent(isProtectedContent)
73 , fSurfaceOrigin(surfaceOrigin) {
Greg Danielf1251112018-08-27 09:55:03 -040074 AHardwareBuffer_acquire(fHardwareBuffer);
75}
76
Stan Iliev7e910df2017-06-02 10:29:21 -040077GrAHardwareBufferImageGenerator::~GrAHardwareBufferImageGenerator() {
Greg Danielf1251112018-08-27 09:55:03 -040078 AHardwareBuffer_release(fHardwareBuffer);
Stan Iliev7e910df2017-06-02 10:29:21 -040079}
80
Stan Iliev7e910df2017-06-02 10:29:21 -040081///////////////////////////////////////////////////////////////////////////////////////////////////
82
Greg Daniel40903af2020-01-30 14:55:05 -050083GrSurfaceProxyView GrAHardwareBufferImageGenerator::makeView(GrRecordingContext* context) {
Robert Phillips9338c602019-02-19 12:52:29 -050084 if (context->priv().abandoned()) {
Greg Daniel40903af2020-01-30 14:55:05 -050085 return {};
Stan Iliev7e910df2017-06-02 10:29:21 -040086 }
87
Robert Phillips9338c602019-02-19 12:52:29 -050088 auto direct = context->priv().asDirectContext();
89 if (!direct) {
Greg Daniel40903af2020-01-30 14:55:05 -050090 return {};
Robert Phillips9338c602019-02-19 12:52:29 -050091 }
92
93 GrBackendFormat backendFormat = GrAHardwareBufferUtils::GetBackendFormat(direct,
Greg Daniel173464d2019-02-06 15:30:34 -050094 fHardwareBuffer,
95 fBufferFormat,
96 false);
97
Robert Phillipsc80b0e92019-07-23 10:27:09 -040098 GrColorType grColorType = SkColorTypeToGrColorType(this->getInfo().colorType());
Stan Iliev7e910df2017-06-02 10:29:21 -040099
Greg Danielf1251112018-08-27 09:55:03 -0400100 int width = this->getInfo().width();
101 int height = this->getInfo().height();
Greg Daniel9af948d2018-08-27 09:53:51 -0400102
Greg Danielf1251112018-08-27 09:55:03 -0400103 GrSurfaceDesc desc;
104 desc.fWidth = width;
105 desc.fHeight = height;
Greg Daniel9af948d2018-08-27 09:53:51 -0400106
Greg Danielf1251112018-08-27 09:55:03 -0400107 GrTextureType textureType = GrTextureType::k2D;
Robert Phillips4217ea72019-01-30 13:08:28 -0500108 if (context->backend() == GrBackendApi::kOpenGL) {
Greg Danielf1251112018-08-27 09:55:03 -0400109 textureType = GrTextureType::kExternal;
Robert Phillips4217ea72019-01-30 13:08:28 -0500110 } else if (context->backend() == GrBackendApi::kVulkan) {
Brian Salomond4764a12019-08-08 12:08:24 -0400111 VkFormat format;
112 SkAssertResult(backendFormat.asVkFormat(&format));
113 if (format == VK_FORMAT_UNDEFINED) {
Greg Daniel14c55c22018-12-04 11:25:03 -0500114 textureType = GrTextureType::kExternal;
115 }
Stan Iliev7e910df2017-06-02 10:29:21 -0400116 }
Greg Daniel6a0176b2018-01-30 09:28:44 -0500117
Robert Phillips9da87e02019-02-04 13:26:26 -0500118 auto proxyProvider = context->priv().proxyProvider();
Greg Danielf1251112018-08-27 09:55:03 -0400119
120 AHardwareBuffer* hardwareBuffer = fHardwareBuffer;
121 AHardwareBuffer_acquire(hardwareBuffer);
122
Stan Ilievc01b5c72018-08-28 10:18:19 -0400123 const bool isProtectedContent = fIsProtectedContent;
Greg Danielf1251112018-08-27 09:55:03 -0400124
Brian Salomon67f01952019-02-14 13:05:25 -0500125 class AutoAHBRelease {
126 public:
127 AutoAHBRelease(AHardwareBuffer* ahb) : fAhb(ahb) {}
128 // std::function() must be CopyConstructible, but ours should never actually be copied.
129 AutoAHBRelease(const AutoAHBRelease&) { SkASSERT(0); }
130 AutoAHBRelease(AutoAHBRelease&& that) : fAhb(that.fAhb) { that.fAhb = nullptr; }
131 ~AutoAHBRelease() { fAhb ? AHardwareBuffer_release(fAhb) : void(); }
Greg Danielf1251112018-08-27 09:55:03 -0400132
Brian Salomon67f01952019-02-14 13:05:25 -0500133 AutoAHBRelease& operator=(AutoAHBRelease&& that) {
134 fAhb = skstd::exchange(that.fAhb, nullptr);
135 return *this;
136 }
137 AutoAHBRelease& operator=(const AutoAHBRelease&) = delete;
138
139 AHardwareBuffer* get() const { return fAhb; }
140
141 private:
142 AHardwareBuffer* fAhb;
143 };
144
Greg Danielce3ddaa2020-01-22 16:58:15 -0500145 GrSwizzle readSwizzle = context->priv().caps()->getReadSwizzle(backendFormat, grColorType);
146
Brian Salomon67f01952019-02-14 13:05:25 -0500147 sk_sp<GrTextureProxy> texProxy = proxyProvider->createLazyProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400148 [direct, buffer = AutoAHBRelease(hardwareBuffer), width, height, isProtectedContent,
149 backendFormat, grColorType](
150 GrResourceProvider* resourceProvider) -> GrSurfaceProxy::LazyCallbackResult {
Greg Daniel173464d2019-02-06 15:30:34 -0500151 GrAHardwareBufferUtils::DeleteImageProc deleteImageProc = nullptr;
Stan Ilieva56b04a2019-08-01 14:22:34 -0400152 GrAHardwareBufferUtils::UpdateImageProc updateImageProc = nullptr;
153 GrAHardwareBufferUtils::TexImageCtx texImageCtx = nullptr;
Greg Danielf1251112018-08-27 09:55:03 -0400154
Greg Daniel173464d2019-02-06 15:30:34 -0500155 GrBackendTexture backendTex =
Robert Phillips9338c602019-02-19 12:52:29 -0500156 GrAHardwareBufferUtils::MakeBackendTexture(direct, buffer.get(),
Greg Daniel173464d2019-02-06 15:30:34 -0500157 width, height,
Greg Danielf1251112018-08-27 09:55:03 -0400158 &deleteImageProc,
Stan Ilieva56b04a2019-08-01 14:22:34 -0400159 &updateImageProc,
160 &texImageCtx,
Stan Iliev114b0912018-08-31 14:02:55 -0400161 isProtectedContent,
Greg Daniel173464d2019-02-06 15:30:34 -0500162 backendFormat,
163 false);
Greg Danielf1251112018-08-27 09:55:03 -0400164 if (!backendTex.isValid()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400165 return {};
Greg Danielf1251112018-08-27 09:55:03 -0400166 }
Stan Ilieva56b04a2019-08-01 14:22:34 -0400167 SkASSERT(deleteImageProc && texImageCtx);
Greg Danielf1251112018-08-27 09:55:03 -0400168
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500169 // We make this texture cacheable to avoid recreating a GrTexture every time this
170 // is invoked. We know the owning SkIamge will send an invalidation message when the
171 // image is destroyed, so the texture will be removed at that time.
Brian Salomonc67c31c2018-12-06 10:00:03 -0500172 sk_sp<GrTexture> tex = resourceProvider->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400173 backendTex, grColorType, kBorrow_GrWrapOwnership, GrWrapCacheable::kYes,
174 kRead_GrIOType);
Greg Danielf1251112018-08-27 09:55:03 -0400175 if (!tex) {
Stan Ilieva56b04a2019-08-01 14:22:34 -0400176 deleteImageProc(texImageCtx);
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400177 return {};
Greg Danielf1251112018-08-27 09:55:03 -0400178 }
179
Greg Daniel637c06a2018-09-12 09:44:25 -0400180 if (deleteImageProc) {
Stan Ilieva56b04a2019-08-01 14:22:34 -0400181 tex->setRelease(deleteImageProc, texImageCtx);
Greg Daniel637c06a2018-09-12 09:44:25 -0400182 }
Stan Iliev7e910df2017-06-02 10:29:21 -0400183
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400184 return tex;
Greg Danielf1251112018-08-27 09:55:03 -0400185 },
Greg Danielce3ddaa2020-01-22 16:58:15 -0500186 backendFormat, desc, readSwizzle, GrRenderable::kNo, 1, fSurfaceOrigin,
187 GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated, GrInternalSurfaceFlags::kReadOnly,
188 SkBackingFit::kExact, SkBudgeted::kNo, GrProtected::kNo,
189 GrSurfaceProxy::UseAllocator::kYes);
Greg Danielf1251112018-08-27 09:55:03 -0400190
Greg Daniel40903af2020-01-30 14:55:05 -0500191 return GrSurfaceProxyView(std::move(texProxy), fSurfaceOrigin, readSwizzle);
Greg Daniel3860cfd12018-09-07 09:13:54 -0400192}
193
194sk_sp<GrTextureProxy> GrAHardwareBufferImageGenerator::onGenerateTexture(
Robert Phillips9338c602019-02-19 12:52:29 -0500195 GrRecordingContext* context, const SkImageInfo& info,
196 const SkIPoint& origin, bool willNeedMipMaps) {
Greg Daniel40903af2020-01-30 14:55:05 -0500197 GrSurfaceProxyView texProxyView = this->makeView(context);
198 if (!texProxyView.proxy()) {
Greg Daniel3860cfd12018-09-07 09:13:54 -0400199 return nullptr;
200 }
201
202 if (0 == origin.fX && 0 == origin.fY &&
203 info.width() == this->getInfo().width() && info.height() == this->getInfo().height()) {
204 // If the caller wants the full texture we're done. The caller will handle making a copy for
205 // mip maps if that is required.
Greg Daniel40903af2020-01-30 14:55:05 -0500206 return texProxyView.asTextureProxyRef();
Greg Daniel3860cfd12018-09-07 09:13:54 -0400207 }
208 // Otherwise, make a copy for the requested subset.
209 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height());
210
211 GrMipMapped mipMapped = willNeedMipMaps ? GrMipMapped::kYes : GrMipMapped::kNo;
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,
215 mipMapped, subset, SkBackingFit::kExact,
216 SkBudgeted::kYes).asTextureProxyRef();
Stan Iliev7e910df2017-06-02 10:29:21 -0400217}
Stan Iliev7e910df2017-06-02 10:29:21 -0400218
219bool GrAHardwareBufferImageGenerator::onIsValid(GrContext* context) const {
220 if (nullptr == context) {
221 return false; //CPU backend is not supported, because hardware buffer can be swizzled
222 }
Robert Phillips4217ea72019-01-30 13:08:28 -0500223 return GrBackendApi::kOpenGL == context->backend() ||
224 GrBackendApi::kVulkan == context->backend();
Stan Iliev7e910df2017-06-02 10:29:21 -0400225}
226
227#endif //SK_BUILD_FOR_ANDROID_FRAMEWORK