blob: 13c15c33d72a1f4839c84c4cf95aa49c43cfffb0 [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 Salomondd4087d2020-12-23 20:36:44 -0500175 dst = dst.clip(this->dimensions(), &pt);
176 if (!dst.hasPixels()) {
Brian Salomon1d435302019-07-01 13:05:28 -0400177 return false;
178 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500179 if (!alpha_types_compatible(this->colorInfo().alphaType(), dst.alphaType())) {
Brian Salomon1d435302019-07-01 13:05:28 -0400180 return false;
181 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500182 // We allow unknown alpha types but only if both src and dst are unknown. Otherwise, it's too
183 // weird to reason about what should be expected.
Greg Daniel6eb8c242019-06-05 10:22:24 -0400184
Brian Salomon982127b2021-01-21 10:43:35 -0500185 sk_sp<GrSurfaceProxy> srcProxy = this->asSurfaceProxyRef();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400186
Stephen White3c0a50f2020-01-16 18:19:54 -0500187 if (srcProxy->framebufferOnly()) {
188 return false;
189 }
190
Greg Daniel6eb8c242019-06-05 10:22:24 -0400191 // MDB TODO: delay this instantiation until later in the method
Adlai Hollerc95b5892020-08-11 12:02:22 -0400192 if (!srcProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400193 return false;
194 }
195
196 GrSurface* srcSurface = srcProxy->peekSurface();
197
Brian Salomondd4087d2020-12-23 20:36:44 -0500198 SkColorSpaceXformSteps::Flags flags =
199 SkColorSpaceXformSteps{this->colorInfo(), dst.info()}.flags;
Mike Klein7321e6a2019-12-03 11:08:40 -0600200 bool unpremul = flags.unpremul,
201 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
202 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400203
Adlai Hollerc95b5892020-08-11 12:02:22 -0400204 const GrCaps* caps = dContext->priv().caps();
Robert Phillips07f0e412020-01-17 15:20:00 -0500205 bool srcIsCompressed = caps->isFormatCompressed(srcSurface->backendFormat());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400206 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
207 // care so much about getImageData performance. However, in order to ensure putImageData/
208 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
209 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
210 // fContext->vaildaPMUPMConversionExists()).
Greg Daniel0258c902019-08-01 13:08:33 -0400211 GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
212 GrRenderable::kYes);
Greg Danielc71c7962020-01-14 16:44:18 -0500213 GrColorType srcColorType = this->colorInfo().colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400214 bool canvas2DFastPath = unpremul && !needColorConversion &&
Brian Salomondd4087d2020-12-23 20:36:44 -0500215 (GrColorType::kRGBA_8888 == dst.colorType() ||
216 GrColorType::kBGRA_8888 == dst.colorType()) &&
Brian Salomon1d435302019-07-01 13:05:28 -0400217 SkToBool(srcProxy->asTextureProxy()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500218 (srcColorType == GrColorType::kRGBA_8888 ||
219 srcColorType == GrColorType::kBGRA_8888) &&
Greg Daniel0258c902019-08-01 13:08:33 -0400220 defaultRGBAFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400221 dContext->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400222
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400223 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400224 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400225 return false;
226 }
227
Brian Salomondc0710f2019-07-01 14:59:32 -0400228 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400229 std::unique_ptr<GrSurfaceContext> tempCtx;
230 if (this->asTextureProxy()) {
231 GrColorType colorType = (canvas2DFastPath || srcIsCompressed)
232 ? GrColorType::kRGBA_8888
233 : this->colorInfo().colorType();
Brian Salomondd4087d2020-12-23 20:36:44 -0500234 SkAlphaType alphaType = canvas2DFastPath ? dst.alphaType()
Brian Salomon590f5672020-12-16 11:44:47 -0500235 : this->colorInfo().alphaType();
236 GrImageInfo tempInfo(colorType,
237 alphaType,
238 this->colorInfo().refColorSpace(),
Brian Salomondd4087d2020-12-23 20:36:44 -0500239 dst.dimensions());
Brian Salomon590f5672020-12-16 11:44:47 -0500240 auto sfc = GrSurfaceFillContext::Make(dContext, tempInfo, SkBackingFit::kApprox);
241 if (!sfc) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400242 return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400243 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400244
245 std::unique_ptr<GrFragmentProcessor> fp;
246 if (canvas2DFastPath) {
247 fp = dContext->priv().createPMToUPMEffect(GrTextureEffect::Make(
248 this->readSurfaceView(), this->colorInfo().alphaType()));
Brian Salomondd4087d2020-12-23 20:36:44 -0500249 if (dst.colorType() == GrColorType::kBGRA_8888) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400250 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
Brian Salomondd4087d2020-12-23 20:36:44 -0500251 dst = GrPixmap(dst.info().makeColorType(GrColorType::kRGBA_8888),
252 dst.addr(),
253 dst.rowBytes());
Brian Salomon72c7b982020-10-06 10:07:38 -0400254 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400255 } else {
256 fp = GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType());
257 }
258 if (!fp) {
259 return false;
260 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500261 sfc->fillRectToRectWithFP(SkIRect::MakePtSize(pt, dst.dimensions()),
262 SkIRect::MakeSize(dst.dimensions()),
Brian Salomon590f5672020-12-16 11:44:47 -0500263 std::move(fp));
Brian Salomon72c7b982020-10-06 10:07:38 -0400264 pt = {0, 0};
Brian Salomon590f5672020-12-16 11:44:47 -0500265 tempCtx = std::move(sfc);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400266 } else {
Brian Salomon72c7b982020-10-06 10:07:38 -0400267 auto restrictions = this->caps()->getDstCopyRestrictions(this->asRenderTargetProxy(),
268 this->colorInfo().colorType());
269 sk_sp<GrSurfaceProxy> copy;
270 static constexpr auto kFit = SkBackingFit::kExact;
271 static constexpr auto kBudgeted = SkBudgeted::kYes;
272 static constexpr auto kMipMapped = GrMipMapped::kNo;
273 if (restrictions.fMustCopyWholeSrc) {
Brian Salomon982127b2021-01-21 10:43:35 -0500274 copy = GrSurfaceProxy::Copy(fContext,
275 std::move(srcProxy),
276 this->origin(),
277 kMipMapped,
278 kFit,
Brian Salomon72c7b982020-10-06 10:07:38 -0400279 kBudgeted);
280 } else {
Brian Salomondd4087d2020-12-23 20:36:44 -0500281 auto srcRect = SkIRect::MakePtSize(pt, dst.dimensions());
Brian Salomon982127b2021-01-21 10:43:35 -0500282 copy = GrSurfaceProxy::Copy(fContext,
283 std::move(srcProxy),
284 this->origin(),
285 kMipMapped,
286 srcRect,
287 kFit,
288 kBudgeted,
289 restrictions.fRectsMustMatch);
Brian Salomon72c7b982020-10-06 10:07:38 -0400290 pt = {0, 0};
291 }
292 if (!copy) {
293 return false;
294 }
295 GrSurfaceProxyView view{std::move(copy), this->origin(), this->readSwizzle()};
Brian Salomon14f99fc2020-12-07 12:19:47 -0500296 tempCtx = GrSurfaceContext::Make(dContext, std::move(view), this->colorInfo());
Brian Salomon72c7b982020-10-06 10:07:38 -0400297 SkASSERT(tempCtx);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400298 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500299 return tempCtx->readPixels(dContext, dst, pt);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400300 }
301
Greg Danielb8d84f82020-02-13 14:25:00 -0500302 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400303
Brian Salomon1d435302019-07-01 13:05:28 -0400304 auto supportedRead = caps->supportedReadPixelsColorType(
Brian Salomondd4087d2020-12-23 20:36:44 -0500305 this->colorInfo().colorType(), srcProxy->backendFormat(), dst.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400306
Brian Salomondd4087d2020-12-23 20:36:44 -0500307 bool makeTight =
308 !caps->readPixelsRowBytesSupport() && dst.rowBytes() != dst.info().minRowBytes();
Brian Salomon1047a492019-07-02 12:25:21 -0400309
310 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomondd4087d2020-12-23 20:36:44 -0500311 (dst.colorType() != supportedRead.fColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400312
Brian Salomonf30b1c12019-06-20 12:25:02 -0400313 std::unique_ptr<char[]> tmpPixels;
Brian Salomondd4087d2020-12-23 20:36:44 -0500314 GrPixmap tmp;
315 void* readDst = dst.addr();
316 size_t readRB = dst.rowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400317 if (convert) {
Brian Salomondd4087d2020-12-23 20:36:44 -0500318 GrImageInfo tmpInfo(supportedRead.fColorType,
319 this->colorInfo().alphaType(),
320 this->colorInfo().refColorSpace(),
321 dst.dimensions());
Brian Salomon1d435302019-07-01 13:05:28 -0400322 size_t tmpRB = tmpInfo.minRowBytes();
323 size_t size = tmpRB * tmpInfo.height();
324 // Chrome MSAN bots require the data to be initialized (hence the ()).
John Stilesfbd050b2020-08-03 13:21:46 -0400325 tmpPixels = std::make_unique<char[]>(size);
Brian Salomondd4087d2020-12-23 20:36:44 -0500326 tmp = {tmpInfo, tmpPixels.get(), tmpRB};
Brian Salomonf30b1c12019-06-20 12:25:02 -0400327
Brian Salomonf30b1c12019-06-20 12:25:02 -0400328 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400329 readRB = tmpRB;
Brian Salomondd4087d2020-12-23 20:36:44 -0500330 pt.fY = flip ? srcSurface->height() - pt.fY - dst.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400331 }
332
Brian Salomon982127b2021-01-21 10:43:35 -0500333 dContext->priv().flushSurface(srcProxy.get());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400334 dContext->submit();
Brian Salomondd4087d2020-12-23 20:36:44 -0500335 if (!dContext->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dst.width(), dst.height(),
336 this->colorInfo().colorType(),
Adlai Hollerc95b5892020-08-11 12:02:22 -0400337 supportedRead.fColorType, readDst, readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400338 return false;
339 }
340
Brian Salomondd4087d2020-12-23 20:36:44 -0500341 if (tmp.hasPixels()) {
342 return GrConvertPixels(dst, tmp, flip);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400343 }
344 return true;
345}
Robert Phillips0d075de2019-03-04 11:08:13 -0500346
Brian Salomondd4087d2020-12-23 20:36:44 -0500347bool GrSurfaceContext::writePixels(GrDirectContext* dContext, GrPixmap src, SkIPoint pt) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400348 ASSERT_SINGLE_OWNER
349 RETURN_FALSE_IF_ABANDONED
350 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400351 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400352
Adlai Hollerc95b5892020-08-11 12:02:22 -0400353 if (!dContext) {
Brian Salomonc320b152018-02-20 14:05:36 -0500354 return false;
355 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500356
Brian Salomon1d435302019-07-01 13:05:28 -0400357 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500358 return false;
359 }
360
Brian Salomondd4087d2020-12-23 20:36:44 -0500361 src = src.clip(this->dimensions(), &pt);
362 if (!src.hasPixels()) {
Brian Salomon1d435302019-07-01 13:05:28 -0400363 return false;
364 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500365 if (!alpha_types_compatible(src.alphaType(), this->colorInfo().alphaType())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400366 return false;
367 }
368
369 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
Stephen White3c0a50f2020-01-16 18:19:54 -0500370
371 if (dstProxy->framebufferOnly()) {
372 return false;
373 }
374
Adlai Hollerc95b5892020-08-11 12:02:22 -0400375 if (!dstProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400376 return false;
377 }
378
379 GrSurface* dstSurface = dstProxy->peekSurface();
380
Brian Salomondd4087d2020-12-23 20:36:44 -0500381 SkColorSpaceXformSteps::Flags flags =
382 SkColorSpaceXformSteps{src.info(), this->colorInfo()}.flags;
Mike Klein7321e6a2019-12-03 11:08:40 -0600383 bool unpremul = flags.unpremul,
384 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
385 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400386
Adlai Hollerc95b5892020-08-11 12:02:22 -0400387 const GrCaps* caps = dContext->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400388
389 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
390 GrRenderable::kNo);
391
Greg Danielc71c7962020-01-14 16:44:18 -0500392 GrColorType dstColorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400393 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
394 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400395 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
Brian Salomondd4087d2020-12-23 20:36:44 -0500396 (src.colorType() == GrColorType::kRGBA_8888 ||
397 src.colorType() == GrColorType::kBGRA_8888) &&
Brian Salomon590f5672020-12-16 11:44:47 -0500398 this->asFillContext() &&
Greg Danielc71c7962020-01-14 16:44:18 -0500399 (dstColorType == GrColorType::kRGBA_8888 ||
400 dstColorType == GrColorType::kBGRA_8888) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400401 rgbaDefaultFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400402 dContext->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400403
404 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500405 GrColorInfo tempColorInfo;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400406 GrBackendFormat format;
Greg Danielbfa19c42019-12-19 16:41:40 -0500407 GrSwizzle tempReadSwizzle;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400408 if (canvas2DFastPath) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500409 tempColorInfo = {GrColorType::kRGBA_8888,
410 kUnpremul_SkAlphaType,
411 this->colorInfo().refColorSpace()};
Greg Daniel7bfc9132019-08-14 14:23:53 -0400412 format = rgbaDefaultFormat;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400413 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500414 tempColorInfo = this->colorInfo();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400415 format = dstProxy->backendFormat().makeTexture2D();
416 if (!format.isValid()) {
417 return false;
418 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500419 tempReadSwizzle = this->readSwizzle();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400420 }
421
Greg Daniel2e52ad12019-06-13 10:04:16 -0400422 // It is more efficient for us to write pixels into a top left origin so we prefer that.
423 // However, if the final proxy isn't a render target then we must use a copy to move the
424 // data into it which requires the origins to match. If the final proxy is a render target
425 // we can use a draw instead which doesn't have this origin restriction. Thus for render
426 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400427 GrSurfaceOrigin tempOrigin =
Brian Salomon590f5672020-12-16 11:44:47 -0500428 this->asFillContext() ? kTopLeft_GrSurfaceOrigin : this->origin();
Adlai Hollerc95b5892020-08-11 12:02:22 -0400429 auto tempProxy = dContext->priv().proxyProvider()->createProxy(
Brian Salomondd4087d2020-12-23 20:36:44 -0500430 format, src.dimensions(), GrRenderable::kNo, 1, GrMipmapped::kNo,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400431 SkBackingFit::kApprox, SkBudgeted::kYes, GrProtected::kNo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400432 if (!tempProxy) {
433 return false;
434 }
Greg Daniel3912a4b2020-01-14 09:56:04 -0500435 GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500436 GrSurfaceContext tempCtx(dContext, tempView, tempColorInfo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400437
438 // In the fast path we always write the srcData to the temp context as though it were RGBA.
439 // When the data is really BGRA the write will cause the R and B channels to be swapped in
440 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
441 // dst.
Brian Salomondd4087d2020-12-23 20:36:44 -0500442 GrColorType origSrcColorType = src.colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400443 if (canvas2DFastPath) {
Brian Salomondd4087d2020-12-23 20:36:44 -0500444 src = {src.info().makeColorType(GrColorType::kRGBA_8888), src.addr(), src.rowBytes()};
Brian Salomon1d435302019-07-01 13:05:28 -0400445 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500446 if (!tempCtx.writePixels(dContext, src, {0, 0})) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400447 return false;
448 }
449
Brian Salomon590f5672020-12-16 11:44:47 -0500450 if (this->asFillContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400451 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400452 if (canvas2DFastPath) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400453 fp = dContext->priv().createUPMToPMEffect(
Brian Salomon14f99fc2020-12-07 12:19:47 -0500454 GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType()));
Brian Salomon1d435302019-07-01 13:05:28 -0400455 // Important: check the original src color type here!
Brian Salomondd4087d2020-12-23 20:36:44 -0500456 if (origSrcColorType == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400457 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
458 }
459 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500460 fp = GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400461 }
462 if (!fp) {
463 return false;
464 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500465 this->asFillContext()->fillRectToRectWithFP(SkIRect::MakeSize(src.dimensions()),
466 SkIRect::MakePtSize(pt, src.dimensions()),
467 std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400468 } else {
Brian Salomondd4087d2020-12-23 20:36:44 -0500469 SkIRect srcRect = SkIRect::MakeSize(src.dimensions());
Brian Salomon1d435302019-07-01 13:05:28 -0400470 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Brian Salomon982127b2021-01-21 10:43:35 -0500471 if (!this->copy(std::move(tempProxy), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400472 return false;
473 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400474 }
475 return true;
476 }
477
Brian Salomon1d435302019-07-01 13:05:28 -0400478 GrColorType allowedColorType =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400479 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400480 dstProxy->backendFormat(),
Brian Salomondd4087d2020-12-23 20:36:44 -0500481 src.colorType()).fColorType;
Greg Danielb8d84f82020-02-13 14:25:00 -0500482 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomondd4087d2020-12-23 20:36:44 -0500483 bool makeTight = !caps->writePixelsRowBytesSupport() &&
484 src.rowBytes() != src.info().minRowBytes();
Brian Salomon1047a492019-07-02 12:25:21 -0400485 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomondd4087d2020-12-23 20:36:44 -0500486 (src.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400487
Brian Salomonf30b1c12019-06-20 12:25:02 -0400488 std::unique_ptr<char[]> tmpPixels;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400489 if (convert) {
Brian Salomondd4087d2020-12-23 20:36:44 -0500490 GrImageInfo tmpInfo(allowedColorType,
491 this->colorInfo().alphaType(),
492 this->colorInfo().refColorSpace(),
493 src.dimensions());
Brian Salomon1d435302019-07-01 13:05:28 -0400494 auto tmpRB = tmpInfo.minRowBytes();
495 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
Brian Salomondd4087d2020-12-23 20:36:44 -0500496 GrPixmap tmp(tmpInfo, tmpPixels.get(), tmpRB);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400497
Brian Salomondd4087d2020-12-23 20:36:44 -0500498 SkAssertResult(GrConvertPixels(tmp, src, flip));
Brian Salomonf30b1c12019-06-20 12:25:02 -0400499
Brian Salomondd4087d2020-12-23 20:36:44 -0500500 src = tmp;
Brian Salomon1d435302019-07-01 13:05:28 -0400501 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400502 }
503
504 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
505 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
506 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
507 // destination proxy)
508 // TODO: should this policy decision just be moved into the drawing manager?
Adlai Hollerc95b5892020-08-11 12:02:22 -0400509 dContext->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400510
Brian Salomondd4087d2020-12-23 20:36:44 -0500511 return dContext->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, src.width(),
512 src.height(), this->colorInfo().colorType(),
513 src.colorType(), src.addr(), src.rowBytes());
Brian Osman45580d32016-11-23 09:37:01 -0500514}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400515
Adlai Hollerc95b5892020-08-11 12:02:22 -0400516void GrSurfaceContext::asyncRescaleAndReadPixels(GrDirectContext* dContext,
517 const SkImageInfo& info,
Brian Salomon63a0a752020-06-26 13:32:09 -0400518 const SkIRect& srcRect,
519 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500520 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400521 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400522 ReadPixelsContext callbackContext) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400523 if (!dContext) {
524 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400525 return;
526 }
527 auto rt = this->asRenderTargetProxy();
528 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400529 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400530 return;
531 }
532 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400533 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400534 return;
535 }
536 auto dstCT = SkColorTypeToGrColorType(info.colorType());
537 if (dstCT == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400538 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400539 return;
540 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500541 bool needsRescale = srcRect.size() != info.dimensions();
Brian Salomon63a0a752020-06-26 13:32:09 -0400542 auto colorTypeOfFinalContext = this->colorInfo().colorType();
543 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
544 if (needsRescale) {
545 colorTypeOfFinalContext = dstCT;
546 backendFormatOfFinalContext =
547 this->caps()->getDefaultBackendFormat(dstCT, GrRenderable::kYes);
548 }
549 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonbacbb922021-01-21 19:48:00 -0500550 backendFormatOfFinalContext,
551 dstCT);
Brian Salomon63a0a752020-06-26 13:32:09 -0400552 // Fail if we can't read from the source surface's color type.
553 if (readInfo.fColorType == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400554 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400555 return;
556 }
557 // Fail if read color type does not have all of dstCT's color channels and those missing color
558 // channels are in the src.
559 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
560 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
561 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
562 if ((~legalReadChannels & dstChannels) & srcChannels) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400563 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400564 return;
565 }
566
Brian Salomonbacbb922021-01-21 19:48:00 -0500567 std::unique_ptr<GrSurfaceFillContext> tempFC;
Brian Salomon63a0a752020-06-26 13:32:09 -0400568 int x = srcRect.fLeft;
569 int y = srcRect.fTop;
570 if (needsRescale) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500571 tempFC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma, rescaleMode);
572 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400573 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400574 return;
575 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500576 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
577 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400578 x = y = 0;
579 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -0500580 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo(),
581 info.colorInfo());
Brian Salomon63a0a752020-06-26 13:32:09 -0400582 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
583 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
584 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomonbacbb922021-01-21 19:48:00 -0500585 SkIRect srcRectToDraw = srcRect;
Brian Salomon63a0a752020-06-26 13:32:09 -0400586 // If the src is not texturable first try to make a copy to a texture.
587 if (!texProxyView.asTextureProxy()) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500588 texProxyView = GrSurfaceProxyView::Copy(fContext,
589 texProxyView,
590 GrMipmapped::kNo,
591 srcRect,
592 SkBackingFit::kApprox,
593 SkBudgeted::kNo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400594 if (!texProxyView) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400595 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400596 return;
597 }
598 SkASSERT(texProxyView.asTextureProxy());
Brian Salomonbacbb922021-01-21 19:48:00 -0500599 srcRectToDraw = SkIRect::MakeSize(srcRect.size());
Brian Salomon63a0a752020-06-26 13:32:09 -0400600 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500601 auto tempInfo = GrImageInfo(info).makeColorType(this->colorInfo().colorType());
602 tempFC = GrSurfaceFillContext::Make(dContext, tempInfo, SkBackingFit::kApprox);
603 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400604 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400605 return;
606 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500607 auto fp = GrTextureEffect::Make(std::move(texProxyView), this->colorInfo().alphaType());
608 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
609 tempFC->fillRectToRectWithFP(srcRectToDraw,
610 SkIRect::MakeSize(tempFC->dimensions()),
611 std::move(fp));
Brian Salomon63a0a752020-06-26 13:32:09 -0400612 x = y = 0;
613 }
614 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500615 auto srcCtx = tempFC ? tempFC.get() : this;
616 return srcCtx->asyncReadPixels(dContext,
617 SkIRect::MakePtSize({x, y}, info.dimensions()),
618 info.colorType(),
619 callback,
620 callbackContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400621}
622
623class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult {
624public:
625 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
626 ~AsyncReadResult() override {
627 for (int i = 0; i < fPlanes.count(); ++i) {
628 if (!fPlanes[i].fMappedBuffer) {
629 delete[] static_cast<const char*>(fPlanes[i].fData);
630 } else {
631 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
632 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
633 }
634 }
635 }
636
637 int count() const override { return fPlanes.count(); }
638 const void* data(int i) const override { return fPlanes[i].fData; }
639 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
640
641 bool addTransferResult(const PixelTransferResult& result,
642 SkISize dimensions,
643 size_t rowBytes,
644 GrClientMappedBufferManager* manager) {
645 SkASSERT(!result.fTransferBuffer->isMapped());
646 const void* mappedData = result.fTransferBuffer->map();
647 if (!mappedData) {
648 return false;
649 }
650 if (result.fPixelConverter) {
651 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
652 result.fPixelConverter(convertedData.get(), mappedData);
653 this->addCpuPlane(std::move(convertedData), rowBytes);
654 result.fTransferBuffer->unmap();
655 } else {
656 manager->insert(result.fTransferBuffer);
657 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
658 }
659 return true;
660 }
661
662 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
663 SkASSERT(data);
664 SkASSERT(rowBytes > 0);
665 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
666 }
667
668private:
669 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
670 SkASSERT(data);
671 SkASSERT(rowBytes > 0);
672 SkASSERT(mappedBuffer);
673 SkASSERT(mappedBuffer->isMapped());
674 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
675 }
676
677 struct Plane {
678 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
679 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
680 const void* fData;
681 size_t fRowBytes;
682 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
683 sk_sp<GrGpuBuffer> fMappedBuffer;
684 };
685 SkSTArray<3, Plane> fPlanes;
686 uint32_t fInboxID;
687};
688
Adlai Hollerc95b5892020-08-11 12:02:22 -0400689void GrSurfaceContext::asyncReadPixels(GrDirectContext* dContext,
690 const SkIRect& rect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400691 SkColorType colorType,
692 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400693 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400694 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
695 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
696
Adlai Hollerc95b5892020-08-11 12:02:22 -0400697 if (!dContext || this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
698 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400699 return;
700 }
701
Adlai Hollerc95b5892020-08-11 12:02:22 -0400702 auto mappedBufferManager = dContext->priv().clientMappedBufferManager();
Brian Salomon63a0a752020-06-26 13:32:09 -0400703
704 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
705
706 if (!transferResult.fTransferBuffer) {
707 auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
708 this->colorInfo().refColorSpace());
709 auto result = std::make_unique<AsyncReadResult>(0);
710 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
711 SkPixmap pm(ii, data.get(), ii.minRowBytes());
712 result->addCpuPlane(std::move(data), pm.rowBytes());
713
Adlai Hollerc95b5892020-08-11 12:02:22 -0400714 SkIPoint pt{rect.fLeft, rect.fTop};
Brian Salomondd4087d2020-12-23 20:36:44 -0500715 if (!this->readPixels(dContext, pm, pt)) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400716 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400717 return;
718 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400719 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400720 return;
721 }
722
723 struct FinishContext {
724 ReadPixelsCallback* fClientCallback;
725 ReadPixelsContext fClientContext;
726 SkISize fSize;
727 SkColorType fColorType;
728 GrClientMappedBufferManager* fMappedBufferManager;
729 PixelTransferResult fTransferResult;
730 };
731 // Assumption is that the caller would like to flush. We could take a parameter or require an
732 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
733 // callback to GrGpu until after the next flush that flushes our op list, though.
734 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400735 callbackContext,
Brian Salomon63a0a752020-06-26 13:32:09 -0400736 rect.size(),
737 colorType,
738 mappedBufferManager,
739 std::move(transferResult)};
740 auto finishCallback = [](GrGpuFinishedContext c) {
741 const auto* context = reinterpret_cast<const FinishContext*>(c);
742 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
743 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
744 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
745 context->fMappedBufferManager)) {
746 result.reset();
747 }
748 (*context->fClientCallback)(context->fClientContext, std::move(result));
749 delete context;
750 };
751 GrFlushInfo flushInfo;
752 flushInfo.fFinishedContext = finishContext;
753 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -0500754
755 dContext->priv().flushSurface(this->asSurfaceProxy(),
756 SkSurface::BackendSurfaceAccess::kNoAccess,
757 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400758}
759
Adlai Hollerc95b5892020-08-11 12:02:22 -0400760void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(GrDirectContext* dContext,
761 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400762 sk_sp<SkColorSpace> dstColorSpace,
763 const SkIRect& srcRect,
764 SkISize dstSize,
765 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500766 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400767 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400768 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400769 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
770 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
771 SkASSERT(!dstSize.isZero());
772 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
773
Adlai Hollerc95b5892020-08-11 12:02:22 -0400774 if (!dContext) {
775 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400776 return;
777 }
778 auto rt = this->asRenderTargetProxy();
779 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400780 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400781 return;
782 }
783 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400784 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400785 return;
786 }
787 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400788 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400789 return;
790 }
791 int x = srcRect.fLeft;
792 int y = srcRect.fTop;
793 bool needsRescale = srcRect.size() != dstSize;
794 GrSurfaceProxyView srcView;
Brian Salomonbacbb922021-01-21 19:48:00 -0500795 auto info = SkImageInfo::Make(dstSize,
796 kRGBA_8888_SkColorType,
797 this->colorInfo().alphaType(),
798 dstColorSpace);
Brian Salomon63a0a752020-06-26 13:32:09 -0400799 if (needsRescale) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400800 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomonbacbb922021-01-21 19:48:00 -0500801 auto tempFC = this->rescale(info,
802 kTopLeft_GrSurfaceOrigin,
803 srcRect,
804 rescaleGamma,
805 rescaleMode);
806 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400807 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400808 return;
809 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500810 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
811 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400812 x = y = 0;
Brian Salomonbacbb922021-01-21 19:48:00 -0500813 srcView = tempFC->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400814 } else {
815 srcView = this->readSurfaceView();
816 if (!srcView.asTextureProxy()) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500817 srcView = GrSurfaceProxyView::Copy(fContext,
818 std::move(srcView),
819 GrMipmapped::kNo,
820 srcRect,
821 SkBackingFit::kApprox,
822 SkBudgeted::kYes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400823 if (!srcView) {
824 // If we can't get a texture copy of the contents then give up.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400825 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400826 return;
827 }
828 SkASSERT(srcView.asTextureProxy());
829 x = y = 0;
830 }
831 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomonbacbb922021-01-21 19:48:00 -0500832 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo(),
833 info.colorInfo());
Brian Salomon63a0a752020-06-26 13:32:09 -0400834 if (xform) {
835 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
Brian Salomonbacbb922021-01-21 19:48:00 -0500836 auto tempFC = GrSurfaceFillContext::Make(dContext,
837 info,
838 SkBackingFit::kApprox,
839 1,
840 GrMipmapped::kNo,
841 GrProtected::kNo,
842 kTopLeft_GrSurfaceOrigin);
843 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400844 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400845 return;
846 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500847 auto fp = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType());
848 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
849 tempFC->fillRectToRectWithFP(srcRectToDraw,
850 SkIRect::MakeSize(tempFC->dimensions()),
851 std::move(fp));
852 srcView = tempFC->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400853 SkASSERT(srcView.asTextureProxy());
854 x = y = 0;
855 }
856 }
857
Brian Salomonbacbb922021-01-21 19:48:00 -0500858 auto yInfo = SkImageInfo::MakeA8(dstSize);
859 auto yFC = GrSurfaceFillContext::MakeWithFallback(dContext, yInfo, SkBackingFit::kApprox);
860
861 auto uvInfo = yInfo.makeWH(yInfo.width()/2, yInfo.height()/2);
862 auto uFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
863 auto vFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
864
865 if (!yFC || !uFC || !vFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400866 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400867 return;
868 }
869
870 float baseM[20];
871 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
872
873 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
874
875 auto texMatrix = SkMatrix::Translate(x, y);
876
Brian Salomon63a0a752020-06-26 13:32:09 -0400877 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
878 PixelTransferResult yTransfer, uTransfer, vTransfer;
879
880 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
881 float yM[20];
882 std::fill_n(yM, 15, 0.f);
883 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500884
885 auto yFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
886 yFP = GrColorMatrixFragmentProcessor::Make(std::move(yFP),
887 yM,
888 /*unpremulInput=*/false,
889 /*clampRGBOutput=*/true,
890 /*premulOutput=*/false);
891 yFC->fillWithFP(std::move(yFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400892 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500893 yTransfer = yFC->transferPixels(GrColorType::kAlpha_8,
894 SkIRect::MakeSize(yFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400895 if (!yTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400896 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400897 return;
898 }
899 }
900
901 texMatrix.preScale(2.f, 2.f);
902 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
903 float uM[20];
904 std::fill_n(uM, 15, 0.f);
905 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500906
907 auto uFP = GrTextureEffect::Make(srcView,
908 this->colorInfo().alphaType(),
909 texMatrix,
910 GrSamplerState::Filter::kLinear);
911 uFP = GrColorMatrixFragmentProcessor::Make(std::move(uFP),
912 uM,
913 /*unpremulInput=*/false,
914 /*clampRGBOutput=*/true,
915 /*premulOutput=*/false);
916 uFC->fillWithFP(std::move(uFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400917 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500918 uTransfer = uFC->transferPixels(GrColorType::kAlpha_8,
919 SkIRect::MakeSize(uFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400920 if (!uTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400921 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400922 return;
923 }
924 }
925
926 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
927 float vM[20];
928 std::fill_n(vM, 15, 0.f);
929 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500930 auto vFP = GrTextureEffect::Make(std::move(srcView),
931 this->colorInfo().alphaType(),
932 texMatrix,
933 GrSamplerState::Filter::kLinear);
934 vFP = GrColorMatrixFragmentProcessor::Make(std::move(vFP),
935 vM,
936 /*unpremulInput=*/false,
937 /*clampRGBOutput=*/true,
938 /*premulOutput=*/false);
939 vFC->fillWithFP(std::move(vFP));
940
Brian Salomon63a0a752020-06-26 13:32:09 -0400941 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500942 vTransfer = vFC->transferPixels(GrColorType::kAlpha_8,
943 SkIRect::MakeSize(vFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400944 if (!vTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400945 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400946 return;
947 }
948 }
949
950 if (doSynchronousRead) {
Brian Salomondd4087d2020-12-23 20:36:44 -0500951 auto [yPmp, yStorage] = GrPixmap::Allocate(yInfo);
952 auto [uPmp, uStorage] = GrPixmap::Allocate(uvInfo);
953 auto [vPmp, vStorage] = GrPixmap::Allocate(uvInfo);
Brian Salomonbacbb922021-01-21 19:48:00 -0500954 if (!yFC->readPixels(dContext, yPmp, {0, 0}) ||
955 !uFC->readPixels(dContext, uPmp, {0, 0}) ||
956 !vFC->readPixels(dContext, vPmp, {0, 0})) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400957 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400958 return;
959 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400960 auto result = std::make_unique<AsyncReadResult>(dContext->priv().contextID());
Brian Salomondd4087d2020-12-23 20:36:44 -0500961 result->addCpuPlane(std::move(yStorage), yPmp.rowBytes());
962 result->addCpuPlane(std::move(uStorage), uPmp.rowBytes());
963 result->addCpuPlane(std::move(vStorage), vPmp.rowBytes());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400964 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400965 return;
966 }
967
968 struct FinishContext {
969 ReadPixelsCallback* fClientCallback;
970 ReadPixelsContext fClientContext;
971 GrClientMappedBufferManager* fMappedBufferManager;
972 SkISize fSize;
973 PixelTransferResult fYTransfer;
974 PixelTransferResult fUTransfer;
975 PixelTransferResult fVTransfer;
976 };
977 // Assumption is that the caller would like to flush. We could take a parameter or require an
978 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
979 // callback to GrGpu until after the next flush that flushes our op list, though.
980 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400981 callbackContext,
982 dContext->priv().clientMappedBufferManager(),
Brian Salomon63a0a752020-06-26 13:32:09 -0400983 dstSize,
984 std::move(yTransfer),
985 std::move(uTransfer),
986 std::move(vTransfer)};
987 auto finishCallback = [](GrGpuFinishedContext c) {
988 const auto* context = reinterpret_cast<const FinishContext*>(c);
989 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
990 auto manager = context->fMappedBufferManager;
991 size_t rowBytes = SkToSizeT(context->fSize.width());
992 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
993 (*context->fClientCallback)(context->fClientContext, nullptr);
994 delete context;
995 return;
996 }
997 rowBytes /= 2;
998 SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
999 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
1000 (*context->fClientCallback)(context->fClientContext, nullptr);
1001 delete context;
1002 return;
1003 }
1004 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
1005 (*context->fClientCallback)(context->fClientContext, nullptr);
1006 delete context;
1007 return;
1008 }
1009 (*context->fClientCallback)(context->fClientContext, std::move(result));
1010 delete context;
1011 };
1012 GrFlushInfo flushInfo;
1013 flushInfo.fFinishedContext = finishContext;
1014 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -05001015 dContext->priv().flushSurface(this->asSurfaceProxy(),
1016 SkSurface::BackendSurfaceAccess::kNoAccess,
1017 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -04001018}
1019
Brian Salomon982127b2021-01-21 10:43:35 -05001020bool GrSurfaceContext::copy(sk_sp<GrSurfaceProxy> src, SkIRect srcRect, SkIPoint dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001021 ASSERT_SINGLE_OWNER
1022 RETURN_FALSE_IF_ABANDONED
1023 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -04001024 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -04001025
Brian Salomon947efe22019-07-16 15:36:11 -04001026 const GrCaps* caps = fContext->priv().caps();
1027
Greg Daniel46cfbc62019-06-07 11:43:30 -04001028 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -05001029 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -04001030
Stephen White3c0a50f2020-01-16 18:19:54 -05001031 if (this->asSurfaceProxy()->framebufferOnly()) {
1032 return false;
1033 }
1034
Brian Salomon982127b2021-01-21 10:43:35 -05001035 if (!caps->canCopySurface(this->asSurfaceProxy(), src.get(), srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -04001036 return false;
1037 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001038
Brian Salomon982127b2021-01-21 10:43:35 -05001039 if (!GrClipSrcRectAndDstPoint(this->dimensions(), src->dimensions(), srcRect, dstPoint,
1040 &srcRect, &dstPoint)) {
1041 return false;
1042 }
1043
1044 if (this->origin() == kBottomLeft_GrSurfaceOrigin) {
1045 int rectHeight = srcRect.height();
1046 srcRect.fTop = src->backingStoreDimensions().height() - srcRect.fBottom;
1047 srcRect.fBottom = srcRect.fTop + rectHeight;
1048 dstPoint.fY = this->asSurfaceProxy()->backingStoreDimensions().height() -
1049 (dstPoint.fY + rectHeight);
1050 }
1051 return this->drawingManager()->newCopyRenderTask(std::move(src),
1052 srcRect,
1053 this->asSurfaceProxyRef(),
1054 dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001055}
Greg Daniel46cfbc62019-06-07 11:43:30 -04001056
Brian Salomonbacbb922021-01-21 19:48:00 -05001057std::unique_ptr<GrSurfaceFillContext> GrSurfaceContext::rescale(const GrImageInfo& info,
Brian Salomoneebe7352020-12-09 16:37:04 -05001058 GrSurfaceOrigin origin,
1059 SkIRect srcRect,
1060 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001061 RescaleMode rescaleMode) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001062 auto sfc = GrSurfaceFillContext::MakeWithFallback(fContext,
1063 info,
Brian Salomon1c86b632020-12-11 12:36:01 -05001064 SkBackingFit::kExact,
Brian Salomon1c86b632020-12-11 12:36:01 -05001065 1,
1066 GrMipmapped::kNo,
1067 this->asSurfaceProxy()->isProtected(),
1068 origin);
Brian Salomonbacbb922021-01-21 19:48:00 -05001069 if (!sfc || !this->rescaleInto(sfc.get(),
1070 SkIRect::MakeSize(sfc->dimensions()),
Brian Salomon1c86b632020-12-11 12:36:01 -05001071 srcRect,
1072 rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001073 rescaleMode)) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001074 return nullptr;
1075 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001076 return sfc;
Brian Salomon1c86b632020-12-11 12:36:01 -05001077}
1078
Brian Salomonbacbb922021-01-21 19:48:00 -05001079bool GrSurfaceContext::rescaleInto(GrSurfaceFillContext* dst,
Brian Salomon1c86b632020-12-11 12:36:01 -05001080 SkIRect dstRect,
1081 SkIRect srcRect,
1082 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001083 RescaleMode rescaleMode) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001084 SkASSERT(dst);
1085 if (!SkIRect::MakeSize(dst->dimensions()).contains((dstRect))) {
1086 return false;
1087 }
1088
Brian Salomone9ad9982019-07-22 16:17:41 -04001089 auto rtProxy = this->asRenderTargetProxy();
1090 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001091 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001092 }
1093
Stephen White3c0a50f2020-01-16 18:19:54 -05001094 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001095 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001096 }
1097
Greg Daniel40903af2020-01-30 14:55:05 -05001098 GrSurfaceProxyView texView = this->readSurfaceView();
Greg Daniel40903af2020-01-30 14:55:05 -05001099 if (!texView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -04001100 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect,
Brian Salomonc5243782020-04-02 12:50:34 -04001101 SkBackingFit::kApprox, SkBudgeted::kNo);
1102 if (!texView) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001103 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001104 }
Greg Daniel40903af2020-01-30 14:55:05 -05001105 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001106 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001107 }
1108
Brian Salomon1c86b632020-12-11 12:36:01 -05001109 SkISize finalSize = dstRect.size();
1110
Brian Salomonbf6b9792019-08-21 09:38:10 -04001111 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
1112 // 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 -05001113 std::unique_ptr<GrSurfaceFillContext> tempA;
1114 std::unique_ptr<GrSurfaceFillContext> tempB;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001115
Brian Salomone9ad9982019-07-22 16:17:41 -04001116 // Assume we should ignore the rescale linear request if the surface has no color space since
1117 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon63a0a752020-06-26 13:32:09 -04001118 if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() &&
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001119 !this->colorInfo().colorSpace()->gammaIsLinear()) {
1120 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomone9ad9982019-07-22 16:17:41 -04001121 // We'll fall back to kRGBA_8888 if half float not supported.
Brian Salomonbacbb922021-01-21 19:48:00 -05001122 GrImageInfo ii(GrColorType::kRGBA_F16,
1123 dst->colorInfo().alphaType(),
1124 std::move(cs),
1125 srcRect.size());
1126 auto linearRTC = GrSurfaceFillContext::MakeWithFallback(fContext,
1127 std::move(ii),
1128 SkBackingFit::kApprox,
1129 1,
1130 GrMipmapped::kNo,
1131 GrProtected::kNo,
1132 dst->origin());
Brian Salomone9ad9982019-07-22 16:17:41 -04001133 if (!linearRTC) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001134 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001135 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001136 auto fp = GrTextureEffect::Make(std::move(texView),
1137 this->colorInfo().alphaType(),
1138 SkMatrix::Translate(srcRect.topLeft()),
1139 GrSamplerState::Filter::kNearest,
1140 GrSamplerState::MipmapMode::kNone);
1141 fp = GrColorSpaceXformEffect::Make(std::move(fp),
1142 this->colorInfo(),
1143 linearRTC->colorInfo());
1144 linearRTC->fillWithFP(std::move(fp));
Greg Daniel40903af2020-01-30 14:55:05 -05001145 texView = linearRTC->readSurfaceView();
1146 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -04001147 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001148 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001149 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001150
Brian Salomon1c86b632020-12-11 12:36:01 -05001151 while (srcRect.size() != finalSize) {
1152 SkISize nextDims = finalSize;
Mike Reed1efa14d2021-01-02 21:44:59 -05001153 if (rescaleMode != RescaleMode::kNearest) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001154 if (srcRect.width() > finalSize.width()) {
1155 nextDims.fWidth = std::max((srcRect.width() + 1)/2, finalSize.width());
1156 } else if (srcRect.width() < finalSize.width()) {
1157 nextDims.fWidth = std::min(srcRect.width()*2, finalSize.width());
Brian Salomon59f31b12020-06-04 17:27:15 -04001158 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001159 if (srcRect.height() > finalSize.height()) {
1160 nextDims.fHeight = std::max((srcRect.height() + 1)/2, finalSize.height());
1161 } else if (srcRect.height() < finalSize.height()) {
1162 nextDims.fHeight = std::min(srcRect.height()*2, finalSize.height());
Brian Salomon59f31b12020-06-04 17:27:15 -04001163 }
1164 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001165 auto input = tempA ? tempA.get() : this;
Brian Salomone9ad9982019-07-22 16:17:41 -04001166 sk_sp<GrColorSpaceXform> xform;
Brian Salomonbacbb922021-01-21 19:48:00 -05001167 GrSurfaceFillContext* stepDst;
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001168 SkIRect stepDstRect;
Brian Salomon1c86b632020-12-11 12:36:01 -05001169 if (nextDims == finalSize) {
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001170 stepDst = dst;
1171 stepDstRect = dstRect;
Brian Salomonbacbb922021-01-21 19:48:00 -05001172 xform = GrColorSpaceXform::Make(input->colorInfo(), dst->colorInfo());
Brian Salomon1c86b632020-12-11 12:36:01 -05001173 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -05001174 GrImageInfo nextInfo(input->colorInfo(), nextDims);
1175 tempB = GrSurfaceFillContext::MakeWithFallback(fContext,
1176 nextInfo,
1177 SkBackingFit::kApprox);
Brian Salomon1c86b632020-12-11 12:36:01 -05001178 if (!tempB) {
1179 return false;
1180 }
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001181 stepDst = tempB.get();
1182 stepDstRect = SkIRect::MakeSize(tempB->dimensions());
Brian Salomone9ad9982019-07-22 16:17:41 -04001183 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001184 std::unique_ptr<GrFragmentProcessor> fp;
Mike Reed1efa14d2021-01-02 21:44:59 -05001185 if (rescaleMode == RescaleMode::kRepeatedCubic) {
Brian Salomon59f31b12020-06-04 17:27:15 -04001186 auto dir = GrBicubicEffect::Direction::kXY;
1187 if (nextDims.width() == srcRect.width()) {
1188 dir = GrBicubicEffect::Direction::kY;
1189 } else if (nextDims.height() == srcRect.height()) {
1190 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -04001191 }
Brian Salomon1af72d12020-06-25 10:47:26 -04001192 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
Mike Reed3867c702020-09-01 13:28:10 -04001193 static constexpr auto kKernel = GrBicubicEffect::gCatmullRom;
Brian Salomon1c86b632020-12-11 12:36:01 -05001194 fp = GrBicubicEffect::MakeSubset(std::move(texView),
1195 input->colorInfo().alphaType(),
Brian Salomonbacbb922021-01-21 19:48:00 -05001196 SkMatrix::I(),
Brian Salomon1c86b632020-12-11 12:36:01 -05001197 kWM,
1198 kWM,
1199 SkRect::Make(srcRect),
1200 kKernel,
1201 dir,
1202 *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -04001203 } else {
Mike Reed1efa14d2021-01-02 21:44:59 -05001204 auto filter = rescaleMode == RescaleMode::kNearest ? GrSamplerState::Filter::kNearest
1205 : GrSamplerState::Filter::kLinear;
Brian Salomonbacbb922021-01-21 19:48:00 -05001206 auto srcRectF = SkRect::Make(srcRect);
1207 fp = GrTextureEffect::MakeSubset(std::move(texView),
1208 this->colorInfo().alphaType(),
1209 SkMatrix::I(),
1210 {filter, GrSamplerState::MipmapMode::kNone},
1211 srcRectF,
1212 srcRectF,
1213 *this->caps());
Brian Salomone9ad9982019-07-22 16:17:41 -04001214 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001215 if (xform) {
1216 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
1217 }
1218 stepDst->fillRectToRectWithFP(srcRect, stepDstRect, std::move(fp));
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001219 texView = stepDst->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -04001220 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001221 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomone9ad9982019-07-22 16:17:41 -04001222 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001223 return true;
Brian Salomone9ad9982019-07-22 16:17:41 -04001224}
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001225
1226GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
1227 const SkIRect& rect) {
1228 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1229 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001230 auto direct = fContext->asDirectContext();
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001231 if (!direct) {
1232 return {};
1233 }
1234 auto rtProxy = this->asRenderTargetProxy();
1235 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1236 return {};
1237 }
1238
1239 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001240 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
1241 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001242 // Fail if read color type does not have all of dstCT's color channels and those missing color
1243 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001244 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1245 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
1246 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1247 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001248 return {};
1249 }
1250
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001251 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001252 !supportedRead.fOffsetAlignmentForTransferBuffer) {
1253 return {};
1254 }
1255
1256 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
1257 size_t size = rowBytes * rect.height();
1258 auto buffer = direct->priv().resourceProvider()->createBuffer(
1259 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
1260 if (!buffer) {
1261 return {};
1262 }
1263 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -05001264 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001265 if (flip) {
1266 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1267 this->height() - rect.fTop);
1268 }
Greg Danielbbfec9d2019-08-20 10:56:51 -04001269 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001270 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -04001271 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001272 PixelTransferResult result;
1273 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001274 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -04001275 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001276 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
1277 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001278 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
1279 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001280 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
1281 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -04001282 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001283 };
1284 }
1285 return result;
1286}
Greg Daniel46e366a2019-12-16 14:38:36 -05001287
1288#ifdef SK_DEBUG
1289void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001290 SkASSERT(fReadView.proxy());
1291 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -04001292 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
1293 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
1294 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
1295 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001296 this->onValidate();
1297}
1298#endif