blob: 2618dd4276eb9fd63d32b36e8a61761f7ab60500 [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"
Robert Phillips6603a172019-03-05 12:35:44 -050013#include "GrRecordingContext.h"
14#include "GrRecordingContextPriv.h"
Jim Van Verth8026ccc2018-10-04 13:10:39 -040015#include "GrRenderTargetContext.h"
16#include "GrTexture.h"
17#include "GrTextureAdjuster.h"
18#include "SkBitmapCache.h"
19#include "SkImage_Gpu.h"
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050020#include "SkPromiseImageTexture.h"
Jim Van Verth8bbce0e2018-10-08 14:34:52 -040021#include "SkReadPixelsRec.h"
Brian Salomon1bf0ed82019-01-16 13:51:35 -050022#include "SkTLList.h"
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050023#include "effects/GrYUVtoRGBEffect.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 Salomonf05e6d32018-12-20 08:41:41 -050026 SkAlphaType at, sk_sp<SkColorSpace> cs)
Jim Van Verth8026ccc2018-10-04 13:10:39 -040027 : INHERITED(width, height, uniqueID)
28 , fContext(std::move(context))
29 , fAlphaType(at)
Jim Van Verth8026ccc2018-10-04 13:10:39 -040030 , fColorSpace(std::move(cs)) {}
31
32SkImage_GpuBase::~SkImage_GpuBase() {}
33
34//////////////////////////////////////////////////////////////////////////////////////////////////
35
Robert Phillipsfd0d9702019-02-01 10:19:42 -050036#if GR_TEST_UTILS
37void SkImage_GpuBase::resetContext(sk_sp<GrContext> newContext) {
Robert Phillipsfe0963c2019-02-07 13:25:07 -050038 SkASSERT(fContext->priv().matches(newContext.get()));
Robert Phillipsfd0d9702019-02-01 10:19:42 -050039 fContext = newContext;
40}
41#endif
42
Jim Van Verth8026ccc2018-10-04 13:10:39 -040043bool SkImage_GpuBase::ValidateBackendTexture(GrContext* ctx, const GrBackendTexture& tex,
44 GrPixelConfig* config, SkColorType ct, SkAlphaType at,
45 sk_sp<SkColorSpace> cs) {
46 if (!tex.isValid()) {
47 return false;
48 }
49 // TODO: Create a SkImageColorInfo struct for color, alpha, and color space so we don't need to
50 // create a fake image info here.
51 SkImageInfo info = SkImageInfo::Make(1, 1, ct, at, cs);
52 if (!SkImageInfoIsValid(info)) {
53 return false;
54 }
Brian Salomonf391d0f2018-12-14 09:18:50 -050055 GrBackendFormat backendFormat = tex.getBackendFormat();
56 if (!backendFormat.isValid()) {
57 return false;
58 }
Robert Phillips9da87e02019-02-04 13:26:26 -050059 *config = ctx->priv().caps()->getConfigFromBackendFormat(backendFormat, ct);
Brian Salomonf391d0f2018-12-14 09:18:50 -050060 return *config != kUnknown_GrPixelConfig;
Jim Van Verth8026ccc2018-10-04 13:10:39 -040061}
62
63//////////////////////////////////////////////////////////////////////////////////////////////////
64
Brian Osmane50cdf02018-10-19 13:02:14 -040065bool SkImage_GpuBase::getROPixels(SkBitmap* dst, CachingHint chint) const {
Robert Phillips920d4882019-03-04 15:16:44 -050066 auto direct = fContext->priv().asDirectContext();
67 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040068 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
69 return false;
70 }
71
Jim Van Verth8026ccc2018-10-04 13:10:39 -040072 const auto desc = SkBitmapCacheDesc::Make(this);
73 if (SkBitmapCache::Find(desc, dst)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040074 SkASSERT(dst->isImmutable());
75 SkASSERT(dst->getPixels());
76 return true;
77 }
78
79 SkBitmapCache::RecPtr rec = nullptr;
80 SkPixmap pmap;
81 if (kAllow_CachingHint == chint) {
82 rec = SkBitmapCache::Alloc(desc, this->onImageInfo(), &pmap);
83 if (!rec) {
84 return false;
85 }
86 } else {
87 if (!dst->tryAllocPixels(this->onImageInfo()) || !dst->peekPixels(&pmap)) {
88 return false;
89 }
90 }
91
Robert Phillips920d4882019-03-04 15:16:44 -050092 sk_sp<GrSurfaceContext> sContext = direct->priv().makeWrappedSurfaceContext(
Robert Phillips6603a172019-03-05 12:35:44 -050093 this->asTextureProxyRef(direct), fColorSpace);
Jim Van Verth8026ccc2018-10-04 13:10:39 -040094 if (!sContext) {
95 return false;
96 }
97
98 if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), 0, 0)) {
99 return false;
100 }
101
102 if (rec) {
103 SkBitmapCache::Add(std::move(rec), dst);
104 this->notifyAddedToRasterCache();
105 }
106 return true;
107}
108
Robert Phillips6603a172019-03-05 12:35:44 -0500109sk_sp<SkImage> SkImage_GpuBase::onMakeSubset(GrRecordingContext* context,
110 const SkIRect& subset) const {
111 if (!context || !fContext->priv().matches(context)) {
112 return nullptr;
113 }
114
115 sk_sp<GrSurfaceProxy> proxy = this->asTextureProxyRef(context);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400116
117 GrSurfaceDesc desc;
118 desc.fWidth = subset.width();
119 desc.fHeight = subset.height();
120 desc.fConfig = proxy->config();
121
Greg Daniel4065d452018-11-16 15:43:41 -0500122 GrBackendFormat format = proxy->backendFormat().makeTexture2D();
123 if (!format.isValid()) {
124 return nullptr;
125 }
126
Brian Salomonf05e6d32018-12-20 08:41:41 -0500127 // TODO: Should this inherit our proxy's budgeted status?
Robert Phillips6603a172019-03-05 12:35:44 -0500128 sk_sp<GrSurfaceContext> sContext(context->priv().makeDeferredSurfaceContext(
Brian Salomonf05e6d32018-12-20 08:41:41 -0500129 format, desc, proxy->origin(), GrMipMapped::kNo, SkBackingFit::kExact,
130 proxy->isBudgeted()));
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400131 if (!sContext) {
132 return nullptr;
133 }
134
135 if (!sContext->copy(proxy.get(), subset, SkIPoint::Make(0, 0))) {
136 return nullptr;
137 }
138
139 // MDB: this call is okay bc we know 'sContext' was kExact
Brian Salomonf05e6d32018-12-20 08:41:41 -0500140 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, fAlphaType,
Brian Osmane9560492019-02-05 17:00:03 -0500141 sContext->asTextureProxyRef(), this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400142}
143
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400144static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
145 switch (info.colorType()) {
146 case kRGBA_8888_SkColorType:
147 case kBGRA_8888_SkColorType:
148 break;
149 default:
150 return; // nothing to do
151 }
152
Brian Salomon3f4cd772019-01-11 16:03:19 -0500153 // SkColor is not necessarily RGBA or BGRA, but it is one of them on little-endian,
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400154 // and in either case, the alpha-byte is always in the same place, so we can safely call
155 // SkPreMultiplyColor()
156 //
157 SkColor* row = (SkColor*)pixels;
158 for (int y = 0; y < info.height(); ++y) {
159 for (int x = 0; x < info.width(); ++x) {
160 row[x] = SkPreMultiplyColor(row[x]);
161 }
162 row = (SkColor*)((char*)(row)+rowBytes);
163 }
164}
165
166bool SkImage_GpuBase::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
167 int srcX, int srcY, CachingHint) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500168 auto direct = fContext->priv().asDirectContext();
169 if (!direct) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400170 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
171 return false;
172 }
173
174 if (!SkImageInfoValidConversion(dstInfo, this->onImageInfo())) {
175 return false;
176 }
177
178 SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, srcX, srcY);
179 if (!rec.trim(this->width(), this->height())) {
180 return false;
181 }
182
183 // TODO: this seems to duplicate code in GrTextureContext::onReadPixels and
184 // GrRenderTargetContext::onReadPixels
185 uint32_t flags = 0;
186 if (kUnpremul_SkAlphaType == rec.fInfo.alphaType() && kPremul_SkAlphaType == fAlphaType) {
187 // let the GPU perform this transformation for us
188 flags = GrContextPriv::kUnpremul_PixelOpsFlag;
189 }
190
Robert Phillips920d4882019-03-04 15:16:44 -0500191 sk_sp<GrSurfaceContext> sContext = direct->priv().makeWrappedSurfaceContext(
Robert Phillips6603a172019-03-05 12:35:44 -0500192 this->asTextureProxyRef(direct), this->refColorSpace());
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400193 if (!sContext) {
194 return false;
195 }
196
197 if (!sContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY, flags)) {
198 return false;
199 }
200
201 // do we have to manually fix-up the alpha channel?
202 // src dst
203 // unpremul premul fix manually
204 // premul unpremul done by kUnpremul_PixelOpsFlag
205 // all other combos need to change.
206 //
207 // Should this be handled by Ganesh? todo:?
208 //
209 if (kPremul_SkAlphaType == rec.fInfo.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
210 apply_premul(rec.fInfo, rec.fPixels, rec.fRowBytes);
211 }
212 return true;
213}
214
Robert Phillips9338c602019-02-19 12:52:29 -0500215sk_sp<GrTextureProxy> SkImage_GpuBase::asTextureProxyRef(GrRecordingContext* context,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400216 const GrSamplerState& params,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400217 SkScalar scaleAdjust[2]) const {
Robert Phillips6603a172019-03-05 12:35:44 -0500218 if (!context || !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400219 SkASSERT(0);
220 return nullptr;
221 }
222
Robert Phillips6603a172019-03-05 12:35:44 -0500223 GrTextureAdjuster adjuster(fContext.get(), this->asTextureProxyRef(context), fAlphaType,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400224 this->uniqueID(), fColorSpace.get());
Brian Osman6064e1c2018-10-19 14:27:54 -0400225 return adjuster.refTextureProxyForParams(params, scaleAdjust);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400226}
227
228GrBackendTexture SkImage_GpuBase::onGetBackendTexture(bool flushPendingGrContextIO,
229 GrSurfaceOrigin* origin) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500230 auto direct = fContext->priv().asDirectContext();
231 if (!direct) {
232 // This image was created with a DDL context and cannot be instantiated.
Robert Phillips6603a172019-03-05 12:35:44 -0500233 return GrBackendTexture(); // invalid
Robert Phillips920d4882019-03-04 15:16:44 -0500234 }
235
Robert Phillips6603a172019-03-05 12:35:44 -0500236 sk_sp<GrTextureProxy> proxy = this->asTextureProxyRef(direct);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400237 SkASSERT(proxy);
238
Robert Phillips920d4882019-03-04 15:16:44 -0500239 if (!proxy->isInstantiated()) {
240 auto resourceProvider = direct->priv().resourceProvider();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400241
Robert Phillips920d4882019-03-04 15:16:44 -0500242 if (!proxy->instantiate(resourceProvider)) {
243 return GrBackendTexture(); // invalid
244 }
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400245 }
246
247 GrTexture* texture = proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400248 if (texture) {
249 if (flushPendingGrContextIO) {
Robert Phillips920d4882019-03-04 15:16:44 -0500250 direct->priv().prepareSurfaceForExternalIO(proxy.get());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400251 }
252 if (origin) {
253 *origin = proxy->origin();
254 }
255 return texture->getBackendTexture();
256 }
257 return GrBackendTexture(); // invalid
258}
259
260GrTexture* SkImage_GpuBase::onGetTexture() const {
261 GrTextureProxy* proxy = this->peekProxy();
Robert Phillips193c4212019-03-04 12:18:53 -0500262 if (proxy && proxy->isInstantiated()) {
263 return proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400264 }
265
Robert Phillips193c4212019-03-04 12:18:53 -0500266 auto direct = fContext->priv().asDirectContext();
267 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400268 // This image was created with a DDL context and cannot be instantiated.
269 return nullptr;
270 }
271
Robert Phillips6603a172019-03-05 12:35:44 -0500272 sk_sp<GrTextureProxy> proxyRef = this->asTextureProxyRef(direct);
Robert Phillips193c4212019-03-04 12:18:53 -0500273 SkASSERT(proxyRef && !proxyRef->isInstantiated());
274
275 if (!proxyRef->instantiate(direct->priv().resourceProvider())) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400276 return nullptr;
277 }
278
Robert Phillips193c4212019-03-04 12:18:53 -0500279 return proxyRef->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400280}
281
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400282bool SkImage_GpuBase::onIsValid(GrContext* context) const {
283 // The base class has already checked that context isn't abandoned (if it's not nullptr)
Robert Phillips920d4882019-03-04 15:16:44 -0500284 if (fContext->priv().abandoned()) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400285 return false;
286 }
287
Robert Phillips920d4882019-03-04 15:16:44 -0500288 if (context && !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400289 return false;
290 }
291
292 return true;
293}
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400294
Jim Van Verth0e671942018-11-09 12:03:57 -0500295bool SkImage_GpuBase::MakeTempTextureProxies(GrContext* ctx, const GrBackendTexture yuvaTextures[],
Jim Van Verth53275362018-11-09 15:42:35 -0500296 int numTextures, const SkYUVAIndex yuvaIndices[4],
297 GrSurfaceOrigin imageOrigin,
Jim Van Verth0e671942018-11-09 12:03:57 -0500298 sk_sp<GrTextureProxy> tempTextureProxies[4]) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500299 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Jim Van Verth0e671942018-11-09 12:03:57 -0500300
301 // We need to make a copy of the input backend textures because we need to preserve the result
302 // of validate_backend_texture.
303 GrBackendTexture yuvaTexturesCopy[4];
304 for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) {
305 yuvaTexturesCopy[textureIndex] = yuvaTextures[textureIndex];
Brian Salomonf391d0f2018-12-14 09:18:50 -0500306 GrBackendFormat backendFormat = yuvaTexturesCopy[textureIndex].getBackendFormat();
307 if (!backendFormat.isValid()) {
308 return false;
309 }
310 yuvaTexturesCopy[textureIndex].fConfig =
Robert Phillips9da87e02019-02-04 13:26:26 -0500311 ctx->priv().caps()->getYUVAConfigFromBackendFormat(backendFormat);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500312 if (yuvaTexturesCopy[textureIndex].fConfig == kUnknown_GrPixelConfig) {
Jim Van Verth0e671942018-11-09 12:03:57 -0500313 return false;
314 }
315 SkASSERT(yuvaTexturesCopy[textureIndex].isValid());
316
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500317 tempTextureProxies[textureIndex] = proxyProvider->wrapBackendTexture(
318 yuvaTexturesCopy[textureIndex], imageOrigin, kBorrow_GrWrapOwnership,
319 GrWrapCacheable::kNo, kRead_GrIOType);
Jim Van Verth0e671942018-11-09 12:03:57 -0500320 if (!tempTextureProxies[textureIndex]) {
321 return false;
322 }
Jim Van Verth53275362018-11-09 15:42:35 -0500323
324 // Check that each texture contains the channel data for the corresponding YUVA index
325 GrPixelConfig config = yuvaTexturesCopy[textureIndex].fConfig;
326 for (int yuvaIndex = 0; yuvaIndex < SkYUVAIndex::kIndexCount; ++yuvaIndex) {
327 if (yuvaIndices[yuvaIndex].fIndex == textureIndex) {
328 switch (yuvaIndices[yuvaIndex].fChannel) {
329 case SkColorChannel::kR:
330 if (kAlpha_8_as_Alpha_GrPixelConfig == config) {
331 return false;
332 }
333 break;
334 case SkColorChannel::kG:
335 case SkColorChannel::kB:
336 if (kAlpha_8_as_Alpha_GrPixelConfig == config ||
337 kAlpha_8_as_Red_GrPixelConfig == config) {
338 return false;
339 }
340 break;
341 case SkColorChannel::kA:
342 default:
343 if (kRGB_888_GrPixelConfig == config) {
344 return false;
345 }
346 break;
347 }
348 }
349 }
Jim Van Verth0e671942018-11-09 12:03:57 -0500350 }
351
352 return true;
353}
354
355bool SkImage_GpuBase::RenderYUVAToRGBA(GrContext* ctx, GrRenderTargetContext* renderTargetContext,
356 const SkRect& rect, SkYUVColorSpace yuvColorSpace,
Brian Osmane9560492019-02-05 17:00:03 -0500357 sk_sp<GrColorSpaceXform> colorSpaceXform,
Jim Van Verth0e671942018-11-09 12:03:57 -0500358 const sk_sp<GrTextureProxy> proxies[4],
359 const SkYUVAIndex yuvaIndices[4]) {
360 SkASSERT(renderTargetContext);
361 if (!renderTargetContext->asSurfaceProxy()) {
362 return false;
363 }
364
365 GrPaint paint;
366 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
367
Brian Osmane9560492019-02-05 17:00:03 -0500368 auto fp = GrYUVtoRGBEffect::Make(proxies, yuvaIndices, yuvColorSpace,
369 GrSamplerState::Filter::kNearest);
370 if (colorSpaceXform) {
371 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(colorSpaceXform));
372 }
373 paint.addColorFragmentProcessor(std::move(fp));
Jim Van Verth0e671942018-11-09 12:03:57 -0500374
375 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Mike Reedfbc887d2019-03-05 01:32:07 +0000376
377 // DDL TODO: in the promise image version we must not flush here
378 ctx->priv().flushSurfaceWrites(renderTargetContext->asSurfaceProxy());
379
Jim Van Verth0e671942018-11-09 12:03:57 -0500380 return true;
381}
382
Brian Salomonbe5a0932018-12-10 10:03:26 -0500383sk_sp<GrTextureProxy> SkImage_GpuBase::MakePromiseImageLazyProxy(
384 GrContext* context, int width, int height, GrSurfaceOrigin origin, GrPixelConfig config,
385 GrBackendFormat backendFormat, GrMipMapped mipMapped,
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500386 PromiseImageTextureFulfillProc fulfillProc,
387 PromiseImageTextureReleaseProc releaseProc,
388 PromiseImageTextureDoneProc doneProc,
Brian Salomon7d88f312019-02-28 10:03:03 -0500389 PromiseImageTextureContext textureContext) {
Brian Salomonbe5a0932018-12-10 10:03:26 -0500390 SkASSERT(context);
391 SkASSERT(width > 0 && height > 0);
392 SkASSERT(doneProc);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500393 SkASSERT(config != kUnknown_GrPixelConfig);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500394
395 if (!fulfillProc || !releaseProc) {
396 doneProc(textureContext);
397 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400398 }
399
Brian Salomonbe5a0932018-12-10 10:03:26 -0500400 if (mipMapped == GrMipMapped::kYes &&
401 GrTextureTypeHasRestrictedSampling(backendFormat.textureType())) {
402 // It is invalid to have a GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE and have mips as
403 // well.
404 doneProc(textureContext);
405 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400406 }
Brian Salomonbe5a0932018-12-10 10:03:26 -0500407
408 /**
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500409 * This class is the lazy instantiation callback for promise images. It manages calling the
410 * client's Fulfill, Release, and Done procs. It attempts to reuse a GrTexture instance in
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500411 * cases where the client provides the same SkPromiseImageTexture as Fulfill results for
412 * multiple SkImages. The created GrTexture is given a key based on a unique ID associated with
413 * the SkPromiseImageTexture.
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500414 *
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500415 * The GrTexutre idle proc mechanism is used to call the Release and Done procs. We use this
416 * instead of the GrSurface release proc because the GrTexture is cached and therefore may
417 * outlive the proxy into which this callback is installed.
418 *
419 * A key invalidation message is installed on the SkPromiseImageTexture so that the GrTexture
420 * is deleted once it can no longer be used to instantiate a proxy.
Brian Salomonbe5a0932018-12-10 10:03:26 -0500421 */
422 class PromiseLazyInstantiateCallback {
423 public:
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500424 PromiseLazyInstantiateCallback(PromiseImageTextureFulfillProc fulfillProc,
425 PromiseImageTextureReleaseProc releaseProc,
426 PromiseImageTextureDoneProc doneProc,
427 PromiseImageTextureContext context,
Brian Salomonbe5a0932018-12-10 10:03:26 -0500428 GrPixelConfig config)
Brian Salomon9ac04072019-03-05 08:27:46 -0500429 : fFulfillProc(fulfillProc), fReleaseProc(releaseProc), fConfig(config) {
430 fDoneCallback = sk_make_sp<GrRefCntedCallback>(doneProc, context);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500431 }
432 PromiseLazyInstantiateCallback(PromiseLazyInstantiateCallback&&) = default;
433 PromiseLazyInstantiateCallback(const PromiseLazyInstantiateCallback&) {
434 // Because we get wrapped in std::function we must be copyable. But we should never
435 // be copied.
436 SkASSERT(false);
437 }
438 PromiseLazyInstantiateCallback& operator=(PromiseLazyInstantiateCallback&&) = default;
439 PromiseLazyInstantiateCallback& operator=(const PromiseLazyInstantiateCallback&) {
440 SkASSERT(false);
441 return *this;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500442 }
Brian Salomon553610d2019-01-14 17:34:12 -0500443
Brian Salomonbe5a0932018-12-10 10:03:26 -0500444 sk_sp<GrSurface> operator()(GrResourceProvider* resourceProvider) {
Brian Salomon9ac04072019-03-05 08:27:46 -0500445 SkASSERT(fDoneCallback);
446 PromiseImageTextureContext textureContext = fDoneCallback->context();
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500447 sk_sp<SkPromiseImageTexture> promiseTexture = fFulfillProc(textureContext);
448 // From here on out our contract is that the release proc must be called, even if
449 // the return from fulfill was invalid or we fail for some other reason.
Brian Salomon9ac04072019-03-05 08:27:46 -0500450 auto releaseCallback = sk_make_sp<GrRefCntedCallback>(fReleaseProc, textureContext);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500451 if (!promiseTexture) {
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500452 return sk_sp<GrTexture>();
453 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500454
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500455 auto backendTexture = promiseTexture->backendTexture();
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500456 backendTexture.fConfig = fConfig;
457 if (!backendTexture.isValid()) {
Brian Salomon426ba462019-01-10 16:33:06 +0000458 return sk_sp<GrTexture>();
Brian Salomon559c6172019-01-10 10:23:44 -0500459 }
460
Brian Salomonf55e8d52019-01-30 17:28:20 -0500461 sk_sp<GrTexture> tex;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500462 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Salomon7d88f312019-02-28 10:03:03 -0500463 GrUniqueKey key;
464 GrUniqueKey::Builder builder(&key, kDomain, 2, "promise");
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500465 builder[0] = promiseTexture->uniqueID();
Brian Salomon1bf0ed82019-01-16 13:51:35 -0500466 builder[1] = fConfig;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500467 builder.finish();
Brian Salomon0d606762019-01-25 09:58:38 -0500468 // A texture with this key may already exist from a different instance of this lazy
469 // callback. This could happen if the client fulfills a promise image with a texture
470 // that was previously used to fulfill a different promise image.
Brian Salomon7d88f312019-02-28 10:03:03 -0500471 if (auto surf = resourceProvider->findByUniqueKey<GrSurface>(key)) {
Brian Salomon0d606762019-01-25 09:58:38 -0500472 tex = sk_ref_sp(surf->asTexture());
473 SkASSERT(tex);
474 } else {
475 if ((tex = resourceProvider->wrapBackendTexture(
476 backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kYes,
477 kRead_GrIOType))) {
Brian Salomon7d88f312019-02-28 10:03:03 -0500478 tex->resourcePriv().setUniqueKey(key);
Brian Salomon0d606762019-01-25 09:58:38 -0500479 } else {
Brian Salomon0d606762019-01-25 09:58:38 -0500480 return sk_sp<GrTexture>();
481 }
482 }
Brian Salomon9ac04072019-03-05 08:27:46 -0500483 tex->addIdleProc(std::move(releaseCallback), GrTexture::IdleState::kFinished);
484 tex->addIdleProc(std::move(fDoneCallback), GrTexture::IdleState::kFinished);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500485 promiseTexture->addKeyToInvalidate(tex->getContext()->priv().contextID(), key);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500486 return std::move(tex);
487 }
488
489 private:
Brian Salomon553610d2019-01-14 17:34:12 -0500490 PromiseImageTextureFulfillProc fFulfillProc;
Brian Salomon9ac04072019-03-05 08:27:46 -0500491 PromiseImageTextureReleaseProc fReleaseProc;
492 sk_sp<GrRefCntedCallback> fDoneCallback;
Brian Salomon553610d2019-01-14 17:34:12 -0500493 GrPixelConfig fConfig;
Brian Salomon7d88f312019-02-28 10:03:03 -0500494 } callback(fulfillProc, releaseProc, doneProc, textureContext, config);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500495
Robert Phillips9da87e02019-02-04 13:26:26 -0500496 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonbe5a0932018-12-10 10:03:26 -0500497
498 GrSurfaceDesc desc;
499 desc.fWidth = width;
500 desc.fHeight = height;
501 desc.fConfig = config;
502
503 // We pass kReadOnly here since we should treat content of the client's texture as immutable.
504 return proxyProvider->createLazyProxy(std::move(callback), backendFormat, desc, origin,
505 mipMapped, GrInternalSurfaceFlags::kReadOnly,
506 SkBackingFit::kExact, SkBudgeted::kNo,
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500507 GrSurfaceProxy::LazyInstantiationType::kSingleUse);
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400508}