blob: 205595526fd362b5821d506453a3f57c77e3e3e3 [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
Greg Daniel54bfb182018-11-20 17:12:36 -05008#include "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
Stan Iliev7e910df2017-06-02 10:29:21 -040015#include "GrAHardwareBufferImageGenerator.h"
16
Derek Sollenberger7a869872017-06-27 15:37:25 -040017#include <android/hardware_buffer.h>
18
Greg Daniel173464d2019-02-06 15:30:34 -050019#include "GrAHardwareBufferUtils.h"
Stan Iliev7e910df2017-06-02 10:29:21 -040020#include "GrBackendSurface.h"
21#include "GrContext.h"
22#include "GrContextPriv.h"
Robert Phillipsadbe1322018-01-17 13:35:46 -050023#include "GrProxyProvider.h"
Robert Phillips9338c602019-02-19 12:52:29 -050024#include "GrRecordingContext.h"
25#include "GrRecordingContextPriv.h"
Stan Ilievdbba55d2017-06-28 13:24:41 -040026#include "GrResourceCache.h"
Robert Phillips00018282017-06-15 15:35:16 -040027#include "GrResourceProvider.h"
Greg Daniel637c06a2018-09-12 09:44:25 -040028#include "GrResourceProviderPriv.h"
Robert Phillips847d4c52017-06-13 18:21:44 -040029#include "GrTexture.h"
Robert Phillipsade9f612017-06-16 07:32:43 -040030#include "GrTextureProxy.h"
Brian Salomon67f01952019-02-14 13:05:25 -050031#include "SkExchange.h"
Stan Ilievdbba55d2017-06-28 13:24:41 -040032#include "SkMessageBus.h"
Greg Danielf1251112018-08-27 09:55:03 -040033#include "gl/GrGLDefines.h"
34#include "gl/GrGLTypes.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
42#include "vk/GrVkExtensions.h"
43#include "vk/GrVkGpu.h"
44#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
Robert Phillips9338c602019-02-19 12:52:29 -050083sk_sp<GrTextureProxy> GrAHardwareBufferImageGenerator::makeProxy(GrRecordingContext* context) {
84 if (context->priv().abandoned()) {
Greg Daniel3860cfd2018-09-07 09:13:54 -040085 return nullptr;
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) {
90 return nullptr;
91 }
92
93 GrBackendFormat backendFormat = GrAHardwareBufferUtils::GetBackendFormat(direct,
Greg Daniel173464d2019-02-06 15:30:34 -050094 fHardwareBuffer,
95 fBufferFormat,
96 false);
97
Robert Phillips9da87e02019-02-04 13:26:26 -050098 GrPixelConfig pixelConfig = context->priv().caps()->getConfigFromBackendFormat(
Brian Salomonf391d0f2018-12-14 09:18:50 -050099 backendFormat, this->getInfo().colorType());
100
101 if (pixelConfig == kUnknown_GrPixelConfig) {
Greg Daniel3860cfd2018-09-07 09:13:54 -0400102 return nullptr;
Stan Iliev7e910df2017-06-02 10:29:21 -0400103 }
104
Greg Danielf1251112018-08-27 09:55:03 -0400105 int width = this->getInfo().width();
106 int height = this->getInfo().height();
Greg Daniel9af948d2018-08-27 09:53:51 -0400107
Greg Danielf1251112018-08-27 09:55:03 -0400108 GrSurfaceDesc desc;
109 desc.fWidth = width;
110 desc.fHeight = height;
111 desc.fConfig = pixelConfig;
Greg Daniel9af948d2018-08-27 09:53:51 -0400112
Greg Danielf1251112018-08-27 09:55:03 -0400113 GrTextureType textureType = GrTextureType::k2D;
Robert Phillips4217ea72019-01-30 13:08:28 -0500114 if (context->backend() == GrBackendApi::kOpenGL) {
Greg Danielf1251112018-08-27 09:55:03 -0400115 textureType = GrTextureType::kExternal;
Robert Phillips4217ea72019-01-30 13:08:28 -0500116 } else if (context->backend() == GrBackendApi::kVulkan) {
Greg Daniel14c55c22018-12-04 11:25:03 -0500117 const VkFormat* format = backendFormat.getVkFormat();
118 SkASSERT(format);
119 if (*format == VK_FORMAT_UNDEFINED) {
120 textureType = GrTextureType::kExternal;
121 }
Stan Iliev7e910df2017-06-02 10:29:21 -0400122 }
Greg Daniel6a0176b2018-01-30 09:28:44 -0500123
Robert Phillips9da87e02019-02-04 13:26:26 -0500124 auto proxyProvider = context->priv().proxyProvider();
Greg Danielf1251112018-08-27 09:55:03 -0400125
126 AHardwareBuffer* hardwareBuffer = fHardwareBuffer;
127 AHardwareBuffer_acquire(hardwareBuffer);
128
Stan Ilievc01b5c72018-08-28 10:18:19 -0400129 const bool isProtectedContent = fIsProtectedContent;
Greg Danielf1251112018-08-27 09:55:03 -0400130
Brian Salomon67f01952019-02-14 13:05:25 -0500131 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 Danielf1251112018-08-27 09:55:03 -0400138
Brian Salomon67f01952019-02-14 13:05:25 -0500139 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 Phillips9338c602019-02-19 12:52:29 -0500152 [direct, buffer = AutoAHBRelease(hardwareBuffer), width, height, pixelConfig,
Brian Salomon67f01952019-02-14 13:05:25 -0500153 isProtectedContent, backendFormat](GrResourceProvider* resourceProvider) {
Greg Daniel173464d2019-02-06 15:30:34 -0500154 GrAHardwareBufferUtils::DeleteImageProc deleteImageProc = nullptr;
155 GrAHardwareBufferUtils::DeleteImageCtx deleteImageCtx = nullptr;
Greg Danielf1251112018-08-27 09:55:03 -0400156
Greg Daniel173464d2019-02-06 15:30:34 -0500157 GrBackendTexture backendTex =
Robert Phillips9338c602019-02-19 12:52:29 -0500158 GrAHardwareBufferUtils::MakeBackendTexture(direct, buffer.get(),
Greg Daniel173464d2019-02-06 15:30:34 -0500159 width, height,
Greg Danielf1251112018-08-27 09:55:03 -0400160 &deleteImageProc,
Stan Ilievc01b5c72018-08-28 10:18:19 -0400161 &deleteImageCtx,
Stan Iliev114b0912018-08-31 14:02:55 -0400162 isProtectedContent,
Greg Daniel173464d2019-02-06 15:30:34 -0500163 backendFormat,
164 false);
Greg Danielf1251112018-08-27 09:55:03 -0400165 if (!backendTex.isValid()) {
166 return sk_sp<GrTexture>();
167 }
168 SkASSERT(deleteImageProc && deleteImageCtx);
169
170 backendTex.fConfig = pixelConfig;
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500171 // 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 Salomonc67c31c2018-12-06 10:00:03 -0500174 sk_sp<GrTexture> tex = resourceProvider->wrapBackendTexture(
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500175 backendTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kYes, kRead_GrIOType);
Greg Danielf1251112018-08-27 09:55:03 -0400176 if (!tex) {
Greg Danielf1251112018-08-27 09:55:03 -0400177 deleteImageProc(deleteImageCtx);
178 return sk_sp<GrTexture>();
179 }
180
Greg Daniel637c06a2018-09-12 09:44:25 -0400181 if (deleteImageProc) {
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500182 tex->setRelease(deleteImageProc, deleteImageCtx);
Greg Daniel637c06a2018-09-12 09:44:25 -0400183 }
Stan Iliev7e910df2017-06-02 10:29:21 -0400184
Greg Danielf1251112018-08-27 09:55:03 -0400185 return tex;
186 },
Brian Salomonc67c31c2018-12-06 10:00:03 -0500187 backendFormat, desc, fSurfaceOrigin, GrMipMapped::kNo,
188 GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, SkBudgeted::kNo);
Greg Danielf1251112018-08-27 09:55:03 -0400189
Greg Daniel3860cfd2018-09-07 09:13:54 -0400190 return texProxy;
191}
192
193sk_sp<GrTextureProxy> GrAHardwareBufferImageGenerator::onGenerateTexture(
Robert Phillips9338c602019-02-19 12:52:29 -0500194 GrRecordingContext* context, const SkImageInfo& info,
195 const SkIPoint& origin, bool willNeedMipMaps) {
Greg Daniel3860cfd2018-09-07 09:13:54 -0400196 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 Salomonfee3f9b2018-12-19 12:34:12 -0500212 return GrSurfaceProxy::Copy(context, texProxy.get(), mipMapped, subset, SkBackingFit::kExact,
213 SkBudgeted::kYes);
Stan Iliev7e910df2017-06-02 10:29:21 -0400214}
Stan Iliev7e910df2017-06-02 10:29:21 -0400215
216bool 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 Phillips4217ea72019-01-30 13:08:28 -0500220 return GrBackendApi::kOpenGL == context->backend() ||
221 GrBackendApi::kVulkan == context->backend();
Stan Iliev7e910df2017-06-02 10:29:21 -0400222}
223
224#endif //SK_BUILD_FOR_ANDROID_FRAMEWORK