blob: 05904670f6d13572629784d7659ccb0d8b4dd195 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkPromiseImageTexture.h"
9#include "include/gpu/GrBackendSurface.h"
10#include "include/gpu/GrContext.h"
11#include "include/gpu/GrTexture.h"
12#include "include/private/GrRecordingContext.h"
13#include "src/core/SkBitmapCache.h"
14#include "src/core/SkTLList.h"
15#include "src/gpu/GrClip.h"
16#include "src/gpu/GrContextPriv.h"
17#include "src/gpu/GrRecordingContextPriv.h"
18#include "src/gpu/GrRenderTargetContext.h"
19#include "src/gpu/GrTextureAdjuster.h"
20#include "src/gpu/effects/GrYUVtoRGBEffect.h"
21#include "src/image/SkImage_Gpu.h"
22#include "src/image/SkImage_GpuBase.h"
23#include "src/image/SkReadPixelsRec.h"
Jim Van Verth8026ccc2018-10-04 13:10:39 -040024
25SkImage_GpuBase::SkImage_GpuBase(sk_sp<GrContext> context, int width, int height, uint32_t uniqueID,
Brian Salomon5ad6fd32019-03-21 15:30:08 -040026 SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs)
27 : INHERITED(SkImageInfo::Make(width, height, ct, at, std::move(cs)), uniqueID)
28 , fContext(std::move(context)) {}
Jim Van Verth8026ccc2018-10-04 13:10:39 -040029
30SkImage_GpuBase::~SkImage_GpuBase() {}
31
32//////////////////////////////////////////////////////////////////////////////////////////////////
33
Robert Phillipsfd0d9702019-02-01 10:19:42 -050034#if GR_TEST_UTILS
35void SkImage_GpuBase::resetContext(sk_sp<GrContext> newContext) {
Robert Phillipsfe0963c2019-02-07 13:25:07 -050036 SkASSERT(fContext->priv().matches(newContext.get()));
Robert Phillipsfd0d9702019-02-01 10:19:42 -050037 fContext = newContext;
38}
39#endif
40
Robert Phillips62221e72019-07-24 15:07:38 -040041bool SkImage_GpuBase::ValidateBackendTexture(const GrCaps* caps, const GrBackendTexture& tex,
42 GrColorType grCT, SkColorType ct, SkAlphaType at,
Jim Van Verth8026ccc2018-10-04 13:10:39 -040043 sk_sp<SkColorSpace> cs) {
44 if (!tex.isValid()) {
45 return false;
46 }
47 // TODO: Create a SkImageColorInfo struct for color, alpha, and color space so we don't need to
48 // create a fake image info here.
49 SkImageInfo info = SkImageInfo::Make(1, 1, ct, at, cs);
50 if (!SkImageInfoIsValid(info)) {
51 return false;
52 }
Brian Salomonf391d0f2018-12-14 09:18:50 -050053 GrBackendFormat backendFormat = tex.getBackendFormat();
54 if (!backendFormat.isValid()) {
55 return false;
56 }
Greg Daniele877dce2019-07-11 10:52:43 -040057
Robert Phillips62221e72019-07-24 15:07:38 -040058 return caps->areColorTypeAndFormatCompatible(grCT, backendFormat);
Jim Van Verth8026ccc2018-10-04 13:10:39 -040059}
60
61//////////////////////////////////////////////////////////////////////////////////////////////////
62
Brian Osmane50cdf02018-10-19 13:02:14 -040063bool SkImage_GpuBase::getROPixels(SkBitmap* dst, CachingHint chint) const {
Robert Phillips920d4882019-03-04 15:16:44 -050064 auto direct = fContext->priv().asDirectContext();
65 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040066 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
67 return false;
68 }
69
Jim Van Verth8026ccc2018-10-04 13:10:39 -040070 const auto desc = SkBitmapCacheDesc::Make(this);
71 if (SkBitmapCache::Find(desc, dst)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040072 SkASSERT(dst->isImmutable());
73 SkASSERT(dst->getPixels());
74 return true;
75 }
76
77 SkBitmapCache::RecPtr rec = nullptr;
78 SkPixmap pmap;
79 if (kAllow_CachingHint == chint) {
Brian Salomon5ad6fd32019-03-21 15:30:08 -040080 rec = SkBitmapCache::Alloc(desc, this->imageInfo(), &pmap);
Jim Van Verth8026ccc2018-10-04 13:10:39 -040081 if (!rec) {
82 return false;
83 }
84 } else {
Brian Salomon5ad6fd32019-03-21 15:30:08 -040085 if (!dst->tryAllocPixels(this->imageInfo()) || !dst->peekPixels(&pmap)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040086 return false;
87 }
88 }
89
Brian Salomond6287472019-06-24 15:50:07 -040090 sk_sp<GrSurfaceContext> sContext =
91 direct->priv().makeWrappedSurfaceContext(this->asTextureProxyRef(direct),
92 SkColorTypeToGrColorType(this->colorType()),
93 this->alphaType(),
94 this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -040095 if (!sContext) {
96 return false;
97 }
98
Brian Salomon1d435302019-07-01 13:05:28 -040099 if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), {0, 0})) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400100 return false;
101 }
102
103 if (rec) {
104 SkBitmapCache::Add(std::move(rec), dst);
105 this->notifyAddedToRasterCache();
106 }
107 return true;
108}
109
Robert Phillips6603a172019-03-05 12:35:44 -0500110sk_sp<SkImage> SkImage_GpuBase::onMakeSubset(GrRecordingContext* context,
111 const SkIRect& subset) const {
112 if (!context || !fContext->priv().matches(context)) {
113 return nullptr;
114 }
115
116 sk_sp<GrSurfaceProxy> proxy = this->asTextureProxyRef(context);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400117
Greg Daniel46cfbc62019-06-07 11:43:30 -0400118 sk_sp<GrTextureProxy> copyProxy = GrSurfaceProxy::Copy(
119 context, proxy.get(), GrMipMapped::kNo, subset, SkBackingFit::kExact,
120 proxy->isBudgeted());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400121
Greg Daniel46cfbc62019-06-07 11:43:30 -0400122 if (!copyProxy) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400123 return nullptr;
124 }
125
126 // MDB: this call is okay bc we know 'sContext' was kExact
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400127 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, this->alphaType(),
Greg Daniel46cfbc62019-06-07 11:43:30 -0400128 std::move(copyProxy), this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400129}
130
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400131bool SkImage_GpuBase::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
132 int srcX, int srcY, CachingHint) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500133 auto direct = fContext->priv().asDirectContext();
134 if (!direct) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400135 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
136 return false;
137 }
138
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400139 if (!SkImageInfoValidConversion(dstInfo, this->imageInfo())) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400140 return false;
141 }
142
Robert Phillips920d4882019-03-04 15:16:44 -0500143 sk_sp<GrSurfaceContext> sContext = direct->priv().makeWrappedSurfaceContext(
Brian Salomond6287472019-06-24 15:50:07 -0400144 this->asTextureProxyRef(direct), SkColorTypeToGrColorType(this->colorType()),
145 this->alphaType(), this->refColorSpace());
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400146 if (!sContext) {
147 return false;
148 }
149
Brian Salomon1d435302019-07-01 13:05:28 -0400150 return sContext->readPixels(dstInfo, dstPixels, dstRB, {srcX, srcY});
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400151}
152
Robert Phillips9338c602019-02-19 12:52:29 -0500153sk_sp<GrTextureProxy> SkImage_GpuBase::asTextureProxyRef(GrRecordingContext* context,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400154 const GrSamplerState& params,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400155 SkScalar scaleAdjust[2]) const {
Robert Phillips6603a172019-03-05 12:35:44 -0500156 if (!context || !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400157 SkASSERT(0);
158 return nullptr;
159 }
160
Brian Salomond6287472019-06-24 15:50:07 -0400161 GrTextureAdjuster adjuster(fContext.get(), this->asTextureProxyRef(context),
162 SkColorTypeToGrColorType(this->colorType()), this->alphaType(),
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400163 this->uniqueID(), this->colorSpace());
Brian Osman6064e1c2018-10-19 14:27:54 -0400164 return adjuster.refTextureProxyForParams(params, scaleAdjust);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400165}
166
167GrBackendTexture SkImage_GpuBase::onGetBackendTexture(bool flushPendingGrContextIO,
168 GrSurfaceOrigin* origin) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500169 auto direct = fContext->priv().asDirectContext();
170 if (!direct) {
171 // This image was created with a DDL context and cannot be instantiated.
Robert Phillips6603a172019-03-05 12:35:44 -0500172 return GrBackendTexture(); // invalid
Robert Phillips920d4882019-03-04 15:16:44 -0500173 }
174
Robert Phillips6603a172019-03-05 12:35:44 -0500175 sk_sp<GrTextureProxy> proxy = this->asTextureProxyRef(direct);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400176 SkASSERT(proxy);
177
Robert Phillips920d4882019-03-04 15:16:44 -0500178 if (!proxy->isInstantiated()) {
179 auto resourceProvider = direct->priv().resourceProvider();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400180
Robert Phillips920d4882019-03-04 15:16:44 -0500181 if (!proxy->instantiate(resourceProvider)) {
182 return GrBackendTexture(); // invalid
183 }
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400184 }
185
186 GrTexture* texture = proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400187 if (texture) {
188 if (flushPendingGrContextIO) {
Greg Daniel4aa13e72019-04-15 14:42:20 -0400189 direct->priv().flushSurface(proxy.get());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400190 }
191 if (origin) {
192 *origin = proxy->origin();
193 }
194 return texture->getBackendTexture();
195 }
196 return GrBackendTexture(); // invalid
197}
198
199GrTexture* SkImage_GpuBase::onGetTexture() const {
200 GrTextureProxy* proxy = this->peekProxy();
Robert Phillips193c4212019-03-04 12:18:53 -0500201 if (proxy && proxy->isInstantiated()) {
202 return proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400203 }
204
Robert Phillips193c4212019-03-04 12:18:53 -0500205 auto direct = fContext->priv().asDirectContext();
206 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400207 // This image was created with a DDL context and cannot be instantiated.
208 return nullptr;
209 }
210
Robert Phillips6603a172019-03-05 12:35:44 -0500211 sk_sp<GrTextureProxy> proxyRef = this->asTextureProxyRef(direct);
Robert Phillips193c4212019-03-04 12:18:53 -0500212 SkASSERT(proxyRef && !proxyRef->isInstantiated());
213
214 if (!proxyRef->instantiate(direct->priv().resourceProvider())) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400215 return nullptr;
216 }
217
Robert Phillips193c4212019-03-04 12:18:53 -0500218 return proxyRef->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400219}
220
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400221bool SkImage_GpuBase::onIsValid(GrContext* context) const {
222 // The base class has already checked that context isn't abandoned (if it's not nullptr)
Robert Phillips920d4882019-03-04 15:16:44 -0500223 if (fContext->priv().abandoned()) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400224 return false;
225 }
226
Robert Phillips920d4882019-03-04 15:16:44 -0500227 if (context && !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400228 return false;
229 }
230
231 return true;
232}
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400233
Jim Van Verth0e671942018-11-09 12:03:57 -0500234bool SkImage_GpuBase::MakeTempTextureProxies(GrContext* ctx, const GrBackendTexture yuvaTextures[],
Jim Van Verth53275362018-11-09 15:42:35 -0500235 int numTextures, const SkYUVAIndex yuvaIndices[4],
236 GrSurfaceOrigin imageOrigin,
Jim Van Verth0e671942018-11-09 12:03:57 -0500237 sk_sp<GrTextureProxy> tempTextureProxies[4]) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500238 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400239 const GrCaps* caps = ctx->priv().caps();
Jim Van Verth0e671942018-11-09 12:03:57 -0500240
Jim Van Verth0e671942018-11-09 12:03:57 -0500241 for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) {
Robert Phillips62221e72019-07-24 15:07:38 -0400242 GrBackendFormat backendFormat = yuvaTextures[textureIndex].getBackendFormat();
Brian Salomonf391d0f2018-12-14 09:18:50 -0500243 if (!backendFormat.isValid()) {
244 return false;
245 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400246
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400247 GrColorType grColorType = caps->getYUVAColorTypeFromBackendFormat(backendFormat);
248 if (GrColorType::kUnknown == grColorType) {
249 return false;
250 }
251
Robert Phillips62221e72019-07-24 15:07:38 -0400252 SkASSERT(yuvaTextures[textureIndex].isValid());
Jim Van Verth0e671942018-11-09 12:03:57 -0500253
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500254 tempTextureProxies[textureIndex] = proxyProvider->wrapBackendTexture(
Robert Phillips62221e72019-07-24 15:07:38 -0400255 yuvaTextures[textureIndex], grColorType, imageOrigin, kBorrow_GrWrapOwnership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500256 GrWrapCacheable::kNo, kRead_GrIOType);
Jim Van Verth0e671942018-11-09 12:03:57 -0500257 if (!tempTextureProxies[textureIndex]) {
258 return false;
259 }
Jim Van Verth53275362018-11-09 15:42:35 -0500260
261 // Check that each texture contains the channel data for the corresponding YUVA index
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400262 auto componentFlags = GrColorTypeComponentFlags(grColorType);
Jim Van Verth53275362018-11-09 15:42:35 -0500263 for (int yuvaIndex = 0; yuvaIndex < SkYUVAIndex::kIndexCount; ++yuvaIndex) {
264 if (yuvaIndices[yuvaIndex].fIndex == textureIndex) {
265 switch (yuvaIndices[yuvaIndex].fChannel) {
266 case SkColorChannel::kR:
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400267 // TODO: Chrome needs to be patched before this can be
268 // enforced.
269// if (!(kRed_SkColorTypeComponentFlag & componentFlags)) {
270// return false;
271// }
272 break;
273 case SkColorChannel::kG:
274 if (!(kGreen_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500275 return false;
276 }
277 break;
Jim Van Verth53275362018-11-09 15:42:35 -0500278 case SkColorChannel::kB:
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400279 if (!(kBlue_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500280 return false;
281 }
282 break;
283 case SkColorChannel::kA:
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400284 if (!(kAlpha_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500285 return false;
286 }
287 break;
288 }
289 }
290 }
Jim Van Verth0e671942018-11-09 12:03:57 -0500291 }
292
293 return true;
294}
295
296bool SkImage_GpuBase::RenderYUVAToRGBA(GrContext* ctx, GrRenderTargetContext* renderTargetContext,
297 const SkRect& rect, SkYUVColorSpace yuvColorSpace,
Brian Osmane9560492019-02-05 17:00:03 -0500298 sk_sp<GrColorSpaceXform> colorSpaceXform,
Jim Van Verth0e671942018-11-09 12:03:57 -0500299 const sk_sp<GrTextureProxy> proxies[4],
300 const SkYUVAIndex yuvaIndices[4]) {
301 SkASSERT(renderTargetContext);
302 if (!renderTargetContext->asSurfaceProxy()) {
303 return false;
304 }
305
306 GrPaint paint;
307 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
308
Brian Osmane9560492019-02-05 17:00:03 -0500309 auto fp = GrYUVtoRGBEffect::Make(proxies, yuvaIndices, yuvColorSpace,
310 GrSamplerState::Filter::kNearest);
311 if (colorSpaceXform) {
312 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(colorSpaceXform));
313 }
314 paint.addColorFragmentProcessor(std::move(fp));
Jim Van Verth0e671942018-11-09 12:03:57 -0500315
316 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Jim Van Verth0e671942018-11-09 12:03:57 -0500317 return true;
318}
319
Brian Salomonbe5a0932018-12-10 10:03:26 -0500320sk_sp<GrTextureProxy> SkImage_GpuBase::MakePromiseImageLazyProxy(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400321 GrContext* context, int width, int height, GrSurfaceOrigin origin, GrColorType colorType,
Brian Salomonbe5a0932018-12-10 10:03:26 -0500322 GrBackendFormat backendFormat, GrMipMapped mipMapped,
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500323 PromiseImageTextureFulfillProc fulfillProc,
324 PromiseImageTextureReleaseProc releaseProc,
325 PromiseImageTextureDoneProc doneProc,
Brian Salomon0cc57542019-03-08 13:28:46 -0500326 PromiseImageTextureContext textureContext,
327 PromiseImageApiVersion version) {
Brian Salomonbe5a0932018-12-10 10:03:26 -0500328 SkASSERT(context);
329 SkASSERT(width > 0 && height > 0);
330 SkASSERT(doneProc);
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400331 SkASSERT(colorType != GrColorType::kUnknown);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500332
333 if (!fulfillProc || !releaseProc) {
334 doneProc(textureContext);
335 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400336 }
337
Brian Salomonbe5a0932018-12-10 10:03:26 -0500338 if (mipMapped == GrMipMapped::kYes &&
339 GrTextureTypeHasRestrictedSampling(backendFormat.textureType())) {
340 // It is invalid to have a GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE and have mips as
341 // well.
342 doneProc(textureContext);
343 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400344 }
Brian Salomonbe5a0932018-12-10 10:03:26 -0500345
346 /**
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500347 * This class is the lazy instantiation callback for promise images. It manages calling the
348 * client's Fulfill, Release, and Done procs. It attempts to reuse a GrTexture instance in
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500349 * cases where the client provides the same SkPromiseImageTexture as Fulfill results for
350 * multiple SkImages. The created GrTexture is given a key based on a unique ID associated with
351 * the SkPromiseImageTexture.
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500352 *
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500353 * The GrTexutre idle proc mechanism is used to call the Release and Done procs. We use this
354 * instead of the GrSurface release proc because the GrTexture is cached and therefore may
355 * outlive the proxy into which this callback is installed.
356 *
357 * A key invalidation message is installed on the SkPromiseImageTexture so that the GrTexture
358 * is deleted once it can no longer be used to instantiate a proxy.
Brian Salomonbe5a0932018-12-10 10:03:26 -0500359 */
360 class PromiseLazyInstantiateCallback {
361 public:
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500362 PromiseLazyInstantiateCallback(PromiseImageTextureFulfillProc fulfillProc,
363 PromiseImageTextureReleaseProc releaseProc,
364 PromiseImageTextureDoneProc doneProc,
365 PromiseImageTextureContext context,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400366 GrColorType colorType,
Brian Salomon0cc57542019-03-08 13:28:46 -0500367 PromiseImageApiVersion version)
368 : fFulfillProc(fulfillProc)
369 , fReleaseProc(releaseProc)
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400370 , fColorType(colorType)
Brian Salomon0cc57542019-03-08 13:28:46 -0500371 , fVersion(version) {
Brian Salomone80b8092019-03-08 13:25:19 -0500372 fDoneCallback = sk_make_sp<GrRefCntedCallback>(doneProc, context);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500373 }
374 PromiseLazyInstantiateCallback(PromiseLazyInstantiateCallback&&) = default;
375 PromiseLazyInstantiateCallback(const PromiseLazyInstantiateCallback&) {
376 // Because we get wrapped in std::function we must be copyable. But we should never
377 // be copied.
378 SkASSERT(false);
379 }
380 PromiseLazyInstantiateCallback& operator=(PromiseLazyInstantiateCallback&&) = default;
381 PromiseLazyInstantiateCallback& operator=(const PromiseLazyInstantiateCallback&) {
382 SkASSERT(false);
383 return *this;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500384 }
Brian Salomon553610d2019-01-14 17:34:12 -0500385
Brian Salomon88b8d112019-03-07 15:25:34 +0000386 ~PromiseLazyInstantiateCallback() {
Brian Salomon876a0172019-03-08 11:12:14 -0500387 // Our destructor can run on any thread. We trigger the unref of fTexture by message.
388 if (fTexture) {
Brian Salomon876a0172019-03-08 11:12:14 -0500389 SkMessageBus<GrGpuResourceFreedMessage>::Post({fTexture, fTextureContextID});
390 }
Brian Salomon88b8d112019-03-07 15:25:34 +0000391 }
392
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400393 GrSurfaceProxy::LazyInstantiationResult operator()(GrResourceProvider* resourceProvider) {
394 // We use the unique key in a way that is unrelated to the SkImage-based key that the
395 // proxy may receive, hence kUnsynced.
396 static constexpr auto kKeySyncMode =
397 GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
398
Brian Salomon876a0172019-03-08 11:12:14 -0500399 // Our proxy is getting instantiated for the second+ time. We are only allowed to call
400 // Fulfill once. So return our cached result.
401 if (fTexture) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400402 return {sk_ref_sp(fTexture), kKeySyncMode};
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400403 } else if (fColorType == GrColorType::kUnknown) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400404 // We've already called fulfill and it failed. Our contract says that we should only
405 // call each callback once.
406 return {};
Brian Salomon876a0172019-03-08 11:12:14 -0500407 }
Brian Salomone80b8092019-03-08 13:25:19 -0500408 SkASSERT(fDoneCallback);
409 PromiseImageTextureContext textureContext = fDoneCallback->context();
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500410 sk_sp<SkPromiseImageTexture> promiseTexture = fFulfillProc(textureContext);
411 // From here on out our contract is that the release proc must be called, even if
412 // the return from fulfill was invalid or we fail for some other reason.
Brian Salomone80b8092019-03-08 13:25:19 -0500413 auto releaseCallback = sk_make_sp<GrRefCntedCallback>(fReleaseProc, textureContext);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500414 if (!promiseTexture) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400415 // This records that we have failed.
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400416 fColorType = GrColorType::kUnknown;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400417 return {};
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500418 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500419
Robert Phillips62221e72019-07-24 15:07:38 -0400420 const GrBackendTexture& backendTexture = promiseTexture->backendTexture();
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500421 if (!backendTexture.isValid()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400422 return {};
Brian Salomon559c6172019-01-10 10:23:44 -0500423 }
424
Brian Salomonf55e8d52019-01-30 17:28:20 -0500425 sk_sp<GrTexture> tex;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500426 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Salomon7d88f312019-02-28 10:03:03 -0500427 GrUniqueKey key;
428 GrUniqueKey::Builder builder(&key, kDomain, 2, "promise");
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500429 builder[0] = promiseTexture->uniqueID();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400430 builder[1] = (uint32_t) fColorType;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500431 builder.finish();
Brian Salomon0d606762019-01-25 09:58:38 -0500432 // A texture with this key may already exist from a different instance of this lazy
433 // callback. This could happen if the client fulfills a promise image with a texture
434 // that was previously used to fulfill a different promise image.
Brian Salomon7d88f312019-02-28 10:03:03 -0500435 if (auto surf = resourceProvider->findByUniqueKey<GrSurface>(key)) {
Brian Salomon0d606762019-01-25 09:58:38 -0500436 tex = sk_ref_sp(surf->asTexture());
437 SkASSERT(tex);
438 } else {
439 if ((tex = resourceProvider->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400440 backendTexture, fColorType, kBorrow_GrWrapOwnership,
441 GrWrapCacheable::kYes, kRead_GrIOType))) {
Brian Salomon7d88f312019-02-28 10:03:03 -0500442 tex->resourcePriv().setUniqueKey(key);
Brian Salomon0d606762019-01-25 09:58:38 -0500443 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400444 return {};
Brian Salomon0d606762019-01-25 09:58:38 -0500445 }
446 }
Brian Salomon0cc57542019-03-08 13:28:46 -0500447 auto releaseIdleState = fVersion == PromiseImageApiVersion::kLegacy
448 ? GrTexture::IdleState::kFinished
449 : GrTexture::IdleState::kFlushed;
450 tex->addIdleProc(std::move(releaseCallback), releaseIdleState);
Brian Salomone80b8092019-03-08 13:25:19 -0500451 tex->addIdleProc(std::move(fDoneCallback), GrTexture::IdleState::kFinished);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500452 promiseTexture->addKeyToInvalidate(tex->getContext()->priv().contextID(), key);
Brian Salomon876a0172019-03-08 11:12:14 -0500453 fTexture = tex.get();
454 // We need to hold on to the GrTexture in case our proxy gets reinstantiated. However,
455 // we can't unref in our destructor because we may be on another thread then. So we
456 // let the cache know it is waiting on an unref message. We will send that message from
457 // our destructor.
458 GrContext* context = fTexture->getContext();
459 context->priv().getResourceCache()->insertDelayedResourceUnref(fTexture);
460 fTextureContextID = context->priv().contextID();
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400461 return {std::move(tex), kKeySyncMode};
Brian Salomonbe5a0932018-12-10 10:03:26 -0500462 }
463
464 private:
Brian Salomone80b8092019-03-08 13:25:19 -0500465 PromiseImageTextureFulfillProc fFulfillProc;
466 PromiseImageTextureReleaseProc fReleaseProc;
467 sk_sp<GrRefCntedCallback> fDoneCallback;
Brian Salomon876a0172019-03-08 11:12:14 -0500468 GrTexture* fTexture = nullptr;
469 uint32_t fTextureContextID = SK_InvalidUniqueID;
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400470 GrColorType fColorType;
Brian Salomon0cc57542019-03-08 13:28:46 -0500471 PromiseImageApiVersion fVersion;
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400472 } callback(fulfillProc, releaseProc, doneProc, textureContext, colorType, version);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500473
Robert Phillips9da87e02019-02-04 13:26:26 -0500474 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonbe5a0932018-12-10 10:03:26 -0500475
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400476 GrPixelConfig config = context->priv().caps()->getConfigFromBackendFormat(
477 backendFormat,
478 colorType);
479
Brian Salomonbe5a0932018-12-10 10:03:26 -0500480 GrSurfaceDesc desc;
481 desc.fWidth = width;
482 desc.fHeight = height;
483 desc.fConfig = config;
484
485 // We pass kReadOnly here since we should treat content of the client's texture as immutable.
Brian Salomone8a766b2019-07-19 14:24:36 -0400486 // The promise API provides no way for the client to indicated that the texture is protected.
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400487 return proxyProvider->createLazyProxy(
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400488 std::move(callback), backendFormat, desc, GrRenderable::kNo, 1, origin, mipMapped,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400489 GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, SkBudgeted::kNo,
Brian Salomone8a766b2019-07-19 14:24:36 -0400490 GrProtected::kNo, GrSurfaceProxy::LazyInstantiationType::kDeinstantiate);
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400491}