blob: 133e49190eedad3540167c4fdf50a4731458ab2f [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,
42 GrPixelConfig* config, SkColorType ct, SkAlphaType at,
43 sk_sp<SkColorSpace> cs) {
44 if (!tex.isValid()) {
45 return false;
46 }
47 // TODO: Create a SkImageColorInfo struct for color, alpha, and color space so we don't need to
48 // create a fake image info here.
49 SkImageInfo info = SkImageInfo::Make(1, 1, ct, at, cs);
50 if (!SkImageInfoIsValid(info)) {
51 return false;
52 }
Brian Salomonf391d0f2018-12-14 09:18:50 -050053 GrBackendFormat backendFormat = tex.getBackendFormat();
54 if (!backendFormat.isValid()) {
55 return false;
56 }
Robert Phillips9da87e02019-02-04 13:26:26 -050057 *config = ctx->priv().caps()->getConfigFromBackendFormat(backendFormat, ct);
Brian Salomonf391d0f2018-12-14 09:18:50 -050058 return *config != kUnknown_GrPixelConfig;
Jim Van Verth8026ccc2018-10-04 13:10:39 -040059}
60
61//////////////////////////////////////////////////////////////////////////////////////////////////
62
Brian Osmane50cdf02018-10-19 13:02:14 -040063bool SkImage_GpuBase::getROPixels(SkBitmap* dst, CachingHint chint) const {
Robert Phillips920d4882019-03-04 15:16:44 -050064 auto direct = fContext->priv().asDirectContext();
65 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040066 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
67 return false;
68 }
69
Jim Van Verth8026ccc2018-10-04 13:10:39 -040070 const auto desc = SkBitmapCacheDesc::Make(this);
71 if (SkBitmapCache::Find(desc, dst)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040072 SkASSERT(dst->isImmutable());
73 SkASSERT(dst->getPixels());
74 return true;
75 }
76
77 SkBitmapCache::RecPtr rec = nullptr;
78 SkPixmap pmap;
79 if (kAllow_CachingHint == chint) {
Brian Salomon5ad6fd32019-03-21 15:30:08 -040080 rec = SkBitmapCache::Alloc(desc, this->imageInfo(), &pmap);
Jim Van Verth8026ccc2018-10-04 13:10:39 -040081 if (!rec) {
82 return false;
83 }
84 } else {
Brian Salomon5ad6fd32019-03-21 15:30:08 -040085 if (!dst->tryAllocPixels(this->imageInfo()) || !dst->peekPixels(&pmap)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040086 return false;
87 }
88 }
89
Robert Phillips920d4882019-03-04 15:16:44 -050090 sk_sp<GrSurfaceContext> sContext = direct->priv().makeWrappedSurfaceContext(
Brian Salomon5ad6fd32019-03-21 15:30:08 -040091 this->asTextureProxyRef(direct), this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -040092 if (!sContext) {
93 return false;
94 }
95
96 if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), 0, 0)) {
97 return false;
98 }
99
100 if (rec) {
101 SkBitmapCache::Add(std::move(rec), dst);
102 this->notifyAddedToRasterCache();
103 }
104 return true;
105}
106
Robert Phillips6603a172019-03-05 12:35:44 -0500107sk_sp<SkImage> SkImage_GpuBase::onMakeSubset(GrRecordingContext* context,
108 const SkIRect& subset) const {
109 if (!context || !fContext->priv().matches(context)) {
110 return nullptr;
111 }
112
113 sk_sp<GrSurfaceProxy> proxy = this->asTextureProxyRef(context);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400114
Greg Daniel46cfbc62019-06-07 11:43:30 -0400115 sk_sp<GrTextureProxy> copyProxy = GrSurfaceProxy::Copy(
116 context, proxy.get(), GrMipMapped::kNo, subset, SkBackingFit::kExact,
117 proxy->isBudgeted());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400118
Greg Daniel46cfbc62019-06-07 11:43:30 -0400119 if (!copyProxy) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400120 return nullptr;
121 }
122
123 // MDB: this call is okay bc we know 'sContext' was kExact
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400124 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, this->alphaType(),
Greg Daniel46cfbc62019-06-07 11:43:30 -0400125 std::move(copyProxy), this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400126}
127
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400128static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
129 switch (info.colorType()) {
130 case kRGBA_8888_SkColorType:
131 case kBGRA_8888_SkColorType:
132 break;
133 default:
134 return; // nothing to do
135 }
136
Brian Salomon3f4cd772019-01-11 16:03:19 -0500137 // SkColor is not necessarily RGBA or BGRA, but it is one of them on little-endian,
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400138 // and in either case, the alpha-byte is always in the same place, so we can safely call
139 // SkPreMultiplyColor()
140 //
141 SkColor* row = (SkColor*)pixels;
142 for (int y = 0; y < info.height(); ++y) {
143 for (int x = 0; x < info.width(); ++x) {
144 row[x] = SkPreMultiplyColor(row[x]);
145 }
146 row = (SkColor*)((char*)(row)+rowBytes);
147 }
148}
149
150bool SkImage_GpuBase::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
151 int srcX, int srcY, CachingHint) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500152 auto direct = fContext->priv().asDirectContext();
153 if (!direct) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400154 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
155 return false;
156 }
157
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400158 if (!SkImageInfoValidConversion(dstInfo, this->imageInfo())) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400159 return false;
160 }
161
162 SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, srcX, srcY);
163 if (!rec.trim(this->width(), this->height())) {
164 return false;
165 }
166
167 // TODO: this seems to duplicate code in GrTextureContext::onReadPixels and
168 // GrRenderTargetContext::onReadPixels
169 uint32_t flags = 0;
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400170 if (kUnpremul_SkAlphaType == rec.fInfo.alphaType() &&
171 kPremul_SkAlphaType == this->alphaType()) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400172 // let the GPU perform this transformation for us
Greg Daniel6eb8c242019-06-05 10:22:24 -0400173 flags = GrSurfaceContext::kUnpremul_PixelOpsFlag;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400174 }
175
Robert Phillips920d4882019-03-04 15:16:44 -0500176 sk_sp<GrSurfaceContext> sContext = direct->priv().makeWrappedSurfaceContext(
Robert Phillips6603a172019-03-05 12:35:44 -0500177 this->asTextureProxyRef(direct), this->refColorSpace());
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400178 if (!sContext) {
179 return false;
180 }
181
182 if (!sContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY, flags)) {
183 return false;
184 }
185
186 // do we have to manually fix-up the alpha channel?
187 // src dst
188 // unpremul premul fix manually
189 // premul unpremul done by kUnpremul_PixelOpsFlag
190 // all other combos need to change.
191 //
192 // Should this be handled by Ganesh? todo:?
193 //
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400194 if (kPremul_SkAlphaType == rec.fInfo.alphaType() &&
195 kUnpremul_SkAlphaType == this->alphaType()) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400196 apply_premul(rec.fInfo, rec.fPixels, rec.fRowBytes);
197 }
198 return true;
199}
200
Robert Phillips9338c602019-02-19 12:52:29 -0500201sk_sp<GrTextureProxy> SkImage_GpuBase::asTextureProxyRef(GrRecordingContext* context,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400202 const GrSamplerState& params,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400203 SkScalar scaleAdjust[2]) const {
Robert Phillips6603a172019-03-05 12:35:44 -0500204 if (!context || !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400205 SkASSERT(0);
206 return nullptr;
207 }
208
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400209 GrTextureAdjuster adjuster(fContext.get(), this->asTextureProxyRef(context), this->alphaType(),
210 this->uniqueID(), this->colorSpace());
Brian Osman6064e1c2018-10-19 14:27:54 -0400211 return adjuster.refTextureProxyForParams(params, scaleAdjust);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400212}
213
214GrBackendTexture SkImage_GpuBase::onGetBackendTexture(bool flushPendingGrContextIO,
215 GrSurfaceOrigin* origin) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500216 auto direct = fContext->priv().asDirectContext();
217 if (!direct) {
218 // This image was created with a DDL context and cannot be instantiated.
Robert Phillips6603a172019-03-05 12:35:44 -0500219 return GrBackendTexture(); // invalid
Robert Phillips920d4882019-03-04 15:16:44 -0500220 }
221
Robert Phillips6603a172019-03-05 12:35:44 -0500222 sk_sp<GrTextureProxy> proxy = this->asTextureProxyRef(direct);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400223 SkASSERT(proxy);
224
Robert Phillips920d4882019-03-04 15:16:44 -0500225 if (!proxy->isInstantiated()) {
226 auto resourceProvider = direct->priv().resourceProvider();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400227
Robert Phillips920d4882019-03-04 15:16:44 -0500228 if (!proxy->instantiate(resourceProvider)) {
229 return GrBackendTexture(); // invalid
230 }
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400231 }
232
233 GrTexture* texture = proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400234 if (texture) {
235 if (flushPendingGrContextIO) {
Greg Daniel4aa13e72019-04-15 14:42:20 -0400236 direct->priv().flushSurface(proxy.get());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400237 }
238 if (origin) {
239 *origin = proxy->origin();
240 }
241 return texture->getBackendTexture();
242 }
243 return GrBackendTexture(); // invalid
244}
245
246GrTexture* SkImage_GpuBase::onGetTexture() const {
247 GrTextureProxy* proxy = this->peekProxy();
Robert Phillips193c4212019-03-04 12:18:53 -0500248 if (proxy && proxy->isInstantiated()) {
249 return proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400250 }
251
Robert Phillips193c4212019-03-04 12:18:53 -0500252 auto direct = fContext->priv().asDirectContext();
253 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400254 // This image was created with a DDL context and cannot be instantiated.
255 return nullptr;
256 }
257
Robert Phillips6603a172019-03-05 12:35:44 -0500258 sk_sp<GrTextureProxy> proxyRef = this->asTextureProxyRef(direct);
Robert Phillips193c4212019-03-04 12:18:53 -0500259 SkASSERT(proxyRef && !proxyRef->isInstantiated());
260
261 if (!proxyRef->instantiate(direct->priv().resourceProvider())) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400262 return nullptr;
263 }
264
Robert Phillips193c4212019-03-04 12:18:53 -0500265 return proxyRef->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400266}
267
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400268bool SkImage_GpuBase::onIsValid(GrContext* context) const {
269 // The base class has already checked that context isn't abandoned (if it's not nullptr)
Robert Phillips920d4882019-03-04 15:16:44 -0500270 if (fContext->priv().abandoned()) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400271 return false;
272 }
273
Robert Phillips920d4882019-03-04 15:16:44 -0500274 if (context && !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400275 return false;
276 }
277
278 return true;
279}
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400280
Jim Van Verth0e671942018-11-09 12:03:57 -0500281bool SkImage_GpuBase::MakeTempTextureProxies(GrContext* ctx, const GrBackendTexture yuvaTextures[],
Jim Van Verth53275362018-11-09 15:42:35 -0500282 int numTextures, const SkYUVAIndex yuvaIndices[4],
283 GrSurfaceOrigin imageOrigin,
Jim Van Verth0e671942018-11-09 12:03:57 -0500284 sk_sp<GrTextureProxy> tempTextureProxies[4]) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500285 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Jim Van Verth0e671942018-11-09 12:03:57 -0500286
287 // We need to make a copy of the input backend textures because we need to preserve the result
288 // of validate_backend_texture.
289 GrBackendTexture yuvaTexturesCopy[4];
290 for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) {
291 yuvaTexturesCopy[textureIndex] = yuvaTextures[textureIndex];
Brian Salomonf391d0f2018-12-14 09:18:50 -0500292 GrBackendFormat backendFormat = yuvaTexturesCopy[textureIndex].getBackendFormat();
293 if (!backendFormat.isValid()) {
294 return false;
295 }
296 yuvaTexturesCopy[textureIndex].fConfig =
Robert Phillips9da87e02019-02-04 13:26:26 -0500297 ctx->priv().caps()->getYUVAConfigFromBackendFormat(backendFormat);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500298 if (yuvaTexturesCopy[textureIndex].fConfig == kUnknown_GrPixelConfig) {
Jim Van Verth0e671942018-11-09 12:03:57 -0500299 return false;
300 }
301 SkASSERT(yuvaTexturesCopy[textureIndex].isValid());
302
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500303 tempTextureProxies[textureIndex] = proxyProvider->wrapBackendTexture(
304 yuvaTexturesCopy[textureIndex], imageOrigin, kBorrow_GrWrapOwnership,
305 GrWrapCacheable::kNo, kRead_GrIOType);
Jim Van Verth0e671942018-11-09 12:03:57 -0500306 if (!tempTextureProxies[textureIndex]) {
307 return false;
308 }
Jim Van Verth53275362018-11-09 15:42:35 -0500309
310 // Check that each texture contains the channel data for the corresponding YUVA index
311 GrPixelConfig config = yuvaTexturesCopy[textureIndex].fConfig;
312 for (int yuvaIndex = 0; yuvaIndex < SkYUVAIndex::kIndexCount; ++yuvaIndex) {
313 if (yuvaIndices[yuvaIndex].fIndex == textureIndex) {
314 switch (yuvaIndices[yuvaIndex].fChannel) {
315 case SkColorChannel::kR:
316 if (kAlpha_8_as_Alpha_GrPixelConfig == config) {
317 return false;
318 }
319 break;
320 case SkColorChannel::kG:
321 case SkColorChannel::kB:
322 if (kAlpha_8_as_Alpha_GrPixelConfig == config ||
323 kAlpha_8_as_Red_GrPixelConfig == config) {
324 return false;
325 }
326 break;
327 case SkColorChannel::kA:
328 default:
329 if (kRGB_888_GrPixelConfig == config) {
330 return false;
331 }
332 break;
333 }
334 }
335 }
Jim Van Verth0e671942018-11-09 12:03:57 -0500336 }
337
338 return true;
339}
340
341bool SkImage_GpuBase::RenderYUVAToRGBA(GrContext* ctx, GrRenderTargetContext* renderTargetContext,
342 const SkRect& rect, SkYUVColorSpace yuvColorSpace,
Brian Osmane9560492019-02-05 17:00:03 -0500343 sk_sp<GrColorSpaceXform> colorSpaceXform,
Jim Van Verth0e671942018-11-09 12:03:57 -0500344 const sk_sp<GrTextureProxy> proxies[4],
345 const SkYUVAIndex yuvaIndices[4]) {
346 SkASSERT(renderTargetContext);
347 if (!renderTargetContext->asSurfaceProxy()) {
348 return false;
349 }
350
351 GrPaint paint;
352 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
353
Brian Osmane9560492019-02-05 17:00:03 -0500354 auto fp = GrYUVtoRGBEffect::Make(proxies, yuvaIndices, yuvColorSpace,
355 GrSamplerState::Filter::kNearest);
356 if (colorSpaceXform) {
357 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(colorSpaceXform));
358 }
359 paint.addColorFragmentProcessor(std::move(fp));
Jim Van Verth0e671942018-11-09 12:03:57 -0500360
361 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Jim Van Verth0e671942018-11-09 12:03:57 -0500362 return true;
363}
364
Brian Salomonbe5a0932018-12-10 10:03:26 -0500365sk_sp<GrTextureProxy> SkImage_GpuBase::MakePromiseImageLazyProxy(
366 GrContext* context, int width, int height, GrSurfaceOrigin origin, GrPixelConfig config,
367 GrBackendFormat backendFormat, GrMipMapped mipMapped,
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500368 PromiseImageTextureFulfillProc fulfillProc,
369 PromiseImageTextureReleaseProc releaseProc,
370 PromiseImageTextureDoneProc doneProc,
Brian Salomon0cc57542019-03-08 13:28:46 -0500371 PromiseImageTextureContext textureContext,
372 PromiseImageApiVersion version) {
Brian Salomonbe5a0932018-12-10 10:03:26 -0500373 SkASSERT(context);
374 SkASSERT(width > 0 && height > 0);
375 SkASSERT(doneProc);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500376 SkASSERT(config != kUnknown_GrPixelConfig);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500377
378 if (!fulfillProc || !releaseProc) {
379 doneProc(textureContext);
380 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400381 }
382
Brian Salomonbe5a0932018-12-10 10:03:26 -0500383 if (mipMapped == GrMipMapped::kYes &&
384 GrTextureTypeHasRestrictedSampling(backendFormat.textureType())) {
385 // It is invalid to have a GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE and have mips as
386 // well.
387 doneProc(textureContext);
388 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400389 }
Brian Salomonbe5a0932018-12-10 10:03:26 -0500390
391 /**
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500392 * This class is the lazy instantiation callback for promise images. It manages calling the
393 * client's Fulfill, Release, and Done procs. It attempts to reuse a GrTexture instance in
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500394 * cases where the client provides the same SkPromiseImageTexture as Fulfill results for
395 * multiple SkImages. The created GrTexture is given a key based on a unique ID associated with
396 * the SkPromiseImageTexture.
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500397 *
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500398 * The GrTexutre idle proc mechanism is used to call the Release and Done procs. We use this
399 * instead of the GrSurface release proc because the GrTexture is cached and therefore may
400 * outlive the proxy into which this callback is installed.
401 *
402 * A key invalidation message is installed on the SkPromiseImageTexture so that the GrTexture
403 * is deleted once it can no longer be used to instantiate a proxy.
Brian Salomonbe5a0932018-12-10 10:03:26 -0500404 */
405 class PromiseLazyInstantiateCallback {
406 public:
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500407 PromiseLazyInstantiateCallback(PromiseImageTextureFulfillProc fulfillProc,
408 PromiseImageTextureReleaseProc releaseProc,
409 PromiseImageTextureDoneProc doneProc,
410 PromiseImageTextureContext context,
Brian Salomon0cc57542019-03-08 13:28:46 -0500411 GrPixelConfig config,
412 PromiseImageApiVersion version)
413 : fFulfillProc(fulfillProc)
414 , fReleaseProc(releaseProc)
415 , fConfig(config)
416 , fVersion(version) {
Brian Salomone80b8092019-03-08 13:25:19 -0500417 fDoneCallback = sk_make_sp<GrRefCntedCallback>(doneProc, context);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500418 }
419 PromiseLazyInstantiateCallback(PromiseLazyInstantiateCallback&&) = default;
420 PromiseLazyInstantiateCallback(const PromiseLazyInstantiateCallback&) {
421 // Because we get wrapped in std::function we must be copyable. But we should never
422 // be copied.
423 SkASSERT(false);
424 }
425 PromiseLazyInstantiateCallback& operator=(PromiseLazyInstantiateCallback&&) = default;
426 PromiseLazyInstantiateCallback& operator=(const PromiseLazyInstantiateCallback&) {
427 SkASSERT(false);
428 return *this;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500429 }
Brian Salomon553610d2019-01-14 17:34:12 -0500430
Brian Salomon88b8d112019-03-07 15:25:34 +0000431 ~PromiseLazyInstantiateCallback() {
Brian Salomon876a0172019-03-08 11:12:14 -0500432 // Our destructor can run on any thread. We trigger the unref of fTexture by message.
433 if (fTexture) {
Brian Salomon876a0172019-03-08 11:12:14 -0500434 SkMessageBus<GrGpuResourceFreedMessage>::Post({fTexture, fTextureContextID});
435 }
Brian Salomon88b8d112019-03-07 15:25:34 +0000436 }
437
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400438 GrSurfaceProxy::LazyInstantiationResult operator()(GrResourceProvider* resourceProvider) {
439 // We use the unique key in a way that is unrelated to the SkImage-based key that the
440 // proxy may receive, hence kUnsynced.
441 static constexpr auto kKeySyncMode =
442 GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
443
Brian Salomon876a0172019-03-08 11:12:14 -0500444 // Our proxy is getting instantiated for the second+ time. We are only allowed to call
445 // Fulfill once. So return our cached result.
446 if (fTexture) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400447 return {sk_ref_sp(fTexture), kKeySyncMode};
Brian Salomond538d3d2019-04-04 12:18:17 -0400448 } else if (fConfig == kUnknown_GrPixelConfig) {
449 // We've already called fulfill and it failed. Our contract says that we should only
450 // call each callback once.
451 return {};
Brian Salomon876a0172019-03-08 11:12:14 -0500452 }
Brian Salomone80b8092019-03-08 13:25:19 -0500453 SkASSERT(fDoneCallback);
454 PromiseImageTextureContext textureContext = fDoneCallback->context();
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500455 sk_sp<SkPromiseImageTexture> promiseTexture = fFulfillProc(textureContext);
456 // From here on out our contract is that the release proc must be called, even if
457 // the return from fulfill was invalid or we fail for some other reason.
Brian Salomone80b8092019-03-08 13:25:19 -0500458 auto releaseCallback = sk_make_sp<GrRefCntedCallback>(fReleaseProc, textureContext);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500459 if (!promiseTexture) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400460 // This records that we have failed.
461 fConfig = kUnknown_GrPixelConfig;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400462 return {};
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500463 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500464
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500465 auto backendTexture = promiseTexture->backendTexture();
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500466 backendTexture.fConfig = fConfig;
467 if (!backendTexture.isValid()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400468 return {};
Brian Salomon559c6172019-01-10 10:23:44 -0500469 }
470
Brian Salomonf55e8d52019-01-30 17:28:20 -0500471 sk_sp<GrTexture> tex;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500472 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Salomon7d88f312019-02-28 10:03:03 -0500473 GrUniqueKey key;
474 GrUniqueKey::Builder builder(&key, kDomain, 2, "promise");
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500475 builder[0] = promiseTexture->uniqueID();
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500476 builder[1] = fConfig;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500477 builder.finish();
Brian Salomon0d606762019-01-25 09:58:38 -0500478 // A texture with this key may already exist from a different instance of this lazy
479 // callback. This could happen if the client fulfills a promise image with a texture
480 // that was previously used to fulfill a different promise image.
Brian Salomon7d88f312019-02-28 10:03:03 -0500481 if (auto surf = resourceProvider->findByUniqueKey<GrSurface>(key)) {
Brian Salomon0d606762019-01-25 09:58:38 -0500482 tex = sk_ref_sp(surf->asTexture());
483 SkASSERT(tex);
484 } else {
485 if ((tex = resourceProvider->wrapBackendTexture(
486 backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kYes,
487 kRead_GrIOType))) {
Brian Salomon7d88f312019-02-28 10:03:03 -0500488 tex->resourcePriv().setUniqueKey(key);
Brian Salomon0d606762019-01-25 09:58:38 -0500489 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400490 return {};
Brian Salomon0d606762019-01-25 09:58:38 -0500491 }
492 }
Brian Salomon0cc57542019-03-08 13:28:46 -0500493 auto releaseIdleState = fVersion == PromiseImageApiVersion::kLegacy
494 ? GrTexture::IdleState::kFinished
495 : GrTexture::IdleState::kFlushed;
496 tex->addIdleProc(std::move(releaseCallback), releaseIdleState);
Brian Salomone80b8092019-03-08 13:25:19 -0500497 tex->addIdleProc(std::move(fDoneCallback), GrTexture::IdleState::kFinished);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500498 promiseTexture->addKeyToInvalidate(tex->getContext()->priv().contextID(), key);
Brian Salomon876a0172019-03-08 11:12:14 -0500499 fTexture = tex.get();
500 // We need to hold on to the GrTexture in case our proxy gets reinstantiated. However,
501 // we can't unref in our destructor because we may be on another thread then. So we
502 // let the cache know it is waiting on an unref message. We will send that message from
503 // our destructor.
504 GrContext* context = fTexture->getContext();
505 context->priv().getResourceCache()->insertDelayedResourceUnref(fTexture);
506 fTextureContextID = context->priv().contextID();
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400507 return {std::move(tex), kKeySyncMode};
Brian Salomonbe5a0932018-12-10 10:03:26 -0500508 }
509
510 private:
Brian Salomone80b8092019-03-08 13:25:19 -0500511 PromiseImageTextureFulfillProc fFulfillProc;
512 PromiseImageTextureReleaseProc fReleaseProc;
513 sk_sp<GrRefCntedCallback> fDoneCallback;
Brian Salomon876a0172019-03-08 11:12:14 -0500514 GrTexture* fTexture = nullptr;
515 uint32_t fTextureContextID = SK_InvalidUniqueID;
Brian Salomon553610d2019-01-14 17:34:12 -0500516 GrPixelConfig fConfig;
Brian Salomon0cc57542019-03-08 13:28:46 -0500517 PromiseImageApiVersion fVersion;
518 } callback(fulfillProc, releaseProc, doneProc, textureContext, config, version);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500519
Robert Phillips9da87e02019-02-04 13:26:26 -0500520 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonbe5a0932018-12-10 10:03:26 -0500521
522 GrSurfaceDesc desc;
523 desc.fWidth = width;
524 desc.fHeight = height;
525 desc.fConfig = config;
526
527 // We pass kReadOnly here since we should treat content of the client's texture as immutable.
528 return proxyProvider->createLazyProxy(std::move(callback), backendFormat, desc, origin,
529 mipMapped, GrInternalSurfaceFlags::kReadOnly,
530 SkBackingFit::kExact, SkBudgeted::kNo,
Brian Salomon876a0172019-03-08 11:12:14 -0500531 GrSurfaceProxy::LazyInstantiationType::kDeinstantiate);
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400532}