blob: bc708d88c174f22410681bca7804605749765d74 [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 Osman3366efd2021-06-23 08:49:02 -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(),
Chris Daltonf5b87f92021-04-19 17:27:09 -060065 SkSurfaceProps());
Brian Salomon590f5672020-12-16 11:44:47 -050066 } 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 Salomone2078f12021-05-24 12:40:46 -0400346 if (!dContext->priv().getGpu()->readPixels(srcSurface,
347 SkIRect::MakePtSize(pt, dst.dimensions()),
Brian Salomondd4087d2020-12-23 20:36:44 -0500348 this->colorInfo().colorType(),
Brian Salomone2078f12021-05-24 12:40:46 -0400349 supportedRead.fColorType,
350 readDst,
351 readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400352 return false;
353 }
354
Brian Salomondd4087d2020-12-23 20:36:44 -0500355 if (tmp.hasPixels()) {
356 return GrConvertPixels(dst, tmp, flip);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400357 }
358 return true;
359}
Robert Phillips0d075de2019-03-04 11:08:13 -0500360
Brian Salomonea1d39b2021-04-01 17:06:52 -0400361bool GrSurfaceContext::writePixels(GrDirectContext* dContext,
362 GrCPixmap src,
Brian Salomon75ee7372021-04-06 15:04:35 -0400363 SkIPoint dstPt) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400364 ASSERT_SINGLE_OWNER
365 RETURN_FALSE_IF_ABANDONED
366 SkDEBUGCODE(this->validate();)
Brian Salomon2c673402021-04-02 14:36:58 -0400367
368 src = src.clip(this->dimensions(), &dstPt);
369 if (!src.hasPixels()) {
370 return false;
371 }
372 if (!src.info().bpp() || src.rowBytes() % src.info().bpp()) {
373 return false;
374 }
Brian Salomon75ee7372021-04-06 15:04:35 -0400375 return this->internalWritePixels(dContext, &src, 1, dstPt);
Brian Salomon2c673402021-04-02 14:36:58 -0400376}
377
378bool GrSurfaceContext::writePixels(GrDirectContext* dContext,
379 const GrCPixmap src[],
Brian Salomon75ee7372021-04-06 15:04:35 -0400380 int numLevels) {
Brian Salomon2c673402021-04-02 14:36:58 -0400381 ASSERT_SINGLE_OWNER
382 RETURN_FALSE_IF_ABANDONED
383 SkDEBUGCODE(this->validate();)
384
385 SkASSERT(dContext);
386 SkASSERT(numLevels >= 1);
387 SkASSERT(src);
388
389 if (numLevels == 1) {
390 if (src->dimensions() != this->dimensions()) {
391 return false;
392 }
Brian Salomon75ee7372021-04-06 15:04:35 -0400393 return this->writePixels(dContext, src[0], {0, 0});
Brian Salomon2c673402021-04-02 14:36:58 -0400394 }
395 if (!this->asTextureProxy() || this->asTextureProxy()->proxyMipmapped() == GrMipmapped::kNo) {
396 return false;
397 }
398
399 SkISize dims = this->dimensions();
400 if (numLevels != SkMipmap::ComputeLevelCount(dims) + 1) {
401 return false;
402 }
403 for (int i = 0; i < numLevels; ++i) {
404 if (src[i].colorInfo() != src[0].colorInfo()) {
405 return false;
406 }
407 if (dims != src[i].dimensions()) {
408 return false;
409 }
410 if (!src[i].info().bpp() || src[i].rowBytes() % src[i].info().bpp()) {
411 return false;
412 }
413 dims = {std::max(1, dims.width()/2), std::max(1, dims.height()/2)};
414 }
Brian Salomon75ee7372021-04-06 15:04:35 -0400415 return this->internalWritePixels(dContext, src, numLevels, {0, 0});
Brian Salomon2c673402021-04-02 14:36:58 -0400416}
417
418bool GrSurfaceContext::internalWritePixels(GrDirectContext* dContext,
419 const GrCPixmap src[],
420 int numLevels,
Brian Salomon75ee7372021-04-06 15:04:35 -0400421 SkIPoint pt) {
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 Salomon75ee7372021-04-06 15:04:35 -0400487 if ((!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) && numLevels == 1) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500488 GrColorInfo tempColorInfo;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400489 GrBackendFormat format;
Greg Danielbfa19c42019-12-19 16:41:40 -0500490 GrSwizzle tempReadSwizzle;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400491 if (canvas2DFastPath) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500492 tempColorInfo = {GrColorType::kRGBA_8888,
493 kUnpremul_SkAlphaType,
494 this->colorInfo().refColorSpace()};
Greg Daniel7bfc9132019-08-14 14:23:53 -0400495 format = rgbaDefaultFormat;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400496 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500497 tempColorInfo = this->colorInfo();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400498 format = dstProxy->backendFormat().makeTexture2D();
499 if (!format.isValid()) {
500 return false;
501 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500502 tempReadSwizzle = this->readSwizzle();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400503 }
504
Greg Daniel2e52ad12019-06-13 10:04:16 -0400505 // It is more efficient for us to write pixels into a top left origin so we prefer that.
506 // However, if the final proxy isn't a render target then we must use a copy to move the
507 // data into it which requires the origins to match. If the final proxy is a render target
508 // we can use a draw instead which doesn't have this origin restriction. Thus for render
509 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400510 GrSurfaceOrigin tempOrigin =
Brian Salomon590f5672020-12-16 11:44:47 -0500511 this->asFillContext() ? kTopLeft_GrSurfaceOrigin : this->origin();
Brian Salomon2c673402021-04-02 14:36:58 -0400512 auto tempProxy = dContext->priv().proxyProvider()->createProxy(format,
513 src[0].dimensions(),
514 GrRenderable::kNo,
515 1,
516 GrMipmapped::kNo,
517 SkBackingFit::kApprox,
518 SkBudgeted::kYes,
519 GrProtected::kNo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400520 if (!tempProxy) {
521 return false;
522 }
Greg Daniel3912a4b2020-01-14 09:56:04 -0500523 GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500524 GrSurfaceContext tempCtx(dContext, tempView, tempColorInfo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400525
526 // In the fast path we always write the srcData to the temp context as though it were RGBA.
527 // When the data is really BGRA the write will cause the R and B channels to be swapped in
528 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
529 // dst.
Brian Salomon2c673402021-04-02 14:36:58 -0400530 GrCPixmap origSrcBase = src[0];
531 GrCPixmap srcBase = origSrcBase;
Brian Salomon1d435302019-07-01 13:05:28 -0400532 if (canvas2DFastPath) {
Brian Salomon2c673402021-04-02 14:36:58 -0400533 srcBase = GrCPixmap(origSrcBase.info().makeColorType(GrColorType::kRGBA_8888),
534 origSrcBase.addr(),
535 origSrcBase.rowBytes());
Brian Salomon1d435302019-07-01 13:05:28 -0400536 }
Brian Salomon2c673402021-04-02 14:36:58 -0400537 if (!tempCtx.writePixels(dContext, srcBase, {0, 0})) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400538 return false;
539 }
540
Brian Salomon590f5672020-12-16 11:44:47 -0500541 if (this->asFillContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400542 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400543 if (canvas2DFastPath) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400544 fp = dContext->priv().createUPMToPMEffect(
Brian Salomon14f99fc2020-12-07 12:19:47 -0500545 GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType()));
Brian Salomon1d435302019-07-01 13:05:28 -0400546 // Important: check the original src color type here!
Brian Salomon2c673402021-04-02 14:36:58 -0400547 if (origSrcBase.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400548 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
549 }
550 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500551 fp = GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400552 }
553 if (!fp) {
554 return false;
555 }
Brian Salomon2c673402021-04-02 14:36:58 -0400556 this->asFillContext()->fillRectToRectWithFP(
557 SkIRect::MakeSize(srcBase.dimensions()),
558 SkIRect::MakePtSize(pt, srcBase.dimensions()),
559 std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400560 } else {
Brian Salomon2c673402021-04-02 14:36:58 -0400561 SkIRect srcRect = SkIRect::MakeSize(srcBase.dimensions());
Brian Salomon1d435302019-07-01 13:05:28 -0400562 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Brian Salomon982127b2021-01-21 10:43:35 -0500563 if (!this->copy(std::move(tempProxy), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400564 return false;
565 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400566 }
567 return true;
568 }
569
Brian Salomon2c673402021-04-02 14:36:58 -0400570 GrColorType srcColorType = src[0].colorType();
571 auto [allowedColorType, _] =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400572 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400573 dstProxy->backendFormat(),
Brian Salomon2c673402021-04-02 14:36:58 -0400574 srcColorType);
Greg Danielb8d84f82020-02-13 14:25:00 -0500575 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400576
Brian Salomon2c673402021-04-02 14:36:58 -0400577 bool convertAll = premul ||
578 unpremul ||
579 needColorConversion ||
580 flip ||
581 (srcColorType != allowedColorType);
582 bool mustBeTight = !caps->writePixelsRowBytesSupport();
583 size_t tmpSize = 0;
584 if (mustBeTight || convertAll) {
585 for (int i = 0; i < numLevels; ++i) {
586 if (convertAll || (mustBeTight && src[i].rowBytes() != src[i].info().minRowBytes())) {
587 tmpSize += src[i].info().makeColorType(allowedColorType).minRowBytes()*
588 src[i].height();
589 }
590 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400591 }
592
Brian Salomon2c673402021-04-02 14:36:58 -0400593 auto tmpData = tmpSize ? SkData::MakeUninitialized(tmpSize) : nullptr;
594 void* tmp = tmpSize ? tmpData->writable_data() : nullptr;
595 SkAutoSTArray<15, GrMipLevel> srcLevels(numLevels);
596 bool ownAllStorage = true;
597 for (int i = 0; i < numLevels; ++i) {
598 if (convertAll || (mustBeTight && src[i].rowBytes() != src[i].info().minRowBytes())) {
599 GrImageInfo tmpInfo(allowedColorType,
600 this->colorInfo().alphaType(),
601 this->colorInfo().refColorSpace(),
602 src[i].dimensions());
603 auto tmpRB = tmpInfo.minRowBytes();
604 GrPixmap tmpPM(tmpInfo, tmp, tmpRB);
605 SkAssertResult(GrConvertPixels(tmpPM, src[i], flip));
606 srcLevels[i] = {tmpPM.addr(), tmpPM.rowBytes(), tmpData};
607 tmp = SkTAddOffset<void>(tmp, tmpRB*tmpPM.height());
608 } else {
609 srcLevels[i] = {src[i].addr(), src[i].rowBytes(), src[i].pixelStorage()};
610 ownAllStorage &= src[i].ownsPixels();
611 }
612 }
613 pt.fY = flip ? dstSurface->height() - pt.fY - src[0].height() : pt.fY;
614
615 if (!dContext->priv().drawingManager()->newWritePixelsTask(
Brian Salomon974c8212021-04-05 16:24:00 -0400616 sk_ref_sp(dstProxy),
617 SkIRect::MakePtSize(pt, src[0].dimensions()),
618 allowedColorType,
619 this->colorInfo().colorType(),
620 srcLevels.begin(),
Brian Salomon75ee7372021-04-06 15:04:35 -0400621 numLevels)) {
Brian Salomon2c673402021-04-02 14:36:58 -0400622 return false;
623 }
624 if (numLevels > 1) {
625 dstProxy->asTextureProxy()->markMipmapsClean();
626 }
627 if (!ownAllStorage) {
628 // If any pixmap doesn't own its pixels then we must flush so that the pixels are pushed to
629 // the GPU before we return.
Brian Salomon24949422021-02-11 09:39:46 -0500630 dContext->priv().flushSurface(dstProxy);
631 }
Brian Salomon2c673402021-04-02 14:36:58 -0400632 return true;
Brian Osman45580d32016-11-23 09:37:01 -0500633}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400634
Adlai Hollerc95b5892020-08-11 12:02:22 -0400635void GrSurfaceContext::asyncRescaleAndReadPixels(GrDirectContext* dContext,
636 const SkImageInfo& info,
Brian Salomon63a0a752020-06-26 13:32:09 -0400637 const SkIRect& srcRect,
638 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500639 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400640 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400641 ReadPixelsContext callbackContext) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400642 if (!dContext) {
643 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400644 return;
645 }
646 auto rt = this->asRenderTargetProxy();
647 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400648 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400649 return;
650 }
651 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400652 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400653 return;
654 }
655 auto dstCT = SkColorTypeToGrColorType(info.colorType());
656 if (dstCT == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400657 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400658 return;
659 }
Brian Salomon827bb722021-05-06 16:24:21 -0400660 bool needsRescale = srcRect.size() != info.dimensions() ||
661 this->origin() == kBottomLeft_GrSurfaceOrigin ||
662 this->colorInfo().alphaType() != info.alphaType() ||
663 !SkColorSpace::Equals(this->colorInfo().colorSpace(), info.colorSpace());
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;
Brian Salomon63a0a752020-06-26 13:32:09 -0400701 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500702 auto srcCtx = tempFC ? tempFC.get() : this;
703 return srcCtx->asyncReadPixels(dContext,
704 SkIRect::MakePtSize({x, y}, info.dimensions()),
705 info.colorType(),
706 callback,
707 callbackContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400708}
709
710class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult {
711public:
Robert Phillips82ad7af2021-03-11 16:00:10 -0500712 AsyncReadResult(GrDirectContext::DirectContextID intendedRecipient)
713 : fIntendedRecipient(intendedRecipient) {
714 }
715
Brian Salomon63a0a752020-06-26 13:32:09 -0400716 ~AsyncReadResult() override {
717 for (int i = 0; i < fPlanes.count(); ++i) {
Robert Phillips82ad7af2021-03-11 16:00:10 -0500718 fPlanes[i].releaseMappedBuffer(fIntendedRecipient);
Brian Salomon63a0a752020-06-26 13:32:09 -0400719 }
720 }
721
722 int count() const override { return fPlanes.count(); }
Brian Salomonbe1084b2021-01-26 13:29:30 -0500723 const void* data(int i) const override { return fPlanes[i].data(); }
724 size_t rowBytes(int i) const override { return fPlanes[i].rowBytes(); }
Brian Salomon63a0a752020-06-26 13:32:09 -0400725
726 bool addTransferResult(const PixelTransferResult& result,
727 SkISize dimensions,
728 size_t rowBytes,
729 GrClientMappedBufferManager* manager) {
730 SkASSERT(!result.fTransferBuffer->isMapped());
731 const void* mappedData = result.fTransferBuffer->map();
732 if (!mappedData) {
733 return false;
734 }
735 if (result.fPixelConverter) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500736 size_t size = rowBytes*dimensions.height();
737 sk_sp<SkData> data = SkData::MakeUninitialized(size);
738 result.fPixelConverter(data->writable_data(), mappedData);
739 this->addCpuPlane(std::move(data), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400740 result.fTransferBuffer->unmap();
741 } else {
742 manager->insert(result.fTransferBuffer);
743 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
744 }
745 return true;
746 }
747
Brian Salomonbe1084b2021-01-26 13:29:30 -0500748 void addCpuPlane(sk_sp<SkData> data, size_t rowBytes) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400749 SkASSERT(data);
750 SkASSERT(rowBytes > 0);
Brian Salomonbe1084b2021-01-26 13:29:30 -0500751 fPlanes.emplace_back(std::move(data), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400752 }
753
754private:
755 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
756 SkASSERT(data);
757 SkASSERT(rowBytes > 0);
758 SkASSERT(mappedBuffer);
759 SkASSERT(mappedBuffer->isMapped());
Brian Salomonbe1084b2021-01-26 13:29:30 -0500760 fPlanes.emplace_back(std::move(mappedBuffer), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400761 }
762
Brian Salomonbe1084b2021-01-26 13:29:30 -0500763 class Plane {
764 public:
765 Plane(sk_sp<GrGpuBuffer> buffer, size_t rowBytes)
766 : fMappedBuffer(std::move(buffer)), fRowBytes(rowBytes) {}
767 Plane(sk_sp<SkData> data, size_t rowBytes) : fData(std::move(data)), fRowBytes(rowBytes) {}
768
769 Plane(const Plane&) = delete;
770 Plane(Plane&&) = default;
771
772 ~Plane() { SkASSERT(!fMappedBuffer); }
773
774 Plane& operator=(const Plane&) = delete;
775 Plane& operator=(Plane&&) = default;
776
Robert Phillips82ad7af2021-03-11 16:00:10 -0500777 void releaseMappedBuffer(GrDirectContext::DirectContextID intendedRecipient) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500778 if (fMappedBuffer) {
779 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
Robert Phillips82ad7af2021-03-11 16:00:10 -0500780 {std::move(fMappedBuffer), intendedRecipient});
Brian Salomonbe1084b2021-01-26 13:29:30 -0500781 }
782 }
783
784 const void* data() const {
785 if (fMappedBuffer) {
786 SkASSERT(!fData);
787 SkASSERT(fMappedBuffer->isMapped());
788 return fMappedBuffer->map();
789 }
790 SkASSERT(fData);
791 return fData->data();
792 }
793
794 size_t rowBytes() const { return fRowBytes; }
795
796 private:
797 sk_sp<SkData> fData;
Brian Salomon1eea1ea2021-01-26 18:12:25 +0000798 sk_sp<GrGpuBuffer> fMappedBuffer;
Brian Salomonbe1084b2021-01-26 13:29:30 -0500799 size_t fRowBytes;
Brian Salomon63a0a752020-06-26 13:32:09 -0400800 };
801 SkSTArray<3, Plane> fPlanes;
Robert Phillips82ad7af2021-03-11 16:00:10 -0500802 GrDirectContext::DirectContextID fIntendedRecipient;
Brian Salomon63a0a752020-06-26 13:32:09 -0400803};
804
Adlai Hollerc95b5892020-08-11 12:02:22 -0400805void GrSurfaceContext::asyncReadPixels(GrDirectContext* dContext,
806 const SkIRect& rect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400807 SkColorType colorType,
808 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400809 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400810 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
811 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
812
Adlai Hollerc95b5892020-08-11 12:02:22 -0400813 if (!dContext || this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
814 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400815 return;
816 }
817
Adlai Hollerc95b5892020-08-11 12:02:22 -0400818 auto mappedBufferManager = dContext->priv().clientMappedBufferManager();
Brian Salomon63a0a752020-06-26 13:32:09 -0400819
820 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
821
822 if (!transferResult.fTransferBuffer) {
823 auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
824 this->colorInfo().refColorSpace());
Robert Phillips82ad7af2021-03-11 16:00:10 -0500825 static const GrDirectContext::DirectContextID kInvalid;
826 auto result = std::make_unique<AsyncReadResult>(kInvalid);
Brian Salomonbe1084b2021-01-26 13:29:30 -0500827 GrPixmap pm = GrPixmap::Allocate(ii);
828 result->addCpuPlane(pm.pixelStorage(), pm.rowBytes());
Brian Salomon63a0a752020-06-26 13:32:09 -0400829
Adlai Hollerc95b5892020-08-11 12:02:22 -0400830 SkIPoint pt{rect.fLeft, rect.fTop};
Brian Salomondd4087d2020-12-23 20:36:44 -0500831 if (!this->readPixels(dContext, pm, pt)) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400832 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400833 return;
834 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400835 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400836 return;
837 }
838
839 struct FinishContext {
840 ReadPixelsCallback* fClientCallback;
841 ReadPixelsContext fClientContext;
842 SkISize fSize;
843 SkColorType fColorType;
Jim Van Vertha655f0d2021-05-18 15:03:27 -0400844 size_t fBufferAlignment;
Brian Salomon63a0a752020-06-26 13:32:09 -0400845 GrClientMappedBufferManager* fMappedBufferManager;
846 PixelTransferResult fTransferResult;
847 };
848 // Assumption is that the caller would like to flush. We could take a parameter or require an
849 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
850 // callback to GrGpu until after the next flush that flushes our op list, though.
851 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400852 callbackContext,
Brian Salomon63a0a752020-06-26 13:32:09 -0400853 rect.size(),
854 colorType,
Jim Van Vertha655f0d2021-05-18 15:03:27 -0400855 this->caps()->transferBufferAlignment(),
Brian Salomon63a0a752020-06-26 13:32:09 -0400856 mappedBufferManager,
857 std::move(transferResult)};
858 auto finishCallback = [](GrGpuFinishedContext c) {
859 const auto* context = reinterpret_cast<const FinishContext*>(c);
Robert Phillips82ad7af2021-03-11 16:00:10 -0500860 auto manager = context->fMappedBufferManager;
861 auto result = std::make_unique<AsyncReadResult>(manager->owningDirectContext());
Jim Van Vertha655f0d2021-05-18 15:03:27 -0400862 size_t rowBytes =
863 GrAlignTo(context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType),
864 context->fBufferAlignment);
Brian Salomon63a0a752020-06-26 13:32:09 -0400865 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
Robert Phillips82ad7af2021-03-11 16:00:10 -0500866 manager)) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400867 result.reset();
868 }
869 (*context->fClientCallback)(context->fClientContext, std::move(result));
870 delete context;
871 };
872 GrFlushInfo flushInfo;
873 flushInfo.fFinishedContext = finishContext;
874 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -0500875
876 dContext->priv().flushSurface(this->asSurfaceProxy(),
877 SkSurface::BackendSurfaceAccess::kNoAccess,
878 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400879}
880
Adlai Hollerc95b5892020-08-11 12:02:22 -0400881void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(GrDirectContext* dContext,
882 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400883 sk_sp<SkColorSpace> dstColorSpace,
884 const SkIRect& srcRect,
885 SkISize dstSize,
886 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500887 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400888 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400889 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400890 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
891 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
892 SkASSERT(!dstSize.isZero());
893 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
894
Adlai Hollerc95b5892020-08-11 12:02:22 -0400895 if (!dContext) {
896 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400897 return;
898 }
899 auto rt = this->asRenderTargetProxy();
900 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400901 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400902 return;
903 }
904 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400905 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400906 return;
907 }
908 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400909 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400910 return;
911 }
912 int x = srcRect.fLeft;
913 int y = srcRect.fTop;
Brian Salomon827bb722021-05-06 16:24:21 -0400914 bool needsRescale = srcRect.size() != dstSize ||
915 !SkColorSpace::Equals(this->colorInfo().colorSpace(), dstColorSpace.get());
916 GrSurfaceProxyView srcView = this->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400917 if (needsRescale) {
Brian Salomon827bb722021-05-06 16:24:21 -0400918 auto info = SkImageInfo::Make(dstSize,
919 kRGBA_8888_SkColorType,
920 this->colorInfo().alphaType(),
921 dstColorSpace);
Brian Salomon63a0a752020-06-26 13:32:09 -0400922 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomonbacbb922021-01-21 19:48:00 -0500923 auto tempFC = this->rescale(info,
924 kTopLeft_GrSurfaceOrigin,
925 srcRect,
926 rescaleGamma,
927 rescaleMode);
928 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400929 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400930 return;
931 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500932 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
933 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400934 x = y = 0;
Brian Salomonbacbb922021-01-21 19:48:00 -0500935 srcView = tempFC->readSurfaceView();
Brian Salomon827bb722021-05-06 16:24:21 -0400936 } else if (!srcView.asTextureProxy()) {
937 srcView = GrSurfaceProxyView::Copy(fContext,
938 std::move(srcView),
939 GrMipmapped::kNo,
940 srcRect,
941 SkBackingFit::kApprox,
942 SkBudgeted::kYes);
943 if (!srcView) {
944 // If we can't get a texture copy of the contents then give up.
945 callback(callbackContext, nullptr);
946 return;
Brian Salomon63a0a752020-06-26 13:32:09 -0400947 }
Brian Salomon827bb722021-05-06 16:24:21 -0400948 SkASSERT(srcView.asTextureProxy());
949 x = y = 0;
Brian Salomon63a0a752020-06-26 13:32:09 -0400950 }
951
Brian Salomonbacbb922021-01-21 19:48:00 -0500952 auto yInfo = SkImageInfo::MakeA8(dstSize);
953 auto yFC = GrSurfaceFillContext::MakeWithFallback(dContext, yInfo, SkBackingFit::kApprox);
954
955 auto uvInfo = yInfo.makeWH(yInfo.width()/2, yInfo.height()/2);
956 auto uFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
957 auto vFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
958
959 if (!yFC || !uFC || !vFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400960 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400961 return;
962 }
963
964 float baseM[20];
965 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
966
967 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
968
969 auto texMatrix = SkMatrix::Translate(x, y);
970
Brian Salomon63a0a752020-06-26 13:32:09 -0400971 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
972 PixelTransferResult yTransfer, uTransfer, vTransfer;
973
974 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
975 float yM[20];
976 std::fill_n(yM, 15, 0.f);
977 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500978
979 auto yFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
Brian Osman3366efd2021-06-23 08:49:02 -0400980 yFP = GrColorMatrixFragmentProcessor::Make(std::move(yFP),
981 yM,
982 /*unpremulInput=*/false,
983 /*clampRGBOutput=*/true,
984 /*premulOutput=*/false);
Brian Salomonbacbb922021-01-21 19:48:00 -0500985 yFC->fillWithFP(std::move(yFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400986 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500987 yTransfer = yFC->transferPixels(GrColorType::kAlpha_8,
988 SkIRect::MakeSize(yFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400989 if (!yTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400990 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400991 return;
992 }
993 }
994
995 texMatrix.preScale(2.f, 2.f);
996 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
997 float uM[20];
998 std::fill_n(uM, 15, 0.f);
999 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -05001000
1001 auto uFP = GrTextureEffect::Make(srcView,
1002 this->colorInfo().alphaType(),
1003 texMatrix,
1004 GrSamplerState::Filter::kLinear);
Brian Osman3366efd2021-06-23 08:49:02 -04001005 uFP = GrColorMatrixFragmentProcessor::Make(std::move(uFP),
1006 uM,
1007 /*unpremulInput=*/false,
1008 /*clampRGBOutput=*/true,
1009 /*premulOutput=*/false);
Brian Salomonbacbb922021-01-21 19:48:00 -05001010 uFC->fillWithFP(std::move(uFP));
Brian Salomon63a0a752020-06-26 13:32:09 -04001011 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001012 uTransfer = uFC->transferPixels(GrColorType::kAlpha_8,
1013 SkIRect::MakeSize(uFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -04001014 if (!uTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001015 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001016 return;
1017 }
1018 }
1019
1020 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
1021 float vM[20];
1022 std::fill_n(vM, 15, 0.f);
1023 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -05001024 auto vFP = GrTextureEffect::Make(std::move(srcView),
1025 this->colorInfo().alphaType(),
1026 texMatrix,
1027 GrSamplerState::Filter::kLinear);
Brian Osman3366efd2021-06-23 08:49:02 -04001028 vFP = GrColorMatrixFragmentProcessor::Make(std::move(vFP),
1029 vM,
1030 /*unpremulInput=*/false,
1031 /*clampRGBOutput=*/true,
1032 /*premulOutput=*/false);
Brian Salomonbacbb922021-01-21 19:48:00 -05001033 vFC->fillWithFP(std::move(vFP));
1034
Brian Salomon63a0a752020-06-26 13:32:09 -04001035 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001036 vTransfer = vFC->transferPixels(GrColorType::kAlpha_8,
1037 SkIRect::MakeSize(vFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -04001038 if (!vTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001039 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001040 return;
1041 }
1042 }
1043
1044 if (doSynchronousRead) {
Brian Salomonbe1084b2021-01-26 13:29:30 -05001045 GrPixmap yPmp = GrPixmap::Allocate(yInfo);
1046 GrPixmap uPmp = GrPixmap::Allocate(uvInfo);
1047 GrPixmap vPmp = GrPixmap::Allocate(uvInfo);
Brian Salomonbacbb922021-01-21 19:48:00 -05001048 if (!yFC->readPixels(dContext, yPmp, {0, 0}) ||
1049 !uFC->readPixels(dContext, uPmp, {0, 0}) ||
1050 !vFC->readPixels(dContext, vPmp, {0, 0})) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001051 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001052 return;
1053 }
Robert Phillips82ad7af2021-03-11 16:00:10 -05001054 auto result = std::make_unique<AsyncReadResult>(dContext->directContextID());
Brian Salomonbe1084b2021-01-26 13:29:30 -05001055 result->addCpuPlane(yPmp.pixelStorage(), yPmp.rowBytes());
1056 result->addCpuPlane(uPmp.pixelStorage(), uPmp.rowBytes());
1057 result->addCpuPlane(vPmp.pixelStorage(), vPmp.rowBytes());
Adlai Hollerc95b5892020-08-11 12:02:22 -04001058 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -04001059 return;
1060 }
1061
1062 struct FinishContext {
1063 ReadPixelsCallback* fClientCallback;
1064 ReadPixelsContext fClientContext;
1065 GrClientMappedBufferManager* fMappedBufferManager;
1066 SkISize fSize;
Jim Van Vertha655f0d2021-05-18 15:03:27 -04001067 size_t fBufferAlignment;
Brian Salomon63a0a752020-06-26 13:32:09 -04001068 PixelTransferResult fYTransfer;
1069 PixelTransferResult fUTransfer;
1070 PixelTransferResult fVTransfer;
1071 };
1072 // Assumption is that the caller would like to flush. We could take a parameter or require an
1073 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1074 // callback to GrGpu until after the next flush that flushes our op list, though.
1075 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -04001076 callbackContext,
1077 dContext->priv().clientMappedBufferManager(),
Brian Salomon63a0a752020-06-26 13:32:09 -04001078 dstSize,
Jim Van Vertha655f0d2021-05-18 15:03:27 -04001079 this->caps()->transferBufferAlignment(),
Brian Salomon63a0a752020-06-26 13:32:09 -04001080 std::move(yTransfer),
1081 std::move(uTransfer),
1082 std::move(vTransfer)};
1083 auto finishCallback = [](GrGpuFinishedContext c) {
1084 const auto* context = reinterpret_cast<const FinishContext*>(c);
Brian Salomon63a0a752020-06-26 13:32:09 -04001085 auto manager = context->fMappedBufferManager;
Robert Phillips82ad7af2021-03-11 16:00:10 -05001086 auto result = std::make_unique<AsyncReadResult>(manager->owningDirectContext());
Brian Salomon63a0a752020-06-26 13:32:09 -04001087 size_t rowBytes = SkToSizeT(context->fSize.width());
Jim Van Vertha655f0d2021-05-18 15:03:27 -04001088 rowBytes = GrAlignTo(rowBytes, context->fBufferAlignment);
Brian Salomon63a0a752020-06-26 13:32:09 -04001089 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
1090 (*context->fClientCallback)(context->fClientContext, nullptr);
1091 delete context;
1092 return;
1093 }
Jim Van Vertha655f0d2021-05-18 15:03:27 -04001094 rowBytes = SkToSizeT(context->fSize.width()) / 2;
1095 rowBytes = GrAlignTo(rowBytes, context->fBufferAlignment);
Brian Salomon63a0a752020-06-26 13:32:09 -04001096 SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
1097 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
1098 (*context->fClientCallback)(context->fClientContext, nullptr);
1099 delete context;
1100 return;
1101 }
1102 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
1103 (*context->fClientCallback)(context->fClientContext, nullptr);
1104 delete context;
1105 return;
1106 }
1107 (*context->fClientCallback)(context->fClientContext, std::move(result));
1108 delete context;
1109 };
1110 GrFlushInfo flushInfo;
1111 flushInfo.fFinishedContext = finishContext;
1112 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -05001113 dContext->priv().flushSurface(this->asSurfaceProxy(),
1114 SkSurface::BackendSurfaceAccess::kNoAccess,
1115 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -04001116}
1117
Brian Salomond63638b2021-03-05 14:00:07 -05001118sk_sp<GrRenderTask> GrSurfaceContext::copy(sk_sp<GrSurfaceProxy> src,
1119 SkIRect srcRect,
1120 SkIPoint dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001121 ASSERT_SINGLE_OWNER
Brian Salomond63638b2021-03-05 14:00:07 -05001122 RETURN_NULLPTR_IF_ABANDONED
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001123 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -04001124 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -04001125
Brian Salomon947efe22019-07-16 15:36:11 -04001126 const GrCaps* caps = fContext->priv().caps();
1127
Greg Daniel46cfbc62019-06-07 11:43:30 -04001128 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -05001129 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -04001130
Stephen White3c0a50f2020-01-16 18:19:54 -05001131 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomond63638b2021-03-05 14:00:07 -05001132 return nullptr;
Stephen White3c0a50f2020-01-16 18:19:54 -05001133 }
1134
Brian Salomon982127b2021-01-21 10:43:35 -05001135 if (!caps->canCopySurface(this->asSurfaceProxy(), src.get(), srcRect, dstPoint)) {
Brian Salomond63638b2021-03-05 14:00:07 -05001136 return nullptr;
Greg Daniel25af6712018-04-25 10:44:38 -04001137 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001138
Brian Salomon982127b2021-01-21 10:43:35 -05001139 return this->drawingManager()->newCopyRenderTask(std::move(src),
1140 srcRect,
1141 this->asSurfaceProxyRef(),
Brian Salomon0f9f8002021-01-22 16:30:50 -05001142 dstPoint,
1143 this->origin());
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001144}
Greg Daniel46cfbc62019-06-07 11:43:30 -04001145
Brian Salomonbacbb922021-01-21 19:48:00 -05001146std::unique_ptr<GrSurfaceFillContext> GrSurfaceContext::rescale(const GrImageInfo& info,
Brian Salomoneebe7352020-12-09 16:37:04 -05001147 GrSurfaceOrigin origin,
1148 SkIRect srcRect,
1149 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001150 RescaleMode rescaleMode) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001151 auto sfc = GrSurfaceFillContext::MakeWithFallback(fContext,
1152 info,
Brian Salomon1c86b632020-12-11 12:36:01 -05001153 SkBackingFit::kExact,
Brian Salomon1c86b632020-12-11 12:36:01 -05001154 1,
1155 GrMipmapped::kNo,
1156 this->asSurfaceProxy()->isProtected(),
1157 origin);
Brian Salomonbacbb922021-01-21 19:48:00 -05001158 if (!sfc || !this->rescaleInto(sfc.get(),
1159 SkIRect::MakeSize(sfc->dimensions()),
Brian Salomon1c86b632020-12-11 12:36:01 -05001160 srcRect,
1161 rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001162 rescaleMode)) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001163 return nullptr;
1164 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001165 return sfc;
Brian Salomon1c86b632020-12-11 12:36:01 -05001166}
1167
Brian Salomonbacbb922021-01-21 19:48:00 -05001168bool GrSurfaceContext::rescaleInto(GrSurfaceFillContext* dst,
Brian Salomon1c86b632020-12-11 12:36:01 -05001169 SkIRect dstRect,
1170 SkIRect srcRect,
1171 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001172 RescaleMode rescaleMode) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001173 SkASSERT(dst);
1174 if (!SkIRect::MakeSize(dst->dimensions()).contains((dstRect))) {
1175 return false;
1176 }
1177
Brian Salomone9ad9982019-07-22 16:17:41 -04001178 auto rtProxy = this->asRenderTargetProxy();
1179 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001180 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001181 }
1182
Stephen White3c0a50f2020-01-16 18:19:54 -05001183 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001184 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001185 }
1186
Greg Daniel40903af2020-01-30 14:55:05 -05001187 GrSurfaceProxyView texView = this->readSurfaceView();
Greg Daniel40903af2020-01-30 14:55:05 -05001188 if (!texView.asTextureProxy()) {
Brian Salomon827bb722021-05-06 16:24:21 -04001189 // TODO: If copying supported specifying a renderable copy then we could return the copy
1190 // when there are no other conversions.
Brian Salomon7e67dca2020-07-21 09:27:25 -04001191 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect,
Brian Salomonc5243782020-04-02 12:50:34 -04001192 SkBackingFit::kApprox, SkBudgeted::kNo);
1193 if (!texView) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001194 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001195 }
Greg Daniel40903af2020-01-30 14:55:05 -05001196 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001197 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001198 }
1199
Brian Salomon1c86b632020-12-11 12:36:01 -05001200 SkISize finalSize = dstRect.size();
Brian Salomon827bb722021-05-06 16:24:21 -04001201 if (finalSize == srcRect.size()) {
1202 rescaleGamma = RescaleGamma::kSrc;
1203 rescaleMode = RescaleMode::kNearest;
1204 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001205
Brian Salomonbf6b9792019-08-21 09:38:10 -04001206 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
1207 // 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 -05001208 std::unique_ptr<GrSurfaceFillContext> tempA;
1209 std::unique_ptr<GrSurfaceFillContext> tempB;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001210
Brian Salomone9ad9982019-07-22 16:17:41 -04001211 // Assume we should ignore the rescale linear request if the surface has no color space since
1212 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon63a0a752020-06-26 13:32:09 -04001213 if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() &&
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001214 !this->colorInfo().colorSpace()->gammaIsLinear()) {
1215 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomone9ad9982019-07-22 16:17:41 -04001216 // We'll fall back to kRGBA_8888 if half float not supported.
Brian Salomonbacbb922021-01-21 19:48:00 -05001217 GrImageInfo ii(GrColorType::kRGBA_F16,
1218 dst->colorInfo().alphaType(),
1219 std::move(cs),
1220 srcRect.size());
1221 auto linearRTC = GrSurfaceFillContext::MakeWithFallback(fContext,
1222 std::move(ii),
1223 SkBackingFit::kApprox,
1224 1,
1225 GrMipmapped::kNo,
1226 GrProtected::kNo,
1227 dst->origin());
Brian Salomone9ad9982019-07-22 16:17:41 -04001228 if (!linearRTC) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001229 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001230 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001231 auto fp = GrTextureEffect::Make(std::move(texView),
1232 this->colorInfo().alphaType(),
1233 SkMatrix::Translate(srcRect.topLeft()),
1234 GrSamplerState::Filter::kNearest,
1235 GrSamplerState::MipmapMode::kNone);
1236 fp = GrColorSpaceXformEffect::Make(std::move(fp),
1237 this->colorInfo(),
1238 linearRTC->colorInfo());
1239 linearRTC->fillWithFP(std::move(fp));
Greg Daniel40903af2020-01-30 14:55:05 -05001240 texView = linearRTC->readSurfaceView();
1241 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -04001242 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001243 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001244 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001245
Brian Salomon827bb722021-05-06 16:24:21 -04001246 do {
Brian Salomon1c86b632020-12-11 12:36:01 -05001247 SkISize nextDims = finalSize;
Mike Reed1efa14d2021-01-02 21:44:59 -05001248 if (rescaleMode != RescaleMode::kNearest) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001249 if (srcRect.width() > finalSize.width()) {
1250 nextDims.fWidth = std::max((srcRect.width() + 1)/2, finalSize.width());
1251 } else if (srcRect.width() < finalSize.width()) {
1252 nextDims.fWidth = std::min(srcRect.width()*2, finalSize.width());
Brian Salomon59f31b12020-06-04 17:27:15 -04001253 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001254 if (srcRect.height() > finalSize.height()) {
1255 nextDims.fHeight = std::max((srcRect.height() + 1)/2, finalSize.height());
1256 } else if (srcRect.height() < finalSize.height()) {
1257 nextDims.fHeight = std::min(srcRect.height()*2, finalSize.height());
Brian Salomon59f31b12020-06-04 17:27:15 -04001258 }
1259 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001260 auto input = tempA ? tempA.get() : this;
Brian Salomone9ad9982019-07-22 16:17:41 -04001261 sk_sp<GrColorSpaceXform> xform;
Brian Salomonbacbb922021-01-21 19:48:00 -05001262 GrSurfaceFillContext* stepDst;
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001263 SkIRect stepDstRect;
Brian Salomon1c86b632020-12-11 12:36:01 -05001264 if (nextDims == finalSize) {
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001265 stepDst = dst;
1266 stepDstRect = dstRect;
Brian Salomonbacbb922021-01-21 19:48:00 -05001267 xform = GrColorSpaceXform::Make(input->colorInfo(), dst->colorInfo());
Brian Salomon1c86b632020-12-11 12:36:01 -05001268 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -05001269 GrImageInfo nextInfo(input->colorInfo(), nextDims);
1270 tempB = GrSurfaceFillContext::MakeWithFallback(fContext,
1271 nextInfo,
1272 SkBackingFit::kApprox);
Brian Salomon1c86b632020-12-11 12:36:01 -05001273 if (!tempB) {
1274 return false;
1275 }
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001276 stepDst = tempB.get();
1277 stepDstRect = SkIRect::MakeSize(tempB->dimensions());
Brian Salomone9ad9982019-07-22 16:17:41 -04001278 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001279 std::unique_ptr<GrFragmentProcessor> fp;
Mike Reed1efa14d2021-01-02 21:44:59 -05001280 if (rescaleMode == RescaleMode::kRepeatedCubic) {
Brian Salomon59f31b12020-06-04 17:27:15 -04001281 auto dir = GrBicubicEffect::Direction::kXY;
1282 if (nextDims.width() == srcRect.width()) {
1283 dir = GrBicubicEffect::Direction::kY;
1284 } else if (nextDims.height() == srcRect.height()) {
1285 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -04001286 }
Brian Salomon1af72d12020-06-25 10:47:26 -04001287 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
Mike Reed3867c702020-09-01 13:28:10 -04001288 static constexpr auto kKernel = GrBicubicEffect::gCatmullRom;
Brian Salomon1c86b632020-12-11 12:36:01 -05001289 fp = GrBicubicEffect::MakeSubset(std::move(texView),
1290 input->colorInfo().alphaType(),
Brian Salomonbacbb922021-01-21 19:48:00 -05001291 SkMatrix::I(),
Brian Salomon1c86b632020-12-11 12:36:01 -05001292 kWM,
1293 kWM,
1294 SkRect::Make(srcRect),
1295 kKernel,
1296 dir,
1297 *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -04001298 } else {
Mike Reed1efa14d2021-01-02 21:44:59 -05001299 auto filter = rescaleMode == RescaleMode::kNearest ? GrSamplerState::Filter::kNearest
1300 : GrSamplerState::Filter::kLinear;
Brian Salomonbacbb922021-01-21 19:48:00 -05001301 auto srcRectF = SkRect::Make(srcRect);
1302 fp = GrTextureEffect::MakeSubset(std::move(texView),
1303 this->colorInfo().alphaType(),
1304 SkMatrix::I(),
1305 {filter, GrSamplerState::MipmapMode::kNone},
1306 srcRectF,
1307 srcRectF,
1308 *this->caps());
Brian Salomone9ad9982019-07-22 16:17:41 -04001309 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001310 if (xform) {
1311 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
1312 }
1313 stepDst->fillRectToRectWithFP(srcRect, stepDstRect, std::move(fp));
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001314 texView = stepDst->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -04001315 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001316 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomon827bb722021-05-06 16:24:21 -04001317 } while (srcRect.size() != finalSize);
Brian Salomon1c86b632020-12-11 12:36:01 -05001318 return true;
Brian Salomone9ad9982019-07-22 16:17:41 -04001319}
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001320
1321GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
1322 const SkIRect& rect) {
1323 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1324 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001325 auto direct = fContext->asDirectContext();
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001326 if (!direct) {
1327 return {};
1328 }
1329 auto rtProxy = this->asRenderTargetProxy();
1330 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1331 return {};
1332 }
1333
1334 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001335 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
1336 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001337 // Fail if read color type does not have all of dstCT's color channels and those missing color
1338 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001339 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1340 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
1341 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1342 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001343 return {};
1344 }
1345
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001346 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001347 !supportedRead.fOffsetAlignmentForTransferBuffer) {
1348 return {};
1349 }
1350
1351 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
Jim Van Vertha655f0d2021-05-18 15:03:27 -04001352 rowBytes = GrAlignTo(rowBytes, this->caps()->transferBufferAlignment());
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001353 size_t size = rowBytes * rect.height();
Greg Daniel2e967df2021-02-08 10:38:31 -05001354 // By using kStream_GrAccessPattern here, we are not able to cache and reuse the buffer for
1355 // multiple reads. Switching to kDynamic_GrAccessPattern would allow for this, however doing
1356 // so causes a crash in a chromium test. See skbug.com/11297
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001357 auto buffer = direct->priv().resourceProvider()->createBuffer(
Greg Daniel393debc2021-02-06 03:37:20 +00001358 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001359 if (!buffer) {
1360 return {};
1361 }
1362 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -05001363 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001364 if (flip) {
1365 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1366 this->height() - rect.fTop);
1367 }
Greg Danielbbfec9d2019-08-20 10:56:51 -04001368 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001369 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -04001370 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001371 PixelTransferResult result;
1372 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001373 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -04001374 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001375 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
1376 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001377 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
1378 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon5392c942021-03-30 16:14:37 -04001379 GrConvertPixels( GrPixmap(dstInfo, dst, dstInfo.minRowBytes()),
1380 GrCPixmap(srcInfo, src, srcInfo.minRowBytes()));
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001381 };
1382 }
1383 return result;
1384}
Greg Daniel46e366a2019-12-16 14:38:36 -05001385
1386#ifdef SK_DEBUG
1387void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001388 SkASSERT(fReadView.proxy());
1389 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -04001390 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
1391 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
1392 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
1393 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001394 this->onValidate();
1395}
1396#endif