blob: 54cd000c797d59d5184e1098320975be6cb3cc92 [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"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040017#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrRecordingContextPriv.h"
19#include "src/gpu/GrRenderTargetContext.h"
20#include "src/gpu/GrTextureAdjuster.h"
21#include "src/gpu/effects/GrYUVtoRGBEffect.h"
22#include "src/image/SkImage_Gpu.h"
23#include "src/image/SkImage_GpuBase.h"
24#include "src/image/SkReadPixelsRec.h"
Jim Van Verth8026ccc2018-10-04 13:10:39 -040025
26SkImage_GpuBase::SkImage_GpuBase(sk_sp<GrContext> context, int width, int height, uint32_t uniqueID,
Brian Salomon5ad6fd32019-03-21 15:30:08 -040027 SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs)
28 : INHERITED(SkImageInfo::Make(width, height, ct, at, std::move(cs)), uniqueID)
29 , fContext(std::move(context)) {}
Jim Van Verth8026ccc2018-10-04 13:10:39 -040030
Brian Salomon729fc0c2019-09-30 16:33:11 +000031SkImage_GpuBase::~SkImage_GpuBase() {}
32
Jim Van Verth8026ccc2018-10-04 13:10:39 -040033//////////////////////////////////////////////////////////////////////////////////////////////////
34
Robert Phillipsfd0d9702019-02-01 10:19:42 -050035#if GR_TEST_UTILS
36void SkImage_GpuBase::resetContext(sk_sp<GrContext> newContext) {
Robert Phillipsfe0963c2019-02-07 13:25:07 -050037 SkASSERT(fContext->priv().matches(newContext.get()));
Robert Phillipsfd0d9702019-02-01 10:19:42 -050038 fContext = newContext;
39}
40#endif
41
Robert Phillips62221e72019-07-24 15:07:38 -040042bool SkImage_GpuBase::ValidateBackendTexture(const GrCaps* caps, const GrBackendTexture& tex,
43 GrColorType grCT, SkColorType ct, SkAlphaType at,
Jim Van Verth8026ccc2018-10-04 13:10:39 -040044 sk_sp<SkColorSpace> cs) {
45 if (!tex.isValid()) {
46 return false;
47 }
48 // TODO: Create a SkImageColorInfo struct for color, alpha, and color space so we don't need to
49 // create a fake image info here.
50 SkImageInfo info = SkImageInfo::Make(1, 1, ct, at, cs);
51 if (!SkImageInfoIsValid(info)) {
52 return false;
53 }
Brian Salomonf391d0f2018-12-14 09:18:50 -050054 GrBackendFormat backendFormat = tex.getBackendFormat();
55 if (!backendFormat.isValid()) {
56 return false;
57 }
Greg Daniele877dce2019-07-11 10:52:43 -040058
Robert Phillips62221e72019-07-24 15:07:38 -040059 return caps->areColorTypeAndFormatCompatible(grCT, backendFormat);
Jim Van Verth8026ccc2018-10-04 13:10:39 -040060}
61
62//////////////////////////////////////////////////////////////////////////////////////////////////
63
Brian Osmane50cdf02018-10-19 13:02:14 -040064bool SkImage_GpuBase::getROPixels(SkBitmap* dst, CachingHint chint) const {
Robert Phillips920d4882019-03-04 15:16:44 -050065 auto direct = fContext->priv().asDirectContext();
66 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040067 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
68 return false;
69 }
70
Jim Van Verth8026ccc2018-10-04 13:10:39 -040071 const auto desc = SkBitmapCacheDesc::Make(this);
72 if (SkBitmapCache::Find(desc, dst)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040073 SkASSERT(dst->isImmutable());
74 SkASSERT(dst->getPixels());
75 return true;
76 }
77
78 SkBitmapCache::RecPtr rec = nullptr;
79 SkPixmap pmap;
80 if (kAllow_CachingHint == chint) {
Brian Salomon5ad6fd32019-03-21 15:30:08 -040081 rec = SkBitmapCache::Alloc(desc, this->imageInfo(), &pmap);
Jim Van Verth8026ccc2018-10-04 13:10:39 -040082 if (!rec) {
83 return false;
84 }
85 } else {
Brian Salomon5ad6fd32019-03-21 15:30:08 -040086 if (!dst->tryAllocPixels(this->imageInfo()) || !dst->peekPixels(&pmap)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040087 return false;
88 }
89 }
90
Greg Danielba88ab62019-07-26 09:14:01 -040091 sk_sp<GrTextureProxy> texProxy = this->asTextureProxyRef(direct);
92 GrColorType grColorType = SkColorTypeAndFormatToGrColorType(fContext->priv().caps(),
93 this->colorType(),
94 texProxy->backendFormat());
95
Brian Salomonbf6b9792019-08-21 09:38:10 -040096 auto sContext = direct->priv().makeWrappedSurfaceContext(
97 std::move(texProxy), grColorType, this->alphaType(), this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -040098 if (!sContext) {
99 return false;
100 }
101
Brian Salomon1d435302019-07-01 13:05:28 -0400102 if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), {0, 0})) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400103 return false;
104 }
105
106 if (rec) {
107 SkBitmapCache::Add(std::move(rec), dst);
108 this->notifyAddedToRasterCache();
109 }
110 return true;
111}
112
Robert Phillips6603a172019-03-05 12:35:44 -0500113sk_sp<SkImage> SkImage_GpuBase::onMakeSubset(GrRecordingContext* context,
114 const SkIRect& subset) const {
115 if (!context || !fContext->priv().matches(context)) {
116 return nullptr;
117 }
118
119 sk_sp<GrSurfaceProxy> proxy = this->asTextureProxyRef(context);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400120
Greg Daniel46cfbc62019-06-07 11:43:30 -0400121 sk_sp<GrTextureProxy> copyProxy = GrSurfaceProxy::Copy(
122 context, proxy.get(), GrMipMapped::kNo, subset, SkBackingFit::kExact,
123 proxy->isBudgeted());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400124
Greg Daniel46cfbc62019-06-07 11:43:30 -0400125 if (!copyProxy) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400126 return nullptr;
127 }
128
129 // MDB: this call is okay bc we know 'sContext' was kExact
Brian Salomon729fc0c2019-09-30 16:33:11 +0000130 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, this->alphaType(),
131 std::move(copyProxy), this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400132}
133
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400134bool SkImage_GpuBase::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
135 int srcX, int srcY, CachingHint) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500136 auto direct = fContext->priv().asDirectContext();
137 if (!direct) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400138 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
139 return false;
140 }
141
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400142 if (!SkImageInfoValidConversion(dstInfo, this->imageInfo())) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400143 return false;
144 }
145
Greg Danielba88ab62019-07-26 09:14:01 -0400146 sk_sp<GrTextureProxy> texProxy = this->asTextureProxyRef(direct);
147 GrColorType grColorType = SkColorTypeAndFormatToGrColorType(fContext->priv().caps(),
148 this->colorType(),
149 texProxy->backendFormat());
150
Brian Salomonbf6b9792019-08-21 09:38:10 -0400151 auto sContext = direct->priv().makeWrappedSurfaceContext(
Greg Danielba88ab62019-07-26 09:14:01 -0400152 std::move(texProxy), grColorType, this->alphaType(), this->refColorSpace());
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400153 if (!sContext) {
154 return false;
155 }
156
Brian Salomon1d435302019-07-01 13:05:28 -0400157 return sContext->readPixels(dstInfo, dstPixels, dstRB, {srcX, srcY});
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400158}
159
Robert Phillips9338c602019-02-19 12:52:29 -0500160sk_sp<GrTextureProxy> SkImage_GpuBase::asTextureProxyRef(GrRecordingContext* context,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400161 const GrSamplerState& params,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400162 SkScalar scaleAdjust[2]) const {
Robert Phillips6603a172019-03-05 12:35:44 -0500163 if (!context || !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400164 SkASSERT(0);
165 return nullptr;
166 }
167
Brian Salomond6287472019-06-24 15:50:07 -0400168 GrTextureAdjuster adjuster(fContext.get(), this->asTextureProxyRef(context),
169 SkColorTypeToGrColorType(this->colorType()), this->alphaType(),
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400170 this->uniqueID(), this->colorSpace());
Brian Osman6064e1c2018-10-19 14:27:54 -0400171 return adjuster.refTextureProxyForParams(params, scaleAdjust);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400172}
173
174GrBackendTexture SkImage_GpuBase::onGetBackendTexture(bool flushPendingGrContextIO,
175 GrSurfaceOrigin* origin) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500176 auto direct = fContext->priv().asDirectContext();
177 if (!direct) {
178 // This image was created with a DDL context and cannot be instantiated.
Robert Phillips6603a172019-03-05 12:35:44 -0500179 return GrBackendTexture(); // invalid
Robert Phillips920d4882019-03-04 15:16:44 -0500180 }
181
Robert Phillips6603a172019-03-05 12:35:44 -0500182 sk_sp<GrTextureProxy> proxy = this->asTextureProxyRef(direct);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400183 SkASSERT(proxy);
184
Robert Phillips920d4882019-03-04 15:16:44 -0500185 if (!proxy->isInstantiated()) {
186 auto resourceProvider = direct->priv().resourceProvider();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400187
Robert Phillips920d4882019-03-04 15:16:44 -0500188 if (!proxy->instantiate(resourceProvider)) {
189 return GrBackendTexture(); // invalid
190 }
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400191 }
192
193 GrTexture* texture = proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400194 if (texture) {
195 if (flushPendingGrContextIO) {
Greg Daniel4aa13e72019-04-15 14:42:20 -0400196 direct->priv().flushSurface(proxy.get());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400197 }
198 if (origin) {
199 *origin = proxy->origin();
200 }
201 return texture->getBackendTexture();
202 }
203 return GrBackendTexture(); // invalid
204}
205
206GrTexture* SkImage_GpuBase::onGetTexture() const {
207 GrTextureProxy* proxy = this->peekProxy();
Robert Phillips193c4212019-03-04 12:18:53 -0500208 if (proxy && proxy->isInstantiated()) {
209 return proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400210 }
211
Robert Phillips193c4212019-03-04 12:18:53 -0500212 auto direct = fContext->priv().asDirectContext();
213 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400214 // This image was created with a DDL context and cannot be instantiated.
215 return nullptr;
216 }
217
Robert Phillips6603a172019-03-05 12:35:44 -0500218 sk_sp<GrTextureProxy> proxyRef = this->asTextureProxyRef(direct);
Robert Phillips193c4212019-03-04 12:18:53 -0500219 SkASSERT(proxyRef && !proxyRef->isInstantiated());
220
221 if (!proxyRef->instantiate(direct->priv().resourceProvider())) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400222 return nullptr;
223 }
224
Robert Phillips193c4212019-03-04 12:18:53 -0500225 return proxyRef->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400226}
227
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400228bool SkImage_GpuBase::onIsValid(GrContext* context) const {
229 // The base class has already checked that context isn't abandoned (if it's not nullptr)
Robert Phillips920d4882019-03-04 15:16:44 -0500230 if (fContext->priv().abandoned()) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400231 return false;
232 }
233
Robert Phillips920d4882019-03-04 15:16:44 -0500234 if (context && !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400235 return false;
236 }
237
238 return true;
239}
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400240
Jim Van Verth0e671942018-11-09 12:03:57 -0500241bool SkImage_GpuBase::MakeTempTextureProxies(GrContext* ctx, const GrBackendTexture yuvaTextures[],
Jim Van Verth53275362018-11-09 15:42:35 -0500242 int numTextures, const SkYUVAIndex yuvaIndices[4],
243 GrSurfaceOrigin imageOrigin,
Jim Van Verth0e671942018-11-09 12:03:57 -0500244 sk_sp<GrTextureProxy> tempTextureProxies[4]) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500245 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400246 const GrCaps* caps = ctx->priv().caps();
Jim Van Verth0e671942018-11-09 12:03:57 -0500247
Jim Van Verth0e671942018-11-09 12:03:57 -0500248 for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) {
Robert Phillips62221e72019-07-24 15:07:38 -0400249 GrBackendFormat backendFormat = yuvaTextures[textureIndex].getBackendFormat();
Brian Salomonf391d0f2018-12-14 09:18:50 -0500250 if (!backendFormat.isValid()) {
251 return false;
252 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400253
Robert Phillips00c9f0d2019-08-02 17:17:35 -0400254 GrColorType grColorType = caps->getYUVAColorTypeFromBackendFormat(
255 backendFormat,
256 yuvaIndices[3].fIndex == textureIndex);
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400257 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 Salomonbeb7f522019-08-30 16:19:42 -0400402 GrSurfaceProxy::LazyCallbackResult operator()(GrResourceProvider* resourceProvider) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400403 // 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 Salomonbeb7f522019-08-30 16:19:42 -0400408 // In order to make the SkImage "thread safe" we rely on holding an extra ref to the
409 // texture in the callback and signalling the unref via a message to the resource cache.
410 // We need to extend the callback's lifetime to that of the proxy.
411 static constexpr auto kReleaseCallbackOnInstantiation = false;
412
Brian Salomon876a0172019-03-08 11:12:14 -0500413 // Our proxy is getting instantiated for the second+ time. We are only allowed to call
414 // Fulfill once. So return our cached result.
415 if (fTexture) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400416 return {sk_ref_sp(fTexture), kReleaseCallbackOnInstantiation, kKeySyncMode};
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400417 } else if (fColorType == GrColorType::kUnknown) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400418 // We've already called fulfill and it failed. Our contract says that we should only
419 // call each callback once.
420 return {};
Brian Salomon876a0172019-03-08 11:12:14 -0500421 }
Brian Salomone80b8092019-03-08 13:25:19 -0500422 SkASSERT(fDoneCallback);
423 PromiseImageTextureContext textureContext = fDoneCallback->context();
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500424 sk_sp<SkPromiseImageTexture> promiseTexture = fFulfillProc(textureContext);
425 // From here on out our contract is that the release proc must be called, even if
426 // the return from fulfill was invalid or we fail for some other reason.
Brian Salomone80b8092019-03-08 13:25:19 -0500427 auto releaseCallback = sk_make_sp<GrRefCntedCallback>(fReleaseProc, textureContext);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500428 if (!promiseTexture) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400429 // This records that we have failed.
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400430 fColorType = GrColorType::kUnknown;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400431 return {};
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500432 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500433
Robert Phillips62221e72019-07-24 15:07:38 -0400434 const GrBackendTexture& backendTexture = promiseTexture->backendTexture();
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500435 if (!backendTexture.isValid()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400436 return {};
Brian Salomon559c6172019-01-10 10:23:44 -0500437 }
438
Brian Salomonf55e8d52019-01-30 17:28:20 -0500439 sk_sp<GrTexture> tex;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500440 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Salomon7d88f312019-02-28 10:03:03 -0500441 GrUniqueKey key;
442 GrUniqueKey::Builder builder(&key, kDomain, 2, "promise");
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500443 builder[0] = promiseTexture->uniqueID();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400444 builder[1] = (uint32_t) fColorType;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500445 builder.finish();
Brian Salomon0d606762019-01-25 09:58:38 -0500446 // A texture with this key may already exist from a different instance of this lazy
447 // callback. This could happen if the client fulfills a promise image with a texture
448 // that was previously used to fulfill a different promise image.
Brian Salomon7d88f312019-02-28 10:03:03 -0500449 if (auto surf = resourceProvider->findByUniqueKey<GrSurface>(key)) {
Brian Salomon0d606762019-01-25 09:58:38 -0500450 tex = sk_ref_sp(surf->asTexture());
451 SkASSERT(tex);
452 } else {
453 if ((tex = resourceProvider->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400454 backendTexture, fColorType, kBorrow_GrWrapOwnership,
455 GrWrapCacheable::kYes, kRead_GrIOType))) {
Brian Salomon7d88f312019-02-28 10:03:03 -0500456 tex->resourcePriv().setUniqueKey(key);
Brian Salomon0d606762019-01-25 09:58:38 -0500457 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400458 return {};
Brian Salomon0d606762019-01-25 09:58:38 -0500459 }
460 }
Brian Salomon0cc57542019-03-08 13:28:46 -0500461 auto releaseIdleState = fVersion == PromiseImageApiVersion::kLegacy
462 ? GrTexture::IdleState::kFinished
463 : GrTexture::IdleState::kFlushed;
464 tex->addIdleProc(std::move(releaseCallback), releaseIdleState);
Brian Salomone80b8092019-03-08 13:25:19 -0500465 tex->addIdleProc(std::move(fDoneCallback), GrTexture::IdleState::kFinished);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500466 promiseTexture->addKeyToInvalidate(tex->getContext()->priv().contextID(), key);
Brian Salomon876a0172019-03-08 11:12:14 -0500467 fTexture = tex.get();
468 // We need to hold on to the GrTexture in case our proxy gets reinstantiated. However,
469 // we can't unref in our destructor because we may be on another thread then. So we
470 // let the cache know it is waiting on an unref message. We will send that message from
471 // our destructor.
472 GrContext* context = fTexture->getContext();
473 context->priv().getResourceCache()->insertDelayedResourceUnref(fTexture);
474 fTextureContextID = context->priv().contextID();
Brian Salomonbeb7f522019-08-30 16:19:42 -0400475 return {std::move(tex), kReleaseCallbackOnInstantiation, kKeySyncMode};
Brian Salomonbe5a0932018-12-10 10:03:26 -0500476 }
477
478 private:
Brian Salomone80b8092019-03-08 13:25:19 -0500479 PromiseImageTextureFulfillProc fFulfillProc;
480 PromiseImageTextureReleaseProc fReleaseProc;
481 sk_sp<GrRefCntedCallback> fDoneCallback;
Brian Salomon876a0172019-03-08 11:12:14 -0500482 GrTexture* fTexture = nullptr;
483 uint32_t fTextureContextID = SK_InvalidUniqueID;
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400484 GrColorType fColorType;
Brian Salomon0cc57542019-03-08 13:28:46 -0500485 PromiseImageApiVersion fVersion;
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400486 } callback(fulfillProc, releaseProc, doneProc, textureContext, colorType, version);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500487
Robert Phillips9da87e02019-02-04 13:26:26 -0500488 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonbe5a0932018-12-10 10:03:26 -0500489
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400490 GrPixelConfig config = context->priv().caps()->getConfigFromBackendFormat(
491 backendFormat,
492 colorType);
493
Brian Salomonbe5a0932018-12-10 10:03:26 -0500494 GrSurfaceDesc desc;
495 desc.fWidth = width;
496 desc.fHeight = height;
497 desc.fConfig = config;
498
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600499 // Ganesh assumes that, when wrapping a mipmapped backend texture from a client, that its
500 // mipmaps are fully fleshed out.
501 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
502 ? GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
503
Brian Salomonbe5a0932018-12-10 10:03:26 -0500504 // We pass kReadOnly here since we should treat content of the client's texture as immutable.
Brian Salomone8a766b2019-07-19 14:24:36 -0400505 // The promise API provides no way for the client to indicated that the texture is protected.
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400506 return proxyProvider->createLazyProxy(
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400507 std::move(callback), backendFormat, desc, GrRenderable::kNo, 1, origin, mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600508 mipMapsStatus, GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, SkBudgeted::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400509 GrProtected::kNo, GrSurfaceProxy::UseAllocator::kYes);
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400510}