blob: 35d5a2e5eb95e0c3f6fa6ab3ef9f1e538b5061d4 [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 Phillips9da87e02019-02-04 13:26:26 -050036 SkASSERT(fContext->priv().contextID() == newContext->priv().contextID());
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
Robert Phillipsfd0d9702019-02-01 10:19:42 -050063uint32_t SkImage_GpuBase::contextID() const {
Robert Phillips9da87e02019-02-04 13:26:26 -050064 return fContext->priv().contextID();
Robert Phillipsfd0d9702019-02-01 10:19:42 -050065}
66
Brian Osmane50cdf02018-10-19 13:02:14 -040067bool SkImage_GpuBase::getROPixels(SkBitmap* dst, CachingHint chint) const {
Robert Phillips9da87e02019-02-04 13:26:26 -050068 if (!fContext->priv().resourceProvider()) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040069 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
70 return false;
71 }
72
Jim Van Verth8026ccc2018-10-04 13:10:39 -040073 const auto desc = SkBitmapCacheDesc::Make(this);
74 if (SkBitmapCache::Find(desc, dst)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040075 SkASSERT(dst->isImmutable());
76 SkASSERT(dst->getPixels());
77 return true;
78 }
79
80 SkBitmapCache::RecPtr rec = nullptr;
81 SkPixmap pmap;
82 if (kAllow_CachingHint == chint) {
83 rec = SkBitmapCache::Alloc(desc, this->onImageInfo(), &pmap);
84 if (!rec) {
85 return false;
86 }
87 } else {
88 if (!dst->tryAllocPixels(this->onImageInfo()) || !dst->peekPixels(&pmap)) {
89 return false;
90 }
91 }
92
Robert Phillips9da87e02019-02-04 13:26:26 -050093 sk_sp<GrSurfaceContext> sContext = fContext->priv().makeWrappedSurfaceContext(
Jim Van Verth8026ccc2018-10-04 13:10:39 -040094 this->asTextureProxyRef(),
95 fColorSpace);
96 if (!sContext) {
97 return false;
98 }
99
100 if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), 0, 0)) {
101 return false;
102 }
103
104 if (rec) {
105 SkBitmapCache::Add(std::move(rec), dst);
106 this->notifyAddedToRasterCache();
107 }
108 return true;
109}
110
111sk_sp<SkImage> SkImage_GpuBase::onMakeSubset(const SkIRect& subset) const {
112 sk_sp<GrSurfaceProxy> proxy = this->asTextureProxyRef();
113
114 GrSurfaceDesc desc;
115 desc.fWidth = subset.width();
116 desc.fHeight = subset.height();
117 desc.fConfig = proxy->config();
118
Greg Daniel4065d452018-11-16 15:43:41 -0500119 GrBackendFormat format = proxy->backendFormat().makeTexture2D();
120 if (!format.isValid()) {
121 return nullptr;
122 }
123
Brian Salomonf05e6d32018-12-20 08:41:41 -0500124 // TODO: Should this inherit our proxy's budgeted status?
Robert Phillips9da87e02019-02-04 13:26:26 -0500125 sk_sp<GrSurfaceContext> sContext(fContext->priv().makeDeferredSurfaceContext(
Brian Salomonf05e6d32018-12-20 08:41:41 -0500126 format, desc, proxy->origin(), GrMipMapped::kNo, SkBackingFit::kExact,
127 proxy->isBudgeted()));
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400128 if (!sContext) {
129 return nullptr;
130 }
131
132 if (!sContext->copy(proxy.get(), subset, SkIPoint::Make(0, 0))) {
133 return nullptr;
134 }
135
136 // MDB: this call is okay bc we know 'sContext' was kExact
Brian Salomonf05e6d32018-12-20 08:41:41 -0500137 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, fAlphaType,
Brian Osmane9560492019-02-05 17:00:03 -0500138 sContext->asTextureProxyRef(), this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400139}
140
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400141static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
142 switch (info.colorType()) {
143 case kRGBA_8888_SkColorType:
144 case kBGRA_8888_SkColorType:
145 break;
146 default:
147 return; // nothing to do
148 }
149
Brian Salomon3f4cd772019-01-11 16:03:19 -0500150 // SkColor is not necessarily RGBA or BGRA, but it is one of them on little-endian,
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400151 // and in either case, the alpha-byte is always in the same place, so we can safely call
152 // SkPreMultiplyColor()
153 //
154 SkColor* row = (SkColor*)pixels;
155 for (int y = 0; y < info.height(); ++y) {
156 for (int x = 0; x < info.width(); ++x) {
157 row[x] = SkPreMultiplyColor(row[x]);
158 }
159 row = (SkColor*)((char*)(row)+rowBytes);
160 }
161}
162
163bool SkImage_GpuBase::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
164 int srcX, int srcY, CachingHint) const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500165 if (!fContext->priv().resourceProvider()) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400166 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
167 return false;
168 }
169
170 if (!SkImageInfoValidConversion(dstInfo, this->onImageInfo())) {
171 return false;
172 }
173
174 SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, srcX, srcY);
175 if (!rec.trim(this->width(), this->height())) {
176 return false;
177 }
178
179 // TODO: this seems to duplicate code in GrTextureContext::onReadPixels and
180 // GrRenderTargetContext::onReadPixels
181 uint32_t flags = 0;
182 if (kUnpremul_SkAlphaType == rec.fInfo.alphaType() && kPremul_SkAlphaType == fAlphaType) {
183 // let the GPU perform this transformation for us
184 flags = GrContextPriv::kUnpremul_PixelOpsFlag;
185 }
186
Robert Phillips9da87e02019-02-04 13:26:26 -0500187 sk_sp<GrSurfaceContext> sContext = fContext->priv().makeWrappedSurfaceContext(
Brian Osmane9560492019-02-05 17:00:03 -0500188 this->asTextureProxyRef(), this->refColorSpace());
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400189 if (!sContext) {
190 return false;
191 }
192
193 if (!sContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY, flags)) {
194 return false;
195 }
196
197 // do we have to manually fix-up the alpha channel?
198 // src dst
199 // unpremul premul fix manually
200 // premul unpremul done by kUnpremul_PixelOpsFlag
201 // all other combos need to change.
202 //
203 // Should this be handled by Ganesh? todo:?
204 //
205 if (kPremul_SkAlphaType == rec.fInfo.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
206 apply_premul(rec.fInfo, rec.fPixels, rec.fRowBytes);
207 }
208 return true;
209}
210
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400211sk_sp<GrTextureProxy> SkImage_GpuBase::asTextureProxyRef(GrContext* context,
212 const GrSamplerState& params,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400213 SkScalar scaleAdjust[2]) const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500214 if (context->priv().contextID() != fContext->priv().contextID()) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400215 SkASSERT(0);
216 return nullptr;
217 }
218
219 GrTextureAdjuster adjuster(fContext.get(), this->asTextureProxyRef(), fAlphaType,
220 this->uniqueID(), fColorSpace.get());
Brian Osman6064e1c2018-10-19 14:27:54 -0400221 return adjuster.refTextureProxyForParams(params, scaleAdjust);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400222}
223
224GrBackendTexture SkImage_GpuBase::onGetBackendTexture(bool flushPendingGrContextIO,
225 GrSurfaceOrigin* origin) const {
226 sk_sp<GrTextureProxy> proxy = this->asTextureProxyRef();
227 SkASSERT(proxy);
228
Robert Phillips9da87e02019-02-04 13:26:26 -0500229 if (!fContext->priv().resourceProvider() && !proxy->isInstantiated()) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400230 // This image was created with a DDL context and cannot be instantiated.
231 return GrBackendTexture();
232}
233
Robert Phillips9da87e02019-02-04 13:26:26 -0500234 if (!proxy->instantiate(fContext->priv().resourceProvider())) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400235 return GrBackendTexture(); // invalid
236 }
237
238 GrTexture* texture = proxy->peekTexture();
239
240 if (texture) {
241 if (flushPendingGrContextIO) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500242 fContext->priv().prepareSurfaceForExternalIO(proxy.get());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400243 }
244 if (origin) {
245 *origin = proxy->origin();
246 }
247 return texture->getBackendTexture();
248 }
249 return GrBackendTexture(); // invalid
250}
251
252GrTexture* SkImage_GpuBase::onGetTexture() const {
253 GrTextureProxy* proxy = this->peekProxy();
254 if (!proxy) {
255 return nullptr;
256 }
257
258 sk_sp<GrTextureProxy> proxyRef = this->asTextureProxyRef();
Robert Phillips9da87e02019-02-04 13:26:26 -0500259 if (!fContext->priv().resourceProvider() && !proxyRef->isInstantiated()) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400260 // This image was created with a DDL context and cannot be instantiated.
261 return nullptr;
262 }
263
Robert Phillips9da87e02019-02-04 13:26:26 -0500264 if (!proxy->instantiate(fContext->priv().resourceProvider())) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400265 return nullptr;
266 }
267
268 return proxy->peekTexture();
269}
270
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400271bool SkImage_GpuBase::onIsValid(GrContext* context) const {
272 // The base class has already checked that context isn't abandoned (if it's not nullptr)
273 if (fContext->abandoned()) {
274 return false;
275 }
276
277 if (context && context != fContext.get()) {
278 return false;
279 }
280
281 return true;
282}
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400283
Jim Van Verth0e671942018-11-09 12:03:57 -0500284bool SkImage_GpuBase::MakeTempTextureProxies(GrContext* ctx, const GrBackendTexture yuvaTextures[],
Jim Van Verth53275362018-11-09 15:42:35 -0500285 int numTextures, const SkYUVAIndex yuvaIndices[4],
286 GrSurfaceOrigin imageOrigin,
Jim Van Verth0e671942018-11-09 12:03:57 -0500287 sk_sp<GrTextureProxy> tempTextureProxies[4]) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500288 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Jim Van Verth0e671942018-11-09 12:03:57 -0500289
290 // We need to make a copy of the input backend textures because we need to preserve the result
291 // of validate_backend_texture.
292 GrBackendTexture yuvaTexturesCopy[4];
293 for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) {
294 yuvaTexturesCopy[textureIndex] = yuvaTextures[textureIndex];
Brian Salomonf391d0f2018-12-14 09:18:50 -0500295 GrBackendFormat backendFormat = yuvaTexturesCopy[textureIndex].getBackendFormat();
296 if (!backendFormat.isValid()) {
297 return false;
298 }
299 yuvaTexturesCopy[textureIndex].fConfig =
Robert Phillips9da87e02019-02-04 13:26:26 -0500300 ctx->priv().caps()->getYUVAConfigFromBackendFormat(backendFormat);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500301 if (yuvaTexturesCopy[textureIndex].fConfig == kUnknown_GrPixelConfig) {
Jim Van Verth0e671942018-11-09 12:03:57 -0500302 return false;
303 }
304 SkASSERT(yuvaTexturesCopy[textureIndex].isValid());
305
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500306 tempTextureProxies[textureIndex] = proxyProvider->wrapBackendTexture(
307 yuvaTexturesCopy[textureIndex], imageOrigin, kBorrow_GrWrapOwnership,
308 GrWrapCacheable::kNo, kRead_GrIOType);
Jim Van Verth0e671942018-11-09 12:03:57 -0500309 if (!tempTextureProxies[textureIndex]) {
310 return false;
311 }
Jim Van Verth53275362018-11-09 15:42:35 -0500312
313 // Check that each texture contains the channel data for the corresponding YUVA index
314 GrPixelConfig config = yuvaTexturesCopy[textureIndex].fConfig;
315 for (int yuvaIndex = 0; yuvaIndex < SkYUVAIndex::kIndexCount; ++yuvaIndex) {
316 if (yuvaIndices[yuvaIndex].fIndex == textureIndex) {
317 switch (yuvaIndices[yuvaIndex].fChannel) {
318 case SkColorChannel::kR:
319 if (kAlpha_8_as_Alpha_GrPixelConfig == config) {
320 return false;
321 }
322 break;
323 case SkColorChannel::kG:
324 case SkColorChannel::kB:
325 if (kAlpha_8_as_Alpha_GrPixelConfig == config ||
326 kAlpha_8_as_Red_GrPixelConfig == config) {
327 return false;
328 }
329 break;
330 case SkColorChannel::kA:
331 default:
332 if (kRGB_888_GrPixelConfig == config) {
333 return false;
334 }
335 break;
336 }
337 }
338 }
Jim Van Verth0e671942018-11-09 12:03:57 -0500339 }
340
341 return true;
342}
343
344bool SkImage_GpuBase::RenderYUVAToRGBA(GrContext* ctx, GrRenderTargetContext* renderTargetContext,
345 const SkRect& rect, SkYUVColorSpace yuvColorSpace,
Brian Osmane9560492019-02-05 17:00:03 -0500346 sk_sp<GrColorSpaceXform> colorSpaceXform,
Jim Van Verth0e671942018-11-09 12:03:57 -0500347 const sk_sp<GrTextureProxy> proxies[4],
348 const SkYUVAIndex yuvaIndices[4]) {
349 SkASSERT(renderTargetContext);
350 if (!renderTargetContext->asSurfaceProxy()) {
351 return false;
352 }
353
354 GrPaint paint;
355 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
356
Brian Osmane9560492019-02-05 17:00:03 -0500357 auto fp = GrYUVtoRGBEffect::Make(proxies, yuvaIndices, yuvColorSpace,
358 GrSamplerState::Filter::kNearest);
359 if (colorSpaceXform) {
360 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(colorSpaceXform));
361 }
362 paint.addColorFragmentProcessor(std::move(fp));
Jim Van Verth0e671942018-11-09 12:03:57 -0500363
364 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
365
366 // DDL TODO: in the promise image version we must not flush here
Robert Phillips9da87e02019-02-04 13:26:26 -0500367 ctx->priv().flushSurfaceWrites(renderTargetContext->asSurfaceProxy());
Jim Van Verth0e671942018-11-09 12:03:57 -0500368
369 return true;
370}
371
Brian Salomonbe5a0932018-12-10 10:03:26 -0500372sk_sp<GrTextureProxy> SkImage_GpuBase::MakePromiseImageLazyProxy(
373 GrContext* context, int width, int height, GrSurfaceOrigin origin, GrPixelConfig config,
374 GrBackendFormat backendFormat, GrMipMapped mipMapped,
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500375 PromiseImageTextureFulfillProc fulfillProc,
376 PromiseImageTextureReleaseProc releaseProc,
377 PromiseImageTextureDoneProc doneProc,
Brian Salomonf55e8d52019-01-30 17:28:20 -0500378 PromiseImageTextureContext textureContext,
379 DelayReleaseCallback delayReleaseCallback) {
Brian Salomonbe5a0932018-12-10 10:03:26 -0500380 SkASSERT(context);
381 SkASSERT(width > 0 && height > 0);
382 SkASSERT(doneProc);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500383 SkASSERT(config != kUnknown_GrPixelConfig);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500384
385 if (!fulfillProc || !releaseProc) {
386 doneProc(textureContext);
387 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400388 }
389
Brian Salomonbe5a0932018-12-10 10:03:26 -0500390 if (mipMapped == GrMipMapped::kYes &&
391 GrTextureTypeHasRestrictedSampling(backendFormat.textureType())) {
392 // It is invalid to have a GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE and have mips as
393 // well.
394 doneProc(textureContext);
395 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400396 }
Brian Salomonbe5a0932018-12-10 10:03:26 -0500397
398 /**
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500399 * This class is the lazy instantiation callback for promise images. It manages calling the
400 * client's Fulfill, Release, and Done procs. It attempts to reuse a GrTexture instance in
401 * cases where the client provides the same SkPromiseImageTexture for successive Fulfill calls.
402 * The created GrTexture is given a key based on a unique ID associated with the
403 * SkPromiseImageTexture. When the texture enters "idle" state (meaning it is not being used by
404 * the GPU and is at rest in the resource cache) the client's Release proc is called
405 * using GrTexture's idle proc mechanism. If the same SkPromiseImageTexture is provided for
406 * another fulfill we find the cached GrTexture. If the proxy, and therefore this object,
407 * is destroyed, we invalidate the GrTexture's key. Also if the client overwrites or
408 * destroys their SkPromiseImageTexture we invalidate the key.
409 *
410 * Currently a GrTexture is only reused for a given SkPromiseImageTexture if the
411 * SkPromiseImageTexture is reused in Fulfill for the same promise SkImage. However, we'd
412 * like to relax that so that a SkPromiseImageTexture can be reused with different promise
413 * SkImages that will reuse a single GrTexture.
Brian Salomonbe5a0932018-12-10 10:03:26 -0500414 */
415 class PromiseLazyInstantiateCallback {
416 public:
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500417 PromiseLazyInstantiateCallback(PromiseImageTextureFulfillProc fulfillProc,
418 PromiseImageTextureReleaseProc releaseProc,
419 PromiseImageTextureDoneProc doneProc,
420 PromiseImageTextureContext context,
Brian Salomonf55e8d52019-01-30 17:28:20 -0500421 DelayReleaseCallback delayReleaseCallback,
Brian Salomonbe5a0932018-12-10 10:03:26 -0500422 GrPixelConfig config)
423 : fFulfillProc(fulfillProc)
Brian Salomonf55e8d52019-01-30 17:28:20 -0500424 , fConfig(config)
425 , fDelayReleaseCallback(delayReleaseCallback) {
Brian Salomon553610d2019-01-14 17:34:12 -0500426 auto doneHelper = sk_make_sp<GrReleaseProcHelper>(doneProc, context);
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500427 fReleaseContext = sk_make_sp<IdleContext::PromiseImageReleaseContext>(
428 releaseProc, context, std::move(doneHelper));
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500429 }
Brian Salomon553610d2019-01-14 17:34:12 -0500430
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500431 ~PromiseLazyInstantiateCallback() = default;
Brian Salomonbe5a0932018-12-10 10:03:26 -0500432
433 sk_sp<GrSurface> operator()(GrResourceProvider* resourceProvider) {
434 if (!resourceProvider) {
Brian Salomonf55e8d52019-01-30 17:28:20 -0500435 if (fDelayedReleaseTexture) {
436 fDelayedReleaseTexture.reset();
437 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500438 return nullptr;
439 }
Brian Salomonf55e8d52019-01-30 17:28:20 -0500440 if (fDelayedReleaseTexture) {
441 return fDelayedReleaseTexture;
442 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500443
444 sk_sp<GrTexture> cachedTexture;
445 SkASSERT(fLastFulfilledKey.isValid() == (fLastFulfillID > 0));
446 if (fLastFulfilledKey.isValid()) {
447 auto surf = resourceProvider->findByUniqueKey<GrSurface>(fLastFulfilledKey);
448 if (surf) {
449 cachedTexture = sk_ref_sp(surf->asTexture());
450 SkASSERT(cachedTexture);
451 }
452 }
453 // If the release callback hasn't been called already by releasing the GrTexture
454 // then we can be sure that won't happen so long as we have a ref to the texture.
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500455 if (cachedTexture && !fReleaseContext->isReleased()) {
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500456 return std::move(cachedTexture);
457 }
458 GrBackendTexture backendTexture;
Brian Salomon553610d2019-01-14 17:34:12 -0500459 sk_sp<SkPromiseImageTexture> promiseTexture =
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500460 fFulfillProc(fReleaseContext->textureContext());
461 fReleaseContext->notifyWasFulfilled();
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500462 if (!promiseTexture) {
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500463 fReleaseContext->release();
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500464 return sk_sp<GrTexture>();
465 }
466 bool same = promiseTexture->uniqueID() == fLastFulfillID;
467 SkASSERT(!same || fLastFulfilledKey.isValid());
468 if (same && cachedTexture) {
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500469 SkASSERT(fReleaseContext->unique());
470 this->addToIdleContext(cachedTexture.get());
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500471 return std::move(cachedTexture);
472 } else if (cachedTexture) {
473 cachedTexture->resourcePriv().removeUniqueKey();
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500474 }
475 fLastFulfillID = promiseTexture->uniqueID();
476
477 backendTexture = promiseTexture->backendTexture();
478 backendTexture.fConfig = fConfig;
479 if (!backendTexture.isValid()) {
480 // Even though the GrBackendTexture is not valid, we must call the release
481 // proc to keep our contract of always calling Fulfill and Release in pairs.
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500482 fReleaseContext->release();
Brian Salomon426ba462019-01-10 16:33:06 +0000483 return sk_sp<GrTexture>();
Brian Salomon559c6172019-01-10 10:23:44 -0500484 }
485
Brian Salomonf55e8d52019-01-30 17:28:20 -0500486 sk_sp<GrTexture> tex;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500487 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
488 GrUniqueKey::Builder builder(&fLastFulfilledKey, kDomain, 2, "promise");
489 builder[0] = promiseTexture->uniqueID();
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500490 builder[1] = fConfig;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500491 builder.finish();
Brian Salomon0d606762019-01-25 09:58:38 -0500492 // A texture with this key may already exist from a different instance of this lazy
493 // callback. This could happen if the client fulfills a promise image with a texture
494 // that was previously used to fulfill a different promise image.
Brian Salomon0d606762019-01-25 09:58:38 -0500495 if (auto surf = resourceProvider->findByUniqueKey<GrSurface>(fLastFulfilledKey)) {
496 tex = sk_ref_sp(surf->asTexture());
497 SkASSERT(tex);
498 } else {
499 if ((tex = resourceProvider->wrapBackendTexture(
500 backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kYes,
501 kRead_GrIOType))) {
502 tex->resourcePriv().setUniqueKey(fLastFulfilledKey);
503 } else {
504 // Even though we failed to wrap the backend texture, we must call the release
505 // proc to keep our contract of always calling Fulfill and Release in pairs.
506 fReleaseContext->release();
507 return sk_sp<GrTexture>();
508 }
509 }
510 this->addToIdleContext(tex.get());
Brian Salomonf55e8d52019-01-30 17:28:20 -0500511 if (fDelayReleaseCallback == DelayReleaseCallback::kYes) {
512 fDelayedReleaseTexture = tex;
513 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500514 tex->resourcePriv().setUniqueKey(fLastFulfilledKey);
515 SkASSERT(fContextID == SK_InvalidUniqueID ||
Robert Phillips9da87e02019-02-04 13:26:26 -0500516 fContextID == tex->getContext()->priv().contextID());
517 fContextID = tex->getContext()->priv().contextID();
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500518 promiseTexture->addKeyToInvalidate(fContextID, fLastFulfilledKey);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500519 return std::move(tex);
520 }
521
522 private:
Brian Salomon553610d2019-01-14 17:34:12 -0500523 // The GrTexture's idle callback mechanism is used to call the client's Release proc via
524 // this class. This also owns a ref counted helper that calls the client's ReleaseProc when
525 // the ref count reaches zero. The callback and any Fulfilled but un-Released texture share
526 // ownership of the IdleContext. Thus, the IdleContext is destroyed and calls the Done proc
527 // after the last fulfilled texture goes idle and calls the Release proc or the proxy's
528 // destructor destroys the lazy callback, whichever comes last.
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500529 class IdleContext {
Brian Salomon553610d2019-01-14 17:34:12 -0500530 public:
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500531 class PromiseImageReleaseContext;
Brian Salomon553610d2019-01-14 17:34:12 -0500532
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500533 IdleContext() = default;
Brian Salomon553610d2019-01-14 17:34:12 -0500534
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500535 ~IdleContext() = default;
Brian Salomon553610d2019-01-14 17:34:12 -0500536
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500537 void addImageReleaseContext(sk_sp<PromiseImageReleaseContext> context) {
538 fReleaseContexts.addToHead(std::move(context));
Brian Salomon553610d2019-01-14 17:34:12 -0500539 }
540
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500541 static void IdleProc(void* context) {
542 IdleContext* idleContext = static_cast<IdleContext*>(context);
543 for (ReleaseContextList::Iter iter = idleContext->fReleaseContexts.headIter();
544 iter.get();
545 iter.next()) {
546 (*iter.get())->release();
547 }
548 idleContext->fReleaseContexts.reset();
549 delete idleContext;
550 }
551
552 class PromiseImageReleaseContext : public SkNVRefCnt<PromiseImageReleaseContext> {
553 public:
554 PromiseImageReleaseContext(PromiseImageTextureReleaseProc releaseProc,
555 PromiseImageTextureContext textureContext,
556 sk_sp<GrReleaseProcHelper> doneHelper)
557 : fReleaseProc(releaseProc)
558 , fTextureContext(textureContext)
559 , fDoneHelper(std::move(doneHelper)) {}
560
561 ~PromiseImageReleaseContext() { SkASSERT(fIsReleased); }
562
563 void release() {
564 SkASSERT(!fIsReleased);
565 fReleaseProc(fTextureContext);
566 fIsReleased = true;
567 }
568
569 void notifyWasFulfilled() { fIsReleased = false; }
570 bool isReleased() const { return fIsReleased; }
571
572 PromiseImageTextureContext textureContext() const { return fTextureContext; }
573
574 private:
575 PromiseImageTextureReleaseProc fReleaseProc;
576 PromiseImageTextureContext fTextureContext;
577 sk_sp<GrReleaseProcHelper> fDoneHelper;
578 bool fIsReleased = true;
579 };
580
Brian Salomon553610d2019-01-14 17:34:12 -0500581 private:
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500582 using ReleaseContextList = SkTLList<sk_sp<PromiseImageReleaseContext>, 4>;
583 ReleaseContextList fReleaseContexts;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500584 };
Brian Salomonbe5a0932018-12-10 10:03:26 -0500585
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500586 void addToIdleContext(GrTexture* texture) {
587 SkASSERT(!fReleaseContext->isReleased());
588 IdleContext* idleContext = static_cast<IdleContext*>(texture->idleContext());
589 if (!idleContext) {
590 idleContext = new IdleContext();
591 texture->setIdleProc(IdleContext::IdleProc, idleContext);
592 }
593 idleContext->addImageReleaseContext(fReleaseContext);
594 }
595
596 sk_sp<IdleContext::PromiseImageReleaseContext> fReleaseContext;
Brian Salomonf55e8d52019-01-30 17:28:20 -0500597 sk_sp<GrTexture> fDelayedReleaseTexture;
Brian Salomon553610d2019-01-14 17:34:12 -0500598 PromiseImageTextureFulfillProc fFulfillProc;
599 GrPixelConfig fConfig;
Brian Salomonf55e8d52019-01-30 17:28:20 -0500600 DelayReleaseCallback fDelayReleaseCallback;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500601
602 // ID of the last SkPromiseImageTexture given to us by the client.
603 uint32_t fLastFulfillID = 0;
604 // ID of the GrContext that we are interacting with.
605 uint32_t fContextID = SK_InvalidUniqueID;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500606 GrUniqueKey fLastFulfilledKey;
Brian Salomonf55e8d52019-01-30 17:28:20 -0500607 } callback(fulfillProc, releaseProc, doneProc, textureContext, delayReleaseCallback, config);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500608
Robert Phillips9da87e02019-02-04 13:26:26 -0500609 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonbe5a0932018-12-10 10:03:26 -0500610
611 GrSurfaceDesc desc;
612 desc.fWidth = width;
613 desc.fHeight = height;
614 desc.fConfig = config;
615
616 // We pass kReadOnly here since we should treat content of the client's texture as immutable.
617 return proxyProvider->createLazyProxy(std::move(callback), backendFormat, desc, origin,
618 mipMapped, GrInternalSurfaceFlags::kReadOnly,
619 SkBackingFit::kExact, SkBudgeted::kNo,
620 GrSurfaceProxy::LazyInstantiationType::kDeinstantiate);
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400621}