blob: 44e25dbe3d3251a5075a94ee20594bc4277358f4 [file] [log] [blame]
Brian Osman45580d32016-11-23 09:37:01 -05001/*
2 * Copyright 2016 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 Daniel46cfbc62019-06-07 11:43:30 -04008#include "src/gpu/GrSurfaceContext.h"
9
John Stilesfbd050b2020-08-03 13:21:46 -040010#include <memory>
11
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040012#include "include/gpu/GrDirectContext.h"
13#include "include/gpu/GrRecordingContext.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040014#include "src/core/SkAutoPixmapStorage.h"
Brian Salomon2c673402021-04-02 14:36:58 -040015#include "src/core/SkMipmap.h"
Brian Salomon63a0a752020-06-26 13:32:09 -040016#include "src/core/SkYUVMath.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040017#include "src/gpu/GrAuditTrail.h"
Robert Phillips5e1aa8b2021-07-09 09:10:02 -040018#include "src/gpu/GrClientMappedBufferManager.h"
Brian Salomon1c86b632020-12-11 12:36:01 -050019#include "src/gpu/GrColorSpaceXform.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040020#include "src/gpu/GrDataUtils.h"
Adlai Hollera0693042020-10-14 11:23:11 -040021#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrDrawingManager.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040023#include "src/gpu/GrGpu.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040024#include "src/gpu/GrImageInfo.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040025#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrRecordingContextPriv.h"
Robert Phillips1a82a4e2021-07-01 10:27:44 -040027#include "src/gpu/GrResourceProvider.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050028#include "src/gpu/GrSurfaceDrawContext.h"
Brian Salomonbacbb922021-01-21 19:48:00 -050029#include "src/gpu/GrSurfaceFillContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/SkGr.h"
Brian Salomone9ad9982019-07-22 16:17:41 -040031#include "src/gpu/effects/GrBicubicEffect.h"
Robert Phillips550de7f2021-07-06 16:28:52 -040032#include "src/gpu/effects/GrTextureEffect.h"
Brian Osman45580d32016-11-23 09:37:01 -050033
Brian Salomond63638b2021-03-05 14:00:07 -050034#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
35#define RETURN_FALSE_IF_ABANDONED if (this->fContext->abandoned()) { return false; }
36#define RETURN_NULLPTR_IF_ABANDONED if (this->fContext->abandoned()) { return nullptr; }
Brian Osman45580d32016-11-23 09:37:01 -050037
Greg Danielbfa19c42019-12-19 16:41:40 -050038std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -050039 GrSurfaceProxyView readView,
Brian Salomon14f99fc2020-12-07 12:19:47 -050040 const GrColorInfo& info) {
Greg Daniele20fcad2020-01-08 11:52:34 -050041 // It is probably not necessary to check if the context is abandoned here since uses of the
42 // GrSurfaceContext which need the context will mostly likely fail later on without an issue.
43 // However having this hear adds some reassurance in case there is a path doesn't handle an
44 // abandoned context correctly. It also lets us early out of some extra work.
Robert Phillips9eb00022020-06-30 15:30:12 -040045 if (context->abandoned()) {
Greg Daniele20fcad2020-01-08 11:52:34 -050046 return nullptr;
47 }
Greg Daniel3912a4b2020-01-14 09:56:04 -050048 GrSurfaceProxy* proxy = readView.proxy();
Greg Danielbfa19c42019-12-19 16:41:40 -050049 SkASSERT(proxy && proxy->asTextureProxy());
50
Greg Danielbfa19c42019-12-19 16:41:40 -050051 std::unique_ptr<GrSurfaceContext> surfaceContext;
Greg Daniel3912a4b2020-01-14 09:56:04 -050052 if (proxy->asRenderTargetProxy()) {
Brian Salomon8afde5f2020-04-01 16:22:00 -040053 // Will we ever want a swizzle that is not the default write swizzle for the format and
Greg Danielbfa19c42019-12-19 16:41:40 -050054 // colorType here? If so we will need to manually pass that in.
Brian Salomonc5243782020-04-02 12:50:34 -040055 GrSwizzle writeSwizzle;
Brian Salomon14f99fc2020-12-07 12:19:47 -050056 if (info.colorType() != GrColorType::kUnknown) {
57 writeSwizzle = context->priv().caps()->getWriteSwizzle(proxy->backendFormat(),
58 info.colorType());
Brian Salomonc5243782020-04-02 12:50:34 -040059 }
Brian Salomon8afde5f2020-04-01 16:22:00 -040060 GrSurfaceProxyView writeView(readView.refProxy(), readView.origin(), writeSwizzle);
Brian Salomon590f5672020-12-16 11:44:47 -050061 if (info.alphaType() == kPremul_SkAlphaType || info.alphaType() == kOpaque_SkAlphaType) {
62 surfaceContext = std::make_unique<GrSurfaceDrawContext>(context,
63 std::move(readView),
64 std::move(writeView),
65 info.colorType(),
66 info.refColorSpace(),
Chris Daltonf5b87f92021-04-19 17:27:09 -060067 SkSurfaceProps());
Brian Salomon590f5672020-12-16 11:44:47 -050068 } else {
69 surfaceContext = std::make_unique<GrSurfaceFillContext>(context,
70 std::move(readView),
71 std::move(writeView),
72 info);
73 }
Greg Danielbfa19c42019-12-19 16:41:40 -050074 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -050075 surfaceContext = std::make_unique<GrSurfaceContext>(context, std::move(readView), info);
Greg Danielbfa19c42019-12-19 16:41:40 -050076 }
Robert Phillips07f0e412020-01-17 15:20:00 -050077 SkDEBUGCODE(surfaceContext->validate();)
Greg Danielbfa19c42019-12-19 16:41:40 -050078 return surfaceContext;
79}
80
Brian Salomona56a7462020-02-07 14:17:25 -050081std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
Brian Salomon14f99fc2020-12-07 12:19:47 -050082 const GrImageInfo& info,
Brian Salomona56a7462020-02-07 14:17:25 -050083 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050084 SkBackingFit fit,
Brian Salomon14f99fc2020-12-07 12:19:47 -050085 GrSurfaceOrigin origin,
86 GrRenderable renderable,
87 int sampleCount,
88 GrMipmapped mipmapped,
89 GrProtected isProtected,
Brian Salomona56a7462020-02-07 14:17:25 -050090 SkBudgeted budgeted) {
Brian Salomon14f99fc2020-12-07 12:19:47 -050091 SkASSERT(context);
92 SkASSERT(renderable == GrRenderable::kYes || sampleCount == 1);
93 if (context->abandoned()) {
94 return nullptr;
Brian Salomond005b692020-04-01 15:47:05 -040095 }
Brian Salomon14f99fc2020-12-07 12:19:47 -050096 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(format,
97 info.dimensions(),
98 renderable,
99 sampleCount,
100 mipmapped,
101 fit,
102 budgeted,
103 isProtected);
Greg Danielbfa19c42019-12-19 16:41:40 -0500104 if (!proxy) {
105 return nullptr;
106 }
107
Brian Salomon14f99fc2020-12-07 12:19:47 -0500108 GrSwizzle swizzle;
109 if (info.colorType() != GrColorType::kUnknown &&
110 !context->priv().caps()->isFormatCompressed(format)) {
111 swizzle = context->priv().caps()->getReadSwizzle(format, info.colorType());
112 }
113
Greg Daniel3912a4b2020-01-14 09:56:04 -0500114 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500115 return GrSurfaceContext::Make(context, std::move(view), info.colorInfo());
116}
117
118std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
119 const GrImageInfo& info,
120 SkBackingFit fit,
121 GrSurfaceOrigin origin,
122 GrRenderable renderable,
123 int sampleCount,
124 GrMipmapped mipmapped,
125 GrProtected isProtected,
126 SkBudgeted budgeted) {
127 GrBackendFormat format = context->priv().caps()->getDefaultBackendFormat(info.colorType(),
128 renderable);
129 return Make(context,
130 info,
131 format,
132 fit,
133 origin,
134 renderable,
135 sampleCount,
136 mipmapped,
137 isProtected,
138 budgeted);
Greg Danielbfa19c42019-12-19 16:41:40 -0500139}
140
Robert Phillips69893702019-02-22 11:16:30 -0500141GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500142 GrSurfaceProxyView readView,
Brian Salomon14f99fc2020-12-07 12:19:47 -0500143 const GrColorInfo& info)
144 : fContext(context), fReadView(std::move(readView)), fColorInfo(info) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400145 SkASSERT(!context->abandoned());
Greg Daniele20fcad2020-01-08 11:52:34 -0500146}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400147
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400148const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); }
149
Robert Phillips0d075de2019-03-04 11:08:13 -0500150GrAuditTrail* GrSurfaceContext::auditTrail() {
151 return fContext->priv().auditTrail();
152}
153
154GrDrawingManager* GrSurfaceContext::drawingManager() {
155 return fContext->priv().drawingManager();
156}
157
158const GrDrawingManager* GrSurfaceContext::drawingManager() const {
159 return fContext->priv().drawingManager();
160}
161
162#ifdef SK_DEBUG
Brian Salomon70fe17e2020-11-30 14:33:58 -0500163GrSingleOwner* GrSurfaceContext::singleOwner() const { return fContext->priv().singleOwner(); }
Robert Phillips0d075de2019-03-04 11:08:13 -0500164#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400165
Brian Salomondd4087d2020-12-23 20:36:44 -0500166static bool alpha_types_compatible(SkAlphaType srcAlphaType, SkAlphaType dstAlphaType) {
167 // If both alpha types are kUnknown things make sense. If not, it's too underspecified.
168 return (srcAlphaType == kUnknown_SkAlphaType) == (dstAlphaType == kUnknown_SkAlphaType);
169}
170
171bool GrSurfaceContext::readPixels(GrDirectContext* dContext, GrPixmap dst, SkIPoint pt) {
Brian Salomon1d435302019-07-01 13:05:28 -0400172 ASSERT_SINGLE_OWNER
173 RETURN_FALSE_IF_ABANDONED
174 SkDEBUGCODE(this->validate();)
175 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400176 if (!fContext->priv().matches(dContext)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400177 return false;
178 }
Brian Salomon46f63232021-01-25 10:13:35 -0500179
180 if (dst.colorType() == GrColorType::kUnknown) {
181 return false;
182 }
183
Brian Salomonc24c8ef2021-02-01 13:32:30 -0500184 if (dst.rowBytes() % dst.info().bpp()) {
185 return false;
186 }
187
Brian Salomondd4087d2020-12-23 20:36:44 -0500188 dst = dst.clip(this->dimensions(), &pt);
189 if (!dst.hasPixels()) {
Brian Salomon1d435302019-07-01 13:05:28 -0400190 return false;
191 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500192 if (!alpha_types_compatible(this->colorInfo().alphaType(), dst.alphaType())) {
Brian Salomon1d435302019-07-01 13:05:28 -0400193 return false;
194 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500195 // We allow unknown alpha types but only if both src and dst are unknown. Otherwise, it's too
196 // weird to reason about what should be expected.
Greg Daniel6eb8c242019-06-05 10:22:24 -0400197
Brian Salomon982127b2021-01-21 10:43:35 -0500198 sk_sp<GrSurfaceProxy> srcProxy = this->asSurfaceProxyRef();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400199
Stephen White3c0a50f2020-01-16 18:19:54 -0500200 if (srcProxy->framebufferOnly()) {
201 return false;
202 }
203
Greg Daniel6eb8c242019-06-05 10:22:24 -0400204 // MDB TODO: delay this instantiation until later in the method
Adlai Hollerc95b5892020-08-11 12:02:22 -0400205 if (!srcProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400206 return false;
207 }
208
209 GrSurface* srcSurface = srcProxy->peekSurface();
210
Brian Salomondd4087d2020-12-23 20:36:44 -0500211 SkColorSpaceXformSteps::Flags flags =
212 SkColorSpaceXformSteps{this->colorInfo(), dst.info()}.flags;
Mike Klein7321e6a2019-12-03 11:08:40 -0600213 bool unpremul = flags.unpremul,
214 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
215 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400216
Adlai Hollerc95b5892020-08-11 12:02:22 -0400217 const GrCaps* caps = dContext->priv().caps();
Robert Phillips07f0e412020-01-17 15:20:00 -0500218 bool srcIsCompressed = caps->isFormatCompressed(srcSurface->backendFormat());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400219 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
220 // care so much about getImageData performance. However, in order to ensure putImageData/
221 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
222 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
223 // fContext->vaildaPMUPMConversionExists()).
Greg Daniel0258c902019-08-01 13:08:33 -0400224 GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
225 GrRenderable::kYes);
Greg Danielc71c7962020-01-14 16:44:18 -0500226 GrColorType srcColorType = this->colorInfo().colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400227 bool canvas2DFastPath = unpremul && !needColorConversion &&
Brian Salomondd4087d2020-12-23 20:36:44 -0500228 (GrColorType::kRGBA_8888 == dst.colorType() ||
229 GrColorType::kBGRA_8888 == dst.colorType()) &&
Brian Salomon1d435302019-07-01 13:05:28 -0400230 SkToBool(srcProxy->asTextureProxy()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500231 (srcColorType == GrColorType::kRGBA_8888 ||
232 srcColorType == GrColorType::kBGRA_8888) &&
Greg Daniel0258c902019-08-01 13:08:33 -0400233 defaultRGBAFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400234 dContext->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400235
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400236 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400237 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400238 return false;
239 }
240
Brian Salomondc0710f2019-07-01 14:59:32 -0400241 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400242 std::unique_ptr<GrSurfaceContext> tempCtx;
243 if (this->asTextureProxy()) {
244 GrColorType colorType = (canvas2DFastPath || srcIsCompressed)
245 ? GrColorType::kRGBA_8888
246 : this->colorInfo().colorType();
Brian Salomondd4087d2020-12-23 20:36:44 -0500247 SkAlphaType alphaType = canvas2DFastPath ? dst.alphaType()
Brian Salomon590f5672020-12-16 11:44:47 -0500248 : this->colorInfo().alphaType();
249 GrImageInfo tempInfo(colorType,
250 alphaType,
251 this->colorInfo().refColorSpace(),
Brian Salomondd4087d2020-12-23 20:36:44 -0500252 dst.dimensions());
Brian Salomon590f5672020-12-16 11:44:47 -0500253 auto sfc = GrSurfaceFillContext::Make(dContext, tempInfo, SkBackingFit::kApprox);
254 if (!sfc) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400255 return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400256 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400257
258 std::unique_ptr<GrFragmentProcessor> fp;
259 if (canvas2DFastPath) {
260 fp = dContext->priv().createPMToUPMEffect(GrTextureEffect::Make(
261 this->readSurfaceView(), this->colorInfo().alphaType()));
Brian Salomondd4087d2020-12-23 20:36:44 -0500262 if (dst.colorType() == GrColorType::kBGRA_8888) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400263 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
Brian Salomondd4087d2020-12-23 20:36:44 -0500264 dst = GrPixmap(dst.info().makeColorType(GrColorType::kRGBA_8888),
265 dst.addr(),
266 dst.rowBytes());
Brian Salomon72c7b982020-10-06 10:07:38 -0400267 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400268 } else {
269 fp = GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType());
270 }
271 if (!fp) {
272 return false;
273 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500274 sfc->fillRectToRectWithFP(SkIRect::MakePtSize(pt, dst.dimensions()),
275 SkIRect::MakeSize(dst.dimensions()),
Brian Salomon590f5672020-12-16 11:44:47 -0500276 std::move(fp));
Brian Salomon72c7b982020-10-06 10:07:38 -0400277 pt = {0, 0};
Brian Salomon590f5672020-12-16 11:44:47 -0500278 tempCtx = std::move(sfc);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400279 } else {
Brian Salomon72c7b982020-10-06 10:07:38 -0400280 auto restrictions = this->caps()->getDstCopyRestrictions(this->asRenderTargetProxy(),
281 this->colorInfo().colorType());
282 sk_sp<GrSurfaceProxy> copy;
283 static constexpr auto kFit = SkBackingFit::kExact;
284 static constexpr auto kBudgeted = SkBudgeted::kYes;
285 static constexpr auto kMipMapped = GrMipMapped::kNo;
286 if (restrictions.fMustCopyWholeSrc) {
Brian Salomon982127b2021-01-21 10:43:35 -0500287 copy = GrSurfaceProxy::Copy(fContext,
288 std::move(srcProxy),
289 this->origin(),
290 kMipMapped,
291 kFit,
Brian Salomon72c7b982020-10-06 10:07:38 -0400292 kBudgeted);
293 } else {
Brian Salomondd4087d2020-12-23 20:36:44 -0500294 auto srcRect = SkIRect::MakePtSize(pt, dst.dimensions());
Brian Salomon982127b2021-01-21 10:43:35 -0500295 copy = GrSurfaceProxy::Copy(fContext,
296 std::move(srcProxy),
297 this->origin(),
298 kMipMapped,
299 srcRect,
300 kFit,
301 kBudgeted,
302 restrictions.fRectsMustMatch);
Brian Salomon72c7b982020-10-06 10:07:38 -0400303 pt = {0, 0};
304 }
305 if (!copy) {
306 return false;
307 }
308 GrSurfaceProxyView view{std::move(copy), this->origin(), this->readSwizzle()};
Brian Salomon14f99fc2020-12-07 12:19:47 -0500309 tempCtx = GrSurfaceContext::Make(dContext, std::move(view), this->colorInfo());
Brian Salomon72c7b982020-10-06 10:07:38 -0400310 SkASSERT(tempCtx);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400311 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500312 return tempCtx->readPixels(dContext, dst, pt);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400313 }
314
Greg Danielb8d84f82020-02-13 14:25:00 -0500315 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400316
Brian Salomon1d435302019-07-01 13:05:28 -0400317 auto supportedRead = caps->supportedReadPixelsColorType(
Brian Salomondd4087d2020-12-23 20:36:44 -0500318 this->colorInfo().colorType(), srcProxy->backendFormat(), dst.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400319
Brian Salomondd4087d2020-12-23 20:36:44 -0500320 bool makeTight =
321 !caps->readPixelsRowBytesSupport() && dst.rowBytes() != dst.info().minRowBytes();
Brian Salomon1047a492019-07-02 12:25:21 -0400322
323 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomondd4087d2020-12-23 20:36:44 -0500324 (dst.colorType() != supportedRead.fColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400325
Brian Salomonf30b1c12019-06-20 12:25:02 -0400326 std::unique_ptr<char[]> tmpPixels;
Brian Salomondd4087d2020-12-23 20:36:44 -0500327 GrPixmap tmp;
328 void* readDst = dst.addr();
329 size_t readRB = dst.rowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400330 if (convert) {
Brian Salomondd4087d2020-12-23 20:36:44 -0500331 GrImageInfo tmpInfo(supportedRead.fColorType,
332 this->colorInfo().alphaType(),
333 this->colorInfo().refColorSpace(),
334 dst.dimensions());
Brian Salomon1d435302019-07-01 13:05:28 -0400335 size_t tmpRB = tmpInfo.minRowBytes();
336 size_t size = tmpRB * tmpInfo.height();
337 // Chrome MSAN bots require the data to be initialized (hence the ()).
John Stilesfbd050b2020-08-03 13:21:46 -0400338 tmpPixels = std::make_unique<char[]>(size);
Brian Salomondd4087d2020-12-23 20:36:44 -0500339 tmp = {tmpInfo, tmpPixels.get(), tmpRB};
Brian Salomonf30b1c12019-06-20 12:25:02 -0400340
Brian Salomonf30b1c12019-06-20 12:25:02 -0400341 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400342 readRB = tmpRB;
Brian Salomondd4087d2020-12-23 20:36:44 -0500343 pt.fY = flip ? srcSurface->height() - pt.fY - dst.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400344 }
345
Brian Salomon982127b2021-01-21 10:43:35 -0500346 dContext->priv().flushSurface(srcProxy.get());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400347 dContext->submit();
Brian Salomone2078f12021-05-24 12:40:46 -0400348 if (!dContext->priv().getGpu()->readPixels(srcSurface,
349 SkIRect::MakePtSize(pt, dst.dimensions()),
Brian Salomondd4087d2020-12-23 20:36:44 -0500350 this->colorInfo().colorType(),
Brian Salomone2078f12021-05-24 12:40:46 -0400351 supportedRead.fColorType,
352 readDst,
353 readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400354 return false;
355 }
356
Brian Salomondd4087d2020-12-23 20:36:44 -0500357 if (tmp.hasPixels()) {
358 return GrConvertPixels(dst, tmp, flip);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400359 }
360 return true;
361}
Robert Phillips0d075de2019-03-04 11:08:13 -0500362
Brian Salomonea1d39b2021-04-01 17:06:52 -0400363bool GrSurfaceContext::writePixels(GrDirectContext* dContext,
364 GrCPixmap src,
Brian Salomon75ee7372021-04-06 15:04:35 -0400365 SkIPoint dstPt) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400366 ASSERT_SINGLE_OWNER
367 RETURN_FALSE_IF_ABANDONED
368 SkDEBUGCODE(this->validate();)
Brian Salomon2c673402021-04-02 14:36:58 -0400369
370 src = src.clip(this->dimensions(), &dstPt);
371 if (!src.hasPixels()) {
372 return false;
373 }
374 if (!src.info().bpp() || src.rowBytes() % src.info().bpp()) {
375 return false;
376 }
Brian Salomon75ee7372021-04-06 15:04:35 -0400377 return this->internalWritePixels(dContext, &src, 1, dstPt);
Brian Salomon2c673402021-04-02 14:36:58 -0400378}
379
380bool GrSurfaceContext::writePixels(GrDirectContext* dContext,
381 const GrCPixmap src[],
Brian Salomon75ee7372021-04-06 15:04:35 -0400382 int numLevels) {
Brian Salomon2c673402021-04-02 14:36:58 -0400383 ASSERT_SINGLE_OWNER
384 RETURN_FALSE_IF_ABANDONED
385 SkDEBUGCODE(this->validate();)
386
387 SkASSERT(dContext);
388 SkASSERT(numLevels >= 1);
389 SkASSERT(src);
390
391 if (numLevels == 1) {
392 if (src->dimensions() != this->dimensions()) {
393 return false;
394 }
Brian Salomon75ee7372021-04-06 15:04:35 -0400395 return this->writePixels(dContext, src[0], {0, 0});
Brian Salomon2c673402021-04-02 14:36:58 -0400396 }
397 if (!this->asTextureProxy() || this->asTextureProxy()->proxyMipmapped() == GrMipmapped::kNo) {
398 return false;
399 }
400
401 SkISize dims = this->dimensions();
402 if (numLevels != SkMipmap::ComputeLevelCount(dims) + 1) {
403 return false;
404 }
405 for (int i = 0; i < numLevels; ++i) {
406 if (src[i].colorInfo() != src[0].colorInfo()) {
407 return false;
408 }
409 if (dims != src[i].dimensions()) {
410 return false;
411 }
412 if (!src[i].info().bpp() || src[i].rowBytes() % src[i].info().bpp()) {
413 return false;
414 }
415 dims = {std::max(1, dims.width()/2), std::max(1, dims.height()/2)};
416 }
Brian Salomon75ee7372021-04-06 15:04:35 -0400417 return this->internalWritePixels(dContext, src, numLevels, {0, 0});
Brian Salomon2c673402021-04-02 14:36:58 -0400418}
419
420bool GrSurfaceContext::internalWritePixels(GrDirectContext* dContext,
421 const GrCPixmap src[],
422 int numLevels,
Brian Salomon75ee7372021-04-06 15:04:35 -0400423 SkIPoint pt) {
Brian Salomon2c673402021-04-02 14:36:58 -0400424 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::internalWritePixels");
425
426 SkASSERT(numLevels >= 1);
427 SkASSERT(src);
428
429 // We can either write to a subset or write MIP levels, but not both.
430 SkASSERT((src[0].dimensions() == this->dimensions() && pt.isZero()) || numLevels == 1);
431 SkASSERT(numLevels == 1 ||
Brian Salomon7cde6c32021-04-02 15:53:42 -0400432 (this->asTextureProxy() && this->asTextureProxy()->mipmapped() == GrMipmapped::kYes));
Brian Salomon2c673402021-04-02 14:36:58 -0400433 // Our public caller should have clipped to the bounds of the surface already.
Brian Salomonea1d39b2021-04-01 17:06:52 -0400434 SkASSERT(SkIRect::MakeSize(this->dimensions()).contains(
435 SkIRect::MakePtSize(pt, src[0].dimensions())));
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400436
Adlai Hollerc95b5892020-08-11 12:02:22 -0400437 if (!dContext) {
Brian Salomonc320b152018-02-20 14:05:36 -0500438 return false;
439 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500440
Brian Salomon1d435302019-07-01 13:05:28 -0400441 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500442 return false;
443 }
444
Brian Salomon2c673402021-04-02 14:36:58 -0400445 if (src[0].colorType() == GrColorType::kUnknown) {
Brian Salomon46f63232021-01-25 10:13:35 -0500446 return false;
447 }
448
Brian Salomon2c673402021-04-02 14:36:58 -0400449 if (!alpha_types_compatible(src[0].alphaType(), this->colorInfo().alphaType())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400450 return false;
451 }
452
453 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
Stephen White3c0a50f2020-01-16 18:19:54 -0500454
455 if (dstProxy->framebufferOnly()) {
456 return false;
457 }
458
Adlai Hollerc95b5892020-08-11 12:02:22 -0400459 if (!dstProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400460 return false;
461 }
462
463 GrSurface* dstSurface = dstProxy->peekSurface();
464
Brian Salomondd4087d2020-12-23 20:36:44 -0500465 SkColorSpaceXformSteps::Flags flags =
Brian Salomon2c673402021-04-02 14:36:58 -0400466 SkColorSpaceXformSteps{src[0].colorInfo(), this->colorInfo()}.flags;
Mike Klein7321e6a2019-12-03 11:08:40 -0600467 bool unpremul = flags.unpremul,
468 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
469 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400470
Adlai Hollerc95b5892020-08-11 12:02:22 -0400471 const GrCaps* caps = dContext->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400472
473 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
474 GrRenderable::kNo);
475
Greg Danielc71c7962020-01-14 16:44:18 -0500476 GrColorType dstColorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400477 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
478 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400479 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
Brian Salomon2c673402021-04-02 14:36:58 -0400480 (src[0].colorType() == GrColorType::kRGBA_8888 ||
481 src[0].colorType() == GrColorType::kBGRA_8888) &&
Brian Salomon590f5672020-12-16 11:44:47 -0500482 this->asFillContext() &&
Greg Danielc71c7962020-01-14 16:44:18 -0500483 (dstColorType == GrColorType::kRGBA_8888 ||
484 dstColorType == GrColorType::kBGRA_8888) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400485 rgbaDefaultFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400486 dContext->priv().validPMUPMConversionExists();
Brian Salomon2c673402021-04-02 14:36:58 -0400487 // Drawing code path doesn't support writing to levels and doesn't support inserting layout
488 // transitions.
Brian Salomon75ee7372021-04-06 15:04:35 -0400489 if ((!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) && numLevels == 1) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500490 GrColorInfo tempColorInfo;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400491 GrBackendFormat format;
Greg Danielbfa19c42019-12-19 16:41:40 -0500492 GrSwizzle tempReadSwizzle;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400493 if (canvas2DFastPath) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500494 tempColorInfo = {GrColorType::kRGBA_8888,
495 kUnpremul_SkAlphaType,
496 this->colorInfo().refColorSpace()};
Greg Daniel7bfc9132019-08-14 14:23:53 -0400497 format = rgbaDefaultFormat;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400498 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500499 tempColorInfo = this->colorInfo();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400500 format = dstProxy->backendFormat().makeTexture2D();
501 if (!format.isValid()) {
502 return false;
503 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500504 tempReadSwizzle = this->readSwizzle();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400505 }
506
Greg Daniel2e52ad12019-06-13 10:04:16 -0400507 // It is more efficient for us to write pixels into a top left origin so we prefer that.
508 // However, if the final proxy isn't a render target then we must use a copy to move the
509 // data into it which requires the origins to match. If the final proxy is a render target
510 // we can use a draw instead which doesn't have this origin restriction. Thus for render
511 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400512 GrSurfaceOrigin tempOrigin =
Brian Salomon590f5672020-12-16 11:44:47 -0500513 this->asFillContext() ? kTopLeft_GrSurfaceOrigin : this->origin();
Brian Salomon2c673402021-04-02 14:36:58 -0400514 auto tempProxy = dContext->priv().proxyProvider()->createProxy(format,
515 src[0].dimensions(),
516 GrRenderable::kNo,
517 1,
518 GrMipmapped::kNo,
519 SkBackingFit::kApprox,
520 SkBudgeted::kYes,
521 GrProtected::kNo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400522 if (!tempProxy) {
523 return false;
524 }
Greg Daniel3912a4b2020-01-14 09:56:04 -0500525 GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500526 GrSurfaceContext tempCtx(dContext, tempView, tempColorInfo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400527
528 // In the fast path we always write the srcData to the temp context as though it were RGBA.
529 // When the data is really BGRA the write will cause the R and B channels to be swapped in
530 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
531 // dst.
Brian Salomon2c673402021-04-02 14:36:58 -0400532 GrCPixmap origSrcBase = src[0];
533 GrCPixmap srcBase = origSrcBase;
Brian Salomon1d435302019-07-01 13:05:28 -0400534 if (canvas2DFastPath) {
Brian Salomon2c673402021-04-02 14:36:58 -0400535 srcBase = GrCPixmap(origSrcBase.info().makeColorType(GrColorType::kRGBA_8888),
536 origSrcBase.addr(),
537 origSrcBase.rowBytes());
Brian Salomon1d435302019-07-01 13:05:28 -0400538 }
Brian Salomon2c673402021-04-02 14:36:58 -0400539 if (!tempCtx.writePixels(dContext, srcBase, {0, 0})) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400540 return false;
541 }
542
Brian Salomon590f5672020-12-16 11:44:47 -0500543 if (this->asFillContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400544 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400545 if (canvas2DFastPath) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400546 fp = dContext->priv().createUPMToPMEffect(
Brian Salomon14f99fc2020-12-07 12:19:47 -0500547 GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType()));
Brian Salomon1d435302019-07-01 13:05:28 -0400548 // Important: check the original src color type here!
Brian Salomon2c673402021-04-02 14:36:58 -0400549 if (origSrcBase.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400550 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
551 }
552 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500553 fp = GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400554 }
555 if (!fp) {
556 return false;
557 }
Brian Salomon2c673402021-04-02 14:36:58 -0400558 this->asFillContext()->fillRectToRectWithFP(
559 SkIRect::MakeSize(srcBase.dimensions()),
560 SkIRect::MakePtSize(pt, srcBase.dimensions()),
561 std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400562 } else {
Brian Salomon2c673402021-04-02 14:36:58 -0400563 SkIRect srcRect = SkIRect::MakeSize(srcBase.dimensions());
Brian Salomon1d435302019-07-01 13:05:28 -0400564 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Brian Salomon982127b2021-01-21 10:43:35 -0500565 if (!this->copy(std::move(tempProxy), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400566 return false;
567 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400568 }
569 return true;
570 }
571
Brian Salomon2c673402021-04-02 14:36:58 -0400572 GrColorType srcColorType = src[0].colorType();
573 auto [allowedColorType, _] =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400574 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400575 dstProxy->backendFormat(),
Brian Salomon2c673402021-04-02 14:36:58 -0400576 srcColorType);
Greg Danielb8d84f82020-02-13 14:25:00 -0500577 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400578
Brian Salomon2c673402021-04-02 14:36:58 -0400579 bool convertAll = premul ||
580 unpremul ||
581 needColorConversion ||
582 flip ||
583 (srcColorType != allowedColorType);
584 bool mustBeTight = !caps->writePixelsRowBytesSupport();
585 size_t tmpSize = 0;
586 if (mustBeTight || convertAll) {
587 for (int i = 0; i < numLevels; ++i) {
588 if (convertAll || (mustBeTight && src[i].rowBytes() != src[i].info().minRowBytes())) {
589 tmpSize += src[i].info().makeColorType(allowedColorType).minRowBytes()*
590 src[i].height();
591 }
592 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400593 }
594
Brian Salomon2c673402021-04-02 14:36:58 -0400595 auto tmpData = tmpSize ? SkData::MakeUninitialized(tmpSize) : nullptr;
596 void* tmp = tmpSize ? tmpData->writable_data() : nullptr;
597 SkAutoSTArray<15, GrMipLevel> srcLevels(numLevels);
598 bool ownAllStorage = true;
599 for (int i = 0; i < numLevels; ++i) {
600 if (convertAll || (mustBeTight && src[i].rowBytes() != src[i].info().minRowBytes())) {
601 GrImageInfo tmpInfo(allowedColorType,
602 this->colorInfo().alphaType(),
603 this->colorInfo().refColorSpace(),
604 src[i].dimensions());
605 auto tmpRB = tmpInfo.minRowBytes();
606 GrPixmap tmpPM(tmpInfo, tmp, tmpRB);
607 SkAssertResult(GrConvertPixels(tmpPM, src[i], flip));
608 srcLevels[i] = {tmpPM.addr(), tmpPM.rowBytes(), tmpData};
609 tmp = SkTAddOffset<void>(tmp, tmpRB*tmpPM.height());
610 } else {
611 srcLevels[i] = {src[i].addr(), src[i].rowBytes(), src[i].pixelStorage()};
612 ownAllStorage &= src[i].ownsPixels();
613 }
614 }
615 pt.fY = flip ? dstSurface->height() - pt.fY - src[0].height() : pt.fY;
616
617 if (!dContext->priv().drawingManager()->newWritePixelsTask(
Brian Salomon974c8212021-04-05 16:24:00 -0400618 sk_ref_sp(dstProxy),
619 SkIRect::MakePtSize(pt, src[0].dimensions()),
620 allowedColorType,
621 this->colorInfo().colorType(),
622 srcLevels.begin(),
Brian Salomon75ee7372021-04-06 15:04:35 -0400623 numLevels)) {
Brian Salomon2c673402021-04-02 14:36:58 -0400624 return false;
625 }
626 if (numLevels > 1) {
627 dstProxy->asTextureProxy()->markMipmapsClean();
628 }
629 if (!ownAllStorage) {
630 // If any pixmap doesn't own its pixels then we must flush so that the pixels are pushed to
631 // the GPU before we return.
Brian Salomon24949422021-02-11 09:39:46 -0500632 dContext->priv().flushSurface(dstProxy);
633 }
Brian Salomon2c673402021-04-02 14:36:58 -0400634 return true;
Brian Osman45580d32016-11-23 09:37:01 -0500635}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400636
Adlai Hollerc95b5892020-08-11 12:02:22 -0400637void GrSurfaceContext::asyncRescaleAndReadPixels(GrDirectContext* dContext,
638 const SkImageInfo& info,
Brian Salomon63a0a752020-06-26 13:32:09 -0400639 const SkIRect& srcRect,
640 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500641 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400642 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400643 ReadPixelsContext callbackContext) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400644 if (!dContext) {
645 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400646 return;
647 }
648 auto rt = this->asRenderTargetProxy();
649 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400650 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400651 return;
652 }
653 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400654 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400655 return;
656 }
657 auto dstCT = SkColorTypeToGrColorType(info.colorType());
658 if (dstCT == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400659 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400660 return;
661 }
Brian Salomon827bb722021-05-06 16:24:21 -0400662 bool needsRescale = srcRect.size() != info.dimensions() ||
663 this->origin() == kBottomLeft_GrSurfaceOrigin ||
664 this->colorInfo().alphaType() != info.alphaType() ||
665 !SkColorSpace::Equals(this->colorInfo().colorSpace(), info.colorSpace());
Brian Salomon63a0a752020-06-26 13:32:09 -0400666 auto colorTypeOfFinalContext = this->colorInfo().colorType();
667 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
668 if (needsRescale) {
669 colorTypeOfFinalContext = dstCT;
670 backendFormatOfFinalContext =
671 this->caps()->getDefaultBackendFormat(dstCT, GrRenderable::kYes);
672 }
673 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonbacbb922021-01-21 19:48:00 -0500674 backendFormatOfFinalContext,
675 dstCT);
Brian Salomon63a0a752020-06-26 13:32:09 -0400676 // Fail if we can't read from the source surface's color type.
677 if (readInfo.fColorType == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400678 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400679 return;
680 }
681 // Fail if read color type does not have all of dstCT's color channels and those missing color
682 // channels are in the src.
683 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
684 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
685 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
686 if ((~legalReadChannels & dstChannels) & srcChannels) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400687 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400688 return;
689 }
690
Brian Salomonbacbb922021-01-21 19:48:00 -0500691 std::unique_ptr<GrSurfaceFillContext> tempFC;
Brian Salomon63a0a752020-06-26 13:32:09 -0400692 int x = srcRect.fLeft;
693 int y = srcRect.fTop;
694 if (needsRescale) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500695 tempFC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma, rescaleMode);
696 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400697 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400698 return;
699 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500700 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
701 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400702 x = y = 0;
Brian Salomon63a0a752020-06-26 13:32:09 -0400703 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500704 auto srcCtx = tempFC ? tempFC.get() : this;
705 return srcCtx->asyncReadPixels(dContext,
706 SkIRect::MakePtSize({x, y}, info.dimensions()),
707 info.colorType(),
708 callback,
709 callbackContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400710}
711
712class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult {
713public:
Robert Phillips82ad7af2021-03-11 16:00:10 -0500714 AsyncReadResult(GrDirectContext::DirectContextID intendedRecipient)
715 : fIntendedRecipient(intendedRecipient) {
716 }
717
Brian Salomon63a0a752020-06-26 13:32:09 -0400718 ~AsyncReadResult() override {
719 for (int i = 0; i < fPlanes.count(); ++i) {
Robert Phillips82ad7af2021-03-11 16:00:10 -0500720 fPlanes[i].releaseMappedBuffer(fIntendedRecipient);
Brian Salomon63a0a752020-06-26 13:32:09 -0400721 }
722 }
723
724 int count() const override { return fPlanes.count(); }
Brian Salomonbe1084b2021-01-26 13:29:30 -0500725 const void* data(int i) const override { return fPlanes[i].data(); }
726 size_t rowBytes(int i) const override { return fPlanes[i].rowBytes(); }
Brian Salomon63a0a752020-06-26 13:32:09 -0400727
728 bool addTransferResult(const PixelTransferResult& result,
729 SkISize dimensions,
730 size_t rowBytes,
731 GrClientMappedBufferManager* manager) {
732 SkASSERT(!result.fTransferBuffer->isMapped());
733 const void* mappedData = result.fTransferBuffer->map();
734 if (!mappedData) {
735 return false;
736 }
737 if (result.fPixelConverter) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500738 size_t size = rowBytes*dimensions.height();
739 sk_sp<SkData> data = SkData::MakeUninitialized(size);
740 result.fPixelConverter(data->writable_data(), mappedData);
741 this->addCpuPlane(std::move(data), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400742 result.fTransferBuffer->unmap();
743 } else {
744 manager->insert(result.fTransferBuffer);
745 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
746 }
747 return true;
748 }
749
Brian Salomonbe1084b2021-01-26 13:29:30 -0500750 void addCpuPlane(sk_sp<SkData> data, size_t rowBytes) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400751 SkASSERT(data);
752 SkASSERT(rowBytes > 0);
Brian Salomonbe1084b2021-01-26 13:29:30 -0500753 fPlanes.emplace_back(std::move(data), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400754 }
755
756private:
757 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
758 SkASSERT(data);
759 SkASSERT(rowBytes > 0);
760 SkASSERT(mappedBuffer);
761 SkASSERT(mappedBuffer->isMapped());
Brian Salomonbe1084b2021-01-26 13:29:30 -0500762 fPlanes.emplace_back(std::move(mappedBuffer), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400763 }
764
Brian Salomonbe1084b2021-01-26 13:29:30 -0500765 class Plane {
766 public:
767 Plane(sk_sp<GrGpuBuffer> buffer, size_t rowBytes)
768 : fMappedBuffer(std::move(buffer)), fRowBytes(rowBytes) {}
769 Plane(sk_sp<SkData> data, size_t rowBytes) : fData(std::move(data)), fRowBytes(rowBytes) {}
770
771 Plane(const Plane&) = delete;
772 Plane(Plane&&) = default;
773
774 ~Plane() { SkASSERT(!fMappedBuffer); }
775
776 Plane& operator=(const Plane&) = delete;
777 Plane& operator=(Plane&&) = default;
778
Robert Phillips82ad7af2021-03-11 16:00:10 -0500779 void releaseMappedBuffer(GrDirectContext::DirectContextID intendedRecipient) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500780 if (fMappedBuffer) {
781 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
Robert Phillips82ad7af2021-03-11 16:00:10 -0500782 {std::move(fMappedBuffer), intendedRecipient});
Brian Salomonbe1084b2021-01-26 13:29:30 -0500783 }
784 }
785
786 const void* data() const {
787 if (fMappedBuffer) {
788 SkASSERT(!fData);
789 SkASSERT(fMappedBuffer->isMapped());
790 return fMappedBuffer->map();
791 }
792 SkASSERT(fData);
793 return fData->data();
794 }
795
796 size_t rowBytes() const { return fRowBytes; }
797
798 private:
799 sk_sp<SkData> fData;
Brian Salomon1eea1ea2021-01-26 18:12:25 +0000800 sk_sp<GrGpuBuffer> fMappedBuffer;
Brian Salomonbe1084b2021-01-26 13:29:30 -0500801 size_t fRowBytes;
Brian Salomon63a0a752020-06-26 13:32:09 -0400802 };
803 SkSTArray<3, Plane> fPlanes;
Robert Phillips82ad7af2021-03-11 16:00:10 -0500804 GrDirectContext::DirectContextID fIntendedRecipient;
Brian Salomon63a0a752020-06-26 13:32:09 -0400805};
806
Adlai Hollerc95b5892020-08-11 12:02:22 -0400807void GrSurfaceContext::asyncReadPixels(GrDirectContext* dContext,
808 const SkIRect& rect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400809 SkColorType colorType,
810 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400811 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400812 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
813 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
814
Adlai Hollerc95b5892020-08-11 12:02:22 -0400815 if (!dContext || this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
816 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400817 return;
818 }
819
Adlai Hollerc95b5892020-08-11 12:02:22 -0400820 auto mappedBufferManager = dContext->priv().clientMappedBufferManager();
Brian Salomon63a0a752020-06-26 13:32:09 -0400821
822 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
823
824 if (!transferResult.fTransferBuffer) {
825 auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
826 this->colorInfo().refColorSpace());
Robert Phillips82ad7af2021-03-11 16:00:10 -0500827 static const GrDirectContext::DirectContextID kInvalid;
828 auto result = std::make_unique<AsyncReadResult>(kInvalid);
Brian Salomonbe1084b2021-01-26 13:29:30 -0500829 GrPixmap pm = GrPixmap::Allocate(ii);
830 result->addCpuPlane(pm.pixelStorage(), pm.rowBytes());
Brian Salomon63a0a752020-06-26 13:32:09 -0400831
Adlai Hollerc95b5892020-08-11 12:02:22 -0400832 SkIPoint pt{rect.fLeft, rect.fTop};
Brian Salomondd4087d2020-12-23 20:36:44 -0500833 if (!this->readPixels(dContext, pm, pt)) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400834 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400835 return;
836 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400837 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400838 return;
839 }
840
841 struct FinishContext {
842 ReadPixelsCallback* fClientCallback;
843 ReadPixelsContext fClientContext;
844 SkISize fSize;
845 SkColorType fColorType;
Jim Van Vertha655f0d2021-05-18 15:03:27 -0400846 size_t fBufferAlignment;
Brian Salomon63a0a752020-06-26 13:32:09 -0400847 GrClientMappedBufferManager* fMappedBufferManager;
848 PixelTransferResult fTransferResult;
849 };
850 // Assumption is that the caller would like to flush. We could take a parameter or require an
851 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
852 // callback to GrGpu until after the next flush that flushes our op list, though.
853 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400854 callbackContext,
Brian Salomon63a0a752020-06-26 13:32:09 -0400855 rect.size(),
856 colorType,
Jim Van Vertha655f0d2021-05-18 15:03:27 -0400857 this->caps()->transferBufferAlignment(),
Brian Salomon63a0a752020-06-26 13:32:09 -0400858 mappedBufferManager,
859 std::move(transferResult)};
860 auto finishCallback = [](GrGpuFinishedContext c) {
861 const auto* context = reinterpret_cast<const FinishContext*>(c);
Robert Phillips82ad7af2021-03-11 16:00:10 -0500862 auto manager = context->fMappedBufferManager;
863 auto result = std::make_unique<AsyncReadResult>(manager->owningDirectContext());
Jim Van Vertha655f0d2021-05-18 15:03:27 -0400864 size_t rowBytes =
865 GrAlignTo(context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType),
866 context->fBufferAlignment);
Brian Salomon63a0a752020-06-26 13:32:09 -0400867 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
Robert Phillips82ad7af2021-03-11 16:00:10 -0500868 manager)) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400869 result.reset();
870 }
871 (*context->fClientCallback)(context->fClientContext, std::move(result));
872 delete context;
873 };
874 GrFlushInfo flushInfo;
875 flushInfo.fFinishedContext = finishContext;
876 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -0500877
878 dContext->priv().flushSurface(this->asSurfaceProxy(),
879 SkSurface::BackendSurfaceAccess::kNoAccess,
880 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400881}
882
Adlai Hollerc95b5892020-08-11 12:02:22 -0400883void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(GrDirectContext* dContext,
884 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400885 sk_sp<SkColorSpace> dstColorSpace,
886 const SkIRect& srcRect,
887 SkISize dstSize,
888 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500889 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400890 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400891 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400892 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
893 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
894 SkASSERT(!dstSize.isZero());
895 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
896
Adlai Hollerc95b5892020-08-11 12:02:22 -0400897 if (!dContext) {
898 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400899 return;
900 }
901 auto rt = this->asRenderTargetProxy();
902 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400903 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400904 return;
905 }
906 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400907 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400908 return;
909 }
910 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400911 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400912 return;
913 }
914 int x = srcRect.fLeft;
915 int y = srcRect.fTop;
Brian Salomon827bb722021-05-06 16:24:21 -0400916 bool needsRescale = srcRect.size() != dstSize ||
917 !SkColorSpace::Equals(this->colorInfo().colorSpace(), dstColorSpace.get());
918 GrSurfaceProxyView srcView = this->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400919 if (needsRescale) {
Brian Salomon827bb722021-05-06 16:24:21 -0400920 auto info = SkImageInfo::Make(dstSize,
921 kRGBA_8888_SkColorType,
922 this->colorInfo().alphaType(),
923 dstColorSpace);
Brian Salomon63a0a752020-06-26 13:32:09 -0400924 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomonbacbb922021-01-21 19:48:00 -0500925 auto tempFC = this->rescale(info,
926 kTopLeft_GrSurfaceOrigin,
927 srcRect,
928 rescaleGamma,
929 rescaleMode);
930 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400931 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400932 return;
933 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500934 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
935 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400936 x = y = 0;
Brian Salomonbacbb922021-01-21 19:48:00 -0500937 srcView = tempFC->readSurfaceView();
Brian Salomon827bb722021-05-06 16:24:21 -0400938 } else if (!srcView.asTextureProxy()) {
939 srcView = GrSurfaceProxyView::Copy(fContext,
940 std::move(srcView),
941 GrMipmapped::kNo,
942 srcRect,
943 SkBackingFit::kApprox,
944 SkBudgeted::kYes);
945 if (!srcView) {
946 // If we can't get a texture copy of the contents then give up.
947 callback(callbackContext, nullptr);
948 return;
Brian Salomon63a0a752020-06-26 13:32:09 -0400949 }
Brian Salomon827bb722021-05-06 16:24:21 -0400950 SkASSERT(srcView.asTextureProxy());
951 x = y = 0;
Brian Salomon63a0a752020-06-26 13:32:09 -0400952 }
953
Brian Salomonbacbb922021-01-21 19:48:00 -0500954 auto yInfo = SkImageInfo::MakeA8(dstSize);
955 auto yFC = GrSurfaceFillContext::MakeWithFallback(dContext, yInfo, SkBackingFit::kApprox);
956
957 auto uvInfo = yInfo.makeWH(yInfo.width()/2, yInfo.height()/2);
958 auto uFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
959 auto vFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
960
961 if (!yFC || !uFC || !vFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400962 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400963 return;
964 }
965
966 float baseM[20];
967 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
968
969 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
970
971 auto texMatrix = SkMatrix::Translate(x, y);
972
Brian Salomoneac233a2021-06-24 11:56:50 -0400973 auto [readCT, offsetAlignment] =
974 this->caps()->supportedReadPixelsColorType(yFC->colorInfo().colorType(),
975 yFC->asSurfaceProxy()->backendFormat(),
976 GrColorType::kAlpha_8);
977 if (readCT == GrColorType::kUnknown) {
978 callback(callbackContext, nullptr);
979 return;
980 }
981 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport() ||
982 !offsetAlignment;
Brian Salomon63a0a752020-06-26 13:32:09 -0400983 PixelTransferResult yTransfer, uTransfer, vTransfer;
984
985 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
986 float yM[20];
987 std::fill_n(yM, 15, 0.f);
988 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500989
990 auto yFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
Brian Osman48f83fd2021-06-23 15:51:26 +0000991 yFP = GrFragmentProcessor::ColorMatrix(std::move(yFP),
992 yM,
993 /*unpremulInput=*/false,
994 /*clampRGBOutput=*/true,
995 /*premulOutput=*/false);
Brian Salomonbacbb922021-01-21 19:48:00 -0500996 yFC->fillWithFP(std::move(yFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400997 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500998 yTransfer = yFC->transferPixels(GrColorType::kAlpha_8,
999 SkIRect::MakeSize(yFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -04001000 if (!yTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001001 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001002 return;
1003 }
1004 }
1005
1006 texMatrix.preScale(2.f, 2.f);
1007 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
1008 float uM[20];
1009 std::fill_n(uM, 15, 0.f);
1010 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -05001011
1012 auto uFP = GrTextureEffect::Make(srcView,
1013 this->colorInfo().alphaType(),
1014 texMatrix,
1015 GrSamplerState::Filter::kLinear);
Brian Osman48f83fd2021-06-23 15:51:26 +00001016 uFP = GrFragmentProcessor::ColorMatrix(std::move(uFP),
1017 uM,
1018 /*unpremulInput=*/false,
1019 /*clampRGBOutput=*/true,
1020 /*premulOutput=*/false);
Brian Salomonbacbb922021-01-21 19:48:00 -05001021 uFC->fillWithFP(std::move(uFP));
Brian Salomon63a0a752020-06-26 13:32:09 -04001022 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001023 uTransfer = uFC->transferPixels(GrColorType::kAlpha_8,
1024 SkIRect::MakeSize(uFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -04001025 if (!uTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001026 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001027 return;
1028 }
1029 }
1030
1031 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
1032 float vM[20];
1033 std::fill_n(vM, 15, 0.f);
1034 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -05001035 auto vFP = GrTextureEffect::Make(std::move(srcView),
1036 this->colorInfo().alphaType(),
1037 texMatrix,
1038 GrSamplerState::Filter::kLinear);
Brian Osman48f83fd2021-06-23 15:51:26 +00001039 vFP = GrFragmentProcessor::ColorMatrix(std::move(vFP),
1040 vM,
1041 /*unpremulInput=*/false,
1042 /*clampRGBOutput=*/true,
1043 /*premulOutput=*/false);
Brian Salomonbacbb922021-01-21 19:48:00 -05001044 vFC->fillWithFP(std::move(vFP));
1045
Brian Salomon63a0a752020-06-26 13:32:09 -04001046 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001047 vTransfer = vFC->transferPixels(GrColorType::kAlpha_8,
1048 SkIRect::MakeSize(vFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -04001049 if (!vTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001050 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001051 return;
1052 }
1053 }
1054
1055 if (doSynchronousRead) {
Brian Salomonbe1084b2021-01-26 13:29:30 -05001056 GrPixmap yPmp = GrPixmap::Allocate(yInfo);
1057 GrPixmap uPmp = GrPixmap::Allocate(uvInfo);
1058 GrPixmap vPmp = GrPixmap::Allocate(uvInfo);
Brian Salomonbacbb922021-01-21 19:48:00 -05001059 if (!yFC->readPixels(dContext, yPmp, {0, 0}) ||
1060 !uFC->readPixels(dContext, uPmp, {0, 0}) ||
1061 !vFC->readPixels(dContext, vPmp, {0, 0})) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001062 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001063 return;
1064 }
Robert Phillips82ad7af2021-03-11 16:00:10 -05001065 auto result = std::make_unique<AsyncReadResult>(dContext->directContextID());
Brian Salomonbe1084b2021-01-26 13:29:30 -05001066 result->addCpuPlane(yPmp.pixelStorage(), yPmp.rowBytes());
1067 result->addCpuPlane(uPmp.pixelStorage(), uPmp.rowBytes());
1068 result->addCpuPlane(vPmp.pixelStorage(), vPmp.rowBytes());
Adlai Hollerc95b5892020-08-11 12:02:22 -04001069 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -04001070 return;
1071 }
1072
1073 struct FinishContext {
1074 ReadPixelsCallback* fClientCallback;
1075 ReadPixelsContext fClientContext;
1076 GrClientMappedBufferManager* fMappedBufferManager;
1077 SkISize fSize;
Jim Van Vertha655f0d2021-05-18 15:03:27 -04001078 size_t fBufferAlignment;
Brian Salomon63a0a752020-06-26 13:32:09 -04001079 PixelTransferResult fYTransfer;
1080 PixelTransferResult fUTransfer;
1081 PixelTransferResult fVTransfer;
1082 };
1083 // Assumption is that the caller would like to flush. We could take a parameter or require an
1084 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1085 // callback to GrGpu until after the next flush that flushes our op list, though.
1086 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -04001087 callbackContext,
1088 dContext->priv().clientMappedBufferManager(),
Brian Salomon63a0a752020-06-26 13:32:09 -04001089 dstSize,
Jim Van Vertha655f0d2021-05-18 15:03:27 -04001090 this->caps()->transferBufferAlignment(),
Brian Salomon63a0a752020-06-26 13:32:09 -04001091 std::move(yTransfer),
1092 std::move(uTransfer),
1093 std::move(vTransfer)};
1094 auto finishCallback = [](GrGpuFinishedContext c) {
1095 const auto* context = reinterpret_cast<const FinishContext*>(c);
Brian Salomon63a0a752020-06-26 13:32:09 -04001096 auto manager = context->fMappedBufferManager;
Robert Phillips82ad7af2021-03-11 16:00:10 -05001097 auto result = std::make_unique<AsyncReadResult>(manager->owningDirectContext());
Brian Salomon63a0a752020-06-26 13:32:09 -04001098 size_t rowBytes = SkToSizeT(context->fSize.width());
Jim Van Vertha655f0d2021-05-18 15:03:27 -04001099 rowBytes = GrAlignTo(rowBytes, context->fBufferAlignment);
Brian Salomon63a0a752020-06-26 13:32:09 -04001100 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
1101 (*context->fClientCallback)(context->fClientContext, nullptr);
1102 delete context;
1103 return;
1104 }
Jim Van Vertha655f0d2021-05-18 15:03:27 -04001105 rowBytes = SkToSizeT(context->fSize.width()) / 2;
1106 rowBytes = GrAlignTo(rowBytes, context->fBufferAlignment);
Brian Salomon63a0a752020-06-26 13:32:09 -04001107 SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
1108 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
1109 (*context->fClientCallback)(context->fClientContext, nullptr);
1110 delete context;
1111 return;
1112 }
1113 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
1114 (*context->fClientCallback)(context->fClientContext, nullptr);
1115 delete context;
1116 return;
1117 }
1118 (*context->fClientCallback)(context->fClientContext, std::move(result));
1119 delete context;
1120 };
1121 GrFlushInfo flushInfo;
1122 flushInfo.fFinishedContext = finishContext;
1123 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -05001124 dContext->priv().flushSurface(this->asSurfaceProxy(),
1125 SkSurface::BackendSurfaceAccess::kNoAccess,
1126 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -04001127}
1128
Brian Salomond63638b2021-03-05 14:00:07 -05001129sk_sp<GrRenderTask> GrSurfaceContext::copy(sk_sp<GrSurfaceProxy> src,
1130 SkIRect srcRect,
1131 SkIPoint dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001132 ASSERT_SINGLE_OWNER
Brian Salomond63638b2021-03-05 14:00:07 -05001133 RETURN_NULLPTR_IF_ABANDONED
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001134 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -04001135 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -04001136
Brian Salomon947efe22019-07-16 15:36:11 -04001137 const GrCaps* caps = fContext->priv().caps();
1138
Greg Daniel46cfbc62019-06-07 11:43:30 -04001139 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -05001140 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -04001141
Stephen White3c0a50f2020-01-16 18:19:54 -05001142 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomond63638b2021-03-05 14:00:07 -05001143 return nullptr;
Stephen White3c0a50f2020-01-16 18:19:54 -05001144 }
1145
Brian Salomon982127b2021-01-21 10:43:35 -05001146 if (!caps->canCopySurface(this->asSurfaceProxy(), src.get(), srcRect, dstPoint)) {
Brian Salomond63638b2021-03-05 14:00:07 -05001147 return nullptr;
Greg Daniel25af6712018-04-25 10:44:38 -04001148 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001149
Brian Salomon982127b2021-01-21 10:43:35 -05001150 return this->drawingManager()->newCopyRenderTask(std::move(src),
1151 srcRect,
1152 this->asSurfaceProxyRef(),
Brian Salomon0f9f8002021-01-22 16:30:50 -05001153 dstPoint,
1154 this->origin());
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001155}
Greg Daniel46cfbc62019-06-07 11:43:30 -04001156
Brian Salomonbacbb922021-01-21 19:48:00 -05001157std::unique_ptr<GrSurfaceFillContext> GrSurfaceContext::rescale(const GrImageInfo& info,
Brian Salomoneebe7352020-12-09 16:37:04 -05001158 GrSurfaceOrigin origin,
1159 SkIRect srcRect,
1160 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001161 RescaleMode rescaleMode) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001162 auto sfc = GrSurfaceFillContext::MakeWithFallback(fContext,
1163 info,
Brian Salomon1c86b632020-12-11 12:36:01 -05001164 SkBackingFit::kExact,
Brian Salomon1c86b632020-12-11 12:36:01 -05001165 1,
1166 GrMipmapped::kNo,
1167 this->asSurfaceProxy()->isProtected(),
1168 origin);
Brian Salomonbacbb922021-01-21 19:48:00 -05001169 if (!sfc || !this->rescaleInto(sfc.get(),
1170 SkIRect::MakeSize(sfc->dimensions()),
Brian Salomon1c86b632020-12-11 12:36:01 -05001171 srcRect,
1172 rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001173 rescaleMode)) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001174 return nullptr;
1175 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001176 return sfc;
Brian Salomon1c86b632020-12-11 12:36:01 -05001177}
1178
Brian Salomonbacbb922021-01-21 19:48:00 -05001179bool GrSurfaceContext::rescaleInto(GrSurfaceFillContext* dst,
Brian Salomon1c86b632020-12-11 12:36:01 -05001180 SkIRect dstRect,
1181 SkIRect srcRect,
1182 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001183 RescaleMode rescaleMode) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001184 SkASSERT(dst);
1185 if (!SkIRect::MakeSize(dst->dimensions()).contains((dstRect))) {
1186 return false;
1187 }
1188
Brian Salomone9ad9982019-07-22 16:17:41 -04001189 auto rtProxy = this->asRenderTargetProxy();
1190 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001191 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001192 }
1193
Stephen White3c0a50f2020-01-16 18:19:54 -05001194 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001195 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001196 }
1197
Greg Daniel40903af2020-01-30 14:55:05 -05001198 GrSurfaceProxyView texView = this->readSurfaceView();
Greg Daniel40903af2020-01-30 14:55:05 -05001199 if (!texView.asTextureProxy()) {
Brian Salomon827bb722021-05-06 16:24:21 -04001200 // TODO: If copying supported specifying a renderable copy then we could return the copy
1201 // when there are no other conversions.
Brian Salomon7e67dca2020-07-21 09:27:25 -04001202 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect,
Brian Salomonc5243782020-04-02 12:50:34 -04001203 SkBackingFit::kApprox, SkBudgeted::kNo);
1204 if (!texView) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001205 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001206 }
Greg Daniel40903af2020-01-30 14:55:05 -05001207 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001208 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001209 }
1210
Brian Salomon1c86b632020-12-11 12:36:01 -05001211 SkISize finalSize = dstRect.size();
Brian Salomon827bb722021-05-06 16:24:21 -04001212 if (finalSize == srcRect.size()) {
1213 rescaleGamma = RescaleGamma::kSrc;
1214 rescaleMode = RescaleMode::kNearest;
1215 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001216
Brian Salomonbf6b9792019-08-21 09:38:10 -04001217 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
1218 // pass B is moved to A. If 'this' is the input on the first pass then tempA is null.
Brian Salomonbacbb922021-01-21 19:48:00 -05001219 std::unique_ptr<GrSurfaceFillContext> tempA;
1220 std::unique_ptr<GrSurfaceFillContext> tempB;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001221
Brian Salomone9ad9982019-07-22 16:17:41 -04001222 // Assume we should ignore the rescale linear request if the surface has no color space since
1223 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon63a0a752020-06-26 13:32:09 -04001224 if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() &&
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001225 !this->colorInfo().colorSpace()->gammaIsLinear()) {
1226 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomone9ad9982019-07-22 16:17:41 -04001227 // We'll fall back to kRGBA_8888 if half float not supported.
Brian Salomonbacbb922021-01-21 19:48:00 -05001228 GrImageInfo ii(GrColorType::kRGBA_F16,
1229 dst->colorInfo().alphaType(),
1230 std::move(cs),
1231 srcRect.size());
1232 auto linearRTC = GrSurfaceFillContext::MakeWithFallback(fContext,
1233 std::move(ii),
1234 SkBackingFit::kApprox,
1235 1,
1236 GrMipmapped::kNo,
1237 GrProtected::kNo,
1238 dst->origin());
Brian Salomone9ad9982019-07-22 16:17:41 -04001239 if (!linearRTC) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001240 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001241 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001242 auto fp = GrTextureEffect::Make(std::move(texView),
1243 this->colorInfo().alphaType(),
1244 SkMatrix::Translate(srcRect.topLeft()),
1245 GrSamplerState::Filter::kNearest,
1246 GrSamplerState::MipmapMode::kNone);
1247 fp = GrColorSpaceXformEffect::Make(std::move(fp),
1248 this->colorInfo(),
1249 linearRTC->colorInfo());
1250 linearRTC->fillWithFP(std::move(fp));
Greg Daniel40903af2020-01-30 14:55:05 -05001251 texView = linearRTC->readSurfaceView();
1252 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -04001253 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001254 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001255 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001256
Brian Salomon827bb722021-05-06 16:24:21 -04001257 do {
Brian Salomon1c86b632020-12-11 12:36:01 -05001258 SkISize nextDims = finalSize;
Mike Reed1efa14d2021-01-02 21:44:59 -05001259 if (rescaleMode != RescaleMode::kNearest) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001260 if (srcRect.width() > finalSize.width()) {
1261 nextDims.fWidth = std::max((srcRect.width() + 1)/2, finalSize.width());
1262 } else if (srcRect.width() < finalSize.width()) {
1263 nextDims.fWidth = std::min(srcRect.width()*2, finalSize.width());
Brian Salomon59f31b12020-06-04 17:27:15 -04001264 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001265 if (srcRect.height() > finalSize.height()) {
1266 nextDims.fHeight = std::max((srcRect.height() + 1)/2, finalSize.height());
1267 } else if (srcRect.height() < finalSize.height()) {
1268 nextDims.fHeight = std::min(srcRect.height()*2, finalSize.height());
Brian Salomon59f31b12020-06-04 17:27:15 -04001269 }
1270 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001271 auto input = tempA ? tempA.get() : this;
Brian Salomone9ad9982019-07-22 16:17:41 -04001272 sk_sp<GrColorSpaceXform> xform;
Brian Salomonbacbb922021-01-21 19:48:00 -05001273 GrSurfaceFillContext* stepDst;
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001274 SkIRect stepDstRect;
Brian Salomon1c86b632020-12-11 12:36:01 -05001275 if (nextDims == finalSize) {
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001276 stepDst = dst;
1277 stepDstRect = dstRect;
Brian Salomonbacbb922021-01-21 19:48:00 -05001278 xform = GrColorSpaceXform::Make(input->colorInfo(), dst->colorInfo());
Brian Salomon1c86b632020-12-11 12:36:01 -05001279 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -05001280 GrImageInfo nextInfo(input->colorInfo(), nextDims);
1281 tempB = GrSurfaceFillContext::MakeWithFallback(fContext,
1282 nextInfo,
1283 SkBackingFit::kApprox);
Brian Salomon1c86b632020-12-11 12:36:01 -05001284 if (!tempB) {
1285 return false;
1286 }
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001287 stepDst = tempB.get();
1288 stepDstRect = SkIRect::MakeSize(tempB->dimensions());
Brian Salomone9ad9982019-07-22 16:17:41 -04001289 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001290 std::unique_ptr<GrFragmentProcessor> fp;
Mike Reed1efa14d2021-01-02 21:44:59 -05001291 if (rescaleMode == RescaleMode::kRepeatedCubic) {
Brian Salomon59f31b12020-06-04 17:27:15 -04001292 auto dir = GrBicubicEffect::Direction::kXY;
1293 if (nextDims.width() == srcRect.width()) {
1294 dir = GrBicubicEffect::Direction::kY;
1295 } else if (nextDims.height() == srcRect.height()) {
1296 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -04001297 }
Brian Salomon1af72d12020-06-25 10:47:26 -04001298 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
Mike Reed3867c702020-09-01 13:28:10 -04001299 static constexpr auto kKernel = GrBicubicEffect::gCatmullRom;
Brian Salomon1c86b632020-12-11 12:36:01 -05001300 fp = GrBicubicEffect::MakeSubset(std::move(texView),
1301 input->colorInfo().alphaType(),
Brian Salomonbacbb922021-01-21 19:48:00 -05001302 SkMatrix::I(),
Brian Salomon1c86b632020-12-11 12:36:01 -05001303 kWM,
1304 kWM,
1305 SkRect::Make(srcRect),
1306 kKernel,
1307 dir,
1308 *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -04001309 } else {
Mike Reed1efa14d2021-01-02 21:44:59 -05001310 auto filter = rescaleMode == RescaleMode::kNearest ? GrSamplerState::Filter::kNearest
1311 : GrSamplerState::Filter::kLinear;
Brian Salomonbacbb922021-01-21 19:48:00 -05001312 auto srcRectF = SkRect::Make(srcRect);
1313 fp = GrTextureEffect::MakeSubset(std::move(texView),
1314 this->colorInfo().alphaType(),
1315 SkMatrix::I(),
1316 {filter, GrSamplerState::MipmapMode::kNone},
1317 srcRectF,
1318 srcRectF,
1319 *this->caps());
Brian Salomone9ad9982019-07-22 16:17:41 -04001320 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001321 if (xform) {
1322 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
1323 }
1324 stepDst->fillRectToRectWithFP(srcRect, stepDstRect, std::move(fp));
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001325 texView = stepDst->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -04001326 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001327 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomon827bb722021-05-06 16:24:21 -04001328 } while (srcRect.size() != finalSize);
Brian Salomon1c86b632020-12-11 12:36:01 -05001329 return true;
Brian Salomone9ad9982019-07-22 16:17:41 -04001330}
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001331
1332GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
1333 const SkIRect& rect) {
1334 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1335 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001336 auto direct = fContext->asDirectContext();
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001337 if (!direct) {
1338 return {};
1339 }
1340 auto rtProxy = this->asRenderTargetProxy();
1341 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1342 return {};
1343 }
1344
1345 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001346 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
1347 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001348 // Fail if read color type does not have all of dstCT's color channels and those missing color
1349 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001350 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1351 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
1352 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1353 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001354 return {};
1355 }
1356
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001357 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001358 !supportedRead.fOffsetAlignmentForTransferBuffer) {
1359 return {};
1360 }
1361
1362 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
Jim Van Vertha655f0d2021-05-18 15:03:27 -04001363 rowBytes = GrAlignTo(rowBytes, this->caps()->transferBufferAlignment());
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001364 size_t size = rowBytes * rect.height();
Greg Daniel2e967df2021-02-08 10:38:31 -05001365 // By using kStream_GrAccessPattern here, we are not able to cache and reuse the buffer for
1366 // multiple reads. Switching to kDynamic_GrAccessPattern would allow for this, however doing
1367 // so causes a crash in a chromium test. See skbug.com/11297
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001368 auto buffer = direct->priv().resourceProvider()->createBuffer(
Greg Daniel393debc2021-02-06 03:37:20 +00001369 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001370 if (!buffer) {
1371 return {};
1372 }
1373 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -05001374 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001375 if (flip) {
1376 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1377 this->height() - rect.fTop);
1378 }
Greg Danielbbfec9d2019-08-20 10:56:51 -04001379 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001380 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -04001381 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001382 PixelTransferResult result;
1383 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001384 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -04001385 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001386 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
1387 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001388 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
1389 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon5392c942021-03-30 16:14:37 -04001390 GrConvertPixels( GrPixmap(dstInfo, dst, dstInfo.minRowBytes()),
1391 GrCPixmap(srcInfo, src, srcInfo.minRowBytes()));
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001392 };
1393 }
1394 return result;
1395}
Greg Daniel46e366a2019-12-16 14:38:36 -05001396
1397#ifdef SK_DEBUG
1398void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001399 SkASSERT(fReadView.proxy());
1400 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -04001401 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
1402 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
1403 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
1404 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001405 this->onValidate();
1406}
1407#endif