blob: 58d0eb8617a2592f9967aed574fef98d468d984f [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);
Greg Danielc594e622019-10-15 14:01:49 -0400120 GrColorType srcColorType = SkColorTypeToGrColorType(this->colorType());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400121
Greg Daniel46cfbc62019-06-07 11:43:30 -0400122 sk_sp<GrTextureProxy> copyProxy = GrSurfaceProxy::Copy(
Greg Danielc594e622019-10-15 14:01:49 -0400123 context, proxy.get(), srcColorType, GrMipMapped::kNo, subset, SkBackingFit::kExact,
Greg Daniel46cfbc62019-06-07 11:43:30 -0400124 proxy->isBudgeted());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400125
Greg Daniel46cfbc62019-06-07 11:43:30 -0400126 if (!copyProxy) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400127 return nullptr;
128 }
129
130 // MDB: this call is okay bc we know 'sContext' was kExact
Brian Salomon729fc0c2019-09-30 16:33:11 +0000131 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, this->alphaType(),
132 std::move(copyProxy), this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400133}
134
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400135bool SkImage_GpuBase::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
136 int srcX, int srcY, CachingHint) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500137 auto direct = fContext->priv().asDirectContext();
138 if (!direct) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400139 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
140 return false;
141 }
142
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400143 if (!SkImageInfoValidConversion(dstInfo, this->imageInfo())) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400144 return false;
145 }
146
Greg Danielba88ab62019-07-26 09:14:01 -0400147 sk_sp<GrTextureProxy> texProxy = this->asTextureProxyRef(direct);
148 GrColorType grColorType = SkColorTypeAndFormatToGrColorType(fContext->priv().caps(),
149 this->colorType(),
150 texProxy->backendFormat());
151
Brian Salomonbf6b9792019-08-21 09:38:10 -0400152 auto sContext = direct->priv().makeWrappedSurfaceContext(
Greg Danielba88ab62019-07-26 09:14:01 -0400153 std::move(texProxy), grColorType, this->alphaType(), this->refColorSpace());
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400154 if (!sContext) {
155 return false;
156 }
157
Brian Salomon1d435302019-07-01 13:05:28 -0400158 return sContext->readPixels(dstInfo, dstPixels, dstRB, {srcX, srcY});
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400159}
160
Robert Phillips9338c602019-02-19 12:52:29 -0500161sk_sp<GrTextureProxy> SkImage_GpuBase::asTextureProxyRef(GrRecordingContext* context,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400162 const GrSamplerState& params,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400163 SkScalar scaleAdjust[2]) const {
Robert Phillips6603a172019-03-05 12:35:44 -0500164 if (!context || !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400165 SkASSERT(0);
166 return nullptr;
167 }
168
Brian Salomond6287472019-06-24 15:50:07 -0400169 GrTextureAdjuster adjuster(fContext.get(), this->asTextureProxyRef(context),
170 SkColorTypeToGrColorType(this->colorType()), this->alphaType(),
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400171 this->uniqueID(), this->colorSpace());
Brian Osman6064e1c2018-10-19 14:27:54 -0400172 return adjuster.refTextureProxyForParams(params, scaleAdjust);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400173}
174
175GrBackendTexture SkImage_GpuBase::onGetBackendTexture(bool flushPendingGrContextIO,
176 GrSurfaceOrigin* origin) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500177 auto direct = fContext->priv().asDirectContext();
178 if (!direct) {
179 // This image was created with a DDL context and cannot be instantiated.
Robert Phillips6603a172019-03-05 12:35:44 -0500180 return GrBackendTexture(); // invalid
Robert Phillips920d4882019-03-04 15:16:44 -0500181 }
182
Robert Phillips6603a172019-03-05 12:35:44 -0500183 sk_sp<GrTextureProxy> proxy = this->asTextureProxyRef(direct);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400184 SkASSERT(proxy);
185
Robert Phillips920d4882019-03-04 15:16:44 -0500186 if (!proxy->isInstantiated()) {
187 auto resourceProvider = direct->priv().resourceProvider();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400188
Robert Phillips920d4882019-03-04 15:16:44 -0500189 if (!proxy->instantiate(resourceProvider)) {
190 return GrBackendTexture(); // invalid
191 }
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400192 }
193
194 GrTexture* texture = proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400195 if (texture) {
196 if (flushPendingGrContextIO) {
Greg Daniel4aa13e72019-04-15 14:42:20 -0400197 direct->priv().flushSurface(proxy.get());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400198 }
199 if (origin) {
200 *origin = proxy->origin();
201 }
202 return texture->getBackendTexture();
203 }
204 return GrBackendTexture(); // invalid
205}
206
207GrTexture* SkImage_GpuBase::onGetTexture() const {
208 GrTextureProxy* proxy = this->peekProxy();
Robert Phillips193c4212019-03-04 12:18:53 -0500209 if (proxy && proxy->isInstantiated()) {
210 return proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400211 }
212
Robert Phillips193c4212019-03-04 12:18:53 -0500213 auto direct = fContext->priv().asDirectContext();
214 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400215 // This image was created with a DDL context and cannot be instantiated.
216 return nullptr;
217 }
218
Robert Phillips6603a172019-03-05 12:35:44 -0500219 sk_sp<GrTextureProxy> proxyRef = this->asTextureProxyRef(direct);
Robert Phillips193c4212019-03-04 12:18:53 -0500220 SkASSERT(proxyRef && !proxyRef->isInstantiated());
221
222 if (!proxyRef->instantiate(direct->priv().resourceProvider())) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400223 return nullptr;
224 }
225
Robert Phillips193c4212019-03-04 12:18:53 -0500226 return proxyRef->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400227}
228
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400229bool SkImage_GpuBase::onIsValid(GrContext* context) const {
230 // The base class has already checked that context isn't abandoned (if it's not nullptr)
Robert Phillips920d4882019-03-04 15:16:44 -0500231 if (fContext->priv().abandoned()) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400232 return false;
233 }
234
Robert Phillips920d4882019-03-04 15:16:44 -0500235 if (context && !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400236 return false;
237 }
238
239 return true;
240}
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400241
Jim Van Verth0e671942018-11-09 12:03:57 -0500242bool SkImage_GpuBase::MakeTempTextureProxies(GrContext* ctx, const GrBackendTexture yuvaTextures[],
Jim Van Verth53275362018-11-09 15:42:35 -0500243 int numTextures, const SkYUVAIndex yuvaIndices[4],
244 GrSurfaceOrigin imageOrigin,
Jim Van Verth0e671942018-11-09 12:03:57 -0500245 sk_sp<GrTextureProxy> tempTextureProxies[4]) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500246 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400247 const GrCaps* caps = ctx->priv().caps();
Jim Van Verth0e671942018-11-09 12:03:57 -0500248
Jim Van Verth0e671942018-11-09 12:03:57 -0500249 for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) {
Robert Phillips62221e72019-07-24 15:07:38 -0400250 GrBackendFormat backendFormat = yuvaTextures[textureIndex].getBackendFormat();
Brian Salomonf391d0f2018-12-14 09:18:50 -0500251 if (!backendFormat.isValid()) {
252 return false;
253 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400254
Robert Phillips00c9f0d2019-08-02 17:17:35 -0400255 GrColorType grColorType = caps->getYUVAColorTypeFromBackendFormat(
256 backendFormat,
257 yuvaIndices[3].fIndex == textureIndex);
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400258 if (GrColorType::kUnknown == grColorType) {
259 return false;
260 }
261
Robert Phillips62221e72019-07-24 15:07:38 -0400262 SkASSERT(yuvaTextures[textureIndex].isValid());
Jim Van Verth0e671942018-11-09 12:03:57 -0500263
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500264 tempTextureProxies[textureIndex] = proxyProvider->wrapBackendTexture(
Robert Phillips62221e72019-07-24 15:07:38 -0400265 yuvaTextures[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.
Robert Phillipsddc21482019-10-16 14:30:09 -0400398 // This unreffed texture pointer is a real problem! When the context has been
399 // abandoned, the GrTexture pointed to by this pointer is deleted! Due to virtual
400 // inheritance any manipulation of this pointer at that point will cause a crash.
401 // For now we "work around" the problem by just passing it, untouched, into the
402 // message bus but this very fragile.
403 // In the future the GrSurface class hierarchy refactoring should eliminate this
404 // difficulty by removing the virtual inheritance.
Brian Salomon876a0172019-03-08 11:12:14 -0500405 if (fTexture) {
Robert Phillipsddc21482019-10-16 14:30:09 -0400406 SkMessageBus<GrTextureFreedMessage>::Post({fTexture, fTextureContextID});
Brian Salomon876a0172019-03-08 11:12:14 -0500407 }
Brian Salomon88b8d112019-03-07 15:25:34 +0000408 }
409
Brian Salomonbeb7f522019-08-30 16:19:42 -0400410 GrSurfaceProxy::LazyCallbackResult operator()(GrResourceProvider* resourceProvider) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400411 // We use the unique key in a way that is unrelated to the SkImage-based key that the
412 // proxy may receive, hence kUnsynced.
413 static constexpr auto kKeySyncMode =
414 GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
415
Brian Salomonbeb7f522019-08-30 16:19:42 -0400416 // In order to make the SkImage "thread safe" we rely on holding an extra ref to the
417 // texture in the callback and signalling the unref via a message to the resource cache.
418 // We need to extend the callback's lifetime to that of the proxy.
419 static constexpr auto kReleaseCallbackOnInstantiation = false;
420
Brian Salomon876a0172019-03-08 11:12:14 -0500421 // Our proxy is getting instantiated for the second+ time. We are only allowed to call
422 // Fulfill once. So return our cached result.
423 if (fTexture) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400424 return {sk_ref_sp(fTexture), kReleaseCallbackOnInstantiation, kKeySyncMode};
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400425 } else if (fColorType == GrColorType::kUnknown) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400426 // We've already called fulfill and it failed. Our contract says that we should only
427 // call each callback once.
428 return {};
Brian Salomon876a0172019-03-08 11:12:14 -0500429 }
Brian Salomone80b8092019-03-08 13:25:19 -0500430 SkASSERT(fDoneCallback);
431 PromiseImageTextureContext textureContext = fDoneCallback->context();
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500432 sk_sp<SkPromiseImageTexture> promiseTexture = fFulfillProc(textureContext);
433 // From here on out our contract is that the release proc must be called, even if
434 // the return from fulfill was invalid or we fail for some other reason.
Brian Salomone80b8092019-03-08 13:25:19 -0500435 auto releaseCallback = sk_make_sp<GrRefCntedCallback>(fReleaseProc, textureContext);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500436 if (!promiseTexture) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400437 // This records that we have failed.
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400438 fColorType = GrColorType::kUnknown;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400439 return {};
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500440 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500441
Robert Phillips62221e72019-07-24 15:07:38 -0400442 const GrBackendTexture& backendTexture = promiseTexture->backendTexture();
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500443 if (!backendTexture.isValid()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400444 return {};
Brian Salomon559c6172019-01-10 10:23:44 -0500445 }
446
Brian Salomonf55e8d52019-01-30 17:28:20 -0500447 sk_sp<GrTexture> tex;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500448 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Salomon7d88f312019-02-28 10:03:03 -0500449 GrUniqueKey key;
450 GrUniqueKey::Builder builder(&key, kDomain, 2, "promise");
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500451 builder[0] = promiseTexture->uniqueID();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400452 builder[1] = (uint32_t) fColorType;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500453 builder.finish();
Brian Salomon0d606762019-01-25 09:58:38 -0500454 // A texture with this key may already exist from a different instance of this lazy
455 // callback. This could happen if the client fulfills a promise image with a texture
456 // that was previously used to fulfill a different promise image.
Brian Salomon7d88f312019-02-28 10:03:03 -0500457 if (auto surf = resourceProvider->findByUniqueKey<GrSurface>(key)) {
Brian Salomon0d606762019-01-25 09:58:38 -0500458 tex = sk_ref_sp(surf->asTexture());
459 SkASSERT(tex);
460 } else {
461 if ((tex = resourceProvider->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400462 backendTexture, fColorType, kBorrow_GrWrapOwnership,
463 GrWrapCacheable::kYes, kRead_GrIOType))) {
Brian Salomon7d88f312019-02-28 10:03:03 -0500464 tex->resourcePriv().setUniqueKey(key);
Brian Salomon0d606762019-01-25 09:58:38 -0500465 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400466 return {};
Brian Salomon0d606762019-01-25 09:58:38 -0500467 }
468 }
Brian Salomon0cc57542019-03-08 13:28:46 -0500469 auto releaseIdleState = fVersion == PromiseImageApiVersion::kLegacy
470 ? GrTexture::IdleState::kFinished
471 : GrTexture::IdleState::kFlushed;
472 tex->addIdleProc(std::move(releaseCallback), releaseIdleState);
Brian Salomone80b8092019-03-08 13:25:19 -0500473 tex->addIdleProc(std::move(fDoneCallback), GrTexture::IdleState::kFinished);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500474 promiseTexture->addKeyToInvalidate(tex->getContext()->priv().contextID(), key);
Brian Salomon876a0172019-03-08 11:12:14 -0500475 fTexture = tex.get();
476 // We need to hold on to the GrTexture in case our proxy gets reinstantiated. However,
477 // we can't unref in our destructor because we may be on another thread then. So we
478 // let the cache know it is waiting on an unref message. We will send that message from
479 // our destructor.
480 GrContext* context = fTexture->getContext();
Robert Phillipsddc21482019-10-16 14:30:09 -0400481 context->priv().getResourceCache()->insertDelayedTextureUnref(fTexture);
Brian Salomon876a0172019-03-08 11:12:14 -0500482 fTextureContextID = context->priv().contextID();
Brian Salomonbeb7f522019-08-30 16:19:42 -0400483 return {std::move(tex), kReleaseCallbackOnInstantiation, kKeySyncMode};
Brian Salomonbe5a0932018-12-10 10:03:26 -0500484 }
485
486 private:
Brian Salomone80b8092019-03-08 13:25:19 -0500487 PromiseImageTextureFulfillProc fFulfillProc;
488 PromiseImageTextureReleaseProc fReleaseProc;
489 sk_sp<GrRefCntedCallback> fDoneCallback;
Brian Salomon876a0172019-03-08 11:12:14 -0500490 GrTexture* fTexture = nullptr;
491 uint32_t fTextureContextID = SK_InvalidUniqueID;
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400492 GrColorType fColorType;
Brian Salomon0cc57542019-03-08 13:28:46 -0500493 PromiseImageApiVersion fVersion;
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400494 } callback(fulfillProc, releaseProc, doneProc, textureContext, colorType, version);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500495
Robert Phillips9da87e02019-02-04 13:26:26 -0500496 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonbe5a0932018-12-10 10:03:26 -0500497
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400498 GrPixelConfig config = context->priv().caps()->getConfigFromBackendFormat(
499 backendFormat,
500 colorType);
501
Brian Salomonbe5a0932018-12-10 10:03:26 -0500502 GrSurfaceDesc desc;
503 desc.fWidth = width;
504 desc.fHeight = height;
505 desc.fConfig = config;
506
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600507 // Ganesh assumes that, when wrapping a mipmapped backend texture from a client, that its
508 // mipmaps are fully fleshed out.
509 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
510 ? GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
511
Brian Salomonbe5a0932018-12-10 10:03:26 -0500512 // We pass kReadOnly here since we should treat content of the client's texture as immutable.
Brian Salomone8a766b2019-07-19 14:24:36 -0400513 // The promise API provides no way for the client to indicated that the texture is protected.
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400514 return proxyProvider->createLazyProxy(
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400515 std::move(callback), backendFormat, desc, GrRenderable::kNo, 1, origin, mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600516 mipMapsStatus, GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, SkBudgeted::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400517 GrProtected::kNo, GrSurfaceProxy::UseAllocator::kYes);
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400518}