blob: fc113363bf79e5bb3a81f942f5fcff496d0a681b [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 Salomonbe1084b2021-01-26 13:29:30 -0500501 if (convert || !src.ownsPixels()) {
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();
517 return dContext->priv().drawingManager()->newWritePixelsTask(
518 this->asSurfaceProxyRef(),
519 SkIRect::MakePtSize(pt, src.dimensions()),
520 src.colorType(),
521 dstColorType,
522 &level,
523 1,
524 src.pixelStorage());
Brian Osman45580d32016-11-23 09:37:01 -0500525}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400526
Adlai Hollerc95b5892020-08-11 12:02:22 -0400527void GrSurfaceContext::asyncRescaleAndReadPixels(GrDirectContext* dContext,
528 const SkImageInfo& info,
Brian Salomon63a0a752020-06-26 13:32:09 -0400529 const SkIRect& srcRect,
530 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500531 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400532 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400533 ReadPixelsContext callbackContext) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400534 if (!dContext) {
535 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400536 return;
537 }
538 auto rt = this->asRenderTargetProxy();
539 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400540 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400541 return;
542 }
543 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400544 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400545 return;
546 }
547 auto dstCT = SkColorTypeToGrColorType(info.colorType());
548 if (dstCT == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400549 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400550 return;
551 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500552 bool needsRescale = srcRect.size() != info.dimensions();
Brian Salomon63a0a752020-06-26 13:32:09 -0400553 auto colorTypeOfFinalContext = this->colorInfo().colorType();
554 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
555 if (needsRescale) {
556 colorTypeOfFinalContext = dstCT;
557 backendFormatOfFinalContext =
558 this->caps()->getDefaultBackendFormat(dstCT, GrRenderable::kYes);
559 }
560 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonbacbb922021-01-21 19:48:00 -0500561 backendFormatOfFinalContext,
562 dstCT);
Brian Salomon63a0a752020-06-26 13:32:09 -0400563 // Fail if we can't read from the source surface's color type.
564 if (readInfo.fColorType == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400565 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400566 return;
567 }
568 // Fail if read color type does not have all of dstCT's color channels and those missing color
569 // channels are in the src.
570 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
571 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
572 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
573 if ((~legalReadChannels & dstChannels) & srcChannels) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400574 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400575 return;
576 }
577
Brian Salomonbacbb922021-01-21 19:48:00 -0500578 std::unique_ptr<GrSurfaceFillContext> tempFC;
Brian Salomon63a0a752020-06-26 13:32:09 -0400579 int x = srcRect.fLeft;
580 int y = srcRect.fTop;
581 if (needsRescale) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500582 tempFC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma, rescaleMode);
583 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400584 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400585 return;
586 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500587 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
588 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400589 x = y = 0;
590 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -0500591 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo(),
592 info.colorInfo());
Brian Salomon63a0a752020-06-26 13:32:09 -0400593 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
594 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
595 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomonbacbb922021-01-21 19:48:00 -0500596 SkIRect srcRectToDraw = srcRect;
Brian Salomon63a0a752020-06-26 13:32:09 -0400597 // If the src is not texturable first try to make a copy to a texture.
598 if (!texProxyView.asTextureProxy()) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500599 texProxyView = GrSurfaceProxyView::Copy(fContext,
600 texProxyView,
601 GrMipmapped::kNo,
602 srcRect,
603 SkBackingFit::kApprox,
604 SkBudgeted::kNo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400605 if (!texProxyView) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400606 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400607 return;
608 }
609 SkASSERT(texProxyView.asTextureProxy());
Brian Salomonbacbb922021-01-21 19:48:00 -0500610 srcRectToDraw = SkIRect::MakeSize(srcRect.size());
Brian Salomon63a0a752020-06-26 13:32:09 -0400611 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500612 auto tempInfo = GrImageInfo(info).makeColorType(this->colorInfo().colorType());
613 tempFC = GrSurfaceFillContext::Make(dContext, tempInfo, SkBackingFit::kApprox);
614 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400615 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400616 return;
617 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500618 auto fp = GrTextureEffect::Make(std::move(texProxyView), this->colorInfo().alphaType());
619 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
620 tempFC->fillRectToRectWithFP(srcRectToDraw,
621 SkIRect::MakeSize(tempFC->dimensions()),
622 std::move(fp));
Brian Salomon63a0a752020-06-26 13:32:09 -0400623 x = y = 0;
624 }
625 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500626 auto srcCtx = tempFC ? tempFC.get() : this;
627 return srcCtx->asyncReadPixels(dContext,
628 SkIRect::MakePtSize({x, y}, info.dimensions()),
629 info.colorType(),
630 callback,
631 callbackContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400632}
633
634class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult {
635public:
636 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
637 ~AsyncReadResult() override {
638 for (int i = 0; i < fPlanes.count(); ++i) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500639 fPlanes[i].releaseMappedBuffer(fInboxID);
Brian Salomon63a0a752020-06-26 13:32:09 -0400640 }
641 }
642
643 int count() const override { return fPlanes.count(); }
Brian Salomonbe1084b2021-01-26 13:29:30 -0500644 const void* data(int i) const override { return fPlanes[i].data(); }
645 size_t rowBytes(int i) const override { return fPlanes[i].rowBytes(); }
Brian Salomon63a0a752020-06-26 13:32:09 -0400646
647 bool addTransferResult(const PixelTransferResult& result,
648 SkISize dimensions,
649 size_t rowBytes,
650 GrClientMappedBufferManager* manager) {
651 SkASSERT(!result.fTransferBuffer->isMapped());
652 const void* mappedData = result.fTransferBuffer->map();
653 if (!mappedData) {
654 return false;
655 }
656 if (result.fPixelConverter) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500657 size_t size = rowBytes*dimensions.height();
658 sk_sp<SkData> data = SkData::MakeUninitialized(size);
659 result.fPixelConverter(data->writable_data(), mappedData);
660 this->addCpuPlane(std::move(data), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400661 result.fTransferBuffer->unmap();
662 } else {
663 manager->insert(result.fTransferBuffer);
664 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
665 }
666 return true;
667 }
668
Brian Salomonbe1084b2021-01-26 13:29:30 -0500669 void addCpuPlane(sk_sp<SkData> data, size_t rowBytes) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400670 SkASSERT(data);
671 SkASSERT(rowBytes > 0);
Brian Salomonbe1084b2021-01-26 13:29:30 -0500672 fPlanes.emplace_back(std::move(data), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400673 }
674
675private:
676 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
677 SkASSERT(data);
678 SkASSERT(rowBytes > 0);
679 SkASSERT(mappedBuffer);
680 SkASSERT(mappedBuffer->isMapped());
Brian Salomonbe1084b2021-01-26 13:29:30 -0500681 fPlanes.emplace_back(std::move(mappedBuffer), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400682 }
683
Brian Salomonbe1084b2021-01-26 13:29:30 -0500684 class Plane {
685 public:
686 Plane(sk_sp<GrGpuBuffer> buffer, size_t rowBytes)
687 : fMappedBuffer(std::move(buffer)), fRowBytes(rowBytes) {}
688 Plane(sk_sp<SkData> data, size_t rowBytes) : fData(std::move(data)), fRowBytes(rowBytes) {}
689
690 Plane(const Plane&) = delete;
691 Plane(Plane&&) = default;
692
693 ~Plane() { SkASSERT(!fMappedBuffer); }
694
695 Plane& operator=(const Plane&) = delete;
696 Plane& operator=(Plane&&) = default;
697
698 void releaseMappedBuffer(uint32_t inboxID) {
699 if (fMappedBuffer) {
700 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
701 {std::move(fMappedBuffer), inboxID});
702 }
703 }
704
705 const void* data() const {
706 if (fMappedBuffer) {
707 SkASSERT(!fData);
708 SkASSERT(fMappedBuffer->isMapped());
709 return fMappedBuffer->map();
710 }
711 SkASSERT(fData);
712 return fData->data();
713 }
714
715 size_t rowBytes() const { return fRowBytes; }
716
717 private:
718 sk_sp<SkData> fData;
Brian Salomon1eea1ea2021-01-26 18:12:25 +0000719 sk_sp<GrGpuBuffer> fMappedBuffer;
Brian Salomonbe1084b2021-01-26 13:29:30 -0500720 size_t fRowBytes;
Brian Salomon63a0a752020-06-26 13:32:09 -0400721 };
722 SkSTArray<3, Plane> fPlanes;
723 uint32_t fInboxID;
724};
725
Adlai Hollerc95b5892020-08-11 12:02:22 -0400726void GrSurfaceContext::asyncReadPixels(GrDirectContext* dContext,
727 const SkIRect& rect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400728 SkColorType colorType,
729 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400730 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400731 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
732 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
733
Adlai Hollerc95b5892020-08-11 12:02:22 -0400734 if (!dContext || this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
735 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400736 return;
737 }
738
Adlai Hollerc95b5892020-08-11 12:02:22 -0400739 auto mappedBufferManager = dContext->priv().clientMappedBufferManager();
Brian Salomon63a0a752020-06-26 13:32:09 -0400740
741 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
742
743 if (!transferResult.fTransferBuffer) {
744 auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
745 this->colorInfo().refColorSpace());
746 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomonbe1084b2021-01-26 13:29:30 -0500747 GrPixmap pm = GrPixmap::Allocate(ii);
748 result->addCpuPlane(pm.pixelStorage(), pm.rowBytes());
Brian Salomon63a0a752020-06-26 13:32:09 -0400749
Adlai Hollerc95b5892020-08-11 12:02:22 -0400750 SkIPoint pt{rect.fLeft, rect.fTop};
Brian Salomondd4087d2020-12-23 20:36:44 -0500751 if (!this->readPixels(dContext, pm, pt)) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400752 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400753 return;
754 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400755 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400756 return;
757 }
758
759 struct FinishContext {
760 ReadPixelsCallback* fClientCallback;
761 ReadPixelsContext fClientContext;
762 SkISize fSize;
763 SkColorType fColorType;
764 GrClientMappedBufferManager* fMappedBufferManager;
765 PixelTransferResult fTransferResult;
766 };
767 // Assumption is that the caller would like to flush. We could take a parameter or require an
768 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
769 // callback to GrGpu until after the next flush that flushes our op list, though.
770 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400771 callbackContext,
Brian Salomon63a0a752020-06-26 13:32:09 -0400772 rect.size(),
773 colorType,
774 mappedBufferManager,
775 std::move(transferResult)};
776 auto finishCallback = [](GrGpuFinishedContext c) {
777 const auto* context = reinterpret_cast<const FinishContext*>(c);
778 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
779 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
780 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
781 context->fMappedBufferManager)) {
782 result.reset();
783 }
784 (*context->fClientCallback)(context->fClientContext, std::move(result));
785 delete context;
786 };
787 GrFlushInfo flushInfo;
788 flushInfo.fFinishedContext = finishContext;
789 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -0500790
791 dContext->priv().flushSurface(this->asSurfaceProxy(),
792 SkSurface::BackendSurfaceAccess::kNoAccess,
793 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400794}
795
Adlai Hollerc95b5892020-08-11 12:02:22 -0400796void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(GrDirectContext* dContext,
797 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400798 sk_sp<SkColorSpace> dstColorSpace,
799 const SkIRect& srcRect,
800 SkISize dstSize,
801 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500802 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400803 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400804 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400805 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
806 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
807 SkASSERT(!dstSize.isZero());
808 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
809
Adlai Hollerc95b5892020-08-11 12:02:22 -0400810 if (!dContext) {
811 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400812 return;
813 }
814 auto rt = this->asRenderTargetProxy();
815 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400816 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400817 return;
818 }
819 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400820 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400821 return;
822 }
823 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400824 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400825 return;
826 }
827 int x = srcRect.fLeft;
828 int y = srcRect.fTop;
829 bool needsRescale = srcRect.size() != dstSize;
830 GrSurfaceProxyView srcView;
Brian Salomonbacbb922021-01-21 19:48:00 -0500831 auto info = SkImageInfo::Make(dstSize,
832 kRGBA_8888_SkColorType,
833 this->colorInfo().alphaType(),
834 dstColorSpace);
Brian Salomon63a0a752020-06-26 13:32:09 -0400835 if (needsRescale) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400836 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomonbacbb922021-01-21 19:48:00 -0500837 auto tempFC = this->rescale(info,
838 kTopLeft_GrSurfaceOrigin,
839 srcRect,
840 rescaleGamma,
841 rescaleMode);
842 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400843 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400844 return;
845 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500846 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
847 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400848 x = y = 0;
Brian Salomonbacbb922021-01-21 19:48:00 -0500849 srcView = tempFC->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400850 } else {
851 srcView = this->readSurfaceView();
852 if (!srcView.asTextureProxy()) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500853 srcView = GrSurfaceProxyView::Copy(fContext,
854 std::move(srcView),
855 GrMipmapped::kNo,
856 srcRect,
857 SkBackingFit::kApprox,
858 SkBudgeted::kYes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400859 if (!srcView) {
860 // If we can't get a texture copy of the contents then give up.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400861 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400862 return;
863 }
864 SkASSERT(srcView.asTextureProxy());
865 x = y = 0;
866 }
867 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomonbacbb922021-01-21 19:48:00 -0500868 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo(),
869 info.colorInfo());
Brian Salomon63a0a752020-06-26 13:32:09 -0400870 if (xform) {
871 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
Brian Salomonbacbb922021-01-21 19:48:00 -0500872 auto tempFC = GrSurfaceFillContext::Make(dContext,
873 info,
874 SkBackingFit::kApprox,
875 1,
876 GrMipmapped::kNo,
877 GrProtected::kNo,
878 kTopLeft_GrSurfaceOrigin);
879 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400880 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400881 return;
882 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500883 auto fp = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType());
884 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
885 tempFC->fillRectToRectWithFP(srcRectToDraw,
886 SkIRect::MakeSize(tempFC->dimensions()),
887 std::move(fp));
888 srcView = tempFC->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400889 SkASSERT(srcView.asTextureProxy());
890 x = y = 0;
891 }
892 }
893
Brian Salomonbacbb922021-01-21 19:48:00 -0500894 auto yInfo = SkImageInfo::MakeA8(dstSize);
895 auto yFC = GrSurfaceFillContext::MakeWithFallback(dContext, yInfo, SkBackingFit::kApprox);
896
897 auto uvInfo = yInfo.makeWH(yInfo.width()/2, yInfo.height()/2);
898 auto uFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
899 auto vFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
900
901 if (!yFC || !uFC || !vFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400902 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400903 return;
904 }
905
906 float baseM[20];
907 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
908
909 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
910
911 auto texMatrix = SkMatrix::Translate(x, y);
912
Brian Salomon63a0a752020-06-26 13:32:09 -0400913 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
914 PixelTransferResult yTransfer, uTransfer, vTransfer;
915
916 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
917 float yM[20];
918 std::fill_n(yM, 15, 0.f);
919 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500920
921 auto yFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
922 yFP = GrColorMatrixFragmentProcessor::Make(std::move(yFP),
923 yM,
924 /*unpremulInput=*/false,
925 /*clampRGBOutput=*/true,
926 /*premulOutput=*/false);
927 yFC->fillWithFP(std::move(yFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400928 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500929 yTransfer = yFC->transferPixels(GrColorType::kAlpha_8,
930 SkIRect::MakeSize(yFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400931 if (!yTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400932 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400933 return;
934 }
935 }
936
937 texMatrix.preScale(2.f, 2.f);
938 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
939 float uM[20];
940 std::fill_n(uM, 15, 0.f);
941 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500942
943 auto uFP = GrTextureEffect::Make(srcView,
944 this->colorInfo().alphaType(),
945 texMatrix,
946 GrSamplerState::Filter::kLinear);
947 uFP = GrColorMatrixFragmentProcessor::Make(std::move(uFP),
948 uM,
949 /*unpremulInput=*/false,
950 /*clampRGBOutput=*/true,
951 /*premulOutput=*/false);
952 uFC->fillWithFP(std::move(uFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400953 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500954 uTransfer = uFC->transferPixels(GrColorType::kAlpha_8,
955 SkIRect::MakeSize(uFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400956 if (!uTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400957 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400958 return;
959 }
960 }
961
962 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
963 float vM[20];
964 std::fill_n(vM, 15, 0.f);
965 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500966 auto vFP = GrTextureEffect::Make(std::move(srcView),
967 this->colorInfo().alphaType(),
968 texMatrix,
969 GrSamplerState::Filter::kLinear);
970 vFP = GrColorMatrixFragmentProcessor::Make(std::move(vFP),
971 vM,
972 /*unpremulInput=*/false,
973 /*clampRGBOutput=*/true,
974 /*premulOutput=*/false);
975 vFC->fillWithFP(std::move(vFP));
976
Brian Salomon63a0a752020-06-26 13:32:09 -0400977 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500978 vTransfer = vFC->transferPixels(GrColorType::kAlpha_8,
979 SkIRect::MakeSize(vFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400980 if (!vTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400981 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400982 return;
983 }
984 }
985
986 if (doSynchronousRead) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500987 GrPixmap yPmp = GrPixmap::Allocate(yInfo);
988 GrPixmap uPmp = GrPixmap::Allocate(uvInfo);
989 GrPixmap vPmp = GrPixmap::Allocate(uvInfo);
Brian Salomonbacbb922021-01-21 19:48:00 -0500990 if (!yFC->readPixels(dContext, yPmp, {0, 0}) ||
991 !uFC->readPixels(dContext, uPmp, {0, 0}) ||
992 !vFC->readPixels(dContext, vPmp, {0, 0})) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400993 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400994 return;
995 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400996 auto result = std::make_unique<AsyncReadResult>(dContext->priv().contextID());
Brian Salomonbe1084b2021-01-26 13:29:30 -0500997 result->addCpuPlane(yPmp.pixelStorage(), yPmp.rowBytes());
998 result->addCpuPlane(uPmp.pixelStorage(), uPmp.rowBytes());
999 result->addCpuPlane(vPmp.pixelStorage(), vPmp.rowBytes());
Adlai Hollerc95b5892020-08-11 12:02:22 -04001000 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -04001001 return;
1002 }
1003
1004 struct FinishContext {
1005 ReadPixelsCallback* fClientCallback;
1006 ReadPixelsContext fClientContext;
1007 GrClientMappedBufferManager* fMappedBufferManager;
1008 SkISize fSize;
1009 PixelTransferResult fYTransfer;
1010 PixelTransferResult fUTransfer;
1011 PixelTransferResult fVTransfer;
1012 };
1013 // Assumption is that the caller would like to flush. We could take a parameter or require an
1014 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1015 // callback to GrGpu until after the next flush that flushes our op list, though.
1016 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -04001017 callbackContext,
1018 dContext->priv().clientMappedBufferManager(),
Brian Salomon63a0a752020-06-26 13:32:09 -04001019 dstSize,
1020 std::move(yTransfer),
1021 std::move(uTransfer),
1022 std::move(vTransfer)};
1023 auto finishCallback = [](GrGpuFinishedContext c) {
1024 const auto* context = reinterpret_cast<const FinishContext*>(c);
1025 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
1026 auto manager = context->fMappedBufferManager;
1027 size_t rowBytes = SkToSizeT(context->fSize.width());
1028 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
1029 (*context->fClientCallback)(context->fClientContext, nullptr);
1030 delete context;
1031 return;
1032 }
1033 rowBytes /= 2;
1034 SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
1035 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
1036 (*context->fClientCallback)(context->fClientContext, nullptr);
1037 delete context;
1038 return;
1039 }
1040 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
1041 (*context->fClientCallback)(context->fClientContext, nullptr);
1042 delete context;
1043 return;
1044 }
1045 (*context->fClientCallback)(context->fClientContext, std::move(result));
1046 delete context;
1047 };
1048 GrFlushInfo flushInfo;
1049 flushInfo.fFinishedContext = finishContext;
1050 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -05001051 dContext->priv().flushSurface(this->asSurfaceProxy(),
1052 SkSurface::BackendSurfaceAccess::kNoAccess,
1053 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -04001054}
1055
Brian Salomon982127b2021-01-21 10:43:35 -05001056bool GrSurfaceContext::copy(sk_sp<GrSurfaceProxy> src, SkIRect srcRect, SkIPoint dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001057 ASSERT_SINGLE_OWNER
1058 RETURN_FALSE_IF_ABANDONED
1059 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -04001060 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -04001061
Brian Salomon947efe22019-07-16 15:36:11 -04001062 const GrCaps* caps = fContext->priv().caps();
1063
Greg Daniel46cfbc62019-06-07 11:43:30 -04001064 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -05001065 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -04001066
Stephen White3c0a50f2020-01-16 18:19:54 -05001067 if (this->asSurfaceProxy()->framebufferOnly()) {
1068 return false;
1069 }
1070
Brian Salomon982127b2021-01-21 10:43:35 -05001071 if (!caps->canCopySurface(this->asSurfaceProxy(), src.get(), srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -04001072 return false;
1073 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001074
Brian Salomon982127b2021-01-21 10:43:35 -05001075 return this->drawingManager()->newCopyRenderTask(std::move(src),
1076 srcRect,
1077 this->asSurfaceProxyRef(),
Brian Salomon0f9f8002021-01-22 16:30:50 -05001078 dstPoint,
1079 this->origin());
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001080}
Greg Daniel46cfbc62019-06-07 11:43:30 -04001081
Brian Salomonbacbb922021-01-21 19:48:00 -05001082std::unique_ptr<GrSurfaceFillContext> GrSurfaceContext::rescale(const GrImageInfo& info,
Brian Salomoneebe7352020-12-09 16:37:04 -05001083 GrSurfaceOrigin origin,
1084 SkIRect srcRect,
1085 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001086 RescaleMode rescaleMode) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001087 auto sfc = GrSurfaceFillContext::MakeWithFallback(fContext,
1088 info,
Brian Salomon1c86b632020-12-11 12:36:01 -05001089 SkBackingFit::kExact,
Brian Salomon1c86b632020-12-11 12:36:01 -05001090 1,
1091 GrMipmapped::kNo,
1092 this->asSurfaceProxy()->isProtected(),
1093 origin);
Brian Salomonbacbb922021-01-21 19:48:00 -05001094 if (!sfc || !this->rescaleInto(sfc.get(),
1095 SkIRect::MakeSize(sfc->dimensions()),
Brian Salomon1c86b632020-12-11 12:36:01 -05001096 srcRect,
1097 rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001098 rescaleMode)) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001099 return nullptr;
1100 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001101 return sfc;
Brian Salomon1c86b632020-12-11 12:36:01 -05001102}
1103
Brian Salomonbacbb922021-01-21 19:48:00 -05001104bool GrSurfaceContext::rescaleInto(GrSurfaceFillContext* dst,
Brian Salomon1c86b632020-12-11 12:36:01 -05001105 SkIRect dstRect,
1106 SkIRect srcRect,
1107 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001108 RescaleMode rescaleMode) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001109 SkASSERT(dst);
1110 if (!SkIRect::MakeSize(dst->dimensions()).contains((dstRect))) {
1111 return false;
1112 }
1113
Brian Salomone9ad9982019-07-22 16:17:41 -04001114 auto rtProxy = this->asRenderTargetProxy();
1115 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001116 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001117 }
1118
Stephen White3c0a50f2020-01-16 18:19:54 -05001119 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001120 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001121 }
1122
Greg Daniel40903af2020-01-30 14:55:05 -05001123 GrSurfaceProxyView texView = this->readSurfaceView();
Greg Daniel40903af2020-01-30 14:55:05 -05001124 if (!texView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -04001125 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect,
Brian Salomonc5243782020-04-02 12:50:34 -04001126 SkBackingFit::kApprox, SkBudgeted::kNo);
1127 if (!texView) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001128 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001129 }
Greg Daniel40903af2020-01-30 14:55:05 -05001130 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001131 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001132 }
1133
Brian Salomon1c86b632020-12-11 12:36:01 -05001134 SkISize finalSize = dstRect.size();
1135
Brian Salomonbf6b9792019-08-21 09:38:10 -04001136 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
1137 // 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 -05001138 std::unique_ptr<GrSurfaceFillContext> tempA;
1139 std::unique_ptr<GrSurfaceFillContext> tempB;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001140
Brian Salomone9ad9982019-07-22 16:17:41 -04001141 // Assume we should ignore the rescale linear request if the surface has no color space since
1142 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon63a0a752020-06-26 13:32:09 -04001143 if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() &&
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001144 !this->colorInfo().colorSpace()->gammaIsLinear()) {
1145 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomone9ad9982019-07-22 16:17:41 -04001146 // We'll fall back to kRGBA_8888 if half float not supported.
Brian Salomonbacbb922021-01-21 19:48:00 -05001147 GrImageInfo ii(GrColorType::kRGBA_F16,
1148 dst->colorInfo().alphaType(),
1149 std::move(cs),
1150 srcRect.size());
1151 auto linearRTC = GrSurfaceFillContext::MakeWithFallback(fContext,
1152 std::move(ii),
1153 SkBackingFit::kApprox,
1154 1,
1155 GrMipmapped::kNo,
1156 GrProtected::kNo,
1157 dst->origin());
Brian Salomone9ad9982019-07-22 16:17:41 -04001158 if (!linearRTC) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001159 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001160 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001161 auto fp = GrTextureEffect::Make(std::move(texView),
1162 this->colorInfo().alphaType(),
1163 SkMatrix::Translate(srcRect.topLeft()),
1164 GrSamplerState::Filter::kNearest,
1165 GrSamplerState::MipmapMode::kNone);
1166 fp = GrColorSpaceXformEffect::Make(std::move(fp),
1167 this->colorInfo(),
1168 linearRTC->colorInfo());
1169 linearRTC->fillWithFP(std::move(fp));
Greg Daniel40903af2020-01-30 14:55:05 -05001170 texView = linearRTC->readSurfaceView();
1171 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -04001172 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001173 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001174 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001175
Brian Salomon1c86b632020-12-11 12:36:01 -05001176 while (srcRect.size() != finalSize) {
1177 SkISize nextDims = finalSize;
Mike Reed1efa14d2021-01-02 21:44:59 -05001178 if (rescaleMode != RescaleMode::kNearest) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001179 if (srcRect.width() > finalSize.width()) {
1180 nextDims.fWidth = std::max((srcRect.width() + 1)/2, finalSize.width());
1181 } else if (srcRect.width() < finalSize.width()) {
1182 nextDims.fWidth = std::min(srcRect.width()*2, finalSize.width());
Brian Salomon59f31b12020-06-04 17:27:15 -04001183 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001184 if (srcRect.height() > finalSize.height()) {
1185 nextDims.fHeight = std::max((srcRect.height() + 1)/2, finalSize.height());
1186 } else if (srcRect.height() < finalSize.height()) {
1187 nextDims.fHeight = std::min(srcRect.height()*2, finalSize.height());
Brian Salomon59f31b12020-06-04 17:27:15 -04001188 }
1189 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001190 auto input = tempA ? tempA.get() : this;
Brian Salomone9ad9982019-07-22 16:17:41 -04001191 sk_sp<GrColorSpaceXform> xform;
Brian Salomonbacbb922021-01-21 19:48:00 -05001192 GrSurfaceFillContext* stepDst;
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001193 SkIRect stepDstRect;
Brian Salomon1c86b632020-12-11 12:36:01 -05001194 if (nextDims == finalSize) {
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001195 stepDst = dst;
1196 stepDstRect = dstRect;
Brian Salomonbacbb922021-01-21 19:48:00 -05001197 xform = GrColorSpaceXform::Make(input->colorInfo(), dst->colorInfo());
Brian Salomon1c86b632020-12-11 12:36:01 -05001198 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -05001199 GrImageInfo nextInfo(input->colorInfo(), nextDims);
1200 tempB = GrSurfaceFillContext::MakeWithFallback(fContext,
1201 nextInfo,
1202 SkBackingFit::kApprox);
Brian Salomon1c86b632020-12-11 12:36:01 -05001203 if (!tempB) {
1204 return false;
1205 }
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001206 stepDst = tempB.get();
1207 stepDstRect = SkIRect::MakeSize(tempB->dimensions());
Brian Salomone9ad9982019-07-22 16:17:41 -04001208 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001209 std::unique_ptr<GrFragmentProcessor> fp;
Mike Reed1efa14d2021-01-02 21:44:59 -05001210 if (rescaleMode == RescaleMode::kRepeatedCubic) {
Brian Salomon59f31b12020-06-04 17:27:15 -04001211 auto dir = GrBicubicEffect::Direction::kXY;
1212 if (nextDims.width() == srcRect.width()) {
1213 dir = GrBicubicEffect::Direction::kY;
1214 } else if (nextDims.height() == srcRect.height()) {
1215 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -04001216 }
Brian Salomon1af72d12020-06-25 10:47:26 -04001217 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
Mike Reed3867c702020-09-01 13:28:10 -04001218 static constexpr auto kKernel = GrBicubicEffect::gCatmullRom;
Brian Salomon1c86b632020-12-11 12:36:01 -05001219 fp = GrBicubicEffect::MakeSubset(std::move(texView),
1220 input->colorInfo().alphaType(),
Brian Salomonbacbb922021-01-21 19:48:00 -05001221 SkMatrix::I(),
Brian Salomon1c86b632020-12-11 12:36:01 -05001222 kWM,
1223 kWM,
1224 SkRect::Make(srcRect),
1225 kKernel,
1226 dir,
1227 *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -04001228 } else {
Mike Reed1efa14d2021-01-02 21:44:59 -05001229 auto filter = rescaleMode == RescaleMode::kNearest ? GrSamplerState::Filter::kNearest
1230 : GrSamplerState::Filter::kLinear;
Brian Salomonbacbb922021-01-21 19:48:00 -05001231 auto srcRectF = SkRect::Make(srcRect);
1232 fp = GrTextureEffect::MakeSubset(std::move(texView),
1233 this->colorInfo().alphaType(),
1234 SkMatrix::I(),
1235 {filter, GrSamplerState::MipmapMode::kNone},
1236 srcRectF,
1237 srcRectF,
1238 *this->caps());
Brian Salomone9ad9982019-07-22 16:17:41 -04001239 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001240 if (xform) {
1241 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
1242 }
1243 stepDst->fillRectToRectWithFP(srcRect, stepDstRect, std::move(fp));
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001244 texView = stepDst->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -04001245 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001246 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomone9ad9982019-07-22 16:17:41 -04001247 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001248 return true;
Brian Salomone9ad9982019-07-22 16:17:41 -04001249}
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001250
1251GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
1252 const SkIRect& rect) {
1253 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1254 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001255 auto direct = fContext->asDirectContext();
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001256 if (!direct) {
1257 return {};
1258 }
1259 auto rtProxy = this->asRenderTargetProxy();
1260 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1261 return {};
1262 }
1263
1264 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001265 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
1266 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001267 // Fail if read color type does not have all of dstCT's color channels and those missing color
1268 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001269 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1270 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
1271 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1272 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001273 return {};
1274 }
1275
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001276 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001277 !supportedRead.fOffsetAlignmentForTransferBuffer) {
1278 return {};
1279 }
1280
1281 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
1282 size_t size = rowBytes * rect.height();
Greg Daniel2e967df2021-02-08 10:38:31 -05001283 // By using kStream_GrAccessPattern here, we are not able to cache and reuse the buffer for
1284 // multiple reads. Switching to kDynamic_GrAccessPattern would allow for this, however doing
1285 // so causes a crash in a chromium test. See skbug.com/11297
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001286 auto buffer = direct->priv().resourceProvider()->createBuffer(
Greg Daniel393debc2021-02-06 03:37:20 +00001287 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001288 if (!buffer) {
1289 return {};
1290 }
1291 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -05001292 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001293 if (flip) {
1294 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1295 this->height() - rect.fTop);
1296 }
Greg Danielbbfec9d2019-08-20 10:56:51 -04001297 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001298 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -04001299 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001300 PixelTransferResult result;
1301 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001302 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -04001303 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001304 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
1305 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001306 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
1307 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001308 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
1309 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -04001310 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001311 };
1312 }
1313 return result;
1314}
Greg Daniel46e366a2019-12-16 14:38:36 -05001315
1316#ifdef SK_DEBUG
1317void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001318 SkASSERT(fReadView.proxy());
1319 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -04001320 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
1321 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
1322 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
1323 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001324 this->onValidate();
1325}
1326#endif