blob: 3eb0aa850557090a00b7d251963d69937d6a6ef8 [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 Salomon2c673402021-04-02 14:36:58 -0400358bool GrSurfaceContext::writePixels(GrDirectContext* dContext, GrCPixmap src, SkIPoint dstPt) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400359 ASSERT_SINGLE_OWNER
360 RETURN_FALSE_IF_ABANDONED
361 SkDEBUGCODE(this->validate();)
Brian Salomon2c673402021-04-02 14:36:58 -0400362
363 src = src.clip(this->dimensions(), &dstPt);
364 if (!src.hasPixels()) {
365 return false;
366 }
367 if (!src.info().bpp() || src.rowBytes() % src.info().bpp()) {
368 return false;
369 }
370 return this->internalWritePixels(dContext, &src, 1, dstPt);
371}
372
373bool GrSurfaceContext::writePixels(GrDirectContext* dContext,
374 const GrCPixmap src[],
375 int numLevels) {
376 ASSERT_SINGLE_OWNER
377 RETURN_FALSE_IF_ABANDONED
378 SkDEBUGCODE(this->validate();)
379
380 SkASSERT(dContext);
381 SkASSERT(numLevels >= 1);
382 SkASSERT(src);
383
384 if (numLevels == 1) {
385 if (src->dimensions() != this->dimensions()) {
386 return false;
387 }
388 return this->writePixels(dContext, src[0], {0, 0});
389 }
390 if (!this->asTextureProxy() || this->asTextureProxy()->proxyMipmapped() == GrMipmapped::kNo) {
391 return false;
392 }
393
394 SkISize dims = this->dimensions();
395 if (numLevels != SkMipmap::ComputeLevelCount(dims) + 1) {
396 return false;
397 }
398 for (int i = 0; i < numLevels; ++i) {
399 if (src[i].colorInfo() != src[0].colorInfo()) {
400 return false;
401 }
402 if (dims != src[i].dimensions()) {
403 return false;
404 }
405 if (!src[i].info().bpp() || src[i].rowBytes() % src[i].info().bpp()) {
406 return false;
407 }
408 dims = {std::max(1, dims.width()/2), std::max(1, dims.height()/2)};
409 }
410 return this->internalWritePixels(dContext, src, numLevels, {0, 0});
411}
412
413bool GrSurfaceContext::internalWritePixels(GrDirectContext* dContext,
414 const GrCPixmap src[],
415 int numLevels,
416 SkIPoint pt) {
417 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::internalWritePixels");
418
419 SkASSERT(numLevels >= 1);
420 SkASSERT(src);
421
422 // We can either write to a subset or write MIP levels, but not both.
423 SkASSERT((src[0].dimensions() == this->dimensions() && pt.isZero()) || numLevels == 1);
424 SkASSERT(numLevels == 1 ||
Brian Salomon7cde6c32021-04-02 15:53:42 -0400425 (this->asTextureProxy() && this->asTextureProxy()->mipmapped() == GrMipmapped::kYes));
Brian Salomon2c673402021-04-02 14:36:58 -0400426 // Our public caller should have clipped to the bounds of the surface already.
427 SkASSERT(SkIRect::MakeSize(this->dimensions())
428 .contains(SkIRect::MakePtSize(pt, src[0].dimensions())));
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400429
Adlai Hollerc95b5892020-08-11 12:02:22 -0400430 if (!dContext) {
Brian Salomonc320b152018-02-20 14:05:36 -0500431 return false;
432 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500433
Brian Salomon1d435302019-07-01 13:05:28 -0400434 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500435 return false;
436 }
437
Brian Salomon2c673402021-04-02 14:36:58 -0400438 if (src[0].colorType() == GrColorType::kUnknown) {
Brian Salomon46f63232021-01-25 10:13:35 -0500439 return false;
440 }
441
Brian Salomon2c673402021-04-02 14:36:58 -0400442 if (!alpha_types_compatible(src[0].alphaType(), this->colorInfo().alphaType())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400443 return false;
444 }
445
446 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
Stephen White3c0a50f2020-01-16 18:19:54 -0500447
448 if (dstProxy->framebufferOnly()) {
449 return false;
450 }
451
Adlai Hollerc95b5892020-08-11 12:02:22 -0400452 if (!dstProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400453 return false;
454 }
455
456 GrSurface* dstSurface = dstProxy->peekSurface();
457
Brian Salomondd4087d2020-12-23 20:36:44 -0500458 SkColorSpaceXformSteps::Flags flags =
Brian Salomon2c673402021-04-02 14:36:58 -0400459 SkColorSpaceXformSteps{src[0].colorInfo(), this->colorInfo()}.flags;
Mike Klein7321e6a2019-12-03 11:08:40 -0600460 bool unpremul = flags.unpremul,
461 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
462 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400463
Adlai Hollerc95b5892020-08-11 12:02:22 -0400464 const GrCaps* caps = dContext->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400465
466 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
467 GrRenderable::kNo);
468
Greg Danielc71c7962020-01-14 16:44:18 -0500469 GrColorType dstColorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400470 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
471 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400472 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
Brian Salomon2c673402021-04-02 14:36:58 -0400473 (src[0].colorType() == GrColorType::kRGBA_8888 ||
474 src[0].colorType() == GrColorType::kBGRA_8888) &&
Brian Salomon590f5672020-12-16 11:44:47 -0500475 this->asFillContext() &&
Greg Danielc71c7962020-01-14 16:44:18 -0500476 (dstColorType == GrColorType::kRGBA_8888 ||
477 dstColorType == GrColorType::kBGRA_8888) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400478 rgbaDefaultFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400479 dContext->priv().validPMUPMConversionExists();
Brian Salomon2c673402021-04-02 14:36:58 -0400480 // Drawing code path doesn't support writing to levels and doesn't support inserting layout
481 // transitions.
482 if ((!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) && numLevels == 1) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500483 GrColorInfo tempColorInfo;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400484 GrBackendFormat format;
Greg Danielbfa19c42019-12-19 16:41:40 -0500485 GrSwizzle tempReadSwizzle;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400486 if (canvas2DFastPath) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500487 tempColorInfo = {GrColorType::kRGBA_8888,
488 kUnpremul_SkAlphaType,
489 this->colorInfo().refColorSpace()};
Greg Daniel7bfc9132019-08-14 14:23:53 -0400490 format = rgbaDefaultFormat;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400491 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500492 tempColorInfo = this->colorInfo();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400493 format = dstProxy->backendFormat().makeTexture2D();
494 if (!format.isValid()) {
495 return false;
496 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500497 tempReadSwizzle = this->readSwizzle();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400498 }
499
Greg Daniel2e52ad12019-06-13 10:04:16 -0400500 // It is more efficient for us to write pixels into a top left origin so we prefer that.
501 // However, if the final proxy isn't a render target then we must use a copy to move the
502 // data into it which requires the origins to match. If the final proxy is a render target
503 // we can use a draw instead which doesn't have this origin restriction. Thus for render
504 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400505 GrSurfaceOrigin tempOrigin =
Brian Salomon590f5672020-12-16 11:44:47 -0500506 this->asFillContext() ? kTopLeft_GrSurfaceOrigin : this->origin();
Brian Salomon2c673402021-04-02 14:36:58 -0400507 auto tempProxy = dContext->priv().proxyProvider()->createProxy(format,
508 src[0].dimensions(),
509 GrRenderable::kNo,
510 1,
511 GrMipmapped::kNo,
512 SkBackingFit::kApprox,
513 SkBudgeted::kYes,
514 GrProtected::kNo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400515 if (!tempProxy) {
516 return false;
517 }
Greg Daniel3912a4b2020-01-14 09:56:04 -0500518 GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500519 GrSurfaceContext tempCtx(dContext, tempView, tempColorInfo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400520
521 // In the fast path we always write the srcData to the temp context as though it were RGBA.
522 // When the data is really BGRA the write will cause the R and B channels to be swapped in
523 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
524 // dst.
Brian Salomon2c673402021-04-02 14:36:58 -0400525 GrCPixmap origSrcBase = src[0];
526 GrCPixmap srcBase = origSrcBase;
Brian Salomon1d435302019-07-01 13:05:28 -0400527 if (canvas2DFastPath) {
Brian Salomon2c673402021-04-02 14:36:58 -0400528 srcBase = GrCPixmap(origSrcBase.info().makeColorType(GrColorType::kRGBA_8888),
529 origSrcBase.addr(),
530 origSrcBase.rowBytes());
Brian Salomon1d435302019-07-01 13:05:28 -0400531 }
Brian Salomon2c673402021-04-02 14:36:58 -0400532 if (!tempCtx.writePixels(dContext, srcBase, {0, 0})) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400533 return false;
534 }
535
Brian Salomon590f5672020-12-16 11:44:47 -0500536 if (this->asFillContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400537 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400538 if (canvas2DFastPath) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400539 fp = dContext->priv().createUPMToPMEffect(
Brian Salomon14f99fc2020-12-07 12:19:47 -0500540 GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType()));
Brian Salomon1d435302019-07-01 13:05:28 -0400541 // Important: check the original src color type here!
Brian Salomon2c673402021-04-02 14:36:58 -0400542 if (origSrcBase.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400543 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
544 }
545 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500546 fp = GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400547 }
548 if (!fp) {
549 return false;
550 }
Brian Salomon2c673402021-04-02 14:36:58 -0400551 this->asFillContext()->fillRectToRectWithFP(
552 SkIRect::MakeSize(srcBase.dimensions()),
553 SkIRect::MakePtSize(pt, srcBase.dimensions()),
554 std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400555 } else {
Brian Salomon2c673402021-04-02 14:36:58 -0400556 SkIRect srcRect = SkIRect::MakeSize(srcBase.dimensions());
Brian Salomon1d435302019-07-01 13:05:28 -0400557 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Brian Salomon982127b2021-01-21 10:43:35 -0500558 if (!this->copy(std::move(tempProxy), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400559 return false;
560 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400561 }
562 return true;
563 }
564
Brian Salomon2c673402021-04-02 14:36:58 -0400565 GrColorType srcColorType = src[0].colorType();
566 auto [allowedColorType, _] =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400567 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400568 dstProxy->backendFormat(),
Brian Salomon2c673402021-04-02 14:36:58 -0400569 srcColorType);
Greg Danielb8d84f82020-02-13 14:25:00 -0500570 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400571
Brian Salomon2c673402021-04-02 14:36:58 -0400572 bool convertAll = premul ||
573 unpremul ||
574 needColorConversion ||
575 flip ||
576 (srcColorType != allowedColorType);
577 bool mustBeTight = !caps->writePixelsRowBytesSupport();
578 size_t tmpSize = 0;
579 if (mustBeTight || convertAll) {
580 for (int i = 0; i < numLevels; ++i) {
581 if (convertAll || (mustBeTight && src[i].rowBytes() != src[i].info().minRowBytes())) {
582 tmpSize += src[i].info().makeColorType(allowedColorType).minRowBytes()*
583 src[i].height();
584 }
585 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400586 }
587
Brian Salomon2c673402021-04-02 14:36:58 -0400588 auto tmpData = tmpSize ? SkData::MakeUninitialized(tmpSize) : nullptr;
589 void* tmp = tmpSize ? tmpData->writable_data() : nullptr;
590 SkAutoSTArray<15, GrMipLevel> srcLevels(numLevels);
591 bool ownAllStorage = true;
592 for (int i = 0; i < numLevels; ++i) {
593 if (convertAll || (mustBeTight && src[i].rowBytes() != src[i].info().minRowBytes())) {
594 GrImageInfo tmpInfo(allowedColorType,
595 this->colorInfo().alphaType(),
596 this->colorInfo().refColorSpace(),
597 src[i].dimensions());
598 auto tmpRB = tmpInfo.minRowBytes();
599 GrPixmap tmpPM(tmpInfo, tmp, tmpRB);
600 SkAssertResult(GrConvertPixels(tmpPM, src[i], flip));
601 srcLevels[i] = {tmpPM.addr(), tmpPM.rowBytes(), tmpData};
602 tmp = SkTAddOffset<void>(tmp, tmpRB*tmpPM.height());
603 } else {
604 srcLevels[i] = {src[i].addr(), src[i].rowBytes(), src[i].pixelStorage()};
605 ownAllStorage &= src[i].ownsPixels();
606 }
607 }
608 pt.fY = flip ? dstSurface->height() - pt.fY - src[0].height() : pt.fY;
609
610 if (!dContext->priv().drawingManager()->newWritePixelsTask(
611 sk_ref_sp(dstProxy),
612 SkIRect::MakePtSize(pt, src[0].dimensions()),
613 this->colorInfo().colorType(),
614 allowedColorType,
615 srcLevels.begin(),
616 numLevels)) {
617 return false;
618 }
619 if (numLevels > 1) {
620 dstProxy->asTextureProxy()->markMipmapsClean();
621 }
622 if (!ownAllStorage) {
623 // If any pixmap doesn't own its pixels then we must flush so that the pixels are pushed to
624 // the GPU before we return.
Brian Salomon24949422021-02-11 09:39:46 -0500625 dContext->priv().flushSurface(dstProxy);
626 }
Brian Salomon2c673402021-04-02 14:36:58 -0400627 return true;
Brian Osman45580d32016-11-23 09:37:01 -0500628}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400629
Adlai Hollerc95b5892020-08-11 12:02:22 -0400630void GrSurfaceContext::asyncRescaleAndReadPixels(GrDirectContext* dContext,
631 const SkImageInfo& info,
Brian Salomon63a0a752020-06-26 13:32:09 -0400632 const SkIRect& srcRect,
633 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500634 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400635 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400636 ReadPixelsContext callbackContext) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400637 if (!dContext) {
638 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400639 return;
640 }
641 auto rt = this->asRenderTargetProxy();
642 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400643 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400644 return;
645 }
646 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400647 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400648 return;
649 }
650 auto dstCT = SkColorTypeToGrColorType(info.colorType());
651 if (dstCT == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400652 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400653 return;
654 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500655 bool needsRescale = srcRect.size() != info.dimensions();
Brian Salomon63a0a752020-06-26 13:32:09 -0400656 auto colorTypeOfFinalContext = this->colorInfo().colorType();
657 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
658 if (needsRescale) {
659 colorTypeOfFinalContext = dstCT;
660 backendFormatOfFinalContext =
661 this->caps()->getDefaultBackendFormat(dstCT, GrRenderable::kYes);
662 }
663 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonbacbb922021-01-21 19:48:00 -0500664 backendFormatOfFinalContext,
665 dstCT);
Brian Salomon63a0a752020-06-26 13:32:09 -0400666 // Fail if we can't read from the source surface's color type.
667 if (readInfo.fColorType == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400668 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400669 return;
670 }
671 // Fail if read color type does not have all of dstCT's color channels and those missing color
672 // channels are in the src.
673 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
674 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
675 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
676 if ((~legalReadChannels & dstChannels) & srcChannels) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400677 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400678 return;
679 }
680
Brian Salomonbacbb922021-01-21 19:48:00 -0500681 std::unique_ptr<GrSurfaceFillContext> tempFC;
Brian Salomon63a0a752020-06-26 13:32:09 -0400682 int x = srcRect.fLeft;
683 int y = srcRect.fTop;
684 if (needsRescale) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500685 tempFC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma, rescaleMode);
686 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400687 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400688 return;
689 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500690 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
691 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400692 x = y = 0;
693 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -0500694 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo(),
695 info.colorInfo());
Brian Salomon63a0a752020-06-26 13:32:09 -0400696 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
697 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
698 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomonbacbb922021-01-21 19:48:00 -0500699 SkIRect srcRectToDraw = srcRect;
Brian Salomon63a0a752020-06-26 13:32:09 -0400700 // If the src is not texturable first try to make a copy to a texture.
701 if (!texProxyView.asTextureProxy()) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500702 texProxyView = GrSurfaceProxyView::Copy(fContext,
703 texProxyView,
704 GrMipmapped::kNo,
705 srcRect,
706 SkBackingFit::kApprox,
707 SkBudgeted::kNo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400708 if (!texProxyView) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400709 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400710 return;
711 }
712 SkASSERT(texProxyView.asTextureProxy());
Brian Salomonbacbb922021-01-21 19:48:00 -0500713 srcRectToDraw = SkIRect::MakeSize(srcRect.size());
Brian Salomon63a0a752020-06-26 13:32:09 -0400714 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500715 auto tempInfo = GrImageInfo(info).makeColorType(this->colorInfo().colorType());
716 tempFC = GrSurfaceFillContext::Make(dContext, tempInfo, SkBackingFit::kApprox);
717 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400718 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400719 return;
720 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500721 auto fp = GrTextureEffect::Make(std::move(texProxyView), this->colorInfo().alphaType());
722 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
723 tempFC->fillRectToRectWithFP(srcRectToDraw,
724 SkIRect::MakeSize(tempFC->dimensions()),
725 std::move(fp));
Brian Salomon63a0a752020-06-26 13:32:09 -0400726 x = y = 0;
727 }
728 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500729 auto srcCtx = tempFC ? tempFC.get() : this;
730 return srcCtx->asyncReadPixels(dContext,
731 SkIRect::MakePtSize({x, y}, info.dimensions()),
732 info.colorType(),
733 callback,
734 callbackContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400735}
736
737class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult {
738public:
Robert Phillips82ad7af2021-03-11 16:00:10 -0500739 AsyncReadResult(GrDirectContext::DirectContextID intendedRecipient)
740 : fIntendedRecipient(intendedRecipient) {
741 }
742
Brian Salomon63a0a752020-06-26 13:32:09 -0400743 ~AsyncReadResult() override {
744 for (int i = 0; i < fPlanes.count(); ++i) {
Robert Phillips82ad7af2021-03-11 16:00:10 -0500745 fPlanes[i].releaseMappedBuffer(fIntendedRecipient);
Brian Salomon63a0a752020-06-26 13:32:09 -0400746 }
747 }
748
749 int count() const override { return fPlanes.count(); }
Brian Salomonbe1084b2021-01-26 13:29:30 -0500750 const void* data(int i) const override { return fPlanes[i].data(); }
751 size_t rowBytes(int i) const override { return fPlanes[i].rowBytes(); }
Brian Salomon63a0a752020-06-26 13:32:09 -0400752
753 bool addTransferResult(const PixelTransferResult& result,
754 SkISize dimensions,
755 size_t rowBytes,
756 GrClientMappedBufferManager* manager) {
757 SkASSERT(!result.fTransferBuffer->isMapped());
758 const void* mappedData = result.fTransferBuffer->map();
759 if (!mappedData) {
760 return false;
761 }
762 if (result.fPixelConverter) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500763 size_t size = rowBytes*dimensions.height();
764 sk_sp<SkData> data = SkData::MakeUninitialized(size);
765 result.fPixelConverter(data->writable_data(), mappedData);
766 this->addCpuPlane(std::move(data), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400767 result.fTransferBuffer->unmap();
768 } else {
769 manager->insert(result.fTransferBuffer);
770 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
771 }
772 return true;
773 }
774
Brian Salomonbe1084b2021-01-26 13:29:30 -0500775 void addCpuPlane(sk_sp<SkData> data, size_t rowBytes) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400776 SkASSERT(data);
777 SkASSERT(rowBytes > 0);
Brian Salomonbe1084b2021-01-26 13:29:30 -0500778 fPlanes.emplace_back(std::move(data), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400779 }
780
781private:
782 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
783 SkASSERT(data);
784 SkASSERT(rowBytes > 0);
785 SkASSERT(mappedBuffer);
786 SkASSERT(mappedBuffer->isMapped());
Brian Salomonbe1084b2021-01-26 13:29:30 -0500787 fPlanes.emplace_back(std::move(mappedBuffer), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400788 }
789
Brian Salomonbe1084b2021-01-26 13:29:30 -0500790 class Plane {
791 public:
792 Plane(sk_sp<GrGpuBuffer> buffer, size_t rowBytes)
793 : fMappedBuffer(std::move(buffer)), fRowBytes(rowBytes) {}
794 Plane(sk_sp<SkData> data, size_t rowBytes) : fData(std::move(data)), fRowBytes(rowBytes) {}
795
796 Plane(const Plane&) = delete;
797 Plane(Plane&&) = default;
798
799 ~Plane() { SkASSERT(!fMappedBuffer); }
800
801 Plane& operator=(const Plane&) = delete;
802 Plane& operator=(Plane&&) = default;
803
Robert Phillips82ad7af2021-03-11 16:00:10 -0500804 void releaseMappedBuffer(GrDirectContext::DirectContextID intendedRecipient) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500805 if (fMappedBuffer) {
806 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
Robert Phillips82ad7af2021-03-11 16:00:10 -0500807 {std::move(fMappedBuffer), intendedRecipient});
Brian Salomonbe1084b2021-01-26 13:29:30 -0500808 }
809 }
810
811 const void* data() const {
812 if (fMappedBuffer) {
813 SkASSERT(!fData);
814 SkASSERT(fMappedBuffer->isMapped());
815 return fMappedBuffer->map();
816 }
817 SkASSERT(fData);
818 return fData->data();
819 }
820
821 size_t rowBytes() const { return fRowBytes; }
822
823 private:
824 sk_sp<SkData> fData;
Brian Salomon1eea1ea2021-01-26 18:12:25 +0000825 sk_sp<GrGpuBuffer> fMappedBuffer;
Brian Salomonbe1084b2021-01-26 13:29:30 -0500826 size_t fRowBytes;
Brian Salomon63a0a752020-06-26 13:32:09 -0400827 };
828 SkSTArray<3, Plane> fPlanes;
Robert Phillips82ad7af2021-03-11 16:00:10 -0500829 GrDirectContext::DirectContextID fIntendedRecipient;
Brian Salomon63a0a752020-06-26 13:32:09 -0400830};
831
Adlai Hollerc95b5892020-08-11 12:02:22 -0400832void GrSurfaceContext::asyncReadPixels(GrDirectContext* dContext,
833 const SkIRect& rect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400834 SkColorType colorType,
835 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400836 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400837 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
838 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
839
Adlai Hollerc95b5892020-08-11 12:02:22 -0400840 if (!dContext || this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
841 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400842 return;
843 }
844
Adlai Hollerc95b5892020-08-11 12:02:22 -0400845 auto mappedBufferManager = dContext->priv().clientMappedBufferManager();
Brian Salomon63a0a752020-06-26 13:32:09 -0400846
847 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
848
849 if (!transferResult.fTransferBuffer) {
850 auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
851 this->colorInfo().refColorSpace());
Robert Phillips82ad7af2021-03-11 16:00:10 -0500852 static const GrDirectContext::DirectContextID kInvalid;
853 auto result = std::make_unique<AsyncReadResult>(kInvalid);
Brian Salomonbe1084b2021-01-26 13:29:30 -0500854 GrPixmap pm = GrPixmap::Allocate(ii);
855 result->addCpuPlane(pm.pixelStorage(), pm.rowBytes());
Brian Salomon63a0a752020-06-26 13:32:09 -0400856
Adlai Hollerc95b5892020-08-11 12:02:22 -0400857 SkIPoint pt{rect.fLeft, rect.fTop};
Brian Salomondd4087d2020-12-23 20:36:44 -0500858 if (!this->readPixels(dContext, pm, pt)) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400859 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400860 return;
861 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400862 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400863 return;
864 }
865
866 struct FinishContext {
867 ReadPixelsCallback* fClientCallback;
868 ReadPixelsContext fClientContext;
869 SkISize fSize;
870 SkColorType fColorType;
871 GrClientMappedBufferManager* fMappedBufferManager;
872 PixelTransferResult fTransferResult;
873 };
874 // Assumption is that the caller would like to flush. We could take a parameter or require an
875 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
876 // callback to GrGpu until after the next flush that flushes our op list, though.
877 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400878 callbackContext,
Brian Salomon63a0a752020-06-26 13:32:09 -0400879 rect.size(),
880 colorType,
881 mappedBufferManager,
882 std::move(transferResult)};
883 auto finishCallback = [](GrGpuFinishedContext c) {
884 const auto* context = reinterpret_cast<const FinishContext*>(c);
Robert Phillips82ad7af2021-03-11 16:00:10 -0500885 auto manager = context->fMappedBufferManager;
886 auto result = std::make_unique<AsyncReadResult>(manager->owningDirectContext());
Brian Salomon63a0a752020-06-26 13:32:09 -0400887 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
888 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
Robert Phillips82ad7af2021-03-11 16:00:10 -0500889 manager)) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400890 result.reset();
891 }
892 (*context->fClientCallback)(context->fClientContext, std::move(result));
893 delete context;
894 };
895 GrFlushInfo flushInfo;
896 flushInfo.fFinishedContext = finishContext;
897 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -0500898
899 dContext->priv().flushSurface(this->asSurfaceProxy(),
900 SkSurface::BackendSurfaceAccess::kNoAccess,
901 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400902}
903
Adlai Hollerc95b5892020-08-11 12:02:22 -0400904void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(GrDirectContext* dContext,
905 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400906 sk_sp<SkColorSpace> dstColorSpace,
907 const SkIRect& srcRect,
908 SkISize dstSize,
909 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500910 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400911 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400912 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400913 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
914 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
915 SkASSERT(!dstSize.isZero());
916 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
917
Adlai Hollerc95b5892020-08-11 12:02:22 -0400918 if (!dContext) {
919 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400920 return;
921 }
922 auto rt = this->asRenderTargetProxy();
923 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400924 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400925 return;
926 }
927 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400928 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400929 return;
930 }
931 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400932 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400933 return;
934 }
935 int x = srcRect.fLeft;
936 int y = srcRect.fTop;
937 bool needsRescale = srcRect.size() != dstSize;
938 GrSurfaceProxyView srcView;
Brian Salomonbacbb922021-01-21 19:48:00 -0500939 auto info = SkImageInfo::Make(dstSize,
940 kRGBA_8888_SkColorType,
941 this->colorInfo().alphaType(),
942 dstColorSpace);
Brian Salomon63a0a752020-06-26 13:32:09 -0400943 if (needsRescale) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400944 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomonbacbb922021-01-21 19:48:00 -0500945 auto tempFC = this->rescale(info,
946 kTopLeft_GrSurfaceOrigin,
947 srcRect,
948 rescaleGamma,
949 rescaleMode);
950 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400951 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400952 return;
953 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500954 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
955 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400956 x = y = 0;
Brian Salomonbacbb922021-01-21 19:48:00 -0500957 srcView = tempFC->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400958 } else {
959 srcView = this->readSurfaceView();
960 if (!srcView.asTextureProxy()) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500961 srcView = GrSurfaceProxyView::Copy(fContext,
962 std::move(srcView),
963 GrMipmapped::kNo,
964 srcRect,
965 SkBackingFit::kApprox,
966 SkBudgeted::kYes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400967 if (!srcView) {
968 // If we can't get a texture copy of the contents then give up.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400969 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400970 return;
971 }
972 SkASSERT(srcView.asTextureProxy());
973 x = y = 0;
974 }
975 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomonbacbb922021-01-21 19:48:00 -0500976 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo(),
977 info.colorInfo());
Brian Salomon63a0a752020-06-26 13:32:09 -0400978 if (xform) {
979 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
Brian Salomonbacbb922021-01-21 19:48:00 -0500980 auto tempFC = GrSurfaceFillContext::Make(dContext,
981 info,
982 SkBackingFit::kApprox,
983 1,
984 GrMipmapped::kNo,
985 GrProtected::kNo,
986 kTopLeft_GrSurfaceOrigin);
987 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400988 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400989 return;
990 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500991 auto fp = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType());
992 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
993 tempFC->fillRectToRectWithFP(srcRectToDraw,
994 SkIRect::MakeSize(tempFC->dimensions()),
995 std::move(fp));
996 srcView = tempFC->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400997 SkASSERT(srcView.asTextureProxy());
998 x = y = 0;
999 }
1000 }
1001
Brian Salomonbacbb922021-01-21 19:48:00 -05001002 auto yInfo = SkImageInfo::MakeA8(dstSize);
1003 auto yFC = GrSurfaceFillContext::MakeWithFallback(dContext, yInfo, SkBackingFit::kApprox);
1004
1005 auto uvInfo = yInfo.makeWH(yInfo.width()/2, yInfo.height()/2);
1006 auto uFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
1007 auto vFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
1008
1009 if (!yFC || !uFC || !vFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001010 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001011 return;
1012 }
1013
1014 float baseM[20];
1015 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
1016
1017 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
1018
1019 auto texMatrix = SkMatrix::Translate(x, y);
1020
Brian Salomon63a0a752020-06-26 13:32:09 -04001021 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
1022 PixelTransferResult yTransfer, uTransfer, vTransfer;
1023
1024 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
1025 float yM[20];
1026 std::fill_n(yM, 15, 0.f);
1027 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -05001028
1029 auto yFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
1030 yFP = GrColorMatrixFragmentProcessor::Make(std::move(yFP),
1031 yM,
1032 /*unpremulInput=*/false,
1033 /*clampRGBOutput=*/true,
1034 /*premulOutput=*/false);
1035 yFC->fillWithFP(std::move(yFP));
Brian Salomon63a0a752020-06-26 13:32:09 -04001036 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001037 yTransfer = yFC->transferPixels(GrColorType::kAlpha_8,
1038 SkIRect::MakeSize(yFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -04001039 if (!yTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001040 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001041 return;
1042 }
1043 }
1044
1045 texMatrix.preScale(2.f, 2.f);
1046 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
1047 float uM[20];
1048 std::fill_n(uM, 15, 0.f);
1049 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -05001050
1051 auto uFP = GrTextureEffect::Make(srcView,
1052 this->colorInfo().alphaType(),
1053 texMatrix,
1054 GrSamplerState::Filter::kLinear);
1055 uFP = GrColorMatrixFragmentProcessor::Make(std::move(uFP),
1056 uM,
1057 /*unpremulInput=*/false,
1058 /*clampRGBOutput=*/true,
1059 /*premulOutput=*/false);
1060 uFC->fillWithFP(std::move(uFP));
Brian Salomon63a0a752020-06-26 13:32:09 -04001061 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001062 uTransfer = uFC->transferPixels(GrColorType::kAlpha_8,
1063 SkIRect::MakeSize(uFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -04001064 if (!uTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001065 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001066 return;
1067 }
1068 }
1069
1070 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
1071 float vM[20];
1072 std::fill_n(vM, 15, 0.f);
1073 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -05001074 auto vFP = GrTextureEffect::Make(std::move(srcView),
1075 this->colorInfo().alphaType(),
1076 texMatrix,
1077 GrSamplerState::Filter::kLinear);
1078 vFP = GrColorMatrixFragmentProcessor::Make(std::move(vFP),
1079 vM,
1080 /*unpremulInput=*/false,
1081 /*clampRGBOutput=*/true,
1082 /*premulOutput=*/false);
1083 vFC->fillWithFP(std::move(vFP));
1084
Brian Salomon63a0a752020-06-26 13:32:09 -04001085 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001086 vTransfer = vFC->transferPixels(GrColorType::kAlpha_8,
1087 SkIRect::MakeSize(vFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -04001088 if (!vTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001089 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001090 return;
1091 }
1092 }
1093
1094 if (doSynchronousRead) {
Brian Salomonbe1084b2021-01-26 13:29:30 -05001095 GrPixmap yPmp = GrPixmap::Allocate(yInfo);
1096 GrPixmap uPmp = GrPixmap::Allocate(uvInfo);
1097 GrPixmap vPmp = GrPixmap::Allocate(uvInfo);
Brian Salomonbacbb922021-01-21 19:48:00 -05001098 if (!yFC->readPixels(dContext, yPmp, {0, 0}) ||
1099 !uFC->readPixels(dContext, uPmp, {0, 0}) ||
1100 !vFC->readPixels(dContext, vPmp, {0, 0})) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001101 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001102 return;
1103 }
Robert Phillips82ad7af2021-03-11 16:00:10 -05001104 auto result = std::make_unique<AsyncReadResult>(dContext->directContextID());
Brian Salomonbe1084b2021-01-26 13:29:30 -05001105 result->addCpuPlane(yPmp.pixelStorage(), yPmp.rowBytes());
1106 result->addCpuPlane(uPmp.pixelStorage(), uPmp.rowBytes());
1107 result->addCpuPlane(vPmp.pixelStorage(), vPmp.rowBytes());
Adlai Hollerc95b5892020-08-11 12:02:22 -04001108 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -04001109 return;
1110 }
1111
1112 struct FinishContext {
1113 ReadPixelsCallback* fClientCallback;
1114 ReadPixelsContext fClientContext;
1115 GrClientMappedBufferManager* fMappedBufferManager;
1116 SkISize fSize;
1117 PixelTransferResult fYTransfer;
1118 PixelTransferResult fUTransfer;
1119 PixelTransferResult fVTransfer;
1120 };
1121 // Assumption is that the caller would like to flush. We could take a parameter or require an
1122 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1123 // callback to GrGpu until after the next flush that flushes our op list, though.
1124 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -04001125 callbackContext,
1126 dContext->priv().clientMappedBufferManager(),
Brian Salomon63a0a752020-06-26 13:32:09 -04001127 dstSize,
1128 std::move(yTransfer),
1129 std::move(uTransfer),
1130 std::move(vTransfer)};
1131 auto finishCallback = [](GrGpuFinishedContext c) {
1132 const auto* context = reinterpret_cast<const FinishContext*>(c);
Brian Salomon63a0a752020-06-26 13:32:09 -04001133 auto manager = context->fMappedBufferManager;
Robert Phillips82ad7af2021-03-11 16:00:10 -05001134 auto result = std::make_unique<AsyncReadResult>(manager->owningDirectContext());
Brian Salomon63a0a752020-06-26 13:32:09 -04001135 size_t rowBytes = SkToSizeT(context->fSize.width());
1136 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
1137 (*context->fClientCallback)(context->fClientContext, nullptr);
1138 delete context;
1139 return;
1140 }
1141 rowBytes /= 2;
1142 SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
1143 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
1144 (*context->fClientCallback)(context->fClientContext, nullptr);
1145 delete context;
1146 return;
1147 }
1148 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
1149 (*context->fClientCallback)(context->fClientContext, nullptr);
1150 delete context;
1151 return;
1152 }
1153 (*context->fClientCallback)(context->fClientContext, std::move(result));
1154 delete context;
1155 };
1156 GrFlushInfo flushInfo;
1157 flushInfo.fFinishedContext = finishContext;
1158 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -05001159 dContext->priv().flushSurface(this->asSurfaceProxy(),
1160 SkSurface::BackendSurfaceAccess::kNoAccess,
1161 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -04001162}
1163
Brian Salomond63638b2021-03-05 14:00:07 -05001164sk_sp<GrRenderTask> GrSurfaceContext::copy(sk_sp<GrSurfaceProxy> src,
1165 SkIRect srcRect,
1166 SkIPoint dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001167 ASSERT_SINGLE_OWNER
Brian Salomond63638b2021-03-05 14:00:07 -05001168 RETURN_NULLPTR_IF_ABANDONED
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001169 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -04001170 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -04001171
Brian Salomon947efe22019-07-16 15:36:11 -04001172 const GrCaps* caps = fContext->priv().caps();
1173
Greg Daniel46cfbc62019-06-07 11:43:30 -04001174 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -05001175 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -04001176
Stephen White3c0a50f2020-01-16 18:19:54 -05001177 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomond63638b2021-03-05 14:00:07 -05001178 return nullptr;
Stephen White3c0a50f2020-01-16 18:19:54 -05001179 }
1180
Brian Salomon982127b2021-01-21 10:43:35 -05001181 if (!caps->canCopySurface(this->asSurfaceProxy(), src.get(), srcRect, dstPoint)) {
Brian Salomond63638b2021-03-05 14:00:07 -05001182 return nullptr;
Greg Daniel25af6712018-04-25 10:44:38 -04001183 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001184
Brian Salomon982127b2021-01-21 10:43:35 -05001185 return this->drawingManager()->newCopyRenderTask(std::move(src),
1186 srcRect,
1187 this->asSurfaceProxyRef(),
Brian Salomon0f9f8002021-01-22 16:30:50 -05001188 dstPoint,
1189 this->origin());
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001190}
Greg Daniel46cfbc62019-06-07 11:43:30 -04001191
Brian Salomonbacbb922021-01-21 19:48:00 -05001192std::unique_ptr<GrSurfaceFillContext> GrSurfaceContext::rescale(const GrImageInfo& info,
Brian Salomoneebe7352020-12-09 16:37:04 -05001193 GrSurfaceOrigin origin,
1194 SkIRect srcRect,
1195 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001196 RescaleMode rescaleMode) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001197 auto sfc = GrSurfaceFillContext::MakeWithFallback(fContext,
1198 info,
Brian Salomon1c86b632020-12-11 12:36:01 -05001199 SkBackingFit::kExact,
Brian Salomon1c86b632020-12-11 12:36:01 -05001200 1,
1201 GrMipmapped::kNo,
1202 this->asSurfaceProxy()->isProtected(),
1203 origin);
Brian Salomonbacbb922021-01-21 19:48:00 -05001204 if (!sfc || !this->rescaleInto(sfc.get(),
1205 SkIRect::MakeSize(sfc->dimensions()),
Brian Salomon1c86b632020-12-11 12:36:01 -05001206 srcRect,
1207 rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001208 rescaleMode)) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001209 return nullptr;
1210 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001211 return sfc;
Brian Salomon1c86b632020-12-11 12:36:01 -05001212}
1213
Brian Salomonbacbb922021-01-21 19:48:00 -05001214bool GrSurfaceContext::rescaleInto(GrSurfaceFillContext* dst,
Brian Salomon1c86b632020-12-11 12:36:01 -05001215 SkIRect dstRect,
1216 SkIRect srcRect,
1217 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001218 RescaleMode rescaleMode) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001219 SkASSERT(dst);
1220 if (!SkIRect::MakeSize(dst->dimensions()).contains((dstRect))) {
1221 return false;
1222 }
1223
Brian Salomone9ad9982019-07-22 16:17:41 -04001224 auto rtProxy = this->asRenderTargetProxy();
1225 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001226 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001227 }
1228
Stephen White3c0a50f2020-01-16 18:19:54 -05001229 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001230 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001231 }
1232
Greg Daniel40903af2020-01-30 14:55:05 -05001233 GrSurfaceProxyView texView = this->readSurfaceView();
Greg Daniel40903af2020-01-30 14:55:05 -05001234 if (!texView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -04001235 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect,
Brian Salomonc5243782020-04-02 12:50:34 -04001236 SkBackingFit::kApprox, SkBudgeted::kNo);
1237 if (!texView) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001238 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001239 }
Greg Daniel40903af2020-01-30 14:55:05 -05001240 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001241 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001242 }
1243
Brian Salomon1c86b632020-12-11 12:36:01 -05001244 SkISize finalSize = dstRect.size();
1245
Brian Salomonbf6b9792019-08-21 09:38:10 -04001246 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
1247 // 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 -05001248 std::unique_ptr<GrSurfaceFillContext> tempA;
1249 std::unique_ptr<GrSurfaceFillContext> tempB;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001250
Brian Salomone9ad9982019-07-22 16:17:41 -04001251 // Assume we should ignore the rescale linear request if the surface has no color space since
1252 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon63a0a752020-06-26 13:32:09 -04001253 if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() &&
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001254 !this->colorInfo().colorSpace()->gammaIsLinear()) {
1255 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomone9ad9982019-07-22 16:17:41 -04001256 // We'll fall back to kRGBA_8888 if half float not supported.
Brian Salomonbacbb922021-01-21 19:48:00 -05001257 GrImageInfo ii(GrColorType::kRGBA_F16,
1258 dst->colorInfo().alphaType(),
1259 std::move(cs),
1260 srcRect.size());
1261 auto linearRTC = GrSurfaceFillContext::MakeWithFallback(fContext,
1262 std::move(ii),
1263 SkBackingFit::kApprox,
1264 1,
1265 GrMipmapped::kNo,
1266 GrProtected::kNo,
1267 dst->origin());
Brian Salomone9ad9982019-07-22 16:17:41 -04001268 if (!linearRTC) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001269 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001270 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001271 auto fp = GrTextureEffect::Make(std::move(texView),
1272 this->colorInfo().alphaType(),
1273 SkMatrix::Translate(srcRect.topLeft()),
1274 GrSamplerState::Filter::kNearest,
1275 GrSamplerState::MipmapMode::kNone);
1276 fp = GrColorSpaceXformEffect::Make(std::move(fp),
1277 this->colorInfo(),
1278 linearRTC->colorInfo());
1279 linearRTC->fillWithFP(std::move(fp));
Greg Daniel40903af2020-01-30 14:55:05 -05001280 texView = linearRTC->readSurfaceView();
1281 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -04001282 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001283 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001284 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001285
Brian Salomon1c86b632020-12-11 12:36:01 -05001286 while (srcRect.size() != finalSize) {
1287 SkISize nextDims = finalSize;
Mike Reed1efa14d2021-01-02 21:44:59 -05001288 if (rescaleMode != RescaleMode::kNearest) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001289 if (srcRect.width() > finalSize.width()) {
1290 nextDims.fWidth = std::max((srcRect.width() + 1)/2, finalSize.width());
1291 } else if (srcRect.width() < finalSize.width()) {
1292 nextDims.fWidth = std::min(srcRect.width()*2, finalSize.width());
Brian Salomon59f31b12020-06-04 17:27:15 -04001293 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001294 if (srcRect.height() > finalSize.height()) {
1295 nextDims.fHeight = std::max((srcRect.height() + 1)/2, finalSize.height());
1296 } else if (srcRect.height() < finalSize.height()) {
1297 nextDims.fHeight = std::min(srcRect.height()*2, finalSize.height());
Brian Salomon59f31b12020-06-04 17:27:15 -04001298 }
1299 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001300 auto input = tempA ? tempA.get() : this;
Brian Salomone9ad9982019-07-22 16:17:41 -04001301 sk_sp<GrColorSpaceXform> xform;
Brian Salomonbacbb922021-01-21 19:48:00 -05001302 GrSurfaceFillContext* stepDst;
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001303 SkIRect stepDstRect;
Brian Salomon1c86b632020-12-11 12:36:01 -05001304 if (nextDims == finalSize) {
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001305 stepDst = dst;
1306 stepDstRect = dstRect;
Brian Salomonbacbb922021-01-21 19:48:00 -05001307 xform = GrColorSpaceXform::Make(input->colorInfo(), dst->colorInfo());
Brian Salomon1c86b632020-12-11 12:36:01 -05001308 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -05001309 GrImageInfo nextInfo(input->colorInfo(), nextDims);
1310 tempB = GrSurfaceFillContext::MakeWithFallback(fContext,
1311 nextInfo,
1312 SkBackingFit::kApprox);
Brian Salomon1c86b632020-12-11 12:36:01 -05001313 if (!tempB) {
1314 return false;
1315 }
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001316 stepDst = tempB.get();
1317 stepDstRect = SkIRect::MakeSize(tempB->dimensions());
Brian Salomone9ad9982019-07-22 16:17:41 -04001318 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001319 std::unique_ptr<GrFragmentProcessor> fp;
Mike Reed1efa14d2021-01-02 21:44:59 -05001320 if (rescaleMode == RescaleMode::kRepeatedCubic) {
Brian Salomon59f31b12020-06-04 17:27:15 -04001321 auto dir = GrBicubicEffect::Direction::kXY;
1322 if (nextDims.width() == srcRect.width()) {
1323 dir = GrBicubicEffect::Direction::kY;
1324 } else if (nextDims.height() == srcRect.height()) {
1325 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -04001326 }
Brian Salomon1af72d12020-06-25 10:47:26 -04001327 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
Mike Reed3867c702020-09-01 13:28:10 -04001328 static constexpr auto kKernel = GrBicubicEffect::gCatmullRom;
Brian Salomon1c86b632020-12-11 12:36:01 -05001329 fp = GrBicubicEffect::MakeSubset(std::move(texView),
1330 input->colorInfo().alphaType(),
Brian Salomonbacbb922021-01-21 19:48:00 -05001331 SkMatrix::I(),
Brian Salomon1c86b632020-12-11 12:36:01 -05001332 kWM,
1333 kWM,
1334 SkRect::Make(srcRect),
1335 kKernel,
1336 dir,
1337 *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -04001338 } else {
Mike Reed1efa14d2021-01-02 21:44:59 -05001339 auto filter = rescaleMode == RescaleMode::kNearest ? GrSamplerState::Filter::kNearest
1340 : GrSamplerState::Filter::kLinear;
Brian Salomonbacbb922021-01-21 19:48:00 -05001341 auto srcRectF = SkRect::Make(srcRect);
1342 fp = GrTextureEffect::MakeSubset(std::move(texView),
1343 this->colorInfo().alphaType(),
1344 SkMatrix::I(),
1345 {filter, GrSamplerState::MipmapMode::kNone},
1346 srcRectF,
1347 srcRectF,
1348 *this->caps());
Brian Salomone9ad9982019-07-22 16:17:41 -04001349 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001350 if (xform) {
1351 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
1352 }
1353 stepDst->fillRectToRectWithFP(srcRect, stepDstRect, std::move(fp));
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001354 texView = stepDst->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -04001355 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001356 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomone9ad9982019-07-22 16:17:41 -04001357 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001358 return true;
Brian Salomone9ad9982019-07-22 16:17:41 -04001359}
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001360
1361GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
1362 const SkIRect& rect) {
1363 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1364 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001365 auto direct = fContext->asDirectContext();
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001366 if (!direct) {
1367 return {};
1368 }
1369 auto rtProxy = this->asRenderTargetProxy();
1370 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1371 return {};
1372 }
1373
1374 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001375 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
1376 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001377 // Fail if read color type does not have all of dstCT's color channels and those missing color
1378 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001379 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1380 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
1381 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1382 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001383 return {};
1384 }
1385
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001386 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001387 !supportedRead.fOffsetAlignmentForTransferBuffer) {
1388 return {};
1389 }
1390
1391 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
1392 size_t size = rowBytes * rect.height();
Greg Daniel2e967df2021-02-08 10:38:31 -05001393 // By using kStream_GrAccessPattern here, we are not able to cache and reuse the buffer for
1394 // multiple reads. Switching to kDynamic_GrAccessPattern would allow for this, however doing
1395 // so causes a crash in a chromium test. See skbug.com/11297
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001396 auto buffer = direct->priv().resourceProvider()->createBuffer(
Greg Daniel393debc2021-02-06 03:37:20 +00001397 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001398 if (!buffer) {
1399 return {};
1400 }
1401 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -05001402 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001403 if (flip) {
1404 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1405 this->height() - rect.fTop);
1406 }
Greg Danielbbfec9d2019-08-20 10:56:51 -04001407 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001408 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -04001409 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001410 PixelTransferResult result;
1411 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001412 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -04001413 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001414 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
1415 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001416 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
1417 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon5392c942021-03-30 16:14:37 -04001418 GrConvertPixels( GrPixmap(dstInfo, dst, dstInfo.minRowBytes()),
1419 GrCPixmap(srcInfo, src, srcInfo.minRowBytes()));
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001420 };
1421 }
1422 return result;
1423}
Greg Daniel46e366a2019-12-16 14:38:36 -05001424
1425#ifdef SK_DEBUG
1426void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001427 SkASSERT(fReadView.proxy());
1428 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -04001429 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
1430 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
1431 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
1432 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001433 this->onValidate();
1434}
1435#endif