blob: 8c28765c34fba11290b4a4c866ef8d6526dd3c4a [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 Salomon63a0a752020-06-26 13:32:09 -040015#include "src/core/SkYUVMath.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040016#include "src/gpu/GrAuditTrail.h"
Brian Salomon1c86b632020-12-11 12:36:01 -050017#include "src/gpu/GrColorSpaceXform.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040018#include "src/gpu/GrDataUtils.h"
Adlai Hollera0693042020-10-14 11:23:11 -040019#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrDrawingManager.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040021#include "src/gpu/GrGpu.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040022#include "src/gpu/GrImageInfo.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040023#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050025#include "src/gpu/GrSurfaceDrawContext.h"
Brian Salomonbacbb922021-01-21 19:48:00 -050026#include "src/gpu/GrSurfaceFillContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/SkGr.h"
Brian Salomone9ad9982019-07-22 16:17:41 -040028#include "src/gpu/effects/GrBicubicEffect.h"
Brian Salomon63a0a752020-06-26 13:32:09 -040029#include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h"
Brian Osman45580d32016-11-23 09:37:01 -050030
Adlai Holler33dbd652020-06-01 12:35:42 -040031#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
Robert Phillips9eb00022020-06-30 15:30:12 -040032#define RETURN_FALSE_IF_ABANDONED if (this->fContext->abandoned()) { return false; }
Brian Osman45580d32016-11-23 09:37:01 -050033
Greg Danielbfa19c42019-12-19 16:41:40 -050034std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -050035 GrSurfaceProxyView readView,
Brian Salomon14f99fc2020-12-07 12:19:47 -050036 const GrColorInfo& info) {
Greg Daniele20fcad2020-01-08 11:52:34 -050037 // It is probably not necessary to check if the context is abandoned here since uses of the
38 // GrSurfaceContext which need the context will mostly likely fail later on without an issue.
39 // However having this hear adds some reassurance in case there is a path doesn't handle an
40 // abandoned context correctly. It also lets us early out of some extra work.
Robert Phillips9eb00022020-06-30 15:30:12 -040041 if (context->abandoned()) {
Greg Daniele20fcad2020-01-08 11:52:34 -050042 return nullptr;
43 }
Greg Daniel3912a4b2020-01-14 09:56:04 -050044 GrSurfaceProxy* proxy = readView.proxy();
Greg Danielbfa19c42019-12-19 16:41:40 -050045 SkASSERT(proxy && proxy->asTextureProxy());
46
Greg Danielbfa19c42019-12-19 16:41:40 -050047 std::unique_ptr<GrSurfaceContext> surfaceContext;
Greg Daniel3912a4b2020-01-14 09:56:04 -050048 if (proxy->asRenderTargetProxy()) {
Brian Salomon8afde5f2020-04-01 16:22:00 -040049 // Will we ever want a swizzle that is not the default write swizzle for the format and
Greg Danielbfa19c42019-12-19 16:41:40 -050050 // colorType here? If so we will need to manually pass that in.
Brian Salomonc5243782020-04-02 12:50:34 -040051 GrSwizzle writeSwizzle;
Brian Salomon14f99fc2020-12-07 12:19:47 -050052 if (info.colorType() != GrColorType::kUnknown) {
53 writeSwizzle = context->priv().caps()->getWriteSwizzle(proxy->backendFormat(),
54 info.colorType());
Brian Salomonc5243782020-04-02 12:50:34 -040055 }
Brian Salomon8afde5f2020-04-01 16:22:00 -040056 GrSurfaceProxyView writeView(readView.refProxy(), readView.origin(), writeSwizzle);
Brian Salomon590f5672020-12-16 11:44:47 -050057 if (info.alphaType() == kPremul_SkAlphaType || info.alphaType() == kOpaque_SkAlphaType) {
58 surfaceContext = std::make_unique<GrSurfaceDrawContext>(context,
59 std::move(readView),
60 std::move(writeView),
61 info.colorType(),
62 info.refColorSpace(),
63 /*surface props*/ nullptr);
64 } else {
65 surfaceContext = std::make_unique<GrSurfaceFillContext>(context,
66 std::move(readView),
67 std::move(writeView),
68 info);
69 }
Greg Danielbfa19c42019-12-19 16:41:40 -050070 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -050071 surfaceContext = std::make_unique<GrSurfaceContext>(context, std::move(readView), info);
Greg Danielbfa19c42019-12-19 16:41:40 -050072 }
Robert Phillips07f0e412020-01-17 15:20:00 -050073 SkDEBUGCODE(surfaceContext->validate();)
Greg Danielbfa19c42019-12-19 16:41:40 -050074 return surfaceContext;
75}
76
Brian Salomona56a7462020-02-07 14:17:25 -050077std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
Brian Salomon14f99fc2020-12-07 12:19:47 -050078 const GrImageInfo& info,
Brian Salomona56a7462020-02-07 14:17:25 -050079 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050080 SkBackingFit fit,
Brian Salomon14f99fc2020-12-07 12:19:47 -050081 GrSurfaceOrigin origin,
82 GrRenderable renderable,
83 int sampleCount,
84 GrMipmapped mipmapped,
85 GrProtected isProtected,
Brian Salomona56a7462020-02-07 14:17:25 -050086 SkBudgeted budgeted) {
Brian Salomon14f99fc2020-12-07 12:19:47 -050087 SkASSERT(context);
88 SkASSERT(renderable == GrRenderable::kYes || sampleCount == 1);
89 if (context->abandoned()) {
90 return nullptr;
Brian Salomond005b692020-04-01 15:47:05 -040091 }
Brian Salomon14f99fc2020-12-07 12:19:47 -050092 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(format,
93 info.dimensions(),
94 renderable,
95 sampleCount,
96 mipmapped,
97 fit,
98 budgeted,
99 isProtected);
Greg Danielbfa19c42019-12-19 16:41:40 -0500100 if (!proxy) {
101 return nullptr;
102 }
103
Brian Salomon14f99fc2020-12-07 12:19:47 -0500104 GrSwizzle swizzle;
105 if (info.colorType() != GrColorType::kUnknown &&
106 !context->priv().caps()->isFormatCompressed(format)) {
107 swizzle = context->priv().caps()->getReadSwizzle(format, info.colorType());
108 }
109
Greg Daniel3912a4b2020-01-14 09:56:04 -0500110 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500111 return GrSurfaceContext::Make(context, std::move(view), info.colorInfo());
112}
113
114std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
115 const GrImageInfo& info,
116 SkBackingFit fit,
117 GrSurfaceOrigin origin,
118 GrRenderable renderable,
119 int sampleCount,
120 GrMipmapped mipmapped,
121 GrProtected isProtected,
122 SkBudgeted budgeted) {
123 GrBackendFormat format = context->priv().caps()->getDefaultBackendFormat(info.colorType(),
124 renderable);
125 return Make(context,
126 info,
127 format,
128 fit,
129 origin,
130 renderable,
131 sampleCount,
132 mipmapped,
133 isProtected,
134 budgeted);
Greg Danielbfa19c42019-12-19 16:41:40 -0500135}
136
Robert Phillips69893702019-02-22 11:16:30 -0500137GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500138 GrSurfaceProxyView readView,
Brian Salomon14f99fc2020-12-07 12:19:47 -0500139 const GrColorInfo& info)
140 : fContext(context), fReadView(std::move(readView)), fColorInfo(info) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400141 SkASSERT(!context->abandoned());
Greg Daniele20fcad2020-01-08 11:52:34 -0500142}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400143
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400144const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); }
145
Robert Phillips0d075de2019-03-04 11:08:13 -0500146GrAuditTrail* GrSurfaceContext::auditTrail() {
147 return fContext->priv().auditTrail();
148}
149
150GrDrawingManager* GrSurfaceContext::drawingManager() {
151 return fContext->priv().drawingManager();
152}
153
154const GrDrawingManager* GrSurfaceContext::drawingManager() const {
155 return fContext->priv().drawingManager();
156}
157
158#ifdef SK_DEBUG
Brian Salomon70fe17e2020-11-30 14:33:58 -0500159GrSingleOwner* GrSurfaceContext::singleOwner() const { return fContext->priv().singleOwner(); }
Robert Phillips0d075de2019-03-04 11:08:13 -0500160#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400161
Brian Salomondd4087d2020-12-23 20:36:44 -0500162static bool alpha_types_compatible(SkAlphaType srcAlphaType, SkAlphaType dstAlphaType) {
163 // If both alpha types are kUnknown things make sense. If not, it's too underspecified.
164 return (srcAlphaType == kUnknown_SkAlphaType) == (dstAlphaType == kUnknown_SkAlphaType);
165}
166
167bool GrSurfaceContext::readPixels(GrDirectContext* dContext, GrPixmap dst, SkIPoint pt) {
Brian Salomon1d435302019-07-01 13:05:28 -0400168 ASSERT_SINGLE_OWNER
169 RETURN_FALSE_IF_ABANDONED
170 SkDEBUGCODE(this->validate();)
171 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400172 if (!fContext->priv().matches(dContext)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400173 return false;
174 }
Brian Salomon46f63232021-01-25 10:13:35 -0500175
176 if (dst.colorType() == GrColorType::kUnknown) {
177 return false;
178 }
179
Brian Salomonc24c8ef2021-02-01 13:32:30 -0500180 if (dst.rowBytes() % dst.info().bpp()) {
181 return false;
182 }
183
Brian Salomondd4087d2020-12-23 20:36:44 -0500184 dst = dst.clip(this->dimensions(), &pt);
185 if (!dst.hasPixels()) {
Brian Salomon1d435302019-07-01 13:05:28 -0400186 return false;
187 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500188 if (!alpha_types_compatible(this->colorInfo().alphaType(), dst.alphaType())) {
Brian Salomon1d435302019-07-01 13:05:28 -0400189 return false;
190 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500191 // We allow unknown alpha types but only if both src and dst are unknown. Otherwise, it's too
192 // weird to reason about what should be expected.
Greg Daniel6eb8c242019-06-05 10:22:24 -0400193
Brian Salomon982127b2021-01-21 10:43:35 -0500194 sk_sp<GrSurfaceProxy> srcProxy = this->asSurfaceProxyRef();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400195
Stephen White3c0a50f2020-01-16 18:19:54 -0500196 if (srcProxy->framebufferOnly()) {
197 return false;
198 }
199
Greg Daniel6eb8c242019-06-05 10:22:24 -0400200 // MDB TODO: delay this instantiation until later in the method
Adlai Hollerc95b5892020-08-11 12:02:22 -0400201 if (!srcProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400202 return false;
203 }
204
205 GrSurface* srcSurface = srcProxy->peekSurface();
206
Brian Salomondd4087d2020-12-23 20:36:44 -0500207 SkColorSpaceXformSteps::Flags flags =
208 SkColorSpaceXformSteps{this->colorInfo(), dst.info()}.flags;
Mike Klein7321e6a2019-12-03 11:08:40 -0600209 bool unpremul = flags.unpremul,
210 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
211 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400212
Adlai Hollerc95b5892020-08-11 12:02:22 -0400213 const GrCaps* caps = dContext->priv().caps();
Robert Phillips07f0e412020-01-17 15:20:00 -0500214 bool srcIsCompressed = caps->isFormatCompressed(srcSurface->backendFormat());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400215 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
216 // care so much about getImageData performance. However, in order to ensure putImageData/
217 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
218 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
219 // fContext->vaildaPMUPMConversionExists()).
Greg Daniel0258c902019-08-01 13:08:33 -0400220 GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
221 GrRenderable::kYes);
Greg Danielc71c7962020-01-14 16:44:18 -0500222 GrColorType srcColorType = this->colorInfo().colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400223 bool canvas2DFastPath = unpremul && !needColorConversion &&
Brian Salomondd4087d2020-12-23 20:36:44 -0500224 (GrColorType::kRGBA_8888 == dst.colorType() ||
225 GrColorType::kBGRA_8888 == dst.colorType()) &&
Brian Salomon1d435302019-07-01 13:05:28 -0400226 SkToBool(srcProxy->asTextureProxy()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500227 (srcColorType == GrColorType::kRGBA_8888 ||
228 srcColorType == GrColorType::kBGRA_8888) &&
Greg Daniel0258c902019-08-01 13:08:33 -0400229 defaultRGBAFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400230 dContext->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400231
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400232 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400233 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400234 return false;
235 }
236
Brian Salomondc0710f2019-07-01 14:59:32 -0400237 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400238 std::unique_ptr<GrSurfaceContext> tempCtx;
239 if (this->asTextureProxy()) {
240 GrColorType colorType = (canvas2DFastPath || srcIsCompressed)
241 ? GrColorType::kRGBA_8888
242 : this->colorInfo().colorType();
Brian Salomondd4087d2020-12-23 20:36:44 -0500243 SkAlphaType alphaType = canvas2DFastPath ? dst.alphaType()
Brian Salomon590f5672020-12-16 11:44:47 -0500244 : this->colorInfo().alphaType();
245 GrImageInfo tempInfo(colorType,
246 alphaType,
247 this->colorInfo().refColorSpace(),
Brian Salomondd4087d2020-12-23 20:36:44 -0500248 dst.dimensions());
Brian Salomon590f5672020-12-16 11:44:47 -0500249 auto sfc = GrSurfaceFillContext::Make(dContext, tempInfo, SkBackingFit::kApprox);
250 if (!sfc) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400251 return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400252 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400253
254 std::unique_ptr<GrFragmentProcessor> fp;
255 if (canvas2DFastPath) {
256 fp = dContext->priv().createPMToUPMEffect(GrTextureEffect::Make(
257 this->readSurfaceView(), this->colorInfo().alphaType()));
Brian Salomondd4087d2020-12-23 20:36:44 -0500258 if (dst.colorType() == GrColorType::kBGRA_8888) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400259 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
Brian Salomondd4087d2020-12-23 20:36:44 -0500260 dst = GrPixmap(dst.info().makeColorType(GrColorType::kRGBA_8888),
261 dst.addr(),
262 dst.rowBytes());
Brian Salomon72c7b982020-10-06 10:07:38 -0400263 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400264 } else {
265 fp = GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType());
266 }
267 if (!fp) {
268 return false;
269 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500270 sfc->fillRectToRectWithFP(SkIRect::MakePtSize(pt, dst.dimensions()),
271 SkIRect::MakeSize(dst.dimensions()),
Brian Salomon590f5672020-12-16 11:44:47 -0500272 std::move(fp));
Brian Salomon72c7b982020-10-06 10:07:38 -0400273 pt = {0, 0};
Brian Salomon590f5672020-12-16 11:44:47 -0500274 tempCtx = std::move(sfc);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400275 } else {
Brian Salomon72c7b982020-10-06 10:07:38 -0400276 auto restrictions = this->caps()->getDstCopyRestrictions(this->asRenderTargetProxy(),
277 this->colorInfo().colorType());
278 sk_sp<GrSurfaceProxy> copy;
279 static constexpr auto kFit = SkBackingFit::kExact;
280 static constexpr auto kBudgeted = SkBudgeted::kYes;
281 static constexpr auto kMipMapped = GrMipMapped::kNo;
282 if (restrictions.fMustCopyWholeSrc) {
Brian Salomon982127b2021-01-21 10:43:35 -0500283 copy = GrSurfaceProxy::Copy(fContext,
284 std::move(srcProxy),
285 this->origin(),
286 kMipMapped,
287 kFit,
Brian Salomon72c7b982020-10-06 10:07:38 -0400288 kBudgeted);
289 } else {
Brian Salomondd4087d2020-12-23 20:36:44 -0500290 auto srcRect = SkIRect::MakePtSize(pt, dst.dimensions());
Brian Salomon982127b2021-01-21 10:43:35 -0500291 copy = GrSurfaceProxy::Copy(fContext,
292 std::move(srcProxy),
293 this->origin(),
294 kMipMapped,
295 srcRect,
296 kFit,
297 kBudgeted,
298 restrictions.fRectsMustMatch);
Brian Salomon72c7b982020-10-06 10:07:38 -0400299 pt = {0, 0};
300 }
301 if (!copy) {
302 return false;
303 }
304 GrSurfaceProxyView view{std::move(copy), this->origin(), this->readSwizzle()};
Brian Salomon14f99fc2020-12-07 12:19:47 -0500305 tempCtx = GrSurfaceContext::Make(dContext, std::move(view), this->colorInfo());
Brian Salomon72c7b982020-10-06 10:07:38 -0400306 SkASSERT(tempCtx);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400307 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500308 return tempCtx->readPixels(dContext, dst, pt);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400309 }
310
Greg Danielb8d84f82020-02-13 14:25:00 -0500311 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400312
Brian Salomon1d435302019-07-01 13:05:28 -0400313 auto supportedRead = caps->supportedReadPixelsColorType(
Brian Salomondd4087d2020-12-23 20:36:44 -0500314 this->colorInfo().colorType(), srcProxy->backendFormat(), dst.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400315
Brian Salomondd4087d2020-12-23 20:36:44 -0500316 bool makeTight =
317 !caps->readPixelsRowBytesSupport() && dst.rowBytes() != dst.info().minRowBytes();
Brian Salomon1047a492019-07-02 12:25:21 -0400318
319 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomondd4087d2020-12-23 20:36:44 -0500320 (dst.colorType() != supportedRead.fColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400321
Brian Salomonf30b1c12019-06-20 12:25:02 -0400322 std::unique_ptr<char[]> tmpPixels;
Brian Salomondd4087d2020-12-23 20:36:44 -0500323 GrPixmap tmp;
324 void* readDst = dst.addr();
325 size_t readRB = dst.rowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400326 if (convert) {
Brian Salomondd4087d2020-12-23 20:36:44 -0500327 GrImageInfo tmpInfo(supportedRead.fColorType,
328 this->colorInfo().alphaType(),
329 this->colorInfo().refColorSpace(),
330 dst.dimensions());
Brian Salomon1d435302019-07-01 13:05:28 -0400331 size_t tmpRB = tmpInfo.minRowBytes();
332 size_t size = tmpRB * tmpInfo.height();
333 // Chrome MSAN bots require the data to be initialized (hence the ()).
John Stilesfbd050b2020-08-03 13:21:46 -0400334 tmpPixels = std::make_unique<char[]>(size);
Brian Salomondd4087d2020-12-23 20:36:44 -0500335 tmp = {tmpInfo, tmpPixels.get(), tmpRB};
Brian Salomonf30b1c12019-06-20 12:25:02 -0400336
Brian Salomonf30b1c12019-06-20 12:25:02 -0400337 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400338 readRB = tmpRB;
Brian Salomondd4087d2020-12-23 20:36:44 -0500339 pt.fY = flip ? srcSurface->height() - pt.fY - dst.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400340 }
341
Brian Salomon982127b2021-01-21 10:43:35 -0500342 dContext->priv().flushSurface(srcProxy.get());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400343 dContext->submit();
Brian Salomondd4087d2020-12-23 20:36:44 -0500344 if (!dContext->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dst.width(), dst.height(),
345 this->colorInfo().colorType(),
Adlai Hollerc95b5892020-08-11 12:02:22 -0400346 supportedRead.fColorType, readDst, readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400347 return false;
348 }
349
Brian Salomondd4087d2020-12-23 20:36:44 -0500350 if (tmp.hasPixels()) {
351 return GrConvertPixels(dst, tmp, flip);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400352 }
353 return true;
354}
Robert Phillips0d075de2019-03-04 11:08:13 -0500355
Brian Salomondd4087d2020-12-23 20:36:44 -0500356bool GrSurfaceContext::writePixels(GrDirectContext* dContext, GrPixmap src, SkIPoint pt) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400357 ASSERT_SINGLE_OWNER
358 RETURN_FALSE_IF_ABANDONED
359 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400360 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400361
Adlai Hollerc95b5892020-08-11 12:02:22 -0400362 if (!dContext) {
Brian Salomonc320b152018-02-20 14:05:36 -0500363 return false;
364 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500365
Brian Salomon1d435302019-07-01 13:05:28 -0400366 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500367 return false;
368 }
369
Brian Salomon46f63232021-01-25 10:13:35 -0500370 if (src.colorType() == GrColorType::kUnknown) {
371 return false;
372 }
373
Brian Salomondd4087d2020-12-23 20:36:44 -0500374 src = src.clip(this->dimensions(), &pt);
375 if (!src.hasPixels()) {
Brian Salomon1d435302019-07-01 13:05:28 -0400376 return false;
377 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500378 if (!alpha_types_compatible(src.alphaType(), this->colorInfo().alphaType())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400379 return false;
380 }
381
382 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
Stephen White3c0a50f2020-01-16 18:19:54 -0500383
384 if (dstProxy->framebufferOnly()) {
385 return false;
386 }
387
Adlai Hollerc95b5892020-08-11 12:02:22 -0400388 if (!dstProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400389 return false;
390 }
391
392 GrSurface* dstSurface = dstProxy->peekSurface();
393
Brian Salomondd4087d2020-12-23 20:36:44 -0500394 SkColorSpaceXformSteps::Flags flags =
395 SkColorSpaceXformSteps{src.info(), this->colorInfo()}.flags;
Mike Klein7321e6a2019-12-03 11:08:40 -0600396 bool unpremul = flags.unpremul,
397 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
398 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400399
Adlai Hollerc95b5892020-08-11 12:02:22 -0400400 const GrCaps* caps = dContext->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400401
402 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
403 GrRenderable::kNo);
404
Greg Danielc71c7962020-01-14 16:44:18 -0500405 GrColorType dstColorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400406 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
407 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400408 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
Brian Salomondd4087d2020-12-23 20:36:44 -0500409 (src.colorType() == GrColorType::kRGBA_8888 ||
410 src.colorType() == GrColorType::kBGRA_8888) &&
Brian Salomon590f5672020-12-16 11:44:47 -0500411 this->asFillContext() &&
Greg Danielc71c7962020-01-14 16:44:18 -0500412 (dstColorType == GrColorType::kRGBA_8888 ||
413 dstColorType == GrColorType::kBGRA_8888) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400414 rgbaDefaultFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400415 dContext->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400416
417 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500418 GrColorInfo tempColorInfo;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400419 GrBackendFormat format;
Greg Danielbfa19c42019-12-19 16:41:40 -0500420 GrSwizzle tempReadSwizzle;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400421 if (canvas2DFastPath) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500422 tempColorInfo = {GrColorType::kRGBA_8888,
423 kUnpremul_SkAlphaType,
424 this->colorInfo().refColorSpace()};
Greg Daniel7bfc9132019-08-14 14:23:53 -0400425 format = rgbaDefaultFormat;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400426 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500427 tempColorInfo = this->colorInfo();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400428 format = dstProxy->backendFormat().makeTexture2D();
429 if (!format.isValid()) {
430 return false;
431 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500432 tempReadSwizzle = this->readSwizzle();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400433 }
434
Greg Daniel2e52ad12019-06-13 10:04:16 -0400435 // It is more efficient for us to write pixels into a top left origin so we prefer that.
436 // However, if the final proxy isn't a render target then we must use a copy to move the
437 // data into it which requires the origins to match. If the final proxy is a render target
438 // we can use a draw instead which doesn't have this origin restriction. Thus for render
439 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400440 GrSurfaceOrigin tempOrigin =
Brian Salomon590f5672020-12-16 11:44:47 -0500441 this->asFillContext() ? kTopLeft_GrSurfaceOrigin : this->origin();
Adlai Hollerc95b5892020-08-11 12:02:22 -0400442 auto tempProxy = dContext->priv().proxyProvider()->createProxy(
Brian Salomondd4087d2020-12-23 20:36:44 -0500443 format, src.dimensions(), GrRenderable::kNo, 1, GrMipmapped::kNo,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400444 SkBackingFit::kApprox, SkBudgeted::kYes, GrProtected::kNo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400445 if (!tempProxy) {
446 return false;
447 }
Greg Daniel3912a4b2020-01-14 09:56:04 -0500448 GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500449 GrSurfaceContext tempCtx(dContext, tempView, tempColorInfo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400450
451 // In the fast path we always write the srcData to the temp context as though it were RGBA.
452 // When the data is really BGRA the write will cause the R and B channels to be swapped in
453 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
454 // dst.
Brian Salomondd4087d2020-12-23 20:36:44 -0500455 GrColorType origSrcColorType = src.colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400456 if (canvas2DFastPath) {
Brian Salomondd4087d2020-12-23 20:36:44 -0500457 src = {src.info().makeColorType(GrColorType::kRGBA_8888), src.addr(), src.rowBytes()};
Brian Salomon1d435302019-07-01 13:05:28 -0400458 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500459 if (!tempCtx.writePixels(dContext, src, {0, 0})) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400460 return false;
461 }
462
Brian Salomon590f5672020-12-16 11:44:47 -0500463 if (this->asFillContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400464 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400465 if (canvas2DFastPath) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400466 fp = dContext->priv().createUPMToPMEffect(
Brian Salomon14f99fc2020-12-07 12:19:47 -0500467 GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType()));
Brian Salomon1d435302019-07-01 13:05:28 -0400468 // Important: check the original src color type here!
Brian Salomondd4087d2020-12-23 20:36:44 -0500469 if (origSrcColorType == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400470 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
471 }
472 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500473 fp = GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400474 }
475 if (!fp) {
476 return false;
477 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500478 this->asFillContext()->fillRectToRectWithFP(SkIRect::MakeSize(src.dimensions()),
479 SkIRect::MakePtSize(pt, src.dimensions()),
480 std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400481 } else {
Brian Salomondd4087d2020-12-23 20:36:44 -0500482 SkIRect srcRect = SkIRect::MakeSize(src.dimensions());
Brian Salomon1d435302019-07-01 13:05:28 -0400483 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Brian Salomon982127b2021-01-21 10:43:35 -0500484 if (!this->copy(std::move(tempProxy), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400485 return false;
486 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400487 }
488 return true;
489 }
490
Brian Salomon1d435302019-07-01 13:05:28 -0400491 GrColorType allowedColorType =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400492 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400493 dstProxy->backendFormat(),
Brian Salomondd4087d2020-12-23 20:36:44 -0500494 src.colorType()).fColorType;
Greg Danielb8d84f82020-02-13 14:25:00 -0500495 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomondd4087d2020-12-23 20:36:44 -0500496 bool makeTight = !caps->writePixelsRowBytesSupport() &&
497 src.rowBytes() != src.info().minRowBytes();
Brian Salomon1047a492019-07-02 12:25:21 -0400498 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomondd4087d2020-12-23 20:36:44 -0500499 (src.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400500
Brian Salomon24949422021-02-11 09:39:46 -0500501 if (convert) {
Brian Salomondd4087d2020-12-23 20:36:44 -0500502 GrImageInfo tmpInfo(allowedColorType,
503 this->colorInfo().alphaType(),
504 this->colorInfo().refColorSpace(),
505 src.dimensions());
Brian Salomonbe1084b2021-01-26 13:29:30 -0500506 GrPixmap tmp = GrPixmap::Allocate(tmpInfo);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400507
Brian Salomondd4087d2020-12-23 20:36:44 -0500508 SkAssertResult(GrConvertPixels(tmp, src, flip));
Brian Salomonf30b1c12019-06-20 12:25:02 -0400509
Brian Salomondd4087d2020-12-23 20:36:44 -0500510 src = tmp;
Brian Salomon1d435302019-07-01 13:05:28 -0400511 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400512 }
513
Brian Salomonbe1084b2021-01-26 13:29:30 -0500514 GrMipLevel level;
515 level.fPixels = src.addr();
516 level.fRowBytes = src.rowBytes();
Brian Salomon24949422021-02-11 09:39:46 -0500517 bool result = dContext->priv().drawingManager()->newWritePixelsTask(
Brian Salomonbe1084b2021-01-26 13:29:30 -0500518 this->asSurfaceProxyRef(),
519 SkIRect::MakePtSize(pt, src.dimensions()),
520 src.colorType(),
521 dstColorType,
522 &level,
523 1,
524 src.pixelStorage());
Brian Salomon24949422021-02-11 09:39:46 -0500525 if (result && !src.ownsPixels()) {
526 // If the pixmap doesn't own its pixels then we must flush so that they are pushed to
527 // the GPU driver before we return.
528 dContext->priv().flushSurface(dstProxy);
529 }
530 return result;
Brian Osman45580d32016-11-23 09:37:01 -0500531}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400532
Adlai Hollerc95b5892020-08-11 12:02:22 -0400533void GrSurfaceContext::asyncRescaleAndReadPixels(GrDirectContext* dContext,
534 const SkImageInfo& info,
Brian Salomon63a0a752020-06-26 13:32:09 -0400535 const SkIRect& srcRect,
536 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500537 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400538 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400539 ReadPixelsContext callbackContext) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400540 if (!dContext) {
541 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400542 return;
543 }
544 auto rt = this->asRenderTargetProxy();
545 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400546 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400547 return;
548 }
549 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400550 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400551 return;
552 }
553 auto dstCT = SkColorTypeToGrColorType(info.colorType());
554 if (dstCT == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400555 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400556 return;
557 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500558 bool needsRescale = srcRect.size() != info.dimensions();
Brian Salomon63a0a752020-06-26 13:32:09 -0400559 auto colorTypeOfFinalContext = this->colorInfo().colorType();
560 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
561 if (needsRescale) {
562 colorTypeOfFinalContext = dstCT;
563 backendFormatOfFinalContext =
564 this->caps()->getDefaultBackendFormat(dstCT, GrRenderable::kYes);
565 }
566 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonbacbb922021-01-21 19:48:00 -0500567 backendFormatOfFinalContext,
568 dstCT);
Brian Salomon63a0a752020-06-26 13:32:09 -0400569 // Fail if we can't read from the source surface's color type.
570 if (readInfo.fColorType == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400571 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400572 return;
573 }
574 // Fail if read color type does not have all of dstCT's color channels and those missing color
575 // channels are in the src.
576 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
577 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
578 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
579 if ((~legalReadChannels & dstChannels) & srcChannels) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400580 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400581 return;
582 }
583
Brian Salomonbacbb922021-01-21 19:48:00 -0500584 std::unique_ptr<GrSurfaceFillContext> tempFC;
Brian Salomon63a0a752020-06-26 13:32:09 -0400585 int x = srcRect.fLeft;
586 int y = srcRect.fTop;
587 if (needsRescale) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500588 tempFC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma, rescaleMode);
589 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400590 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400591 return;
592 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500593 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
594 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400595 x = y = 0;
596 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -0500597 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo(),
598 info.colorInfo());
Brian Salomon63a0a752020-06-26 13:32:09 -0400599 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
600 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
601 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomonbacbb922021-01-21 19:48:00 -0500602 SkIRect srcRectToDraw = srcRect;
Brian Salomon63a0a752020-06-26 13:32:09 -0400603 // If the src is not texturable first try to make a copy to a texture.
604 if (!texProxyView.asTextureProxy()) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500605 texProxyView = GrSurfaceProxyView::Copy(fContext,
606 texProxyView,
607 GrMipmapped::kNo,
608 srcRect,
609 SkBackingFit::kApprox,
610 SkBudgeted::kNo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400611 if (!texProxyView) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400612 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400613 return;
614 }
615 SkASSERT(texProxyView.asTextureProxy());
Brian Salomonbacbb922021-01-21 19:48:00 -0500616 srcRectToDraw = SkIRect::MakeSize(srcRect.size());
Brian Salomon63a0a752020-06-26 13:32:09 -0400617 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500618 auto tempInfo = GrImageInfo(info).makeColorType(this->colorInfo().colorType());
619 tempFC = GrSurfaceFillContext::Make(dContext, tempInfo, SkBackingFit::kApprox);
620 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400621 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400622 return;
623 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500624 auto fp = GrTextureEffect::Make(std::move(texProxyView), this->colorInfo().alphaType());
625 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
626 tempFC->fillRectToRectWithFP(srcRectToDraw,
627 SkIRect::MakeSize(tempFC->dimensions()),
628 std::move(fp));
Brian Salomon63a0a752020-06-26 13:32:09 -0400629 x = y = 0;
630 }
631 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500632 auto srcCtx = tempFC ? tempFC.get() : this;
633 return srcCtx->asyncReadPixels(dContext,
634 SkIRect::MakePtSize({x, y}, info.dimensions()),
635 info.colorType(),
636 callback,
637 callbackContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400638}
639
640class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult {
641public:
642 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
643 ~AsyncReadResult() override {
644 for (int i = 0; i < fPlanes.count(); ++i) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500645 fPlanes[i].releaseMappedBuffer(fInboxID);
Brian Salomon63a0a752020-06-26 13:32:09 -0400646 }
647 }
648
649 int count() const override { return fPlanes.count(); }
Brian Salomonbe1084b2021-01-26 13:29:30 -0500650 const void* data(int i) const override { return fPlanes[i].data(); }
651 size_t rowBytes(int i) const override { return fPlanes[i].rowBytes(); }
Brian Salomon63a0a752020-06-26 13:32:09 -0400652
653 bool addTransferResult(const PixelTransferResult& result,
654 SkISize dimensions,
655 size_t rowBytes,
656 GrClientMappedBufferManager* manager) {
657 SkASSERT(!result.fTransferBuffer->isMapped());
658 const void* mappedData = result.fTransferBuffer->map();
659 if (!mappedData) {
660 return false;
661 }
662 if (result.fPixelConverter) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500663 size_t size = rowBytes*dimensions.height();
664 sk_sp<SkData> data = SkData::MakeUninitialized(size);
665 result.fPixelConverter(data->writable_data(), mappedData);
666 this->addCpuPlane(std::move(data), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400667 result.fTransferBuffer->unmap();
668 } else {
669 manager->insert(result.fTransferBuffer);
670 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
671 }
672 return true;
673 }
674
Brian Salomonbe1084b2021-01-26 13:29:30 -0500675 void addCpuPlane(sk_sp<SkData> data, size_t rowBytes) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400676 SkASSERT(data);
677 SkASSERT(rowBytes > 0);
Brian Salomonbe1084b2021-01-26 13:29:30 -0500678 fPlanes.emplace_back(std::move(data), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400679 }
680
681private:
682 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
683 SkASSERT(data);
684 SkASSERT(rowBytes > 0);
685 SkASSERT(mappedBuffer);
686 SkASSERT(mappedBuffer->isMapped());
Brian Salomonbe1084b2021-01-26 13:29:30 -0500687 fPlanes.emplace_back(std::move(mappedBuffer), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400688 }
689
Brian Salomonbe1084b2021-01-26 13:29:30 -0500690 class Plane {
691 public:
692 Plane(sk_sp<GrGpuBuffer> buffer, size_t rowBytes)
693 : fMappedBuffer(std::move(buffer)), fRowBytes(rowBytes) {}
694 Plane(sk_sp<SkData> data, size_t rowBytes) : fData(std::move(data)), fRowBytes(rowBytes) {}
695
696 Plane(const Plane&) = delete;
697 Plane(Plane&&) = default;
698
699 ~Plane() { SkASSERT(!fMappedBuffer); }
700
701 Plane& operator=(const Plane&) = delete;
702 Plane& operator=(Plane&&) = default;
703
704 void releaseMappedBuffer(uint32_t inboxID) {
705 if (fMappedBuffer) {
706 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
707 {std::move(fMappedBuffer), inboxID});
708 }
709 }
710
711 const void* data() const {
712 if (fMappedBuffer) {
713 SkASSERT(!fData);
714 SkASSERT(fMappedBuffer->isMapped());
715 return fMappedBuffer->map();
716 }
717 SkASSERT(fData);
718 return fData->data();
719 }
720
721 size_t rowBytes() const { return fRowBytes; }
722
723 private:
724 sk_sp<SkData> fData;
Brian Salomon1eea1ea2021-01-26 18:12:25 +0000725 sk_sp<GrGpuBuffer> fMappedBuffer;
Brian Salomonbe1084b2021-01-26 13:29:30 -0500726 size_t fRowBytes;
Brian Salomon63a0a752020-06-26 13:32:09 -0400727 };
728 SkSTArray<3, Plane> fPlanes;
729 uint32_t fInboxID;
730};
731
Adlai Hollerc95b5892020-08-11 12:02:22 -0400732void GrSurfaceContext::asyncReadPixels(GrDirectContext* dContext,
733 const SkIRect& rect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400734 SkColorType colorType,
735 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400736 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400737 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
738 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
739
Adlai Hollerc95b5892020-08-11 12:02:22 -0400740 if (!dContext || this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
741 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400742 return;
743 }
744
Adlai Hollerc95b5892020-08-11 12:02:22 -0400745 auto mappedBufferManager = dContext->priv().clientMappedBufferManager();
Brian Salomon63a0a752020-06-26 13:32:09 -0400746
747 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
748
749 if (!transferResult.fTransferBuffer) {
750 auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
751 this->colorInfo().refColorSpace());
752 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomonbe1084b2021-01-26 13:29:30 -0500753 GrPixmap pm = GrPixmap::Allocate(ii);
754 result->addCpuPlane(pm.pixelStorage(), pm.rowBytes());
Brian Salomon63a0a752020-06-26 13:32:09 -0400755
Adlai Hollerc95b5892020-08-11 12:02:22 -0400756 SkIPoint pt{rect.fLeft, rect.fTop};
Brian Salomondd4087d2020-12-23 20:36:44 -0500757 if (!this->readPixels(dContext, pm, pt)) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400758 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400759 return;
760 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400761 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400762 return;
763 }
764
765 struct FinishContext {
766 ReadPixelsCallback* fClientCallback;
767 ReadPixelsContext fClientContext;
768 SkISize fSize;
769 SkColorType fColorType;
770 GrClientMappedBufferManager* fMappedBufferManager;
771 PixelTransferResult fTransferResult;
772 };
773 // Assumption is that the caller would like to flush. We could take a parameter or require an
774 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
775 // callback to GrGpu until after the next flush that flushes our op list, though.
776 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400777 callbackContext,
Brian Salomon63a0a752020-06-26 13:32:09 -0400778 rect.size(),
779 colorType,
780 mappedBufferManager,
781 std::move(transferResult)};
782 auto finishCallback = [](GrGpuFinishedContext c) {
783 const auto* context = reinterpret_cast<const FinishContext*>(c);
784 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
785 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
786 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
787 context->fMappedBufferManager)) {
788 result.reset();
789 }
790 (*context->fClientCallback)(context->fClientContext, std::move(result));
791 delete context;
792 };
793 GrFlushInfo flushInfo;
794 flushInfo.fFinishedContext = finishContext;
795 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -0500796
797 dContext->priv().flushSurface(this->asSurfaceProxy(),
798 SkSurface::BackendSurfaceAccess::kNoAccess,
799 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400800}
801
Adlai Hollerc95b5892020-08-11 12:02:22 -0400802void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(GrDirectContext* dContext,
803 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400804 sk_sp<SkColorSpace> dstColorSpace,
805 const SkIRect& srcRect,
806 SkISize dstSize,
807 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500808 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400809 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400810 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400811 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
812 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
813 SkASSERT(!dstSize.isZero());
814 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
815
Adlai Hollerc95b5892020-08-11 12:02:22 -0400816 if (!dContext) {
817 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400818 return;
819 }
820 auto rt = this->asRenderTargetProxy();
821 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400822 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400823 return;
824 }
825 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400826 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400827 return;
828 }
829 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400830 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400831 return;
832 }
833 int x = srcRect.fLeft;
834 int y = srcRect.fTop;
835 bool needsRescale = srcRect.size() != dstSize;
836 GrSurfaceProxyView srcView;
Brian Salomonbacbb922021-01-21 19:48:00 -0500837 auto info = SkImageInfo::Make(dstSize,
838 kRGBA_8888_SkColorType,
839 this->colorInfo().alphaType(),
840 dstColorSpace);
Brian Salomon63a0a752020-06-26 13:32:09 -0400841 if (needsRescale) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400842 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomonbacbb922021-01-21 19:48:00 -0500843 auto tempFC = this->rescale(info,
844 kTopLeft_GrSurfaceOrigin,
845 srcRect,
846 rescaleGamma,
847 rescaleMode);
848 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400849 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400850 return;
851 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500852 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
853 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400854 x = y = 0;
Brian Salomonbacbb922021-01-21 19:48:00 -0500855 srcView = tempFC->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400856 } else {
857 srcView = this->readSurfaceView();
858 if (!srcView.asTextureProxy()) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500859 srcView = GrSurfaceProxyView::Copy(fContext,
860 std::move(srcView),
861 GrMipmapped::kNo,
862 srcRect,
863 SkBackingFit::kApprox,
864 SkBudgeted::kYes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400865 if (!srcView) {
866 // If we can't get a texture copy of the contents then give up.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400867 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400868 return;
869 }
870 SkASSERT(srcView.asTextureProxy());
871 x = y = 0;
872 }
873 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomonbacbb922021-01-21 19:48:00 -0500874 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo(),
875 info.colorInfo());
Brian Salomon63a0a752020-06-26 13:32:09 -0400876 if (xform) {
877 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
Brian Salomonbacbb922021-01-21 19:48:00 -0500878 auto tempFC = GrSurfaceFillContext::Make(dContext,
879 info,
880 SkBackingFit::kApprox,
881 1,
882 GrMipmapped::kNo,
883 GrProtected::kNo,
884 kTopLeft_GrSurfaceOrigin);
885 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400886 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400887 return;
888 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500889 auto fp = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType());
890 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
891 tempFC->fillRectToRectWithFP(srcRectToDraw,
892 SkIRect::MakeSize(tempFC->dimensions()),
893 std::move(fp));
894 srcView = tempFC->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400895 SkASSERT(srcView.asTextureProxy());
896 x = y = 0;
897 }
898 }
899
Brian Salomonbacbb922021-01-21 19:48:00 -0500900 auto yInfo = SkImageInfo::MakeA8(dstSize);
901 auto yFC = GrSurfaceFillContext::MakeWithFallback(dContext, yInfo, SkBackingFit::kApprox);
902
903 auto uvInfo = yInfo.makeWH(yInfo.width()/2, yInfo.height()/2);
904 auto uFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
905 auto vFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
906
907 if (!yFC || !uFC || !vFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400908 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400909 return;
910 }
911
912 float baseM[20];
913 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
914
915 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
916
917 auto texMatrix = SkMatrix::Translate(x, y);
918
Brian Salomon63a0a752020-06-26 13:32:09 -0400919 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
920 PixelTransferResult yTransfer, uTransfer, vTransfer;
921
922 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
923 float yM[20];
924 std::fill_n(yM, 15, 0.f);
925 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500926
927 auto yFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
928 yFP = GrColorMatrixFragmentProcessor::Make(std::move(yFP),
929 yM,
930 /*unpremulInput=*/false,
931 /*clampRGBOutput=*/true,
932 /*premulOutput=*/false);
933 yFC->fillWithFP(std::move(yFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400934 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500935 yTransfer = yFC->transferPixels(GrColorType::kAlpha_8,
936 SkIRect::MakeSize(yFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400937 if (!yTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400938 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400939 return;
940 }
941 }
942
943 texMatrix.preScale(2.f, 2.f);
944 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
945 float uM[20];
946 std::fill_n(uM, 15, 0.f);
947 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500948
949 auto uFP = GrTextureEffect::Make(srcView,
950 this->colorInfo().alphaType(),
951 texMatrix,
952 GrSamplerState::Filter::kLinear);
953 uFP = GrColorMatrixFragmentProcessor::Make(std::move(uFP),
954 uM,
955 /*unpremulInput=*/false,
956 /*clampRGBOutput=*/true,
957 /*premulOutput=*/false);
958 uFC->fillWithFP(std::move(uFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400959 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500960 uTransfer = uFC->transferPixels(GrColorType::kAlpha_8,
961 SkIRect::MakeSize(uFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400962 if (!uTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400963 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400964 return;
965 }
966 }
967
968 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
969 float vM[20];
970 std::fill_n(vM, 15, 0.f);
971 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500972 auto vFP = GrTextureEffect::Make(std::move(srcView),
973 this->colorInfo().alphaType(),
974 texMatrix,
975 GrSamplerState::Filter::kLinear);
976 vFP = GrColorMatrixFragmentProcessor::Make(std::move(vFP),
977 vM,
978 /*unpremulInput=*/false,
979 /*clampRGBOutput=*/true,
980 /*premulOutput=*/false);
981 vFC->fillWithFP(std::move(vFP));
982
Brian Salomon63a0a752020-06-26 13:32:09 -0400983 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500984 vTransfer = vFC->transferPixels(GrColorType::kAlpha_8,
985 SkIRect::MakeSize(vFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400986 if (!vTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400987 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400988 return;
989 }
990 }
991
992 if (doSynchronousRead) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500993 GrPixmap yPmp = GrPixmap::Allocate(yInfo);
994 GrPixmap uPmp = GrPixmap::Allocate(uvInfo);
995 GrPixmap vPmp = GrPixmap::Allocate(uvInfo);
Brian Salomonbacbb922021-01-21 19:48:00 -0500996 if (!yFC->readPixels(dContext, yPmp, {0, 0}) ||
997 !uFC->readPixels(dContext, uPmp, {0, 0}) ||
998 !vFC->readPixels(dContext, vPmp, {0, 0})) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400999 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001000 return;
1001 }
Adlai Hollerc95b5892020-08-11 12:02:22 -04001002 auto result = std::make_unique<AsyncReadResult>(dContext->priv().contextID());
Brian Salomonbe1084b2021-01-26 13:29:30 -05001003 result->addCpuPlane(yPmp.pixelStorage(), yPmp.rowBytes());
1004 result->addCpuPlane(uPmp.pixelStorage(), uPmp.rowBytes());
1005 result->addCpuPlane(vPmp.pixelStorage(), vPmp.rowBytes());
Adlai Hollerc95b5892020-08-11 12:02:22 -04001006 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -04001007 return;
1008 }
1009
1010 struct FinishContext {
1011 ReadPixelsCallback* fClientCallback;
1012 ReadPixelsContext fClientContext;
1013 GrClientMappedBufferManager* fMappedBufferManager;
1014 SkISize fSize;
1015 PixelTransferResult fYTransfer;
1016 PixelTransferResult fUTransfer;
1017 PixelTransferResult fVTransfer;
1018 };
1019 // Assumption is that the caller would like to flush. We could take a parameter or require an
1020 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1021 // callback to GrGpu until after the next flush that flushes our op list, though.
1022 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -04001023 callbackContext,
1024 dContext->priv().clientMappedBufferManager(),
Brian Salomon63a0a752020-06-26 13:32:09 -04001025 dstSize,
1026 std::move(yTransfer),
1027 std::move(uTransfer),
1028 std::move(vTransfer)};
1029 auto finishCallback = [](GrGpuFinishedContext c) {
1030 const auto* context = reinterpret_cast<const FinishContext*>(c);
1031 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
1032 auto manager = context->fMappedBufferManager;
1033 size_t rowBytes = SkToSizeT(context->fSize.width());
1034 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
1035 (*context->fClientCallback)(context->fClientContext, nullptr);
1036 delete context;
1037 return;
1038 }
1039 rowBytes /= 2;
1040 SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
1041 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
1042 (*context->fClientCallback)(context->fClientContext, nullptr);
1043 delete context;
1044 return;
1045 }
1046 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
1047 (*context->fClientCallback)(context->fClientContext, nullptr);
1048 delete context;
1049 return;
1050 }
1051 (*context->fClientCallback)(context->fClientContext, std::move(result));
1052 delete context;
1053 };
1054 GrFlushInfo flushInfo;
1055 flushInfo.fFinishedContext = finishContext;
1056 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -05001057 dContext->priv().flushSurface(this->asSurfaceProxy(),
1058 SkSurface::BackendSurfaceAccess::kNoAccess,
1059 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -04001060}
1061
Brian Salomon982127b2021-01-21 10:43:35 -05001062bool GrSurfaceContext::copy(sk_sp<GrSurfaceProxy> src, SkIRect srcRect, SkIPoint dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001063 ASSERT_SINGLE_OWNER
1064 RETURN_FALSE_IF_ABANDONED
1065 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -04001066 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -04001067
Brian Salomon947efe22019-07-16 15:36:11 -04001068 const GrCaps* caps = fContext->priv().caps();
1069
Greg Daniel46cfbc62019-06-07 11:43:30 -04001070 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -05001071 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -04001072
Stephen White3c0a50f2020-01-16 18:19:54 -05001073 if (this->asSurfaceProxy()->framebufferOnly()) {
1074 return false;
1075 }
1076
Brian Salomon982127b2021-01-21 10:43:35 -05001077 if (!caps->canCopySurface(this->asSurfaceProxy(), src.get(), srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -04001078 return false;
1079 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001080
Brian Salomon982127b2021-01-21 10:43:35 -05001081 return this->drawingManager()->newCopyRenderTask(std::move(src),
1082 srcRect,
1083 this->asSurfaceProxyRef(),
Brian Salomon0f9f8002021-01-22 16:30:50 -05001084 dstPoint,
1085 this->origin());
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001086}
Greg Daniel46cfbc62019-06-07 11:43:30 -04001087
Brian Salomonbacbb922021-01-21 19:48:00 -05001088std::unique_ptr<GrSurfaceFillContext> GrSurfaceContext::rescale(const GrImageInfo& info,
Brian Salomoneebe7352020-12-09 16:37:04 -05001089 GrSurfaceOrigin origin,
1090 SkIRect srcRect,
1091 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001092 RescaleMode rescaleMode) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001093 auto sfc = GrSurfaceFillContext::MakeWithFallback(fContext,
1094 info,
Brian Salomon1c86b632020-12-11 12:36:01 -05001095 SkBackingFit::kExact,
Brian Salomon1c86b632020-12-11 12:36:01 -05001096 1,
1097 GrMipmapped::kNo,
1098 this->asSurfaceProxy()->isProtected(),
1099 origin);
Brian Salomonbacbb922021-01-21 19:48:00 -05001100 if (!sfc || !this->rescaleInto(sfc.get(),
1101 SkIRect::MakeSize(sfc->dimensions()),
Brian Salomon1c86b632020-12-11 12:36:01 -05001102 srcRect,
1103 rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001104 rescaleMode)) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001105 return nullptr;
1106 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001107 return sfc;
Brian Salomon1c86b632020-12-11 12:36:01 -05001108}
1109
Brian Salomonbacbb922021-01-21 19:48:00 -05001110bool GrSurfaceContext::rescaleInto(GrSurfaceFillContext* dst,
Brian Salomon1c86b632020-12-11 12:36:01 -05001111 SkIRect dstRect,
1112 SkIRect srcRect,
1113 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001114 RescaleMode rescaleMode) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001115 SkASSERT(dst);
1116 if (!SkIRect::MakeSize(dst->dimensions()).contains((dstRect))) {
1117 return false;
1118 }
1119
Brian Salomone9ad9982019-07-22 16:17:41 -04001120 auto rtProxy = this->asRenderTargetProxy();
1121 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001122 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001123 }
1124
Stephen White3c0a50f2020-01-16 18:19:54 -05001125 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001126 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001127 }
1128
Greg Daniel40903af2020-01-30 14:55:05 -05001129 GrSurfaceProxyView texView = this->readSurfaceView();
Greg Daniel40903af2020-01-30 14:55:05 -05001130 if (!texView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -04001131 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect,
Brian Salomonc5243782020-04-02 12:50:34 -04001132 SkBackingFit::kApprox, SkBudgeted::kNo);
1133 if (!texView) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001134 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001135 }
Greg Daniel40903af2020-01-30 14:55:05 -05001136 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001137 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001138 }
1139
Brian Salomon1c86b632020-12-11 12:36:01 -05001140 SkISize finalSize = dstRect.size();
1141
Brian Salomonbf6b9792019-08-21 09:38:10 -04001142 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
1143 // 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 -05001144 std::unique_ptr<GrSurfaceFillContext> tempA;
1145 std::unique_ptr<GrSurfaceFillContext> tempB;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001146
Brian Salomone9ad9982019-07-22 16:17:41 -04001147 // Assume we should ignore the rescale linear request if the surface has no color space since
1148 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon63a0a752020-06-26 13:32:09 -04001149 if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() &&
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001150 !this->colorInfo().colorSpace()->gammaIsLinear()) {
1151 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomone9ad9982019-07-22 16:17:41 -04001152 // We'll fall back to kRGBA_8888 if half float not supported.
Brian Salomonbacbb922021-01-21 19:48:00 -05001153 GrImageInfo ii(GrColorType::kRGBA_F16,
1154 dst->colorInfo().alphaType(),
1155 std::move(cs),
1156 srcRect.size());
1157 auto linearRTC = GrSurfaceFillContext::MakeWithFallback(fContext,
1158 std::move(ii),
1159 SkBackingFit::kApprox,
1160 1,
1161 GrMipmapped::kNo,
1162 GrProtected::kNo,
1163 dst->origin());
Brian Salomone9ad9982019-07-22 16:17:41 -04001164 if (!linearRTC) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001165 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001166 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001167 auto fp = GrTextureEffect::Make(std::move(texView),
1168 this->colorInfo().alphaType(),
1169 SkMatrix::Translate(srcRect.topLeft()),
1170 GrSamplerState::Filter::kNearest,
1171 GrSamplerState::MipmapMode::kNone);
1172 fp = GrColorSpaceXformEffect::Make(std::move(fp),
1173 this->colorInfo(),
1174 linearRTC->colorInfo());
1175 linearRTC->fillWithFP(std::move(fp));
Greg Daniel40903af2020-01-30 14:55:05 -05001176 texView = linearRTC->readSurfaceView();
1177 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -04001178 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001179 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001180 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001181
Brian Salomon1c86b632020-12-11 12:36:01 -05001182 while (srcRect.size() != finalSize) {
1183 SkISize nextDims = finalSize;
Mike Reed1efa14d2021-01-02 21:44:59 -05001184 if (rescaleMode != RescaleMode::kNearest) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001185 if (srcRect.width() > finalSize.width()) {
1186 nextDims.fWidth = std::max((srcRect.width() + 1)/2, finalSize.width());
1187 } else if (srcRect.width() < finalSize.width()) {
1188 nextDims.fWidth = std::min(srcRect.width()*2, finalSize.width());
Brian Salomon59f31b12020-06-04 17:27:15 -04001189 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001190 if (srcRect.height() > finalSize.height()) {
1191 nextDims.fHeight = std::max((srcRect.height() + 1)/2, finalSize.height());
1192 } else if (srcRect.height() < finalSize.height()) {
1193 nextDims.fHeight = std::min(srcRect.height()*2, finalSize.height());
Brian Salomon59f31b12020-06-04 17:27:15 -04001194 }
1195 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001196 auto input = tempA ? tempA.get() : this;
Brian Salomone9ad9982019-07-22 16:17:41 -04001197 sk_sp<GrColorSpaceXform> xform;
Brian Salomonbacbb922021-01-21 19:48:00 -05001198 GrSurfaceFillContext* stepDst;
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001199 SkIRect stepDstRect;
Brian Salomon1c86b632020-12-11 12:36:01 -05001200 if (nextDims == finalSize) {
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001201 stepDst = dst;
1202 stepDstRect = dstRect;
Brian Salomonbacbb922021-01-21 19:48:00 -05001203 xform = GrColorSpaceXform::Make(input->colorInfo(), dst->colorInfo());
Brian Salomon1c86b632020-12-11 12:36:01 -05001204 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -05001205 GrImageInfo nextInfo(input->colorInfo(), nextDims);
1206 tempB = GrSurfaceFillContext::MakeWithFallback(fContext,
1207 nextInfo,
1208 SkBackingFit::kApprox);
Brian Salomon1c86b632020-12-11 12:36:01 -05001209 if (!tempB) {
1210 return false;
1211 }
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001212 stepDst = tempB.get();
1213 stepDstRect = SkIRect::MakeSize(tempB->dimensions());
Brian Salomone9ad9982019-07-22 16:17:41 -04001214 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001215 std::unique_ptr<GrFragmentProcessor> fp;
Mike Reed1efa14d2021-01-02 21:44:59 -05001216 if (rescaleMode == RescaleMode::kRepeatedCubic) {
Brian Salomon59f31b12020-06-04 17:27:15 -04001217 auto dir = GrBicubicEffect::Direction::kXY;
1218 if (nextDims.width() == srcRect.width()) {
1219 dir = GrBicubicEffect::Direction::kY;
1220 } else if (nextDims.height() == srcRect.height()) {
1221 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -04001222 }
Brian Salomon1af72d12020-06-25 10:47:26 -04001223 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
Mike Reed3867c702020-09-01 13:28:10 -04001224 static constexpr auto kKernel = GrBicubicEffect::gCatmullRom;
Brian Salomon1c86b632020-12-11 12:36:01 -05001225 fp = GrBicubicEffect::MakeSubset(std::move(texView),
1226 input->colorInfo().alphaType(),
Brian Salomonbacbb922021-01-21 19:48:00 -05001227 SkMatrix::I(),
Brian Salomon1c86b632020-12-11 12:36:01 -05001228 kWM,
1229 kWM,
1230 SkRect::Make(srcRect),
1231 kKernel,
1232 dir,
1233 *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -04001234 } else {
Mike Reed1efa14d2021-01-02 21:44:59 -05001235 auto filter = rescaleMode == RescaleMode::kNearest ? GrSamplerState::Filter::kNearest
1236 : GrSamplerState::Filter::kLinear;
Brian Salomonbacbb922021-01-21 19:48:00 -05001237 auto srcRectF = SkRect::Make(srcRect);
1238 fp = GrTextureEffect::MakeSubset(std::move(texView),
1239 this->colorInfo().alphaType(),
1240 SkMatrix::I(),
1241 {filter, GrSamplerState::MipmapMode::kNone},
1242 srcRectF,
1243 srcRectF,
1244 *this->caps());
Brian Salomone9ad9982019-07-22 16:17:41 -04001245 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001246 if (xform) {
1247 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
1248 }
1249 stepDst->fillRectToRectWithFP(srcRect, stepDstRect, std::move(fp));
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001250 texView = stepDst->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -04001251 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001252 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomone9ad9982019-07-22 16:17:41 -04001253 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001254 return true;
Brian Salomone9ad9982019-07-22 16:17:41 -04001255}
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001256
1257GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
1258 const SkIRect& rect) {
1259 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1260 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001261 auto direct = fContext->asDirectContext();
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001262 if (!direct) {
1263 return {};
1264 }
1265 auto rtProxy = this->asRenderTargetProxy();
1266 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1267 return {};
1268 }
1269
1270 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001271 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
1272 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001273 // Fail if read color type does not have all of dstCT's color channels and those missing color
1274 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001275 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1276 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
1277 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1278 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001279 return {};
1280 }
1281
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001282 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001283 !supportedRead.fOffsetAlignmentForTransferBuffer) {
1284 return {};
1285 }
1286
1287 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
1288 size_t size = rowBytes * rect.height();
Greg Daniel2e967df2021-02-08 10:38:31 -05001289 // By using kStream_GrAccessPattern here, we are not able to cache and reuse the buffer for
1290 // multiple reads. Switching to kDynamic_GrAccessPattern would allow for this, however doing
1291 // so causes a crash in a chromium test. See skbug.com/11297
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001292 auto buffer = direct->priv().resourceProvider()->createBuffer(
Greg Daniel393debc2021-02-06 03:37:20 +00001293 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001294 if (!buffer) {
1295 return {};
1296 }
1297 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -05001298 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001299 if (flip) {
1300 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1301 this->height() - rect.fTop);
1302 }
Greg Danielbbfec9d2019-08-20 10:56:51 -04001303 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001304 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -04001305 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001306 PixelTransferResult result;
1307 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001308 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -04001309 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001310 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
1311 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001312 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
1313 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001314 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
1315 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -04001316 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001317 };
1318 }
1319 return result;
1320}
Greg Daniel46e366a2019-12-16 14:38:36 -05001321
1322#ifdef SK_DEBUG
1323void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001324 SkASSERT(fReadView.proxy());
1325 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -04001326 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
1327 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
1328 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
1329 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001330 this->onValidate();
1331}
1332#endif