blob: d31e10de2afbb3ec61557351067cbf2ec4b6a7e9 [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
Brian Salomon9f2b86c2019-10-22 10:37:46 -040026SkImage_GpuBase::SkImage_GpuBase(sk_sp<GrContext> context, SkISize size, uint32_t uniqueID,
Brian Salomon5ad6fd32019-03-21 15:30:08 -040027 SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs)
Brian Salomon9f2b86c2019-10-22 10:37:46 -040028 : INHERITED(SkImageInfo::Make(size, ct, at, std::move(cs)), uniqueID)
Brian Salomon5ad6fd32019-03-21 15:30:08 -040029 , fContext(std::move(context)) {}
Jim Van Verth8026ccc2018-10-04 13:10:39 -040030
Jim Van Verth8026ccc2018-10-04 13:10:39 -040031//////////////////////////////////////////////////////////////////////////////////////////////////
32
Robert Phillipsfd0d9702019-02-01 10:19:42 -050033#if GR_TEST_UTILS
34void SkImage_GpuBase::resetContext(sk_sp<GrContext> newContext) {
Robert Phillipsfe0963c2019-02-07 13:25:07 -050035 SkASSERT(fContext->priv().matches(newContext.get()));
Robert Phillipsfd0d9702019-02-01 10:19:42 -050036 fContext = newContext;
37}
38#endif
39
Robert Phillips62221e72019-07-24 15:07:38 -040040bool SkImage_GpuBase::ValidateBackendTexture(const GrCaps* caps, const GrBackendTexture& tex,
41 GrColorType grCT, SkColorType ct, SkAlphaType at,
Jim Van Verth8026ccc2018-10-04 13:10:39 -040042 sk_sp<SkColorSpace> cs) {
43 if (!tex.isValid()) {
44 return false;
45 }
46 // TODO: Create a SkImageColorInfo struct for color, alpha, and color space so we don't need to
47 // create a fake image info here.
48 SkImageInfo info = SkImageInfo::Make(1, 1, ct, at, cs);
49 if (!SkImageInfoIsValid(info)) {
50 return false;
51 }
Brian Salomonf391d0f2018-12-14 09:18:50 -050052 GrBackendFormat backendFormat = tex.getBackendFormat();
53 if (!backendFormat.isValid()) {
54 return false;
55 }
Greg Daniele877dce2019-07-11 10:52:43 -040056
Robert Phillips62221e72019-07-24 15:07:38 -040057 return caps->areColorTypeAndFormatCompatible(grCT, backendFormat);
Jim Van Verth8026ccc2018-10-04 13:10:39 -040058}
59
60//////////////////////////////////////////////////////////////////////////////////////////////////
61
Brian Osmane50cdf02018-10-19 13:02:14 -040062bool SkImage_GpuBase::getROPixels(SkBitmap* dst, CachingHint chint) const {
Robert Phillips920d4882019-03-04 15:16:44 -050063 auto direct = fContext->priv().asDirectContext();
64 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040065 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
66 return false;
67 }
68
Jim Van Verth8026ccc2018-10-04 13:10:39 -040069 const auto desc = SkBitmapCacheDesc::Make(this);
70 if (SkBitmapCache::Find(desc, dst)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040071 SkASSERT(dst->isImmutable());
72 SkASSERT(dst->getPixels());
73 return true;
74 }
75
76 SkBitmapCache::RecPtr rec = nullptr;
77 SkPixmap pmap;
78 if (kAllow_CachingHint == chint) {
Brian Salomon5ad6fd32019-03-21 15:30:08 -040079 rec = SkBitmapCache::Alloc(desc, this->imageInfo(), &pmap);
Jim Van Verth8026ccc2018-10-04 13:10:39 -040080 if (!rec) {
81 return false;
82 }
83 } else {
Brian Salomon5ad6fd32019-03-21 15:30:08 -040084 if (!dst->tryAllocPixels(this->imageInfo()) || !dst->peekPixels(&pmap)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040085 return false;
86 }
87 }
88
Greg Danielba88ab62019-07-26 09:14:01 -040089 sk_sp<GrTextureProxy> texProxy = this->asTextureProxyRef(direct);
90 GrColorType grColorType = SkColorTypeAndFormatToGrColorType(fContext->priv().caps(),
91 this->colorType(),
92 texProxy->backendFormat());
93
Brian Salomonbf6b9792019-08-21 09:38:10 -040094 auto sContext = direct->priv().makeWrappedSurfaceContext(
95 std::move(texProxy), grColorType, this->alphaType(), this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -040096 if (!sContext) {
97 return false;
98 }
99
Brian Salomon1d435302019-07-01 13:05:28 -0400100 if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), {0, 0})) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400101 return false;
102 }
103
104 if (rec) {
105 SkBitmapCache::Add(std::move(rec), dst);
106 this->notifyAddedToRasterCache();
107 }
108 return true;
109}
110
Robert Phillips6603a172019-03-05 12:35:44 -0500111sk_sp<SkImage> SkImage_GpuBase::onMakeSubset(GrRecordingContext* context,
112 const SkIRect& subset) const {
113 if (!context || !fContext->priv().matches(context)) {
114 return nullptr;
115 }
116
117 sk_sp<GrSurfaceProxy> proxy = this->asTextureProxyRef(context);
Greg Danielc594e622019-10-15 14:01:49 -0400118 GrColorType srcColorType = SkColorTypeToGrColorType(this->colorType());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400119
Greg Daniel46cfbc62019-06-07 11:43:30 -0400120 sk_sp<GrTextureProxy> copyProxy = GrSurfaceProxy::Copy(
Greg Danielc594e622019-10-15 14:01:49 -0400121 context, proxy.get(), srcColorType, GrMipMapped::kNo, subset, SkBackingFit::kExact,
Greg Daniel46cfbc62019-06-07 11:43:30 -0400122 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 Salomon729fc0c2019-09-30 16:33:11 +0000129 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, this->alphaType(),
130 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
Greg Danielba88ab62019-07-26 09:14:01 -0400145 sk_sp<GrTextureProxy> texProxy = this->asTextureProxyRef(direct);
146 GrColorType grColorType = SkColorTypeAndFormatToGrColorType(fContext->priv().caps(),
147 this->colorType(),
148 texProxy->backendFormat());
149
Brian Salomonbf6b9792019-08-21 09:38:10 -0400150 auto sContext = direct->priv().makeWrappedSurfaceContext(
Greg Danielba88ab62019-07-26 09:14:01 -0400151 std::move(texProxy), grColorType, 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
Jim Van Verth0e671942018-11-09 12:03:57 -0500247 for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) {
Robert Phillips62221e72019-07-24 15:07:38 -0400248 GrBackendFormat backendFormat = yuvaTextures[textureIndex].getBackendFormat();
Brian Salomonf391d0f2018-12-14 09:18:50 -0500249 if (!backendFormat.isValid()) {
250 return false;
251 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400252
Robert Phillips00c9f0d2019-08-02 17:17:35 -0400253 GrColorType grColorType = caps->getYUVAColorTypeFromBackendFormat(
254 backendFormat,
255 yuvaIndices[3].fIndex == textureIndex);
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400256 if (GrColorType::kUnknown == grColorType) {
257 return false;
258 }
259
Robert Phillips62221e72019-07-24 15:07:38 -0400260 SkASSERT(yuvaTextures[textureIndex].isValid());
Jim Van Verth0e671942018-11-09 12:03:57 -0500261
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500262 tempTextureProxies[textureIndex] = proxyProvider->wrapBackendTexture(
Robert Phillips62221e72019-07-24 15:07:38 -0400263 yuvaTextures[textureIndex], grColorType, imageOrigin, kBorrow_GrWrapOwnership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500264 GrWrapCacheable::kNo, kRead_GrIOType);
Jim Van Verth0e671942018-11-09 12:03:57 -0500265 if (!tempTextureProxies[textureIndex]) {
266 return false;
267 }
Jim Van Verth53275362018-11-09 15:42:35 -0500268
269 // Check that each texture contains the channel data for the corresponding YUVA index
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400270 auto componentFlags = GrColorTypeComponentFlags(grColorType);
Jim Van Verth53275362018-11-09 15:42:35 -0500271 for (int yuvaIndex = 0; yuvaIndex < SkYUVAIndex::kIndexCount; ++yuvaIndex) {
272 if (yuvaIndices[yuvaIndex].fIndex == textureIndex) {
273 switch (yuvaIndices[yuvaIndex].fChannel) {
274 case SkColorChannel::kR:
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400275 // TODO: Chrome needs to be patched before this can be
276 // enforced.
277// if (!(kRed_SkColorTypeComponentFlag & componentFlags)) {
278// return false;
279// }
280 break;
281 case SkColorChannel::kG:
282 if (!(kGreen_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500283 return false;
284 }
285 break;
Jim Van Verth53275362018-11-09 15:42:35 -0500286 case SkColorChannel::kB:
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400287 if (!(kBlue_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500288 return false;
289 }
290 break;
291 case SkColorChannel::kA:
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400292 if (!(kAlpha_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500293 return false;
294 }
295 break;
296 }
297 }
298 }
Jim Van Verth0e671942018-11-09 12:03:57 -0500299 }
300
301 return true;
302}
303
304bool SkImage_GpuBase::RenderYUVAToRGBA(GrContext* ctx, GrRenderTargetContext* renderTargetContext,
305 const SkRect& rect, SkYUVColorSpace yuvColorSpace,
Brian Osmane9560492019-02-05 17:00:03 -0500306 sk_sp<GrColorSpaceXform> colorSpaceXform,
Jim Van Verth0e671942018-11-09 12:03:57 -0500307 const sk_sp<GrTextureProxy> proxies[4],
308 const SkYUVAIndex yuvaIndices[4]) {
309 SkASSERT(renderTargetContext);
310 if (!renderTargetContext->asSurfaceProxy()) {
311 return false;
312 }
313
314 GrPaint paint;
315 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
316
Brian Osmane9560492019-02-05 17:00:03 -0500317 auto fp = GrYUVtoRGBEffect::Make(proxies, yuvaIndices, yuvColorSpace,
318 GrSamplerState::Filter::kNearest);
319 if (colorSpaceXform) {
320 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(colorSpaceXform));
321 }
322 paint.addColorFragmentProcessor(std::move(fp));
Jim Van Verth0e671942018-11-09 12:03:57 -0500323
324 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Jim Van Verth0e671942018-11-09 12:03:57 -0500325 return true;
326}
327
Brian Salomonbe5a0932018-12-10 10:03:26 -0500328sk_sp<GrTextureProxy> SkImage_GpuBase::MakePromiseImageLazyProxy(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400329 GrContext* context, int width, int height, GrSurfaceOrigin origin, GrColorType colorType,
Brian Salomonbe5a0932018-12-10 10:03:26 -0500330 GrBackendFormat backendFormat, GrMipMapped mipMapped,
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500331 PromiseImageTextureFulfillProc fulfillProc,
332 PromiseImageTextureReleaseProc releaseProc,
333 PromiseImageTextureDoneProc doneProc,
Brian Salomon0cc57542019-03-08 13:28:46 -0500334 PromiseImageTextureContext textureContext,
335 PromiseImageApiVersion version) {
Brian Salomonbe5a0932018-12-10 10:03:26 -0500336 SkASSERT(context);
337 SkASSERT(width > 0 && height > 0);
338 SkASSERT(doneProc);
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400339 SkASSERT(colorType != GrColorType::kUnknown);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500340
341 if (!fulfillProc || !releaseProc) {
342 doneProc(textureContext);
343 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400344 }
345
Brian Salomonbe5a0932018-12-10 10:03:26 -0500346 if (mipMapped == GrMipMapped::kYes &&
347 GrTextureTypeHasRestrictedSampling(backendFormat.textureType())) {
348 // It is invalid to have a GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE and have mips as
349 // well.
350 doneProc(textureContext);
351 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400352 }
Brian Salomonbe5a0932018-12-10 10:03:26 -0500353
354 /**
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500355 * This class is the lazy instantiation callback for promise images. It manages calling the
356 * client's Fulfill, Release, and Done procs. It attempts to reuse a GrTexture instance in
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500357 * cases where the client provides the same SkPromiseImageTexture as Fulfill results for
358 * multiple SkImages. The created GrTexture is given a key based on a unique ID associated with
359 * the SkPromiseImageTexture.
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500360 *
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500361 * The GrTexutre idle proc mechanism is used to call the Release and Done procs. We use this
362 * instead of the GrSurface release proc because the GrTexture is cached and therefore may
363 * outlive the proxy into which this callback is installed.
364 *
365 * A key invalidation message is installed on the SkPromiseImageTexture so that the GrTexture
366 * is deleted once it can no longer be used to instantiate a proxy.
Brian Salomonbe5a0932018-12-10 10:03:26 -0500367 */
368 class PromiseLazyInstantiateCallback {
369 public:
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500370 PromiseLazyInstantiateCallback(PromiseImageTextureFulfillProc fulfillProc,
371 PromiseImageTextureReleaseProc releaseProc,
372 PromiseImageTextureDoneProc doneProc,
373 PromiseImageTextureContext context,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400374 GrColorType colorType,
Brian Salomon0cc57542019-03-08 13:28:46 -0500375 PromiseImageApiVersion version)
376 : fFulfillProc(fulfillProc)
377 , fReleaseProc(releaseProc)
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400378 , fColorType(colorType)
Brian Salomon0cc57542019-03-08 13:28:46 -0500379 , fVersion(version) {
Brian Salomone80b8092019-03-08 13:25:19 -0500380 fDoneCallback = sk_make_sp<GrRefCntedCallback>(doneProc, context);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500381 }
382 PromiseLazyInstantiateCallback(PromiseLazyInstantiateCallback&&) = default;
383 PromiseLazyInstantiateCallback(const PromiseLazyInstantiateCallback&) {
384 // Because we get wrapped in std::function we must be copyable. But we should never
385 // be copied.
386 SkASSERT(false);
387 }
388 PromiseLazyInstantiateCallback& operator=(PromiseLazyInstantiateCallback&&) = default;
389 PromiseLazyInstantiateCallback& operator=(const PromiseLazyInstantiateCallback&) {
390 SkASSERT(false);
391 return *this;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500392 }
Brian Salomon553610d2019-01-14 17:34:12 -0500393
Brian Salomon88b8d112019-03-07 15:25:34 +0000394 ~PromiseLazyInstantiateCallback() {
Brian Salomon876a0172019-03-08 11:12:14 -0500395 // Our destructor can run on any thread. We trigger the unref of fTexture by message.
Robert Phillipsddc21482019-10-16 14:30:09 -0400396 // This unreffed texture pointer is a real problem! When the context has been
397 // abandoned, the GrTexture pointed to by this pointer is deleted! Due to virtual
398 // inheritance any manipulation of this pointer at that point will cause a crash.
399 // For now we "work around" the problem by just passing it, untouched, into the
400 // message bus but this very fragile.
401 // In the future the GrSurface class hierarchy refactoring should eliminate this
402 // difficulty by removing the virtual inheritance.
Brian Salomon876a0172019-03-08 11:12:14 -0500403 if (fTexture) {
Robert Phillipsddc21482019-10-16 14:30:09 -0400404 SkMessageBus<GrTextureFreedMessage>::Post({fTexture, fTextureContextID});
Brian Salomon876a0172019-03-08 11:12:14 -0500405 }
Brian Salomon88b8d112019-03-07 15:25:34 +0000406 }
407
Brian Salomonbeb7f522019-08-30 16:19:42 -0400408 GrSurfaceProxy::LazyCallbackResult operator()(GrResourceProvider* resourceProvider) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400409 // 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 Salomonbeb7f522019-08-30 16:19:42 -0400414 // In order to make the SkImage "thread safe" we rely on holding an extra ref to the
415 // texture in the callback and signalling the unref via a message to the resource cache.
416 // We need to extend the callback's lifetime to that of the proxy.
417 static constexpr auto kReleaseCallbackOnInstantiation = false;
418
Brian Salomon876a0172019-03-08 11:12:14 -0500419 // Our proxy is getting instantiated for the second+ time. We are only allowed to call
420 // Fulfill once. So return our cached result.
421 if (fTexture) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400422 return {sk_ref_sp(fTexture), kReleaseCallbackOnInstantiation, kKeySyncMode};
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400423 } else if (fColorType == GrColorType::kUnknown) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400424 // We've already called fulfill and it failed. Our contract says that we should only
425 // call each callback once.
426 return {};
Brian Salomon876a0172019-03-08 11:12:14 -0500427 }
Brian Salomone80b8092019-03-08 13:25:19 -0500428 SkASSERT(fDoneCallback);
429 PromiseImageTextureContext textureContext = fDoneCallback->context();
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500430 sk_sp<SkPromiseImageTexture> promiseTexture = fFulfillProc(textureContext);
431 // From here on out our contract is that the release proc must be called, even if
432 // the return from fulfill was invalid or we fail for some other reason.
Brian Salomone80b8092019-03-08 13:25:19 -0500433 auto releaseCallback = sk_make_sp<GrRefCntedCallback>(fReleaseProc, textureContext);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500434 if (!promiseTexture) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400435 // This records that we have failed.
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400436 fColorType = GrColorType::kUnknown;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400437 return {};
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500438 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500439
Robert Phillips62221e72019-07-24 15:07:38 -0400440 const GrBackendTexture& backendTexture = promiseTexture->backendTexture();
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500441 if (!backendTexture.isValid()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400442 return {};
Brian Salomon559c6172019-01-10 10:23:44 -0500443 }
444
Brian Salomonf55e8d52019-01-30 17:28:20 -0500445 sk_sp<GrTexture> tex;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500446 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Salomon7d88f312019-02-28 10:03:03 -0500447 GrUniqueKey key;
448 GrUniqueKey::Builder builder(&key, kDomain, 2, "promise");
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500449 builder[0] = promiseTexture->uniqueID();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400450 builder[1] = (uint32_t) fColorType;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500451 builder.finish();
Brian Salomon0d606762019-01-25 09:58:38 -0500452 // A texture with this key may already exist from a different instance of this lazy
453 // callback. This could happen if the client fulfills a promise image with a texture
454 // that was previously used to fulfill a different promise image.
Brian Salomon7d88f312019-02-28 10:03:03 -0500455 if (auto surf = resourceProvider->findByUniqueKey<GrSurface>(key)) {
Brian Salomon0d606762019-01-25 09:58:38 -0500456 tex = sk_ref_sp(surf->asTexture());
457 SkASSERT(tex);
458 } else {
459 if ((tex = resourceProvider->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400460 backendTexture, fColorType, kBorrow_GrWrapOwnership,
461 GrWrapCacheable::kYes, kRead_GrIOType))) {
Brian Salomon7d88f312019-02-28 10:03:03 -0500462 tex->resourcePriv().setUniqueKey(key);
Brian Salomon0d606762019-01-25 09:58:38 -0500463 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400464 return {};
Brian Salomon0d606762019-01-25 09:58:38 -0500465 }
466 }
Brian Salomon0cc57542019-03-08 13:28:46 -0500467 auto releaseIdleState = fVersion == PromiseImageApiVersion::kLegacy
468 ? GrTexture::IdleState::kFinished
469 : GrTexture::IdleState::kFlushed;
470 tex->addIdleProc(std::move(releaseCallback), releaseIdleState);
Brian Salomone80b8092019-03-08 13:25:19 -0500471 tex->addIdleProc(std::move(fDoneCallback), GrTexture::IdleState::kFinished);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500472 promiseTexture->addKeyToInvalidate(tex->getContext()->priv().contextID(), key);
Brian Salomon876a0172019-03-08 11:12:14 -0500473 fTexture = tex.get();
474 // We need to hold on to the GrTexture in case our proxy gets reinstantiated. However,
475 // we can't unref in our destructor because we may be on another thread then. So we
476 // let the cache know it is waiting on an unref message. We will send that message from
477 // our destructor.
478 GrContext* context = fTexture->getContext();
Robert Phillipsddc21482019-10-16 14:30:09 -0400479 context->priv().getResourceCache()->insertDelayedTextureUnref(fTexture);
Brian Salomon876a0172019-03-08 11:12:14 -0500480 fTextureContextID = context->priv().contextID();
Brian Salomonbeb7f522019-08-30 16:19:42 -0400481 return {std::move(tex), kReleaseCallbackOnInstantiation, kKeySyncMode};
Brian Salomonbe5a0932018-12-10 10:03:26 -0500482 }
483
484 private:
Brian Salomone80b8092019-03-08 13:25:19 -0500485 PromiseImageTextureFulfillProc fFulfillProc;
486 PromiseImageTextureReleaseProc fReleaseProc;
487 sk_sp<GrRefCntedCallback> fDoneCallback;
Brian Salomon876a0172019-03-08 11:12:14 -0500488 GrTexture* fTexture = nullptr;
489 uint32_t fTextureContextID = SK_InvalidUniqueID;
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400490 GrColorType fColorType;
Brian Salomon0cc57542019-03-08 13:28:46 -0500491 PromiseImageApiVersion fVersion;
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400492 } callback(fulfillProc, releaseProc, doneProc, textureContext, colorType, version);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500493
Robert Phillips9da87e02019-02-04 13:26:26 -0500494 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonbe5a0932018-12-10 10:03:26 -0500495
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400496 GrPixelConfig config = context->priv().caps()->getConfigFromBackendFormat(
497 backendFormat,
498 colorType);
499
Brian Salomonbe5a0932018-12-10 10:03:26 -0500500 GrSurfaceDesc desc;
501 desc.fWidth = width;
502 desc.fHeight = height;
503 desc.fConfig = config;
504
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600505 // Ganesh assumes that, when wrapping a mipmapped backend texture from a client, that its
506 // mipmaps are fully fleshed out.
507 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
508 ? GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
509
Brian Salomonbe5a0932018-12-10 10:03:26 -0500510 // We pass kReadOnly here since we should treat content of the client's texture as immutable.
Brian Salomone8a766b2019-07-19 14:24:36 -0400511 // The promise API provides no way for the client to indicated that the texture is protected.
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400512 return proxyProvider->createLazyProxy(
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400513 std::move(callback), backendFormat, desc, GrRenderable::kNo, 1, origin, mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600514 mipMapsStatus, GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, SkBudgeted::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400515 GrProtected::kNo, GrSurfaceProxy::UseAllocator::kYes);
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400516}