blob: 6a21b93960b0b2b3d760a7939c76a4379f2ad74e [file] [log] [blame]
Jim Van Verth8026ccc2018-10-04 13:10:39 -04001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkPromiseImageTexture.h"
9#include "include/gpu/GrBackendSurface.h"
10#include "include/gpu/GrContext.h"
11#include "include/gpu/GrTexture.h"
12#include "include/private/GrRecordingContext.h"
13#include "src/core/SkBitmapCache.h"
14#include "src/core/SkTLList.h"
15#include "src/gpu/GrClip.h"
16#include "src/gpu/GrContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040017#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrRecordingContextPriv.h"
19#include "src/gpu/GrRenderTargetContext.h"
20#include "src/gpu/GrTextureAdjuster.h"
21#include "src/gpu/effects/GrYUVtoRGBEffect.h"
22#include "src/image/SkImage_Gpu.h"
23#include "src/image/SkImage_GpuBase.h"
24#include "src/image/SkReadPixelsRec.h"
Jim Van Verth8026ccc2018-10-04 13:10:39 -040025
Brian Salomon9f2b86c2019-10-22 10:37:46 -040026SkImage_GpuBase::SkImage_GpuBase(sk_sp<GrContext> context, SkISize size, uint32_t uniqueID,
Brian Salomon5ad6fd32019-03-21 15:30:08 -040027 SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs)
Brian Salomon9f2b86c2019-10-22 10:37:46 -040028 : INHERITED(SkImageInfo::Make(size, ct, at, std::move(cs)), uniqueID)
Brian Salomon5ad6fd32019-03-21 15:30:08 -040029 , fContext(std::move(context)) {}
Jim Van Verth8026ccc2018-10-04 13:10:39 -040030
Jim Van Verth8026ccc2018-10-04 13:10:39 -040031//////////////////////////////////////////////////////////////////////////////////////////////////
32
Robert Phillipsfd0d9702019-02-01 10:19:42 -050033#if GR_TEST_UTILS
34void SkImage_GpuBase::resetContext(sk_sp<GrContext> newContext) {
Robert Phillipsfe0963c2019-02-07 13:25:07 -050035 SkASSERT(fContext->priv().matches(newContext.get()));
Robert Phillipsfd0d9702019-02-01 10:19:42 -050036 fContext = newContext;
37}
38#endif
39
Robert Phillips62221e72019-07-24 15:07:38 -040040bool SkImage_GpuBase::ValidateBackendTexture(const GrCaps* caps, const GrBackendTexture& tex,
41 GrColorType grCT, SkColorType ct, SkAlphaType at,
Jim Van Verth8026ccc2018-10-04 13:10:39 -040042 sk_sp<SkColorSpace> cs) {
43 if (!tex.isValid()) {
44 return false;
45 }
46 // TODO: Create a SkImageColorInfo struct for color, alpha, and color space so we don't need to
47 // create a fake image info here.
48 SkImageInfo info = SkImageInfo::Make(1, 1, ct, at, cs);
49 if (!SkImageInfoIsValid(info)) {
50 return false;
51 }
Brian Salomonf391d0f2018-12-14 09:18:50 -050052 GrBackendFormat backendFormat = tex.getBackendFormat();
53 if (!backendFormat.isValid()) {
54 return false;
55 }
Greg Daniele877dce2019-07-11 10:52:43 -040056
Robert Phillips62221e72019-07-24 15:07:38 -040057 return caps->areColorTypeAndFormatCompatible(grCT, backendFormat);
Jim Van Verth8026ccc2018-10-04 13:10:39 -040058}
59
Robert Phillipsead321b2019-12-19 10:16:32 -050060bool SkImage_GpuBase::ValidateCompressedBackendTexture(const GrCaps* caps,
61 const GrBackendTexture& tex,
62 SkAlphaType at) {
63
64 if (!tex.isValid() || tex.width() <= 0 || tex.height() <= 0) {
65 return false;
66 }
67
68 if (tex.width() > caps->maxTextureSize() || tex.height() > caps->maxTextureSize()) {
69 return false;
70 }
71
72 if (at == kUnknown_SkAlphaType) {
73 return false;
74 }
75
76 GrBackendFormat backendFormat = tex.getBackendFormat();
77 if (!backendFormat.isValid()) {
78 return false;
79 }
80
81 if (!caps->isFormatCompressed(backendFormat)) {
82 return false;
83 }
84
85 return true;
86}
87
Jim Van Verth8026ccc2018-10-04 13:10:39 -040088//////////////////////////////////////////////////////////////////////////////////////////////////
89
Brian Osmane50cdf02018-10-19 13:02:14 -040090bool SkImage_GpuBase::getROPixels(SkBitmap* dst, CachingHint chint) const {
Robert Phillips920d4882019-03-04 15:16:44 -050091 auto direct = fContext->priv().asDirectContext();
92 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040093 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
94 return false;
95 }
96
Jim Van Verth8026ccc2018-10-04 13:10:39 -040097 const auto desc = SkBitmapCacheDesc::Make(this);
98 if (SkBitmapCache::Find(desc, dst)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -040099 SkASSERT(dst->isImmutable());
100 SkASSERT(dst->getPixels());
101 return true;
102 }
103
104 SkBitmapCache::RecPtr rec = nullptr;
105 SkPixmap pmap;
106 if (kAllow_CachingHint == chint) {
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400107 rec = SkBitmapCache::Alloc(desc, this->imageInfo(), &pmap);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400108 if (!rec) {
109 return false;
110 }
111 } else {
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400112 if (!dst->tryAllocPixels(this->imageInfo()) || !dst->peekPixels(&pmap)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400113 return false;
114 }
115 }
116
Greg Daniel3912a4b2020-01-14 09:56:04 -0500117 GrSurfaceProxyView view = this->asSurfaceProxyViewRef(direct);
Greg Danielba88ab62019-07-26 09:14:01 -0400118 GrColorType grColorType = SkColorTypeAndFormatToGrColorType(fContext->priv().caps(),
119 this->colorType(),
Greg Daniel3912a4b2020-01-14 09:56:04 -0500120 view.proxy()->backendFormat());
Greg Danielba88ab62019-07-26 09:14:01 -0400121
Greg Daniel3912a4b2020-01-14 09:56:04 -0500122 auto sContext = GrSurfaceContext::Make(direct, std::move(view), grColorType,
Greg Danielbfa19c42019-12-19 16:41:40 -0500123 this->alphaType(), this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400124 if (!sContext) {
125 return false;
126 }
127
Brian Salomon1d435302019-07-01 13:05:28 -0400128 if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), {0, 0})) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400129 return false;
130 }
131
132 if (rec) {
133 SkBitmapCache::Add(std::move(rec), dst);
134 this->notifyAddedToRasterCache();
135 }
136 return true;
137}
138
Robert Phillips6603a172019-03-05 12:35:44 -0500139sk_sp<SkImage> SkImage_GpuBase::onMakeSubset(GrRecordingContext* context,
140 const SkIRect& subset) const {
141 if (!context || !fContext->priv().matches(context)) {
142 return nullptr;
143 }
144
145 sk_sp<GrSurfaceProxy> proxy = this->asTextureProxyRef(context);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400146
Greg Daniel3155f7f2020-01-16 16:54:26 -0500147 GrColorType grColorType = SkColorTypeToGrColorType(this->colorType());
148
Brian Salomonfc118442019-11-22 19:09:27 -0500149 sk_sp<GrTextureProxy> copyProxy =
Greg Daniel3155f7f2020-01-16 16:54:26 -0500150 GrSurfaceProxy::Copy(context, proxy.get(), grColorType, GrMipMapped::kNo, subset,
Brian Salomonfc118442019-11-22 19:09:27 -0500151 SkBackingFit::kExact, proxy->isBudgeted());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400152
Greg Daniel46cfbc62019-06-07 11:43:30 -0400153 if (!copyProxy) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400154 return nullptr;
155 }
156
Greg Daniel1a372c32019-12-17 13:53:38 -0500157 const GrSurfaceProxyView& currView = this->getSurfaceProxyView(context);
Greg Daniel81b98972019-12-13 11:09:43 -0500158 if (!currView.proxy()) {
159 return nullptr;
160 }
161
162 GrSurfaceProxyView view(std::move(copyProxy), currView.origin(), currView.swizzle());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400163 // MDB: this call is okay bc we know 'sContext' was kExact
Greg Daniel7c165a42020-01-22 12:22:36 -0500164 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, std::move(view),
165 this->colorType(), this->alphaType(), this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400166}
167
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400168bool SkImage_GpuBase::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
169 int srcX, int srcY, CachingHint) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500170 auto direct = fContext->priv().asDirectContext();
171 if (!direct) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400172 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
173 return false;
174 }
175
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400176 if (!SkImageInfoValidConversion(dstInfo, this->imageInfo())) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400177 return false;
178 }
179
Greg Daniel3912a4b2020-01-14 09:56:04 -0500180 GrSurfaceProxyView view = this->asSurfaceProxyViewRef(direct);
Greg Danielba88ab62019-07-26 09:14:01 -0400181 GrColorType grColorType = SkColorTypeAndFormatToGrColorType(fContext->priv().caps(),
182 this->colorType(),
Greg Daniel3912a4b2020-01-14 09:56:04 -0500183 view.proxy()->backendFormat());
Greg Danielba88ab62019-07-26 09:14:01 -0400184
Greg Daniel3912a4b2020-01-14 09:56:04 -0500185 auto sContext = GrSurfaceContext::Make(direct, std::move(view), grColorType,
Greg Danielbfa19c42019-12-19 16:41:40 -0500186 this->alphaType(), this->refColorSpace());
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400187 if (!sContext) {
188 return false;
189 }
190
Brian Salomon1d435302019-07-01 13:05:28 -0400191 return sContext->readPixels(dstInfo, dstPixels, dstRB, {srcX, srcY});
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400192}
193
Robert Phillips9338c602019-02-19 12:52:29 -0500194sk_sp<GrTextureProxy> SkImage_GpuBase::asTextureProxyRef(GrRecordingContext* context,
Brian Salomonccb61422020-01-09 10:46:36 -0500195 GrSamplerState params,
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400196 SkScalar scaleAdjust[2]) const {
Robert Phillips6603a172019-03-05 12:35:44 -0500197 if (!context || !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400198 SkASSERT(0);
199 return nullptr;
200 }
201
Brian Salomond6287472019-06-24 15:50:07 -0400202 GrTextureAdjuster adjuster(fContext.get(), this->asTextureProxyRef(context),
Brian Salomon1a217eb2019-10-24 10:50:36 -0400203 this->imageInfo().colorInfo(), this->uniqueID());
Brian Osman6064e1c2018-10-19 14:27:54 -0400204 return adjuster.refTextureProxyForParams(params, scaleAdjust);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400205}
206
207GrBackendTexture SkImage_GpuBase::onGetBackendTexture(bool flushPendingGrContextIO,
208 GrSurfaceOrigin* origin) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500209 auto direct = fContext->priv().asDirectContext();
210 if (!direct) {
211 // This image was created with a DDL context and cannot be instantiated.
Robert Phillips6603a172019-03-05 12:35:44 -0500212 return GrBackendTexture(); // invalid
Robert Phillips920d4882019-03-04 15:16:44 -0500213 }
214
Robert Phillips6603a172019-03-05 12:35:44 -0500215 sk_sp<GrTextureProxy> proxy = this->asTextureProxyRef(direct);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400216 SkASSERT(proxy);
217
Robert Phillips920d4882019-03-04 15:16:44 -0500218 if (!proxy->isInstantiated()) {
219 auto resourceProvider = direct->priv().resourceProvider();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400220
Robert Phillips920d4882019-03-04 15:16:44 -0500221 if (!proxy->instantiate(resourceProvider)) {
222 return GrBackendTexture(); // invalid
223 }
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400224 }
225
226 GrTexture* texture = proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400227 if (texture) {
228 if (flushPendingGrContextIO) {
Greg Daniel4aa13e72019-04-15 14:42:20 -0400229 direct->priv().flushSurface(proxy.get());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400230 }
231 if (origin) {
232 *origin = proxy->origin();
233 }
234 return texture->getBackendTexture();
235 }
236 return GrBackendTexture(); // invalid
237}
238
239GrTexture* SkImage_GpuBase::onGetTexture() const {
240 GrTextureProxy* proxy = this->peekProxy();
Robert Phillips193c4212019-03-04 12:18:53 -0500241 if (proxy && proxy->isInstantiated()) {
242 return proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400243 }
244
Robert Phillips193c4212019-03-04 12:18:53 -0500245 auto direct = fContext->priv().asDirectContext();
246 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400247 // This image was created with a DDL context and cannot be instantiated.
248 return nullptr;
249 }
250
Robert Phillips6603a172019-03-05 12:35:44 -0500251 sk_sp<GrTextureProxy> proxyRef = this->asTextureProxyRef(direct);
Robert Phillips193c4212019-03-04 12:18:53 -0500252 SkASSERT(proxyRef && !proxyRef->isInstantiated());
253
254 if (!proxyRef->instantiate(direct->priv().resourceProvider())) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400255 return nullptr;
256 }
257
Robert Phillips193c4212019-03-04 12:18:53 -0500258 return proxyRef->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400259}
260
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400261bool SkImage_GpuBase::onIsValid(GrContext* context) const {
262 // The base class has already checked that context isn't abandoned (if it's not nullptr)
Robert Phillips920d4882019-03-04 15:16:44 -0500263 if (fContext->priv().abandoned()) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400264 return false;
265 }
266
Robert Phillips920d4882019-03-04 15:16:44 -0500267 if (context && !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400268 return false;
269 }
270
271 return true;
272}
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400273
Jim Van Verth0e671942018-11-09 12:03:57 -0500274bool SkImage_GpuBase::MakeTempTextureProxies(GrContext* ctx, const GrBackendTexture yuvaTextures[],
Jim Van Verth53275362018-11-09 15:42:35 -0500275 int numTextures, const SkYUVAIndex yuvaIndices[4],
276 GrSurfaceOrigin imageOrigin,
Jim Van Verth0e671942018-11-09 12:03:57 -0500277 sk_sp<GrTextureProxy> tempTextureProxies[4]) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500278 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400279 const GrCaps* caps = ctx->priv().caps();
Jim Van Verth0e671942018-11-09 12:03:57 -0500280
Jim Van Verth0e671942018-11-09 12:03:57 -0500281 for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) {
Robert Phillips62221e72019-07-24 15:07:38 -0400282 GrBackendFormat backendFormat = yuvaTextures[textureIndex].getBackendFormat();
Brian Salomonf391d0f2018-12-14 09:18:50 -0500283 if (!backendFormat.isValid()) {
284 return false;
285 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400286
Robert Phillips00c9f0d2019-08-02 17:17:35 -0400287 GrColorType grColorType = caps->getYUVAColorTypeFromBackendFormat(
288 backendFormat,
289 yuvaIndices[3].fIndex == textureIndex);
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400290 if (GrColorType::kUnknown == grColorType) {
291 return false;
292 }
293
Robert Phillips62221e72019-07-24 15:07:38 -0400294 SkASSERT(yuvaTextures[textureIndex].isValid());
Jim Van Verth0e671942018-11-09 12:03:57 -0500295
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500296 tempTextureProxies[textureIndex] = proxyProvider->wrapBackendTexture(
Robert Phillips62221e72019-07-24 15:07:38 -0400297 yuvaTextures[textureIndex], grColorType, imageOrigin, kBorrow_GrWrapOwnership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500298 GrWrapCacheable::kNo, kRead_GrIOType);
Jim Van Verth0e671942018-11-09 12:03:57 -0500299 if (!tempTextureProxies[textureIndex]) {
300 return false;
301 }
Jim Van Verth53275362018-11-09 15:42:35 -0500302
303 // Check that each texture contains the channel data for the corresponding YUVA index
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400304 auto componentFlags = GrColorTypeComponentFlags(grColorType);
Jim Van Verth53275362018-11-09 15:42:35 -0500305 for (int yuvaIndex = 0; yuvaIndex < SkYUVAIndex::kIndexCount; ++yuvaIndex) {
306 if (yuvaIndices[yuvaIndex].fIndex == textureIndex) {
307 switch (yuvaIndices[yuvaIndex].fChannel) {
308 case SkColorChannel::kR:
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400309 // TODO: Chrome needs to be patched before this can be
310 // enforced.
311// if (!(kRed_SkColorTypeComponentFlag & componentFlags)) {
312// return false;
313// }
314 break;
315 case SkColorChannel::kG:
316 if (!(kGreen_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500317 return false;
318 }
319 break;
Jim Van Verth53275362018-11-09 15:42:35 -0500320 case SkColorChannel::kB:
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400321 if (!(kBlue_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500322 return false;
323 }
324 break;
325 case SkColorChannel::kA:
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400326 if (!(kAlpha_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500327 return false;
328 }
329 break;
330 }
331 }
332 }
Jim Van Verth0e671942018-11-09 12:03:57 -0500333 }
334
335 return true;
336}
337
338bool SkImage_GpuBase::RenderYUVAToRGBA(GrContext* ctx, GrRenderTargetContext* renderTargetContext,
339 const SkRect& rect, SkYUVColorSpace yuvColorSpace,
Brian Osmane9560492019-02-05 17:00:03 -0500340 sk_sp<GrColorSpaceXform> colorSpaceXform,
Jim Van Verth0e671942018-11-09 12:03:57 -0500341 const sk_sp<GrTextureProxy> proxies[4],
342 const SkYUVAIndex yuvaIndices[4]) {
343 SkASSERT(renderTargetContext);
344 if (!renderTargetContext->asSurfaceProxy()) {
345 return false;
346 }
347
348 GrPaint paint;
349 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
350
Brian Salomonca6b2f42020-01-24 11:31:21 -0500351 const auto& caps = *ctx->priv().caps();
Brian Osmane9560492019-02-05 17:00:03 -0500352 auto fp = GrYUVtoRGBEffect::Make(proxies, yuvaIndices, yuvColorSpace,
Brian Salomonca6b2f42020-01-24 11:31:21 -0500353 GrSamplerState::Filter::kNearest, caps);
Brian Osmane9560492019-02-05 17:00:03 -0500354 if (colorSpaceXform) {
355 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(colorSpaceXform));
356 }
357 paint.addColorFragmentProcessor(std::move(fp));
Jim Van Verth0e671942018-11-09 12:03:57 -0500358
359 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Jim Van Verth0e671942018-11-09 12:03:57 -0500360 return true;
361}
362
Brian Salomonbe5a0932018-12-10 10:03:26 -0500363sk_sp<GrTextureProxy> SkImage_GpuBase::MakePromiseImageLazyProxy(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400364 GrContext* context, int width, int height, GrSurfaceOrigin origin, GrColorType colorType,
Brian Salomonbe5a0932018-12-10 10:03:26 -0500365 GrBackendFormat backendFormat, GrMipMapped mipMapped,
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500366 PromiseImageTextureFulfillProc fulfillProc,
367 PromiseImageTextureReleaseProc releaseProc,
368 PromiseImageTextureDoneProc doneProc,
Brian Salomon0cc57542019-03-08 13:28:46 -0500369 PromiseImageTextureContext textureContext,
370 PromiseImageApiVersion version) {
Brian Salomonbe5a0932018-12-10 10:03:26 -0500371 SkASSERT(context);
372 SkASSERT(width > 0 && height > 0);
373 SkASSERT(doneProc);
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400374 SkASSERT(colorType != GrColorType::kUnknown);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500375
376 if (!fulfillProc || !releaseProc) {
377 doneProc(textureContext);
378 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400379 }
380
Brian Salomonbe5a0932018-12-10 10:03:26 -0500381 if (mipMapped == GrMipMapped::kYes &&
382 GrTextureTypeHasRestrictedSampling(backendFormat.textureType())) {
383 // It is invalid to have a GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE and have mips as
384 // well.
385 doneProc(textureContext);
386 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400387 }
Brian Salomonbe5a0932018-12-10 10:03:26 -0500388
389 /**
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500390 * This class is the lazy instantiation callback for promise images. It manages calling the
391 * client's Fulfill, Release, and Done procs. It attempts to reuse a GrTexture instance in
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500392 * cases where the client provides the same SkPromiseImageTexture as Fulfill results for
393 * multiple SkImages. The created GrTexture is given a key based on a unique ID associated with
394 * the SkPromiseImageTexture.
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500395 *
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500396 * The GrTexutre idle proc mechanism is used to call the Release and Done procs. We use this
397 * instead of the GrSurface release proc because the GrTexture is cached and therefore may
398 * outlive the proxy into which this callback is installed.
399 *
400 * A key invalidation message is installed on the SkPromiseImageTexture so that the GrTexture
401 * is deleted once it can no longer be used to instantiate a proxy.
Brian Salomonbe5a0932018-12-10 10:03:26 -0500402 */
403 class PromiseLazyInstantiateCallback {
404 public:
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500405 PromiseLazyInstantiateCallback(PromiseImageTextureFulfillProc fulfillProc,
406 PromiseImageTextureReleaseProc releaseProc,
407 PromiseImageTextureDoneProc doneProc,
408 PromiseImageTextureContext context,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400409 GrColorType colorType,
Brian Salomon0cc57542019-03-08 13:28:46 -0500410 PromiseImageApiVersion version)
411 : fFulfillProc(fulfillProc)
412 , fReleaseProc(releaseProc)
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400413 , fColorType(colorType)
Brian Salomon0cc57542019-03-08 13:28:46 -0500414 , fVersion(version) {
Brian Salomone80b8092019-03-08 13:25:19 -0500415 fDoneCallback = sk_make_sp<GrRefCntedCallback>(doneProc, context);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500416 }
417 PromiseLazyInstantiateCallback(PromiseLazyInstantiateCallback&&) = default;
418 PromiseLazyInstantiateCallback(const PromiseLazyInstantiateCallback&) {
419 // Because we get wrapped in std::function we must be copyable. But we should never
420 // be copied.
421 SkASSERT(false);
422 }
423 PromiseLazyInstantiateCallback& operator=(PromiseLazyInstantiateCallback&&) = default;
424 PromiseLazyInstantiateCallback& operator=(const PromiseLazyInstantiateCallback&) {
425 SkASSERT(false);
426 return *this;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500427 }
Brian Salomon553610d2019-01-14 17:34:12 -0500428
Brian Salomon88b8d112019-03-07 15:25:34 +0000429 ~PromiseLazyInstantiateCallback() {
Brian Salomon876a0172019-03-08 11:12:14 -0500430 // Our destructor can run on any thread. We trigger the unref of fTexture by message.
Robert Phillipsddc21482019-10-16 14:30:09 -0400431 // This unreffed texture pointer is a real problem! When the context has been
432 // abandoned, the GrTexture pointed to by this pointer is deleted! Due to virtual
433 // inheritance any manipulation of this pointer at that point will cause a crash.
434 // For now we "work around" the problem by just passing it, untouched, into the
435 // message bus but this very fragile.
436 // In the future the GrSurface class hierarchy refactoring should eliminate this
437 // difficulty by removing the virtual inheritance.
Brian Salomon876a0172019-03-08 11:12:14 -0500438 if (fTexture) {
Robert Phillipsddc21482019-10-16 14:30:09 -0400439 SkMessageBus<GrTextureFreedMessage>::Post({fTexture, fTextureContextID});
Brian Salomon876a0172019-03-08 11:12:14 -0500440 }
Brian Salomon88b8d112019-03-07 15:25:34 +0000441 }
442
Brian Salomonbeb7f522019-08-30 16:19:42 -0400443 GrSurfaceProxy::LazyCallbackResult operator()(GrResourceProvider* resourceProvider) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400444 // We use the unique key in a way that is unrelated to the SkImage-based key that the
445 // proxy may receive, hence kUnsynced.
446 static constexpr auto kKeySyncMode =
447 GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
448
Brian Salomonbeb7f522019-08-30 16:19:42 -0400449 // In order to make the SkImage "thread safe" we rely on holding an extra ref to the
450 // texture in the callback and signalling the unref via a message to the resource cache.
451 // We need to extend the callback's lifetime to that of the proxy.
452 static constexpr auto kReleaseCallbackOnInstantiation = false;
453
Brian Salomon876a0172019-03-08 11:12:14 -0500454 // Our proxy is getting instantiated for the second+ time. We are only allowed to call
455 // Fulfill once. So return our cached result.
456 if (fTexture) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400457 return {sk_ref_sp(fTexture), kReleaseCallbackOnInstantiation, kKeySyncMode};
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400458 } else if (fColorType == GrColorType::kUnknown) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400459 // We've already called fulfill and it failed. Our contract says that we should only
460 // call each callback once.
461 return {};
Brian Salomon876a0172019-03-08 11:12:14 -0500462 }
Brian Salomone80b8092019-03-08 13:25:19 -0500463 SkASSERT(fDoneCallback);
464 PromiseImageTextureContext textureContext = fDoneCallback->context();
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500465 sk_sp<SkPromiseImageTexture> promiseTexture = fFulfillProc(textureContext);
466 // From here on out our contract is that the release proc must be called, even if
467 // the return from fulfill was invalid or we fail for some other reason.
Brian Salomone80b8092019-03-08 13:25:19 -0500468 auto releaseCallback = sk_make_sp<GrRefCntedCallback>(fReleaseProc, textureContext);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500469 if (!promiseTexture) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400470 // This records that we have failed.
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400471 fColorType = GrColorType::kUnknown;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400472 return {};
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500473 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500474
Robert Phillips62221e72019-07-24 15:07:38 -0400475 const GrBackendTexture& backendTexture = promiseTexture->backendTexture();
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500476 if (!backendTexture.isValid()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400477 return {};
Brian Salomon559c6172019-01-10 10:23:44 -0500478 }
479
Brian Salomonf55e8d52019-01-30 17:28:20 -0500480 sk_sp<GrTexture> tex;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500481 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Salomon7d88f312019-02-28 10:03:03 -0500482 GrUniqueKey key;
483 GrUniqueKey::Builder builder(&key, kDomain, 2, "promise");
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500484 builder[0] = promiseTexture->uniqueID();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400485 builder[1] = (uint32_t) fColorType;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500486 builder.finish();
Brian Salomon0d606762019-01-25 09:58:38 -0500487 // A texture with this key may already exist from a different instance of this lazy
488 // callback. This could happen if the client fulfills a promise image with a texture
489 // that was previously used to fulfill a different promise image.
Brian Salomon7d88f312019-02-28 10:03:03 -0500490 if (auto surf = resourceProvider->findByUniqueKey<GrSurface>(key)) {
Brian Salomon0d606762019-01-25 09:58:38 -0500491 tex = sk_ref_sp(surf->asTexture());
492 SkASSERT(tex);
493 } else {
494 if ((tex = resourceProvider->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400495 backendTexture, fColorType, kBorrow_GrWrapOwnership,
496 GrWrapCacheable::kYes, kRead_GrIOType))) {
Brian Salomon7d88f312019-02-28 10:03:03 -0500497 tex->resourcePriv().setUniqueKey(key);
Brian Salomon0d606762019-01-25 09:58:38 -0500498 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400499 return {};
Brian Salomon0d606762019-01-25 09:58:38 -0500500 }
501 }
Brian Salomon0cc57542019-03-08 13:28:46 -0500502 auto releaseIdleState = fVersion == PromiseImageApiVersion::kLegacy
503 ? GrTexture::IdleState::kFinished
504 : GrTexture::IdleState::kFlushed;
505 tex->addIdleProc(std::move(releaseCallback), releaseIdleState);
Brian Salomone80b8092019-03-08 13:25:19 -0500506 tex->addIdleProc(std::move(fDoneCallback), GrTexture::IdleState::kFinished);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500507 promiseTexture->addKeyToInvalidate(tex->getContext()->priv().contextID(), key);
Brian Salomon876a0172019-03-08 11:12:14 -0500508 fTexture = tex.get();
509 // We need to hold on to the GrTexture in case our proxy gets reinstantiated. However,
510 // we can't unref in our destructor because we may be on another thread then. So we
511 // let the cache know it is waiting on an unref message. We will send that message from
512 // our destructor.
513 GrContext* context = fTexture->getContext();
Robert Phillipsddc21482019-10-16 14:30:09 -0400514 context->priv().getResourceCache()->insertDelayedTextureUnref(fTexture);
Brian Salomon876a0172019-03-08 11:12:14 -0500515 fTextureContextID = context->priv().contextID();
Brian Salomonbeb7f522019-08-30 16:19:42 -0400516 return {std::move(tex), kReleaseCallbackOnInstantiation, kKeySyncMode};
Brian Salomonbe5a0932018-12-10 10:03:26 -0500517 }
518
519 private:
Brian Salomone80b8092019-03-08 13:25:19 -0500520 PromiseImageTextureFulfillProc fFulfillProc;
521 PromiseImageTextureReleaseProc fReleaseProc;
522 sk_sp<GrRefCntedCallback> fDoneCallback;
Brian Salomon876a0172019-03-08 11:12:14 -0500523 GrTexture* fTexture = nullptr;
524 uint32_t fTextureContextID = SK_InvalidUniqueID;
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400525 GrColorType fColorType;
Brian Salomon0cc57542019-03-08 13:28:46 -0500526 PromiseImageApiVersion fVersion;
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400527 } callback(fulfillProc, releaseProc, doneProc, textureContext, colorType, version);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500528
Robert Phillips9da87e02019-02-04 13:26:26 -0500529 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonbe5a0932018-12-10 10:03:26 -0500530
531 GrSurfaceDesc desc;
532 desc.fWidth = width;
533 desc.fHeight = height;
Brian Salomonbe5a0932018-12-10 10:03:26 -0500534
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600535 // Ganesh assumes that, when wrapping a mipmapped backend texture from a client, that its
536 // mipmaps are fully fleshed out.
537 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
538 ? GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
539
Greg Danielce3ddaa2020-01-22 16:58:15 -0500540 GrSwizzle readSwizzle = context->priv().caps()->getReadSwizzle(backendFormat, colorType);
541
Brian Salomonbe5a0932018-12-10 10:03:26 -0500542 // We pass kReadOnly here since we should treat content of the client's texture as immutable.
Brian Salomone8a766b2019-07-19 14:24:36 -0400543 // The promise API provides no way for the client to indicated that the texture is protected.
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400544 return proxyProvider->createLazyProxy(
Greg Danielce3ddaa2020-01-22 16:58:15 -0500545 std::move(callback), backendFormat, desc, readSwizzle, GrRenderable::kNo, 1, origin,
546 mipMapped, mipMapsStatus, GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact,
547 SkBudgeted::kNo, GrProtected::kNo, GrSurfaceProxy::UseAllocator::kYes);
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400548}