blob: 31b65345f954a816502214684971fac1cc954dbf [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,
360 SkIPoint dstPt,
361 bool prepForSampling) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400362 ASSERT_SINGLE_OWNER
363 RETURN_FALSE_IF_ABANDONED
364 SkDEBUGCODE(this->validate();)
Brian Salomon2c673402021-04-02 14:36:58 -0400365
366 src = src.clip(this->dimensions(), &dstPt);
367 if (!src.hasPixels()) {
368 return false;
369 }
370 if (!src.info().bpp() || src.rowBytes() % src.info().bpp()) {
371 return false;
372 }
Brian Salomonea1d39b2021-04-01 17:06:52 -0400373 return this->internalWritePixels(dContext, &src, 1, dstPt, prepForSampling);
Brian Salomon2c673402021-04-02 14:36:58 -0400374}
375
376bool GrSurfaceContext::writePixels(GrDirectContext* dContext,
377 const GrCPixmap src[],
Brian Salomonea1d39b2021-04-01 17:06:52 -0400378 int numLevels,
379 bool prepForSampling) {
Brian Salomon2c673402021-04-02 14:36:58 -0400380 ASSERT_SINGLE_OWNER
381 RETURN_FALSE_IF_ABANDONED
382 SkDEBUGCODE(this->validate();)
383
384 SkASSERT(dContext);
385 SkASSERT(numLevels >= 1);
386 SkASSERT(src);
387
388 if (numLevels == 1) {
389 if (src->dimensions() != this->dimensions()) {
390 return false;
391 }
Brian Salomonea1d39b2021-04-01 17:06:52 -0400392 return this->writePixels(dContext, src[0], {0, 0}, prepForSampling);
Brian Salomon2c673402021-04-02 14:36:58 -0400393 }
394 if (!this->asTextureProxy() || this->asTextureProxy()->proxyMipmapped() == GrMipmapped::kNo) {
395 return false;
396 }
397
398 SkISize dims = this->dimensions();
399 if (numLevels != SkMipmap::ComputeLevelCount(dims) + 1) {
400 return false;
401 }
402 for (int i = 0; i < numLevels; ++i) {
403 if (src[i].colorInfo() != src[0].colorInfo()) {
404 return false;
405 }
406 if (dims != src[i].dimensions()) {
407 return false;
408 }
409 if (!src[i].info().bpp() || src[i].rowBytes() % src[i].info().bpp()) {
410 return false;
411 }
412 dims = {std::max(1, dims.width()/2), std::max(1, dims.height()/2)};
413 }
Brian Salomonea1d39b2021-04-01 17:06:52 -0400414 return this->internalWritePixels(dContext, src, numLevels, {0, 0}, prepForSampling);
Brian Salomon2c673402021-04-02 14:36:58 -0400415}
416
417bool GrSurfaceContext::internalWritePixels(GrDirectContext* dContext,
418 const GrCPixmap src[],
419 int numLevels,
Brian Salomonea1d39b2021-04-01 17:06:52 -0400420 SkIPoint pt,
421 bool prepForSampling) {
Brian Salomon2c673402021-04-02 14:36:58 -0400422 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::internalWritePixels");
423
424 SkASSERT(numLevels >= 1);
425 SkASSERT(src);
426
427 // We can either write to a subset or write MIP levels, but not both.
428 SkASSERT((src[0].dimensions() == this->dimensions() && pt.isZero()) || numLevels == 1);
429 SkASSERT(numLevels == 1 ||
Brian Salomon7cde6c32021-04-02 15:53:42 -0400430 (this->asTextureProxy() && this->asTextureProxy()->mipmapped() == GrMipmapped::kYes));
Brian Salomon2c673402021-04-02 14:36:58 -0400431 // Our public caller should have clipped to the bounds of the surface already.
Brian Salomonea1d39b2021-04-01 17:06:52 -0400432 SkASSERT(SkIRect::MakeSize(this->dimensions()).contains(
433 SkIRect::MakePtSize(pt, src[0].dimensions())));
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400434
Adlai Hollerc95b5892020-08-11 12:02:22 -0400435 if (!dContext) {
Brian Salomonc320b152018-02-20 14:05:36 -0500436 return false;
437 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500438
Brian Salomon1d435302019-07-01 13:05:28 -0400439 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500440 return false;
441 }
442
Brian Salomon2c673402021-04-02 14:36:58 -0400443 if (src[0].colorType() == GrColorType::kUnknown) {
Brian Salomon46f63232021-01-25 10:13:35 -0500444 return false;
445 }
446
Brian Salomon2c673402021-04-02 14:36:58 -0400447 if (!alpha_types_compatible(src[0].alphaType(), this->colorInfo().alphaType())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400448 return false;
449 }
450
451 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
Stephen White3c0a50f2020-01-16 18:19:54 -0500452
453 if (dstProxy->framebufferOnly()) {
454 return false;
455 }
456
Adlai Hollerc95b5892020-08-11 12:02:22 -0400457 if (!dstProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400458 return false;
459 }
460
461 GrSurface* dstSurface = dstProxy->peekSurface();
462
Brian Salomondd4087d2020-12-23 20:36:44 -0500463 SkColorSpaceXformSteps::Flags flags =
Brian Salomon2c673402021-04-02 14:36:58 -0400464 SkColorSpaceXformSteps{src[0].colorInfo(), this->colorInfo()}.flags;
Mike Klein7321e6a2019-12-03 11:08:40 -0600465 bool unpremul = flags.unpremul,
466 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
467 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400468
Adlai Hollerc95b5892020-08-11 12:02:22 -0400469 const GrCaps* caps = dContext->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400470
471 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
472 GrRenderable::kNo);
473
Greg Danielc71c7962020-01-14 16:44:18 -0500474 GrColorType dstColorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400475 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
476 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400477 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
Brian Salomon2c673402021-04-02 14:36:58 -0400478 (src[0].colorType() == GrColorType::kRGBA_8888 ||
479 src[0].colorType() == GrColorType::kBGRA_8888) &&
Brian Salomon590f5672020-12-16 11:44:47 -0500480 this->asFillContext() &&
Greg Danielc71c7962020-01-14 16:44:18 -0500481 (dstColorType == GrColorType::kRGBA_8888 ||
482 dstColorType == GrColorType::kBGRA_8888) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400483 rgbaDefaultFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400484 dContext->priv().validPMUPMConversionExists();
Brian Salomon2c673402021-04-02 14:36:58 -0400485 // Drawing code path doesn't support writing to levels and doesn't support inserting layout
486 // transitions.
Brian Salomonea1d39b2021-04-01 17:06:52 -0400487 if ((!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) &&
488 numLevels == 1 &&
489 !prepForSampling) {
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 Salomonea1d39b2021-04-01 17:06:52 -0400618 sk_ref_sp(dstProxy),
619 SkIRect::MakePtSize(pt, src[0].dimensions()),
620 this->colorInfo().colorType(),
621 allowedColorType,
622 srcLevels.begin(),
623 numLevels,
624 prepForSampling)) {
Brian Salomon2c673402021-04-02 14:36:58 -0400625 return false;
626 }
627 if (numLevels > 1) {
628 dstProxy->asTextureProxy()->markMipmapsClean();
629 }
630 if (!ownAllStorage) {
631 // If any pixmap doesn't own its pixels then we must flush so that the pixels are pushed to
632 // the GPU before we return.
Brian Salomon24949422021-02-11 09:39:46 -0500633 dContext->priv().flushSurface(dstProxy);
634 }
Brian Salomon2c673402021-04-02 14:36:58 -0400635 return true;
Brian Osman45580d32016-11-23 09:37:01 -0500636}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400637
Adlai Hollerc95b5892020-08-11 12:02:22 -0400638void GrSurfaceContext::asyncRescaleAndReadPixels(GrDirectContext* dContext,
639 const SkImageInfo& info,
Brian Salomon63a0a752020-06-26 13:32:09 -0400640 const SkIRect& srcRect,
641 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500642 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400643 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400644 ReadPixelsContext callbackContext) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400645 if (!dContext) {
646 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400647 return;
648 }
649 auto rt = this->asRenderTargetProxy();
650 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400651 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400652 return;
653 }
654 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400655 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400656 return;
657 }
658 auto dstCT = SkColorTypeToGrColorType(info.colorType());
659 if (dstCT == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400660 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400661 return;
662 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500663 bool needsRescale = srcRect.size() != info.dimensions();
Brian Salomon63a0a752020-06-26 13:32:09 -0400664 auto colorTypeOfFinalContext = this->colorInfo().colorType();
665 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
666 if (needsRescale) {
667 colorTypeOfFinalContext = dstCT;
668 backendFormatOfFinalContext =
669 this->caps()->getDefaultBackendFormat(dstCT, GrRenderable::kYes);
670 }
671 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonbacbb922021-01-21 19:48:00 -0500672 backendFormatOfFinalContext,
673 dstCT);
Brian Salomon63a0a752020-06-26 13:32:09 -0400674 // Fail if we can't read from the source surface's color type.
675 if (readInfo.fColorType == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400676 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400677 return;
678 }
679 // Fail if read color type does not have all of dstCT's color channels and those missing color
680 // channels are in the src.
681 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
682 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
683 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
684 if ((~legalReadChannels & dstChannels) & srcChannels) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400685 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400686 return;
687 }
688
Brian Salomonbacbb922021-01-21 19:48:00 -0500689 std::unique_ptr<GrSurfaceFillContext> tempFC;
Brian Salomon63a0a752020-06-26 13:32:09 -0400690 int x = srcRect.fLeft;
691 int y = srcRect.fTop;
692 if (needsRescale) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500693 tempFC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma, rescaleMode);
694 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400695 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400696 return;
697 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500698 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
699 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400700 x = y = 0;
701 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -0500702 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo(),
703 info.colorInfo());
Brian Salomon63a0a752020-06-26 13:32:09 -0400704 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
705 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
706 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomonbacbb922021-01-21 19:48:00 -0500707 SkIRect srcRectToDraw = srcRect;
Brian Salomon63a0a752020-06-26 13:32:09 -0400708 // If the src is not texturable first try to make a copy to a texture.
709 if (!texProxyView.asTextureProxy()) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500710 texProxyView = GrSurfaceProxyView::Copy(fContext,
711 texProxyView,
712 GrMipmapped::kNo,
713 srcRect,
714 SkBackingFit::kApprox,
715 SkBudgeted::kNo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400716 if (!texProxyView) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400717 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400718 return;
719 }
720 SkASSERT(texProxyView.asTextureProxy());
Brian Salomonbacbb922021-01-21 19:48:00 -0500721 srcRectToDraw = SkIRect::MakeSize(srcRect.size());
Brian Salomon63a0a752020-06-26 13:32:09 -0400722 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500723 auto tempInfo = GrImageInfo(info).makeColorType(this->colorInfo().colorType());
724 tempFC = GrSurfaceFillContext::Make(dContext, tempInfo, SkBackingFit::kApprox);
725 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400726 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400727 return;
728 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500729 auto fp = GrTextureEffect::Make(std::move(texProxyView), this->colorInfo().alphaType());
730 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
731 tempFC->fillRectToRectWithFP(srcRectToDraw,
732 SkIRect::MakeSize(tempFC->dimensions()),
733 std::move(fp));
Brian Salomon63a0a752020-06-26 13:32:09 -0400734 x = y = 0;
735 }
736 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500737 auto srcCtx = tempFC ? tempFC.get() : this;
738 return srcCtx->asyncReadPixels(dContext,
739 SkIRect::MakePtSize({x, y}, info.dimensions()),
740 info.colorType(),
741 callback,
742 callbackContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400743}
744
745class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult {
746public:
Robert Phillips82ad7af2021-03-11 16:00:10 -0500747 AsyncReadResult(GrDirectContext::DirectContextID intendedRecipient)
748 : fIntendedRecipient(intendedRecipient) {
749 }
750
Brian Salomon63a0a752020-06-26 13:32:09 -0400751 ~AsyncReadResult() override {
752 for (int i = 0; i < fPlanes.count(); ++i) {
Robert Phillips82ad7af2021-03-11 16:00:10 -0500753 fPlanes[i].releaseMappedBuffer(fIntendedRecipient);
Brian Salomon63a0a752020-06-26 13:32:09 -0400754 }
755 }
756
757 int count() const override { return fPlanes.count(); }
Brian Salomonbe1084b2021-01-26 13:29:30 -0500758 const void* data(int i) const override { return fPlanes[i].data(); }
759 size_t rowBytes(int i) const override { return fPlanes[i].rowBytes(); }
Brian Salomon63a0a752020-06-26 13:32:09 -0400760
761 bool addTransferResult(const PixelTransferResult& result,
762 SkISize dimensions,
763 size_t rowBytes,
764 GrClientMappedBufferManager* manager) {
765 SkASSERT(!result.fTransferBuffer->isMapped());
766 const void* mappedData = result.fTransferBuffer->map();
767 if (!mappedData) {
768 return false;
769 }
770 if (result.fPixelConverter) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500771 size_t size = rowBytes*dimensions.height();
772 sk_sp<SkData> data = SkData::MakeUninitialized(size);
773 result.fPixelConverter(data->writable_data(), mappedData);
774 this->addCpuPlane(std::move(data), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400775 result.fTransferBuffer->unmap();
776 } else {
777 manager->insert(result.fTransferBuffer);
778 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
779 }
780 return true;
781 }
782
Brian Salomonbe1084b2021-01-26 13:29:30 -0500783 void addCpuPlane(sk_sp<SkData> data, size_t rowBytes) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400784 SkASSERT(data);
785 SkASSERT(rowBytes > 0);
Brian Salomonbe1084b2021-01-26 13:29:30 -0500786 fPlanes.emplace_back(std::move(data), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400787 }
788
789private:
790 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
791 SkASSERT(data);
792 SkASSERT(rowBytes > 0);
793 SkASSERT(mappedBuffer);
794 SkASSERT(mappedBuffer->isMapped());
Brian Salomonbe1084b2021-01-26 13:29:30 -0500795 fPlanes.emplace_back(std::move(mappedBuffer), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400796 }
797
Brian Salomonbe1084b2021-01-26 13:29:30 -0500798 class Plane {
799 public:
800 Plane(sk_sp<GrGpuBuffer> buffer, size_t rowBytes)
801 : fMappedBuffer(std::move(buffer)), fRowBytes(rowBytes) {}
802 Plane(sk_sp<SkData> data, size_t rowBytes) : fData(std::move(data)), fRowBytes(rowBytes) {}
803
804 Plane(const Plane&) = delete;
805 Plane(Plane&&) = default;
806
807 ~Plane() { SkASSERT(!fMappedBuffer); }
808
809 Plane& operator=(const Plane&) = delete;
810 Plane& operator=(Plane&&) = default;
811
Robert Phillips82ad7af2021-03-11 16:00:10 -0500812 void releaseMappedBuffer(GrDirectContext::DirectContextID intendedRecipient) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500813 if (fMappedBuffer) {
814 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
Robert Phillips82ad7af2021-03-11 16:00:10 -0500815 {std::move(fMappedBuffer), intendedRecipient});
Brian Salomonbe1084b2021-01-26 13:29:30 -0500816 }
817 }
818
819 const void* data() const {
820 if (fMappedBuffer) {
821 SkASSERT(!fData);
822 SkASSERT(fMappedBuffer->isMapped());
823 return fMappedBuffer->map();
824 }
825 SkASSERT(fData);
826 return fData->data();
827 }
828
829 size_t rowBytes() const { return fRowBytes; }
830
831 private:
832 sk_sp<SkData> fData;
Brian Salomon1eea1ea2021-01-26 18:12:25 +0000833 sk_sp<GrGpuBuffer> fMappedBuffer;
Brian Salomonbe1084b2021-01-26 13:29:30 -0500834 size_t fRowBytes;
Brian Salomon63a0a752020-06-26 13:32:09 -0400835 };
836 SkSTArray<3, Plane> fPlanes;
Robert Phillips82ad7af2021-03-11 16:00:10 -0500837 GrDirectContext::DirectContextID fIntendedRecipient;
Brian Salomon63a0a752020-06-26 13:32:09 -0400838};
839
Adlai Hollerc95b5892020-08-11 12:02:22 -0400840void GrSurfaceContext::asyncReadPixels(GrDirectContext* dContext,
841 const SkIRect& rect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400842 SkColorType colorType,
843 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400844 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400845 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
846 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
847
Adlai Hollerc95b5892020-08-11 12:02:22 -0400848 if (!dContext || this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
849 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400850 return;
851 }
852
Adlai Hollerc95b5892020-08-11 12:02:22 -0400853 auto mappedBufferManager = dContext->priv().clientMappedBufferManager();
Brian Salomon63a0a752020-06-26 13:32:09 -0400854
855 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
856
857 if (!transferResult.fTransferBuffer) {
858 auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
859 this->colorInfo().refColorSpace());
Robert Phillips82ad7af2021-03-11 16:00:10 -0500860 static const GrDirectContext::DirectContextID kInvalid;
861 auto result = std::make_unique<AsyncReadResult>(kInvalid);
Brian Salomonbe1084b2021-01-26 13:29:30 -0500862 GrPixmap pm = GrPixmap::Allocate(ii);
863 result->addCpuPlane(pm.pixelStorage(), pm.rowBytes());
Brian Salomon63a0a752020-06-26 13:32:09 -0400864
Adlai Hollerc95b5892020-08-11 12:02:22 -0400865 SkIPoint pt{rect.fLeft, rect.fTop};
Brian Salomondd4087d2020-12-23 20:36:44 -0500866 if (!this->readPixels(dContext, pm, pt)) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400867 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400868 return;
869 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400870 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400871 return;
872 }
873
874 struct FinishContext {
875 ReadPixelsCallback* fClientCallback;
876 ReadPixelsContext fClientContext;
877 SkISize fSize;
878 SkColorType fColorType;
879 GrClientMappedBufferManager* fMappedBufferManager;
880 PixelTransferResult fTransferResult;
881 };
882 // Assumption is that the caller would like to flush. We could take a parameter or require an
883 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
884 // callback to GrGpu until after the next flush that flushes our op list, though.
885 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400886 callbackContext,
Brian Salomon63a0a752020-06-26 13:32:09 -0400887 rect.size(),
888 colorType,
889 mappedBufferManager,
890 std::move(transferResult)};
891 auto finishCallback = [](GrGpuFinishedContext c) {
892 const auto* context = reinterpret_cast<const FinishContext*>(c);
Robert Phillips82ad7af2021-03-11 16:00:10 -0500893 auto manager = context->fMappedBufferManager;
894 auto result = std::make_unique<AsyncReadResult>(manager->owningDirectContext());
Brian Salomon63a0a752020-06-26 13:32:09 -0400895 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
896 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
Robert Phillips82ad7af2021-03-11 16:00:10 -0500897 manager)) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400898 result.reset();
899 }
900 (*context->fClientCallback)(context->fClientContext, std::move(result));
901 delete context;
902 };
903 GrFlushInfo flushInfo;
904 flushInfo.fFinishedContext = finishContext;
905 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -0500906
907 dContext->priv().flushSurface(this->asSurfaceProxy(),
908 SkSurface::BackendSurfaceAccess::kNoAccess,
909 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400910}
911
Adlai Hollerc95b5892020-08-11 12:02:22 -0400912void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(GrDirectContext* dContext,
913 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400914 sk_sp<SkColorSpace> dstColorSpace,
915 const SkIRect& srcRect,
916 SkISize dstSize,
917 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500918 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400919 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400920 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400921 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
922 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
923 SkASSERT(!dstSize.isZero());
924 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
925
Adlai Hollerc95b5892020-08-11 12:02:22 -0400926 if (!dContext) {
927 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400928 return;
929 }
930 auto rt = this->asRenderTargetProxy();
931 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400932 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400933 return;
934 }
935 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400936 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400937 return;
938 }
939 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400940 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400941 return;
942 }
943 int x = srcRect.fLeft;
944 int y = srcRect.fTop;
945 bool needsRescale = srcRect.size() != dstSize;
946 GrSurfaceProxyView srcView;
Brian Salomonbacbb922021-01-21 19:48:00 -0500947 auto info = SkImageInfo::Make(dstSize,
948 kRGBA_8888_SkColorType,
949 this->colorInfo().alphaType(),
950 dstColorSpace);
Brian Salomon63a0a752020-06-26 13:32:09 -0400951 if (needsRescale) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400952 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomonbacbb922021-01-21 19:48:00 -0500953 auto tempFC = this->rescale(info,
954 kTopLeft_GrSurfaceOrigin,
955 srcRect,
956 rescaleGamma,
957 rescaleMode);
958 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400959 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400960 return;
961 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500962 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
963 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400964 x = y = 0;
Brian Salomonbacbb922021-01-21 19:48:00 -0500965 srcView = tempFC->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400966 } else {
967 srcView = this->readSurfaceView();
968 if (!srcView.asTextureProxy()) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500969 srcView = GrSurfaceProxyView::Copy(fContext,
970 std::move(srcView),
971 GrMipmapped::kNo,
972 srcRect,
973 SkBackingFit::kApprox,
974 SkBudgeted::kYes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400975 if (!srcView) {
976 // If we can't get a texture copy of the contents then give up.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400977 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400978 return;
979 }
980 SkASSERT(srcView.asTextureProxy());
981 x = y = 0;
982 }
983 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomonbacbb922021-01-21 19:48:00 -0500984 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo(),
985 info.colorInfo());
Brian Salomon63a0a752020-06-26 13:32:09 -0400986 if (xform) {
987 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
Brian Salomonbacbb922021-01-21 19:48:00 -0500988 auto tempFC = GrSurfaceFillContext::Make(dContext,
989 info,
990 SkBackingFit::kApprox,
991 1,
992 GrMipmapped::kNo,
993 GrProtected::kNo,
994 kTopLeft_GrSurfaceOrigin);
995 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400996 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400997 return;
998 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500999 auto fp = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType());
1000 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
1001 tempFC->fillRectToRectWithFP(srcRectToDraw,
1002 SkIRect::MakeSize(tempFC->dimensions()),
1003 std::move(fp));
1004 srcView = tempFC->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -04001005 SkASSERT(srcView.asTextureProxy());
1006 x = y = 0;
1007 }
1008 }
1009
Brian Salomonbacbb922021-01-21 19:48:00 -05001010 auto yInfo = SkImageInfo::MakeA8(dstSize);
1011 auto yFC = GrSurfaceFillContext::MakeWithFallback(dContext, yInfo, SkBackingFit::kApprox);
1012
1013 auto uvInfo = yInfo.makeWH(yInfo.width()/2, yInfo.height()/2);
1014 auto uFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
1015 auto vFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
1016
1017 if (!yFC || !uFC || !vFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001018 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001019 return;
1020 }
1021
1022 float baseM[20];
1023 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
1024
1025 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
1026
1027 auto texMatrix = SkMatrix::Translate(x, y);
1028
Brian Salomon63a0a752020-06-26 13:32:09 -04001029 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
1030 PixelTransferResult yTransfer, uTransfer, vTransfer;
1031
1032 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
1033 float yM[20];
1034 std::fill_n(yM, 15, 0.f);
1035 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -05001036
1037 auto yFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
1038 yFP = GrColorMatrixFragmentProcessor::Make(std::move(yFP),
1039 yM,
1040 /*unpremulInput=*/false,
1041 /*clampRGBOutput=*/true,
1042 /*premulOutput=*/false);
1043 yFC->fillWithFP(std::move(yFP));
Brian Salomon63a0a752020-06-26 13:32:09 -04001044 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001045 yTransfer = yFC->transferPixels(GrColorType::kAlpha_8,
1046 SkIRect::MakeSize(yFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -04001047 if (!yTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001048 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001049 return;
1050 }
1051 }
1052
1053 texMatrix.preScale(2.f, 2.f);
1054 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
1055 float uM[20];
1056 std::fill_n(uM, 15, 0.f);
1057 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -05001058
1059 auto uFP = GrTextureEffect::Make(srcView,
1060 this->colorInfo().alphaType(),
1061 texMatrix,
1062 GrSamplerState::Filter::kLinear);
1063 uFP = GrColorMatrixFragmentProcessor::Make(std::move(uFP),
1064 uM,
1065 /*unpremulInput=*/false,
1066 /*clampRGBOutput=*/true,
1067 /*premulOutput=*/false);
1068 uFC->fillWithFP(std::move(uFP));
Brian Salomon63a0a752020-06-26 13:32:09 -04001069 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001070 uTransfer = uFC->transferPixels(GrColorType::kAlpha_8,
1071 SkIRect::MakeSize(uFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -04001072 if (!uTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001073 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001074 return;
1075 }
1076 }
1077
1078 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
1079 float vM[20];
1080 std::fill_n(vM, 15, 0.f);
1081 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -05001082 auto vFP = GrTextureEffect::Make(std::move(srcView),
1083 this->colorInfo().alphaType(),
1084 texMatrix,
1085 GrSamplerState::Filter::kLinear);
1086 vFP = GrColorMatrixFragmentProcessor::Make(std::move(vFP),
1087 vM,
1088 /*unpremulInput=*/false,
1089 /*clampRGBOutput=*/true,
1090 /*premulOutput=*/false);
1091 vFC->fillWithFP(std::move(vFP));
1092
Brian Salomon63a0a752020-06-26 13:32:09 -04001093 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001094 vTransfer = vFC->transferPixels(GrColorType::kAlpha_8,
1095 SkIRect::MakeSize(vFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -04001096 if (!vTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001097 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001098 return;
1099 }
1100 }
1101
1102 if (doSynchronousRead) {
Brian Salomonbe1084b2021-01-26 13:29:30 -05001103 GrPixmap yPmp = GrPixmap::Allocate(yInfo);
1104 GrPixmap uPmp = GrPixmap::Allocate(uvInfo);
1105 GrPixmap vPmp = GrPixmap::Allocate(uvInfo);
Brian Salomonbacbb922021-01-21 19:48:00 -05001106 if (!yFC->readPixels(dContext, yPmp, {0, 0}) ||
1107 !uFC->readPixels(dContext, uPmp, {0, 0}) ||
1108 !vFC->readPixels(dContext, vPmp, {0, 0})) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001109 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001110 return;
1111 }
Robert Phillips82ad7af2021-03-11 16:00:10 -05001112 auto result = std::make_unique<AsyncReadResult>(dContext->directContextID());
Brian Salomonbe1084b2021-01-26 13:29:30 -05001113 result->addCpuPlane(yPmp.pixelStorage(), yPmp.rowBytes());
1114 result->addCpuPlane(uPmp.pixelStorage(), uPmp.rowBytes());
1115 result->addCpuPlane(vPmp.pixelStorage(), vPmp.rowBytes());
Adlai Hollerc95b5892020-08-11 12:02:22 -04001116 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -04001117 return;
1118 }
1119
1120 struct FinishContext {
1121 ReadPixelsCallback* fClientCallback;
1122 ReadPixelsContext fClientContext;
1123 GrClientMappedBufferManager* fMappedBufferManager;
1124 SkISize fSize;
1125 PixelTransferResult fYTransfer;
1126 PixelTransferResult fUTransfer;
1127 PixelTransferResult fVTransfer;
1128 };
1129 // Assumption is that the caller would like to flush. We could take a parameter or require an
1130 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1131 // callback to GrGpu until after the next flush that flushes our op list, though.
1132 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -04001133 callbackContext,
1134 dContext->priv().clientMappedBufferManager(),
Brian Salomon63a0a752020-06-26 13:32:09 -04001135 dstSize,
1136 std::move(yTransfer),
1137 std::move(uTransfer),
1138 std::move(vTransfer)};
1139 auto finishCallback = [](GrGpuFinishedContext c) {
1140 const auto* context = reinterpret_cast<const FinishContext*>(c);
Brian Salomon63a0a752020-06-26 13:32:09 -04001141 auto manager = context->fMappedBufferManager;
Robert Phillips82ad7af2021-03-11 16:00:10 -05001142 auto result = std::make_unique<AsyncReadResult>(manager->owningDirectContext());
Brian Salomon63a0a752020-06-26 13:32:09 -04001143 size_t rowBytes = SkToSizeT(context->fSize.width());
1144 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
1145 (*context->fClientCallback)(context->fClientContext, nullptr);
1146 delete context;
1147 return;
1148 }
1149 rowBytes /= 2;
1150 SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
1151 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
1152 (*context->fClientCallback)(context->fClientContext, nullptr);
1153 delete context;
1154 return;
1155 }
1156 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
1157 (*context->fClientCallback)(context->fClientContext, nullptr);
1158 delete context;
1159 return;
1160 }
1161 (*context->fClientCallback)(context->fClientContext, std::move(result));
1162 delete context;
1163 };
1164 GrFlushInfo flushInfo;
1165 flushInfo.fFinishedContext = finishContext;
1166 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -05001167 dContext->priv().flushSurface(this->asSurfaceProxy(),
1168 SkSurface::BackendSurfaceAccess::kNoAccess,
1169 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -04001170}
1171
Brian Salomond63638b2021-03-05 14:00:07 -05001172sk_sp<GrRenderTask> GrSurfaceContext::copy(sk_sp<GrSurfaceProxy> src,
1173 SkIRect srcRect,
1174 SkIPoint dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001175 ASSERT_SINGLE_OWNER
Brian Salomond63638b2021-03-05 14:00:07 -05001176 RETURN_NULLPTR_IF_ABANDONED
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001177 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -04001178 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -04001179
Brian Salomon947efe22019-07-16 15:36:11 -04001180 const GrCaps* caps = fContext->priv().caps();
1181
Greg Daniel46cfbc62019-06-07 11:43:30 -04001182 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -05001183 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -04001184
Stephen White3c0a50f2020-01-16 18:19:54 -05001185 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomond63638b2021-03-05 14:00:07 -05001186 return nullptr;
Stephen White3c0a50f2020-01-16 18:19:54 -05001187 }
1188
Brian Salomon982127b2021-01-21 10:43:35 -05001189 if (!caps->canCopySurface(this->asSurfaceProxy(), src.get(), srcRect, dstPoint)) {
Brian Salomond63638b2021-03-05 14:00:07 -05001190 return nullptr;
Greg Daniel25af6712018-04-25 10:44:38 -04001191 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001192
Brian Salomon982127b2021-01-21 10:43:35 -05001193 return this->drawingManager()->newCopyRenderTask(std::move(src),
1194 srcRect,
1195 this->asSurfaceProxyRef(),
Brian Salomon0f9f8002021-01-22 16:30:50 -05001196 dstPoint,
1197 this->origin());
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001198}
Greg Daniel46cfbc62019-06-07 11:43:30 -04001199
Brian Salomonbacbb922021-01-21 19:48:00 -05001200std::unique_ptr<GrSurfaceFillContext> GrSurfaceContext::rescale(const GrImageInfo& info,
Brian Salomoneebe7352020-12-09 16:37:04 -05001201 GrSurfaceOrigin origin,
1202 SkIRect srcRect,
1203 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001204 RescaleMode rescaleMode) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001205 auto sfc = GrSurfaceFillContext::MakeWithFallback(fContext,
1206 info,
Brian Salomon1c86b632020-12-11 12:36:01 -05001207 SkBackingFit::kExact,
Brian Salomon1c86b632020-12-11 12:36:01 -05001208 1,
1209 GrMipmapped::kNo,
1210 this->asSurfaceProxy()->isProtected(),
1211 origin);
Brian Salomonbacbb922021-01-21 19:48:00 -05001212 if (!sfc || !this->rescaleInto(sfc.get(),
1213 SkIRect::MakeSize(sfc->dimensions()),
Brian Salomon1c86b632020-12-11 12:36:01 -05001214 srcRect,
1215 rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001216 rescaleMode)) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001217 return nullptr;
1218 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001219 return sfc;
Brian Salomon1c86b632020-12-11 12:36:01 -05001220}
1221
Brian Salomonbacbb922021-01-21 19:48:00 -05001222bool GrSurfaceContext::rescaleInto(GrSurfaceFillContext* dst,
Brian Salomon1c86b632020-12-11 12:36:01 -05001223 SkIRect dstRect,
1224 SkIRect srcRect,
1225 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001226 RescaleMode rescaleMode) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001227 SkASSERT(dst);
1228 if (!SkIRect::MakeSize(dst->dimensions()).contains((dstRect))) {
1229 return false;
1230 }
1231
Brian Salomone9ad9982019-07-22 16:17:41 -04001232 auto rtProxy = this->asRenderTargetProxy();
1233 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001234 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001235 }
1236
Stephen White3c0a50f2020-01-16 18:19:54 -05001237 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001238 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001239 }
1240
Greg Daniel40903af2020-01-30 14:55:05 -05001241 GrSurfaceProxyView texView = this->readSurfaceView();
Greg Daniel40903af2020-01-30 14:55:05 -05001242 if (!texView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -04001243 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect,
Brian Salomonc5243782020-04-02 12:50:34 -04001244 SkBackingFit::kApprox, SkBudgeted::kNo);
1245 if (!texView) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001246 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001247 }
Greg Daniel40903af2020-01-30 14:55:05 -05001248 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001249 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001250 }
1251
Brian Salomon1c86b632020-12-11 12:36:01 -05001252 SkISize finalSize = dstRect.size();
1253
Brian Salomonbf6b9792019-08-21 09:38:10 -04001254 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
1255 // 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 -05001256 std::unique_ptr<GrSurfaceFillContext> tempA;
1257 std::unique_ptr<GrSurfaceFillContext> tempB;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001258
Brian Salomone9ad9982019-07-22 16:17:41 -04001259 // Assume we should ignore the rescale linear request if the surface has no color space since
1260 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon63a0a752020-06-26 13:32:09 -04001261 if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() &&
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001262 !this->colorInfo().colorSpace()->gammaIsLinear()) {
1263 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomone9ad9982019-07-22 16:17:41 -04001264 // We'll fall back to kRGBA_8888 if half float not supported.
Brian Salomonbacbb922021-01-21 19:48:00 -05001265 GrImageInfo ii(GrColorType::kRGBA_F16,
1266 dst->colorInfo().alphaType(),
1267 std::move(cs),
1268 srcRect.size());
1269 auto linearRTC = GrSurfaceFillContext::MakeWithFallback(fContext,
1270 std::move(ii),
1271 SkBackingFit::kApprox,
1272 1,
1273 GrMipmapped::kNo,
1274 GrProtected::kNo,
1275 dst->origin());
Brian Salomone9ad9982019-07-22 16:17:41 -04001276 if (!linearRTC) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001277 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001278 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001279 auto fp = GrTextureEffect::Make(std::move(texView),
1280 this->colorInfo().alphaType(),
1281 SkMatrix::Translate(srcRect.topLeft()),
1282 GrSamplerState::Filter::kNearest,
1283 GrSamplerState::MipmapMode::kNone);
1284 fp = GrColorSpaceXformEffect::Make(std::move(fp),
1285 this->colorInfo(),
1286 linearRTC->colorInfo());
1287 linearRTC->fillWithFP(std::move(fp));
Greg Daniel40903af2020-01-30 14:55:05 -05001288 texView = linearRTC->readSurfaceView();
1289 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -04001290 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001291 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001292 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001293
Brian Salomon1c86b632020-12-11 12:36:01 -05001294 while (srcRect.size() != finalSize) {
1295 SkISize nextDims = finalSize;
Mike Reed1efa14d2021-01-02 21:44:59 -05001296 if (rescaleMode != RescaleMode::kNearest) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001297 if (srcRect.width() > finalSize.width()) {
1298 nextDims.fWidth = std::max((srcRect.width() + 1)/2, finalSize.width());
1299 } else if (srcRect.width() < finalSize.width()) {
1300 nextDims.fWidth = std::min(srcRect.width()*2, finalSize.width());
Brian Salomon59f31b12020-06-04 17:27:15 -04001301 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001302 if (srcRect.height() > finalSize.height()) {
1303 nextDims.fHeight = std::max((srcRect.height() + 1)/2, finalSize.height());
1304 } else if (srcRect.height() < finalSize.height()) {
1305 nextDims.fHeight = std::min(srcRect.height()*2, finalSize.height());
Brian Salomon59f31b12020-06-04 17:27:15 -04001306 }
1307 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001308 auto input = tempA ? tempA.get() : this;
Brian Salomone9ad9982019-07-22 16:17:41 -04001309 sk_sp<GrColorSpaceXform> xform;
Brian Salomonbacbb922021-01-21 19:48:00 -05001310 GrSurfaceFillContext* stepDst;
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001311 SkIRect stepDstRect;
Brian Salomon1c86b632020-12-11 12:36:01 -05001312 if (nextDims == finalSize) {
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001313 stepDst = dst;
1314 stepDstRect = dstRect;
Brian Salomonbacbb922021-01-21 19:48:00 -05001315 xform = GrColorSpaceXform::Make(input->colorInfo(), dst->colorInfo());
Brian Salomon1c86b632020-12-11 12:36:01 -05001316 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -05001317 GrImageInfo nextInfo(input->colorInfo(), nextDims);
1318 tempB = GrSurfaceFillContext::MakeWithFallback(fContext,
1319 nextInfo,
1320 SkBackingFit::kApprox);
Brian Salomon1c86b632020-12-11 12:36:01 -05001321 if (!tempB) {
1322 return false;
1323 }
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001324 stepDst = tempB.get();
1325 stepDstRect = SkIRect::MakeSize(tempB->dimensions());
Brian Salomone9ad9982019-07-22 16:17:41 -04001326 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001327 std::unique_ptr<GrFragmentProcessor> fp;
Mike Reed1efa14d2021-01-02 21:44:59 -05001328 if (rescaleMode == RescaleMode::kRepeatedCubic) {
Brian Salomon59f31b12020-06-04 17:27:15 -04001329 auto dir = GrBicubicEffect::Direction::kXY;
1330 if (nextDims.width() == srcRect.width()) {
1331 dir = GrBicubicEffect::Direction::kY;
1332 } else if (nextDims.height() == srcRect.height()) {
1333 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -04001334 }
Brian Salomon1af72d12020-06-25 10:47:26 -04001335 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
Mike Reed3867c702020-09-01 13:28:10 -04001336 static constexpr auto kKernel = GrBicubicEffect::gCatmullRom;
Brian Salomon1c86b632020-12-11 12:36:01 -05001337 fp = GrBicubicEffect::MakeSubset(std::move(texView),
1338 input->colorInfo().alphaType(),
Brian Salomonbacbb922021-01-21 19:48:00 -05001339 SkMatrix::I(),
Brian Salomon1c86b632020-12-11 12:36:01 -05001340 kWM,
1341 kWM,
1342 SkRect::Make(srcRect),
1343 kKernel,
1344 dir,
1345 *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -04001346 } else {
Mike Reed1efa14d2021-01-02 21:44:59 -05001347 auto filter = rescaleMode == RescaleMode::kNearest ? GrSamplerState::Filter::kNearest
1348 : GrSamplerState::Filter::kLinear;
Brian Salomonbacbb922021-01-21 19:48:00 -05001349 auto srcRectF = SkRect::Make(srcRect);
1350 fp = GrTextureEffect::MakeSubset(std::move(texView),
1351 this->colorInfo().alphaType(),
1352 SkMatrix::I(),
1353 {filter, GrSamplerState::MipmapMode::kNone},
1354 srcRectF,
1355 srcRectF,
1356 *this->caps());
Brian Salomone9ad9982019-07-22 16:17:41 -04001357 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001358 if (xform) {
1359 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
1360 }
1361 stepDst->fillRectToRectWithFP(srcRect, stepDstRect, std::move(fp));
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001362 texView = stepDst->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -04001363 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001364 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomone9ad9982019-07-22 16:17:41 -04001365 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001366 return true;
Brian Salomone9ad9982019-07-22 16:17:41 -04001367}
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001368
1369GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
1370 const SkIRect& rect) {
1371 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1372 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001373 auto direct = fContext->asDirectContext();
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001374 if (!direct) {
1375 return {};
1376 }
1377 auto rtProxy = this->asRenderTargetProxy();
1378 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1379 return {};
1380 }
1381
1382 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001383 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
1384 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001385 // Fail if read color type does not have all of dstCT's color channels and those missing color
1386 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001387 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1388 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
1389 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1390 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001391 return {};
1392 }
1393
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001394 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001395 !supportedRead.fOffsetAlignmentForTransferBuffer) {
1396 return {};
1397 }
1398
1399 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
1400 size_t size = rowBytes * rect.height();
Greg Daniel2e967df2021-02-08 10:38:31 -05001401 // By using kStream_GrAccessPattern here, we are not able to cache and reuse the buffer for
1402 // multiple reads. Switching to kDynamic_GrAccessPattern would allow for this, however doing
1403 // so causes a crash in a chromium test. See skbug.com/11297
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001404 auto buffer = direct->priv().resourceProvider()->createBuffer(
Greg Daniel393debc2021-02-06 03:37:20 +00001405 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001406 if (!buffer) {
1407 return {};
1408 }
1409 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -05001410 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001411 if (flip) {
1412 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1413 this->height() - rect.fTop);
1414 }
Greg Danielbbfec9d2019-08-20 10:56:51 -04001415 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001416 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -04001417 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001418 PixelTransferResult result;
1419 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001420 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -04001421 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001422 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
1423 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001424 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
1425 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon5392c942021-03-30 16:14:37 -04001426 GrConvertPixels( GrPixmap(dstInfo, dst, dstInfo.minRowBytes()),
1427 GrCPixmap(srcInfo, src, srcInfo.minRowBytes()));
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001428 };
1429 }
1430 return result;
1431}
Greg Daniel46e366a2019-12-16 14:38:36 -05001432
1433#ifdef SK_DEBUG
1434void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001435 SkASSERT(fReadView.proxy());
1436 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -04001437 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
1438 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
1439 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
1440 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001441 this->onValidate();
1442}
1443#endif