blob: 563d2905a2fca93f2ca986d203abd31853019dc1 [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
Greg Danielc7672092020-02-06 14:32:54 -05008#include "src/image/SkImage_GpuBase.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkPromiseImageTexture.h"
11#include "include/gpu/GrBackendSurface.h"
12#include "include/gpu/GrContext.h"
13#include "include/gpu/GrTexture.h"
14#include "include/private/GrRecordingContext.h"
15#include "src/core/SkBitmapCache.h"
16#include "src/core/SkTLList.h"
17#include "src/gpu/GrClip.h"
18#include "src/gpu/GrContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040019#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrRecordingContextPriv.h"
21#include "src/gpu/GrRenderTargetContext.h"
22#include "src/gpu/GrTextureAdjuster.h"
23#include "src/gpu/effects/GrYUVtoRGBEffect.h"
24#include "src/image/SkImage_Gpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/image/SkReadPixelsRec.h"
Jim Van Verth8026ccc2018-10-04 13:10:39 -040026
Brian Salomon9f2b86c2019-10-22 10:37:46 -040027SkImage_GpuBase::SkImage_GpuBase(sk_sp<GrContext> context, SkISize size, uint32_t uniqueID,
Brian Salomon5ad6fd32019-03-21 15:30:08 -040028 SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs)
Brian Salomon9f2b86c2019-10-22 10:37:46 -040029 : INHERITED(SkImageInfo::Make(size, ct, at, std::move(cs)), uniqueID)
Brian Salomon5ad6fd32019-03-21 15:30:08 -040030 , fContext(std::move(context)) {}
Jim Van Verth8026ccc2018-10-04 13:10:39 -040031
Jim Van Verth8026ccc2018-10-04 13:10:39 -040032//////////////////////////////////////////////////////////////////////////////////////////////////
33
Robert Phillipsfd0d9702019-02-01 10:19:42 -050034#if GR_TEST_UTILS
35void SkImage_GpuBase::resetContext(sk_sp<GrContext> newContext) {
Robert Phillipsfe0963c2019-02-07 13:25:07 -050036 SkASSERT(fContext->priv().matches(newContext.get()));
Robert Phillipsfd0d9702019-02-01 10:19:42 -050037 fContext = newContext;
38}
39#endif
40
Robert Phillips62221e72019-07-24 15:07:38 -040041bool SkImage_GpuBase::ValidateBackendTexture(const GrCaps* caps, const GrBackendTexture& tex,
42 GrColorType grCT, SkColorType ct, SkAlphaType at,
Jim Van Verth8026ccc2018-10-04 13:10:39 -040043 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 }
Greg Daniele877dce2019-07-11 10:52:43 -040057
Robert Phillips62221e72019-07-24 15:07:38 -040058 return caps->areColorTypeAndFormatCompatible(grCT, backendFormat);
Jim Van Verth8026ccc2018-10-04 13:10:39 -040059}
60
Robert Phillipsead321b2019-12-19 10:16:32 -050061bool SkImage_GpuBase::ValidateCompressedBackendTexture(const GrCaps* caps,
62 const GrBackendTexture& tex,
63 SkAlphaType at) {
Robert Phillipsead321b2019-12-19 10:16:32 -050064 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 Daniel37c127f2020-02-05 10:37:27 -0500117 const GrSurfaceProxyView* view = this->view(direct);
118 SkASSERT(view);
Greg Danielc7672092020-02-06 14:32:54 -0500119 GrColorType grColorType = SkColorTypeAndFormatToGrColorType(
120 fContext->priv().caps(), this->colorType(), view->proxy()->backendFormat());
Greg Danielba88ab62019-07-26 09:14:01 -0400121
Greg Daniel37c127f2020-02-05 10:37:27 -0500122 auto sContext = GrSurfaceContext::Make(direct, *view, grColorType, this->alphaType(),
123 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
Greg Daniel37c127f2020-02-05 10:37:27 -0500145 const GrSurfaceProxyView* view = this->view(context);
146 SkASSERT(view && view->proxy());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400147
Greg Daniel3155f7f2020-01-16 16:54:26 -0500148 GrColorType grColorType = SkColorTypeToGrColorType(this->colorType());
149
Greg Danielc7672092020-02-06 14:32:54 -0500150 GrSurfaceProxyView copyView = GrSurfaceProxy::Copy(
151 context, view->proxy(), view->origin(), grColorType, GrMipMapped::kNo, subset,
152 SkBackingFit::kExact, view->proxy()->isBudgeted());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400153
Greg Daniel40903af2020-01-30 14:55:05 -0500154 if (!copyView.proxy()) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400155 return nullptr;
156 }
157
158 // MDB: this call is okay bc we know 'sContext' was kExact
Greg Daniel40903af2020-01-30 14:55:05 -0500159 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, std::move(copyView),
Greg Daniel7c165a42020-01-22 12:22:36 -0500160 this->colorType(), this->alphaType(), this->refColorSpace());
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400161}
162
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400163bool SkImage_GpuBase::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
164 int srcX, int srcY, CachingHint) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500165 auto direct = fContext->priv().asDirectContext();
166 if (!direct) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400167 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
168 return false;
169 }
170
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400171 if (!SkImageInfoValidConversion(dstInfo, this->imageInfo())) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400172 return false;
173 }
174
Greg Daniel37c127f2020-02-05 10:37:27 -0500175 const GrSurfaceProxyView* view = this->view(direct);
176 SkASSERT(view);
Greg Danielc7672092020-02-06 14:32:54 -0500177 GrColorType grColorType = SkColorTypeAndFormatToGrColorType(
178 fContext->priv().caps(), this->colorType(), view->proxy()->backendFormat());
Greg Danielba88ab62019-07-26 09:14:01 -0400179
Greg Daniel37c127f2020-02-05 10:37:27 -0500180 auto sContext = GrSurfaceContext::Make(direct, *view, grColorType, this->alphaType(),
181 this->refColorSpace());
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400182 if (!sContext) {
183 return false;
184 }
185
Brian Salomon1d435302019-07-01 13:05:28 -0400186 return sContext->readPixels(dstInfo, dstPixels, dstRB, {srcX, srcY});
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400187}
188
Greg Danielfebdedf2020-02-05 17:06:27 -0500189GrSurfaceProxyView SkImage_GpuBase::refView(GrRecordingContext* context, GrSamplerState params,
190 SkScalar scaleAdjust[2]) const {
Robert Phillips6603a172019-03-05 12:35:44 -0500191 if (!context || !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400192 SkASSERT(0);
Greg Danielfebdedf2020-02-05 17:06:27 -0500193 return {};
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400194 }
195
Greg Daniel37c127f2020-02-05 10:37:27 -0500196 GrTextureAdjuster adjuster(fContext.get(), *this->view(context), this->imageInfo().colorInfo(),
197 this->uniqueID());
Greg Danielfebdedf2020-02-05 17:06:27 -0500198 return adjuster.viewForParams(params, scaleAdjust);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400199}
200
201GrBackendTexture SkImage_GpuBase::onGetBackendTexture(bool flushPendingGrContextIO,
202 GrSurfaceOrigin* origin) const {
Robert Phillips920d4882019-03-04 15:16:44 -0500203 auto direct = fContext->priv().asDirectContext();
204 if (!direct) {
205 // This image was created with a DDL context and cannot be instantiated.
Greg Danielc7672092020-02-06 14:32:54 -0500206 return GrBackendTexture(); // invalid
Robert Phillips920d4882019-03-04 15:16:44 -0500207 }
208
Greg Danielfebdedf2020-02-05 17:06:27 -0500209 const GrSurfaceProxyView* view = this->view(direct);
210 SkASSERT(view && *view);
211 GrSurfaceProxy* proxy = view->proxy();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400212
Robert Phillips920d4882019-03-04 15:16:44 -0500213 if (!proxy->isInstantiated()) {
214 auto resourceProvider = direct->priv().resourceProvider();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400215
Robert Phillips920d4882019-03-04 15:16:44 -0500216 if (!proxy->instantiate(resourceProvider)) {
Greg Danielc7672092020-02-06 14:32:54 -0500217 return GrBackendTexture(); // invalid
Robert Phillips920d4882019-03-04 15:16:44 -0500218 }
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400219 }
220
221 GrTexture* texture = proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400222 if (texture) {
223 if (flushPendingGrContextIO) {
Greg Daniel55f040b2020-02-13 15:38:32 +0000224 direct->priv().flushSurface(proxy);
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400225 }
226 if (origin) {
227 *origin = proxy->origin();
228 }
229 return texture->getBackendTexture();
230 }
Greg Danielc7672092020-02-06 14:32:54 -0500231 return GrBackendTexture(); // invalid
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400232}
233
234GrTexture* SkImage_GpuBase::onGetTexture() const {
235 GrTextureProxy* proxy = this->peekProxy();
Robert Phillips193c4212019-03-04 12:18:53 -0500236 if (proxy && proxy->isInstantiated()) {
237 return proxy->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400238 }
239
Robert Phillips193c4212019-03-04 12:18:53 -0500240 auto direct = fContext->priv().asDirectContext();
241 if (!direct) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400242 // This image was created with a DDL context and cannot be instantiated.
243 return nullptr;
244 }
245
Greg Danielfebdedf2020-02-05 17:06:27 -0500246 const GrSurfaceProxyView* view = this->view(direct);
247 SkASSERT(view && *view && !view->proxy()->isInstantiated());
Robert Phillips193c4212019-03-04 12:18:53 -0500248
Greg Danielfebdedf2020-02-05 17:06:27 -0500249 if (!view->proxy()->instantiate(direct->priv().resourceProvider())) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400250 return nullptr;
251 }
252
Greg Danielfebdedf2020-02-05 17:06:27 -0500253 return view->proxy()->peekTexture();
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400254}
255
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400256bool SkImage_GpuBase::onIsValid(GrContext* context) const {
257 // The base class has already checked that context isn't abandoned (if it's not nullptr)
Robert Phillips920d4882019-03-04 15:16:44 -0500258 if (fContext->priv().abandoned()) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400259 return false;
260 }
261
Robert Phillips920d4882019-03-04 15:16:44 -0500262 if (context && !fContext->priv().matches(context)) {
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400263 return false;
264 }
265
266 return true;
267}
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400268
Jim Van Verth0e671942018-11-09 12:03:57 -0500269bool SkImage_GpuBase::MakeTempTextureProxies(GrContext* ctx, const GrBackendTexture yuvaTextures[],
Jim Van Verth53275362018-11-09 15:42:35 -0500270 int numTextures, const SkYUVAIndex yuvaIndices[4],
271 GrSurfaceOrigin imageOrigin,
Greg Danielc7672092020-02-06 14:32:54 -0500272 GrSurfaceProxyView tempViews[4]) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500273 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400274 const GrCaps* caps = ctx->priv().caps();
Jim Van Verth0e671942018-11-09 12:03:57 -0500275
Jim Van Verth0e671942018-11-09 12:03:57 -0500276 for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) {
Robert Phillips62221e72019-07-24 15:07:38 -0400277 GrBackendFormat backendFormat = yuvaTextures[textureIndex].getBackendFormat();
Brian Salomonf391d0f2018-12-14 09:18:50 -0500278 if (!backendFormat.isValid()) {
279 return false;
280 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400281
Robert Phillips00c9f0d2019-08-02 17:17:35 -0400282 GrColorType grColorType = caps->getYUVAColorTypeFromBackendFormat(
Greg Danielc7672092020-02-06 14:32:54 -0500283 backendFormat, yuvaIndices[3].fIndex == textureIndex);
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400284 if (GrColorType::kUnknown == grColorType) {
285 return false;
286 }
287
Robert Phillips62221e72019-07-24 15:07:38 -0400288 SkASSERT(yuvaTextures[textureIndex].isValid());
Jim Van Verth0e671942018-11-09 12:03:57 -0500289
Greg Danielc7672092020-02-06 14:32:54 -0500290 auto proxy = proxyProvider->wrapBackendTexture(yuvaTextures[textureIndex], grColorType,
291 imageOrigin, kBorrow_GrWrapOwnership,
292 GrWrapCacheable::kNo, kRead_GrIOType);
293 if (!proxy) {
Jim Van Verth0e671942018-11-09 12:03:57 -0500294 return false;
295 }
Greg Danielc7672092020-02-06 14:32:54 -0500296 GrSwizzle swizzle = caps->getReadSwizzle(proxy->backendFormat(), grColorType);
297 tempViews[textureIndex] = GrSurfaceProxyView(std::move(proxy), imageOrigin, swizzle);
Jim Van Verth53275362018-11-09 15:42:35 -0500298
299 // Check that each texture contains the channel data for the corresponding YUVA index
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400300 auto componentFlags = GrColorTypeComponentFlags(grColorType);
Jim Van Verth53275362018-11-09 15:42:35 -0500301 for (int yuvaIndex = 0; yuvaIndex < SkYUVAIndex::kIndexCount; ++yuvaIndex) {
302 if (yuvaIndices[yuvaIndex].fIndex == textureIndex) {
303 switch (yuvaIndices[yuvaIndex].fChannel) {
304 case SkColorChannel::kR:
Greg Danielc7672092020-02-06 14:32:54 -0500305 // TODO: Chrome needs to be patched before this can be
306 // enforced.
307 // if (!(kRed_SkColorTypeComponentFlag & componentFlags)) {
308 // return false;
309 // }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400310 break;
311 case SkColorChannel::kG:
312 if (!(kGreen_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500313 return false;
314 }
315 break;
Jim Van Verth53275362018-11-09 15:42:35 -0500316 case SkColorChannel::kB:
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400317 if (!(kBlue_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500318 return false;
319 }
320 break;
321 case SkColorChannel::kA:
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400322 if (!(kAlpha_SkColorTypeComponentFlag & componentFlags)) {
Jim Van Verth53275362018-11-09 15:42:35 -0500323 return false;
324 }
325 break;
326 }
327 }
328 }
Jim Van Verth0e671942018-11-09 12:03:57 -0500329 }
330
331 return true;
332}
333
334bool SkImage_GpuBase::RenderYUVAToRGBA(GrContext* ctx, GrRenderTargetContext* renderTargetContext,
335 const SkRect& rect, SkYUVColorSpace yuvColorSpace,
Brian Osmane9560492019-02-05 17:00:03 -0500336 sk_sp<GrColorSpaceXform> colorSpaceXform,
Greg Danielc7672092020-02-06 14:32:54 -0500337 GrSurfaceProxyView views[4],
Jim Van Verth0e671942018-11-09 12:03:57 -0500338 const SkYUVAIndex yuvaIndices[4]) {
339 SkASSERT(renderTargetContext);
340 if (!renderTargetContext->asSurfaceProxy()) {
341 return false;
342 }
343
344 GrPaint paint;
345 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
346
Brian Salomonca6b2f42020-01-24 11:31:21 -0500347 const auto& caps = *ctx->priv().caps();
Greg Danielc7672092020-02-06 14:32:54 -0500348 auto fp = GrYUVtoRGBEffect::Make(views, yuvaIndices, yuvColorSpace,
Brian Salomonca6b2f42020-01-24 11:31:21 -0500349 GrSamplerState::Filter::kNearest, caps);
Brian Osmane9560492019-02-05 17:00:03 -0500350 if (colorSpaceXform) {
351 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(colorSpaceXform));
352 }
353 paint.addColorFragmentProcessor(std::move(fp));
Jim Van Verth0e671942018-11-09 12:03:57 -0500354
355 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Jim Van Verth0e671942018-11-09 12:03:57 -0500356 return true;
357}
358
Brian Salomonbe5a0932018-12-10 10:03:26 -0500359sk_sp<GrTextureProxy> SkImage_GpuBase::MakePromiseImageLazyProxy(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400360 GrContext* context, int width, int height, GrSurfaceOrigin origin, GrColorType colorType,
Brian Salomonbe5a0932018-12-10 10:03:26 -0500361 GrBackendFormat backendFormat, GrMipMapped mipMapped,
Greg Danielc7672092020-02-06 14:32:54 -0500362 PromiseImageTextureFulfillProc fulfillProc, PromiseImageTextureReleaseProc releaseProc,
363 PromiseImageTextureDoneProc doneProc, PromiseImageTextureContext textureContext,
Brian Salomon0cc57542019-03-08 13:28:46 -0500364 PromiseImageApiVersion version) {
Brian Salomonbe5a0932018-12-10 10:03:26 -0500365 SkASSERT(context);
366 SkASSERT(width > 0 && height > 0);
367 SkASSERT(doneProc);
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400368 SkASSERT(colorType != GrColorType::kUnknown);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500369
370 if (!fulfillProc || !releaseProc) {
371 doneProc(textureContext);
372 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400373 }
374
Brian Salomonbe5a0932018-12-10 10:03:26 -0500375 if (mipMapped == GrMipMapped::kYes &&
376 GrTextureTypeHasRestrictedSampling(backendFormat.textureType())) {
377 // It is invalid to have a GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE and have mips as
378 // well.
379 doneProc(textureContext);
380 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400381 }
Brian Salomonbe5a0932018-12-10 10:03:26 -0500382
383 /**
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500384 * This class is the lazy instantiation callback for promise images. It manages calling the
385 * client's Fulfill, Release, and Done procs. It attempts to reuse a GrTexture instance in
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500386 * cases where the client provides the same SkPromiseImageTexture as Fulfill results for
387 * multiple SkImages. The created GrTexture is given a key based on a unique ID associated with
388 * the SkPromiseImageTexture.
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500389 *
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500390 * The GrTexutre idle proc mechanism is used to call the Release and Done procs. We use this
391 * instead of the GrSurface release proc because the GrTexture is cached and therefore may
392 * outlive the proxy into which this callback is installed.
393 *
394 * A key invalidation message is installed on the SkPromiseImageTexture so that the GrTexture
395 * is deleted once it can no longer be used to instantiate a proxy.
Brian Salomonbe5a0932018-12-10 10:03:26 -0500396 */
397 class PromiseLazyInstantiateCallback {
398 public:
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500399 PromiseLazyInstantiateCallback(PromiseImageTextureFulfillProc fulfillProc,
400 PromiseImageTextureReleaseProc releaseProc,
401 PromiseImageTextureDoneProc doneProc,
402 PromiseImageTextureContext context,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400403 GrColorType colorType,
Brian Salomon0cc57542019-03-08 13:28:46 -0500404 PromiseImageApiVersion version)
405 : fFulfillProc(fulfillProc)
406 , fReleaseProc(releaseProc)
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400407 , fColorType(colorType)
Brian Salomon0cc57542019-03-08 13:28:46 -0500408 , fVersion(version) {
Brian Salomone80b8092019-03-08 13:25:19 -0500409 fDoneCallback = sk_make_sp<GrRefCntedCallback>(doneProc, context);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500410 }
411 PromiseLazyInstantiateCallback(PromiseLazyInstantiateCallback&&) = default;
412 PromiseLazyInstantiateCallback(const PromiseLazyInstantiateCallback&) {
413 // Because we get wrapped in std::function we must be copyable. But we should never
414 // be copied.
415 SkASSERT(false);
416 }
417 PromiseLazyInstantiateCallback& operator=(PromiseLazyInstantiateCallback&&) = default;
418 PromiseLazyInstantiateCallback& operator=(const PromiseLazyInstantiateCallback&) {
419 SkASSERT(false);
420 return *this;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500421 }
Brian Salomon553610d2019-01-14 17:34:12 -0500422
Brian Salomon88b8d112019-03-07 15:25:34 +0000423 ~PromiseLazyInstantiateCallback() {
Brian Salomon876a0172019-03-08 11:12:14 -0500424 // Our destructor can run on any thread. We trigger the unref of fTexture by message.
Robert Phillipsddc21482019-10-16 14:30:09 -0400425 // This unreffed texture pointer is a real problem! When the context has been
426 // abandoned, the GrTexture pointed to by this pointer is deleted! Due to virtual
427 // inheritance any manipulation of this pointer at that point will cause a crash.
428 // For now we "work around" the problem by just passing it, untouched, into the
429 // message bus but this very fragile.
430 // In the future the GrSurface class hierarchy refactoring should eliminate this
431 // difficulty by removing the virtual inheritance.
Brian Salomon876a0172019-03-08 11:12:14 -0500432 if (fTexture) {
Robert Phillipsddc21482019-10-16 14:30:09 -0400433 SkMessageBus<GrTextureFreedMessage>::Post({fTexture, fTextureContextID});
Brian Salomon876a0172019-03-08 11:12:14 -0500434 }
Brian Salomon88b8d112019-03-07 15:25:34 +0000435 }
436
Brian Salomonbeb7f522019-08-30 16:19:42 -0400437 GrSurfaceProxy::LazyCallbackResult operator()(GrResourceProvider* resourceProvider) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400438 // We use the unique key in a way that is unrelated to the SkImage-based key that the
439 // proxy may receive, hence kUnsynced.
440 static constexpr auto kKeySyncMode =
441 GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
442
Brian Salomonbeb7f522019-08-30 16:19:42 -0400443 // In order to make the SkImage "thread safe" we rely on holding an extra ref to the
444 // texture in the callback and signalling the unref via a message to the resource cache.
445 // We need to extend the callback's lifetime to that of the proxy.
446 static constexpr auto kReleaseCallbackOnInstantiation = false;
447
Brian Salomon876a0172019-03-08 11:12:14 -0500448 // Our proxy is getting instantiated for the second+ time. We are only allowed to call
449 // Fulfill once. So return our cached result.
450 if (fTexture) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400451 return {sk_ref_sp(fTexture), kReleaseCallbackOnInstantiation, kKeySyncMode};
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400452 } else if (fColorType == GrColorType::kUnknown) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400453 // We've already called fulfill and it failed. Our contract says that we should only
454 // call each callback once.
455 return {};
Brian Salomon876a0172019-03-08 11:12:14 -0500456 }
Brian Salomone80b8092019-03-08 13:25:19 -0500457 SkASSERT(fDoneCallback);
458 PromiseImageTextureContext textureContext = fDoneCallback->context();
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500459 sk_sp<SkPromiseImageTexture> promiseTexture = fFulfillProc(textureContext);
460 // From here on out our contract is that the release proc must be called, even if
461 // the return from fulfill was invalid or we fail for some other reason.
Brian Salomone80b8092019-03-08 13:25:19 -0500462 auto releaseCallback = sk_make_sp<GrRefCntedCallback>(fReleaseProc, textureContext);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500463 if (!promiseTexture) {
Brian Salomond538d3d2019-04-04 12:18:17 -0400464 // This records that we have failed.
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400465 fColorType = GrColorType::kUnknown;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400466 return {};
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500467 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500468
Robert Phillips62221e72019-07-24 15:07:38 -0400469 const GrBackendTexture& backendTexture = promiseTexture->backendTexture();
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500470 if (!backendTexture.isValid()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400471 return {};
Brian Salomon559c6172019-01-10 10:23:44 -0500472 }
473
Brian Salomonf55e8d52019-01-30 17:28:20 -0500474 sk_sp<GrTexture> tex;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500475 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Salomon7d88f312019-02-28 10:03:03 -0500476 GrUniqueKey key;
477 GrUniqueKey::Builder builder(&key, kDomain, 2, "promise");
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500478 builder[0] = promiseTexture->uniqueID();
Greg Danielc7672092020-02-06 14:32:54 -0500479 builder[1] = (uint32_t)fColorType;
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500480 builder.finish();
Brian Salomon0d606762019-01-25 09:58:38 -0500481 // A texture with this key may already exist from a different instance of this lazy
482 // callback. This could happen if the client fulfills a promise image with a texture
483 // that was previously used to fulfill a different promise image.
Brian Salomon7d88f312019-02-28 10:03:03 -0500484 if (auto surf = resourceProvider->findByUniqueKey<GrSurface>(key)) {
Brian Salomon0d606762019-01-25 09:58:38 -0500485 tex = sk_ref_sp(surf->asTexture());
486 SkASSERT(tex);
487 } else {
488 if ((tex = resourceProvider->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400489 backendTexture, fColorType, kBorrow_GrWrapOwnership,
490 GrWrapCacheable::kYes, kRead_GrIOType))) {
Brian Salomon7d88f312019-02-28 10:03:03 -0500491 tex->resourcePriv().setUniqueKey(key);
Brian Salomon0d606762019-01-25 09:58:38 -0500492 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400493 return {};
Brian Salomon0d606762019-01-25 09:58:38 -0500494 }
495 }
Brian Salomon0cc57542019-03-08 13:28:46 -0500496 auto releaseIdleState = fVersion == PromiseImageApiVersion::kLegacy
497 ? GrTexture::IdleState::kFinished
498 : GrTexture::IdleState::kFlushed;
499 tex->addIdleProc(std::move(releaseCallback), releaseIdleState);
Brian Salomone80b8092019-03-08 13:25:19 -0500500 tex->addIdleProc(std::move(fDoneCallback), GrTexture::IdleState::kFinished);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500501 promiseTexture->addKeyToInvalidate(tex->getContext()->priv().contextID(), key);
Brian Salomon876a0172019-03-08 11:12:14 -0500502 fTexture = tex.get();
503 // We need to hold on to the GrTexture in case our proxy gets reinstantiated. However,
504 // we can't unref in our destructor because we may be on another thread then. So we
505 // let the cache know it is waiting on an unref message. We will send that message from
506 // our destructor.
507 GrContext* context = fTexture->getContext();
Robert Phillipsddc21482019-10-16 14:30:09 -0400508 context->priv().getResourceCache()->insertDelayedTextureUnref(fTexture);
Brian Salomon876a0172019-03-08 11:12:14 -0500509 fTextureContextID = context->priv().contextID();
Brian Salomonbeb7f522019-08-30 16:19:42 -0400510 return {std::move(tex), kReleaseCallbackOnInstantiation, kKeySyncMode};
Brian Salomonbe5a0932018-12-10 10:03:26 -0500511 }
512
513 private:
Brian Salomone80b8092019-03-08 13:25:19 -0500514 PromiseImageTextureFulfillProc fFulfillProc;
515 PromiseImageTextureReleaseProc fReleaseProc;
516 sk_sp<GrRefCntedCallback> fDoneCallback;
Brian Salomon876a0172019-03-08 11:12:14 -0500517 GrTexture* fTexture = nullptr;
518 uint32_t fTextureContextID = SK_InvalidUniqueID;
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400519 GrColorType fColorType;
Brian Salomon0cc57542019-03-08 13:28:46 -0500520 PromiseImageApiVersion fVersion;
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400521 } callback(fulfillProc, releaseProc, doneProc, textureContext, colorType, version);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500522
Robert Phillips9da87e02019-02-04 13:26:26 -0500523 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonbe5a0932018-12-10 10:03:26 -0500524
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600525 // Ganesh assumes that, when wrapping a mipmapped backend texture from a client, that its
526 // mipmaps are fully fleshed out.
527 GrMipMapsStatus mipMapsStatus = (GrMipMapped::kYes == mipMapped)
528 ? GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
529
Greg Danielce3ddaa2020-01-22 16:58:15 -0500530 GrSwizzle readSwizzle = context->priv().caps()->getReadSwizzle(backendFormat, colorType);
531
Brian Salomonbe5a0932018-12-10 10:03:26 -0500532 // We pass kReadOnly here since we should treat content of the client's texture as immutable.
Brian Salomone8a766b2019-07-19 14:24:36 -0400533 // The promise API provides no way for the client to indicated that the texture is protected.
Brian Salomona56a7462020-02-07 14:17:25 -0500534 return proxyProvider->createLazyProxy(std::move(callback), backendFormat, {width, height},
535 readSwizzle, GrRenderable::kNo, 1, origin, mipMapped,
536 mipMapsStatus, GrInternalSurfaceFlags::kReadOnly,
537 SkBackingFit::kExact, SkBudgeted::kNo, GrProtected::kNo,
538 GrSurfaceProxy::UseAllocator::kYes);
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400539}