blob: 0d2fb5e47ac7e92ea9bdd84adc00a5bbc239c84c [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 Phillips00c9f0d2019-08-02 17:17:35 -0400256 GrColorType grColorType = caps->getYUVAColorTypeFromBackendFormat(
257 backendFormat,
258 yuvaIndices[3].fIndex == textureIndex);
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400259 if (GrColorType::kUnknown == grColorType) {
260 return false;
261 }
262
Robert Phillips62221e72019-07-24 15:07:38 -0400263 SkASSERT(yuvaTextures[textureIndex].isValid());
Jim Van Verth0e671942018-11-09 12:03:57 -0500264
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500265 tempTextureProxies[textureIndex] = proxyProvider->wrapBackendTexture(
Robert Phillips62221e72019-07-24 15:07:38 -0400266 yuvaTextures[textureIndex], grColorType, imageOrigin, kBorrow_GrWrapOwnership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500267 GrWrapCacheable::kNo, kRead_GrIOType);
Jim Van Verth0e671942018-11-09 12:03:57 -0500268 if (!tempTextureProxies[textureIndex]) {
269 return false;
270 }
Jim Van Verth53275362018-11-09 15:42:35 -0500271
272 // Check that each texture contains the channel data for the corresponding YUVA index
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400273 auto componentFlags = GrColorTypeComponentFlags(grColorType);
Jim Van Verth53275362018-11-09 15:42:35 -0500274 for (int yuvaIndex = 0; yuvaIndex < SkYUVAIndex::kIndexCount; ++yuvaIndex) {
275 if (yuvaIndices[yuvaIndex].fIndex == textureIndex) {
276 switch (yuvaIndices[yuvaIndex].fChannel) {
277 case SkColorChannel::kR:
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400278 // TODO: Chrome needs to be patched before this can be
279 // enforced.
280// if (!(kRed_SkColorTypeComponentFlag & componentFlags)) {
281// return false;
282// }
283 break;
284 case SkColorChannel::kG:
285 if (!(kGreen_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500286 return false;
287 }
288 break;
Jim Van Verth53275362018-11-09 15:42:35 -0500289 case SkColorChannel::kB:
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400290 if (!(kBlue_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500291 return false;
292 }
293 break;
294 case SkColorChannel::kA:
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400295 if (!(kAlpha_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500296 return false;
297 }
298 break;
299 }
300 }
301 }
Jim Van Verth0e671942018-11-09 12:03:57 -0500302 }
303
304 return true;
305}
306
307bool SkImage_GpuBase::RenderYUVAToRGBA(GrContext* ctx, GrRenderTargetContext* renderTargetContext,
308 const SkRect& rect, SkYUVColorSpace yuvColorSpace,
Brian Osmane9560492019-02-05 17:00:03 -0500309 sk_sp<GrColorSpaceXform> colorSpaceXform,
Jim Van Verth0e671942018-11-09 12:03:57 -0500310 const sk_sp<GrTextureProxy> proxies[4],
311 const SkYUVAIndex yuvaIndices[4]) {
312 SkASSERT(renderTargetContext);
313 if (!renderTargetContext->asSurfaceProxy()) {
314 return false;
315 }
316
317 GrPaint paint;
318 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
319
Brian Osmane9560492019-02-05 17:00:03 -0500320 auto fp = GrYUVtoRGBEffect::Make(proxies, yuvaIndices, yuvColorSpace,
321 GrSamplerState::Filter::kNearest);
322 if (colorSpaceXform) {
323 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(colorSpaceXform));
324 }
325 paint.addColorFragmentProcessor(std::move(fp));
Jim Van Verth0e671942018-11-09 12:03:57 -0500326
327 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Jim Van Verth0e671942018-11-09 12:03:57 -0500328 return true;
329}
330
Brian Salomonbe5a0932018-12-10 10:03:26 -0500331sk_sp<GrTextureProxy> SkImage_GpuBase::MakePromiseImageLazyProxy(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400332 GrContext* context, int width, int height, GrSurfaceOrigin origin, GrColorType colorType,
Brian Salomonbe5a0932018-12-10 10:03:26 -0500333 GrBackendFormat backendFormat, GrMipMapped mipMapped,
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500334 PromiseImageTextureFulfillProc fulfillProc,
335 PromiseImageTextureReleaseProc releaseProc,
336 PromiseImageTextureDoneProc doneProc,
Brian Salomon0cc57542019-03-08 13:28:46 -0500337 PromiseImageTextureContext textureContext,
338 PromiseImageApiVersion version) {
Brian Salomonbe5a0932018-12-10 10:03:26 -0500339 SkASSERT(context);
340 SkASSERT(width > 0 && height > 0);
341 SkASSERT(doneProc);
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400342 SkASSERT(colorType != GrColorType::kUnknown);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500343
344 if (!fulfillProc || !releaseProc) {
345 doneProc(textureContext);
346 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400347 }
348
Brian Salomonbe5a0932018-12-10 10:03:26 -0500349 if (mipMapped == GrMipMapped::kYes &&
350 GrTextureTypeHasRestrictedSampling(backendFormat.textureType())) {
351 // It is invalid to have a GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE and have mips as
352 // well.
353 doneProc(textureContext);
354 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400355 }
Brian Salomonbe5a0932018-12-10 10:03:26 -0500356
357 /**
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500358 * This class is the lazy instantiation callback for promise images. It manages calling the
359 * client's Fulfill, Release, and Done procs. It attempts to reuse a GrTexture instance in
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500360 * cases where the client provides the same SkPromiseImageTexture as Fulfill results for
361 * multiple SkImages. The created GrTexture is given a key based on a unique ID associated with
362 * the SkPromiseImageTexture.
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500363 *
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500364 * The GrTexutre idle proc mechanism is used to call the Release and Done procs. We use this
365 * instead of the GrSurface release proc because the GrTexture is cached and therefore may
366 * outlive the proxy into which this callback is installed.
367 *
368 * A key invalidation message is installed on the SkPromiseImageTexture so that the GrTexture
369 * is deleted once it can no longer be used to instantiate a proxy.
Brian Salomonbe5a0932018-12-10 10:03:26 -0500370 */
371 class PromiseLazyInstantiateCallback {
372 public:
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500373 PromiseLazyInstantiateCallback(PromiseImageTextureFulfillProc fulfillProc,
374 PromiseImageTextureReleaseProc releaseProc,
375 PromiseImageTextureDoneProc doneProc,
376 PromiseImageTextureContext context,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400377 GrColorType colorType,
Brian Salomon0cc57542019-03-08 13:28:46 -0500378 PromiseImageApiVersion version)
379 : fFulfillProc(fulfillProc)
380 , fReleaseProc(releaseProc)
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400381 , fColorType(colorType)
Brian Salomon0cc57542019-03-08 13:28:46 -0500382 , fVersion(version) {
Brian Salomone80b8092019-03-08 13:25:19 -0500383 fDoneCallback = sk_make_sp<GrRefCntedCallback>(doneProc, context);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500384 }
385 PromiseLazyInstantiateCallback(PromiseLazyInstantiateCallback&&) = default;
386 PromiseLazyInstantiateCallback(const PromiseLazyInstantiateCallback&) {
387 // Because we get wrapped in std::function we must be copyable. But we should never
388 // be copied.
389 SkASSERT(false);
390 }
391 PromiseLazyInstantiateCallback& operator=(PromiseLazyInstantiateCallback&&) = default;
392 PromiseLazyInstantiateCallback& operator=(const PromiseLazyInstantiateCallback&) {
393 SkASSERT(false);
394 return *this;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500395 }
Brian Salomon553610d2019-01-14 17:34:12 -0500396
Brian Salomon88b8d112019-03-07 15:25:34 +0000397 ~PromiseLazyInstantiateCallback() {
Brian Salomon876a0172019-03-08 11:12:14 -0500398 // Our destructor can run on any thread. We trigger the unref of fTexture by message.
399 if (fTexture) {
Brian Salomon876a0172019-03-08 11:12:14 -0500400 SkMessageBus<GrGpuResourceFreedMessage>::Post({fTexture, fTextureContextID});
401 }
Brian Salomon88b8d112019-03-07 15:25:34 +0000402 }
403
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400404 GrSurfaceProxy::LazyInstantiationResult operator()(GrResourceProvider* resourceProvider) {
405 // We use the unique key in a way that is unrelated to the SkImage-based key that the
406 // proxy may receive, hence kUnsynced.
407 static constexpr auto kKeySyncMode =
408 GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
409
Brian Salomon876a0172019-03-08 11:12:14 -0500410 // Our proxy is getting instantiated for the second+ time. We are only allowed to call
411 // Fulfill once. So return our cached result.
412 if (fTexture) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400413 return {sk_ref_sp(fTexture), kKeySyncMode};
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400414 } else if (fColorType == GrColorType::kUnknown) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400415 // We've already called fulfill and it failed. Our contract says that we should only
416 // call each callback once.
417 return {};
Brian Salomon876a0172019-03-08 11:12:14 -0500418 }
Brian Salomone80b8092019-03-08 13:25:19 -0500419 SkASSERT(fDoneCallback);
420 PromiseImageTextureContext textureContext = fDoneCallback->context();
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500421 sk_sp<SkPromiseImageTexture> promiseTexture = fFulfillProc(textureContext);
422 // From here on out our contract is that the release proc must be called, even if
423 // the return from fulfill was invalid or we fail for some other reason.
Brian Salomone80b8092019-03-08 13:25:19 -0500424 auto releaseCallback = sk_make_sp<GrRefCntedCallback>(fReleaseProc, textureContext);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500425 if (!promiseTexture) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400426 // This records that we have failed.
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400427 fColorType = GrColorType::kUnknown;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400428 return {};
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500429 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500430
Robert Phillips62221e72019-07-24 15:07:38 -0400431 const GrBackendTexture& backendTexture = promiseTexture->backendTexture();
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500432 if (!backendTexture.isValid()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400433 return {};
Brian Salomon559c6172019-01-10 10:23:44 -0500434 }
435
Brian Salomonf55e8d52019-01-30 17:28:20 -0500436 sk_sp<GrTexture> tex;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500437 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Salomon7d88f312019-02-28 10:03:03 -0500438 GrUniqueKey key;
439 GrUniqueKey::Builder builder(&key, kDomain, 2, "promise");
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500440 builder[0] = promiseTexture->uniqueID();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400441 builder[1] = (uint32_t) fColorType;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500442 builder.finish();
Brian Salomon0d606762019-01-25 09:58:38 -0500443 // A texture with this key may already exist from a different instance of this lazy
444 // callback. This could happen if the client fulfills a promise image with a texture
445 // that was previously used to fulfill a different promise image.
Brian Salomon7d88f312019-02-28 10:03:03 -0500446 if (auto surf = resourceProvider->findByUniqueKey<GrSurface>(key)) {
Brian Salomon0d606762019-01-25 09:58:38 -0500447 tex = sk_ref_sp(surf->asTexture());
448 SkASSERT(tex);
449 } else {
450 if ((tex = resourceProvider->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400451 backendTexture, fColorType, kBorrow_GrWrapOwnership,
452 GrWrapCacheable::kYes, kRead_GrIOType))) {
Brian Salomon7d88f312019-02-28 10:03:03 -0500453 tex->resourcePriv().setUniqueKey(key);
Brian Salomon0d606762019-01-25 09:58:38 -0500454 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400455 return {};
Brian Salomon0d606762019-01-25 09:58:38 -0500456 }
457 }
Brian Salomon0cc57542019-03-08 13:28:46 -0500458 auto releaseIdleState = fVersion == PromiseImageApiVersion::kLegacy
459 ? GrTexture::IdleState::kFinished
460 : GrTexture::IdleState::kFlushed;
461 tex->addIdleProc(std::move(releaseCallback), releaseIdleState);
Brian Salomone80b8092019-03-08 13:25:19 -0500462 tex->addIdleProc(std::move(fDoneCallback), GrTexture::IdleState::kFinished);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500463 promiseTexture->addKeyToInvalidate(tex->getContext()->priv().contextID(), key);
Brian Salomon876a0172019-03-08 11:12:14 -0500464 fTexture = tex.get();
465 // We need to hold on to the GrTexture in case our proxy gets reinstantiated. However,
466 // we can't unref in our destructor because we may be on another thread then. So we
467 // let the cache know it is waiting on an unref message. We will send that message from
468 // our destructor.
469 GrContext* context = fTexture->getContext();
470 context->priv().getResourceCache()->insertDelayedResourceUnref(fTexture);
471 fTextureContextID = context->priv().contextID();
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400472 return {std::move(tex), kKeySyncMode};
Brian Salomonbe5a0932018-12-10 10:03:26 -0500473 }
474
475 private:
Brian Salomone80b8092019-03-08 13:25:19 -0500476 PromiseImageTextureFulfillProc fFulfillProc;
477 PromiseImageTextureReleaseProc fReleaseProc;
478 sk_sp<GrRefCntedCallback> fDoneCallback;
Brian Salomon876a0172019-03-08 11:12:14 -0500479 GrTexture* fTexture = nullptr;
480 uint32_t fTextureContextID = SK_InvalidUniqueID;
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400481 GrColorType fColorType;
Brian Salomon0cc57542019-03-08 13:28:46 -0500482 PromiseImageApiVersion fVersion;
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400483 } callback(fulfillProc, releaseProc, doneProc, textureContext, colorType, version);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500484
Robert Phillips9da87e02019-02-04 13:26:26 -0500485 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonbe5a0932018-12-10 10:03:26 -0500486
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400487 GrPixelConfig config = context->priv().caps()->getConfigFromBackendFormat(
488 backendFormat,
489 colorType);
490
Brian Salomonbe5a0932018-12-10 10:03:26 -0500491 GrSurfaceDesc desc;
492 desc.fWidth = width;
493 desc.fHeight = height;
494 desc.fConfig = config;
495
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600496 // Ganesh assumes that, when wrapping a mipmapped backend texture from a client, that its
497 // mipmaps are fully fleshed out.
498 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
499 ? GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
500
Brian Salomonbe5a0932018-12-10 10:03:26 -0500501 // We pass kReadOnly here since we should treat content of the client's texture as immutable.
Brian Salomone8a766b2019-07-19 14:24:36 -0400502 // The promise API provides no way for the client to indicated that the texture is protected.
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400503 return proxyProvider->createLazyProxy(
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400504 std::move(callback), backendFormat, desc, GrRenderable::kNo, 1, origin, mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600505 mipMapsStatus, GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, SkBudgeted::kNo,
Brian Salomone8a766b2019-07-19 14:24:36 -0400506 GrProtected::kNo, GrSurfaceProxy::LazyInstantiationType::kDeinstantiate);
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400507}