blob: 60fc1c486767c869a7121070a7e9d83b11881d2f [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
Greg Danielba88ab62019-07-26 09:14:01 -040090 sk_sp<GrTextureProxy> texProxy = this->asTextureProxyRef(direct);
91 GrColorType grColorType = SkColorTypeAndFormatToGrColorType(fContext->priv().caps(),
92 this->colorType(),
93 texProxy->backendFormat());
94
Brian Salomond6287472019-06-24 15:50:07 -040095 sk_sp<GrSurfaceContext> sContext =
Greg Danielba88ab62019-07-26 09:14:01 -040096 direct->priv().makeWrappedSurfaceContext(std::move(texProxy),
97 grColorType,
Brian Salomond6287472019-06-24 15:50:07 -040098 this->alphaType(),
99 this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400100 if (!sContext) {
101 return false;
102 }
103
Brian Salomon1d435302019-07-01 13:05:28 -0400104 if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), {0, 0})) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400105 return false;
106 }
107
108 if (rec) {
109 SkBitmapCache::Add(std::move(rec), dst);
110 this->notifyAddedToRasterCache();
111 }
112 return true;
113}
114
Robert Phillips6603a172019-03-05 12:35:44 -0500115sk_sp<SkImage> SkImage_GpuBase::onMakeSubset(GrRecordingContext* context,
116 const SkIRect& subset) const {
117 if (!context || !fContext->priv().matches(context)) {
118 return nullptr;
119 }
120
121 sk_sp<GrSurfaceProxy> proxy = this->asTextureProxyRef(context);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400122
Greg Daniel46cfbc62019-06-07 11:43:30 -0400123 sk_sp<GrTextureProxy> copyProxy = GrSurfaceProxy::Copy(
124 context, proxy.get(), GrMipMapped::kNo, subset, SkBackingFit::kExact,
125 proxy->isBudgeted());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400126
Greg Daniel46cfbc62019-06-07 11:43:30 -0400127 if (!copyProxy) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400128 return nullptr;
129 }
130
131 // MDB: this call is okay bc we know 'sContext' was kExact
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400132 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, this->alphaType(),
Greg Daniel46cfbc62019-06-07 11:43:30 -0400133 std::move(copyProxy), this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400134}
135
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400136bool SkImage_GpuBase::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
137 int srcX, int srcY, CachingHint) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500138 auto direct = fContext->priv().asDirectContext();
139 if (!direct) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400140 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
141 return false;
142 }
143
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400144 if (!SkImageInfoValidConversion(dstInfo, this->imageInfo())) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400145 return false;
146 }
147
Greg Danielba88ab62019-07-26 09:14:01 -0400148 sk_sp<GrTextureProxy> texProxy = this->asTextureProxyRef(direct);
149 GrColorType grColorType = SkColorTypeAndFormatToGrColorType(fContext->priv().caps(),
150 this->colorType(),
151 texProxy->backendFormat());
152
Robert Phillips920d4882019-03-04 15:16:44 -0500153 sk_sp<GrSurfaceContext> sContext = direct->priv().makeWrappedSurfaceContext(
Greg Danielba88ab62019-07-26 09:14:01 -0400154 std::move(texProxy), grColorType, this->alphaType(), this->refColorSpace());
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400155 if (!sContext) {
156 return false;
157 }
158
Brian Salomon1d435302019-07-01 13:05:28 -0400159 return sContext->readPixels(dstInfo, dstPixels, dstRB, {srcX, srcY});
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400160}
161
Robert Phillips9338c602019-02-19 12:52:29 -0500162sk_sp<GrTextureProxy> SkImage_GpuBase::asTextureProxyRef(GrRecordingContext* context,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400163 const GrSamplerState& params,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400164 SkScalar scaleAdjust[2]) const {
Robert Phillips6603a172019-03-05 12:35:44 -0500165 if (!context || !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400166 SkASSERT(0);
167 return nullptr;
168 }
169
Brian Salomond6287472019-06-24 15:50:07 -0400170 GrTextureAdjuster adjuster(fContext.get(), this->asTextureProxyRef(context),
171 SkColorTypeToGrColorType(this->colorType()), this->alphaType(),
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400172 this->uniqueID(), this->colorSpace());
Brian Osman6064e1c2018-10-19 14:27:54 -0400173 return adjuster.refTextureProxyForParams(params, scaleAdjust);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400174}
175
176GrBackendTexture SkImage_GpuBase::onGetBackendTexture(bool flushPendingGrContextIO,
177 GrSurfaceOrigin* origin) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500178 auto direct = fContext->priv().asDirectContext();
179 if (!direct) {
180 // This image was created with a DDL context and cannot be instantiated.
Robert Phillips6603a172019-03-05 12:35:44 -0500181 return GrBackendTexture(); // invalid
Robert Phillips920d4882019-03-04 15:16:44 -0500182 }
183
Robert Phillips6603a172019-03-05 12:35:44 -0500184 sk_sp<GrTextureProxy> proxy = this->asTextureProxyRef(direct);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400185 SkASSERT(proxy);
186
Robert Phillips920d4882019-03-04 15:16:44 -0500187 if (!proxy->isInstantiated()) {
188 auto resourceProvider = direct->priv().resourceProvider();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400189
Robert Phillips920d4882019-03-04 15:16:44 -0500190 if (!proxy->instantiate(resourceProvider)) {
191 return GrBackendTexture(); // invalid
192 }
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400193 }
194
195 GrTexture* texture = proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400196 if (texture) {
197 if (flushPendingGrContextIO) {
Greg Daniel4aa13e72019-04-15 14:42:20 -0400198 direct->priv().flushSurface(proxy.get());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400199 }
200 if (origin) {
201 *origin = proxy->origin();
202 }
203 return texture->getBackendTexture();
204 }
205 return GrBackendTexture(); // invalid
206}
207
208GrTexture* SkImage_GpuBase::onGetTexture() const {
209 GrTextureProxy* proxy = this->peekProxy();
Robert Phillips193c4212019-03-04 12:18:53 -0500210 if (proxy && proxy->isInstantiated()) {
211 return proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400212 }
213
Robert Phillips193c4212019-03-04 12:18:53 -0500214 auto direct = fContext->priv().asDirectContext();
215 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400216 // This image was created with a DDL context and cannot be instantiated.
217 return nullptr;
218 }
219
Robert Phillips6603a172019-03-05 12:35:44 -0500220 sk_sp<GrTextureProxy> proxyRef = this->asTextureProxyRef(direct);
Robert Phillips193c4212019-03-04 12:18:53 -0500221 SkASSERT(proxyRef && !proxyRef->isInstantiated());
222
223 if (!proxyRef->instantiate(direct->priv().resourceProvider())) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400224 return nullptr;
225 }
226
Robert Phillips193c4212019-03-04 12:18:53 -0500227 return proxyRef->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400228}
229
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400230bool SkImage_GpuBase::onIsValid(GrContext* context) const {
231 // The base class has already checked that context isn't abandoned (if it's not nullptr)
Robert Phillips920d4882019-03-04 15:16:44 -0500232 if (fContext->priv().abandoned()) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400233 return false;
234 }
235
Robert Phillips920d4882019-03-04 15:16:44 -0500236 if (context && !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400237 return false;
238 }
239
240 return true;
241}
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400242
Jim Van Verth0e671942018-11-09 12:03:57 -0500243bool SkImage_GpuBase::MakeTempTextureProxies(GrContext* ctx, const GrBackendTexture yuvaTextures[],
Jim Van Verth53275362018-11-09 15:42:35 -0500244 int numTextures, const SkYUVAIndex yuvaIndices[4],
245 GrSurfaceOrigin imageOrigin,
Jim Van Verth0e671942018-11-09 12:03:57 -0500246 sk_sp<GrTextureProxy> tempTextureProxies[4]) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500247 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400248 const GrCaps* caps = ctx->priv().caps();
Jim Van Verth0e671942018-11-09 12:03:57 -0500249
Jim Van Verth0e671942018-11-09 12:03:57 -0500250 for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) {
Robert Phillips62221e72019-07-24 15:07:38 -0400251 GrBackendFormat backendFormat = yuvaTextures[textureIndex].getBackendFormat();
Brian Salomonf391d0f2018-12-14 09:18:50 -0500252 if (!backendFormat.isValid()) {
253 return false;
254 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400255
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400256 GrColorType grColorType = caps->getYUVAColorTypeFromBackendFormat(backendFormat);
257 if (GrColorType::kUnknown == grColorType) {
258 return false;
259 }
260
Robert Phillips62221e72019-07-24 15:07:38 -0400261 SkASSERT(yuvaTextures[textureIndex].isValid());
Jim Van Verth0e671942018-11-09 12:03:57 -0500262
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500263 tempTextureProxies[textureIndex] = proxyProvider->wrapBackendTexture(
Robert Phillips62221e72019-07-24 15:07:38 -0400264 yuvaTextures[textureIndex], grColorType, imageOrigin, kBorrow_GrWrapOwnership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500265 GrWrapCacheable::kNo, kRead_GrIOType);
Jim Van Verth0e671942018-11-09 12:03:57 -0500266 if (!tempTextureProxies[textureIndex]) {
267 return false;
268 }
Jim Van Verth53275362018-11-09 15:42:35 -0500269
270 // Check that each texture contains the channel data for the corresponding YUVA index
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400271 auto componentFlags = GrColorTypeComponentFlags(grColorType);
Jim Van Verth53275362018-11-09 15:42:35 -0500272 for (int yuvaIndex = 0; yuvaIndex < SkYUVAIndex::kIndexCount; ++yuvaIndex) {
273 if (yuvaIndices[yuvaIndex].fIndex == textureIndex) {
274 switch (yuvaIndices[yuvaIndex].fChannel) {
275 case SkColorChannel::kR:
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400276 // TODO: Chrome needs to be patched before this can be
277 // enforced.
278// if (!(kRed_SkColorTypeComponentFlag & componentFlags)) {
279// return false;
280// }
281 break;
282 case SkColorChannel::kG:
283 if (!(kGreen_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500284 return false;
285 }
286 break;
Jim Van Verth53275362018-11-09 15:42:35 -0500287 case SkColorChannel::kB:
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400288 if (!(kBlue_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500289 return false;
290 }
291 break;
292 case SkColorChannel::kA:
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400293 if (!(kAlpha_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500294 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(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400330 GrContext* context, int width, int height, GrSurfaceOrigin origin, GrColorType colorType,
Brian Salomonbe5a0932018-12-10 10:03:26 -0500331 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);
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400340 SkASSERT(colorType != GrColorType::kUnknown);
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,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400375 GrColorType colorType,
Brian Salomon0cc57542019-03-08 13:28:46 -0500376 PromiseImageApiVersion version)
377 : fFulfillProc(fulfillProc)
378 , fReleaseProc(releaseProc)
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400379 , fColorType(colorType)
Brian Salomon0cc57542019-03-08 13:28:46 -0500380 , 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};
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400412 } else if (fColorType == GrColorType::kUnknown) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400413 // 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.
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400425 fColorType = GrColorType::kUnknown;
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
Robert Phillips62221e72019-07-24 15:07:38 -0400429 const GrBackendTexture& backendTexture = promiseTexture->backendTexture();
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500430 if (!backendTexture.isValid()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400431 return {};
Brian Salomon559c6172019-01-10 10:23:44 -0500432 }
433
Brian Salomonf55e8d52019-01-30 17:28:20 -0500434 sk_sp<GrTexture> tex;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500435 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Salomon7d88f312019-02-28 10:03:03 -0500436 GrUniqueKey key;
437 GrUniqueKey::Builder builder(&key, kDomain, 2, "promise");
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500438 builder[0] = promiseTexture->uniqueID();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400439 builder[1] = (uint32_t) fColorType;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500440 builder.finish();
Brian Salomon0d606762019-01-25 09:58:38 -0500441 // A texture with this key may already exist from a different instance of this lazy
442 // callback. This could happen if the client fulfills a promise image with a texture
443 // that was previously used to fulfill a different promise image.
Brian Salomon7d88f312019-02-28 10:03:03 -0500444 if (auto surf = resourceProvider->findByUniqueKey<GrSurface>(key)) {
Brian Salomon0d606762019-01-25 09:58:38 -0500445 tex = sk_ref_sp(surf->asTexture());
446 SkASSERT(tex);
447 } else {
448 if ((tex = resourceProvider->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400449 backendTexture, fColorType, kBorrow_GrWrapOwnership,
450 GrWrapCacheable::kYes, kRead_GrIOType))) {
Brian Salomon7d88f312019-02-28 10:03:03 -0500451 tex->resourcePriv().setUniqueKey(key);
Brian Salomon0d606762019-01-25 09:58:38 -0500452 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400453 return {};
Brian Salomon0d606762019-01-25 09:58:38 -0500454 }
455 }
Brian Salomon0cc57542019-03-08 13:28:46 -0500456 auto releaseIdleState = fVersion == PromiseImageApiVersion::kLegacy
457 ? GrTexture::IdleState::kFinished
458 : GrTexture::IdleState::kFlushed;
459 tex->addIdleProc(std::move(releaseCallback), releaseIdleState);
Brian Salomone80b8092019-03-08 13:25:19 -0500460 tex->addIdleProc(std::move(fDoneCallback), GrTexture::IdleState::kFinished);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500461 promiseTexture->addKeyToInvalidate(tex->getContext()->priv().contextID(), key);
Brian Salomon876a0172019-03-08 11:12:14 -0500462 fTexture = tex.get();
463 // We need to hold on to the GrTexture in case our proxy gets reinstantiated. However,
464 // we can't unref in our destructor because we may be on another thread then. So we
465 // let the cache know it is waiting on an unref message. We will send that message from
466 // our destructor.
467 GrContext* context = fTexture->getContext();
468 context->priv().getResourceCache()->insertDelayedResourceUnref(fTexture);
469 fTextureContextID = context->priv().contextID();
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400470 return {std::move(tex), kKeySyncMode};
Brian Salomonbe5a0932018-12-10 10:03:26 -0500471 }
472
473 private:
Brian Salomone80b8092019-03-08 13:25:19 -0500474 PromiseImageTextureFulfillProc fFulfillProc;
475 PromiseImageTextureReleaseProc fReleaseProc;
476 sk_sp<GrRefCntedCallback> fDoneCallback;
Brian Salomon876a0172019-03-08 11:12:14 -0500477 GrTexture* fTexture = nullptr;
478 uint32_t fTextureContextID = SK_InvalidUniqueID;
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400479 GrColorType fColorType;
Brian Salomon0cc57542019-03-08 13:28:46 -0500480 PromiseImageApiVersion fVersion;
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400481 } callback(fulfillProc, releaseProc, doneProc, textureContext, colorType, version);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500482
Robert Phillips9da87e02019-02-04 13:26:26 -0500483 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonbe5a0932018-12-10 10:03:26 -0500484
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400485 GrPixelConfig config = context->priv().caps()->getConfigFromBackendFormat(
486 backendFormat,
487 colorType);
488
Brian Salomonbe5a0932018-12-10 10:03:26 -0500489 GrSurfaceDesc desc;
490 desc.fWidth = width;
491 desc.fHeight = height;
492 desc.fConfig = config;
493
494 // We pass kReadOnly here since we should treat content of the client's texture as immutable.
Brian Salomone8a766b2019-07-19 14:24:36 -0400495 // The promise API provides no way for the client to indicated that the texture is protected.
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400496 return proxyProvider->createLazyProxy(
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400497 std::move(callback), backendFormat, desc, GrRenderable::kNo, 1, origin, mipMapped,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400498 GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, SkBudgeted::kNo,
Brian Salomone8a766b2019-07-19 14:24:36 -0400499 GrProtected::kNo, GrSurfaceProxy::LazyInstantiationType::kDeinstantiate);
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400500}