blob: 22995917fc2d139175f834b9885e7ed57dbeb607 [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
Jim Van Verth8026ccc2018-10-04 13:10:39 -040041bool SkImage_GpuBase::ValidateBackendTexture(GrContext* ctx, const GrBackendTexture& tex,
42 GrPixelConfig* config, SkColorType ct, SkAlphaType at,
43 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 }
Robert Phillips9da87e02019-02-04 13:26:26 -050057 *config = ctx->priv().caps()->getConfigFromBackendFormat(backendFormat, ct);
Brian Salomonf391d0f2018-12-14 09:18:50 -050058 return *config != kUnknown_GrPixelConfig;
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();
Jim Van Verth0e671942018-11-09 12:03:57 -0500239
240 // We need to make a copy of the input backend textures because we need to preserve the result
241 // of validate_backend_texture.
242 GrBackendTexture yuvaTexturesCopy[4];
243 for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) {
244 yuvaTexturesCopy[textureIndex] = yuvaTextures[textureIndex];
Brian Salomonf391d0f2018-12-14 09:18:50 -0500245 GrBackendFormat backendFormat = yuvaTexturesCopy[textureIndex].getBackendFormat();
246 if (!backendFormat.isValid()) {
247 return false;
248 }
249 yuvaTexturesCopy[textureIndex].fConfig =
Robert Phillips9da87e02019-02-04 13:26:26 -0500250 ctx->priv().caps()->getYUVAConfigFromBackendFormat(backendFormat);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500251 if (yuvaTexturesCopy[textureIndex].fConfig == kUnknown_GrPixelConfig) {
Jim Van Verth0e671942018-11-09 12:03:57 -0500252 return false;
253 }
254 SkASSERT(yuvaTexturesCopy[textureIndex].isValid());
255
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500256 tempTextureProxies[textureIndex] = proxyProvider->wrapBackendTexture(
257 yuvaTexturesCopy[textureIndex], imageOrigin, kBorrow_GrWrapOwnership,
258 GrWrapCacheable::kNo, kRead_GrIOType);
Jim Van Verth0e671942018-11-09 12:03:57 -0500259 if (!tempTextureProxies[textureIndex]) {
260 return false;
261 }
Jim Van Verth53275362018-11-09 15:42:35 -0500262
263 // Check that each texture contains the channel data for the corresponding YUVA index
264 GrPixelConfig config = yuvaTexturesCopy[textureIndex].fConfig;
265 for (int yuvaIndex = 0; yuvaIndex < SkYUVAIndex::kIndexCount; ++yuvaIndex) {
266 if (yuvaIndices[yuvaIndex].fIndex == textureIndex) {
267 switch (yuvaIndices[yuvaIndex].fChannel) {
268 case SkColorChannel::kR:
269 if (kAlpha_8_as_Alpha_GrPixelConfig == config) {
270 return false;
271 }
272 break;
273 case SkColorChannel::kG:
274 case SkColorChannel::kB:
275 if (kAlpha_8_as_Alpha_GrPixelConfig == config ||
276 kAlpha_8_as_Red_GrPixelConfig == config) {
277 return false;
278 }
279 break;
280 case SkColorChannel::kA:
281 default:
282 if (kRGB_888_GrPixelConfig == config) {
283 return false;
284 }
285 break;
286 }
287 }
288 }
Jim Van Verth0e671942018-11-09 12:03:57 -0500289 }
290
291 return true;
292}
293
294bool SkImage_GpuBase::RenderYUVAToRGBA(GrContext* ctx, GrRenderTargetContext* renderTargetContext,
295 const SkRect& rect, SkYUVColorSpace yuvColorSpace,
Brian Osmane9560492019-02-05 17:00:03 -0500296 sk_sp<GrColorSpaceXform> colorSpaceXform,
Jim Van Verth0e671942018-11-09 12:03:57 -0500297 const sk_sp<GrTextureProxy> proxies[4],
298 const SkYUVAIndex yuvaIndices[4]) {
299 SkASSERT(renderTargetContext);
300 if (!renderTargetContext->asSurfaceProxy()) {
301 return false;
302 }
303
304 GrPaint paint;
305 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
306
Brian Osmane9560492019-02-05 17:00:03 -0500307 auto fp = GrYUVtoRGBEffect::Make(proxies, yuvaIndices, yuvColorSpace,
308 GrSamplerState::Filter::kNearest);
309 if (colorSpaceXform) {
310 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(colorSpaceXform));
311 }
312 paint.addColorFragmentProcessor(std::move(fp));
Jim Van Verth0e671942018-11-09 12:03:57 -0500313
314 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Jim Van Verth0e671942018-11-09 12:03:57 -0500315 return true;
316}
317
Brian Salomonbe5a0932018-12-10 10:03:26 -0500318sk_sp<GrTextureProxy> SkImage_GpuBase::MakePromiseImageLazyProxy(
319 GrContext* context, int width, int height, GrSurfaceOrigin origin, GrPixelConfig config,
320 GrBackendFormat backendFormat, GrMipMapped mipMapped,
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500321 PromiseImageTextureFulfillProc fulfillProc,
322 PromiseImageTextureReleaseProc releaseProc,
323 PromiseImageTextureDoneProc doneProc,
Brian Salomon0cc57542019-03-08 13:28:46 -0500324 PromiseImageTextureContext textureContext,
325 PromiseImageApiVersion version) {
Brian Salomonbe5a0932018-12-10 10:03:26 -0500326 SkASSERT(context);
327 SkASSERT(width > 0 && height > 0);
328 SkASSERT(doneProc);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500329 SkASSERT(config != kUnknown_GrPixelConfig);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500330
331 if (!fulfillProc || !releaseProc) {
332 doneProc(textureContext);
333 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400334 }
335
Brian Salomonbe5a0932018-12-10 10:03:26 -0500336 if (mipMapped == GrMipMapped::kYes &&
337 GrTextureTypeHasRestrictedSampling(backendFormat.textureType())) {
338 // It is invalid to have a GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE and have mips as
339 // well.
340 doneProc(textureContext);
341 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400342 }
Brian Salomonbe5a0932018-12-10 10:03:26 -0500343
344 /**
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500345 * This class is the lazy instantiation callback for promise images. It manages calling the
346 * client's Fulfill, Release, and Done procs. It attempts to reuse a GrTexture instance in
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500347 * cases where the client provides the same SkPromiseImageTexture as Fulfill results for
348 * multiple SkImages. The created GrTexture is given a key based on a unique ID associated with
349 * the SkPromiseImageTexture.
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500350 *
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500351 * The GrTexutre idle proc mechanism is used to call the Release and Done procs. We use this
352 * instead of the GrSurface release proc because the GrTexture is cached and therefore may
353 * outlive the proxy into which this callback is installed.
354 *
355 * A key invalidation message is installed on the SkPromiseImageTexture so that the GrTexture
356 * is deleted once it can no longer be used to instantiate a proxy.
Brian Salomonbe5a0932018-12-10 10:03:26 -0500357 */
358 class PromiseLazyInstantiateCallback {
359 public:
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500360 PromiseLazyInstantiateCallback(PromiseImageTextureFulfillProc fulfillProc,
361 PromiseImageTextureReleaseProc releaseProc,
362 PromiseImageTextureDoneProc doneProc,
363 PromiseImageTextureContext context,
Brian Salomon0cc57542019-03-08 13:28:46 -0500364 GrPixelConfig config,
365 PromiseImageApiVersion version)
366 : fFulfillProc(fulfillProc)
367 , fReleaseProc(releaseProc)
368 , fConfig(config)
369 , fVersion(version) {
Brian Salomone80b8092019-03-08 13:25:19 -0500370 fDoneCallback = sk_make_sp<GrRefCntedCallback>(doneProc, context);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500371 }
372 PromiseLazyInstantiateCallback(PromiseLazyInstantiateCallback&&) = default;
373 PromiseLazyInstantiateCallback(const PromiseLazyInstantiateCallback&) {
374 // Because we get wrapped in std::function we must be copyable. But we should never
375 // be copied.
376 SkASSERT(false);
377 }
378 PromiseLazyInstantiateCallback& operator=(PromiseLazyInstantiateCallback&&) = default;
379 PromiseLazyInstantiateCallback& operator=(const PromiseLazyInstantiateCallback&) {
380 SkASSERT(false);
381 return *this;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500382 }
Brian Salomon553610d2019-01-14 17:34:12 -0500383
Brian Salomon88b8d112019-03-07 15:25:34 +0000384 ~PromiseLazyInstantiateCallback() {
Brian Salomon876a0172019-03-08 11:12:14 -0500385 // Our destructor can run on any thread. We trigger the unref of fTexture by message.
386 if (fTexture) {
Brian Salomon876a0172019-03-08 11:12:14 -0500387 SkMessageBus<GrGpuResourceFreedMessage>::Post({fTexture, fTextureContextID});
388 }
Brian Salomon88b8d112019-03-07 15:25:34 +0000389 }
390
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400391 GrSurfaceProxy::LazyInstantiationResult operator()(GrResourceProvider* resourceProvider) {
392 // We use the unique key in a way that is unrelated to the SkImage-based key that the
393 // proxy may receive, hence kUnsynced.
394 static constexpr auto kKeySyncMode =
395 GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
396
Brian Salomon876a0172019-03-08 11:12:14 -0500397 // Our proxy is getting instantiated for the second+ time. We are only allowed to call
398 // Fulfill once. So return our cached result.
399 if (fTexture) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400400 return {sk_ref_sp(fTexture), kKeySyncMode};
Brian Salomond538d3d2019-04-04 12:18:17 -0400401 } else if (fConfig == kUnknown_GrPixelConfig) {
402 // We've already called fulfill and it failed. Our contract says that we should only
403 // call each callback once.
404 return {};
Brian Salomon876a0172019-03-08 11:12:14 -0500405 }
Brian Salomone80b8092019-03-08 13:25:19 -0500406 SkASSERT(fDoneCallback);
407 PromiseImageTextureContext textureContext = fDoneCallback->context();
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500408 sk_sp<SkPromiseImageTexture> promiseTexture = fFulfillProc(textureContext);
409 // From here on out our contract is that the release proc must be called, even if
410 // the return from fulfill was invalid or we fail for some other reason.
Brian Salomone80b8092019-03-08 13:25:19 -0500411 auto releaseCallback = sk_make_sp<GrRefCntedCallback>(fReleaseProc, textureContext);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500412 if (!promiseTexture) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400413 // This records that we have failed.
414 fConfig = kUnknown_GrPixelConfig;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400415 return {};
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500416 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500417
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500418 auto backendTexture = promiseTexture->backendTexture();
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500419 backendTexture.fConfig = fConfig;
420 if (!backendTexture.isValid()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400421 return {};
Brian Salomon559c6172019-01-10 10:23:44 -0500422 }
423
Brian Salomonf55e8d52019-01-30 17:28:20 -0500424 sk_sp<GrTexture> tex;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500425 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Salomon7d88f312019-02-28 10:03:03 -0500426 GrUniqueKey key;
427 GrUniqueKey::Builder builder(&key, kDomain, 2, "promise");
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500428 builder[0] = promiseTexture->uniqueID();
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500429 builder[1] = fConfig;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500430 builder.finish();
Brian Salomon0d606762019-01-25 09:58:38 -0500431 // A texture with this key may already exist from a different instance of this lazy
432 // callback. This could happen if the client fulfills a promise image with a texture
433 // that was previously used to fulfill a different promise image.
Brian Salomon7d88f312019-02-28 10:03:03 -0500434 if (auto surf = resourceProvider->findByUniqueKey<GrSurface>(key)) {
Brian Salomon0d606762019-01-25 09:58:38 -0500435 tex = sk_ref_sp(surf->asTexture());
436 SkASSERT(tex);
437 } else {
438 if ((tex = resourceProvider->wrapBackendTexture(
439 backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kYes,
440 kRead_GrIOType))) {
Brian Salomon7d88f312019-02-28 10:03:03 -0500441 tex->resourcePriv().setUniqueKey(key);
Brian Salomon0d606762019-01-25 09:58:38 -0500442 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400443 return {};
Brian Salomon0d606762019-01-25 09:58:38 -0500444 }
445 }
Brian Salomon0cc57542019-03-08 13:28:46 -0500446 auto releaseIdleState = fVersion == PromiseImageApiVersion::kLegacy
447 ? GrTexture::IdleState::kFinished
448 : GrTexture::IdleState::kFlushed;
449 tex->addIdleProc(std::move(releaseCallback), releaseIdleState);
Brian Salomone80b8092019-03-08 13:25:19 -0500450 tex->addIdleProc(std::move(fDoneCallback), GrTexture::IdleState::kFinished);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500451 promiseTexture->addKeyToInvalidate(tex->getContext()->priv().contextID(), key);
Brian Salomon876a0172019-03-08 11:12:14 -0500452 fTexture = tex.get();
453 // We need to hold on to the GrTexture in case our proxy gets reinstantiated. However,
454 // we can't unref in our destructor because we may be on another thread then. So we
455 // let the cache know it is waiting on an unref message. We will send that message from
456 // our destructor.
457 GrContext* context = fTexture->getContext();
458 context->priv().getResourceCache()->insertDelayedResourceUnref(fTexture);
459 fTextureContextID = context->priv().contextID();
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400460 return {std::move(tex), kKeySyncMode};
Brian Salomonbe5a0932018-12-10 10:03:26 -0500461 }
462
463 private:
Brian Salomone80b8092019-03-08 13:25:19 -0500464 PromiseImageTextureFulfillProc fFulfillProc;
465 PromiseImageTextureReleaseProc fReleaseProc;
466 sk_sp<GrRefCntedCallback> fDoneCallback;
Brian Salomon876a0172019-03-08 11:12:14 -0500467 GrTexture* fTexture = nullptr;
468 uint32_t fTextureContextID = SK_InvalidUniqueID;
Brian Salomon553610d2019-01-14 17:34:12 -0500469 GrPixelConfig fConfig;
Brian Salomon0cc57542019-03-08 13:28:46 -0500470 PromiseImageApiVersion fVersion;
471 } callback(fulfillProc, releaseProc, doneProc, textureContext, config, version);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500472
Robert Phillips9da87e02019-02-04 13:26:26 -0500473 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonbe5a0932018-12-10 10:03:26 -0500474
475 GrSurfaceDesc desc;
476 desc.fWidth = width;
477 desc.fHeight = height;
478 desc.fConfig = config;
479
480 // We pass kReadOnly here since we should treat content of the client's texture as immutable.
481 return proxyProvider->createLazyProxy(std::move(callback), backendFormat, desc, origin,
482 mipMapped, GrInternalSurfaceFlags::kReadOnly,
483 SkBackingFit::kExact, SkBudgeted::kNo,
Brian Salomon876a0172019-03-08 11:12:14 -0500484 GrSurfaceProxy::LazyInstantiationType::kDeinstantiate);
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400485}