blob: bea720675311526227cd86158e0f43f2ae2a77e1 [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"
Brian Salomon1c86b632020-12-11 12:36:01 -050018#include "src/gpu/GrColorSpaceXform.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040019#include "src/gpu/GrDataUtils.h"
Adlai Hollera0693042020-10-14 11:23:11 -040020#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrDrawingManager.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040022#include "src/gpu/GrGpu.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040023#include "src/gpu/GrImageInfo.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040024#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050026#include "src/gpu/GrSurfaceDrawContext.h"
Brian Salomonbacbb922021-01-21 19:48:00 -050027#include "src/gpu/GrSurfaceFillContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/SkGr.h"
Brian Salomone9ad9982019-07-22 16:17:41 -040029#include "src/gpu/effects/GrBicubicEffect.h"
Brian Salomon63a0a752020-06-26 13:32:09 -040030#include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h"
Brian Osman45580d32016-11-23 09:37:01 -050031
Brian Salomond63638b2021-03-05 14:00:07 -050032#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
33#define RETURN_FALSE_IF_ABANDONED if (this->fContext->abandoned()) { return false; }
34#define RETURN_NULLPTR_IF_ABANDONED if (this->fContext->abandoned()) { return nullptr; }
Brian Osman45580d32016-11-23 09:37:01 -050035
Greg Danielbfa19c42019-12-19 16:41:40 -050036std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -050037 GrSurfaceProxyView readView,
Brian Salomon14f99fc2020-12-07 12:19:47 -050038 const GrColorInfo& info) {
Greg Daniele20fcad2020-01-08 11:52:34 -050039 // It is probably not necessary to check if the context is abandoned here since uses of the
40 // GrSurfaceContext which need the context will mostly likely fail later on without an issue.
41 // However having this hear adds some reassurance in case there is a path doesn't handle an
42 // abandoned context correctly. It also lets us early out of some extra work.
Robert Phillips9eb00022020-06-30 15:30:12 -040043 if (context->abandoned()) {
Greg Daniele20fcad2020-01-08 11:52:34 -050044 return nullptr;
45 }
Greg Daniel3912a4b2020-01-14 09:56:04 -050046 GrSurfaceProxy* proxy = readView.proxy();
Greg Danielbfa19c42019-12-19 16:41:40 -050047 SkASSERT(proxy && proxy->asTextureProxy());
48
Greg Danielbfa19c42019-12-19 16:41:40 -050049 std::unique_ptr<GrSurfaceContext> surfaceContext;
Greg Daniel3912a4b2020-01-14 09:56:04 -050050 if (proxy->asRenderTargetProxy()) {
Brian Salomon8afde5f2020-04-01 16:22:00 -040051 // Will we ever want a swizzle that is not the default write swizzle for the format and
Greg Danielbfa19c42019-12-19 16:41:40 -050052 // colorType here? If so we will need to manually pass that in.
Brian Salomonc5243782020-04-02 12:50:34 -040053 GrSwizzle writeSwizzle;
Brian Salomon14f99fc2020-12-07 12:19:47 -050054 if (info.colorType() != GrColorType::kUnknown) {
55 writeSwizzle = context->priv().caps()->getWriteSwizzle(proxy->backendFormat(),
56 info.colorType());
Brian Salomonc5243782020-04-02 12:50:34 -040057 }
Brian Salomon8afde5f2020-04-01 16:22:00 -040058 GrSurfaceProxyView writeView(readView.refProxy(), readView.origin(), writeSwizzle);
Brian Salomon590f5672020-12-16 11:44:47 -050059 if (info.alphaType() == kPremul_SkAlphaType || info.alphaType() == kOpaque_SkAlphaType) {
60 surfaceContext = std::make_unique<GrSurfaceDrawContext>(context,
61 std::move(readView),
62 std::move(writeView),
63 info.colorType(),
64 info.refColorSpace(),
65 /*surface props*/ nullptr);
66 } else {
67 surfaceContext = std::make_unique<GrSurfaceFillContext>(context,
68 std::move(readView),
69 std::move(writeView),
70 info);
71 }
Greg Danielbfa19c42019-12-19 16:41:40 -050072 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -050073 surfaceContext = std::make_unique<GrSurfaceContext>(context, std::move(readView), info);
Greg Danielbfa19c42019-12-19 16:41:40 -050074 }
Robert Phillips07f0e412020-01-17 15:20:00 -050075 SkDEBUGCODE(surfaceContext->validate();)
Greg Danielbfa19c42019-12-19 16:41:40 -050076 return surfaceContext;
77}
78
Brian Salomona56a7462020-02-07 14:17:25 -050079std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
Brian Salomon14f99fc2020-12-07 12:19:47 -050080 const GrImageInfo& info,
Brian Salomona56a7462020-02-07 14:17:25 -050081 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050082 SkBackingFit fit,
Brian Salomon14f99fc2020-12-07 12:19:47 -050083 GrSurfaceOrigin origin,
84 GrRenderable renderable,
85 int sampleCount,
86 GrMipmapped mipmapped,
87 GrProtected isProtected,
Brian Salomona56a7462020-02-07 14:17:25 -050088 SkBudgeted budgeted) {
Brian Salomon14f99fc2020-12-07 12:19:47 -050089 SkASSERT(context);
90 SkASSERT(renderable == GrRenderable::kYes || sampleCount == 1);
91 if (context->abandoned()) {
92 return nullptr;
Brian Salomond005b692020-04-01 15:47:05 -040093 }
Brian Salomon14f99fc2020-12-07 12:19:47 -050094 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(format,
95 info.dimensions(),
96 renderable,
97 sampleCount,
98 mipmapped,
99 fit,
100 budgeted,
101 isProtected);
Greg Danielbfa19c42019-12-19 16:41:40 -0500102 if (!proxy) {
103 return nullptr;
104 }
105
Brian Salomon14f99fc2020-12-07 12:19:47 -0500106 GrSwizzle swizzle;
107 if (info.colorType() != GrColorType::kUnknown &&
108 !context->priv().caps()->isFormatCompressed(format)) {
109 swizzle = context->priv().caps()->getReadSwizzle(format, info.colorType());
110 }
111
Greg Daniel3912a4b2020-01-14 09:56:04 -0500112 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500113 return GrSurfaceContext::Make(context, std::move(view), info.colorInfo());
114}
115
116std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
117 const GrImageInfo& info,
118 SkBackingFit fit,
119 GrSurfaceOrigin origin,
120 GrRenderable renderable,
121 int sampleCount,
122 GrMipmapped mipmapped,
123 GrProtected isProtected,
124 SkBudgeted budgeted) {
125 GrBackendFormat format = context->priv().caps()->getDefaultBackendFormat(info.colorType(),
126 renderable);
127 return Make(context,
128 info,
129 format,
130 fit,
131 origin,
132 renderable,
133 sampleCount,
134 mipmapped,
135 isProtected,
136 budgeted);
Greg Danielbfa19c42019-12-19 16:41:40 -0500137}
138
Robert Phillips69893702019-02-22 11:16:30 -0500139GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500140 GrSurfaceProxyView readView,
Brian Salomon14f99fc2020-12-07 12:19:47 -0500141 const GrColorInfo& info)
142 : fContext(context), fReadView(std::move(readView)), fColorInfo(info) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400143 SkASSERT(!context->abandoned());
Greg Daniele20fcad2020-01-08 11:52:34 -0500144}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400145
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400146const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); }
147
Robert Phillips0d075de2019-03-04 11:08:13 -0500148GrAuditTrail* GrSurfaceContext::auditTrail() {
149 return fContext->priv().auditTrail();
150}
151
152GrDrawingManager* GrSurfaceContext::drawingManager() {
153 return fContext->priv().drawingManager();
154}
155
156const GrDrawingManager* GrSurfaceContext::drawingManager() const {
157 return fContext->priv().drawingManager();
158}
159
160#ifdef SK_DEBUG
Brian Salomon70fe17e2020-11-30 14:33:58 -0500161GrSingleOwner* GrSurfaceContext::singleOwner() const { return fContext->priv().singleOwner(); }
Robert Phillips0d075de2019-03-04 11:08:13 -0500162#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400163
Brian Salomondd4087d2020-12-23 20:36:44 -0500164static bool alpha_types_compatible(SkAlphaType srcAlphaType, SkAlphaType dstAlphaType) {
165 // If both alpha types are kUnknown things make sense. If not, it's too underspecified.
166 return (srcAlphaType == kUnknown_SkAlphaType) == (dstAlphaType == kUnknown_SkAlphaType);
167}
168
169bool GrSurfaceContext::readPixels(GrDirectContext* dContext, GrPixmap dst, SkIPoint pt) {
Brian Salomon1d435302019-07-01 13:05:28 -0400170 ASSERT_SINGLE_OWNER
171 RETURN_FALSE_IF_ABANDONED
172 SkDEBUGCODE(this->validate();)
173 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400174 if (!fContext->priv().matches(dContext)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400175 return false;
176 }
Brian Salomon46f63232021-01-25 10:13:35 -0500177
178 if (dst.colorType() == GrColorType::kUnknown) {
179 return false;
180 }
181
Brian Salomonc24c8ef2021-02-01 13:32:30 -0500182 if (dst.rowBytes() % dst.info().bpp()) {
183 return false;
184 }
185
Brian Salomondd4087d2020-12-23 20:36:44 -0500186 dst = dst.clip(this->dimensions(), &pt);
187 if (!dst.hasPixels()) {
Brian Salomon1d435302019-07-01 13:05:28 -0400188 return false;
189 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500190 if (!alpha_types_compatible(this->colorInfo().alphaType(), dst.alphaType())) {
Brian Salomon1d435302019-07-01 13:05:28 -0400191 return false;
192 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500193 // We allow unknown alpha types but only if both src and dst are unknown. Otherwise, it's too
194 // weird to reason about what should be expected.
Greg Daniel6eb8c242019-06-05 10:22:24 -0400195
Brian Salomon982127b2021-01-21 10:43:35 -0500196 sk_sp<GrSurfaceProxy> srcProxy = this->asSurfaceProxyRef();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400197
Stephen White3c0a50f2020-01-16 18:19:54 -0500198 if (srcProxy->framebufferOnly()) {
199 return false;
200 }
201
Greg Daniel6eb8c242019-06-05 10:22:24 -0400202 // MDB TODO: delay this instantiation until later in the method
Adlai Hollerc95b5892020-08-11 12:02:22 -0400203 if (!srcProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400204 return false;
205 }
206
207 GrSurface* srcSurface = srcProxy->peekSurface();
208
Brian Salomondd4087d2020-12-23 20:36:44 -0500209 SkColorSpaceXformSteps::Flags flags =
210 SkColorSpaceXformSteps{this->colorInfo(), dst.info()}.flags;
Mike Klein7321e6a2019-12-03 11:08:40 -0600211 bool unpremul = flags.unpremul,
212 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
213 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400214
Adlai Hollerc95b5892020-08-11 12:02:22 -0400215 const GrCaps* caps = dContext->priv().caps();
Robert Phillips07f0e412020-01-17 15:20:00 -0500216 bool srcIsCompressed = caps->isFormatCompressed(srcSurface->backendFormat());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400217 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
218 // care so much about getImageData performance. However, in order to ensure putImageData/
219 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
220 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
221 // fContext->vaildaPMUPMConversionExists()).
Greg Daniel0258c902019-08-01 13:08:33 -0400222 GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
223 GrRenderable::kYes);
Greg Danielc71c7962020-01-14 16:44:18 -0500224 GrColorType srcColorType = this->colorInfo().colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400225 bool canvas2DFastPath = unpremul && !needColorConversion &&
Brian Salomondd4087d2020-12-23 20:36:44 -0500226 (GrColorType::kRGBA_8888 == dst.colorType() ||
227 GrColorType::kBGRA_8888 == dst.colorType()) &&
Brian Salomon1d435302019-07-01 13:05:28 -0400228 SkToBool(srcProxy->asTextureProxy()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500229 (srcColorType == GrColorType::kRGBA_8888 ||
230 srcColorType == GrColorType::kBGRA_8888) &&
Greg Daniel0258c902019-08-01 13:08:33 -0400231 defaultRGBAFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400232 dContext->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400233
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400234 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400235 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400236 return false;
237 }
238
Brian Salomondc0710f2019-07-01 14:59:32 -0400239 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400240 std::unique_ptr<GrSurfaceContext> tempCtx;
241 if (this->asTextureProxy()) {
242 GrColorType colorType = (canvas2DFastPath || srcIsCompressed)
243 ? GrColorType::kRGBA_8888
244 : this->colorInfo().colorType();
Brian Salomondd4087d2020-12-23 20:36:44 -0500245 SkAlphaType alphaType = canvas2DFastPath ? dst.alphaType()
Brian Salomon590f5672020-12-16 11:44:47 -0500246 : this->colorInfo().alphaType();
247 GrImageInfo tempInfo(colorType,
248 alphaType,
249 this->colorInfo().refColorSpace(),
Brian Salomondd4087d2020-12-23 20:36:44 -0500250 dst.dimensions());
Brian Salomon590f5672020-12-16 11:44:47 -0500251 auto sfc = GrSurfaceFillContext::Make(dContext, tempInfo, SkBackingFit::kApprox);
252 if (!sfc) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400253 return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400254 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400255
256 std::unique_ptr<GrFragmentProcessor> fp;
257 if (canvas2DFastPath) {
258 fp = dContext->priv().createPMToUPMEffect(GrTextureEffect::Make(
259 this->readSurfaceView(), this->colorInfo().alphaType()));
Brian Salomondd4087d2020-12-23 20:36:44 -0500260 if (dst.colorType() == GrColorType::kBGRA_8888) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400261 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
Brian Salomondd4087d2020-12-23 20:36:44 -0500262 dst = GrPixmap(dst.info().makeColorType(GrColorType::kRGBA_8888),
263 dst.addr(),
264 dst.rowBytes());
Brian Salomon72c7b982020-10-06 10:07:38 -0400265 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400266 } else {
267 fp = GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType());
268 }
269 if (!fp) {
270 return false;
271 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500272 sfc->fillRectToRectWithFP(SkIRect::MakePtSize(pt, dst.dimensions()),
273 SkIRect::MakeSize(dst.dimensions()),
Brian Salomon590f5672020-12-16 11:44:47 -0500274 std::move(fp));
Brian Salomon72c7b982020-10-06 10:07:38 -0400275 pt = {0, 0};
Brian Salomon590f5672020-12-16 11:44:47 -0500276 tempCtx = std::move(sfc);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400277 } else {
Brian Salomon72c7b982020-10-06 10:07:38 -0400278 auto restrictions = this->caps()->getDstCopyRestrictions(this->asRenderTargetProxy(),
279 this->colorInfo().colorType());
280 sk_sp<GrSurfaceProxy> copy;
281 static constexpr auto kFit = SkBackingFit::kExact;
282 static constexpr auto kBudgeted = SkBudgeted::kYes;
283 static constexpr auto kMipMapped = GrMipMapped::kNo;
284 if (restrictions.fMustCopyWholeSrc) {
Brian Salomon982127b2021-01-21 10:43:35 -0500285 copy = GrSurfaceProxy::Copy(fContext,
286 std::move(srcProxy),
287 this->origin(),
288 kMipMapped,
289 kFit,
Brian Salomon72c7b982020-10-06 10:07:38 -0400290 kBudgeted);
291 } else {
Brian Salomondd4087d2020-12-23 20:36:44 -0500292 auto srcRect = SkIRect::MakePtSize(pt, dst.dimensions());
Brian Salomon982127b2021-01-21 10:43:35 -0500293 copy = GrSurfaceProxy::Copy(fContext,
294 std::move(srcProxy),
295 this->origin(),
296 kMipMapped,
297 srcRect,
298 kFit,
299 kBudgeted,
300 restrictions.fRectsMustMatch);
Brian Salomon72c7b982020-10-06 10:07:38 -0400301 pt = {0, 0};
302 }
303 if (!copy) {
304 return false;
305 }
306 GrSurfaceProxyView view{std::move(copy), this->origin(), this->readSwizzle()};
Brian Salomon14f99fc2020-12-07 12:19:47 -0500307 tempCtx = GrSurfaceContext::Make(dContext, std::move(view), this->colorInfo());
Brian Salomon72c7b982020-10-06 10:07:38 -0400308 SkASSERT(tempCtx);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400309 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500310 return tempCtx->readPixels(dContext, dst, pt);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400311 }
312
Greg Danielb8d84f82020-02-13 14:25:00 -0500313 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400314
Brian Salomon1d435302019-07-01 13:05:28 -0400315 auto supportedRead = caps->supportedReadPixelsColorType(
Brian Salomondd4087d2020-12-23 20:36:44 -0500316 this->colorInfo().colorType(), srcProxy->backendFormat(), dst.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400317
Brian Salomondd4087d2020-12-23 20:36:44 -0500318 bool makeTight =
319 !caps->readPixelsRowBytesSupport() && dst.rowBytes() != dst.info().minRowBytes();
Brian Salomon1047a492019-07-02 12:25:21 -0400320
321 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomondd4087d2020-12-23 20:36:44 -0500322 (dst.colorType() != supportedRead.fColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400323
Brian Salomonf30b1c12019-06-20 12:25:02 -0400324 std::unique_ptr<char[]> tmpPixels;
Brian Salomondd4087d2020-12-23 20:36:44 -0500325 GrPixmap tmp;
326 void* readDst = dst.addr();
327 size_t readRB = dst.rowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400328 if (convert) {
Brian Salomondd4087d2020-12-23 20:36:44 -0500329 GrImageInfo tmpInfo(supportedRead.fColorType,
330 this->colorInfo().alphaType(),
331 this->colorInfo().refColorSpace(),
332 dst.dimensions());
Brian Salomon1d435302019-07-01 13:05:28 -0400333 size_t tmpRB = tmpInfo.minRowBytes();
334 size_t size = tmpRB * tmpInfo.height();
335 // Chrome MSAN bots require the data to be initialized (hence the ()).
John Stilesfbd050b2020-08-03 13:21:46 -0400336 tmpPixels = std::make_unique<char[]>(size);
Brian Salomondd4087d2020-12-23 20:36:44 -0500337 tmp = {tmpInfo, tmpPixels.get(), tmpRB};
Brian Salomonf30b1c12019-06-20 12:25:02 -0400338
Brian Salomonf30b1c12019-06-20 12:25:02 -0400339 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400340 readRB = tmpRB;
Brian Salomondd4087d2020-12-23 20:36:44 -0500341 pt.fY = flip ? srcSurface->height() - pt.fY - dst.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400342 }
343
Brian Salomon982127b2021-01-21 10:43:35 -0500344 dContext->priv().flushSurface(srcProxy.get());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400345 dContext->submit();
Brian Salomondd4087d2020-12-23 20:36:44 -0500346 if (!dContext->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dst.width(), dst.height(),
347 this->colorInfo().colorType(),
Adlai Hollerc95b5892020-08-11 12:02:22 -0400348 supportedRead.fColorType, readDst, readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400349 return false;
350 }
351
Brian Salomondd4087d2020-12-23 20:36:44 -0500352 if (tmp.hasPixels()) {
353 return GrConvertPixels(dst, tmp, flip);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400354 }
355 return true;
356}
Robert Phillips0d075de2019-03-04 11:08:13 -0500357
Brian Salomonea1d39b2021-04-01 17:06:52 -0400358bool GrSurfaceContext::writePixels(GrDirectContext* dContext,
359 GrCPixmap src,
Brian Salomon75ee7372021-04-06 15:04:35 -0400360 SkIPoint dstPt) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400361 ASSERT_SINGLE_OWNER
362 RETURN_FALSE_IF_ABANDONED
363 SkDEBUGCODE(this->validate();)
Brian Salomon2c673402021-04-02 14:36:58 -0400364
365 src = src.clip(this->dimensions(), &dstPt);
366 if (!src.hasPixels()) {
367 return false;
368 }
369 if (!src.info().bpp() || src.rowBytes() % src.info().bpp()) {
370 return false;
371 }
Brian Salomon75ee7372021-04-06 15:04:35 -0400372 return this->internalWritePixels(dContext, &src, 1, dstPt);
Brian Salomon2c673402021-04-02 14:36:58 -0400373}
374
375bool GrSurfaceContext::writePixels(GrDirectContext* dContext,
376 const GrCPixmap src[],
Brian Salomon75ee7372021-04-06 15:04:35 -0400377 int numLevels) {
Brian Salomon2c673402021-04-02 14:36:58 -0400378 ASSERT_SINGLE_OWNER
379 RETURN_FALSE_IF_ABANDONED
380 SkDEBUGCODE(this->validate();)
381
382 SkASSERT(dContext);
383 SkASSERT(numLevels >= 1);
384 SkASSERT(src);
385
386 if (numLevels == 1) {
387 if (src->dimensions() != this->dimensions()) {
388 return false;
389 }
Brian Salomon75ee7372021-04-06 15:04:35 -0400390 return this->writePixels(dContext, src[0], {0, 0});
Brian Salomon2c673402021-04-02 14:36:58 -0400391 }
392 if (!this->asTextureProxy() || this->asTextureProxy()->proxyMipmapped() == GrMipmapped::kNo) {
393 return false;
394 }
395
396 SkISize dims = this->dimensions();
397 if (numLevels != SkMipmap::ComputeLevelCount(dims) + 1) {
398 return false;
399 }
400 for (int i = 0; i < numLevels; ++i) {
401 if (src[i].colorInfo() != src[0].colorInfo()) {
402 return false;
403 }
404 if (dims != src[i].dimensions()) {
405 return false;
406 }
407 if (!src[i].info().bpp() || src[i].rowBytes() % src[i].info().bpp()) {
408 return false;
409 }
410 dims = {std::max(1, dims.width()/2), std::max(1, dims.height()/2)};
411 }
Brian Salomon75ee7372021-04-06 15:04:35 -0400412 return this->internalWritePixels(dContext, src, numLevels, {0, 0});
Brian Salomon2c673402021-04-02 14:36:58 -0400413}
414
415bool GrSurfaceContext::internalWritePixels(GrDirectContext* dContext,
416 const GrCPixmap src[],
417 int numLevels,
Brian Salomon75ee7372021-04-06 15:04:35 -0400418 SkIPoint pt) {
Brian Salomon2c673402021-04-02 14:36:58 -0400419 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::internalWritePixels");
420
421 SkASSERT(numLevels >= 1);
422 SkASSERT(src);
423
424 // We can either write to a subset or write MIP levels, but not both.
425 SkASSERT((src[0].dimensions() == this->dimensions() && pt.isZero()) || numLevels == 1);
426 SkASSERT(numLevels == 1 ||
Brian Salomon7cde6c32021-04-02 15:53:42 -0400427 (this->asTextureProxy() && this->asTextureProxy()->mipmapped() == GrMipmapped::kYes));
Brian Salomon2c673402021-04-02 14:36:58 -0400428 // Our public caller should have clipped to the bounds of the surface already.
Brian Salomonea1d39b2021-04-01 17:06:52 -0400429 SkASSERT(SkIRect::MakeSize(this->dimensions()).contains(
430 SkIRect::MakePtSize(pt, src[0].dimensions())));
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400431
Adlai Hollerc95b5892020-08-11 12:02:22 -0400432 if (!dContext) {
Brian Salomonc320b152018-02-20 14:05:36 -0500433 return false;
434 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500435
Brian Salomon1d435302019-07-01 13:05:28 -0400436 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500437 return false;
438 }
439
Brian Salomon2c673402021-04-02 14:36:58 -0400440 if (src[0].colorType() == GrColorType::kUnknown) {
Brian Salomon46f63232021-01-25 10:13:35 -0500441 return false;
442 }
443
Brian Salomon2c673402021-04-02 14:36:58 -0400444 if (!alpha_types_compatible(src[0].alphaType(), this->colorInfo().alphaType())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400445 return false;
446 }
447
448 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
Stephen White3c0a50f2020-01-16 18:19:54 -0500449
450 if (dstProxy->framebufferOnly()) {
451 return false;
452 }
453
Adlai Hollerc95b5892020-08-11 12:02:22 -0400454 if (!dstProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400455 return false;
456 }
457
458 GrSurface* dstSurface = dstProxy->peekSurface();
459
Brian Salomondd4087d2020-12-23 20:36:44 -0500460 SkColorSpaceXformSteps::Flags flags =
Brian Salomon2c673402021-04-02 14:36:58 -0400461 SkColorSpaceXformSteps{src[0].colorInfo(), this->colorInfo()}.flags;
Mike Klein7321e6a2019-12-03 11:08:40 -0600462 bool unpremul = flags.unpremul,
463 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
464 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400465
Adlai Hollerc95b5892020-08-11 12:02:22 -0400466 const GrCaps* caps = dContext->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400467
468 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
469 GrRenderable::kNo);
470
Greg Danielc71c7962020-01-14 16:44:18 -0500471 GrColorType dstColorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400472 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
473 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400474 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
Brian Salomon2c673402021-04-02 14:36:58 -0400475 (src[0].colorType() == GrColorType::kRGBA_8888 ||
476 src[0].colorType() == GrColorType::kBGRA_8888) &&
Brian Salomon590f5672020-12-16 11:44:47 -0500477 this->asFillContext() &&
Greg Danielc71c7962020-01-14 16:44:18 -0500478 (dstColorType == GrColorType::kRGBA_8888 ||
479 dstColorType == GrColorType::kBGRA_8888) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400480 rgbaDefaultFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400481 dContext->priv().validPMUPMConversionExists();
Brian Salomon2c673402021-04-02 14:36:58 -0400482 // Drawing code path doesn't support writing to levels and doesn't support inserting layout
483 // transitions.
Brian Salomon75ee7372021-04-06 15:04:35 -0400484 if ((!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) && numLevels == 1) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500485 GrColorInfo tempColorInfo;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400486 GrBackendFormat format;
Greg Danielbfa19c42019-12-19 16:41:40 -0500487 GrSwizzle tempReadSwizzle;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400488 if (canvas2DFastPath) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500489 tempColorInfo = {GrColorType::kRGBA_8888,
490 kUnpremul_SkAlphaType,
491 this->colorInfo().refColorSpace()};
Greg Daniel7bfc9132019-08-14 14:23:53 -0400492 format = rgbaDefaultFormat;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400493 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500494 tempColorInfo = this->colorInfo();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400495 format = dstProxy->backendFormat().makeTexture2D();
496 if (!format.isValid()) {
497 return false;
498 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500499 tempReadSwizzle = this->readSwizzle();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400500 }
501
Greg Daniel2e52ad12019-06-13 10:04:16 -0400502 // It is more efficient for us to write pixels into a top left origin so we prefer that.
503 // However, if the final proxy isn't a render target then we must use a copy to move the
504 // data into it which requires the origins to match. If the final proxy is a render target
505 // we can use a draw instead which doesn't have this origin restriction. Thus for render
506 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400507 GrSurfaceOrigin tempOrigin =
Brian Salomon590f5672020-12-16 11:44:47 -0500508 this->asFillContext() ? kTopLeft_GrSurfaceOrigin : this->origin();
Brian Salomon2c673402021-04-02 14:36:58 -0400509 auto tempProxy = dContext->priv().proxyProvider()->createProxy(format,
510 src[0].dimensions(),
511 GrRenderable::kNo,
512 1,
513 GrMipmapped::kNo,
514 SkBackingFit::kApprox,
515 SkBudgeted::kYes,
516 GrProtected::kNo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400517 if (!tempProxy) {
518 return false;
519 }
Greg Daniel3912a4b2020-01-14 09:56:04 -0500520 GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500521 GrSurfaceContext tempCtx(dContext, tempView, tempColorInfo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400522
523 // In the fast path we always write the srcData to the temp context as though it were RGBA.
524 // When the data is really BGRA the write will cause the R and B channels to be swapped in
525 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
526 // dst.
Brian Salomon2c673402021-04-02 14:36:58 -0400527 GrCPixmap origSrcBase = src[0];
528 GrCPixmap srcBase = origSrcBase;
Brian Salomon1d435302019-07-01 13:05:28 -0400529 if (canvas2DFastPath) {
Brian Salomon2c673402021-04-02 14:36:58 -0400530 srcBase = GrCPixmap(origSrcBase.info().makeColorType(GrColorType::kRGBA_8888),
531 origSrcBase.addr(),
532 origSrcBase.rowBytes());
Brian Salomon1d435302019-07-01 13:05:28 -0400533 }
Brian Salomon2c673402021-04-02 14:36:58 -0400534 if (!tempCtx.writePixels(dContext, srcBase, {0, 0})) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400535 return false;
536 }
537
Brian Salomon590f5672020-12-16 11:44:47 -0500538 if (this->asFillContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400539 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400540 if (canvas2DFastPath) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400541 fp = dContext->priv().createUPMToPMEffect(
Brian Salomon14f99fc2020-12-07 12:19:47 -0500542 GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType()));
Brian Salomon1d435302019-07-01 13:05:28 -0400543 // Important: check the original src color type here!
Brian Salomon2c673402021-04-02 14:36:58 -0400544 if (origSrcBase.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400545 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
546 }
547 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500548 fp = GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400549 }
550 if (!fp) {
551 return false;
552 }
Brian Salomon2c673402021-04-02 14:36:58 -0400553 this->asFillContext()->fillRectToRectWithFP(
554 SkIRect::MakeSize(srcBase.dimensions()),
555 SkIRect::MakePtSize(pt, srcBase.dimensions()),
556 std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400557 } else {
Brian Salomon2c673402021-04-02 14:36:58 -0400558 SkIRect srcRect = SkIRect::MakeSize(srcBase.dimensions());
Brian Salomon1d435302019-07-01 13:05:28 -0400559 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Brian Salomon982127b2021-01-21 10:43:35 -0500560 if (!this->copy(std::move(tempProxy), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400561 return false;
562 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400563 }
564 return true;
565 }
566
Brian Salomon2c673402021-04-02 14:36:58 -0400567 GrColorType srcColorType = src[0].colorType();
568 auto [allowedColorType, _] =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400569 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400570 dstProxy->backendFormat(),
Brian Salomon2c673402021-04-02 14:36:58 -0400571 srcColorType);
Greg Danielb8d84f82020-02-13 14:25:00 -0500572 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400573
Brian Salomon2c673402021-04-02 14:36:58 -0400574 bool convertAll = premul ||
575 unpremul ||
576 needColorConversion ||
577 flip ||
578 (srcColorType != allowedColorType);
579 bool mustBeTight = !caps->writePixelsRowBytesSupport();
580 size_t tmpSize = 0;
581 if (mustBeTight || convertAll) {
582 for (int i = 0; i < numLevels; ++i) {
583 if (convertAll || (mustBeTight && src[i].rowBytes() != src[i].info().minRowBytes())) {
584 tmpSize += src[i].info().makeColorType(allowedColorType).minRowBytes()*
585 src[i].height();
586 }
587 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400588 }
589
Brian Salomon2c673402021-04-02 14:36:58 -0400590 auto tmpData = tmpSize ? SkData::MakeUninitialized(tmpSize) : nullptr;
591 void* tmp = tmpSize ? tmpData->writable_data() : nullptr;
592 SkAutoSTArray<15, GrMipLevel> srcLevels(numLevels);
593 bool ownAllStorage = true;
594 for (int i = 0; i < numLevels; ++i) {
595 if (convertAll || (mustBeTight && src[i].rowBytes() != src[i].info().minRowBytes())) {
596 GrImageInfo tmpInfo(allowedColorType,
597 this->colorInfo().alphaType(),
598 this->colorInfo().refColorSpace(),
599 src[i].dimensions());
600 auto tmpRB = tmpInfo.minRowBytes();
601 GrPixmap tmpPM(tmpInfo, tmp, tmpRB);
602 SkAssertResult(GrConvertPixels(tmpPM, src[i], flip));
603 srcLevels[i] = {tmpPM.addr(), tmpPM.rowBytes(), tmpData};
604 tmp = SkTAddOffset<void>(tmp, tmpRB*tmpPM.height());
605 } else {
606 srcLevels[i] = {src[i].addr(), src[i].rowBytes(), src[i].pixelStorage()};
607 ownAllStorage &= src[i].ownsPixels();
608 }
609 }
610 pt.fY = flip ? dstSurface->height() - pt.fY - src[0].height() : pt.fY;
611
612 if (!dContext->priv().drawingManager()->newWritePixelsTask(
Brian Salomon974c8212021-04-05 16:24:00 -0400613 sk_ref_sp(dstProxy),
614 SkIRect::MakePtSize(pt, src[0].dimensions()),
615 allowedColorType,
616 this->colorInfo().colorType(),
617 srcLevels.begin(),
Brian Salomon75ee7372021-04-06 15:04:35 -0400618 numLevels)) {
Brian Salomon2c673402021-04-02 14:36:58 -0400619 return false;
620 }
621 if (numLevels > 1) {
622 dstProxy->asTextureProxy()->markMipmapsClean();
623 }
624 if (!ownAllStorage) {
625 // If any pixmap doesn't own its pixels then we must flush so that the pixels are pushed to
626 // the GPU before we return.
Brian Salomon24949422021-02-11 09:39:46 -0500627 dContext->priv().flushSurface(dstProxy);
628 }
Brian Salomon2c673402021-04-02 14:36:58 -0400629 return true;
Brian Osman45580d32016-11-23 09:37:01 -0500630}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400631
Adlai Hollerc95b5892020-08-11 12:02:22 -0400632void GrSurfaceContext::asyncRescaleAndReadPixels(GrDirectContext* dContext,
633 const SkImageInfo& info,
Brian Salomon63a0a752020-06-26 13:32:09 -0400634 const SkIRect& srcRect,
635 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500636 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400637 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400638 ReadPixelsContext callbackContext) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400639 if (!dContext) {
640 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400641 return;
642 }
643 auto rt = this->asRenderTargetProxy();
644 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400645 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400646 return;
647 }
648 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400649 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400650 return;
651 }
652 auto dstCT = SkColorTypeToGrColorType(info.colorType());
653 if (dstCT == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400654 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400655 return;
656 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500657 bool needsRescale = srcRect.size() != info.dimensions();
Brian Salomon63a0a752020-06-26 13:32:09 -0400658 auto colorTypeOfFinalContext = this->colorInfo().colorType();
659 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
660 if (needsRescale) {
661 colorTypeOfFinalContext = dstCT;
662 backendFormatOfFinalContext =
663 this->caps()->getDefaultBackendFormat(dstCT, GrRenderable::kYes);
664 }
665 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonbacbb922021-01-21 19:48:00 -0500666 backendFormatOfFinalContext,
667 dstCT);
Brian Salomon63a0a752020-06-26 13:32:09 -0400668 // Fail if we can't read from the source surface's color type.
669 if (readInfo.fColorType == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400670 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400671 return;
672 }
673 // Fail if read color type does not have all of dstCT's color channels and those missing color
674 // channels are in the src.
675 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
676 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
677 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
678 if ((~legalReadChannels & dstChannels) & srcChannels) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400679 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400680 return;
681 }
682
Brian Salomonbacbb922021-01-21 19:48:00 -0500683 std::unique_ptr<GrSurfaceFillContext> tempFC;
Brian Salomon63a0a752020-06-26 13:32:09 -0400684 int x = srcRect.fLeft;
685 int y = srcRect.fTop;
686 if (needsRescale) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500687 tempFC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma, rescaleMode);
688 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400689 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400690 return;
691 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500692 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
693 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400694 x = y = 0;
695 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -0500696 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo(),
697 info.colorInfo());
Brian Salomon63a0a752020-06-26 13:32:09 -0400698 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
699 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
700 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomonbacbb922021-01-21 19:48:00 -0500701 SkIRect srcRectToDraw = srcRect;
Brian Salomon63a0a752020-06-26 13:32:09 -0400702 // If the src is not texturable first try to make a copy to a texture.
703 if (!texProxyView.asTextureProxy()) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500704 texProxyView = GrSurfaceProxyView::Copy(fContext,
705 texProxyView,
706 GrMipmapped::kNo,
707 srcRect,
708 SkBackingFit::kApprox,
709 SkBudgeted::kNo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400710 if (!texProxyView) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400711 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400712 return;
713 }
714 SkASSERT(texProxyView.asTextureProxy());
Brian Salomonbacbb922021-01-21 19:48:00 -0500715 srcRectToDraw = SkIRect::MakeSize(srcRect.size());
Brian Salomon63a0a752020-06-26 13:32:09 -0400716 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500717 auto tempInfo = GrImageInfo(info).makeColorType(this->colorInfo().colorType());
718 tempFC = GrSurfaceFillContext::Make(dContext, tempInfo, SkBackingFit::kApprox);
719 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400720 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400721 return;
722 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500723 auto fp = GrTextureEffect::Make(std::move(texProxyView), this->colorInfo().alphaType());
724 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
725 tempFC->fillRectToRectWithFP(srcRectToDraw,
726 SkIRect::MakeSize(tempFC->dimensions()),
727 std::move(fp));
Brian Salomon63a0a752020-06-26 13:32:09 -0400728 x = y = 0;
729 }
730 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500731 auto srcCtx = tempFC ? tempFC.get() : this;
732 return srcCtx->asyncReadPixels(dContext,
733 SkIRect::MakePtSize({x, y}, info.dimensions()),
734 info.colorType(),
735 callback,
736 callbackContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400737}
738
739class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult {
740public:
Robert Phillips82ad7af2021-03-11 16:00:10 -0500741 AsyncReadResult(GrDirectContext::DirectContextID intendedRecipient)
742 : fIntendedRecipient(intendedRecipient) {
743 }
744
Brian Salomon63a0a752020-06-26 13:32:09 -0400745 ~AsyncReadResult() override {
746 for (int i = 0; i < fPlanes.count(); ++i) {
Robert Phillips82ad7af2021-03-11 16:00:10 -0500747 fPlanes[i].releaseMappedBuffer(fIntendedRecipient);
Brian Salomon63a0a752020-06-26 13:32:09 -0400748 }
749 }
750
751 int count() const override { return fPlanes.count(); }
Brian Salomonbe1084b2021-01-26 13:29:30 -0500752 const void* data(int i) const override { return fPlanes[i].data(); }
753 size_t rowBytes(int i) const override { return fPlanes[i].rowBytes(); }
Brian Salomon63a0a752020-06-26 13:32:09 -0400754
755 bool addTransferResult(const PixelTransferResult& result,
756 SkISize dimensions,
757 size_t rowBytes,
758 GrClientMappedBufferManager* manager) {
759 SkASSERT(!result.fTransferBuffer->isMapped());
760 const void* mappedData = result.fTransferBuffer->map();
761 if (!mappedData) {
762 return false;
763 }
764 if (result.fPixelConverter) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500765 size_t size = rowBytes*dimensions.height();
766 sk_sp<SkData> data = SkData::MakeUninitialized(size);
767 result.fPixelConverter(data->writable_data(), mappedData);
768 this->addCpuPlane(std::move(data), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400769 result.fTransferBuffer->unmap();
770 } else {
771 manager->insert(result.fTransferBuffer);
772 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
773 }
774 return true;
775 }
776
Brian Salomonbe1084b2021-01-26 13:29:30 -0500777 void addCpuPlane(sk_sp<SkData> data, size_t rowBytes) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400778 SkASSERT(data);
779 SkASSERT(rowBytes > 0);
Brian Salomonbe1084b2021-01-26 13:29:30 -0500780 fPlanes.emplace_back(std::move(data), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400781 }
782
783private:
784 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
785 SkASSERT(data);
786 SkASSERT(rowBytes > 0);
787 SkASSERT(mappedBuffer);
788 SkASSERT(mappedBuffer->isMapped());
Brian Salomonbe1084b2021-01-26 13:29:30 -0500789 fPlanes.emplace_back(std::move(mappedBuffer), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400790 }
791
Brian Salomonbe1084b2021-01-26 13:29:30 -0500792 class Plane {
793 public:
794 Plane(sk_sp<GrGpuBuffer> buffer, size_t rowBytes)
795 : fMappedBuffer(std::move(buffer)), fRowBytes(rowBytes) {}
796 Plane(sk_sp<SkData> data, size_t rowBytes) : fData(std::move(data)), fRowBytes(rowBytes) {}
797
798 Plane(const Plane&) = delete;
799 Plane(Plane&&) = default;
800
801 ~Plane() { SkASSERT(!fMappedBuffer); }
802
803 Plane& operator=(const Plane&) = delete;
804 Plane& operator=(Plane&&) = default;
805
Robert Phillips82ad7af2021-03-11 16:00:10 -0500806 void releaseMappedBuffer(GrDirectContext::DirectContextID intendedRecipient) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500807 if (fMappedBuffer) {
808 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
Robert Phillips82ad7af2021-03-11 16:00:10 -0500809 {std::move(fMappedBuffer), intendedRecipient});
Brian Salomonbe1084b2021-01-26 13:29:30 -0500810 }
811 }
812
813 const void* data() const {
814 if (fMappedBuffer) {
815 SkASSERT(!fData);
816 SkASSERT(fMappedBuffer->isMapped());
817 return fMappedBuffer->map();
818 }
819 SkASSERT(fData);
820 return fData->data();
821 }
822
823 size_t rowBytes() const { return fRowBytes; }
824
825 private:
826 sk_sp<SkData> fData;
Brian Salomon1eea1ea2021-01-26 18:12:25 +0000827 sk_sp<GrGpuBuffer> fMappedBuffer;
Brian Salomonbe1084b2021-01-26 13:29:30 -0500828 size_t fRowBytes;
Brian Salomon63a0a752020-06-26 13:32:09 -0400829 };
830 SkSTArray<3, Plane> fPlanes;
Robert Phillips82ad7af2021-03-11 16:00:10 -0500831 GrDirectContext::DirectContextID fIntendedRecipient;
Brian Salomon63a0a752020-06-26 13:32:09 -0400832};
833
Adlai Hollerc95b5892020-08-11 12:02:22 -0400834void GrSurfaceContext::asyncReadPixels(GrDirectContext* dContext,
835 const SkIRect& rect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400836 SkColorType colorType,
837 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400838 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400839 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
840 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
841
Adlai Hollerc95b5892020-08-11 12:02:22 -0400842 if (!dContext || this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
843 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400844 return;
845 }
846
Adlai Hollerc95b5892020-08-11 12:02:22 -0400847 auto mappedBufferManager = dContext->priv().clientMappedBufferManager();
Brian Salomon63a0a752020-06-26 13:32:09 -0400848
849 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
850
851 if (!transferResult.fTransferBuffer) {
852 auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
853 this->colorInfo().refColorSpace());
Robert Phillips82ad7af2021-03-11 16:00:10 -0500854 static const GrDirectContext::DirectContextID kInvalid;
855 auto result = std::make_unique<AsyncReadResult>(kInvalid);
Brian Salomonbe1084b2021-01-26 13:29:30 -0500856 GrPixmap pm = GrPixmap::Allocate(ii);
857 result->addCpuPlane(pm.pixelStorage(), pm.rowBytes());
Brian Salomon63a0a752020-06-26 13:32:09 -0400858
Adlai Hollerc95b5892020-08-11 12:02:22 -0400859 SkIPoint pt{rect.fLeft, rect.fTop};
Brian Salomondd4087d2020-12-23 20:36:44 -0500860 if (!this->readPixels(dContext, pm, pt)) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400861 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400862 return;
863 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400864 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400865 return;
866 }
867
868 struct FinishContext {
869 ReadPixelsCallback* fClientCallback;
870 ReadPixelsContext fClientContext;
871 SkISize fSize;
872 SkColorType fColorType;
873 GrClientMappedBufferManager* fMappedBufferManager;
874 PixelTransferResult fTransferResult;
875 };
876 // Assumption is that the caller would like to flush. We could take a parameter or require an
877 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
878 // callback to GrGpu until after the next flush that flushes our op list, though.
879 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400880 callbackContext,
Brian Salomon63a0a752020-06-26 13:32:09 -0400881 rect.size(),
882 colorType,
883 mappedBufferManager,
884 std::move(transferResult)};
885 auto finishCallback = [](GrGpuFinishedContext c) {
886 const auto* context = reinterpret_cast<const FinishContext*>(c);
Robert Phillips82ad7af2021-03-11 16:00:10 -0500887 auto manager = context->fMappedBufferManager;
888 auto result = std::make_unique<AsyncReadResult>(manager->owningDirectContext());
Brian Salomon63a0a752020-06-26 13:32:09 -0400889 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
890 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
Robert Phillips82ad7af2021-03-11 16:00:10 -0500891 manager)) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400892 result.reset();
893 }
894 (*context->fClientCallback)(context->fClientContext, std::move(result));
895 delete context;
896 };
897 GrFlushInfo flushInfo;
898 flushInfo.fFinishedContext = finishContext;
899 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -0500900
901 dContext->priv().flushSurface(this->asSurfaceProxy(),
902 SkSurface::BackendSurfaceAccess::kNoAccess,
903 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400904}
905
Adlai Hollerc95b5892020-08-11 12:02:22 -0400906void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(GrDirectContext* dContext,
907 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400908 sk_sp<SkColorSpace> dstColorSpace,
909 const SkIRect& srcRect,
910 SkISize dstSize,
911 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500912 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400913 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400914 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400915 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
916 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
917 SkASSERT(!dstSize.isZero());
918 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
919
Adlai Hollerc95b5892020-08-11 12:02:22 -0400920 if (!dContext) {
921 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400922 return;
923 }
924 auto rt = this->asRenderTargetProxy();
925 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400926 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400927 return;
928 }
929 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400930 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400931 return;
932 }
933 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400934 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400935 return;
936 }
937 int x = srcRect.fLeft;
938 int y = srcRect.fTop;
939 bool needsRescale = srcRect.size() != dstSize;
940 GrSurfaceProxyView srcView;
Brian Salomonbacbb922021-01-21 19:48:00 -0500941 auto info = SkImageInfo::Make(dstSize,
942 kRGBA_8888_SkColorType,
943 this->colorInfo().alphaType(),
944 dstColorSpace);
Brian Salomon63a0a752020-06-26 13:32:09 -0400945 if (needsRescale) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400946 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomonbacbb922021-01-21 19:48:00 -0500947 auto tempFC = this->rescale(info,
948 kTopLeft_GrSurfaceOrigin,
949 srcRect,
950 rescaleGamma,
951 rescaleMode);
952 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400953 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400954 return;
955 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500956 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
957 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400958 x = y = 0;
Brian Salomonbacbb922021-01-21 19:48:00 -0500959 srcView = tempFC->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400960 } else {
961 srcView = this->readSurfaceView();
962 if (!srcView.asTextureProxy()) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500963 srcView = GrSurfaceProxyView::Copy(fContext,
964 std::move(srcView),
965 GrMipmapped::kNo,
966 srcRect,
967 SkBackingFit::kApprox,
968 SkBudgeted::kYes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400969 if (!srcView) {
970 // If we can't get a texture copy of the contents then give up.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400971 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400972 return;
973 }
974 SkASSERT(srcView.asTextureProxy());
975 x = y = 0;
976 }
977 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomonbacbb922021-01-21 19:48:00 -0500978 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo(),
979 info.colorInfo());
Brian Salomon63a0a752020-06-26 13:32:09 -0400980 if (xform) {
981 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
Brian Salomonbacbb922021-01-21 19:48:00 -0500982 auto tempFC = GrSurfaceFillContext::Make(dContext,
983 info,
984 SkBackingFit::kApprox,
985 1,
986 GrMipmapped::kNo,
987 GrProtected::kNo,
988 kTopLeft_GrSurfaceOrigin);
989 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400990 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400991 return;
992 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500993 auto fp = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType());
994 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
995 tempFC->fillRectToRectWithFP(srcRectToDraw,
996 SkIRect::MakeSize(tempFC->dimensions()),
997 std::move(fp));
998 srcView = tempFC->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400999 SkASSERT(srcView.asTextureProxy());
1000 x = y = 0;
1001 }
1002 }
1003
Brian Salomonbacbb922021-01-21 19:48:00 -05001004 auto yInfo = SkImageInfo::MakeA8(dstSize);
1005 auto yFC = GrSurfaceFillContext::MakeWithFallback(dContext, yInfo, SkBackingFit::kApprox);
1006
1007 auto uvInfo = yInfo.makeWH(yInfo.width()/2, yInfo.height()/2);
1008 auto uFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
1009 auto vFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
1010
1011 if (!yFC || !uFC || !vFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001012 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001013 return;
1014 }
1015
1016 float baseM[20];
1017 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
1018
1019 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
1020
1021 auto texMatrix = SkMatrix::Translate(x, y);
1022
Brian Salomon63a0a752020-06-26 13:32:09 -04001023 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
1024 PixelTransferResult yTransfer, uTransfer, vTransfer;
1025
1026 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
1027 float yM[20];
1028 std::fill_n(yM, 15, 0.f);
1029 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -05001030
1031 auto yFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
1032 yFP = GrColorMatrixFragmentProcessor::Make(std::move(yFP),
1033 yM,
1034 /*unpremulInput=*/false,
1035 /*clampRGBOutput=*/true,
1036 /*premulOutput=*/false);
1037 yFC->fillWithFP(std::move(yFP));
Brian Salomon63a0a752020-06-26 13:32:09 -04001038 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001039 yTransfer = yFC->transferPixels(GrColorType::kAlpha_8,
1040 SkIRect::MakeSize(yFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -04001041 if (!yTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001042 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001043 return;
1044 }
1045 }
1046
1047 texMatrix.preScale(2.f, 2.f);
1048 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
1049 float uM[20];
1050 std::fill_n(uM, 15, 0.f);
1051 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -05001052
1053 auto uFP = GrTextureEffect::Make(srcView,
1054 this->colorInfo().alphaType(),
1055 texMatrix,
1056 GrSamplerState::Filter::kLinear);
1057 uFP = GrColorMatrixFragmentProcessor::Make(std::move(uFP),
1058 uM,
1059 /*unpremulInput=*/false,
1060 /*clampRGBOutput=*/true,
1061 /*premulOutput=*/false);
1062 uFC->fillWithFP(std::move(uFP));
Brian Salomon63a0a752020-06-26 13:32:09 -04001063 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001064 uTransfer = uFC->transferPixels(GrColorType::kAlpha_8,
1065 SkIRect::MakeSize(uFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -04001066 if (!uTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001067 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001068 return;
1069 }
1070 }
1071
1072 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
1073 float vM[20];
1074 std::fill_n(vM, 15, 0.f);
1075 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -05001076 auto vFP = GrTextureEffect::Make(std::move(srcView),
1077 this->colorInfo().alphaType(),
1078 texMatrix,
1079 GrSamplerState::Filter::kLinear);
1080 vFP = GrColorMatrixFragmentProcessor::Make(std::move(vFP),
1081 vM,
1082 /*unpremulInput=*/false,
1083 /*clampRGBOutput=*/true,
1084 /*premulOutput=*/false);
1085 vFC->fillWithFP(std::move(vFP));
1086
Brian Salomon63a0a752020-06-26 13:32:09 -04001087 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001088 vTransfer = vFC->transferPixels(GrColorType::kAlpha_8,
1089 SkIRect::MakeSize(vFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -04001090 if (!vTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001091 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001092 return;
1093 }
1094 }
1095
1096 if (doSynchronousRead) {
Brian Salomonbe1084b2021-01-26 13:29:30 -05001097 GrPixmap yPmp = GrPixmap::Allocate(yInfo);
1098 GrPixmap uPmp = GrPixmap::Allocate(uvInfo);
1099 GrPixmap vPmp = GrPixmap::Allocate(uvInfo);
Brian Salomonbacbb922021-01-21 19:48:00 -05001100 if (!yFC->readPixels(dContext, yPmp, {0, 0}) ||
1101 !uFC->readPixels(dContext, uPmp, {0, 0}) ||
1102 !vFC->readPixels(dContext, vPmp, {0, 0})) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001103 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001104 return;
1105 }
Robert Phillips82ad7af2021-03-11 16:00:10 -05001106 auto result = std::make_unique<AsyncReadResult>(dContext->directContextID());
Brian Salomonbe1084b2021-01-26 13:29:30 -05001107 result->addCpuPlane(yPmp.pixelStorage(), yPmp.rowBytes());
1108 result->addCpuPlane(uPmp.pixelStorage(), uPmp.rowBytes());
1109 result->addCpuPlane(vPmp.pixelStorage(), vPmp.rowBytes());
Adlai Hollerc95b5892020-08-11 12:02:22 -04001110 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -04001111 return;
1112 }
1113
1114 struct FinishContext {
1115 ReadPixelsCallback* fClientCallback;
1116 ReadPixelsContext fClientContext;
1117 GrClientMappedBufferManager* fMappedBufferManager;
1118 SkISize fSize;
1119 PixelTransferResult fYTransfer;
1120 PixelTransferResult fUTransfer;
1121 PixelTransferResult fVTransfer;
1122 };
1123 // Assumption is that the caller would like to flush. We could take a parameter or require an
1124 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1125 // callback to GrGpu until after the next flush that flushes our op list, though.
1126 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -04001127 callbackContext,
1128 dContext->priv().clientMappedBufferManager(),
Brian Salomon63a0a752020-06-26 13:32:09 -04001129 dstSize,
1130 std::move(yTransfer),
1131 std::move(uTransfer),
1132 std::move(vTransfer)};
1133 auto finishCallback = [](GrGpuFinishedContext c) {
1134 const auto* context = reinterpret_cast<const FinishContext*>(c);
Brian Salomon63a0a752020-06-26 13:32:09 -04001135 auto manager = context->fMappedBufferManager;
Robert Phillips82ad7af2021-03-11 16:00:10 -05001136 auto result = std::make_unique<AsyncReadResult>(manager->owningDirectContext());
Brian Salomon63a0a752020-06-26 13:32:09 -04001137 size_t rowBytes = SkToSizeT(context->fSize.width());
1138 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
1139 (*context->fClientCallback)(context->fClientContext, nullptr);
1140 delete context;
1141 return;
1142 }
1143 rowBytes /= 2;
1144 SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
1145 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
1146 (*context->fClientCallback)(context->fClientContext, nullptr);
1147 delete context;
1148 return;
1149 }
1150 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
1151 (*context->fClientCallback)(context->fClientContext, nullptr);
1152 delete context;
1153 return;
1154 }
1155 (*context->fClientCallback)(context->fClientContext, std::move(result));
1156 delete context;
1157 };
1158 GrFlushInfo flushInfo;
1159 flushInfo.fFinishedContext = finishContext;
1160 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -05001161 dContext->priv().flushSurface(this->asSurfaceProxy(),
1162 SkSurface::BackendSurfaceAccess::kNoAccess,
1163 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -04001164}
1165
Brian Salomond63638b2021-03-05 14:00:07 -05001166sk_sp<GrRenderTask> GrSurfaceContext::copy(sk_sp<GrSurfaceProxy> src,
1167 SkIRect srcRect,
1168 SkIPoint dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001169 ASSERT_SINGLE_OWNER
Brian Salomond63638b2021-03-05 14:00:07 -05001170 RETURN_NULLPTR_IF_ABANDONED
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001171 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -04001172 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -04001173
Brian Salomon947efe22019-07-16 15:36:11 -04001174 const GrCaps* caps = fContext->priv().caps();
1175
Greg Daniel46cfbc62019-06-07 11:43:30 -04001176 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -05001177 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -04001178
Stephen White3c0a50f2020-01-16 18:19:54 -05001179 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomond63638b2021-03-05 14:00:07 -05001180 return nullptr;
Stephen White3c0a50f2020-01-16 18:19:54 -05001181 }
1182
Brian Salomon982127b2021-01-21 10:43:35 -05001183 if (!caps->canCopySurface(this->asSurfaceProxy(), src.get(), srcRect, dstPoint)) {
Brian Salomond63638b2021-03-05 14:00:07 -05001184 return nullptr;
Greg Daniel25af6712018-04-25 10:44:38 -04001185 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001186
Brian Salomon982127b2021-01-21 10:43:35 -05001187 return this->drawingManager()->newCopyRenderTask(std::move(src),
1188 srcRect,
1189 this->asSurfaceProxyRef(),
Brian Salomon0f9f8002021-01-22 16:30:50 -05001190 dstPoint,
1191 this->origin());
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001192}
Greg Daniel46cfbc62019-06-07 11:43:30 -04001193
Brian Salomonbacbb922021-01-21 19:48:00 -05001194std::unique_ptr<GrSurfaceFillContext> GrSurfaceContext::rescale(const GrImageInfo& info,
Brian Salomoneebe7352020-12-09 16:37:04 -05001195 GrSurfaceOrigin origin,
1196 SkIRect srcRect,
1197 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001198 RescaleMode rescaleMode) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001199 auto sfc = GrSurfaceFillContext::MakeWithFallback(fContext,
1200 info,
Brian Salomon1c86b632020-12-11 12:36:01 -05001201 SkBackingFit::kExact,
Brian Salomon1c86b632020-12-11 12:36:01 -05001202 1,
1203 GrMipmapped::kNo,
1204 this->asSurfaceProxy()->isProtected(),
1205 origin);
Brian Salomonbacbb922021-01-21 19:48:00 -05001206 if (!sfc || !this->rescaleInto(sfc.get(),
1207 SkIRect::MakeSize(sfc->dimensions()),
Brian Salomon1c86b632020-12-11 12:36:01 -05001208 srcRect,
1209 rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001210 rescaleMode)) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001211 return nullptr;
1212 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001213 return sfc;
Brian Salomon1c86b632020-12-11 12:36:01 -05001214}
1215
Brian Salomonbacbb922021-01-21 19:48:00 -05001216bool GrSurfaceContext::rescaleInto(GrSurfaceFillContext* dst,
Brian Salomon1c86b632020-12-11 12:36:01 -05001217 SkIRect dstRect,
1218 SkIRect srcRect,
1219 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001220 RescaleMode rescaleMode) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001221 SkASSERT(dst);
1222 if (!SkIRect::MakeSize(dst->dimensions()).contains((dstRect))) {
1223 return false;
1224 }
1225
Brian Salomone9ad9982019-07-22 16:17:41 -04001226 auto rtProxy = this->asRenderTargetProxy();
1227 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001228 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001229 }
1230
Stephen White3c0a50f2020-01-16 18:19:54 -05001231 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001232 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001233 }
1234
Greg Daniel40903af2020-01-30 14:55:05 -05001235 GrSurfaceProxyView texView = this->readSurfaceView();
Greg Daniel40903af2020-01-30 14:55:05 -05001236 if (!texView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -04001237 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect,
Brian Salomonc5243782020-04-02 12:50:34 -04001238 SkBackingFit::kApprox, SkBudgeted::kNo);
1239 if (!texView) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001240 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001241 }
Greg Daniel40903af2020-01-30 14:55:05 -05001242 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001243 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001244 }
1245
Brian Salomon1c86b632020-12-11 12:36:01 -05001246 SkISize finalSize = dstRect.size();
1247
Brian Salomonbf6b9792019-08-21 09:38:10 -04001248 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
1249 // 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 -05001250 std::unique_ptr<GrSurfaceFillContext> tempA;
1251 std::unique_ptr<GrSurfaceFillContext> tempB;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001252
Brian Salomone9ad9982019-07-22 16:17:41 -04001253 // Assume we should ignore the rescale linear request if the surface has no color space since
1254 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon63a0a752020-06-26 13:32:09 -04001255 if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() &&
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001256 !this->colorInfo().colorSpace()->gammaIsLinear()) {
1257 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomone9ad9982019-07-22 16:17:41 -04001258 // We'll fall back to kRGBA_8888 if half float not supported.
Brian Salomonbacbb922021-01-21 19:48:00 -05001259 GrImageInfo ii(GrColorType::kRGBA_F16,
1260 dst->colorInfo().alphaType(),
1261 std::move(cs),
1262 srcRect.size());
1263 auto linearRTC = GrSurfaceFillContext::MakeWithFallback(fContext,
1264 std::move(ii),
1265 SkBackingFit::kApprox,
1266 1,
1267 GrMipmapped::kNo,
1268 GrProtected::kNo,
1269 dst->origin());
Brian Salomone9ad9982019-07-22 16:17:41 -04001270 if (!linearRTC) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001271 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001272 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001273 auto fp = GrTextureEffect::Make(std::move(texView),
1274 this->colorInfo().alphaType(),
1275 SkMatrix::Translate(srcRect.topLeft()),
1276 GrSamplerState::Filter::kNearest,
1277 GrSamplerState::MipmapMode::kNone);
1278 fp = GrColorSpaceXformEffect::Make(std::move(fp),
1279 this->colorInfo(),
1280 linearRTC->colorInfo());
1281 linearRTC->fillWithFP(std::move(fp));
Greg Daniel40903af2020-01-30 14:55:05 -05001282 texView = linearRTC->readSurfaceView();
1283 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -04001284 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001285 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001286 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001287
Brian Salomon1c86b632020-12-11 12:36:01 -05001288 while (srcRect.size() != finalSize) {
1289 SkISize nextDims = finalSize;
Mike Reed1efa14d2021-01-02 21:44:59 -05001290 if (rescaleMode != RescaleMode::kNearest) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001291 if (srcRect.width() > finalSize.width()) {
1292 nextDims.fWidth = std::max((srcRect.width() + 1)/2, finalSize.width());
1293 } else if (srcRect.width() < finalSize.width()) {
1294 nextDims.fWidth = std::min(srcRect.width()*2, finalSize.width());
Brian Salomon59f31b12020-06-04 17:27:15 -04001295 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001296 if (srcRect.height() > finalSize.height()) {
1297 nextDims.fHeight = std::max((srcRect.height() + 1)/2, finalSize.height());
1298 } else if (srcRect.height() < finalSize.height()) {
1299 nextDims.fHeight = std::min(srcRect.height()*2, finalSize.height());
Brian Salomon59f31b12020-06-04 17:27:15 -04001300 }
1301 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001302 auto input = tempA ? tempA.get() : this;
Brian Salomone9ad9982019-07-22 16:17:41 -04001303 sk_sp<GrColorSpaceXform> xform;
Brian Salomonbacbb922021-01-21 19:48:00 -05001304 GrSurfaceFillContext* stepDst;
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001305 SkIRect stepDstRect;
Brian Salomon1c86b632020-12-11 12:36:01 -05001306 if (nextDims == finalSize) {
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001307 stepDst = dst;
1308 stepDstRect = dstRect;
Brian Salomonbacbb922021-01-21 19:48:00 -05001309 xform = GrColorSpaceXform::Make(input->colorInfo(), dst->colorInfo());
Brian Salomon1c86b632020-12-11 12:36:01 -05001310 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -05001311 GrImageInfo nextInfo(input->colorInfo(), nextDims);
1312 tempB = GrSurfaceFillContext::MakeWithFallback(fContext,
1313 nextInfo,
1314 SkBackingFit::kApprox);
Brian Salomon1c86b632020-12-11 12:36:01 -05001315 if (!tempB) {
1316 return false;
1317 }
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001318 stepDst = tempB.get();
1319 stepDstRect = SkIRect::MakeSize(tempB->dimensions());
Brian Salomone9ad9982019-07-22 16:17:41 -04001320 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001321 std::unique_ptr<GrFragmentProcessor> fp;
Mike Reed1efa14d2021-01-02 21:44:59 -05001322 if (rescaleMode == RescaleMode::kRepeatedCubic) {
Brian Salomon59f31b12020-06-04 17:27:15 -04001323 auto dir = GrBicubicEffect::Direction::kXY;
1324 if (nextDims.width() == srcRect.width()) {
1325 dir = GrBicubicEffect::Direction::kY;
1326 } else if (nextDims.height() == srcRect.height()) {
1327 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -04001328 }
Brian Salomon1af72d12020-06-25 10:47:26 -04001329 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
Mike Reed3867c702020-09-01 13:28:10 -04001330 static constexpr auto kKernel = GrBicubicEffect::gCatmullRom;
Brian Salomon1c86b632020-12-11 12:36:01 -05001331 fp = GrBicubicEffect::MakeSubset(std::move(texView),
1332 input->colorInfo().alphaType(),
Brian Salomonbacbb922021-01-21 19:48:00 -05001333 SkMatrix::I(),
Brian Salomon1c86b632020-12-11 12:36:01 -05001334 kWM,
1335 kWM,
1336 SkRect::Make(srcRect),
1337 kKernel,
1338 dir,
1339 *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -04001340 } else {
Mike Reed1efa14d2021-01-02 21:44:59 -05001341 auto filter = rescaleMode == RescaleMode::kNearest ? GrSamplerState::Filter::kNearest
1342 : GrSamplerState::Filter::kLinear;
Brian Salomonbacbb922021-01-21 19:48:00 -05001343 auto srcRectF = SkRect::Make(srcRect);
1344 fp = GrTextureEffect::MakeSubset(std::move(texView),
1345 this->colorInfo().alphaType(),
1346 SkMatrix::I(),
1347 {filter, GrSamplerState::MipmapMode::kNone},
1348 srcRectF,
1349 srcRectF,
1350 *this->caps());
Brian Salomone9ad9982019-07-22 16:17:41 -04001351 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001352 if (xform) {
1353 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
1354 }
1355 stepDst->fillRectToRectWithFP(srcRect, stepDstRect, std::move(fp));
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001356 texView = stepDst->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -04001357 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001358 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomone9ad9982019-07-22 16:17:41 -04001359 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001360 return true;
Brian Salomone9ad9982019-07-22 16:17:41 -04001361}
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001362
1363GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
1364 const SkIRect& rect) {
1365 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1366 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001367 auto direct = fContext->asDirectContext();
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001368 if (!direct) {
1369 return {};
1370 }
1371 auto rtProxy = this->asRenderTargetProxy();
1372 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1373 return {};
1374 }
1375
1376 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001377 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
1378 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001379 // Fail if read color type does not have all of dstCT's color channels and those missing color
1380 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001381 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1382 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
1383 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1384 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001385 return {};
1386 }
1387
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001388 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001389 !supportedRead.fOffsetAlignmentForTransferBuffer) {
1390 return {};
1391 }
1392
1393 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
1394 size_t size = rowBytes * rect.height();
Greg Daniel2e967df2021-02-08 10:38:31 -05001395 // By using kStream_GrAccessPattern here, we are not able to cache and reuse the buffer for
1396 // multiple reads. Switching to kDynamic_GrAccessPattern would allow for this, however doing
1397 // so causes a crash in a chromium test. See skbug.com/11297
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001398 auto buffer = direct->priv().resourceProvider()->createBuffer(
Greg Daniel393debc2021-02-06 03:37:20 +00001399 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001400 if (!buffer) {
1401 return {};
1402 }
1403 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -05001404 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001405 if (flip) {
1406 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1407 this->height() - rect.fTop);
1408 }
Greg Danielbbfec9d2019-08-20 10:56:51 -04001409 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001410 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -04001411 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001412 PixelTransferResult result;
1413 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001414 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -04001415 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001416 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
1417 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001418 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
1419 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon5392c942021-03-30 16:14:37 -04001420 GrConvertPixels( GrPixmap(dstInfo, dst, dstInfo.minRowBytes()),
1421 GrCPixmap(srcInfo, src, srcInfo.minRowBytes()));
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001422 };
1423 }
1424 return result;
1425}
Greg Daniel46e366a2019-12-16 14:38:36 -05001426
1427#ifdef SK_DEBUG
1428void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001429 SkASSERT(fReadView.proxy());
1430 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -04001431 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
1432 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
1433 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
1434 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001435 this->onValidate();
1436}
1437#endif