blob: 201daad704c30ae3dd7da88b56ecd4ef8fa5a15a [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,
Robert Phillipsc80b0e92019-07-23 10:27:09 -040042 GrPixelConfig* config, GrColorType grCT,
43 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 Phillips1cd1ed82019-07-23 13:21:01 -040059 if (!ctx->priv().caps()->areColorTypeAndFormatCompatible(grCT, backendFormat)) {
60 return false;
61 }
62
Greg Daniele877dce2019-07-11 10:52:43 -040063 *config = ctx->priv().caps()->getConfigFromBackendFormat(backendFormat, grCT);
Brian Salomonf391d0f2018-12-14 09:18:50 -050064 return *config != kUnknown_GrPixelConfig;
Jim Van Verth8026ccc2018-10-04 13:10:39 -040065}
66
67//////////////////////////////////////////////////////////////////////////////////////////////////
68
Brian Osmane50cdf02018-10-19 13:02:14 -040069bool SkImage_GpuBase::getROPixels(SkBitmap* dst, CachingHint chint) const {
Robert Phillips920d4882019-03-04 15:16:44 -050070 auto direct = fContext->priv().asDirectContext();
71 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040072 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
73 return false;
74 }
75
Jim Van Verth8026ccc2018-10-04 13:10:39 -040076 const auto desc = SkBitmapCacheDesc::Make(this);
77 if (SkBitmapCache::Find(desc, dst)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040078 SkASSERT(dst->isImmutable());
79 SkASSERT(dst->getPixels());
80 return true;
81 }
82
83 SkBitmapCache::RecPtr rec = nullptr;
84 SkPixmap pmap;
85 if (kAllow_CachingHint == chint) {
Brian Salomon5ad6fd32019-03-21 15:30:08 -040086 rec = SkBitmapCache::Alloc(desc, this->imageInfo(), &pmap);
Jim Van Verth8026ccc2018-10-04 13:10:39 -040087 if (!rec) {
88 return false;
89 }
90 } else {
Brian Salomon5ad6fd32019-03-21 15:30:08 -040091 if (!dst->tryAllocPixels(this->imageInfo()) || !dst->peekPixels(&pmap)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040092 return false;
93 }
94 }
95
Brian Salomond6287472019-06-24 15:50:07 -040096 sk_sp<GrSurfaceContext> sContext =
97 direct->priv().makeWrappedSurfaceContext(this->asTextureProxyRef(direct),
98 SkColorTypeToGrColorType(this->colorType()),
99 this->alphaType(),
100 this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400101 if (!sContext) {
102 return false;
103 }
104
Brian Salomon1d435302019-07-01 13:05:28 -0400105 if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), {0, 0})) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400106 return false;
107 }
108
109 if (rec) {
110 SkBitmapCache::Add(std::move(rec), dst);
111 this->notifyAddedToRasterCache();
112 }
113 return true;
114}
115
Robert Phillips6603a172019-03-05 12:35:44 -0500116sk_sp<SkImage> SkImage_GpuBase::onMakeSubset(GrRecordingContext* context,
117 const SkIRect& subset) const {
118 if (!context || !fContext->priv().matches(context)) {
119 return nullptr;
120 }
121
122 sk_sp<GrSurfaceProxy> proxy = this->asTextureProxyRef(context);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400123
Greg Daniel46cfbc62019-06-07 11:43:30 -0400124 sk_sp<GrTextureProxy> copyProxy = GrSurfaceProxy::Copy(
125 context, proxy.get(), GrMipMapped::kNo, subset, SkBackingFit::kExact,
126 proxy->isBudgeted());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400127
Greg Daniel46cfbc62019-06-07 11:43:30 -0400128 if (!copyProxy) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400129 return nullptr;
130 }
131
132 // MDB: this call is okay bc we know 'sContext' was kExact
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400133 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, this->alphaType(),
Greg Daniel46cfbc62019-06-07 11:43:30 -0400134 std::move(copyProxy), this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400135}
136
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400137bool SkImage_GpuBase::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
138 int srcX, int srcY, CachingHint) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500139 auto direct = fContext->priv().asDirectContext();
140 if (!direct) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400141 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
142 return false;
143 }
144
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400145 if (!SkImageInfoValidConversion(dstInfo, this->imageInfo())) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400146 return false;
147 }
148
Robert Phillips920d4882019-03-04 15:16:44 -0500149 sk_sp<GrSurfaceContext> sContext = direct->priv().makeWrappedSurfaceContext(
Brian Salomond6287472019-06-24 15:50:07 -0400150 this->asTextureProxyRef(direct), SkColorTypeToGrColorType(this->colorType()),
151 this->alphaType(), this->refColorSpace());
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400152 if (!sContext) {
153 return false;
154 }
155
Brian Salomon1d435302019-07-01 13:05:28 -0400156 return sContext->readPixels(dstInfo, dstPixels, dstRB, {srcX, srcY});
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400157}
158
Robert Phillips9338c602019-02-19 12:52:29 -0500159sk_sp<GrTextureProxy> SkImage_GpuBase::asTextureProxyRef(GrRecordingContext* context,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400160 const GrSamplerState& params,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400161 SkScalar scaleAdjust[2]) const {
Robert Phillips6603a172019-03-05 12:35:44 -0500162 if (!context || !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400163 SkASSERT(0);
164 return nullptr;
165 }
166
Brian Salomond6287472019-06-24 15:50:07 -0400167 GrTextureAdjuster adjuster(fContext.get(), this->asTextureProxyRef(context),
168 SkColorTypeToGrColorType(this->colorType()), this->alphaType(),
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400169 this->uniqueID(), this->colorSpace());
Brian Osman6064e1c2018-10-19 14:27:54 -0400170 return adjuster.refTextureProxyForParams(params, scaleAdjust);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400171}
172
173GrBackendTexture SkImage_GpuBase::onGetBackendTexture(bool flushPendingGrContextIO,
174 GrSurfaceOrigin* origin) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500175 auto direct = fContext->priv().asDirectContext();
176 if (!direct) {
177 // This image was created with a DDL context and cannot be instantiated.
Robert Phillips6603a172019-03-05 12:35:44 -0500178 return GrBackendTexture(); // invalid
Robert Phillips920d4882019-03-04 15:16:44 -0500179 }
180
Robert Phillips6603a172019-03-05 12:35:44 -0500181 sk_sp<GrTextureProxy> proxy = this->asTextureProxyRef(direct);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400182 SkASSERT(proxy);
183
Robert Phillips920d4882019-03-04 15:16:44 -0500184 if (!proxy->isInstantiated()) {
185 auto resourceProvider = direct->priv().resourceProvider();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400186
Robert Phillips920d4882019-03-04 15:16:44 -0500187 if (!proxy->instantiate(resourceProvider)) {
188 return GrBackendTexture(); // invalid
189 }
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400190 }
191
192 GrTexture* texture = proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400193 if (texture) {
194 if (flushPendingGrContextIO) {
Greg Daniel4aa13e72019-04-15 14:42:20 -0400195 direct->priv().flushSurface(proxy.get());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400196 }
197 if (origin) {
198 *origin = proxy->origin();
199 }
200 return texture->getBackendTexture();
201 }
202 return GrBackendTexture(); // invalid
203}
204
205GrTexture* SkImage_GpuBase::onGetTexture() const {
206 GrTextureProxy* proxy = this->peekProxy();
Robert Phillips193c4212019-03-04 12:18:53 -0500207 if (proxy && proxy->isInstantiated()) {
208 return proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400209 }
210
Robert Phillips193c4212019-03-04 12:18:53 -0500211 auto direct = fContext->priv().asDirectContext();
212 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400213 // This image was created with a DDL context and cannot be instantiated.
214 return nullptr;
215 }
216
Robert Phillips6603a172019-03-05 12:35:44 -0500217 sk_sp<GrTextureProxy> proxyRef = this->asTextureProxyRef(direct);
Robert Phillips193c4212019-03-04 12:18:53 -0500218 SkASSERT(proxyRef && !proxyRef->isInstantiated());
219
220 if (!proxyRef->instantiate(direct->priv().resourceProvider())) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400221 return nullptr;
222 }
223
Robert Phillips193c4212019-03-04 12:18:53 -0500224 return proxyRef->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400225}
226
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400227bool SkImage_GpuBase::onIsValid(GrContext* context) const {
228 // The base class has already checked that context isn't abandoned (if it's not nullptr)
Robert Phillips920d4882019-03-04 15:16:44 -0500229 if (fContext->priv().abandoned()) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400230 return false;
231 }
232
Robert Phillips920d4882019-03-04 15:16:44 -0500233 if (context && !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400234 return false;
235 }
236
237 return true;
238}
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400239
Jim Van Verth0e671942018-11-09 12:03:57 -0500240bool SkImage_GpuBase::MakeTempTextureProxies(GrContext* ctx, const GrBackendTexture yuvaTextures[],
Jim Van Verth53275362018-11-09 15:42:35 -0500241 int numTextures, const SkYUVAIndex yuvaIndices[4],
242 GrSurfaceOrigin imageOrigin,
Jim Van Verth0e671942018-11-09 12:03:57 -0500243 sk_sp<GrTextureProxy> tempTextureProxies[4]) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500244 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400245 const GrCaps* caps = ctx->priv().caps();
Jim Van Verth0e671942018-11-09 12:03:57 -0500246
247 // We need to make a copy of the input backend textures because we need to preserve the result
248 // of validate_backend_texture.
249 GrBackendTexture yuvaTexturesCopy[4];
250 for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) {
251 yuvaTexturesCopy[textureIndex] = yuvaTextures[textureIndex];
Brian Salomonf391d0f2018-12-14 09:18:50 -0500252 GrBackendFormat backendFormat = yuvaTexturesCopy[textureIndex].getBackendFormat();
253 if (!backendFormat.isValid()) {
254 return false;
255 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400256
Brian Salomonf391d0f2018-12-14 09:18:50 -0500257 yuvaTexturesCopy[textureIndex].fConfig =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400258 caps->getYUVAConfigFromBackendFormat(backendFormat);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500259 if (yuvaTexturesCopy[textureIndex].fConfig == kUnknown_GrPixelConfig) {
Jim Van Verth0e671942018-11-09 12:03:57 -0500260 return false;
261 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400262 GrColorType grColorType = caps->getYUVAColorTypeFromBackendFormat(backendFormat);
263 if (GrColorType::kUnknown == grColorType) {
264 return false;
265 }
266
Jim Van Verth0e671942018-11-09 12:03:57 -0500267 SkASSERT(yuvaTexturesCopy[textureIndex].isValid());
268
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500269 tempTextureProxies[textureIndex] = proxyProvider->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400270 yuvaTexturesCopy[textureIndex], grColorType, imageOrigin, kBorrow_GrWrapOwnership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500271 GrWrapCacheable::kNo, kRead_GrIOType);
Jim Van Verth0e671942018-11-09 12:03:57 -0500272 if (!tempTextureProxies[textureIndex]) {
273 return false;
274 }
Jim Van Verth53275362018-11-09 15:42:35 -0500275
276 // Check that each texture contains the channel data for the corresponding YUVA index
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400277 auto componentFlags = GrColorTypeComponentFlags(grColorType);
Jim Van Verth53275362018-11-09 15:42:35 -0500278 for (int yuvaIndex = 0; yuvaIndex < SkYUVAIndex::kIndexCount; ++yuvaIndex) {
279 if (yuvaIndices[yuvaIndex].fIndex == textureIndex) {
280 switch (yuvaIndices[yuvaIndex].fChannel) {
281 case SkColorChannel::kR:
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400282 // TODO: Chrome needs to be patched before this can be
283 // enforced.
284// if (!(kRed_SkColorTypeComponentFlag & componentFlags)) {
285// return false;
286// }
287 break;
288 case SkColorChannel::kG:
289 if (!(kGreen_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500290 return false;
291 }
292 break;
Jim Van Verth53275362018-11-09 15:42:35 -0500293 case SkColorChannel::kB:
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400294 if (!(kBlue_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500295 return false;
296 }
297 break;
298 case SkColorChannel::kA:
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400299 if (!(kAlpha_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500300 return false;
301 }
302 break;
303 }
304 }
305 }
Jim Van Verth0e671942018-11-09 12:03:57 -0500306 }
307
308 return true;
309}
310
311bool SkImage_GpuBase::RenderYUVAToRGBA(GrContext* ctx, GrRenderTargetContext* renderTargetContext,
312 const SkRect& rect, SkYUVColorSpace yuvColorSpace,
Brian Osmane9560492019-02-05 17:00:03 -0500313 sk_sp<GrColorSpaceXform> colorSpaceXform,
Jim Van Verth0e671942018-11-09 12:03:57 -0500314 const sk_sp<GrTextureProxy> proxies[4],
315 const SkYUVAIndex yuvaIndices[4]) {
316 SkASSERT(renderTargetContext);
317 if (!renderTargetContext->asSurfaceProxy()) {
318 return false;
319 }
320
321 GrPaint paint;
322 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
323
Brian Osmane9560492019-02-05 17:00:03 -0500324 auto fp = GrYUVtoRGBEffect::Make(proxies, yuvaIndices, yuvColorSpace,
325 GrSamplerState::Filter::kNearest);
326 if (colorSpaceXform) {
327 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(colorSpaceXform));
328 }
329 paint.addColorFragmentProcessor(std::move(fp));
Jim Van Verth0e671942018-11-09 12:03:57 -0500330
331 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Jim Van Verth0e671942018-11-09 12:03:57 -0500332 return true;
333}
334
Brian Salomonbe5a0932018-12-10 10:03:26 -0500335sk_sp<GrTextureProxy> SkImage_GpuBase::MakePromiseImageLazyProxy(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400336 GrContext* context, int width, int height, GrSurfaceOrigin origin, GrColorType colorType,
Brian Salomonbe5a0932018-12-10 10:03:26 -0500337 GrBackendFormat backendFormat, GrMipMapped mipMapped,
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500338 PromiseImageTextureFulfillProc fulfillProc,
339 PromiseImageTextureReleaseProc releaseProc,
340 PromiseImageTextureDoneProc doneProc,
Brian Salomon0cc57542019-03-08 13:28:46 -0500341 PromiseImageTextureContext textureContext,
342 PromiseImageApiVersion version) {
Brian Salomonbe5a0932018-12-10 10:03:26 -0500343 SkASSERT(context);
344 SkASSERT(width > 0 && height > 0);
345 SkASSERT(doneProc);
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400346 SkASSERT(colorType != GrColorType::kUnknown);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500347
348 if (!fulfillProc || !releaseProc) {
349 doneProc(textureContext);
350 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400351 }
352
Brian Salomonbe5a0932018-12-10 10:03:26 -0500353 if (mipMapped == GrMipMapped::kYes &&
354 GrTextureTypeHasRestrictedSampling(backendFormat.textureType())) {
355 // It is invalid to have a GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE and have mips as
356 // well.
357 doneProc(textureContext);
358 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400359 }
Brian Salomonbe5a0932018-12-10 10:03:26 -0500360
361 /**
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500362 * This class is the lazy instantiation callback for promise images. It manages calling the
363 * client's Fulfill, Release, and Done procs. It attempts to reuse a GrTexture instance in
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500364 * cases where the client provides the same SkPromiseImageTexture as Fulfill results for
365 * multiple SkImages. The created GrTexture is given a key based on a unique ID associated with
366 * the SkPromiseImageTexture.
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500367 *
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500368 * The GrTexutre idle proc mechanism is used to call the Release and Done procs. We use this
369 * instead of the GrSurface release proc because the GrTexture is cached and therefore may
370 * outlive the proxy into which this callback is installed.
371 *
372 * A key invalidation message is installed on the SkPromiseImageTexture so that the GrTexture
373 * is deleted once it can no longer be used to instantiate a proxy.
Brian Salomonbe5a0932018-12-10 10:03:26 -0500374 */
375 class PromiseLazyInstantiateCallback {
376 public:
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500377 PromiseLazyInstantiateCallback(PromiseImageTextureFulfillProc fulfillProc,
378 PromiseImageTextureReleaseProc releaseProc,
379 PromiseImageTextureDoneProc doneProc,
380 PromiseImageTextureContext context,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400381 GrColorType colorType,
Brian Salomon0cc57542019-03-08 13:28:46 -0500382 PromiseImageApiVersion version)
383 : fFulfillProc(fulfillProc)
384 , fReleaseProc(releaseProc)
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400385 , fColorType(colorType)
Brian Salomon0cc57542019-03-08 13:28:46 -0500386 , fVersion(version) {
Brian Salomone80b8092019-03-08 13:25:19 -0500387 fDoneCallback = sk_make_sp<GrRefCntedCallback>(doneProc, context);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500388 }
389 PromiseLazyInstantiateCallback(PromiseLazyInstantiateCallback&&) = default;
390 PromiseLazyInstantiateCallback(const PromiseLazyInstantiateCallback&) {
391 // Because we get wrapped in std::function we must be copyable. But we should never
392 // be copied.
393 SkASSERT(false);
394 }
395 PromiseLazyInstantiateCallback& operator=(PromiseLazyInstantiateCallback&&) = default;
396 PromiseLazyInstantiateCallback& operator=(const PromiseLazyInstantiateCallback&) {
397 SkASSERT(false);
398 return *this;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500399 }
Brian Salomon553610d2019-01-14 17:34:12 -0500400
Brian Salomon88b8d112019-03-07 15:25:34 +0000401 ~PromiseLazyInstantiateCallback() {
Brian Salomon876a0172019-03-08 11:12:14 -0500402 // Our destructor can run on any thread. We trigger the unref of fTexture by message.
403 if (fTexture) {
Brian Salomon876a0172019-03-08 11:12:14 -0500404 SkMessageBus<GrGpuResourceFreedMessage>::Post({fTexture, fTextureContextID});
405 }
Brian Salomon88b8d112019-03-07 15:25:34 +0000406 }
407
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400408 GrSurfaceProxy::LazyInstantiationResult operator()(GrResourceProvider* resourceProvider) {
409 // We use the unique key in a way that is unrelated to the SkImage-based key that the
410 // proxy may receive, hence kUnsynced.
411 static constexpr auto kKeySyncMode =
412 GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
413
Brian Salomon876a0172019-03-08 11:12:14 -0500414 // Our proxy is getting instantiated for the second+ time. We are only allowed to call
415 // Fulfill once. So return our cached result.
416 if (fTexture) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400417 return {sk_ref_sp(fTexture), kKeySyncMode};
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400418 } else if (fColorType == GrColorType::kUnknown) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400419 // We've already called fulfill and it failed. Our contract says that we should only
420 // call each callback once.
421 return {};
Brian Salomon876a0172019-03-08 11:12:14 -0500422 }
Brian Salomone80b8092019-03-08 13:25:19 -0500423 SkASSERT(fDoneCallback);
424 PromiseImageTextureContext textureContext = fDoneCallback->context();
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500425 sk_sp<SkPromiseImageTexture> promiseTexture = fFulfillProc(textureContext);
426 // From here on out our contract is that the release proc must be called, even if
427 // the return from fulfill was invalid or we fail for some other reason.
Brian Salomone80b8092019-03-08 13:25:19 -0500428 auto releaseCallback = sk_make_sp<GrRefCntedCallback>(fReleaseProc, textureContext);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500429 if (!promiseTexture) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400430 // This records that we have failed.
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400431 fColorType = GrColorType::kUnknown;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400432 return {};
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500433 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500434
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500435 auto backendTexture = promiseTexture->backendTexture();
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500436 if (!backendTexture.isValid()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400437 return {};
Brian Salomon559c6172019-01-10 10:23:44 -0500438 }
439
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400440 // TODO: delete this block
441 {
442 GrPixelConfig config = resourceProvider->caps()->getConfigFromBackendFormat(
443 backendTexture.getBackendFormat(),
444 fColorType);
445 SkASSERT(kUnknown_GrPixelConfig != config);
446 backendTexture.fConfig = config;
447 }
448
Brian Salomonf55e8d52019-01-30 17:28:20 -0500449 sk_sp<GrTexture> tex;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500450 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Salomon7d88f312019-02-28 10:03:03 -0500451 GrUniqueKey key;
452 GrUniqueKey::Builder builder(&key, kDomain, 2, "promise");
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500453 builder[0] = promiseTexture->uniqueID();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400454 builder[1] = (uint32_t) fColorType;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500455 builder.finish();
Brian Salomon0d606762019-01-25 09:58:38 -0500456 // A texture with this key may already exist from a different instance of this lazy
457 // callback. This could happen if the client fulfills a promise image with a texture
458 // that was previously used to fulfill a different promise image.
Brian Salomon7d88f312019-02-28 10:03:03 -0500459 if (auto surf = resourceProvider->findByUniqueKey<GrSurface>(key)) {
Brian Salomon0d606762019-01-25 09:58:38 -0500460 tex = sk_ref_sp(surf->asTexture());
461 SkASSERT(tex);
462 } else {
463 if ((tex = resourceProvider->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400464 backendTexture, fColorType, kBorrow_GrWrapOwnership,
465 GrWrapCacheable::kYes, kRead_GrIOType))) {
Brian Salomon7d88f312019-02-28 10:03:03 -0500466 tex->resourcePriv().setUniqueKey(key);
Brian Salomon0d606762019-01-25 09:58:38 -0500467 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400468 return {};
Brian Salomon0d606762019-01-25 09:58:38 -0500469 }
470 }
Brian Salomon0cc57542019-03-08 13:28:46 -0500471 auto releaseIdleState = fVersion == PromiseImageApiVersion::kLegacy
472 ? GrTexture::IdleState::kFinished
473 : GrTexture::IdleState::kFlushed;
474 tex->addIdleProc(std::move(releaseCallback), releaseIdleState);
Brian Salomone80b8092019-03-08 13:25:19 -0500475 tex->addIdleProc(std::move(fDoneCallback), GrTexture::IdleState::kFinished);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500476 promiseTexture->addKeyToInvalidate(tex->getContext()->priv().contextID(), key);
Brian Salomon876a0172019-03-08 11:12:14 -0500477 fTexture = tex.get();
478 // We need to hold on to the GrTexture in case our proxy gets reinstantiated. However,
479 // we can't unref in our destructor because we may be on another thread then. So we
480 // let the cache know it is waiting on an unref message. We will send that message from
481 // our destructor.
482 GrContext* context = fTexture->getContext();
483 context->priv().getResourceCache()->insertDelayedResourceUnref(fTexture);
484 fTextureContextID = context->priv().contextID();
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400485 return {std::move(tex), kKeySyncMode};
Brian Salomonbe5a0932018-12-10 10:03:26 -0500486 }
487
488 private:
Brian Salomone80b8092019-03-08 13:25:19 -0500489 PromiseImageTextureFulfillProc fFulfillProc;
490 PromiseImageTextureReleaseProc fReleaseProc;
491 sk_sp<GrRefCntedCallback> fDoneCallback;
Brian Salomon876a0172019-03-08 11:12:14 -0500492 GrTexture* fTexture = nullptr;
493 uint32_t fTextureContextID = SK_InvalidUniqueID;
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400494 GrColorType fColorType;
Brian Salomon0cc57542019-03-08 13:28:46 -0500495 PromiseImageApiVersion fVersion;
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400496 } callback(fulfillProc, releaseProc, doneProc, textureContext, colorType, version);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500497
Robert Phillips9da87e02019-02-04 13:26:26 -0500498 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonbe5a0932018-12-10 10:03:26 -0500499
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400500 GrPixelConfig config = context->priv().caps()->getConfigFromBackendFormat(
501 backendFormat,
502 colorType);
503
Brian Salomonbe5a0932018-12-10 10:03:26 -0500504 GrSurfaceDesc desc;
505 desc.fWidth = width;
506 desc.fHeight = height;
507 desc.fConfig = config;
508
509 // We pass kReadOnly here since we should treat content of the client's texture as immutable.
Brian Salomone8a766b2019-07-19 14:24:36 -0400510 // The promise API provides no way for the client to indicated that the texture is protected.
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400511 return proxyProvider->createLazyProxy(
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400512 std::move(callback), backendFormat, desc, GrRenderable::kNo, 1, origin, mipMapped,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400513 GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, SkBudgeted::kNo,
Brian Salomone8a766b2019-07-19 14:24:36 -0400514 GrProtected::kNo, GrSurfaceProxy::LazyInstantiationType::kDeinstantiate);
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400515}