blob: 594fd05bd90f64abd3f90b9f7467db53aa2c2bab [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
Brian Salomoncdd8a0a2019-01-10 12:09:52 -05008#include "SkImage_GpuBase.h"
Jim Van Verth8026ccc2018-10-04 13:10:39 -04009#include "GrBackendSurface.h"
10#include "GrClip.h"
11#include "GrContext.h"
12#include "GrContextPriv.h"
13#include "GrRenderTargetContext.h"
14#include "GrTexture.h"
15#include "GrTextureAdjuster.h"
16#include "SkBitmapCache.h"
17#include "SkImage_Gpu.h"
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050018#include "SkPromiseImageTexture.h"
Jim Van Verth8bbce0e2018-10-08 14:34:52 -040019#include "SkReadPixelsRec.h"
Brian Salomon1bf0ed82019-01-16 13:51:35 -050020#include "SkTLList.h"
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050021#include "effects/GrYUVtoRGBEffect.h"
Jim Van Verth8026ccc2018-10-04 13:10:39 -040022
23SkImage_GpuBase::SkImage_GpuBase(sk_sp<GrContext> context, int width, int height, uint32_t uniqueID,
Brian Salomonf05e6d32018-12-20 08:41:41 -050024 SkAlphaType at, sk_sp<SkColorSpace> cs)
Jim Van Verth8026ccc2018-10-04 13:10:39 -040025 : INHERITED(width, height, uniqueID)
26 , fContext(std::move(context))
27 , fAlphaType(at)
Jim Van Verth8026ccc2018-10-04 13:10:39 -040028 , fColorSpace(std::move(cs)) {}
29
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) {
80 rec = SkBitmapCache::Alloc(desc, this->onImageInfo(), &pmap);
81 if (!rec) {
82 return false;
83 }
84 } else {
85 if (!dst->tryAllocPixels(this->onImageInfo()) || !dst->peekPixels(&pmap)) {
86 return false;
87 }
88 }
89
Robert Phillips920d4882019-03-04 15:16:44 -050090 sk_sp<GrSurfaceContext> sContext = direct->priv().makeWrappedSurfaceContext(
Jim Van Verth8026ccc2018-10-04 13:10:39 -040091 this->asTextureProxyRef(),
92 fColorSpace);
93 if (!sContext) {
94 return false;
95 }
96
97 if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), 0, 0)) {
98 return false;
99 }
100
101 if (rec) {
102 SkBitmapCache::Add(std::move(rec), dst);
103 this->notifyAddedToRasterCache();
104 }
105 return true;
106}
107
108sk_sp<SkImage> SkImage_GpuBase::onMakeSubset(const SkIRect& subset) const {
109 sk_sp<GrSurfaceProxy> proxy = this->asTextureProxyRef();
110
111 GrSurfaceDesc desc;
112 desc.fWidth = subset.width();
113 desc.fHeight = subset.height();
114 desc.fConfig = proxy->config();
115
Greg Daniel4065d452018-11-16 15:43:41 -0500116 GrBackendFormat format = proxy->backendFormat().makeTexture2D();
117 if (!format.isValid()) {
118 return nullptr;
119 }
120
Brian Salomonf05e6d32018-12-20 08:41:41 -0500121 // TODO: Should this inherit our proxy's budgeted status?
Robert Phillips9da87e02019-02-04 13:26:26 -0500122 sk_sp<GrSurfaceContext> sContext(fContext->priv().makeDeferredSurfaceContext(
Brian Salomonf05e6d32018-12-20 08:41:41 -0500123 format, desc, proxy->origin(), GrMipMapped::kNo, SkBackingFit::kExact,
124 proxy->isBudgeted()));
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400125 if (!sContext) {
126 return nullptr;
127 }
128
129 if (!sContext->copy(proxy.get(), subset, SkIPoint::Make(0, 0))) {
130 return nullptr;
131 }
132
133 // MDB: this call is okay bc we know 'sContext' was kExact
Brian Salomonf05e6d32018-12-20 08:41:41 -0500134 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, fAlphaType,
Brian Osmane9560492019-02-05 17:00:03 -0500135 sContext->asTextureProxyRef(), this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400136}
137
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400138static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
139 switch (info.colorType()) {
140 case kRGBA_8888_SkColorType:
141 case kBGRA_8888_SkColorType:
142 break;
143 default:
144 return; // nothing to do
145 }
146
Brian Salomon3f4cd772019-01-11 16:03:19 -0500147 // SkColor is not necessarily RGBA or BGRA, but it is one of them on little-endian,
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400148 // and in either case, the alpha-byte is always in the same place, so we can safely call
149 // SkPreMultiplyColor()
150 //
151 SkColor* row = (SkColor*)pixels;
152 for (int y = 0; y < info.height(); ++y) {
153 for (int x = 0; x < info.width(); ++x) {
154 row[x] = SkPreMultiplyColor(row[x]);
155 }
156 row = (SkColor*)((char*)(row)+rowBytes);
157 }
158}
159
160bool SkImage_GpuBase::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
161 int srcX, int srcY, CachingHint) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500162 auto direct = fContext->priv().asDirectContext();
163 if (!direct) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400164 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
165 return false;
166 }
167
168 if (!SkImageInfoValidConversion(dstInfo, this->onImageInfo())) {
169 return false;
170 }
171
172 SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, srcX, srcY);
173 if (!rec.trim(this->width(), this->height())) {
174 return false;
175 }
176
177 // TODO: this seems to duplicate code in GrTextureContext::onReadPixels and
178 // GrRenderTargetContext::onReadPixels
179 uint32_t flags = 0;
180 if (kUnpremul_SkAlphaType == rec.fInfo.alphaType() && kPremul_SkAlphaType == fAlphaType) {
181 // let the GPU perform this transformation for us
182 flags = GrContextPriv::kUnpremul_PixelOpsFlag;
183 }
184
Robert Phillips920d4882019-03-04 15:16:44 -0500185 sk_sp<GrSurfaceContext> sContext = direct->priv().makeWrappedSurfaceContext(
Brian Osmane9560492019-02-05 17:00:03 -0500186 this->asTextureProxyRef(), this->refColorSpace());
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400187 if (!sContext) {
188 return false;
189 }
190
191 if (!sContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY, flags)) {
192 return false;
193 }
194
195 // do we have to manually fix-up the alpha channel?
196 // src dst
197 // unpremul premul fix manually
198 // premul unpremul done by kUnpremul_PixelOpsFlag
199 // all other combos need to change.
200 //
201 // Should this be handled by Ganesh? todo:?
202 //
203 if (kPremul_SkAlphaType == rec.fInfo.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
204 apply_premul(rec.fInfo, rec.fPixels, rec.fRowBytes);
205 }
206 return true;
207}
208
Robert Phillips9338c602019-02-19 12:52:29 -0500209sk_sp<GrTextureProxy> SkImage_GpuBase::asTextureProxyRef(GrRecordingContext* context,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400210 const GrSamplerState& params,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400211 SkScalar scaleAdjust[2]) const {
Robert Phillipsfe0963c2019-02-07 13:25:07 -0500212 if (!fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400213 SkASSERT(0);
214 return nullptr;
215 }
216
217 GrTextureAdjuster adjuster(fContext.get(), this->asTextureProxyRef(), fAlphaType,
218 this->uniqueID(), fColorSpace.get());
Brian Osman6064e1c2018-10-19 14:27:54 -0400219 return adjuster.refTextureProxyForParams(params, scaleAdjust);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400220}
221
222GrBackendTexture SkImage_GpuBase::onGetBackendTexture(bool flushPendingGrContextIO,
223 GrSurfaceOrigin* origin) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500224 auto direct = fContext->priv().asDirectContext();
225 if (!direct) {
226 // This image was created with a DDL context and cannot be instantiated.
227 return GrBackendTexture();
228 }
229
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400230 sk_sp<GrTextureProxy> proxy = this->asTextureProxyRef();
231 SkASSERT(proxy);
232
Robert Phillips920d4882019-03-04 15:16:44 -0500233 if (!proxy->isInstantiated()) {
234 auto resourceProvider = direct->priv().resourceProvider();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400235
Robert Phillips920d4882019-03-04 15:16:44 -0500236 if (!proxy->instantiate(resourceProvider)) {
237 return GrBackendTexture(); // invalid
238 }
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400239 }
240
241 GrTexture* texture = proxy->peekTexture();
242
243 if (texture) {
244 if (flushPendingGrContextIO) {
Robert Phillips920d4882019-03-04 15:16:44 -0500245 direct->priv().prepareSurfaceForExternalIO(proxy.get());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400246 }
247 if (origin) {
248 *origin = proxy->origin();
249 }
250 return texture->getBackendTexture();
251 }
252 return GrBackendTexture(); // invalid
253}
254
255GrTexture* SkImage_GpuBase::onGetTexture() const {
256 GrTextureProxy* proxy = this->peekProxy();
Robert Phillips193c4212019-03-04 12:18:53 -0500257 if (proxy && proxy->isInstantiated()) {
258 return proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400259 }
260
Robert Phillips193c4212019-03-04 12:18:53 -0500261 auto direct = fContext->priv().asDirectContext();
262 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400263 // This image was created with a DDL context and cannot be instantiated.
264 return nullptr;
265 }
266
Robert Phillips193c4212019-03-04 12:18:53 -0500267 sk_sp<GrTextureProxy> proxyRef = this->asTextureProxyRef();
268 SkASSERT(proxyRef && !proxyRef->isInstantiated());
269
270 if (!proxyRef->instantiate(direct->priv().resourceProvider())) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400271 return nullptr;
272 }
273
Robert Phillips193c4212019-03-04 12:18:53 -0500274 return proxyRef->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400275}
276
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400277bool SkImage_GpuBase::onIsValid(GrContext* context) const {
278 // The base class has already checked that context isn't abandoned (if it's not nullptr)
Robert Phillips920d4882019-03-04 15:16:44 -0500279 if (fContext->priv().abandoned()) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400280 return false;
281 }
282
Robert Phillips920d4882019-03-04 15:16:44 -0500283 if (context && !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400284 return false;
285 }
286
287 return true;
288}
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400289
Jim Van Verth0e671942018-11-09 12:03:57 -0500290bool SkImage_GpuBase::MakeTempTextureProxies(GrContext* ctx, const GrBackendTexture yuvaTextures[],
Jim Van Verth53275362018-11-09 15:42:35 -0500291 int numTextures, const SkYUVAIndex yuvaIndices[4],
292 GrSurfaceOrigin imageOrigin,
Jim Van Verth0e671942018-11-09 12:03:57 -0500293 sk_sp<GrTextureProxy> tempTextureProxies[4]) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500294 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Jim Van Verth0e671942018-11-09 12:03:57 -0500295
296 // We need to make a copy of the input backend textures because we need to preserve the result
297 // of validate_backend_texture.
298 GrBackendTexture yuvaTexturesCopy[4];
299 for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) {
300 yuvaTexturesCopy[textureIndex] = yuvaTextures[textureIndex];
Brian Salomonf391d0f2018-12-14 09:18:50 -0500301 GrBackendFormat backendFormat = yuvaTexturesCopy[textureIndex].getBackendFormat();
302 if (!backendFormat.isValid()) {
303 return false;
304 }
305 yuvaTexturesCopy[textureIndex].fConfig =
Robert Phillips9da87e02019-02-04 13:26:26 -0500306 ctx->priv().caps()->getYUVAConfigFromBackendFormat(backendFormat);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500307 if (yuvaTexturesCopy[textureIndex].fConfig == kUnknown_GrPixelConfig) {
Jim Van Verth0e671942018-11-09 12:03:57 -0500308 return false;
309 }
310 SkASSERT(yuvaTexturesCopy[textureIndex].isValid());
311
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500312 tempTextureProxies[textureIndex] = proxyProvider->wrapBackendTexture(
313 yuvaTexturesCopy[textureIndex], imageOrigin, kBorrow_GrWrapOwnership,
314 GrWrapCacheable::kNo, kRead_GrIOType);
Jim Van Verth0e671942018-11-09 12:03:57 -0500315 if (!tempTextureProxies[textureIndex]) {
316 return false;
317 }
Jim Van Verth53275362018-11-09 15:42:35 -0500318
319 // Check that each texture contains the channel data for the corresponding YUVA index
320 GrPixelConfig config = yuvaTexturesCopy[textureIndex].fConfig;
321 for (int yuvaIndex = 0; yuvaIndex < SkYUVAIndex::kIndexCount; ++yuvaIndex) {
322 if (yuvaIndices[yuvaIndex].fIndex == textureIndex) {
323 switch (yuvaIndices[yuvaIndex].fChannel) {
324 case SkColorChannel::kR:
325 if (kAlpha_8_as_Alpha_GrPixelConfig == config) {
326 return false;
327 }
328 break;
329 case SkColorChannel::kG:
330 case SkColorChannel::kB:
331 if (kAlpha_8_as_Alpha_GrPixelConfig == config ||
332 kAlpha_8_as_Red_GrPixelConfig == config) {
333 return false;
334 }
335 break;
336 case SkColorChannel::kA:
337 default:
338 if (kRGB_888_GrPixelConfig == config) {
339 return false;
340 }
341 break;
342 }
343 }
344 }
Jim Van Verth0e671942018-11-09 12:03:57 -0500345 }
346
347 return true;
348}
349
350bool SkImage_GpuBase::RenderYUVAToRGBA(GrContext* ctx, GrRenderTargetContext* renderTargetContext,
351 const SkRect& rect, SkYUVColorSpace yuvColorSpace,
Brian Osmane9560492019-02-05 17:00:03 -0500352 sk_sp<GrColorSpaceXform> colorSpaceXform,
Jim Van Verth0e671942018-11-09 12:03:57 -0500353 const sk_sp<GrTextureProxy> proxies[4],
354 const SkYUVAIndex yuvaIndices[4]) {
355 SkASSERT(renderTargetContext);
356 if (!renderTargetContext->asSurfaceProxy()) {
357 return false;
358 }
359
360 GrPaint paint;
361 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
362
Brian Osmane9560492019-02-05 17:00:03 -0500363 auto fp = GrYUVtoRGBEffect::Make(proxies, yuvaIndices, yuvColorSpace,
364 GrSamplerState::Filter::kNearest);
365 if (colorSpaceXform) {
366 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(colorSpaceXform));
367 }
368 paint.addColorFragmentProcessor(std::move(fp));
Jim Van Verth0e671942018-11-09 12:03:57 -0500369
370 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Mike Reedfbc887d2019-03-05 01:32:07 +0000371
372 // DDL TODO: in the promise image version we must not flush here
373 ctx->priv().flushSurfaceWrites(renderTargetContext->asSurfaceProxy());
374
Jim Van Verth0e671942018-11-09 12:03:57 -0500375 return true;
376}
377
Brian Salomonbe5a0932018-12-10 10:03:26 -0500378sk_sp<GrTextureProxy> SkImage_GpuBase::MakePromiseImageLazyProxy(
379 GrContext* context, int width, int height, GrSurfaceOrigin origin, GrPixelConfig config,
380 GrBackendFormat backendFormat, GrMipMapped mipMapped,
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500381 PromiseImageTextureFulfillProc fulfillProc,
382 PromiseImageTextureReleaseProc releaseProc,
383 PromiseImageTextureDoneProc doneProc,
Brian Salomon7d88f312019-02-28 10:03:03 -0500384 PromiseImageTextureContext textureContext) {
Brian Salomonbe5a0932018-12-10 10:03:26 -0500385 SkASSERT(context);
386 SkASSERT(width > 0 && height > 0);
387 SkASSERT(doneProc);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500388 SkASSERT(config != kUnknown_GrPixelConfig);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500389
390 if (!fulfillProc || !releaseProc) {
391 doneProc(textureContext);
392 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400393 }
394
Brian Salomonbe5a0932018-12-10 10:03:26 -0500395 if (mipMapped == GrMipMapped::kYes &&
396 GrTextureTypeHasRestrictedSampling(backendFormat.textureType())) {
397 // It is invalid to have a GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE and have mips as
398 // well.
399 doneProc(textureContext);
400 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400401 }
Brian Salomonbe5a0932018-12-10 10:03:26 -0500402
403 /**
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500404 * This class is the lazy instantiation callback for promise images. It manages calling the
405 * client's Fulfill, Release, and Done procs. It attempts to reuse a GrTexture instance in
406 * cases where the client provides the same SkPromiseImageTexture for successive Fulfill calls.
407 * The created GrTexture is given a key based on a unique ID associated with the
408 * SkPromiseImageTexture. When the texture enters "idle" state (meaning it is not being used by
409 * the GPU and is at rest in the resource cache) the client's Release proc is called
410 * using GrTexture's idle proc mechanism. If the same SkPromiseImageTexture is provided for
411 * another fulfill we find the cached GrTexture. If the proxy, and therefore this object,
412 * is destroyed, we invalidate the GrTexture's key. Also if the client overwrites or
413 * destroys their SkPromiseImageTexture we invalidate the key.
414 *
415 * Currently a GrTexture is only reused for a given SkPromiseImageTexture if the
416 * SkPromiseImageTexture is reused in Fulfill for the same promise SkImage. However, we'd
417 * like to relax that so that a SkPromiseImageTexture can be reused with different promise
418 * SkImages that will reuse a single GrTexture.
Brian Salomonbe5a0932018-12-10 10:03:26 -0500419 */
420 class PromiseLazyInstantiateCallback {
421 public:
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500422 PromiseLazyInstantiateCallback(PromiseImageTextureFulfillProc fulfillProc,
423 PromiseImageTextureReleaseProc releaseProc,
424 PromiseImageTextureDoneProc doneProc,
425 PromiseImageTextureContext context,
Brian Salomonbe5a0932018-12-10 10:03:26 -0500426 GrPixelConfig config)
Brian Salomon7d88f312019-02-28 10:03:03 -0500427 : fFulfillProc(fulfillProc), fConfig(config) {
Brian Salomon553610d2019-01-14 17:34:12 -0500428 auto doneHelper = sk_make_sp<GrReleaseProcHelper>(doneProc, context);
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500429 fReleaseContext = sk_make_sp<IdleContext::PromiseImageReleaseContext>(
430 releaseProc, context, std::move(doneHelper));
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500431 }
Brian Salomon553610d2019-01-14 17:34:12 -0500432
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500433 ~PromiseLazyInstantiateCallback() = default;
Brian Salomonbe5a0932018-12-10 10:03:26 -0500434
435 sk_sp<GrSurface> operator()(GrResourceProvider* resourceProvider) {
Brian Salomon7d88f312019-02-28 10:03:03 -0500436 if (fTexture) {
437 return fTexture;
Brian Salomonf55e8d52019-01-30 17:28:20 -0500438 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500439
440 sk_sp<GrTexture> cachedTexture;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500441 GrBackendTexture backendTexture;
Brian Salomon553610d2019-01-14 17:34:12 -0500442 sk_sp<SkPromiseImageTexture> promiseTexture =
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500443 fFulfillProc(fReleaseContext->textureContext());
444 fReleaseContext->notifyWasFulfilled();
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500445 if (!promiseTexture) {
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500446 fReleaseContext->release();
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500447 return sk_sp<GrTexture>();
448 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500449
450 backendTexture = promiseTexture->backendTexture();
451 backendTexture.fConfig = fConfig;
452 if (!backendTexture.isValid()) {
453 // Even though the GrBackendTexture is not valid, we must call the release
454 // proc to keep our contract of always calling Fulfill and Release in pairs.
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500455 fReleaseContext->release();
Brian Salomon426ba462019-01-10 16:33:06 +0000456 return sk_sp<GrTexture>();
Brian Salomon559c6172019-01-10 10:23:44 -0500457 }
458
Brian Salomonf55e8d52019-01-30 17:28:20 -0500459 sk_sp<GrTexture> tex;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500460 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Salomon7d88f312019-02-28 10:03:03 -0500461 GrUniqueKey key;
462 GrUniqueKey::Builder builder(&key, kDomain, 2, "promise");
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500463 builder[0] = promiseTexture->uniqueID();
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500464 builder[1] = fConfig;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500465 builder.finish();
Brian Salomon0d606762019-01-25 09:58:38 -0500466 // A texture with this key may already exist from a different instance of this lazy
467 // callback. This could happen if the client fulfills a promise image with a texture
468 // that was previously used to fulfill a different promise image.
Brian Salomon7d88f312019-02-28 10:03:03 -0500469 if (auto surf = resourceProvider->findByUniqueKey<GrSurface>(key)) {
Brian Salomon0d606762019-01-25 09:58:38 -0500470 tex = sk_ref_sp(surf->asTexture());
471 SkASSERT(tex);
472 } else {
473 if ((tex = resourceProvider->wrapBackendTexture(
474 backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kYes,
475 kRead_GrIOType))) {
Brian Salomon7d88f312019-02-28 10:03:03 -0500476 tex->resourcePriv().setUniqueKey(key);
Brian Salomon0d606762019-01-25 09:58:38 -0500477 } else {
478 // Even though we failed to wrap the backend texture, we must call the release
479 // proc to keep our contract of always calling Fulfill and Release in pairs.
480 fReleaseContext->release();
481 return sk_sp<GrTexture>();
482 }
483 }
484 this->addToIdleContext(tex.get());
Brian Salomon7d88f312019-02-28 10:03:03 -0500485 fTexture = tex;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500486 SkASSERT(fContextID == SK_InvalidUniqueID ||
Robert Phillips9da87e02019-02-04 13:26:26 -0500487 fContextID == tex->getContext()->priv().contextID());
488 fContextID = tex->getContext()->priv().contextID();
Brian Salomon7d88f312019-02-28 10:03:03 -0500489 promiseTexture->addKeyToInvalidate(fContextID, key);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500490 return std::move(tex);
491 }
492
493 private:
Brian Salomon553610d2019-01-14 17:34:12 -0500494 // The GrTexture's idle callback mechanism is used to call the client's Release proc via
495 // this class. This also owns a ref counted helper that calls the client's ReleaseProc when
496 // the ref count reaches zero. The callback and any Fulfilled but un-Released texture share
497 // ownership of the IdleContext. Thus, the IdleContext is destroyed and calls the Done proc
498 // after the last fulfilled texture goes idle and calls the Release proc or the proxy's
499 // destructor destroys the lazy callback, whichever comes last.
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500500 class IdleContext {
Brian Salomon553610d2019-01-14 17:34:12 -0500501 public:
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500502 class PromiseImageReleaseContext;
Brian Salomon553610d2019-01-14 17:34:12 -0500503
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500504 IdleContext() = default;
Brian Salomon553610d2019-01-14 17:34:12 -0500505
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500506 ~IdleContext() = default;
Brian Salomon553610d2019-01-14 17:34:12 -0500507
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500508 void addImageReleaseContext(sk_sp<PromiseImageReleaseContext> context) {
509 fReleaseContexts.addToHead(std::move(context));
Brian Salomon553610d2019-01-14 17:34:12 -0500510 }
511
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500512 static void IdleProc(void* context) {
513 IdleContext* idleContext = static_cast<IdleContext*>(context);
514 for (ReleaseContextList::Iter iter = idleContext->fReleaseContexts.headIter();
515 iter.get();
516 iter.next()) {
517 (*iter.get())->release();
518 }
519 idleContext->fReleaseContexts.reset();
520 delete idleContext;
521 }
522
523 class PromiseImageReleaseContext : public SkNVRefCnt<PromiseImageReleaseContext> {
524 public:
525 PromiseImageReleaseContext(PromiseImageTextureReleaseProc releaseProc,
526 PromiseImageTextureContext textureContext,
527 sk_sp<GrReleaseProcHelper> doneHelper)
528 : fReleaseProc(releaseProc)
529 , fTextureContext(textureContext)
530 , fDoneHelper(std::move(doneHelper)) {}
531
532 ~PromiseImageReleaseContext() { SkASSERT(fIsReleased); }
533
534 void release() {
535 SkASSERT(!fIsReleased);
536 fReleaseProc(fTextureContext);
537 fIsReleased = true;
538 }
539
540 void notifyWasFulfilled() { fIsReleased = false; }
541 bool isReleased() const { return fIsReleased; }
542
543 PromiseImageTextureContext textureContext() const { return fTextureContext; }
544
545 private:
546 PromiseImageTextureReleaseProc fReleaseProc;
547 PromiseImageTextureContext fTextureContext;
548 sk_sp<GrReleaseProcHelper> fDoneHelper;
549 bool fIsReleased = true;
550 };
551
Brian Salomon553610d2019-01-14 17:34:12 -0500552 private:
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500553 using ReleaseContextList = SkTLList<sk_sp<PromiseImageReleaseContext>, 4>;
554 ReleaseContextList fReleaseContexts;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500555 };
Brian Salomonbe5a0932018-12-10 10:03:26 -0500556
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500557 void addToIdleContext(GrTexture* texture) {
558 SkASSERT(!fReleaseContext->isReleased());
559 IdleContext* idleContext = static_cast<IdleContext*>(texture->idleContext());
560 if (!idleContext) {
561 idleContext = new IdleContext();
562 texture->setIdleProc(IdleContext::IdleProc, idleContext);
563 }
564 idleContext->addImageReleaseContext(fReleaseContext);
565 }
566
567 sk_sp<IdleContext::PromiseImageReleaseContext> fReleaseContext;
Brian Salomon7d88f312019-02-28 10:03:03 -0500568 sk_sp<GrTexture> fTexture;
Brian Salomon553610d2019-01-14 17:34:12 -0500569 PromiseImageTextureFulfillProc fFulfillProc;
570 GrPixelConfig fConfig;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500571
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500572 // ID of the GrContext that we are interacting with.
573 uint32_t fContextID = SK_InvalidUniqueID;
Brian Salomon7d88f312019-02-28 10:03:03 -0500574 } callback(fulfillProc, releaseProc, doneProc, textureContext, config);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500575
Robert Phillips9da87e02019-02-04 13:26:26 -0500576 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonbe5a0932018-12-10 10:03:26 -0500577
578 GrSurfaceDesc desc;
579 desc.fWidth = width;
580 desc.fHeight = height;
581 desc.fConfig = config;
582
583 // We pass kReadOnly here since we should treat content of the client's texture as immutable.
584 return proxyProvider->createLazyProxy(std::move(callback), backendFormat, desc, origin,
585 mipMapped, GrInternalSurfaceFlags::kReadOnly,
586 SkBackingFit::kExact, SkBudgeted::kNo,
587 GrSurfaceProxy::LazyInstantiationType::kDeinstantiate);
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400588}