blob: 2a78ff8262f4af44823c85e0301193c32c9cdfba [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
Robert Phillips920d4882019-03-04 15:16:44 -050090 sk_sp<GrSurfaceContext> sContext = direct->priv().makeWrappedSurfaceContext(
Brian Salomon5ad6fd32019-03-21 15:30:08 -040091 this->asTextureProxyRef(direct), this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -040092 if (!sContext) {
93 return false;
94 }
95
96 if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), 0, 0)) {
97 return false;
98 }
99
100 if (rec) {
101 SkBitmapCache::Add(std::move(rec), dst);
102 this->notifyAddedToRasterCache();
103 }
104 return true;
105}
106
Robert Phillips6603a172019-03-05 12:35:44 -0500107sk_sp<SkImage> SkImage_GpuBase::onMakeSubset(GrRecordingContext* context,
108 const SkIRect& subset) const {
109 if (!context || !fContext->priv().matches(context)) {
110 return nullptr;
111 }
112
113 sk_sp<GrSurfaceProxy> proxy = this->asTextureProxyRef(context);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400114
115 GrSurfaceDesc desc;
116 desc.fWidth = subset.width();
117 desc.fHeight = subset.height();
118 desc.fConfig = proxy->config();
119
Greg Daniel4065d452018-11-16 15:43:41 -0500120 GrBackendFormat format = proxy->backendFormat().makeTexture2D();
121 if (!format.isValid()) {
122 return nullptr;
123 }
124
Brian Salomonf05e6d32018-12-20 08:41:41 -0500125 // TODO: Should this inherit our proxy's budgeted status?
Robert Phillips6603a172019-03-05 12:35:44 -0500126 sk_sp<GrSurfaceContext> sContext(context->priv().makeDeferredSurfaceContext(
Brian Salomonf05e6d32018-12-20 08:41:41 -0500127 format, desc, proxy->origin(), GrMipMapped::kNo, SkBackingFit::kExact,
128 proxy->isBudgeted()));
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400129 if (!sContext) {
130 return nullptr;
131 }
132
133 if (!sContext->copy(proxy.get(), subset, SkIPoint::Make(0, 0))) {
134 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(),
Brian Osmane9560492019-02-05 17:00:03 -0500139 sContext->asTextureProxyRef(), this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400140}
141
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400142static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
143 switch (info.colorType()) {
144 case kRGBA_8888_SkColorType:
145 case kBGRA_8888_SkColorType:
146 break;
147 default:
148 return; // nothing to do
149 }
150
Brian Salomon3f4cd772019-01-11 16:03:19 -0500151 // SkColor is not necessarily RGBA or BGRA, but it is one of them on little-endian,
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400152 // and in either case, the alpha-byte is always in the same place, so we can safely call
153 // SkPreMultiplyColor()
154 //
155 SkColor* row = (SkColor*)pixels;
156 for (int y = 0; y < info.height(); ++y) {
157 for (int x = 0; x < info.width(); ++x) {
158 row[x] = SkPreMultiplyColor(row[x]);
159 }
160 row = (SkColor*)((char*)(row)+rowBytes);
161 }
162}
163
164bool SkImage_GpuBase::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
165 int srcX, int srcY, CachingHint) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500166 auto direct = fContext->priv().asDirectContext();
167 if (!direct) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400168 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
169 return false;
170 }
171
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400172 if (!SkImageInfoValidConversion(dstInfo, this->imageInfo())) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400173 return false;
174 }
175
176 SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, srcX, srcY);
177 if (!rec.trim(this->width(), this->height())) {
178 return false;
179 }
180
181 // TODO: this seems to duplicate code in GrTextureContext::onReadPixels and
182 // GrRenderTargetContext::onReadPixels
183 uint32_t flags = 0;
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400184 if (kUnpremul_SkAlphaType == rec.fInfo.alphaType() &&
185 kPremul_SkAlphaType == this->alphaType()) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400186 // let the GPU perform this transformation for us
187 flags = GrContextPriv::kUnpremul_PixelOpsFlag;
188 }
189
Robert Phillips920d4882019-03-04 15:16:44 -0500190 sk_sp<GrSurfaceContext> sContext = direct->priv().makeWrappedSurfaceContext(
Robert Phillips6603a172019-03-05 12:35:44 -0500191 this->asTextureProxyRef(direct), this->refColorSpace());
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400192 if (!sContext) {
193 return false;
194 }
195
196 if (!sContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY, flags)) {
197 return false;
198 }
199
200 // do we have to manually fix-up the alpha channel?
201 // src dst
202 // unpremul premul fix manually
203 // premul unpremul done by kUnpremul_PixelOpsFlag
204 // all other combos need to change.
205 //
206 // Should this be handled by Ganesh? todo:?
207 //
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400208 if (kPremul_SkAlphaType == rec.fInfo.alphaType() &&
209 kUnpremul_SkAlphaType == this->alphaType()) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400210 apply_premul(rec.fInfo, rec.fPixels, rec.fRowBytes);
211 }
212 return true;
213}
214
Robert Phillips9338c602019-02-19 12:52:29 -0500215sk_sp<GrTextureProxy> SkImage_GpuBase::asTextureProxyRef(GrRecordingContext* context,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400216 const GrSamplerState& params,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400217 SkScalar scaleAdjust[2]) const {
Robert Phillips6603a172019-03-05 12:35:44 -0500218 if (!context || !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400219 SkASSERT(0);
220 return nullptr;
221 }
222
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400223 GrTextureAdjuster adjuster(fContext.get(), this->asTextureProxyRef(context), this->alphaType(),
224 this->uniqueID(), this->colorSpace());
Brian Osman6064e1c2018-10-19 14:27:54 -0400225 return adjuster.refTextureProxyForParams(params, scaleAdjust);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400226}
227
228GrBackendTexture SkImage_GpuBase::onGetBackendTexture(bool flushPendingGrContextIO,
229 GrSurfaceOrigin* origin) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500230 auto direct = fContext->priv().asDirectContext();
231 if (!direct) {
232 // This image was created with a DDL context and cannot be instantiated.
Robert Phillips6603a172019-03-05 12:35:44 -0500233 return GrBackendTexture(); // invalid
Robert Phillips920d4882019-03-04 15:16:44 -0500234 }
235
Robert Phillips6603a172019-03-05 12:35:44 -0500236 sk_sp<GrTextureProxy> proxy = this->asTextureProxyRef(direct);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400237 SkASSERT(proxy);
238
Robert Phillips920d4882019-03-04 15:16:44 -0500239 if (!proxy->isInstantiated()) {
240 auto resourceProvider = direct->priv().resourceProvider();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400241
Robert Phillips920d4882019-03-04 15:16:44 -0500242 if (!proxy->instantiate(resourceProvider)) {
243 return GrBackendTexture(); // invalid
244 }
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400245 }
246
247 GrTexture* texture = proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400248 if (texture) {
249 if (flushPendingGrContextIO) {
Greg Daniel4aa13e72019-04-15 14:42:20 -0400250 direct->priv().flushSurface(proxy.get());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400251 }
252 if (origin) {
253 *origin = proxy->origin();
254 }
255 return texture->getBackendTexture();
256 }
257 return GrBackendTexture(); // invalid
258}
259
260GrTexture* SkImage_GpuBase::onGetTexture() const {
261 GrTextureProxy* proxy = this->peekProxy();
Robert Phillips193c4212019-03-04 12:18:53 -0500262 if (proxy && proxy->isInstantiated()) {
263 return proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400264 }
265
Robert Phillips193c4212019-03-04 12:18:53 -0500266 auto direct = fContext->priv().asDirectContext();
267 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400268 // This image was created with a DDL context and cannot be instantiated.
269 return nullptr;
270 }
271
Robert Phillips6603a172019-03-05 12:35:44 -0500272 sk_sp<GrTextureProxy> proxyRef = this->asTextureProxyRef(direct);
Robert Phillips193c4212019-03-04 12:18:53 -0500273 SkASSERT(proxyRef && !proxyRef->isInstantiated());
274
275 if (!proxyRef->instantiate(direct->priv().resourceProvider())) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400276 return nullptr;
277 }
278
Robert Phillips193c4212019-03-04 12:18:53 -0500279 return proxyRef->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400280}
281
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400282bool SkImage_GpuBase::onIsValid(GrContext* context) const {
283 // The base class has already checked that context isn't abandoned (if it's not nullptr)
Robert Phillips920d4882019-03-04 15:16:44 -0500284 if (fContext->priv().abandoned()) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400285 return false;
286 }
287
Robert Phillips920d4882019-03-04 15:16:44 -0500288 if (context && !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400289 return false;
290 }
291
292 return true;
293}
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400294
Jim Van Verth0e671942018-11-09 12:03:57 -0500295bool SkImage_GpuBase::MakeTempTextureProxies(GrContext* ctx, const GrBackendTexture yuvaTextures[],
Jim Van Verth53275362018-11-09 15:42:35 -0500296 int numTextures, const SkYUVAIndex yuvaIndices[4],
297 GrSurfaceOrigin imageOrigin,
Jim Van Verth0e671942018-11-09 12:03:57 -0500298 sk_sp<GrTextureProxy> tempTextureProxies[4]) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500299 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Jim Van Verth0e671942018-11-09 12:03:57 -0500300
301 // We need to make a copy of the input backend textures because we need to preserve the result
302 // of validate_backend_texture.
303 GrBackendTexture yuvaTexturesCopy[4];
304 for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) {
305 yuvaTexturesCopy[textureIndex] = yuvaTextures[textureIndex];
Brian Salomonf391d0f2018-12-14 09:18:50 -0500306 GrBackendFormat backendFormat = yuvaTexturesCopy[textureIndex].getBackendFormat();
307 if (!backendFormat.isValid()) {
308 return false;
309 }
310 yuvaTexturesCopy[textureIndex].fConfig =
Robert Phillips9da87e02019-02-04 13:26:26 -0500311 ctx->priv().caps()->getYUVAConfigFromBackendFormat(backendFormat);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500312 if (yuvaTexturesCopy[textureIndex].fConfig == kUnknown_GrPixelConfig) {
Jim Van Verth0e671942018-11-09 12:03:57 -0500313 return false;
314 }
315 SkASSERT(yuvaTexturesCopy[textureIndex].isValid());
316
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500317 tempTextureProxies[textureIndex] = proxyProvider->wrapBackendTexture(
318 yuvaTexturesCopy[textureIndex], imageOrigin, kBorrow_GrWrapOwnership,
319 GrWrapCacheable::kNo, kRead_GrIOType);
Jim Van Verth0e671942018-11-09 12:03:57 -0500320 if (!tempTextureProxies[textureIndex]) {
321 return false;
322 }
Jim Van Verth53275362018-11-09 15:42:35 -0500323
324 // Check that each texture contains the channel data for the corresponding YUVA index
325 GrPixelConfig config = yuvaTexturesCopy[textureIndex].fConfig;
326 for (int yuvaIndex = 0; yuvaIndex < SkYUVAIndex::kIndexCount; ++yuvaIndex) {
327 if (yuvaIndices[yuvaIndex].fIndex == textureIndex) {
328 switch (yuvaIndices[yuvaIndex].fChannel) {
329 case SkColorChannel::kR:
330 if (kAlpha_8_as_Alpha_GrPixelConfig == config) {
331 return false;
332 }
333 break;
334 case SkColorChannel::kG:
335 case SkColorChannel::kB:
336 if (kAlpha_8_as_Alpha_GrPixelConfig == config ||
337 kAlpha_8_as_Red_GrPixelConfig == config) {
338 return false;
339 }
340 break;
341 case SkColorChannel::kA:
342 default:
343 if (kRGB_888_GrPixelConfig == config) {
344 return false;
345 }
346 break;
347 }
348 }
349 }
Jim Van Verth0e671942018-11-09 12:03:57 -0500350 }
351
352 return true;
353}
354
355bool SkImage_GpuBase::RenderYUVAToRGBA(GrContext* ctx, GrRenderTargetContext* renderTargetContext,
356 const SkRect& rect, SkYUVColorSpace yuvColorSpace,
Brian Osmane9560492019-02-05 17:00:03 -0500357 sk_sp<GrColorSpaceXform> colorSpaceXform,
Jim Van Verth0e671942018-11-09 12:03:57 -0500358 const sk_sp<GrTextureProxy> proxies[4],
359 const SkYUVAIndex yuvaIndices[4]) {
360 SkASSERT(renderTargetContext);
361 if (!renderTargetContext->asSurfaceProxy()) {
362 return false;
363 }
364
365 GrPaint paint;
366 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
367
Brian Osmane9560492019-02-05 17:00:03 -0500368 auto fp = GrYUVtoRGBEffect::Make(proxies, yuvaIndices, yuvColorSpace,
369 GrSamplerState::Filter::kNearest);
370 if (colorSpaceXform) {
371 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(colorSpaceXform));
372 }
373 paint.addColorFragmentProcessor(std::move(fp));
Jim Van Verth0e671942018-11-09 12:03:57 -0500374
375 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Jim Van Verth0e671942018-11-09 12:03:57 -0500376 return true;
377}
378
Brian Salomonbe5a0932018-12-10 10:03:26 -0500379sk_sp<GrTextureProxy> SkImage_GpuBase::MakePromiseImageLazyProxy(
380 GrContext* context, int width, int height, GrSurfaceOrigin origin, GrPixelConfig config,
381 GrBackendFormat backendFormat, GrMipMapped mipMapped,
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500382 PromiseImageTextureFulfillProc fulfillProc,
383 PromiseImageTextureReleaseProc releaseProc,
384 PromiseImageTextureDoneProc doneProc,
Brian Salomon0cc57542019-03-08 13:28:46 -0500385 PromiseImageTextureContext textureContext,
386 PromiseImageApiVersion version) {
Brian Salomonbe5a0932018-12-10 10:03:26 -0500387 SkASSERT(context);
388 SkASSERT(width > 0 && height > 0);
389 SkASSERT(doneProc);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500390 SkASSERT(config != kUnknown_GrPixelConfig);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500391
392 if (!fulfillProc || !releaseProc) {
393 doneProc(textureContext);
394 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400395 }
396
Brian Salomonbe5a0932018-12-10 10:03:26 -0500397 if (mipMapped == GrMipMapped::kYes &&
398 GrTextureTypeHasRestrictedSampling(backendFormat.textureType())) {
399 // It is invalid to have a GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE and have mips as
400 // well.
401 doneProc(textureContext);
402 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400403 }
Brian Salomonbe5a0932018-12-10 10:03:26 -0500404
405 /**
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500406 * This class is the lazy instantiation callback for promise images. It manages calling the
407 * client's Fulfill, Release, and Done procs. It attempts to reuse a GrTexture instance in
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500408 * cases where the client provides the same SkPromiseImageTexture as Fulfill results for
409 * multiple SkImages. The created GrTexture is given a key based on a unique ID associated with
410 * the SkPromiseImageTexture.
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500411 *
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500412 * The GrTexutre idle proc mechanism is used to call the Release and Done procs. We use this
413 * instead of the GrSurface release proc because the GrTexture is cached and therefore may
414 * outlive the proxy into which this callback is installed.
415 *
416 * A key invalidation message is installed on the SkPromiseImageTexture so that the GrTexture
417 * is deleted once it can no longer be used to instantiate a proxy.
Brian Salomonbe5a0932018-12-10 10:03:26 -0500418 */
419 class PromiseLazyInstantiateCallback {
420 public:
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500421 PromiseLazyInstantiateCallback(PromiseImageTextureFulfillProc fulfillProc,
422 PromiseImageTextureReleaseProc releaseProc,
423 PromiseImageTextureDoneProc doneProc,
424 PromiseImageTextureContext context,
Brian Salomon0cc57542019-03-08 13:28:46 -0500425 GrPixelConfig config,
426 PromiseImageApiVersion version)
427 : fFulfillProc(fulfillProc)
428 , fReleaseProc(releaseProc)
429 , fConfig(config)
430 , fVersion(version) {
Brian Salomone80b8092019-03-08 13:25:19 -0500431 fDoneCallback = sk_make_sp<GrRefCntedCallback>(doneProc, context);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500432 }
433 PromiseLazyInstantiateCallback(PromiseLazyInstantiateCallback&&) = default;
434 PromiseLazyInstantiateCallback(const PromiseLazyInstantiateCallback&) {
435 // Because we get wrapped in std::function we must be copyable. But we should never
436 // be copied.
437 SkASSERT(false);
438 }
439 PromiseLazyInstantiateCallback& operator=(PromiseLazyInstantiateCallback&&) = default;
440 PromiseLazyInstantiateCallback& operator=(const PromiseLazyInstantiateCallback&) {
441 SkASSERT(false);
442 return *this;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500443 }
Brian Salomon553610d2019-01-14 17:34:12 -0500444
Brian Salomon88b8d112019-03-07 15:25:34 +0000445 ~PromiseLazyInstantiateCallback() {
Brian Salomon876a0172019-03-08 11:12:14 -0500446 // Our destructor can run on any thread. We trigger the unref of fTexture by message.
447 if (fTexture) {
Brian Salomon876a0172019-03-08 11:12:14 -0500448 SkMessageBus<GrGpuResourceFreedMessage>::Post({fTexture, fTextureContextID});
449 }
Brian Salomon88b8d112019-03-07 15:25:34 +0000450 }
451
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400452 GrSurfaceProxy::LazyInstantiationResult operator()(GrResourceProvider* resourceProvider) {
453 // We use the unique key in a way that is unrelated to the SkImage-based key that the
454 // proxy may receive, hence kUnsynced.
455 static constexpr auto kKeySyncMode =
456 GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
457
Brian Salomon876a0172019-03-08 11:12:14 -0500458 // Our proxy is getting instantiated for the second+ time. We are only allowed to call
459 // Fulfill once. So return our cached result.
460 if (fTexture) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400461 return {sk_ref_sp(fTexture), kKeySyncMode};
Brian Salomond538d3d2019-04-04 12:18:17 -0400462 } else if (fConfig == kUnknown_GrPixelConfig) {
463 // We've already called fulfill and it failed. Our contract says that we should only
464 // call each callback once.
465 return {};
Brian Salomon876a0172019-03-08 11:12:14 -0500466 }
Brian Salomone80b8092019-03-08 13:25:19 -0500467 SkASSERT(fDoneCallback);
468 PromiseImageTextureContext textureContext = fDoneCallback->context();
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500469 sk_sp<SkPromiseImageTexture> promiseTexture = fFulfillProc(textureContext);
470 // From here on out our contract is that the release proc must be called, even if
471 // the return from fulfill was invalid or we fail for some other reason.
Brian Salomone80b8092019-03-08 13:25:19 -0500472 auto releaseCallback = sk_make_sp<GrRefCntedCallback>(fReleaseProc, textureContext);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500473 if (!promiseTexture) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400474 // This records that we have failed.
475 fConfig = kUnknown_GrPixelConfig;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400476 return {};
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500477 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500478
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500479 auto backendTexture = promiseTexture->backendTexture();
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500480 backendTexture.fConfig = fConfig;
481 if (!backendTexture.isValid()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400482 return {};
Brian Salomon559c6172019-01-10 10:23:44 -0500483 }
484
Brian Salomonf55e8d52019-01-30 17:28:20 -0500485 sk_sp<GrTexture> tex;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500486 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Salomon7d88f312019-02-28 10:03:03 -0500487 GrUniqueKey key;
488 GrUniqueKey::Builder builder(&key, kDomain, 2, "promise");
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500489 builder[0] = promiseTexture->uniqueID();
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500490 builder[1] = fConfig;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500491 builder.finish();
Brian Salomon0d606762019-01-25 09:58:38 -0500492 // A texture with this key may already exist from a different instance of this lazy
493 // callback. This could happen if the client fulfills a promise image with a texture
494 // that was previously used to fulfill a different promise image.
Brian Salomon7d88f312019-02-28 10:03:03 -0500495 if (auto surf = resourceProvider->findByUniqueKey<GrSurface>(key)) {
Brian Salomon0d606762019-01-25 09:58:38 -0500496 tex = sk_ref_sp(surf->asTexture());
497 SkASSERT(tex);
498 } else {
499 if ((tex = resourceProvider->wrapBackendTexture(
500 backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kYes,
501 kRead_GrIOType))) {
Brian Salomon7d88f312019-02-28 10:03:03 -0500502 tex->resourcePriv().setUniqueKey(key);
Brian Salomon0d606762019-01-25 09:58:38 -0500503 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400504 return {};
Brian Salomon0d606762019-01-25 09:58:38 -0500505 }
506 }
Brian Salomon0cc57542019-03-08 13:28:46 -0500507 auto releaseIdleState = fVersion == PromiseImageApiVersion::kLegacy
508 ? GrTexture::IdleState::kFinished
509 : GrTexture::IdleState::kFlushed;
510 tex->addIdleProc(std::move(releaseCallback), releaseIdleState);
Brian Salomone80b8092019-03-08 13:25:19 -0500511 tex->addIdleProc(std::move(fDoneCallback), GrTexture::IdleState::kFinished);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500512 promiseTexture->addKeyToInvalidate(tex->getContext()->priv().contextID(), key);
Brian Salomon876a0172019-03-08 11:12:14 -0500513 fTexture = tex.get();
514 // We need to hold on to the GrTexture in case our proxy gets reinstantiated. However,
515 // we can't unref in our destructor because we may be on another thread then. So we
516 // let the cache know it is waiting on an unref message. We will send that message from
517 // our destructor.
518 GrContext* context = fTexture->getContext();
519 context->priv().getResourceCache()->insertDelayedResourceUnref(fTexture);
520 fTextureContextID = context->priv().contextID();
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400521 return {std::move(tex), kKeySyncMode};
Brian Salomonbe5a0932018-12-10 10:03:26 -0500522 }
523
524 private:
Brian Salomone80b8092019-03-08 13:25:19 -0500525 PromiseImageTextureFulfillProc fFulfillProc;
526 PromiseImageTextureReleaseProc fReleaseProc;
527 sk_sp<GrRefCntedCallback> fDoneCallback;
Brian Salomon876a0172019-03-08 11:12:14 -0500528 GrTexture* fTexture = nullptr;
529 uint32_t fTextureContextID = SK_InvalidUniqueID;
Brian Salomon553610d2019-01-14 17:34:12 -0500530 GrPixelConfig fConfig;
Brian Salomon0cc57542019-03-08 13:28:46 -0500531 PromiseImageApiVersion fVersion;
532 } callback(fulfillProc, releaseProc, doneProc, textureContext, config, version);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500533
Robert Phillips9da87e02019-02-04 13:26:26 -0500534 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonbe5a0932018-12-10 10:03:26 -0500535
536 GrSurfaceDesc desc;
537 desc.fWidth = width;
538 desc.fHeight = height;
539 desc.fConfig = config;
540
541 // We pass kReadOnly here since we should treat content of the client's texture as immutable.
542 return proxyProvider->createLazyProxy(std::move(callback), backendFormat, desc, origin,
543 mipMapped, GrInternalSurfaceFlags::kReadOnly,
544 SkBackingFit::kExact, SkBudgeted::kNo,
Brian Salomon876a0172019-03-08 11:12:14 -0500545 GrSurfaceProxy::LazyInstantiationType::kDeinstantiate);
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400546}