blob: 73aecf4414907243a75909f995780df6854694b5 [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 Salomoncdd8a0a2019-01-10 12:09:52 -050020#include "effects/GrYUVtoRGBEffect.h"
Jim Van Verth8026ccc2018-10-04 13:10:39 -040021
22SkImage_GpuBase::SkImage_GpuBase(sk_sp<GrContext> context, int width, int height, uint32_t uniqueID,
Brian Salomonf05e6d32018-12-20 08:41:41 -050023 SkAlphaType at, sk_sp<SkColorSpace> cs)
Jim Van Verth8026ccc2018-10-04 13:10:39 -040024 : INHERITED(width, height, uniqueID)
25 , fContext(std::move(context))
26 , fAlphaType(at)
Jim Van Verth8026ccc2018-10-04 13:10:39 -040027 , fColorSpace(std::move(cs)) {}
28
29SkImage_GpuBase::~SkImage_GpuBase() {}
30
31//////////////////////////////////////////////////////////////////////////////////////////////////
32
33bool SkImage_GpuBase::ValidateBackendTexture(GrContext* ctx, const GrBackendTexture& tex,
34 GrPixelConfig* config, SkColorType ct, SkAlphaType at,
35 sk_sp<SkColorSpace> cs) {
36 if (!tex.isValid()) {
37 return false;
38 }
39 // TODO: Create a SkImageColorInfo struct for color, alpha, and color space so we don't need to
40 // create a fake image info here.
41 SkImageInfo info = SkImageInfo::Make(1, 1, ct, at, cs);
42 if (!SkImageInfoIsValid(info)) {
43 return false;
44 }
Brian Salomonf391d0f2018-12-14 09:18:50 -050045 GrBackendFormat backendFormat = tex.getBackendFormat();
46 if (!backendFormat.isValid()) {
47 return false;
48 }
49 *config = ctx->contextPriv().caps()->getConfigFromBackendFormat(backendFormat, ct);
50 return *config != kUnknown_GrPixelConfig;
Jim Van Verth8026ccc2018-10-04 13:10:39 -040051}
52
53//////////////////////////////////////////////////////////////////////////////////////////////////
54
Brian Osmane50cdf02018-10-19 13:02:14 -040055bool SkImage_GpuBase::getROPixels(SkBitmap* dst, CachingHint chint) const {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040056 if (!fContext->contextPriv().resourceProvider()) {
57 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
58 return false;
59 }
60
Jim Van Verth8026ccc2018-10-04 13:10:39 -040061 const auto desc = SkBitmapCacheDesc::Make(this);
62 if (SkBitmapCache::Find(desc, dst)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040063 SkASSERT(dst->isImmutable());
64 SkASSERT(dst->getPixels());
65 return true;
66 }
67
68 SkBitmapCache::RecPtr rec = nullptr;
69 SkPixmap pmap;
70 if (kAllow_CachingHint == chint) {
71 rec = SkBitmapCache::Alloc(desc, this->onImageInfo(), &pmap);
72 if (!rec) {
73 return false;
74 }
75 } else {
76 if (!dst->tryAllocPixels(this->onImageInfo()) || !dst->peekPixels(&pmap)) {
77 return false;
78 }
79 }
80
81 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeWrappedSurfaceContext(
82 this->asTextureProxyRef(),
83 fColorSpace);
84 if (!sContext) {
85 return false;
86 }
87
88 if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), 0, 0)) {
89 return false;
90 }
91
92 if (rec) {
93 SkBitmapCache::Add(std::move(rec), dst);
94 this->notifyAddedToRasterCache();
95 }
96 return true;
97}
98
99sk_sp<SkImage> SkImage_GpuBase::onMakeSubset(const SkIRect& subset) const {
100 sk_sp<GrSurfaceProxy> proxy = this->asTextureProxyRef();
101
102 GrSurfaceDesc desc;
103 desc.fWidth = subset.width();
104 desc.fHeight = subset.height();
105 desc.fConfig = proxy->config();
106
Greg Daniel4065d452018-11-16 15:43:41 -0500107 GrBackendFormat format = proxy->backendFormat().makeTexture2D();
108 if (!format.isValid()) {
109 return nullptr;
110 }
111
Brian Salomonf05e6d32018-12-20 08:41:41 -0500112 // TODO: Should this inherit our proxy's budgeted status?
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400113 sk_sp<GrSurfaceContext> sContext(fContext->contextPriv().makeDeferredSurfaceContext(
Brian Salomonf05e6d32018-12-20 08:41:41 -0500114 format, desc, proxy->origin(), GrMipMapped::kNo, SkBackingFit::kExact,
115 proxy->isBudgeted()));
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400116 if (!sContext) {
117 return nullptr;
118 }
119
120 if (!sContext->copy(proxy.get(), subset, SkIPoint::Make(0, 0))) {
121 return nullptr;
122 }
123
124 // MDB: this call is okay bc we know 'sContext' was kExact
Brian Salomonf05e6d32018-12-20 08:41:41 -0500125 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, fAlphaType,
126 sContext->asTextureProxyRef(), fColorSpace);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400127}
128
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400129static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
130 switch (info.colorType()) {
131 case kRGBA_8888_SkColorType:
132 case kBGRA_8888_SkColorType:
133 break;
134 default:
135 return; // nothing to do
136 }
137
138 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
139 // and in either case, the alpha-byte is always in the same place, so we can safely call
140 // SkPreMultiplyColor()
141 //
142 SkColor* row = (SkColor*)pixels;
143 for (int y = 0; y < info.height(); ++y) {
144 for (int x = 0; x < info.width(); ++x) {
145 row[x] = SkPreMultiplyColor(row[x]);
146 }
147 row = (SkColor*)((char*)(row)+rowBytes);
148 }
149}
150
151bool SkImage_GpuBase::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
152 int srcX, int srcY, CachingHint) const {
153 if (!fContext->contextPriv().resourceProvider()) {
154 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
155 return false;
156 }
157
158 if (!SkImageInfoValidConversion(dstInfo, this->onImageInfo())) {
159 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;
170 if (kUnpremul_SkAlphaType == rec.fInfo.alphaType() && kPremul_SkAlphaType == fAlphaType) {
171 // let the GPU perform this transformation for us
172 flags = GrContextPriv::kUnpremul_PixelOpsFlag;
173 }
174
175 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeWrappedSurfaceContext(
176 this->asTextureProxyRef(), fColorSpace);
177 if (!sContext) {
178 return false;
179 }
180
181 if (!sContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY, flags)) {
182 return false;
183 }
184
185 // do we have to manually fix-up the alpha channel?
186 // src dst
187 // unpremul premul fix manually
188 // premul unpremul done by kUnpremul_PixelOpsFlag
189 // all other combos need to change.
190 //
191 // Should this be handled by Ganesh? todo:?
192 //
193 if (kPremul_SkAlphaType == rec.fInfo.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
194 apply_premul(rec.fInfo, rec.fPixels, rec.fRowBytes);
195 }
196 return true;
197}
198
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400199sk_sp<GrTextureProxy> SkImage_GpuBase::asTextureProxyRef(GrContext* context,
200 const GrSamplerState& params,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400201 SkScalar scaleAdjust[2]) const {
202 if (context->uniqueID() != fContext->uniqueID()) {
203 SkASSERT(0);
204 return nullptr;
205 }
206
207 GrTextureAdjuster adjuster(fContext.get(), this->asTextureProxyRef(), fAlphaType,
208 this->uniqueID(), fColorSpace.get());
Brian Osman6064e1c2018-10-19 14:27:54 -0400209 return adjuster.refTextureProxyForParams(params, scaleAdjust);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400210}
211
212GrBackendTexture SkImage_GpuBase::onGetBackendTexture(bool flushPendingGrContextIO,
213 GrSurfaceOrigin* origin) const {
214 sk_sp<GrTextureProxy> proxy = this->asTextureProxyRef();
215 SkASSERT(proxy);
216
217 if (!fContext->contextPriv().resourceProvider() && !proxy->isInstantiated()) {
218 // This image was created with a DDL context and cannot be instantiated.
219 return GrBackendTexture();
220}
221
222 if (!proxy->instantiate(fContext->contextPriv().resourceProvider())) {
223 return GrBackendTexture(); // invalid
224 }
225
226 GrTexture* texture = proxy->peekTexture();
227
228 if (texture) {
229 if (flushPendingGrContextIO) {
230 fContext->contextPriv().prepareSurfaceForExternalIO(proxy.get());
231 }
232 if (origin) {
233 *origin = proxy->origin();
234 }
235 return texture->getBackendTexture();
236 }
237 return GrBackendTexture(); // invalid
238}
239
240GrTexture* SkImage_GpuBase::onGetTexture() const {
241 GrTextureProxy* proxy = this->peekProxy();
242 if (!proxy) {
243 return nullptr;
244 }
245
246 sk_sp<GrTextureProxy> proxyRef = this->asTextureProxyRef();
247 if (!fContext->contextPriv().resourceProvider() && !proxyRef->isInstantiated()) {
248 // This image was created with a DDL context and cannot be instantiated.
249 return nullptr;
250 }
251
252 if (!proxy->instantiate(fContext->contextPriv().resourceProvider())) {
253 return nullptr;
254 }
255
256 return proxy->peekTexture();
257}
258
259sk_sp<SkImage> SkImage_GpuBase::onMakeColorSpace(sk_sp<SkColorSpace> target) const {
Brian Osmanbe686f02018-10-12 11:18:02 -0400260 auto xform = GrColorSpaceXformEffect::Make(fColorSpace.get(), fAlphaType,
261 target.get(), fAlphaType);
Brian Osmanb4ae4992018-10-18 11:16:14 -0400262 SkASSERT(xform);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400263
Brian Osman9c111352018-10-16 10:18:26 -0400264 sk_sp<GrTextureProxy> proxy = this->asTextureProxyRef();
265
Greg Daniel4065d452018-11-16 15:43:41 -0500266 GrBackendFormat format = proxy->backendFormat().makeTexture2D();
267 if (!format.isValid()) {
268 return nullptr;
269 }
270
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400271 sk_sp<GrRenderTargetContext> renderTargetContext(
Brian Osman9c111352018-10-16 10:18:26 -0400272 fContext->contextPriv().makeDeferredRenderTargetContextWithFallback(
Greg Daniel4065d452018-11-16 15:43:41 -0500273 format, SkBackingFit::kExact, this->width(), this->height(),
274 proxy->config(), nullptr));
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400275 if (!renderTargetContext) {
276 return nullptr;
277 }
278
279 GrPaint paint;
280 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Osman9c111352018-10-16 10:18:26 -0400281 paint.addColorTextureProcessor(std::move(proxy), SkMatrix::I());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400282 paint.addColorFragmentProcessor(std::move(xform));
283
Brian Osmanb4ae4992018-10-18 11:16:14 -0400284 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
285 SkRect::MakeIWH(this->width(), this->height()));
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400286 if (!renderTargetContext->asTextureProxy()) {
287 return nullptr;
288 }
289
290 // MDB: this call is okay bc we know 'renderTargetContext' was exact
Brian Salomonf05e6d32018-12-20 08:41:41 -0500291 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, fAlphaType,
292 renderTargetContext->asTextureProxyRef(), std::move(target));
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400293}
294
295bool SkImage_GpuBase::onIsValid(GrContext* context) const {
296 // The base class has already checked that context isn't abandoned (if it's not nullptr)
297 if (fContext->abandoned()) {
298 return false;
299 }
300
301 if (context && context != fContext.get()) {
302 return false;
303 }
304
305 return true;
306}
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400307
Jim Van Verth0e671942018-11-09 12:03:57 -0500308bool SkImage_GpuBase::MakeTempTextureProxies(GrContext* ctx, const GrBackendTexture yuvaTextures[],
Jim Van Verth53275362018-11-09 15:42:35 -0500309 int numTextures, const SkYUVAIndex yuvaIndices[4],
310 GrSurfaceOrigin imageOrigin,
Jim Van Verth0e671942018-11-09 12:03:57 -0500311 sk_sp<GrTextureProxy> tempTextureProxies[4]) {
312 GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider();
313
314 // We need to make a copy of the input backend textures because we need to preserve the result
315 // of validate_backend_texture.
316 GrBackendTexture yuvaTexturesCopy[4];
317 for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) {
318 yuvaTexturesCopy[textureIndex] = yuvaTextures[textureIndex];
Brian Salomonf391d0f2018-12-14 09:18:50 -0500319 GrBackendFormat backendFormat = yuvaTexturesCopy[textureIndex].getBackendFormat();
320 if (!backendFormat.isValid()) {
321 return false;
322 }
323 yuvaTexturesCopy[textureIndex].fConfig =
324 ctx->contextPriv().caps()->getYUVAConfigFromBackendFormat(backendFormat);
325 if (yuvaTexturesCopy[textureIndex].fConfig == kUnknown_GrPixelConfig) {
Jim Van Verth0e671942018-11-09 12:03:57 -0500326 return false;
327 }
328 SkASSERT(yuvaTexturesCopy[textureIndex].isValid());
329
330 tempTextureProxies[textureIndex] =
Brian Salomonc67c31c2018-12-06 10:00:03 -0500331 proxyProvider->wrapBackendTexture(yuvaTexturesCopy[textureIndex], imageOrigin,
332 kBorrow_GrWrapOwnership, kRead_GrIOType);
Jim Van Verth0e671942018-11-09 12:03:57 -0500333 if (!tempTextureProxies[textureIndex]) {
334 return false;
335 }
Jim Van Verth53275362018-11-09 15:42:35 -0500336
337 // Check that each texture contains the channel data for the corresponding YUVA index
338 GrPixelConfig config = yuvaTexturesCopy[textureIndex].fConfig;
339 for (int yuvaIndex = 0; yuvaIndex < SkYUVAIndex::kIndexCount; ++yuvaIndex) {
340 if (yuvaIndices[yuvaIndex].fIndex == textureIndex) {
341 switch (yuvaIndices[yuvaIndex].fChannel) {
342 case SkColorChannel::kR:
343 if (kAlpha_8_as_Alpha_GrPixelConfig == config) {
344 return false;
345 }
346 break;
347 case SkColorChannel::kG:
348 case SkColorChannel::kB:
349 if (kAlpha_8_as_Alpha_GrPixelConfig == config ||
350 kAlpha_8_as_Red_GrPixelConfig == config) {
351 return false;
352 }
353 break;
354 case SkColorChannel::kA:
355 default:
356 if (kRGB_888_GrPixelConfig == config) {
357 return false;
358 }
359 break;
360 }
361 }
362 }
Jim Van Verth0e671942018-11-09 12:03:57 -0500363 }
364
365 return true;
366}
367
368bool SkImage_GpuBase::RenderYUVAToRGBA(GrContext* ctx, GrRenderTargetContext* renderTargetContext,
369 const SkRect& rect, SkYUVColorSpace yuvColorSpace,
370 const sk_sp<GrTextureProxy> proxies[4],
371 const SkYUVAIndex yuvaIndices[4]) {
372 SkASSERT(renderTargetContext);
373 if (!renderTargetContext->asSurfaceProxy()) {
374 return false;
375 }
376
377 GrPaint paint;
378 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
379
Jim Van Verth0e671942018-11-09 12:03:57 -0500380 paint.addColorFragmentProcessor(GrYUVtoRGBEffect::Make(proxies, yuvaIndices,
381 yuvColorSpace,
382 GrSamplerState::Filter::kNearest));
383
384 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
385
386 // DDL TODO: in the promise image version we must not flush here
387 ctx->contextPriv().flushSurfaceWrites(renderTargetContext->asSurfaceProxy());
388
389 return true;
390}
391
Brian Salomonbe5a0932018-12-10 10:03:26 -0500392sk_sp<GrTextureProxy> SkImage_GpuBase::MakePromiseImageLazyProxy(
393 GrContext* context, int width, int height, GrSurfaceOrigin origin, GrPixelConfig config,
394 GrBackendFormat backendFormat, GrMipMapped mipMapped,
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500395 PromiseImageTextureFulfillProc fulfillProc,
396 PromiseImageTextureReleaseProc releaseProc,
397 PromiseImageTextureDoneProc doneProc,
398 PromiseImageTextureContext textureContext) {
Brian Salomonbe5a0932018-12-10 10:03:26 -0500399 SkASSERT(context);
400 SkASSERT(width > 0 && height > 0);
401 SkASSERT(doneProc);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500402 SkASSERT(config != kUnknown_GrPixelConfig);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500403
404 if (!fulfillProc || !releaseProc) {
405 doneProc(textureContext);
406 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400407 }
408
Brian Salomonbe5a0932018-12-10 10:03:26 -0500409 if (mipMapped == GrMipMapped::kYes &&
410 GrTextureTypeHasRestrictedSampling(backendFormat.textureType())) {
411 // It is invalid to have a GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE and have mips as
412 // well.
413 doneProc(textureContext);
414 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400415 }
Brian Salomonbe5a0932018-12-10 10:03:26 -0500416
417 /**
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500418 * This class is the lazy instantiation callback for promise images. It manages calling the
419 * client's Fulfill, Release, and Done procs. It attempts to reuse a GrTexture instance in
420 * cases where the client provides the same SkPromiseImageTexture for successive Fulfill calls.
421 * The created GrTexture is given a key based on a unique ID associated with the
422 * SkPromiseImageTexture. When the texture enters "idle" state (meaning it is not being used by
423 * the GPU and is at rest in the resource cache) the client's Release proc is called
424 * using GrTexture's idle proc mechanism. If the same SkPromiseImageTexture is provided for
425 * another fulfill we find the cached GrTexture. If the proxy, and therefore this object,
426 * is destroyed, we invalidate the GrTexture's key. Also if the client overwrites or
427 * destroys their SkPromiseImageTexture we invalidate the key.
428 *
429 * Currently a GrTexture is only reused for a given SkPromiseImageTexture if the
430 * SkPromiseImageTexture is reused in Fulfill for the same promise SkImage. However, we'd
431 * like to relax that so that a SkPromiseImageTexture can be reused with different promise
432 * SkImages that will reuse a single GrTexture.
Brian Salomonbe5a0932018-12-10 10:03:26 -0500433 */
434 class PromiseLazyInstantiateCallback {
435 public:
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500436 PromiseLazyInstantiateCallback(PromiseImageTextureFulfillProc fulfillProc,
437 PromiseImageTextureReleaseProc releaseProc,
438 PromiseImageTextureDoneProc doneProc,
439 PromiseImageTextureContext context,
Brian Salomonbe5a0932018-12-10 10:03:26 -0500440 GrPixelConfig config)
441 : fFulfillProc(fulfillProc)
442 , fReleaseProc(releaseProc)
443 , fContext(context)
444 , fConfig(config) {
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500445 fDoneHelper = sk_make_sp<GrReleaseProcHelper>(doneProc, context);
446 static std::atomic<uint32_t> gUniqueID;
447 fUniqueID = gUniqueID.fetch_add(1) + 1;
448 }
449 ~PromiseLazyInstantiateCallback() {
450 // If we've already released the texture then it is safe to call done now. Here we may
451 // be on any thread.
452 if (fIdleContext && fIdleContext->fWasReleased.load()) {
453 // We still own a ref on fDoneHelper so no other thread can be calling the done
454 // proc.
455 fDoneHelper->callAndClear();
456 }
457 // Remove the key from the texture so that the texture will be removed from the cache.
458 // If we didn't just call the done proc above then it will get called when the texture
459 // is removed from the cache after this message is processed.
460 if (fLastFulfilledKey.isValid()) {
461 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(
462 GrUniqueKeyInvalidatedMessage(fLastFulfilledKey, fContextID));
463 }
Brian Salomonbe5a0932018-12-10 10:03:26 -0500464 }
465
466 sk_sp<GrSurface> operator()(GrResourceProvider* resourceProvider) {
467 if (!resourceProvider) {
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500468 return nullptr;
469 }
470
471 sk_sp<GrTexture> cachedTexture;
472 SkASSERT(fLastFulfilledKey.isValid() == (fLastFulfillID > 0));
473 if (fLastFulfilledKey.isValid()) {
474 auto surf = resourceProvider->findByUniqueKey<GrSurface>(fLastFulfilledKey);
475 if (surf) {
476 cachedTexture = sk_ref_sp(surf->asTexture());
477 SkASSERT(cachedTexture);
478 }
479 }
480 // If the release callback hasn't been called already by releasing the GrTexture
481 // then we can be sure that won't happen so long as we have a ref to the texture.
482 // Moreoever, only this thread should be able to change the atomic to true, hence the
483 // relaxed memory order.
484 if (cachedTexture && !fIdleContext->fWasReleased.load(std::memory_order_relaxed)) {
485 return std::move(cachedTexture);
486 }
487 GrBackendTexture backendTexture;
488 SkPromiseImageTexture* promiseTexture = fFulfillProc(fContext);
489 if (!promiseTexture) {
490 fReleaseProc(fContext, nullptr);
491 return sk_sp<GrTexture>();
492 }
493 bool same = promiseTexture->uniqueID() == fLastFulfillID;
494 SkASSERT(!same || fLastFulfilledKey.isValid());
495 if (same && cachedTexture) {
496 SkASSERT(fIdleContext->unique());
497 // Reset the purgeable context so that we balance the new fulfill with a release.
498 fIdleContext->ref();
499 SkASSERT(fIdleContext->fReleaseProc == fReleaseProc);
500 SkASSERT(fIdleContext->fTextureContext == fContext);
501 // Memory order relaxed because only this thread can change fWasReleased to true.
502 fIdleContext->fWasReleased.store(false, std::memory_order_relaxed);
503 cachedTexture->setIdleProc(IdleProc, fIdleContext.get());
504 return std::move(cachedTexture);
505 } else if (cachedTexture) {
506 cachedTexture->resourcePriv().removeUniqueKey();
507 // We don't want calling the client's done proc to be tied to the old texture.
508 cachedTexture->setRelease(nullptr);
509 }
510 fLastFulfillID = promiseTexture->uniqueID();
511
512 backendTexture = promiseTexture->backendTexture();
513 backendTexture.fConfig = fConfig;
514 if (!backendTexture.isValid()) {
515 // Even though the GrBackendTexture is not valid, we must call the release
516 // proc to keep our contract of always calling Fulfill and Release in pairs.
517 fReleaseProc(fContext, promiseTexture);
Brian Salomon426ba462019-01-10 16:33:06 +0000518 return sk_sp<GrTexture>();
Brian Salomon559c6172019-01-10 10:23:44 -0500519 }
520
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500521 auto tex = resourceProvider->wrapBackendTexture(backendTexture, kBorrow_GrWrapOwnership,
522 kRead_GrIOType);
523 if (!tex) {
524 // Even though we failed to wrap the backend texture, we must call the release
525 // proc to keep our contract of always calling Fulfill and Release in pairs.
526 fReleaseProc(fContext, promiseTexture);
527 return sk_sp<GrTexture>();
Brian Salomon426ba462019-01-10 16:33:06 +0000528 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500529 fIdleContext = sk_make_sp<IdleContext>(fReleaseProc, fContext, promiseTexture);
530 // The texture gets a ref, which is balanced when the idle callback is called.
531 fIdleContext->ref();
532 tex->setIdleProc(IdleProc, fIdleContext.get());
533 tex->setRelease(fDoneHelper);
534 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
535 GrUniqueKey::Builder builder(&fLastFulfilledKey, kDomain, 2, "promise");
536 builder[0] = promiseTexture->uniqueID();
537 builder[1] = fUniqueID;
538 builder.finish();
539 tex->resourcePriv().setUniqueKey(fLastFulfilledKey);
540 SkASSERT(fContextID == SK_InvalidUniqueID ||
541 fContextID == tex->getContext()->uniqueID());
542 fContextID = tex->getContext()->uniqueID();
543 promiseTexture->addKeyToInvalidate(fContextID, fLastFulfilledKey);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500544 return std::move(tex);
545 }
546
547 private:
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500548 struct IdleContext : public SkNVRefCnt<IdleContext> {
549 IdleContext(PromiseImageTextureReleaseProc proc, PromiseImageTextureContext context,
550 const SkPromiseImageTexture* texture)
551 : fReleaseProc(proc), fTextureContext(context), fPromiseImageTexture(texture) {}
552 PromiseImageTextureReleaseProc fReleaseProc;
553 PromiseImageTextureContext fTextureContext;
554 const SkPromiseImageTexture* fPromiseImageTexture;
555 std::atomic<bool> fWasReleased{false};
556 };
557 static void IdleProc(void* context) {
558 IdleContext* rc = static_cast<IdleContext*>(context);
559 SkASSERT(!rc->fWasReleased.load());
560 rc->fReleaseProc(rc->fTextureContext, rc->fPromiseImageTexture);
561 rc->fWasReleased.store(true);
562 // Drop the texture's implicit ref on the IdleContext.
563 rc->unref();
Brian Salomonbe5a0932018-12-10 10:03:26 -0500564 }
565
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500566 PromiseImageTextureFulfillProc fFulfillProc;
567 PromiseImageTextureReleaseProc fReleaseProc;
568 PromiseImageTextureContext fContext;
Brian Salomonbe5a0932018-12-10 10:03:26 -0500569
570 GrPixelConfig fConfig;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500571 sk_sp<IdleContext> fIdleContext;
Brian Salomonbe5a0932018-12-10 10:03:26 -0500572 sk_sp<GrReleaseProcHelper> fDoneHelper;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500573
574 // ID of the last SkPromiseImageTexture given to us by the client.
575 uint32_t fLastFulfillID = 0;
576 // ID of the GrContext that we are interacting with.
577 uint32_t fContextID = SK_InvalidUniqueID;
578 // Unique ID of this lazy instantiation callback.
579 uint32_t fUniqueID;
580 GrUniqueKey fLastFulfilledKey;
Brian Salomonbe5a0932018-12-10 10:03:26 -0500581 } callback(fulfillProc, releaseProc, doneProc, textureContext, config);
582
583 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
584
585 GrSurfaceDesc desc;
586 desc.fWidth = width;
587 desc.fHeight = height;
588 desc.fConfig = config;
589
590 // We pass kReadOnly here since we should treat content of the client's texture as immutable.
591 return proxyProvider->createLazyProxy(std::move(callback), backendFormat, desc, origin,
592 mipMapped, GrInternalSurfaceFlags::kReadOnly,
593 SkBackingFit::kExact, SkBudgeted::kNo,
594 GrSurfaceProxy::LazyInstantiationType::kDeinstantiate);
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400595}