blob: be2a3e27c301fdae20ce97285a34ebf07950b4b7 [file] [log] [blame]
Jim Van Verth8026ccc2018-10-04 13:10:39 -04001/*
2 * Copyright 2018 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
Greg Danielc7672092020-02-06 14:32:54 -05008#include "src/image/SkImage_GpuBase.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkPromiseImageTexture.h"
11#include "include/gpu/GrBackendSurface.h"
12#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/GrRecordingContext.h"
14#include "src/core/SkBitmapCache.h"
15#include "src/core/SkTLList.h"
16#include "src/gpu/GrClip.h"
17#include "src/gpu/GrContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040018#include "src/gpu/GrImageInfo.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040019#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrRecordingContextPriv.h"
21#include "src/gpu/GrRenderTargetContext.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000022#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrTextureAdjuster.h"
24#include "src/gpu/effects/GrYUVtoRGBEffect.h"
25#include "src/image/SkImage_Gpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/image/SkReadPixelsRec.h"
Jim Van Verth8026ccc2018-10-04 13:10:39 -040027
Brian Salomon9f2b86c2019-10-22 10:37:46 -040028SkImage_GpuBase::SkImage_GpuBase(sk_sp<GrContext> context, SkISize size, uint32_t uniqueID,
Brian Salomon5ad6fd32019-03-21 15:30:08 -040029 SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs)
Brian Salomon9f2b86c2019-10-22 10:37:46 -040030 : INHERITED(SkImageInfo::Make(size, ct, at, std::move(cs)), uniqueID)
Brian Salomon5ad6fd32019-03-21 15:30:08 -040031 , fContext(std::move(context)) {}
Jim Van Verth8026ccc2018-10-04 13:10:39 -040032
Jim Van Verth8026ccc2018-10-04 13:10:39 -040033//////////////////////////////////////////////////////////////////////////////////////////////////
34
Robert Phillipsfd0d9702019-02-01 10:19:42 -050035#if GR_TEST_UTILS
36void SkImage_GpuBase::resetContext(sk_sp<GrContext> newContext) {
Robert Phillipsfe0963c2019-02-07 13:25:07 -050037 SkASSERT(fContext->priv().matches(newContext.get()));
Robert Phillipsfd0d9702019-02-01 10:19:42 -050038 fContext = newContext;
39}
40#endif
41
Robert Phillips62221e72019-07-24 15:07:38 -040042bool SkImage_GpuBase::ValidateBackendTexture(const GrCaps* caps, const GrBackendTexture& tex,
43 GrColorType grCT, SkColorType ct, SkAlphaType at,
Jim Van Verth8026ccc2018-10-04 13:10:39 -040044 sk_sp<SkColorSpace> cs) {
45 if (!tex.isValid()) {
46 return false;
47 }
Brian Salomon2cae3b02020-03-27 11:18:08 -040048 SkColorInfo info(ct, at, cs);
49 if (!SkColorInfoIsValid(info)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040050 return false;
51 }
Brian Salomonf391d0f2018-12-14 09:18:50 -050052 GrBackendFormat backendFormat = tex.getBackendFormat();
53 if (!backendFormat.isValid()) {
54 return false;
55 }
Greg Daniele877dce2019-07-11 10:52:43 -040056
Robert Phillips62221e72019-07-24 15:07:38 -040057 return caps->areColorTypeAndFormatCompatible(grCT, backendFormat);
Jim Van Verth8026ccc2018-10-04 13:10:39 -040058}
59
Robert Phillipsead321b2019-12-19 10:16:32 -050060bool SkImage_GpuBase::ValidateCompressedBackendTexture(const GrCaps* caps,
61 const GrBackendTexture& tex,
62 SkAlphaType at) {
Robert Phillipsead321b2019-12-19 10:16:32 -050063 if (!tex.isValid() || tex.width() <= 0 || tex.height() <= 0) {
64 return false;
65 }
66
67 if (tex.width() > caps->maxTextureSize() || tex.height() > caps->maxTextureSize()) {
68 return false;
69 }
70
71 if (at == kUnknown_SkAlphaType) {
72 return false;
73 }
74
75 GrBackendFormat backendFormat = tex.getBackendFormat();
76 if (!backendFormat.isValid()) {
77 return false;
78 }
79
80 if (!caps->isFormatCompressed(backendFormat)) {
81 return false;
82 }
83
84 return true;
85}
86
Jim Van Verth8026ccc2018-10-04 13:10:39 -040087//////////////////////////////////////////////////////////////////////////////////////////////////
88
Brian Osmane50cdf02018-10-19 13:02:14 -040089bool SkImage_GpuBase::getROPixels(SkBitmap* dst, CachingHint chint) const {
Robert Phillips920d4882019-03-04 15:16:44 -050090 auto direct = fContext->priv().asDirectContext();
91 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040092 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
93 return false;
94 }
95
Jim Van Verth8026ccc2018-10-04 13:10:39 -040096 const auto desc = SkBitmapCacheDesc::Make(this);
97 if (SkBitmapCache::Find(desc, dst)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040098 SkASSERT(dst->isImmutable());
99 SkASSERT(dst->getPixels());
100 return true;
101 }
102
103 SkBitmapCache::RecPtr rec = nullptr;
104 SkPixmap pmap;
105 if (kAllow_CachingHint == chint) {
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400106 rec = SkBitmapCache::Alloc(desc, this->imageInfo(), &pmap);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400107 if (!rec) {
108 return false;
109 }
110 } else {
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400111 if (!dst->tryAllocPixels(this->imageInfo()) || !dst->peekPixels(&pmap)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400112 return false;
113 }
114 }
115
Greg Daniel37c127f2020-02-05 10:37:27 -0500116 const GrSurfaceProxyView* view = this->view(direct);
117 SkASSERT(view);
Greg Danielc7672092020-02-06 14:32:54 -0500118 GrColorType grColorType = SkColorTypeAndFormatToGrColorType(
119 fContext->priv().caps(), this->colorType(), view->proxy()->backendFormat());
Greg Danielba88ab62019-07-26 09:14:01 -0400120
Greg Daniel37c127f2020-02-05 10:37:27 -0500121 auto sContext = GrSurfaceContext::Make(direct, *view, grColorType, this->alphaType(),
122 this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400123 if (!sContext) {
124 return false;
125 }
126
Brian Salomon1d435302019-07-01 13:05:28 -0400127 if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), {0, 0})) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400128 return false;
129 }
130
131 if (rec) {
132 SkBitmapCache::Add(std::move(rec), dst);
133 this->notifyAddedToRasterCache();
134 }
135 return true;
136}
137
Robert Phillips6603a172019-03-05 12:35:44 -0500138sk_sp<SkImage> SkImage_GpuBase::onMakeSubset(GrRecordingContext* context,
139 const SkIRect& subset) const {
140 if (!context || !fContext->priv().matches(context)) {
141 return nullptr;
142 }
143
Greg Daniel37c127f2020-02-05 10:37:27 -0500144 const GrSurfaceProxyView* view = this->view(context);
145 SkASSERT(view && view->proxy());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400146
Brian Salomonc5243782020-04-02 12:50:34 -0400147 auto copyView = GrSurfaceProxyView::Copy(context, *view, GrMipMapped::kNo, subset,
148 SkBackingFit::kExact, view->proxy()->isBudgeted());
Greg Daniel3155f7f2020-01-16 16:54:26 -0500149
Brian Salomonc5243782020-04-02 12:50:34 -0400150 if (!copyView) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400151 return nullptr;
152 }
153
154 // MDB: this call is okay bc we know 'sContext' was kExact
Greg Daniel40903af2020-01-30 14:55:05 -0500155 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, std::move(copyView),
Greg Daniel7c165a42020-01-22 12:22:36 -0500156 this->colorType(), this->alphaType(), this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400157}
158
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400159bool SkImage_GpuBase::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
160 int srcX, int srcY, CachingHint) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500161 auto direct = fContext->priv().asDirectContext();
162 if (!direct) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400163 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
164 return false;
165 }
166
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400167 if (!SkImageInfoValidConversion(dstInfo, this->imageInfo())) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400168 return false;
169 }
170
Greg Daniel37c127f2020-02-05 10:37:27 -0500171 const GrSurfaceProxyView* view = this->view(direct);
172 SkASSERT(view);
Greg Danielc7672092020-02-06 14:32:54 -0500173 GrColorType grColorType = SkColorTypeAndFormatToGrColorType(
174 fContext->priv().caps(), this->colorType(), view->proxy()->backendFormat());
Greg Danielba88ab62019-07-26 09:14:01 -0400175
Greg Daniel37c127f2020-02-05 10:37:27 -0500176 auto sContext = GrSurfaceContext::Make(direct, *view, grColorType, this->alphaType(),
177 this->refColorSpace());
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400178 if (!sContext) {
179 return false;
180 }
181
Brian Salomon1d435302019-07-01 13:05:28 -0400182 return sContext->readPixels(dstInfo, dstPixels, dstRB, {srcX, srcY});
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400183}
184
Brian Salomonc8d092a2020-02-24 10:14:21 -0500185GrSurfaceProxyView SkImage_GpuBase::refView(GrRecordingContext* context,
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500186 GrMipMapped mipMapped) const {
Robert Phillips6603a172019-03-05 12:35:44 -0500187 if (!context || !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400188 SkASSERT(0);
Greg Danielfebdedf2020-02-05 17:06:27 -0500189 return {};
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400190 }
191
Greg Daniel37c127f2020-02-05 10:37:27 -0500192 GrTextureAdjuster adjuster(fContext.get(), *this->view(context), this->imageInfo().colorInfo(),
193 this->uniqueID());
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500194 return adjuster.view(mipMapped);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400195}
196
197GrBackendTexture SkImage_GpuBase::onGetBackendTexture(bool flushPendingGrContextIO,
198 GrSurfaceOrigin* origin) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500199 auto direct = fContext->priv().asDirectContext();
200 if (!direct) {
201 // This image was created with a DDL context and cannot be instantiated.
Greg Danielc7672092020-02-06 14:32:54 -0500202 return GrBackendTexture(); // invalid
Robert Phillips920d4882019-03-04 15:16:44 -0500203 }
204
Greg Danielfebdedf2020-02-05 17:06:27 -0500205 const GrSurfaceProxyView* view = this->view(direct);
206 SkASSERT(view && *view);
207 GrSurfaceProxy* proxy = view->proxy();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400208
Robert Phillips920d4882019-03-04 15:16:44 -0500209 if (!proxy->isInstantiated()) {
210 auto resourceProvider = direct->priv().resourceProvider();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400211
Robert Phillips920d4882019-03-04 15:16:44 -0500212 if (!proxy->instantiate(resourceProvider)) {
Greg Danielc7672092020-02-06 14:32:54 -0500213 return GrBackendTexture(); // invalid
Robert Phillips920d4882019-03-04 15:16:44 -0500214 }
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400215 }
216
217 GrTexture* texture = proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400218 if (texture) {
219 if (flushPendingGrContextIO) {
Greg Daniel55f040b2020-02-13 15:38:32 +0000220 direct->priv().flushSurface(proxy);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400221 }
222 if (origin) {
Greg Danielb8d84f82020-02-13 14:25:00 -0500223 *origin = view->origin();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400224 }
225 return texture->getBackendTexture();
226 }
Greg Danielc7672092020-02-06 14:32:54 -0500227 return GrBackendTexture(); // invalid
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400228}
229
Greg Daniel7c902112020-03-06 13:07:10 -0500230GrTexture* SkImage_GpuBase::getTexture() const {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400231 GrTextureProxy* proxy = this->peekProxy();
Robert Phillips193c4212019-03-04 12:18:53 -0500232 if (proxy && proxy->isInstantiated()) {
233 return proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400234 }
235
Robert Phillips193c4212019-03-04 12:18:53 -0500236 auto direct = fContext->priv().asDirectContext();
237 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400238 // This image was created with a DDL context and cannot be instantiated.
239 return nullptr;
240 }
241
Greg Danielfebdedf2020-02-05 17:06:27 -0500242 const GrSurfaceProxyView* view = this->view(direct);
243 SkASSERT(view && *view && !view->proxy()->isInstantiated());
Robert Phillips193c4212019-03-04 12:18:53 -0500244
Greg Danielfebdedf2020-02-05 17:06:27 -0500245 if (!view->proxy()->instantiate(direct->priv().resourceProvider())) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400246 return nullptr;
247 }
248
Greg Danielfebdedf2020-02-05 17:06:27 -0500249 return view->proxy()->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400250}
251
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400252bool SkImage_GpuBase::onIsValid(GrContext* context) const {
253 // The base class has already checked that context isn't abandoned (if it's not nullptr)
Robert Phillips920d4882019-03-04 15:16:44 -0500254 if (fContext->priv().abandoned()) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400255 return false;
256 }
257
Robert Phillips920d4882019-03-04 15:16:44 -0500258 if (context && !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400259 return false;
260 }
261
262 return true;
263}
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400264
Jim Van Verth0e671942018-11-09 12:03:57 -0500265bool SkImage_GpuBase::MakeTempTextureProxies(GrContext* ctx, const GrBackendTexture yuvaTextures[],
Jim Van Verth53275362018-11-09 15:42:35 -0500266 int numTextures, const SkYUVAIndex yuvaIndices[4],
267 GrSurfaceOrigin imageOrigin,
Greg Danielc7672092020-02-06 14:32:54 -0500268 GrSurfaceProxyView tempViews[4]) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500269 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400270 const GrCaps* caps = ctx->priv().caps();
Jim Van Verth0e671942018-11-09 12:03:57 -0500271
Jim Van Verth0e671942018-11-09 12:03:57 -0500272 for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) {
Robert Phillips62221e72019-07-24 15:07:38 -0400273 GrBackendFormat backendFormat = yuvaTextures[textureIndex].getBackendFormat();
Brian Salomonf391d0f2018-12-14 09:18:50 -0500274 if (!backendFormat.isValid()) {
275 return false;
276 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400277
Robert Phillips00c9f0d2019-08-02 17:17:35 -0400278 GrColorType grColorType = caps->getYUVAColorTypeFromBackendFormat(
Greg Danielc7672092020-02-06 14:32:54 -0500279 backendFormat, yuvaIndices[3].fIndex == textureIndex);
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400280 if (GrColorType::kUnknown == grColorType) {
281 return false;
282 }
283
Robert Phillips62221e72019-07-24 15:07:38 -0400284 SkASSERT(yuvaTextures[textureIndex].isValid());
Jim Van Verth0e671942018-11-09 12:03:57 -0500285
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400286 auto proxy = proxyProvider->wrapBackendTexture(yuvaTextures[textureIndex],
Greg Daniel3a365112020-02-14 10:47:18 -0500287 kBorrow_GrWrapOwnership,
Greg Danielc7672092020-02-06 14:32:54 -0500288 GrWrapCacheable::kNo, kRead_GrIOType);
289 if (!proxy) {
Jim Van Verth0e671942018-11-09 12:03:57 -0500290 return false;
291 }
Greg Danielc7672092020-02-06 14:32:54 -0500292 GrSwizzle swizzle = caps->getReadSwizzle(proxy->backendFormat(), grColorType);
293 tempViews[textureIndex] = GrSurfaceProxyView(std::move(proxy), imageOrigin, swizzle);
Jim Van Verth53275362018-11-09 15:42:35 -0500294
295 // Check that each texture contains the channel data for the corresponding YUVA index
Brian Salomon2f23ae62020-03-26 16:17:56 -0400296 auto channelFlags = GrColorTypeChannelFlags(grColorType);
Jim Van Verth53275362018-11-09 15:42:35 -0500297 for (int yuvaIndex = 0; yuvaIndex < SkYUVAIndex::kIndexCount; ++yuvaIndex) {
298 if (yuvaIndices[yuvaIndex].fIndex == textureIndex) {
299 switch (yuvaIndices[yuvaIndex].fChannel) {
300 case SkColorChannel::kR:
Greg Danielc7672092020-02-06 14:32:54 -0500301 // TODO: Chrome needs to be patched before this can be
302 // enforced.
Brian Salomon2f23ae62020-03-26 16:17:56 -0400303 // if (!(kRed_SkColorChannelFlag & channelFlags)) {
Greg Danielc7672092020-02-06 14:32:54 -0500304 // return false;
305 // }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400306 break;
307 case SkColorChannel::kG:
Brian Salomon2f23ae62020-03-26 16:17:56 -0400308 if (!(kGreen_SkColorChannelFlag & channelFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500309 return false;
310 }
311 break;
Jim Van Verth53275362018-11-09 15:42:35 -0500312 case SkColorChannel::kB:
Brian Salomon2f23ae62020-03-26 16:17:56 -0400313 if (!(kBlue_SkColorChannelFlag & channelFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500314 return false;
315 }
316 break;
317 case SkColorChannel::kA:
Brian Salomon2f23ae62020-03-26 16:17:56 -0400318 if (!(kAlpha_SkColorChannelFlag & channelFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500319 return false;
320 }
321 break;
322 }
323 }
324 }
Jim Van Verth0e671942018-11-09 12:03:57 -0500325 }
326
327 return true;
328}
329
330bool SkImage_GpuBase::RenderYUVAToRGBA(GrContext* ctx, GrRenderTargetContext* renderTargetContext,
331 const SkRect& rect, SkYUVColorSpace yuvColorSpace,
Brian Osmane9560492019-02-05 17:00:03 -0500332 sk_sp<GrColorSpaceXform> colorSpaceXform,
Greg Danielc7672092020-02-06 14:32:54 -0500333 GrSurfaceProxyView views[4],
Jim Van Verth0e671942018-11-09 12:03:57 -0500334 const SkYUVAIndex yuvaIndices[4]) {
335 SkASSERT(renderTargetContext);
336 if (!renderTargetContext->asSurfaceProxy()) {
337 return false;
338 }
339
340 GrPaint paint;
341 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
342
Brian Salomonca6b2f42020-01-24 11:31:21 -0500343 const auto& caps = *ctx->priv().caps();
Greg Danielc7672092020-02-06 14:32:54 -0500344 auto fp = GrYUVtoRGBEffect::Make(views, yuvaIndices, yuvColorSpace,
Brian Salomonca6b2f42020-01-24 11:31:21 -0500345 GrSamplerState::Filter::kNearest, caps);
Brian Osmane9560492019-02-05 17:00:03 -0500346 if (colorSpaceXform) {
347 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(colorSpaceXform));
348 }
349 paint.addColorFragmentProcessor(std::move(fp));
Jim Van Verth0e671942018-11-09 12:03:57 -0500350
351 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Jim Van Verth0e671942018-11-09 12:03:57 -0500352 return true;
353}
354
Brian Salomonbe5a0932018-12-10 10:03:26 -0500355sk_sp<GrTextureProxy> SkImage_GpuBase::MakePromiseImageLazyProxy(
Brian Salomonc3ce54a2020-04-01 16:52:37 -0400356 GrContext* context, int width, int height,
Brian Salomonbe5a0932018-12-10 10:03:26 -0500357 GrBackendFormat backendFormat, GrMipMapped mipMapped,
Greg Danielc7672092020-02-06 14:32:54 -0500358 PromiseImageTextureFulfillProc fulfillProc, PromiseImageTextureReleaseProc releaseProc,
359 PromiseImageTextureDoneProc doneProc, PromiseImageTextureContext textureContext,
Brian Salomon0cc57542019-03-08 13:28:46 -0500360 PromiseImageApiVersion version) {
Brian Salomonbe5a0932018-12-10 10:03:26 -0500361 SkASSERT(context);
362 SkASSERT(width > 0 && height > 0);
363 SkASSERT(doneProc);
364
365 if (!fulfillProc || !releaseProc) {
366 doneProc(textureContext);
367 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400368 }
369
Brian Salomonbe5a0932018-12-10 10:03:26 -0500370 if (mipMapped == GrMipMapped::kYes &&
371 GrTextureTypeHasRestrictedSampling(backendFormat.textureType())) {
372 // It is invalid to have a GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE and have mips as
373 // well.
374 doneProc(textureContext);
375 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400376 }
Brian Salomonbe5a0932018-12-10 10:03:26 -0500377
378 /**
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500379 * This class is the lazy instantiation callback for promise images. It manages calling the
380 * client's Fulfill, Release, and Done procs. It attempts to reuse a GrTexture instance in
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500381 * cases where the client provides the same SkPromiseImageTexture as Fulfill results for
382 * multiple SkImages. The created GrTexture is given a key based on a unique ID associated with
383 * the SkPromiseImageTexture.
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500384 *
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500385 * The GrTexutre idle proc mechanism is used to call the Release and Done procs. We use this
386 * instead of the GrSurface release proc because the GrTexture is cached and therefore may
387 * outlive the proxy into which this callback is installed.
388 *
389 * A key invalidation message is installed on the SkPromiseImageTexture so that the GrTexture
390 * is deleted once it can no longer be used to instantiate a proxy.
Brian Salomonbe5a0932018-12-10 10:03:26 -0500391 */
392 class PromiseLazyInstantiateCallback {
393 public:
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500394 PromiseLazyInstantiateCallback(PromiseImageTextureFulfillProc fulfillProc,
395 PromiseImageTextureReleaseProc releaseProc,
396 PromiseImageTextureDoneProc doneProc,
397 PromiseImageTextureContext context,
Brian Salomon0cc57542019-03-08 13:28:46 -0500398 PromiseImageApiVersion version)
399 : fFulfillProc(fulfillProc)
400 , fReleaseProc(releaseProc)
Brian Salomon0cc57542019-03-08 13:28:46 -0500401 , fVersion(version) {
Brian Salomone80b8092019-03-08 13:25:19 -0500402 fDoneCallback = sk_make_sp<GrRefCntedCallback>(doneProc, context);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500403 }
404 PromiseLazyInstantiateCallback(PromiseLazyInstantiateCallback&&) = default;
405 PromiseLazyInstantiateCallback(const PromiseLazyInstantiateCallback&) {
406 // Because we get wrapped in std::function we must be copyable. But we should never
407 // be copied.
408 SkASSERT(false);
409 }
410 PromiseLazyInstantiateCallback& operator=(PromiseLazyInstantiateCallback&&) = default;
411 PromiseLazyInstantiateCallback& operator=(const PromiseLazyInstantiateCallback&) {
412 SkASSERT(false);
413 return *this;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500414 }
Brian Salomon553610d2019-01-14 17:34:12 -0500415
Brian Salomon88b8d112019-03-07 15:25:34 +0000416 ~PromiseLazyInstantiateCallback() {
Brian Salomon876a0172019-03-08 11:12:14 -0500417 // Our destructor can run on any thread. We trigger the unref of fTexture by message.
Robert Phillipsddc21482019-10-16 14:30:09 -0400418 // This unreffed texture pointer is a real problem! When the context has been
419 // abandoned, the GrTexture pointed to by this pointer is deleted! Due to virtual
420 // inheritance any manipulation of this pointer at that point will cause a crash.
421 // For now we "work around" the problem by just passing it, untouched, into the
422 // message bus but this very fragile.
423 // In the future the GrSurface class hierarchy refactoring should eliminate this
424 // difficulty by removing the virtual inheritance.
Brian Salomon876a0172019-03-08 11:12:14 -0500425 if (fTexture) {
Robert Phillipsddc21482019-10-16 14:30:09 -0400426 SkMessageBus<GrTextureFreedMessage>::Post({fTexture, fTextureContextID});
Brian Salomon876a0172019-03-08 11:12:14 -0500427 }
Brian Salomon88b8d112019-03-07 15:25:34 +0000428 }
429
Brian Salomonbeb7f522019-08-30 16:19:42 -0400430 GrSurfaceProxy::LazyCallbackResult operator()(GrResourceProvider* resourceProvider) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400431 // We use the unique key in a way that is unrelated to the SkImage-based key that the
432 // proxy may receive, hence kUnsynced.
433 static constexpr auto kKeySyncMode =
434 GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
435
Brian Salomonbeb7f522019-08-30 16:19:42 -0400436 // In order to make the SkImage "thread safe" we rely on holding an extra ref to the
437 // texture in the callback and signalling the unref via a message to the resource cache.
438 // We need to extend the callback's lifetime to that of the proxy.
439 static constexpr auto kReleaseCallbackOnInstantiation = false;
440
Brian Salomon876a0172019-03-08 11:12:14 -0500441 // Our proxy is getting instantiated for the second+ time. We are only allowed to call
442 // Fulfill once. So return our cached result.
443 if (fTexture) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400444 return {sk_ref_sp(fTexture), kReleaseCallbackOnInstantiation, kKeySyncMode};
Brian Salomonc3ce54a2020-04-01 16:52:37 -0400445 } else if (fFulfillProcFailed) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400446 // We've already called fulfill and it failed. Our contract says that we should only
447 // call each callback once.
448 return {};
Brian Salomon876a0172019-03-08 11:12:14 -0500449 }
Brian Salomone80b8092019-03-08 13:25:19 -0500450 SkASSERT(fDoneCallback);
451 PromiseImageTextureContext textureContext = fDoneCallback->context();
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500452 sk_sp<SkPromiseImageTexture> promiseTexture = fFulfillProc(textureContext);
453 // From here on out our contract is that the release proc must be called, even if
454 // the return from fulfill was invalid or we fail for some other reason.
Brian Salomone80b8092019-03-08 13:25:19 -0500455 auto releaseCallback = sk_make_sp<GrRefCntedCallback>(fReleaseProc, textureContext);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500456 if (!promiseTexture) {
Brian Salomonc3ce54a2020-04-01 16:52:37 -0400457 fFulfillProcFailed = true;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400458 return {};
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500459 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500460
Robert Phillips62221e72019-07-24 15:07:38 -0400461 const GrBackendTexture& backendTexture = promiseTexture->backendTexture();
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500462 if (!backendTexture.isValid()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400463 return {};
Brian Salomon559c6172019-01-10 10:23:44 -0500464 }
465
Brian Salomonf55e8d52019-01-30 17:28:20 -0500466 sk_sp<GrTexture> tex;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500467 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Salomon7d88f312019-02-28 10:03:03 -0500468 GrUniqueKey key;
Brian Salomonc3ce54a2020-04-01 16:52:37 -0400469 GrUniqueKey::Builder builder(&key, kDomain, 1, "promise");
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500470 builder[0] = promiseTexture->uniqueID();
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500471 builder.finish();
Brian Salomon0d606762019-01-25 09:58:38 -0500472 // A texture with this key may already exist from a different instance of this lazy
473 // callback. This could happen if the client fulfills a promise image with a texture
474 // that was previously used to fulfill a different promise image.
Brian Salomon7d88f312019-02-28 10:03:03 -0500475 if (auto surf = resourceProvider->findByUniqueKey<GrSurface>(key)) {
Brian Salomon0d606762019-01-25 09:58:38 -0500476 tex = sk_ref_sp(surf->asTexture());
477 SkASSERT(tex);
478 } else {
479 if ((tex = resourceProvider->wrapBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400480 backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kYes,
481 kRead_GrIOType))) {
Brian Salomon7d88f312019-02-28 10:03:03 -0500482 tex->resourcePriv().setUniqueKey(key);
Brian Salomon0d606762019-01-25 09:58:38 -0500483 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400484 return {};
Brian Salomon0d606762019-01-25 09:58:38 -0500485 }
486 }
Brian Salomon0cc57542019-03-08 13:28:46 -0500487 auto releaseIdleState = fVersion == PromiseImageApiVersion::kLegacy
488 ? GrTexture::IdleState::kFinished
489 : GrTexture::IdleState::kFlushed;
490 tex->addIdleProc(std::move(releaseCallback), releaseIdleState);
Brian Salomone80b8092019-03-08 13:25:19 -0500491 tex->addIdleProc(std::move(fDoneCallback), GrTexture::IdleState::kFinished);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500492 promiseTexture->addKeyToInvalidate(tex->getContext()->priv().contextID(), key);
Brian Salomon876a0172019-03-08 11:12:14 -0500493 fTexture = tex.get();
494 // We need to hold on to the GrTexture in case our proxy gets reinstantiated. However,
495 // we can't unref in our destructor because we may be on another thread then. So we
496 // let the cache know it is waiting on an unref message. We will send that message from
497 // our destructor.
498 GrContext* context = fTexture->getContext();
Robert Phillipsddc21482019-10-16 14:30:09 -0400499 context->priv().getResourceCache()->insertDelayedTextureUnref(fTexture);
Brian Salomon876a0172019-03-08 11:12:14 -0500500 fTextureContextID = context->priv().contextID();
Brian Salomonbeb7f522019-08-30 16:19:42 -0400501 return {std::move(tex), kReleaseCallbackOnInstantiation, kKeySyncMode};
Brian Salomonbe5a0932018-12-10 10:03:26 -0500502 }
503
504 private:
Brian Salomone80b8092019-03-08 13:25:19 -0500505 PromiseImageTextureFulfillProc fFulfillProc;
506 PromiseImageTextureReleaseProc fReleaseProc;
507 sk_sp<GrRefCntedCallback> fDoneCallback;
Brian Salomon876a0172019-03-08 11:12:14 -0500508 GrTexture* fTexture = nullptr;
509 uint32_t fTextureContextID = SK_InvalidUniqueID;
Brian Salomon0cc57542019-03-08 13:28:46 -0500510 PromiseImageApiVersion fVersion;
Brian Salomonc3ce54a2020-04-01 16:52:37 -0400511 bool fFulfillProcFailed = false;
512 } callback(fulfillProc, releaseProc, doneProc, textureContext, version);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500513
Robert Phillips9da87e02019-02-04 13:26:26 -0500514 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonbe5a0932018-12-10 10:03:26 -0500515
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600516 // Ganesh assumes that, when wrapping a mipmapped backend texture from a client, that its
517 // mipmaps are fully fleshed out.
518 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
519 ? GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
520
Brian Salomonbe5a0932018-12-10 10:03:26 -0500521 // We pass kReadOnly here since we should treat content of the client's texture as immutable.
Brian Salomone8a766b2019-07-19 14:24:36 -0400522 // The promise API provides no way for the client to indicated that the texture is protected.
Greg Daniel3a365112020-02-14 10:47:18 -0500523 return proxyProvider->createLazyProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400524 std::move(callback), backendFormat, {width, height}, GrRenderable::kNo, 1, mipMapped,
525 mipMapsStatus, GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, SkBudgeted::kNo,
526 GrProtected::kNo, GrSurfaceProxy::UseAllocator::kYes);
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400527}