blob: e2f70aeeeb379f6c023ed965c756d3ef6285779b [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
Greg Daniele877dce2019-07-11 10:52:43 -040059 *config = ctx->priv().caps()->getConfigFromBackendFormat(backendFormat, grCT);
Brian Salomonf391d0f2018-12-14 09:18:50 -050060 return *config != kUnknown_GrPixelConfig;
Jim Van Verth8026ccc2018-10-04 13:10:39 -040061}
62
63//////////////////////////////////////////////////////////////////////////////////////////////////
64
Brian Osmane50cdf02018-10-19 13:02:14 -040065bool SkImage_GpuBase::getROPixels(SkBitmap* dst, CachingHint chint) const {
Robert Phillips920d4882019-03-04 15:16:44 -050066 auto direct = fContext->priv().asDirectContext();
67 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040068 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
69 return false;
70 }
71
Jim Van Verth8026ccc2018-10-04 13:10:39 -040072 const auto desc = SkBitmapCacheDesc::Make(this);
73 if (SkBitmapCache::Find(desc, dst)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040074 SkASSERT(dst->isImmutable());
75 SkASSERT(dst->getPixels());
76 return true;
77 }
78
79 SkBitmapCache::RecPtr rec = nullptr;
80 SkPixmap pmap;
81 if (kAllow_CachingHint == chint) {
Brian Salomon5ad6fd32019-03-21 15:30:08 -040082 rec = SkBitmapCache::Alloc(desc, this->imageInfo(), &pmap);
Jim Van Verth8026ccc2018-10-04 13:10:39 -040083 if (!rec) {
84 return false;
85 }
86 } else {
Brian Salomon5ad6fd32019-03-21 15:30:08 -040087 if (!dst->tryAllocPixels(this->imageInfo()) || !dst->peekPixels(&pmap)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040088 return false;
89 }
90 }
91
Brian Salomond6287472019-06-24 15:50:07 -040092 sk_sp<GrSurfaceContext> sContext =
93 direct->priv().makeWrappedSurfaceContext(this->asTextureProxyRef(direct),
94 SkColorTypeToGrColorType(this->colorType()),
95 this->alphaType(),
96 this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -040097 if (!sContext) {
98 return false;
99 }
100
Brian Salomon1d435302019-07-01 13:05:28 -0400101 if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), {0, 0})) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400102 return false;
103 }
104
105 if (rec) {
106 SkBitmapCache::Add(std::move(rec), dst);
107 this->notifyAddedToRasterCache();
108 }
109 return true;
110}
111
Robert Phillips6603a172019-03-05 12:35:44 -0500112sk_sp<SkImage> SkImage_GpuBase::onMakeSubset(GrRecordingContext* context,
113 const SkIRect& subset) const {
114 if (!context || !fContext->priv().matches(context)) {
115 return nullptr;
116 }
117
118 sk_sp<GrSurfaceProxy> proxy = this->asTextureProxyRef(context);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400119
Greg Daniel46cfbc62019-06-07 11:43:30 -0400120 sk_sp<GrTextureProxy> copyProxy = GrSurfaceProxy::Copy(
121 context, proxy.get(), GrMipMapped::kNo, subset, SkBackingFit::kExact,
122 proxy->isBudgeted());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400123
Greg Daniel46cfbc62019-06-07 11:43:30 -0400124 if (!copyProxy) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400125 return nullptr;
126 }
127
128 // MDB: this call is okay bc we know 'sContext' was kExact
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400129 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, this->alphaType(),
Greg Daniel46cfbc62019-06-07 11:43:30 -0400130 std::move(copyProxy), this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400131}
132
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400133bool SkImage_GpuBase::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
134 int srcX, int srcY, CachingHint) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500135 auto direct = fContext->priv().asDirectContext();
136 if (!direct) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400137 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
138 return false;
139 }
140
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400141 if (!SkImageInfoValidConversion(dstInfo, this->imageInfo())) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400142 return false;
143 }
144
Robert Phillips920d4882019-03-04 15:16:44 -0500145 sk_sp<GrSurfaceContext> sContext = direct->priv().makeWrappedSurfaceContext(
Brian Salomond6287472019-06-24 15:50:07 -0400146 this->asTextureProxyRef(direct), SkColorTypeToGrColorType(this->colorType()),
147 this->alphaType(), this->refColorSpace());
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400148 if (!sContext) {
149 return false;
150 }
151
Brian Salomon1d435302019-07-01 13:05:28 -0400152 return sContext->readPixels(dstInfo, dstPixels, dstRB, {srcX, srcY});
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400153}
154
Robert Phillips9338c602019-02-19 12:52:29 -0500155sk_sp<GrTextureProxy> SkImage_GpuBase::asTextureProxyRef(GrRecordingContext* context,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400156 const GrSamplerState& params,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400157 SkScalar scaleAdjust[2]) const {
Robert Phillips6603a172019-03-05 12:35:44 -0500158 if (!context || !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400159 SkASSERT(0);
160 return nullptr;
161 }
162
Brian Salomond6287472019-06-24 15:50:07 -0400163 GrTextureAdjuster adjuster(fContext.get(), this->asTextureProxyRef(context),
164 SkColorTypeToGrColorType(this->colorType()), this->alphaType(),
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400165 this->uniqueID(), this->colorSpace());
Brian Osman6064e1c2018-10-19 14:27:54 -0400166 return adjuster.refTextureProxyForParams(params, scaleAdjust);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400167}
168
169GrBackendTexture SkImage_GpuBase::onGetBackendTexture(bool flushPendingGrContextIO,
170 GrSurfaceOrigin* origin) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500171 auto direct = fContext->priv().asDirectContext();
172 if (!direct) {
173 // This image was created with a DDL context and cannot be instantiated.
Robert Phillips6603a172019-03-05 12:35:44 -0500174 return GrBackendTexture(); // invalid
Robert Phillips920d4882019-03-04 15:16:44 -0500175 }
176
Robert Phillips6603a172019-03-05 12:35:44 -0500177 sk_sp<GrTextureProxy> proxy = this->asTextureProxyRef(direct);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400178 SkASSERT(proxy);
179
Robert Phillips920d4882019-03-04 15:16:44 -0500180 if (!proxy->isInstantiated()) {
181 auto resourceProvider = direct->priv().resourceProvider();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400182
Robert Phillips920d4882019-03-04 15:16:44 -0500183 if (!proxy->instantiate(resourceProvider)) {
184 return GrBackendTexture(); // invalid
185 }
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400186 }
187
188 GrTexture* texture = proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400189 if (texture) {
190 if (flushPendingGrContextIO) {
Greg Daniel4aa13e72019-04-15 14:42:20 -0400191 direct->priv().flushSurface(proxy.get());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400192 }
193 if (origin) {
194 *origin = proxy->origin();
195 }
196 return texture->getBackendTexture();
197 }
198 return GrBackendTexture(); // invalid
199}
200
201GrTexture* SkImage_GpuBase::onGetTexture() const {
202 GrTextureProxy* proxy = this->peekProxy();
Robert Phillips193c4212019-03-04 12:18:53 -0500203 if (proxy && proxy->isInstantiated()) {
204 return proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400205 }
206
Robert Phillips193c4212019-03-04 12:18:53 -0500207 auto direct = fContext->priv().asDirectContext();
208 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400209 // This image was created with a DDL context and cannot be instantiated.
210 return nullptr;
211 }
212
Robert Phillips6603a172019-03-05 12:35:44 -0500213 sk_sp<GrTextureProxy> proxyRef = this->asTextureProxyRef(direct);
Robert Phillips193c4212019-03-04 12:18:53 -0500214 SkASSERT(proxyRef && !proxyRef->isInstantiated());
215
216 if (!proxyRef->instantiate(direct->priv().resourceProvider())) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400217 return nullptr;
218 }
219
Robert Phillips193c4212019-03-04 12:18:53 -0500220 return proxyRef->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400221}
222
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400223bool SkImage_GpuBase::onIsValid(GrContext* context) const {
224 // The base class has already checked that context isn't abandoned (if it's not nullptr)
Robert Phillips920d4882019-03-04 15:16:44 -0500225 if (fContext->priv().abandoned()) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400226 return false;
227 }
228
Robert Phillips920d4882019-03-04 15:16:44 -0500229 if (context && !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400230 return false;
231 }
232
233 return true;
234}
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400235
Jim Van Verth0e671942018-11-09 12:03:57 -0500236bool SkImage_GpuBase::MakeTempTextureProxies(GrContext* ctx, const GrBackendTexture yuvaTextures[],
Jim Van Verth53275362018-11-09 15:42:35 -0500237 int numTextures, const SkYUVAIndex yuvaIndices[4],
238 GrSurfaceOrigin imageOrigin,
Jim Van Verth0e671942018-11-09 12:03:57 -0500239 sk_sp<GrTextureProxy> tempTextureProxies[4]) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500240 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400241 const GrCaps* caps = ctx->priv().caps();
Jim Van Verth0e671942018-11-09 12:03:57 -0500242
243 // We need to make a copy of the input backend textures because we need to preserve the result
244 // of validate_backend_texture.
245 GrBackendTexture yuvaTexturesCopy[4];
246 for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) {
247 yuvaTexturesCopy[textureIndex] = yuvaTextures[textureIndex];
Brian Salomonf391d0f2018-12-14 09:18:50 -0500248 GrBackendFormat backendFormat = yuvaTexturesCopy[textureIndex].getBackendFormat();
249 if (!backendFormat.isValid()) {
250 return false;
251 }
252 yuvaTexturesCopy[textureIndex].fConfig =
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400253 caps->getYUVAConfigFromBackendFormat(backendFormat);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500254 if (yuvaTexturesCopy[textureIndex].fConfig == kUnknown_GrPixelConfig) {
Jim Van Verth0e671942018-11-09 12:03:57 -0500255 return false;
256 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400257 GrColorType grColorType = caps->getYUVAColorTypeFromBackendFormat(backendFormat);
258 if (GrColorType::kUnknown == grColorType) {
259 return false;
260 }
261
Jim Van Verth0e671942018-11-09 12:03:57 -0500262 SkASSERT(yuvaTexturesCopy[textureIndex].isValid());
263
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500264 tempTextureProxies[textureIndex] = proxyProvider->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400265 yuvaTexturesCopy[textureIndex], grColorType, imageOrigin, kBorrow_GrWrapOwnership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500266 GrWrapCacheable::kNo, kRead_GrIOType);
Jim Van Verth0e671942018-11-09 12:03:57 -0500267 if (!tempTextureProxies[textureIndex]) {
268 return false;
269 }
Jim Van Verth53275362018-11-09 15:42:35 -0500270
271 // Check that each texture contains the channel data for the corresponding YUVA index
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400272 auto componentFlags = GrColorTypeComponentFlags(grColorType);
Jim Van Verth53275362018-11-09 15:42:35 -0500273 for (int yuvaIndex = 0; yuvaIndex < SkYUVAIndex::kIndexCount; ++yuvaIndex) {
274 if (yuvaIndices[yuvaIndex].fIndex == textureIndex) {
275 switch (yuvaIndices[yuvaIndex].fChannel) {
276 case SkColorChannel::kR:
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400277 // TODO: Chrome needs to be patched before this can be
278 // enforced.
279// if (!(kRed_SkColorTypeComponentFlag & componentFlags)) {
280// return false;
281// }
282 break;
283 case SkColorChannel::kG:
284 if (!(kGreen_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500285 return false;
286 }
287 break;
Jim Van Verth53275362018-11-09 15:42:35 -0500288 case SkColorChannel::kB:
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400289 if (!(kBlue_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500290 return false;
291 }
292 break;
293 case SkColorChannel::kA:
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400294 if (!(kAlpha_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500295 return false;
296 }
297 break;
298 }
299 }
300 }
Jim Van Verth0e671942018-11-09 12:03:57 -0500301 }
302
303 return true;
304}
305
306bool SkImage_GpuBase::RenderYUVAToRGBA(GrContext* ctx, GrRenderTargetContext* renderTargetContext,
307 const SkRect& rect, SkYUVColorSpace yuvColorSpace,
Brian Osmane9560492019-02-05 17:00:03 -0500308 sk_sp<GrColorSpaceXform> colorSpaceXform,
Jim Van Verth0e671942018-11-09 12:03:57 -0500309 const sk_sp<GrTextureProxy> proxies[4],
310 const SkYUVAIndex yuvaIndices[4]) {
311 SkASSERT(renderTargetContext);
312 if (!renderTargetContext->asSurfaceProxy()) {
313 return false;
314 }
315
316 GrPaint paint;
317 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
318
Brian Osmane9560492019-02-05 17:00:03 -0500319 auto fp = GrYUVtoRGBEffect::Make(proxies, yuvaIndices, yuvColorSpace,
320 GrSamplerState::Filter::kNearest);
321 if (colorSpaceXform) {
322 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(colorSpaceXform));
323 }
324 paint.addColorFragmentProcessor(std::move(fp));
Jim Van Verth0e671942018-11-09 12:03:57 -0500325
326 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Jim Van Verth0e671942018-11-09 12:03:57 -0500327 return true;
328}
329
Brian Salomonbe5a0932018-12-10 10:03:26 -0500330sk_sp<GrTextureProxy> SkImage_GpuBase::MakePromiseImageLazyProxy(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400331 GrContext* context, int width, int height, GrSurfaceOrigin origin, GrColorType colorType,
Brian Salomonbe5a0932018-12-10 10:03:26 -0500332 GrBackendFormat backendFormat, GrMipMapped mipMapped,
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500333 PromiseImageTextureFulfillProc fulfillProc,
334 PromiseImageTextureReleaseProc releaseProc,
335 PromiseImageTextureDoneProc doneProc,
Brian Salomon0cc57542019-03-08 13:28:46 -0500336 PromiseImageTextureContext textureContext,
337 PromiseImageApiVersion version) {
Brian Salomonbe5a0932018-12-10 10:03:26 -0500338 SkASSERT(context);
339 SkASSERT(width > 0 && height > 0);
340 SkASSERT(doneProc);
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400341 SkASSERT(colorType != GrColorType::kUnknown);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500342
343 if (!fulfillProc || !releaseProc) {
344 doneProc(textureContext);
345 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400346 }
347
Brian Salomonbe5a0932018-12-10 10:03:26 -0500348 if (mipMapped == GrMipMapped::kYes &&
349 GrTextureTypeHasRestrictedSampling(backendFormat.textureType())) {
350 // It is invalid to have a GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE and have mips as
351 // well.
352 doneProc(textureContext);
353 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400354 }
Brian Salomonbe5a0932018-12-10 10:03:26 -0500355
356 /**
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500357 * This class is the lazy instantiation callback for promise images. It manages calling the
358 * client's Fulfill, Release, and Done procs. It attempts to reuse a GrTexture instance in
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500359 * cases where the client provides the same SkPromiseImageTexture as Fulfill results for
360 * multiple SkImages. The created GrTexture is given a key based on a unique ID associated with
361 * the SkPromiseImageTexture.
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500362 *
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500363 * The GrTexutre idle proc mechanism is used to call the Release and Done procs. We use this
364 * instead of the GrSurface release proc because the GrTexture is cached and therefore may
365 * outlive the proxy into which this callback is installed.
366 *
367 * A key invalidation message is installed on the SkPromiseImageTexture so that the GrTexture
368 * is deleted once it can no longer be used to instantiate a proxy.
Brian Salomonbe5a0932018-12-10 10:03:26 -0500369 */
370 class PromiseLazyInstantiateCallback {
371 public:
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500372 PromiseLazyInstantiateCallback(PromiseImageTextureFulfillProc fulfillProc,
373 PromiseImageTextureReleaseProc releaseProc,
374 PromiseImageTextureDoneProc doneProc,
375 PromiseImageTextureContext context,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400376 GrColorType colorType,
Brian Salomon0cc57542019-03-08 13:28:46 -0500377 PromiseImageApiVersion version)
378 : fFulfillProc(fulfillProc)
379 , fReleaseProc(releaseProc)
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400380 , fColorType(colorType)
Brian Salomon0cc57542019-03-08 13:28:46 -0500381 , fVersion(version) {
Brian Salomone80b8092019-03-08 13:25:19 -0500382 fDoneCallback = sk_make_sp<GrRefCntedCallback>(doneProc, context);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500383 }
384 PromiseLazyInstantiateCallback(PromiseLazyInstantiateCallback&&) = default;
385 PromiseLazyInstantiateCallback(const PromiseLazyInstantiateCallback&) {
386 // Because we get wrapped in std::function we must be copyable. But we should never
387 // be copied.
388 SkASSERT(false);
389 }
390 PromiseLazyInstantiateCallback& operator=(PromiseLazyInstantiateCallback&&) = default;
391 PromiseLazyInstantiateCallback& operator=(const PromiseLazyInstantiateCallback&) {
392 SkASSERT(false);
393 return *this;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500394 }
Brian Salomon553610d2019-01-14 17:34:12 -0500395
Brian Salomon88b8d112019-03-07 15:25:34 +0000396 ~PromiseLazyInstantiateCallback() {
Brian Salomon876a0172019-03-08 11:12:14 -0500397 // Our destructor can run on any thread. We trigger the unref of fTexture by message.
398 if (fTexture) {
Brian Salomon876a0172019-03-08 11:12:14 -0500399 SkMessageBus<GrGpuResourceFreedMessage>::Post({fTexture, fTextureContextID});
400 }
Brian Salomon88b8d112019-03-07 15:25:34 +0000401 }
402
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400403 GrSurfaceProxy::LazyInstantiationResult operator()(GrResourceProvider* resourceProvider) {
404 // We use the unique key in a way that is unrelated to the SkImage-based key that the
405 // proxy may receive, hence kUnsynced.
406 static constexpr auto kKeySyncMode =
407 GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
408
Brian Salomon876a0172019-03-08 11:12:14 -0500409 // Our proxy is getting instantiated for the second+ time. We are only allowed to call
410 // Fulfill once. So return our cached result.
411 if (fTexture) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400412 return {sk_ref_sp(fTexture), kKeySyncMode};
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400413 } else if (fColorType == GrColorType::kUnknown) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400414 // We've already called fulfill and it failed. Our contract says that we should only
415 // call each callback once.
416 return {};
Brian Salomon876a0172019-03-08 11:12:14 -0500417 }
Brian Salomone80b8092019-03-08 13:25:19 -0500418 SkASSERT(fDoneCallback);
419 PromiseImageTextureContext textureContext = fDoneCallback->context();
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500420 sk_sp<SkPromiseImageTexture> promiseTexture = fFulfillProc(textureContext);
421 // From here on out our contract is that the release proc must be called, even if
422 // the return from fulfill was invalid or we fail for some other reason.
Brian Salomone80b8092019-03-08 13:25:19 -0500423 auto releaseCallback = sk_make_sp<GrRefCntedCallback>(fReleaseProc, textureContext);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500424 if (!promiseTexture) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400425 // This records that we have failed.
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400426 fColorType = GrColorType::kUnknown;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400427 return {};
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500428 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500429
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500430 auto backendTexture = promiseTexture->backendTexture();
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500431 if (!backendTexture.isValid()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400432 return {};
Brian Salomon559c6172019-01-10 10:23:44 -0500433 }
434
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400435 // TODO: delete this block
436 {
437 GrPixelConfig config = resourceProvider->caps()->getConfigFromBackendFormat(
438 backendTexture.getBackendFormat(),
439 fColorType);
440 SkASSERT(kUnknown_GrPixelConfig != config);
441 backendTexture.fConfig = config;
442 }
443
Brian Salomonf55e8d52019-01-30 17:28:20 -0500444 sk_sp<GrTexture> tex;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500445 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Salomon7d88f312019-02-28 10:03:03 -0500446 GrUniqueKey key;
447 GrUniqueKey::Builder builder(&key, kDomain, 2, "promise");
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500448 builder[0] = promiseTexture->uniqueID();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400449 builder[1] = (uint32_t) fColorType;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500450 builder.finish();
Brian Salomon0d606762019-01-25 09:58:38 -0500451 // A texture with this key may already exist from a different instance of this lazy
452 // callback. This could happen if the client fulfills a promise image with a texture
453 // that was previously used to fulfill a different promise image.
Brian Salomon7d88f312019-02-28 10:03:03 -0500454 if (auto surf = resourceProvider->findByUniqueKey<GrSurface>(key)) {
Brian Salomon0d606762019-01-25 09:58:38 -0500455 tex = sk_ref_sp(surf->asTexture());
456 SkASSERT(tex);
457 } else {
458 if ((tex = resourceProvider->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400459 backendTexture, fColorType, kBorrow_GrWrapOwnership,
460 GrWrapCacheable::kYes, kRead_GrIOType))) {
Brian Salomon7d88f312019-02-28 10:03:03 -0500461 tex->resourcePriv().setUniqueKey(key);
Brian Salomon0d606762019-01-25 09:58:38 -0500462 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400463 return {};
Brian Salomon0d606762019-01-25 09:58:38 -0500464 }
465 }
Brian Salomon0cc57542019-03-08 13:28:46 -0500466 auto releaseIdleState = fVersion == PromiseImageApiVersion::kLegacy
467 ? GrTexture::IdleState::kFinished
468 : GrTexture::IdleState::kFlushed;
469 tex->addIdleProc(std::move(releaseCallback), releaseIdleState);
Brian Salomone80b8092019-03-08 13:25:19 -0500470 tex->addIdleProc(std::move(fDoneCallback), GrTexture::IdleState::kFinished);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500471 promiseTexture->addKeyToInvalidate(tex->getContext()->priv().contextID(), key);
Brian Salomon876a0172019-03-08 11:12:14 -0500472 fTexture = tex.get();
473 // We need to hold on to the GrTexture in case our proxy gets reinstantiated. However,
474 // we can't unref in our destructor because we may be on another thread then. So we
475 // let the cache know it is waiting on an unref message. We will send that message from
476 // our destructor.
477 GrContext* context = fTexture->getContext();
478 context->priv().getResourceCache()->insertDelayedResourceUnref(fTexture);
479 fTextureContextID = context->priv().contextID();
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400480 return {std::move(tex), kKeySyncMode};
Brian Salomonbe5a0932018-12-10 10:03:26 -0500481 }
482
483 private:
Brian Salomone80b8092019-03-08 13:25:19 -0500484 PromiseImageTextureFulfillProc fFulfillProc;
485 PromiseImageTextureReleaseProc fReleaseProc;
486 sk_sp<GrRefCntedCallback> fDoneCallback;
Brian Salomon876a0172019-03-08 11:12:14 -0500487 GrTexture* fTexture = nullptr;
488 uint32_t fTextureContextID = SK_InvalidUniqueID;
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400489 GrColorType fColorType;
Brian Salomon0cc57542019-03-08 13:28:46 -0500490 PromiseImageApiVersion fVersion;
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400491 } callback(fulfillProc, releaseProc, doneProc, textureContext, colorType, version);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500492
Robert Phillips9da87e02019-02-04 13:26:26 -0500493 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonbe5a0932018-12-10 10:03:26 -0500494
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400495 GrPixelConfig config = context->priv().caps()->getConfigFromBackendFormat(
496 backendFormat,
497 colorType);
498
Brian Salomonbe5a0932018-12-10 10:03:26 -0500499 GrSurfaceDesc desc;
500 desc.fWidth = width;
501 desc.fHeight = height;
502 desc.fConfig = config;
503
504 // 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,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400508 GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, SkBudgeted::kNo,
Brian Salomone8a766b2019-07-19 14:24:36 -0400509 GrProtected::kNo, GrSurfaceProxy::LazyInstantiationType::kDeinstantiate);
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400510}