blob: c5434468870e1bad3f68f6c589a90c56f59c4bef [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 }
Greg Daniele877dce2019-07-11 10:52:43 -040057
58 GrColorType grCT = SkColorTypeToGrColorType(ct);
59 // Until we support SRGB in the SkColorType we have to do this manual check here to make sure
60 // we use the correct GrColorType.
61 if (ctx->priv().caps()->isFormatSRGB(backendFormat)) {
62 if (grCT != GrColorType::kRGBA_8888) {
63 return false;
64 }
65 grCT = GrColorType::kRGBA_8888_SRGB;
66 }
67
68 *config = ctx->priv().caps()->getConfigFromBackendFormat(backendFormat, grCT);
Brian Salomonf391d0f2018-12-14 09:18:50 -050069 return *config != kUnknown_GrPixelConfig;
Jim Van Verth8026ccc2018-10-04 13:10:39 -040070}
71
72//////////////////////////////////////////////////////////////////////////////////////////////////
73
Brian Osmane50cdf02018-10-19 13:02:14 -040074bool SkImage_GpuBase::getROPixels(SkBitmap* dst, CachingHint chint) const {
Robert Phillips920d4882019-03-04 15:16:44 -050075 auto direct = fContext->priv().asDirectContext();
76 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040077 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
78 return false;
79 }
80
Jim Van Verth8026ccc2018-10-04 13:10:39 -040081 const auto desc = SkBitmapCacheDesc::Make(this);
82 if (SkBitmapCache::Find(desc, dst)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040083 SkASSERT(dst->isImmutable());
84 SkASSERT(dst->getPixels());
85 return true;
86 }
87
88 SkBitmapCache::RecPtr rec = nullptr;
89 SkPixmap pmap;
90 if (kAllow_CachingHint == chint) {
Brian Salomon5ad6fd32019-03-21 15:30:08 -040091 rec = SkBitmapCache::Alloc(desc, this->imageInfo(), &pmap);
Jim Van Verth8026ccc2018-10-04 13:10:39 -040092 if (!rec) {
93 return false;
94 }
95 } else {
Brian Salomon5ad6fd32019-03-21 15:30:08 -040096 if (!dst->tryAllocPixels(this->imageInfo()) || !dst->peekPixels(&pmap)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040097 return false;
98 }
99 }
100
Brian Salomond6287472019-06-24 15:50:07 -0400101 sk_sp<GrSurfaceContext> sContext =
102 direct->priv().makeWrappedSurfaceContext(this->asTextureProxyRef(direct),
103 SkColorTypeToGrColorType(this->colorType()),
104 this->alphaType(),
105 this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400106 if (!sContext) {
107 return false;
108 }
109
Brian Salomon1d435302019-07-01 13:05:28 -0400110 if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), {0, 0})) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400111 return false;
112 }
113
114 if (rec) {
115 SkBitmapCache::Add(std::move(rec), dst);
116 this->notifyAddedToRasterCache();
117 }
118 return true;
119}
120
Robert Phillips6603a172019-03-05 12:35:44 -0500121sk_sp<SkImage> SkImage_GpuBase::onMakeSubset(GrRecordingContext* context,
122 const SkIRect& subset) const {
123 if (!context || !fContext->priv().matches(context)) {
124 return nullptr;
125 }
126
127 sk_sp<GrSurfaceProxy> proxy = this->asTextureProxyRef(context);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400128
Greg Daniel46cfbc62019-06-07 11:43:30 -0400129 sk_sp<GrTextureProxy> copyProxy = GrSurfaceProxy::Copy(
130 context, proxy.get(), GrMipMapped::kNo, subset, SkBackingFit::kExact,
131 proxy->isBudgeted());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400132
Greg Daniel46cfbc62019-06-07 11:43:30 -0400133 if (!copyProxy) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400134 return nullptr;
135 }
136
137 // MDB: this call is okay bc we know 'sContext' was kExact
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400138 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, this->alphaType(),
Greg Daniel46cfbc62019-06-07 11:43:30 -0400139 std::move(copyProxy), this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400140}
141
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400142bool SkImage_GpuBase::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
143 int srcX, int srcY, CachingHint) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500144 auto direct = fContext->priv().asDirectContext();
145 if (!direct) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400146 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
147 return false;
148 }
149
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400150 if (!SkImageInfoValidConversion(dstInfo, this->imageInfo())) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400151 return false;
152 }
153
Robert Phillips920d4882019-03-04 15:16:44 -0500154 sk_sp<GrSurfaceContext> sContext = direct->priv().makeWrappedSurfaceContext(
Brian Salomond6287472019-06-24 15:50:07 -0400155 this->asTextureProxyRef(direct), SkColorTypeToGrColorType(this->colorType()),
156 this->alphaType(), this->refColorSpace());
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400157 if (!sContext) {
158 return false;
159 }
160
Brian Salomon1d435302019-07-01 13:05:28 -0400161 return sContext->readPixels(dstInfo, dstPixels, dstRB, {srcX, srcY});
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400162}
163
Robert Phillips9338c602019-02-19 12:52:29 -0500164sk_sp<GrTextureProxy> SkImage_GpuBase::asTextureProxyRef(GrRecordingContext* context,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400165 const GrSamplerState& params,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400166 SkScalar scaleAdjust[2]) const {
Robert Phillips6603a172019-03-05 12:35:44 -0500167 if (!context || !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400168 SkASSERT(0);
169 return nullptr;
170 }
171
Brian Salomond6287472019-06-24 15:50:07 -0400172 GrTextureAdjuster adjuster(fContext.get(), this->asTextureProxyRef(context),
173 SkColorTypeToGrColorType(this->colorType()), this->alphaType(),
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400174 this->uniqueID(), this->colorSpace());
Brian Osman6064e1c2018-10-19 14:27:54 -0400175 return adjuster.refTextureProxyForParams(params, scaleAdjust);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400176}
177
178GrBackendTexture SkImage_GpuBase::onGetBackendTexture(bool flushPendingGrContextIO,
179 GrSurfaceOrigin* origin) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500180 auto direct = fContext->priv().asDirectContext();
181 if (!direct) {
182 // This image was created with a DDL context and cannot be instantiated.
Robert Phillips6603a172019-03-05 12:35:44 -0500183 return GrBackendTexture(); // invalid
Robert Phillips920d4882019-03-04 15:16:44 -0500184 }
185
Robert Phillips6603a172019-03-05 12:35:44 -0500186 sk_sp<GrTextureProxy> proxy = this->asTextureProxyRef(direct);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400187 SkASSERT(proxy);
188
Robert Phillips920d4882019-03-04 15:16:44 -0500189 if (!proxy->isInstantiated()) {
190 auto resourceProvider = direct->priv().resourceProvider();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400191
Robert Phillips920d4882019-03-04 15:16:44 -0500192 if (!proxy->instantiate(resourceProvider)) {
193 return GrBackendTexture(); // invalid
194 }
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400195 }
196
197 GrTexture* texture = proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400198 if (texture) {
199 if (flushPendingGrContextIO) {
Greg Daniel4aa13e72019-04-15 14:42:20 -0400200 direct->priv().flushSurface(proxy.get());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400201 }
202 if (origin) {
203 *origin = proxy->origin();
204 }
205 return texture->getBackendTexture();
206 }
207 return GrBackendTexture(); // invalid
208}
209
210GrTexture* SkImage_GpuBase::onGetTexture() const {
211 GrTextureProxy* proxy = this->peekProxy();
Robert Phillips193c4212019-03-04 12:18:53 -0500212 if (proxy && proxy->isInstantiated()) {
213 return proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400214 }
215
Robert Phillips193c4212019-03-04 12:18:53 -0500216 auto direct = fContext->priv().asDirectContext();
217 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400218 // This image was created with a DDL context and cannot be instantiated.
219 return nullptr;
220 }
221
Robert Phillips6603a172019-03-05 12:35:44 -0500222 sk_sp<GrTextureProxy> proxyRef = this->asTextureProxyRef(direct);
Robert Phillips193c4212019-03-04 12:18:53 -0500223 SkASSERT(proxyRef && !proxyRef->isInstantiated());
224
225 if (!proxyRef->instantiate(direct->priv().resourceProvider())) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400226 return nullptr;
227 }
228
Robert Phillips193c4212019-03-04 12:18:53 -0500229 return proxyRef->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400230}
231
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400232bool SkImage_GpuBase::onIsValid(GrContext* context) const {
233 // The base class has already checked that context isn't abandoned (if it's not nullptr)
Robert Phillips920d4882019-03-04 15:16:44 -0500234 if (fContext->priv().abandoned()) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400235 return false;
236 }
237
Robert Phillips920d4882019-03-04 15:16:44 -0500238 if (context && !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400239 return false;
240 }
241
242 return true;
243}
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400244
Jim Van Verth0e671942018-11-09 12:03:57 -0500245bool SkImage_GpuBase::MakeTempTextureProxies(GrContext* ctx, const GrBackendTexture yuvaTextures[],
Jim Van Verth53275362018-11-09 15:42:35 -0500246 int numTextures, const SkYUVAIndex yuvaIndices[4],
247 GrSurfaceOrigin imageOrigin,
Jim Van Verth0e671942018-11-09 12:03:57 -0500248 sk_sp<GrTextureProxy> tempTextureProxies[4]) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500249 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Jim Van Verth0e671942018-11-09 12:03:57 -0500250
251 // We need to make a copy of the input backend textures because we need to preserve the result
252 // of validate_backend_texture.
253 GrBackendTexture yuvaTexturesCopy[4];
254 for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) {
255 yuvaTexturesCopy[textureIndex] = yuvaTextures[textureIndex];
Brian Salomonf391d0f2018-12-14 09:18:50 -0500256 GrBackendFormat backendFormat = yuvaTexturesCopy[textureIndex].getBackendFormat();
257 if (!backendFormat.isValid()) {
258 return false;
259 }
260 yuvaTexturesCopy[textureIndex].fConfig =
Robert Phillips9da87e02019-02-04 13:26:26 -0500261 ctx->priv().caps()->getYUVAConfigFromBackendFormat(backendFormat);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500262 if (yuvaTexturesCopy[textureIndex].fConfig == kUnknown_GrPixelConfig) {
Jim Van Verth0e671942018-11-09 12:03:57 -0500263 return false;
264 }
265 SkASSERT(yuvaTexturesCopy[textureIndex].isValid());
266
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500267 tempTextureProxies[textureIndex] = proxyProvider->wrapBackendTexture(
268 yuvaTexturesCopy[textureIndex], imageOrigin, kBorrow_GrWrapOwnership,
269 GrWrapCacheable::kNo, kRead_GrIOType);
Jim Van Verth0e671942018-11-09 12:03:57 -0500270 if (!tempTextureProxies[textureIndex]) {
271 return false;
272 }
Jim Van Verth53275362018-11-09 15:42:35 -0500273
274 // Check that each texture contains the channel data for the corresponding YUVA index
275 GrPixelConfig config = yuvaTexturesCopy[textureIndex].fConfig;
276 for (int yuvaIndex = 0; yuvaIndex < SkYUVAIndex::kIndexCount; ++yuvaIndex) {
277 if (yuvaIndices[yuvaIndex].fIndex == textureIndex) {
278 switch (yuvaIndices[yuvaIndex].fChannel) {
279 case SkColorChannel::kR:
280 if (kAlpha_8_as_Alpha_GrPixelConfig == config) {
281 return false;
282 }
283 break;
284 case SkColorChannel::kG:
285 case SkColorChannel::kB:
286 if (kAlpha_8_as_Alpha_GrPixelConfig == config ||
287 kAlpha_8_as_Red_GrPixelConfig == config) {
288 return false;
289 }
290 break;
291 case SkColorChannel::kA:
292 default:
293 if (kRGB_888_GrPixelConfig == config) {
294 return false;
295 }
296 break;
297 }
298 }
299 }
Jim Van Verth0e671942018-11-09 12:03:57 -0500300 }
301
302 return true;
303}
304
305bool SkImage_GpuBase::RenderYUVAToRGBA(GrContext* ctx, GrRenderTargetContext* renderTargetContext,
306 const SkRect& rect, SkYUVColorSpace yuvColorSpace,
Brian Osmane9560492019-02-05 17:00:03 -0500307 sk_sp<GrColorSpaceXform> colorSpaceXform,
Jim Van Verth0e671942018-11-09 12:03:57 -0500308 const sk_sp<GrTextureProxy> proxies[4],
309 const SkYUVAIndex yuvaIndices[4]) {
310 SkASSERT(renderTargetContext);
311 if (!renderTargetContext->asSurfaceProxy()) {
312 return false;
313 }
314
315 GrPaint paint;
316 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
317
Brian Osmane9560492019-02-05 17:00:03 -0500318 auto fp = GrYUVtoRGBEffect::Make(proxies, yuvaIndices, yuvColorSpace,
319 GrSamplerState::Filter::kNearest);
320 if (colorSpaceXform) {
321 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(colorSpaceXform));
322 }
323 paint.addColorFragmentProcessor(std::move(fp));
Jim Van Verth0e671942018-11-09 12:03:57 -0500324
325 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Jim Van Verth0e671942018-11-09 12:03:57 -0500326 return true;
327}
328
Brian Salomonbe5a0932018-12-10 10:03:26 -0500329sk_sp<GrTextureProxy> SkImage_GpuBase::MakePromiseImageLazyProxy(
330 GrContext* context, int width, int height, GrSurfaceOrigin origin, GrPixelConfig config,
331 GrBackendFormat backendFormat, GrMipMapped mipMapped,
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500332 PromiseImageTextureFulfillProc fulfillProc,
333 PromiseImageTextureReleaseProc releaseProc,
334 PromiseImageTextureDoneProc doneProc,
Brian Salomon0cc57542019-03-08 13:28:46 -0500335 PromiseImageTextureContext textureContext,
336 PromiseImageApiVersion version) {
Brian Salomonbe5a0932018-12-10 10:03:26 -0500337 SkASSERT(context);
338 SkASSERT(width > 0 && height > 0);
339 SkASSERT(doneProc);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500340 SkASSERT(config != kUnknown_GrPixelConfig);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500341
342 if (!fulfillProc || !releaseProc) {
343 doneProc(textureContext);
344 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400345 }
346
Brian Salomonbe5a0932018-12-10 10:03:26 -0500347 if (mipMapped == GrMipMapped::kYes &&
348 GrTextureTypeHasRestrictedSampling(backendFormat.textureType())) {
349 // It is invalid to have a GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE and have mips as
350 // well.
351 doneProc(textureContext);
352 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400353 }
Brian Salomonbe5a0932018-12-10 10:03:26 -0500354
355 /**
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500356 * This class is the lazy instantiation callback for promise images. It manages calling the
357 * client's Fulfill, Release, and Done procs. It attempts to reuse a GrTexture instance in
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500358 * cases where the client provides the same SkPromiseImageTexture as Fulfill results for
359 * multiple SkImages. The created GrTexture is given a key based on a unique ID associated with
360 * the SkPromiseImageTexture.
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500361 *
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500362 * The GrTexutre idle proc mechanism is used to call the Release and Done procs. We use this
363 * instead of the GrSurface release proc because the GrTexture is cached and therefore may
364 * outlive the proxy into which this callback is installed.
365 *
366 * A key invalidation message is installed on the SkPromiseImageTexture so that the GrTexture
367 * is deleted once it can no longer be used to instantiate a proxy.
Brian Salomonbe5a0932018-12-10 10:03:26 -0500368 */
369 class PromiseLazyInstantiateCallback {
370 public:
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500371 PromiseLazyInstantiateCallback(PromiseImageTextureFulfillProc fulfillProc,
372 PromiseImageTextureReleaseProc releaseProc,
373 PromiseImageTextureDoneProc doneProc,
374 PromiseImageTextureContext context,
Brian Salomon0cc57542019-03-08 13:28:46 -0500375 GrPixelConfig config,
376 PromiseImageApiVersion version)
377 : fFulfillProc(fulfillProc)
378 , fReleaseProc(releaseProc)
379 , fConfig(config)
380 , fVersion(version) {
Brian Salomone80b8092019-03-08 13:25:19 -0500381 fDoneCallback = sk_make_sp<GrRefCntedCallback>(doneProc, context);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500382 }
383 PromiseLazyInstantiateCallback(PromiseLazyInstantiateCallback&&) = default;
384 PromiseLazyInstantiateCallback(const PromiseLazyInstantiateCallback&) {
385 // Because we get wrapped in std::function we must be copyable. But we should never
386 // be copied.
387 SkASSERT(false);
388 }
389 PromiseLazyInstantiateCallback& operator=(PromiseLazyInstantiateCallback&&) = default;
390 PromiseLazyInstantiateCallback& operator=(const PromiseLazyInstantiateCallback&) {
391 SkASSERT(false);
392 return *this;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500393 }
Brian Salomon553610d2019-01-14 17:34:12 -0500394
Brian Salomon88b8d112019-03-07 15:25:34 +0000395 ~PromiseLazyInstantiateCallback() {
Brian Salomon876a0172019-03-08 11:12:14 -0500396 // Our destructor can run on any thread. We trigger the unref of fTexture by message.
397 if (fTexture) {
Brian Salomon876a0172019-03-08 11:12:14 -0500398 SkMessageBus<GrGpuResourceFreedMessage>::Post({fTexture, fTextureContextID});
399 }
Brian Salomon88b8d112019-03-07 15:25:34 +0000400 }
401
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400402 GrSurfaceProxy::LazyInstantiationResult operator()(GrResourceProvider* resourceProvider) {
403 // We use the unique key in a way that is unrelated to the SkImage-based key that the
404 // proxy may receive, hence kUnsynced.
405 static constexpr auto kKeySyncMode =
406 GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
407
Brian Salomon876a0172019-03-08 11:12:14 -0500408 // Our proxy is getting instantiated for the second+ time. We are only allowed to call
409 // Fulfill once. So return our cached result.
410 if (fTexture) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400411 return {sk_ref_sp(fTexture), kKeySyncMode};
Brian Salomond538d3d2019-04-04 12:18:17 -0400412 } else if (fConfig == kUnknown_GrPixelConfig) {
413 // We've already called fulfill and it failed. Our contract says that we should only
414 // call each callback once.
415 return {};
Brian Salomon876a0172019-03-08 11:12:14 -0500416 }
Brian Salomone80b8092019-03-08 13:25:19 -0500417 SkASSERT(fDoneCallback);
418 PromiseImageTextureContext textureContext = fDoneCallback->context();
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500419 sk_sp<SkPromiseImageTexture> promiseTexture = fFulfillProc(textureContext);
420 // From here on out our contract is that the release proc must be called, even if
421 // the return from fulfill was invalid or we fail for some other reason.
Brian Salomone80b8092019-03-08 13:25:19 -0500422 auto releaseCallback = sk_make_sp<GrRefCntedCallback>(fReleaseProc, textureContext);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500423 if (!promiseTexture) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400424 // This records that we have failed.
425 fConfig = kUnknown_GrPixelConfig;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400426 return {};
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500427 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500428
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500429 auto backendTexture = promiseTexture->backendTexture();
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500430 backendTexture.fConfig = fConfig;
431 if (!backendTexture.isValid()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400432 return {};
Brian Salomon559c6172019-01-10 10:23:44 -0500433 }
434
Brian Salomonf55e8d52019-01-30 17:28:20 -0500435 sk_sp<GrTexture> tex;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500436 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Salomon7d88f312019-02-28 10:03:03 -0500437 GrUniqueKey key;
438 GrUniqueKey::Builder builder(&key, kDomain, 2, "promise");
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500439 builder[0] = promiseTexture->uniqueID();
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500440 builder[1] = fConfig;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500441 builder.finish();
Brian Salomon0d606762019-01-25 09:58:38 -0500442 // A texture with this key may already exist from a different instance of this lazy
443 // callback. This could happen if the client fulfills a promise image with a texture
444 // that was previously used to fulfill a different promise image.
Brian Salomon7d88f312019-02-28 10:03:03 -0500445 if (auto surf = resourceProvider->findByUniqueKey<GrSurface>(key)) {
Brian Salomon0d606762019-01-25 09:58:38 -0500446 tex = sk_ref_sp(surf->asTexture());
447 SkASSERT(tex);
448 } else {
449 if ((tex = resourceProvider->wrapBackendTexture(
450 backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kYes,
451 kRead_GrIOType))) {
Brian Salomon7d88f312019-02-28 10:03:03 -0500452 tex->resourcePriv().setUniqueKey(key);
Brian Salomon0d606762019-01-25 09:58:38 -0500453 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400454 return {};
Brian Salomon0d606762019-01-25 09:58:38 -0500455 }
456 }
Brian Salomon0cc57542019-03-08 13:28:46 -0500457 auto releaseIdleState = fVersion == PromiseImageApiVersion::kLegacy
458 ? GrTexture::IdleState::kFinished
459 : GrTexture::IdleState::kFlushed;
460 tex->addIdleProc(std::move(releaseCallback), releaseIdleState);
Brian Salomone80b8092019-03-08 13:25:19 -0500461 tex->addIdleProc(std::move(fDoneCallback), GrTexture::IdleState::kFinished);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500462 promiseTexture->addKeyToInvalidate(tex->getContext()->priv().contextID(), key);
Brian Salomon876a0172019-03-08 11:12:14 -0500463 fTexture = tex.get();
464 // We need to hold on to the GrTexture in case our proxy gets reinstantiated. However,
465 // we can't unref in our destructor because we may be on another thread then. So we
466 // let the cache know it is waiting on an unref message. We will send that message from
467 // our destructor.
468 GrContext* context = fTexture->getContext();
469 context->priv().getResourceCache()->insertDelayedResourceUnref(fTexture);
470 fTextureContextID = context->priv().contextID();
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400471 return {std::move(tex), kKeySyncMode};
Brian Salomonbe5a0932018-12-10 10:03:26 -0500472 }
473
474 private:
Brian Salomone80b8092019-03-08 13:25:19 -0500475 PromiseImageTextureFulfillProc fFulfillProc;
476 PromiseImageTextureReleaseProc fReleaseProc;
477 sk_sp<GrRefCntedCallback> fDoneCallback;
Brian Salomon876a0172019-03-08 11:12:14 -0500478 GrTexture* fTexture = nullptr;
479 uint32_t fTextureContextID = SK_InvalidUniqueID;
Brian Salomon553610d2019-01-14 17:34:12 -0500480 GrPixelConfig fConfig;
Brian Salomon0cc57542019-03-08 13:28:46 -0500481 PromiseImageApiVersion fVersion;
482 } callback(fulfillProc, releaseProc, doneProc, textureContext, config, version);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500483
Robert Phillips9da87e02019-02-04 13:26:26 -0500484 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonbe5a0932018-12-10 10:03:26 -0500485
486 GrSurfaceDesc desc;
487 desc.fWidth = width;
488 desc.fHeight = height;
489 desc.fConfig = config;
490
491 // We pass kReadOnly here since we should treat content of the client's texture as immutable.
492 return proxyProvider->createLazyProxy(std::move(callback), backendFormat, desc, origin,
493 mipMapped, GrInternalSurfaceFlags::kReadOnly,
494 SkBackingFit::kExact, SkBudgeted::kNo,
Brian Salomon876a0172019-03-08 11:12:14 -0500495 GrSurfaceProxy::LazyInstantiationType::kDeinstantiate);
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400496}