blob: 8d38e0b9658c55a95c4bfc66682576da72cad936 [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
Brian Salomond63638b2021-03-05 14:00:07 -050031#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
32#define RETURN_FALSE_IF_ABANDONED if (this->fContext->abandoned()) { return false; }
33#define RETURN_NULLPTR_IF_ABANDONED if (this->fContext->abandoned()) { return nullptr; }
Brian Osman45580d32016-11-23 09:37:01 -050034
Greg Danielbfa19c42019-12-19 16:41:40 -050035std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -050036 GrSurfaceProxyView readView,
Brian Salomon14f99fc2020-12-07 12:19:47 -050037 const GrColorInfo& info) {
Greg Daniele20fcad2020-01-08 11:52:34 -050038 // It is probably not necessary to check if the context is abandoned here since uses of the
39 // GrSurfaceContext which need the context will mostly likely fail later on without an issue.
40 // However having this hear adds some reassurance in case there is a path doesn't handle an
41 // abandoned context correctly. It also lets us early out of some extra work.
Robert Phillips9eb00022020-06-30 15:30:12 -040042 if (context->abandoned()) {
Greg Daniele20fcad2020-01-08 11:52:34 -050043 return nullptr;
44 }
Greg Daniel3912a4b2020-01-14 09:56:04 -050045 GrSurfaceProxy* proxy = readView.proxy();
Greg Danielbfa19c42019-12-19 16:41:40 -050046 SkASSERT(proxy && proxy->asTextureProxy());
47
Greg Danielbfa19c42019-12-19 16:41:40 -050048 std::unique_ptr<GrSurfaceContext> surfaceContext;
Greg Daniel3912a4b2020-01-14 09:56:04 -050049 if (proxy->asRenderTargetProxy()) {
Brian Salomon8afde5f2020-04-01 16:22:00 -040050 // Will we ever want a swizzle that is not the default write swizzle for the format and
Greg Danielbfa19c42019-12-19 16:41:40 -050051 // colorType here? If so we will need to manually pass that in.
Brian Salomonc5243782020-04-02 12:50:34 -040052 GrSwizzle writeSwizzle;
Brian Salomon14f99fc2020-12-07 12:19:47 -050053 if (info.colorType() != GrColorType::kUnknown) {
54 writeSwizzle = context->priv().caps()->getWriteSwizzle(proxy->backendFormat(),
55 info.colorType());
Brian Salomonc5243782020-04-02 12:50:34 -040056 }
Brian Salomon8afde5f2020-04-01 16:22:00 -040057 GrSurfaceProxyView writeView(readView.refProxy(), readView.origin(), writeSwizzle);
Brian Salomon590f5672020-12-16 11:44:47 -050058 if (info.alphaType() == kPremul_SkAlphaType || info.alphaType() == kOpaque_SkAlphaType) {
59 surfaceContext = std::make_unique<GrSurfaceDrawContext>(context,
60 std::move(readView),
61 std::move(writeView),
62 info.colorType(),
63 info.refColorSpace(),
64 /*surface props*/ nullptr);
65 } else {
66 surfaceContext = std::make_unique<GrSurfaceFillContext>(context,
67 std::move(readView),
68 std::move(writeView),
69 info);
70 }
Greg Danielbfa19c42019-12-19 16:41:40 -050071 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -050072 surfaceContext = std::make_unique<GrSurfaceContext>(context, std::move(readView), info);
Greg Danielbfa19c42019-12-19 16:41:40 -050073 }
Robert Phillips07f0e412020-01-17 15:20:00 -050074 SkDEBUGCODE(surfaceContext->validate();)
Greg Danielbfa19c42019-12-19 16:41:40 -050075 return surfaceContext;
76}
77
Brian Salomona56a7462020-02-07 14:17:25 -050078std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
Brian Salomon14f99fc2020-12-07 12:19:47 -050079 const GrImageInfo& info,
Brian Salomona56a7462020-02-07 14:17:25 -050080 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050081 SkBackingFit fit,
Brian Salomon14f99fc2020-12-07 12:19:47 -050082 GrSurfaceOrigin origin,
83 GrRenderable renderable,
84 int sampleCount,
85 GrMipmapped mipmapped,
86 GrProtected isProtected,
Brian Salomona56a7462020-02-07 14:17:25 -050087 SkBudgeted budgeted) {
Brian Salomon14f99fc2020-12-07 12:19:47 -050088 SkASSERT(context);
89 SkASSERT(renderable == GrRenderable::kYes || sampleCount == 1);
90 if (context->abandoned()) {
91 return nullptr;
Brian Salomond005b692020-04-01 15:47:05 -040092 }
Brian Salomon14f99fc2020-12-07 12:19:47 -050093 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(format,
94 info.dimensions(),
95 renderable,
96 sampleCount,
97 mipmapped,
98 fit,
99 budgeted,
100 isProtected);
Greg Danielbfa19c42019-12-19 16:41:40 -0500101 if (!proxy) {
102 return nullptr;
103 }
104
Brian Salomon14f99fc2020-12-07 12:19:47 -0500105 GrSwizzle swizzle;
106 if (info.colorType() != GrColorType::kUnknown &&
107 !context->priv().caps()->isFormatCompressed(format)) {
108 swizzle = context->priv().caps()->getReadSwizzle(format, info.colorType());
109 }
110
Greg Daniel3912a4b2020-01-14 09:56:04 -0500111 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500112 return GrSurfaceContext::Make(context, std::move(view), info.colorInfo());
113}
114
115std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
116 const GrImageInfo& info,
117 SkBackingFit fit,
118 GrSurfaceOrigin origin,
119 GrRenderable renderable,
120 int sampleCount,
121 GrMipmapped mipmapped,
122 GrProtected isProtected,
123 SkBudgeted budgeted) {
124 GrBackendFormat format = context->priv().caps()->getDefaultBackendFormat(info.colorType(),
125 renderable);
126 return Make(context,
127 info,
128 format,
129 fit,
130 origin,
131 renderable,
132 sampleCount,
133 mipmapped,
134 isProtected,
135 budgeted);
Greg Danielbfa19c42019-12-19 16:41:40 -0500136}
137
Robert Phillips69893702019-02-22 11:16:30 -0500138GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500139 GrSurfaceProxyView readView,
Brian Salomon14f99fc2020-12-07 12:19:47 -0500140 const GrColorInfo& info)
141 : fContext(context), fReadView(std::move(readView)), fColorInfo(info) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400142 SkASSERT(!context->abandoned());
Greg Daniele20fcad2020-01-08 11:52:34 -0500143}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400144
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400145const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); }
146
Robert Phillips0d075de2019-03-04 11:08:13 -0500147GrAuditTrail* GrSurfaceContext::auditTrail() {
148 return fContext->priv().auditTrail();
149}
150
151GrDrawingManager* GrSurfaceContext::drawingManager() {
152 return fContext->priv().drawingManager();
153}
154
155const GrDrawingManager* GrSurfaceContext::drawingManager() const {
156 return fContext->priv().drawingManager();
157}
158
159#ifdef SK_DEBUG
Brian Salomon70fe17e2020-11-30 14:33:58 -0500160GrSingleOwner* GrSurfaceContext::singleOwner() const { return fContext->priv().singleOwner(); }
Robert Phillips0d075de2019-03-04 11:08:13 -0500161#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400162
Brian Salomondd4087d2020-12-23 20:36:44 -0500163static bool alpha_types_compatible(SkAlphaType srcAlphaType, SkAlphaType dstAlphaType) {
164 // If both alpha types are kUnknown things make sense. If not, it's too underspecified.
165 return (srcAlphaType == kUnknown_SkAlphaType) == (dstAlphaType == kUnknown_SkAlphaType);
166}
167
168bool GrSurfaceContext::readPixels(GrDirectContext* dContext, GrPixmap dst, SkIPoint pt) {
Brian Salomon1d435302019-07-01 13:05:28 -0400169 ASSERT_SINGLE_OWNER
170 RETURN_FALSE_IF_ABANDONED
171 SkDEBUGCODE(this->validate();)
172 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400173 if (!fContext->priv().matches(dContext)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400174 return false;
175 }
Brian Salomon46f63232021-01-25 10:13:35 -0500176
177 if (dst.colorType() == GrColorType::kUnknown) {
178 return false;
179 }
180
Brian Salomonc24c8ef2021-02-01 13:32:30 -0500181 if (dst.rowBytes() % dst.info().bpp()) {
182 return false;
183 }
184
Brian Salomondd4087d2020-12-23 20:36:44 -0500185 dst = dst.clip(this->dimensions(), &pt);
186 if (!dst.hasPixels()) {
Brian Salomon1d435302019-07-01 13:05:28 -0400187 return false;
188 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500189 if (!alpha_types_compatible(this->colorInfo().alphaType(), dst.alphaType())) {
Brian Salomon1d435302019-07-01 13:05:28 -0400190 return false;
191 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500192 // We allow unknown alpha types but only if both src and dst are unknown. Otherwise, it's too
193 // weird to reason about what should be expected.
Greg Daniel6eb8c242019-06-05 10:22:24 -0400194
Brian Salomon982127b2021-01-21 10:43:35 -0500195 sk_sp<GrSurfaceProxy> srcProxy = this->asSurfaceProxyRef();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400196
Stephen White3c0a50f2020-01-16 18:19:54 -0500197 if (srcProxy->framebufferOnly()) {
198 return false;
199 }
200
Greg Daniel6eb8c242019-06-05 10:22:24 -0400201 // MDB TODO: delay this instantiation until later in the method
Adlai Hollerc95b5892020-08-11 12:02:22 -0400202 if (!srcProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400203 return false;
204 }
205
206 GrSurface* srcSurface = srcProxy->peekSurface();
207
Brian Salomondd4087d2020-12-23 20:36:44 -0500208 SkColorSpaceXformSteps::Flags flags =
209 SkColorSpaceXformSteps{this->colorInfo(), dst.info()}.flags;
Mike Klein7321e6a2019-12-03 11:08:40 -0600210 bool unpremul = flags.unpremul,
211 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
212 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400213
Adlai Hollerc95b5892020-08-11 12:02:22 -0400214 const GrCaps* caps = dContext->priv().caps();
Robert Phillips07f0e412020-01-17 15:20:00 -0500215 bool srcIsCompressed = caps->isFormatCompressed(srcSurface->backendFormat());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400216 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
217 // care so much about getImageData performance. However, in order to ensure putImageData/
218 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
219 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
220 // fContext->vaildaPMUPMConversionExists()).
Greg Daniel0258c902019-08-01 13:08:33 -0400221 GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
222 GrRenderable::kYes);
Greg Danielc71c7962020-01-14 16:44:18 -0500223 GrColorType srcColorType = this->colorInfo().colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400224 bool canvas2DFastPath = unpremul && !needColorConversion &&
Brian Salomondd4087d2020-12-23 20:36:44 -0500225 (GrColorType::kRGBA_8888 == dst.colorType() ||
226 GrColorType::kBGRA_8888 == dst.colorType()) &&
Brian Salomon1d435302019-07-01 13:05:28 -0400227 SkToBool(srcProxy->asTextureProxy()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500228 (srcColorType == GrColorType::kRGBA_8888 ||
229 srcColorType == GrColorType::kBGRA_8888) &&
Greg Daniel0258c902019-08-01 13:08:33 -0400230 defaultRGBAFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400231 dContext->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400232
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400233 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400234 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400235 return false;
236 }
237
Brian Salomondc0710f2019-07-01 14:59:32 -0400238 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400239 std::unique_ptr<GrSurfaceContext> tempCtx;
240 if (this->asTextureProxy()) {
241 GrColorType colorType = (canvas2DFastPath || srcIsCompressed)
242 ? GrColorType::kRGBA_8888
243 : this->colorInfo().colorType();
Brian Salomondd4087d2020-12-23 20:36:44 -0500244 SkAlphaType alphaType = canvas2DFastPath ? dst.alphaType()
Brian Salomon590f5672020-12-16 11:44:47 -0500245 : this->colorInfo().alphaType();
246 GrImageInfo tempInfo(colorType,
247 alphaType,
248 this->colorInfo().refColorSpace(),
Brian Salomondd4087d2020-12-23 20:36:44 -0500249 dst.dimensions());
Brian Salomon590f5672020-12-16 11:44:47 -0500250 auto sfc = GrSurfaceFillContext::Make(dContext, tempInfo, SkBackingFit::kApprox);
251 if (!sfc) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400252 return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400253 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400254
255 std::unique_ptr<GrFragmentProcessor> fp;
256 if (canvas2DFastPath) {
257 fp = dContext->priv().createPMToUPMEffect(GrTextureEffect::Make(
258 this->readSurfaceView(), this->colorInfo().alphaType()));
Brian Salomondd4087d2020-12-23 20:36:44 -0500259 if (dst.colorType() == GrColorType::kBGRA_8888) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400260 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
Brian Salomondd4087d2020-12-23 20:36:44 -0500261 dst = GrPixmap(dst.info().makeColorType(GrColorType::kRGBA_8888),
262 dst.addr(),
263 dst.rowBytes());
Brian Salomon72c7b982020-10-06 10:07:38 -0400264 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400265 } else {
266 fp = GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType());
267 }
268 if (!fp) {
269 return false;
270 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500271 sfc->fillRectToRectWithFP(SkIRect::MakePtSize(pt, dst.dimensions()),
272 SkIRect::MakeSize(dst.dimensions()),
Brian Salomon590f5672020-12-16 11:44:47 -0500273 std::move(fp));
Brian Salomon72c7b982020-10-06 10:07:38 -0400274 pt = {0, 0};
Brian Salomon590f5672020-12-16 11:44:47 -0500275 tempCtx = std::move(sfc);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400276 } else {
Brian Salomon72c7b982020-10-06 10:07:38 -0400277 auto restrictions = this->caps()->getDstCopyRestrictions(this->asRenderTargetProxy(),
278 this->colorInfo().colorType());
279 sk_sp<GrSurfaceProxy> copy;
280 static constexpr auto kFit = SkBackingFit::kExact;
281 static constexpr auto kBudgeted = SkBudgeted::kYes;
282 static constexpr auto kMipMapped = GrMipMapped::kNo;
283 if (restrictions.fMustCopyWholeSrc) {
Brian Salomon982127b2021-01-21 10:43:35 -0500284 copy = GrSurfaceProxy::Copy(fContext,
285 std::move(srcProxy),
286 this->origin(),
287 kMipMapped,
288 kFit,
Brian Salomon72c7b982020-10-06 10:07:38 -0400289 kBudgeted);
290 } else {
Brian Salomondd4087d2020-12-23 20:36:44 -0500291 auto srcRect = SkIRect::MakePtSize(pt, dst.dimensions());
Brian Salomon982127b2021-01-21 10:43:35 -0500292 copy = GrSurfaceProxy::Copy(fContext,
293 std::move(srcProxy),
294 this->origin(),
295 kMipMapped,
296 srcRect,
297 kFit,
298 kBudgeted,
299 restrictions.fRectsMustMatch);
Brian Salomon72c7b982020-10-06 10:07:38 -0400300 pt = {0, 0};
301 }
302 if (!copy) {
303 return false;
304 }
305 GrSurfaceProxyView view{std::move(copy), this->origin(), this->readSwizzle()};
Brian Salomon14f99fc2020-12-07 12:19:47 -0500306 tempCtx = GrSurfaceContext::Make(dContext, std::move(view), this->colorInfo());
Brian Salomon72c7b982020-10-06 10:07:38 -0400307 SkASSERT(tempCtx);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400308 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500309 return tempCtx->readPixels(dContext, dst, pt);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400310 }
311
Greg Danielb8d84f82020-02-13 14:25:00 -0500312 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400313
Brian Salomon1d435302019-07-01 13:05:28 -0400314 auto supportedRead = caps->supportedReadPixelsColorType(
Brian Salomondd4087d2020-12-23 20:36:44 -0500315 this->colorInfo().colorType(), srcProxy->backendFormat(), dst.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400316
Brian Salomondd4087d2020-12-23 20:36:44 -0500317 bool makeTight =
318 !caps->readPixelsRowBytesSupport() && dst.rowBytes() != dst.info().minRowBytes();
Brian Salomon1047a492019-07-02 12:25:21 -0400319
320 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomondd4087d2020-12-23 20:36:44 -0500321 (dst.colorType() != supportedRead.fColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400322
Brian Salomonf30b1c12019-06-20 12:25:02 -0400323 std::unique_ptr<char[]> tmpPixels;
Brian Salomondd4087d2020-12-23 20:36:44 -0500324 GrPixmap tmp;
325 void* readDst = dst.addr();
326 size_t readRB = dst.rowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400327 if (convert) {
Brian Salomondd4087d2020-12-23 20:36:44 -0500328 GrImageInfo tmpInfo(supportedRead.fColorType,
329 this->colorInfo().alphaType(),
330 this->colorInfo().refColorSpace(),
331 dst.dimensions());
Brian Salomon1d435302019-07-01 13:05:28 -0400332 size_t tmpRB = tmpInfo.minRowBytes();
333 size_t size = tmpRB * tmpInfo.height();
334 // Chrome MSAN bots require the data to be initialized (hence the ()).
John Stilesfbd050b2020-08-03 13:21:46 -0400335 tmpPixels = std::make_unique<char[]>(size);
Brian Salomondd4087d2020-12-23 20:36:44 -0500336 tmp = {tmpInfo, tmpPixels.get(), tmpRB};
Brian Salomonf30b1c12019-06-20 12:25:02 -0400337
Brian Salomonf30b1c12019-06-20 12:25:02 -0400338 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400339 readRB = tmpRB;
Brian Salomondd4087d2020-12-23 20:36:44 -0500340 pt.fY = flip ? srcSurface->height() - pt.fY - dst.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400341 }
342
Brian Salomon982127b2021-01-21 10:43:35 -0500343 dContext->priv().flushSurface(srcProxy.get());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400344 dContext->submit();
Brian Salomondd4087d2020-12-23 20:36:44 -0500345 if (!dContext->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dst.width(), dst.height(),
346 this->colorInfo().colorType(),
Adlai Hollerc95b5892020-08-11 12:02:22 -0400347 supportedRead.fColorType, readDst, readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400348 return false;
349 }
350
Brian Salomondd4087d2020-12-23 20:36:44 -0500351 if (tmp.hasPixels()) {
352 return GrConvertPixels(dst, tmp, flip);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400353 }
354 return true;
355}
Robert Phillips0d075de2019-03-04 11:08:13 -0500356
Brian Salomondd4087d2020-12-23 20:36:44 -0500357bool GrSurfaceContext::writePixels(GrDirectContext* dContext, GrPixmap src, SkIPoint pt) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400358 ASSERT_SINGLE_OWNER
359 RETURN_FALSE_IF_ABANDONED
360 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400361 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400362
Adlai Hollerc95b5892020-08-11 12:02:22 -0400363 if (!dContext) {
Brian Salomonc320b152018-02-20 14:05:36 -0500364 return false;
365 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500366
Brian Salomon1d435302019-07-01 13:05:28 -0400367 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500368 return false;
369 }
370
Brian Salomon46f63232021-01-25 10:13:35 -0500371 if (src.colorType() == GrColorType::kUnknown) {
372 return false;
373 }
374
Brian Salomondd4087d2020-12-23 20:36:44 -0500375 src = src.clip(this->dimensions(), &pt);
376 if (!src.hasPixels()) {
Brian Salomon1d435302019-07-01 13:05:28 -0400377 return false;
378 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500379 if (!alpha_types_compatible(src.alphaType(), this->colorInfo().alphaType())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400380 return false;
381 }
382
383 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
Stephen White3c0a50f2020-01-16 18:19:54 -0500384
385 if (dstProxy->framebufferOnly()) {
386 return false;
387 }
388
Adlai Hollerc95b5892020-08-11 12:02:22 -0400389 if (!dstProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400390 return false;
391 }
392
393 GrSurface* dstSurface = dstProxy->peekSurface();
394
Brian Salomondd4087d2020-12-23 20:36:44 -0500395 SkColorSpaceXformSteps::Flags flags =
396 SkColorSpaceXformSteps{src.info(), this->colorInfo()}.flags;
Mike Klein7321e6a2019-12-03 11:08:40 -0600397 bool unpremul = flags.unpremul,
398 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
399 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400400
Adlai Hollerc95b5892020-08-11 12:02:22 -0400401 const GrCaps* caps = dContext->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400402
403 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
404 GrRenderable::kNo);
405
Greg Danielc71c7962020-01-14 16:44:18 -0500406 GrColorType dstColorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400407 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
408 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400409 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
Brian Salomondd4087d2020-12-23 20:36:44 -0500410 (src.colorType() == GrColorType::kRGBA_8888 ||
411 src.colorType() == GrColorType::kBGRA_8888) &&
Brian Salomon590f5672020-12-16 11:44:47 -0500412 this->asFillContext() &&
Greg Danielc71c7962020-01-14 16:44:18 -0500413 (dstColorType == GrColorType::kRGBA_8888 ||
414 dstColorType == GrColorType::kBGRA_8888) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400415 rgbaDefaultFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400416 dContext->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400417
418 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500419 GrColorInfo tempColorInfo;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400420 GrBackendFormat format;
Greg Danielbfa19c42019-12-19 16:41:40 -0500421 GrSwizzle tempReadSwizzle;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400422 if (canvas2DFastPath) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500423 tempColorInfo = {GrColorType::kRGBA_8888,
424 kUnpremul_SkAlphaType,
425 this->colorInfo().refColorSpace()};
Greg Daniel7bfc9132019-08-14 14:23:53 -0400426 format = rgbaDefaultFormat;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400427 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500428 tempColorInfo = this->colorInfo();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400429 format = dstProxy->backendFormat().makeTexture2D();
430 if (!format.isValid()) {
431 return false;
432 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500433 tempReadSwizzle = this->readSwizzle();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400434 }
435
Greg Daniel2e52ad12019-06-13 10:04:16 -0400436 // It is more efficient for us to write pixels into a top left origin so we prefer that.
437 // However, if the final proxy isn't a render target then we must use a copy to move the
438 // data into it which requires the origins to match. If the final proxy is a render target
439 // we can use a draw instead which doesn't have this origin restriction. Thus for render
440 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400441 GrSurfaceOrigin tempOrigin =
Brian Salomon590f5672020-12-16 11:44:47 -0500442 this->asFillContext() ? kTopLeft_GrSurfaceOrigin : this->origin();
Adlai Hollerc95b5892020-08-11 12:02:22 -0400443 auto tempProxy = dContext->priv().proxyProvider()->createProxy(
Brian Salomondd4087d2020-12-23 20:36:44 -0500444 format, src.dimensions(), GrRenderable::kNo, 1, GrMipmapped::kNo,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400445 SkBackingFit::kApprox, SkBudgeted::kYes, GrProtected::kNo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400446 if (!tempProxy) {
447 return false;
448 }
Greg Daniel3912a4b2020-01-14 09:56:04 -0500449 GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500450 GrSurfaceContext tempCtx(dContext, tempView, tempColorInfo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400451
452 // In the fast path we always write the srcData to the temp context as though it were RGBA.
453 // When the data is really BGRA the write will cause the R and B channels to be swapped in
454 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
455 // dst.
Brian Salomondd4087d2020-12-23 20:36:44 -0500456 GrColorType origSrcColorType = src.colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400457 if (canvas2DFastPath) {
Brian Salomondd4087d2020-12-23 20:36:44 -0500458 src = {src.info().makeColorType(GrColorType::kRGBA_8888), src.addr(), src.rowBytes()};
Brian Salomon1d435302019-07-01 13:05:28 -0400459 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500460 if (!tempCtx.writePixels(dContext, src, {0, 0})) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400461 return false;
462 }
463
Brian Salomon590f5672020-12-16 11:44:47 -0500464 if (this->asFillContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400465 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400466 if (canvas2DFastPath) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400467 fp = dContext->priv().createUPMToPMEffect(
Brian Salomon14f99fc2020-12-07 12:19:47 -0500468 GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType()));
Brian Salomon1d435302019-07-01 13:05:28 -0400469 // Important: check the original src color type here!
Brian Salomondd4087d2020-12-23 20:36:44 -0500470 if (origSrcColorType == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400471 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
472 }
473 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500474 fp = GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400475 }
476 if (!fp) {
477 return false;
478 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500479 this->asFillContext()->fillRectToRectWithFP(SkIRect::MakeSize(src.dimensions()),
480 SkIRect::MakePtSize(pt, src.dimensions()),
481 std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400482 } else {
Brian Salomondd4087d2020-12-23 20:36:44 -0500483 SkIRect srcRect = SkIRect::MakeSize(src.dimensions());
Brian Salomon1d435302019-07-01 13:05:28 -0400484 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Brian Salomon982127b2021-01-21 10:43:35 -0500485 if (!this->copy(std::move(tempProxy), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400486 return false;
487 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400488 }
489 return true;
490 }
491
Brian Salomon1d435302019-07-01 13:05:28 -0400492 GrColorType allowedColorType =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400493 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400494 dstProxy->backendFormat(),
Brian Salomondd4087d2020-12-23 20:36:44 -0500495 src.colorType()).fColorType;
Greg Danielb8d84f82020-02-13 14:25:00 -0500496 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomondd4087d2020-12-23 20:36:44 -0500497 bool makeTight = !caps->writePixelsRowBytesSupport() &&
498 src.rowBytes() != src.info().minRowBytes();
Brian Salomon1047a492019-07-02 12:25:21 -0400499 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomondd4087d2020-12-23 20:36:44 -0500500 (src.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400501
Brian Salomon24949422021-02-11 09:39:46 -0500502 if (convert) {
Brian Salomondd4087d2020-12-23 20:36:44 -0500503 GrImageInfo tmpInfo(allowedColorType,
504 this->colorInfo().alphaType(),
505 this->colorInfo().refColorSpace(),
506 src.dimensions());
Brian Salomonbe1084b2021-01-26 13:29:30 -0500507 GrPixmap tmp = GrPixmap::Allocate(tmpInfo);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400508
Brian Salomondd4087d2020-12-23 20:36:44 -0500509 SkAssertResult(GrConvertPixels(tmp, src, flip));
Brian Salomonf30b1c12019-06-20 12:25:02 -0400510
Brian Salomondd4087d2020-12-23 20:36:44 -0500511 src = tmp;
Brian Salomon1d435302019-07-01 13:05:28 -0400512 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400513 }
514
Brian Salomonbe1084b2021-01-26 13:29:30 -0500515 GrMipLevel level;
516 level.fPixels = src.addr();
517 level.fRowBytes = src.rowBytes();
Brian Salomon24949422021-02-11 09:39:46 -0500518 bool result = dContext->priv().drawingManager()->newWritePixelsTask(
Brian Salomonbe1084b2021-01-26 13:29:30 -0500519 this->asSurfaceProxyRef(),
520 SkIRect::MakePtSize(pt, src.dimensions()),
521 src.colorType(),
522 dstColorType,
523 &level,
524 1,
525 src.pixelStorage());
Brian Salomon24949422021-02-11 09:39:46 -0500526 if (result && !src.ownsPixels()) {
527 // If the pixmap doesn't own its pixels then we must flush so that they are pushed to
528 // the GPU driver before we return.
529 dContext->priv().flushSurface(dstProxy);
530 }
531 return result;
Brian Osman45580d32016-11-23 09:37:01 -0500532}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400533
Adlai Hollerc95b5892020-08-11 12:02:22 -0400534void GrSurfaceContext::asyncRescaleAndReadPixels(GrDirectContext* dContext,
535 const SkImageInfo& info,
Brian Salomon63a0a752020-06-26 13:32:09 -0400536 const SkIRect& srcRect,
537 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500538 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400539 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400540 ReadPixelsContext callbackContext) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400541 if (!dContext) {
542 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400543 return;
544 }
545 auto rt = this->asRenderTargetProxy();
546 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400547 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400548 return;
549 }
550 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400551 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400552 return;
553 }
554 auto dstCT = SkColorTypeToGrColorType(info.colorType());
555 if (dstCT == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400556 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400557 return;
558 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500559 bool needsRescale = srcRect.size() != info.dimensions();
Brian Salomon63a0a752020-06-26 13:32:09 -0400560 auto colorTypeOfFinalContext = this->colorInfo().colorType();
561 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
562 if (needsRescale) {
563 colorTypeOfFinalContext = dstCT;
564 backendFormatOfFinalContext =
565 this->caps()->getDefaultBackendFormat(dstCT, GrRenderable::kYes);
566 }
567 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonbacbb922021-01-21 19:48:00 -0500568 backendFormatOfFinalContext,
569 dstCT);
Brian Salomon63a0a752020-06-26 13:32:09 -0400570 // Fail if we can't read from the source surface's color type.
571 if (readInfo.fColorType == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400572 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400573 return;
574 }
575 // Fail if read color type does not have all of dstCT's color channels and those missing color
576 // channels are in the src.
577 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
578 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
579 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
580 if ((~legalReadChannels & dstChannels) & srcChannels) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400581 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400582 return;
583 }
584
Brian Salomonbacbb922021-01-21 19:48:00 -0500585 std::unique_ptr<GrSurfaceFillContext> tempFC;
Brian Salomon63a0a752020-06-26 13:32:09 -0400586 int x = srcRect.fLeft;
587 int y = srcRect.fTop;
588 if (needsRescale) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500589 tempFC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma, rescaleMode);
590 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400591 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400592 return;
593 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500594 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
595 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400596 x = y = 0;
597 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -0500598 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo(),
599 info.colorInfo());
Brian Salomon63a0a752020-06-26 13:32:09 -0400600 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
601 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
602 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomonbacbb922021-01-21 19:48:00 -0500603 SkIRect srcRectToDraw = srcRect;
Brian Salomon63a0a752020-06-26 13:32:09 -0400604 // If the src is not texturable first try to make a copy to a texture.
605 if (!texProxyView.asTextureProxy()) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500606 texProxyView = GrSurfaceProxyView::Copy(fContext,
607 texProxyView,
608 GrMipmapped::kNo,
609 srcRect,
610 SkBackingFit::kApprox,
611 SkBudgeted::kNo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400612 if (!texProxyView) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400613 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400614 return;
615 }
616 SkASSERT(texProxyView.asTextureProxy());
Brian Salomonbacbb922021-01-21 19:48:00 -0500617 srcRectToDraw = SkIRect::MakeSize(srcRect.size());
Brian Salomon63a0a752020-06-26 13:32:09 -0400618 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500619 auto tempInfo = GrImageInfo(info).makeColorType(this->colorInfo().colorType());
620 tempFC = GrSurfaceFillContext::Make(dContext, tempInfo, SkBackingFit::kApprox);
621 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400622 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400623 return;
624 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500625 auto fp = GrTextureEffect::Make(std::move(texProxyView), this->colorInfo().alphaType());
626 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
627 tempFC->fillRectToRectWithFP(srcRectToDraw,
628 SkIRect::MakeSize(tempFC->dimensions()),
629 std::move(fp));
Brian Salomon63a0a752020-06-26 13:32:09 -0400630 x = y = 0;
631 }
632 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500633 auto srcCtx = tempFC ? tempFC.get() : this;
634 return srcCtx->asyncReadPixels(dContext,
635 SkIRect::MakePtSize({x, y}, info.dimensions()),
636 info.colorType(),
637 callback,
638 callbackContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400639}
640
641class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult {
642public:
643 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
644 ~AsyncReadResult() override {
645 for (int i = 0; i < fPlanes.count(); ++i) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500646 fPlanes[i].releaseMappedBuffer(fInboxID);
Brian Salomon63a0a752020-06-26 13:32:09 -0400647 }
648 }
649
650 int count() const override { return fPlanes.count(); }
Brian Salomonbe1084b2021-01-26 13:29:30 -0500651 const void* data(int i) const override { return fPlanes[i].data(); }
652 size_t rowBytes(int i) const override { return fPlanes[i].rowBytes(); }
Brian Salomon63a0a752020-06-26 13:32:09 -0400653
654 bool addTransferResult(const PixelTransferResult& result,
655 SkISize dimensions,
656 size_t rowBytes,
657 GrClientMappedBufferManager* manager) {
658 SkASSERT(!result.fTransferBuffer->isMapped());
659 const void* mappedData = result.fTransferBuffer->map();
660 if (!mappedData) {
661 return false;
662 }
663 if (result.fPixelConverter) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500664 size_t size = rowBytes*dimensions.height();
665 sk_sp<SkData> data = SkData::MakeUninitialized(size);
666 result.fPixelConverter(data->writable_data(), mappedData);
667 this->addCpuPlane(std::move(data), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400668 result.fTransferBuffer->unmap();
669 } else {
670 manager->insert(result.fTransferBuffer);
671 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
672 }
673 return true;
674 }
675
Brian Salomonbe1084b2021-01-26 13:29:30 -0500676 void addCpuPlane(sk_sp<SkData> data, size_t rowBytes) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400677 SkASSERT(data);
678 SkASSERT(rowBytes > 0);
Brian Salomonbe1084b2021-01-26 13:29:30 -0500679 fPlanes.emplace_back(std::move(data), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400680 }
681
682private:
683 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
684 SkASSERT(data);
685 SkASSERT(rowBytes > 0);
686 SkASSERT(mappedBuffer);
687 SkASSERT(mappedBuffer->isMapped());
Brian Salomonbe1084b2021-01-26 13:29:30 -0500688 fPlanes.emplace_back(std::move(mappedBuffer), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400689 }
690
Brian Salomonbe1084b2021-01-26 13:29:30 -0500691 class Plane {
692 public:
693 Plane(sk_sp<GrGpuBuffer> buffer, size_t rowBytes)
694 : fMappedBuffer(std::move(buffer)), fRowBytes(rowBytes) {}
695 Plane(sk_sp<SkData> data, size_t rowBytes) : fData(std::move(data)), fRowBytes(rowBytes) {}
696
697 Plane(const Plane&) = delete;
698 Plane(Plane&&) = default;
699
700 ~Plane() { SkASSERT(!fMappedBuffer); }
701
702 Plane& operator=(const Plane&) = delete;
703 Plane& operator=(Plane&&) = default;
704
705 void releaseMappedBuffer(uint32_t inboxID) {
706 if (fMappedBuffer) {
707 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
708 {std::move(fMappedBuffer), inboxID});
709 }
710 }
711
712 const void* data() const {
713 if (fMappedBuffer) {
714 SkASSERT(!fData);
715 SkASSERT(fMappedBuffer->isMapped());
716 return fMappedBuffer->map();
717 }
718 SkASSERT(fData);
719 return fData->data();
720 }
721
722 size_t rowBytes() const { return fRowBytes; }
723
724 private:
725 sk_sp<SkData> fData;
Brian Salomon1eea1ea2021-01-26 18:12:25 +0000726 sk_sp<GrGpuBuffer> fMappedBuffer;
Brian Salomonbe1084b2021-01-26 13:29:30 -0500727 size_t fRowBytes;
Brian Salomon63a0a752020-06-26 13:32:09 -0400728 };
729 SkSTArray<3, Plane> fPlanes;
730 uint32_t fInboxID;
731};
732
Adlai Hollerc95b5892020-08-11 12:02:22 -0400733void GrSurfaceContext::asyncReadPixels(GrDirectContext* dContext,
734 const SkIRect& rect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400735 SkColorType colorType,
736 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400737 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400738 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
739 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
740
Adlai Hollerc95b5892020-08-11 12:02:22 -0400741 if (!dContext || this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
742 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400743 return;
744 }
745
Adlai Hollerc95b5892020-08-11 12:02:22 -0400746 auto mappedBufferManager = dContext->priv().clientMappedBufferManager();
Brian Salomon63a0a752020-06-26 13:32:09 -0400747
748 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
749
750 if (!transferResult.fTransferBuffer) {
751 auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
752 this->colorInfo().refColorSpace());
753 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomonbe1084b2021-01-26 13:29:30 -0500754 GrPixmap pm = GrPixmap::Allocate(ii);
755 result->addCpuPlane(pm.pixelStorage(), pm.rowBytes());
Brian Salomon63a0a752020-06-26 13:32:09 -0400756
Adlai Hollerc95b5892020-08-11 12:02:22 -0400757 SkIPoint pt{rect.fLeft, rect.fTop};
Brian Salomondd4087d2020-12-23 20:36:44 -0500758 if (!this->readPixels(dContext, pm, pt)) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400759 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400760 return;
761 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400762 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400763 return;
764 }
765
766 struct FinishContext {
767 ReadPixelsCallback* fClientCallback;
768 ReadPixelsContext fClientContext;
769 SkISize fSize;
770 SkColorType fColorType;
771 GrClientMappedBufferManager* fMappedBufferManager;
772 PixelTransferResult fTransferResult;
773 };
774 // Assumption is that the caller would like to flush. We could take a parameter or require an
775 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
776 // callback to GrGpu until after the next flush that flushes our op list, though.
777 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400778 callbackContext,
Brian Salomon63a0a752020-06-26 13:32:09 -0400779 rect.size(),
780 colorType,
781 mappedBufferManager,
782 std::move(transferResult)};
783 auto finishCallback = [](GrGpuFinishedContext c) {
784 const auto* context = reinterpret_cast<const FinishContext*>(c);
785 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
786 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
787 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
788 context->fMappedBufferManager)) {
789 result.reset();
790 }
791 (*context->fClientCallback)(context->fClientContext, std::move(result));
792 delete context;
793 };
794 GrFlushInfo flushInfo;
795 flushInfo.fFinishedContext = finishContext;
796 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -0500797
798 dContext->priv().flushSurface(this->asSurfaceProxy(),
799 SkSurface::BackendSurfaceAccess::kNoAccess,
800 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400801}
802
Adlai Hollerc95b5892020-08-11 12:02:22 -0400803void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(GrDirectContext* dContext,
804 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400805 sk_sp<SkColorSpace> dstColorSpace,
806 const SkIRect& srcRect,
807 SkISize dstSize,
808 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500809 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400810 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400811 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400812 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
813 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
814 SkASSERT(!dstSize.isZero());
815 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
816
Adlai Hollerc95b5892020-08-11 12:02:22 -0400817 if (!dContext) {
818 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400819 return;
820 }
821 auto rt = this->asRenderTargetProxy();
822 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400823 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400824 return;
825 }
826 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400827 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400828 return;
829 }
830 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400831 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400832 return;
833 }
834 int x = srcRect.fLeft;
835 int y = srcRect.fTop;
836 bool needsRescale = srcRect.size() != dstSize;
837 GrSurfaceProxyView srcView;
Brian Salomonbacbb922021-01-21 19:48:00 -0500838 auto info = SkImageInfo::Make(dstSize,
839 kRGBA_8888_SkColorType,
840 this->colorInfo().alphaType(),
841 dstColorSpace);
Brian Salomon63a0a752020-06-26 13:32:09 -0400842 if (needsRescale) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400843 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomonbacbb922021-01-21 19:48:00 -0500844 auto tempFC = this->rescale(info,
845 kTopLeft_GrSurfaceOrigin,
846 srcRect,
847 rescaleGamma,
848 rescaleMode);
849 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400850 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400851 return;
852 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500853 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
854 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400855 x = y = 0;
Brian Salomonbacbb922021-01-21 19:48:00 -0500856 srcView = tempFC->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400857 } else {
858 srcView = this->readSurfaceView();
859 if (!srcView.asTextureProxy()) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500860 srcView = GrSurfaceProxyView::Copy(fContext,
861 std::move(srcView),
862 GrMipmapped::kNo,
863 srcRect,
864 SkBackingFit::kApprox,
865 SkBudgeted::kYes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400866 if (!srcView) {
867 // If we can't get a texture copy of the contents then give up.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400868 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400869 return;
870 }
871 SkASSERT(srcView.asTextureProxy());
872 x = y = 0;
873 }
874 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomonbacbb922021-01-21 19:48:00 -0500875 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo(),
876 info.colorInfo());
Brian Salomon63a0a752020-06-26 13:32:09 -0400877 if (xform) {
878 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
Brian Salomonbacbb922021-01-21 19:48:00 -0500879 auto tempFC = GrSurfaceFillContext::Make(dContext,
880 info,
881 SkBackingFit::kApprox,
882 1,
883 GrMipmapped::kNo,
884 GrProtected::kNo,
885 kTopLeft_GrSurfaceOrigin);
886 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400887 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400888 return;
889 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500890 auto fp = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType());
891 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
892 tempFC->fillRectToRectWithFP(srcRectToDraw,
893 SkIRect::MakeSize(tempFC->dimensions()),
894 std::move(fp));
895 srcView = tempFC->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400896 SkASSERT(srcView.asTextureProxy());
897 x = y = 0;
898 }
899 }
900
Brian Salomonbacbb922021-01-21 19:48:00 -0500901 auto yInfo = SkImageInfo::MakeA8(dstSize);
902 auto yFC = GrSurfaceFillContext::MakeWithFallback(dContext, yInfo, SkBackingFit::kApprox);
903
904 auto uvInfo = yInfo.makeWH(yInfo.width()/2, yInfo.height()/2);
905 auto uFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
906 auto vFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
907
908 if (!yFC || !uFC || !vFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400909 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400910 return;
911 }
912
913 float baseM[20];
914 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
915
916 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
917
918 auto texMatrix = SkMatrix::Translate(x, y);
919
Brian Salomon63a0a752020-06-26 13:32:09 -0400920 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
921 PixelTransferResult yTransfer, uTransfer, vTransfer;
922
923 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
924 float yM[20];
925 std::fill_n(yM, 15, 0.f);
926 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500927
928 auto yFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
929 yFP = GrColorMatrixFragmentProcessor::Make(std::move(yFP),
930 yM,
931 /*unpremulInput=*/false,
932 /*clampRGBOutput=*/true,
933 /*premulOutput=*/false);
934 yFC->fillWithFP(std::move(yFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400935 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500936 yTransfer = yFC->transferPixels(GrColorType::kAlpha_8,
937 SkIRect::MakeSize(yFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400938 if (!yTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400939 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400940 return;
941 }
942 }
943
944 texMatrix.preScale(2.f, 2.f);
945 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
946 float uM[20];
947 std::fill_n(uM, 15, 0.f);
948 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500949
950 auto uFP = GrTextureEffect::Make(srcView,
951 this->colorInfo().alphaType(),
952 texMatrix,
953 GrSamplerState::Filter::kLinear);
954 uFP = GrColorMatrixFragmentProcessor::Make(std::move(uFP),
955 uM,
956 /*unpremulInput=*/false,
957 /*clampRGBOutput=*/true,
958 /*premulOutput=*/false);
959 uFC->fillWithFP(std::move(uFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400960 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500961 uTransfer = uFC->transferPixels(GrColorType::kAlpha_8,
962 SkIRect::MakeSize(uFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400963 if (!uTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400964 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400965 return;
966 }
967 }
968
969 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
970 float vM[20];
971 std::fill_n(vM, 15, 0.f);
972 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500973 auto vFP = GrTextureEffect::Make(std::move(srcView),
974 this->colorInfo().alphaType(),
975 texMatrix,
976 GrSamplerState::Filter::kLinear);
977 vFP = GrColorMatrixFragmentProcessor::Make(std::move(vFP),
978 vM,
979 /*unpremulInput=*/false,
980 /*clampRGBOutput=*/true,
981 /*premulOutput=*/false);
982 vFC->fillWithFP(std::move(vFP));
983
Brian Salomon63a0a752020-06-26 13:32:09 -0400984 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500985 vTransfer = vFC->transferPixels(GrColorType::kAlpha_8,
986 SkIRect::MakeSize(vFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400987 if (!vTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400988 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400989 return;
990 }
991 }
992
993 if (doSynchronousRead) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500994 GrPixmap yPmp = GrPixmap::Allocate(yInfo);
995 GrPixmap uPmp = GrPixmap::Allocate(uvInfo);
996 GrPixmap vPmp = GrPixmap::Allocate(uvInfo);
Brian Salomonbacbb922021-01-21 19:48:00 -0500997 if (!yFC->readPixels(dContext, yPmp, {0, 0}) ||
998 !uFC->readPixels(dContext, uPmp, {0, 0}) ||
999 !vFC->readPixels(dContext, vPmp, {0, 0})) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001000 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001001 return;
1002 }
Adlai Hollerc95b5892020-08-11 12:02:22 -04001003 auto result = std::make_unique<AsyncReadResult>(dContext->priv().contextID());
Brian Salomonbe1084b2021-01-26 13:29:30 -05001004 result->addCpuPlane(yPmp.pixelStorage(), yPmp.rowBytes());
1005 result->addCpuPlane(uPmp.pixelStorage(), uPmp.rowBytes());
1006 result->addCpuPlane(vPmp.pixelStorage(), vPmp.rowBytes());
Adlai Hollerc95b5892020-08-11 12:02:22 -04001007 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -04001008 return;
1009 }
1010
1011 struct FinishContext {
1012 ReadPixelsCallback* fClientCallback;
1013 ReadPixelsContext fClientContext;
1014 GrClientMappedBufferManager* fMappedBufferManager;
1015 SkISize fSize;
1016 PixelTransferResult fYTransfer;
1017 PixelTransferResult fUTransfer;
1018 PixelTransferResult fVTransfer;
1019 };
1020 // Assumption is that the caller would like to flush. We could take a parameter or require an
1021 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1022 // callback to GrGpu until after the next flush that flushes our op list, though.
1023 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -04001024 callbackContext,
1025 dContext->priv().clientMappedBufferManager(),
Brian Salomon63a0a752020-06-26 13:32:09 -04001026 dstSize,
1027 std::move(yTransfer),
1028 std::move(uTransfer),
1029 std::move(vTransfer)};
1030 auto finishCallback = [](GrGpuFinishedContext c) {
1031 const auto* context = reinterpret_cast<const FinishContext*>(c);
1032 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
1033 auto manager = context->fMappedBufferManager;
1034 size_t rowBytes = SkToSizeT(context->fSize.width());
1035 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
1036 (*context->fClientCallback)(context->fClientContext, nullptr);
1037 delete context;
1038 return;
1039 }
1040 rowBytes /= 2;
1041 SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
1042 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
1043 (*context->fClientCallback)(context->fClientContext, nullptr);
1044 delete context;
1045 return;
1046 }
1047 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
1048 (*context->fClientCallback)(context->fClientContext, nullptr);
1049 delete context;
1050 return;
1051 }
1052 (*context->fClientCallback)(context->fClientContext, std::move(result));
1053 delete context;
1054 };
1055 GrFlushInfo flushInfo;
1056 flushInfo.fFinishedContext = finishContext;
1057 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -05001058 dContext->priv().flushSurface(this->asSurfaceProxy(),
1059 SkSurface::BackendSurfaceAccess::kNoAccess,
1060 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -04001061}
1062
Brian Salomond63638b2021-03-05 14:00:07 -05001063sk_sp<GrRenderTask> GrSurfaceContext::copy(sk_sp<GrSurfaceProxy> src,
1064 SkIRect srcRect,
1065 SkIPoint dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001066 ASSERT_SINGLE_OWNER
Brian Salomond63638b2021-03-05 14:00:07 -05001067 RETURN_NULLPTR_IF_ABANDONED
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001068 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -04001069 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -04001070
Brian Salomon947efe22019-07-16 15:36:11 -04001071 const GrCaps* caps = fContext->priv().caps();
1072
Greg Daniel46cfbc62019-06-07 11:43:30 -04001073 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -05001074 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -04001075
Stephen White3c0a50f2020-01-16 18:19:54 -05001076 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomond63638b2021-03-05 14:00:07 -05001077 return nullptr;
Stephen White3c0a50f2020-01-16 18:19:54 -05001078 }
1079
Brian Salomon982127b2021-01-21 10:43:35 -05001080 if (!caps->canCopySurface(this->asSurfaceProxy(), src.get(), srcRect, dstPoint)) {
Brian Salomond63638b2021-03-05 14:00:07 -05001081 return nullptr;
Greg Daniel25af6712018-04-25 10:44:38 -04001082 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001083
Brian Salomon982127b2021-01-21 10:43:35 -05001084 return this->drawingManager()->newCopyRenderTask(std::move(src),
1085 srcRect,
1086 this->asSurfaceProxyRef(),
Brian Salomon0f9f8002021-01-22 16:30:50 -05001087 dstPoint,
1088 this->origin());
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001089}
Greg Daniel46cfbc62019-06-07 11:43:30 -04001090
Brian Salomonbacbb922021-01-21 19:48:00 -05001091std::unique_ptr<GrSurfaceFillContext> GrSurfaceContext::rescale(const GrImageInfo& info,
Brian Salomoneebe7352020-12-09 16:37:04 -05001092 GrSurfaceOrigin origin,
1093 SkIRect srcRect,
1094 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001095 RescaleMode rescaleMode) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001096 auto sfc = GrSurfaceFillContext::MakeWithFallback(fContext,
1097 info,
Brian Salomon1c86b632020-12-11 12:36:01 -05001098 SkBackingFit::kExact,
Brian Salomon1c86b632020-12-11 12:36:01 -05001099 1,
1100 GrMipmapped::kNo,
1101 this->asSurfaceProxy()->isProtected(),
1102 origin);
Brian Salomonbacbb922021-01-21 19:48:00 -05001103 if (!sfc || !this->rescaleInto(sfc.get(),
1104 SkIRect::MakeSize(sfc->dimensions()),
Brian Salomon1c86b632020-12-11 12:36:01 -05001105 srcRect,
1106 rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001107 rescaleMode)) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001108 return nullptr;
1109 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001110 return sfc;
Brian Salomon1c86b632020-12-11 12:36:01 -05001111}
1112
Brian Salomonbacbb922021-01-21 19:48:00 -05001113bool GrSurfaceContext::rescaleInto(GrSurfaceFillContext* dst,
Brian Salomon1c86b632020-12-11 12:36:01 -05001114 SkIRect dstRect,
1115 SkIRect srcRect,
1116 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001117 RescaleMode rescaleMode) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001118 SkASSERT(dst);
1119 if (!SkIRect::MakeSize(dst->dimensions()).contains((dstRect))) {
1120 return false;
1121 }
1122
Brian Salomone9ad9982019-07-22 16:17:41 -04001123 auto rtProxy = this->asRenderTargetProxy();
1124 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001125 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001126 }
1127
Stephen White3c0a50f2020-01-16 18:19:54 -05001128 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001129 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001130 }
1131
Greg Daniel40903af2020-01-30 14:55:05 -05001132 GrSurfaceProxyView texView = this->readSurfaceView();
Greg Daniel40903af2020-01-30 14:55:05 -05001133 if (!texView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -04001134 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect,
Brian Salomonc5243782020-04-02 12:50:34 -04001135 SkBackingFit::kApprox, SkBudgeted::kNo);
1136 if (!texView) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001137 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001138 }
Greg Daniel40903af2020-01-30 14:55:05 -05001139 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001140 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001141 }
1142
Brian Salomon1c86b632020-12-11 12:36:01 -05001143 SkISize finalSize = dstRect.size();
1144
Brian Salomonbf6b9792019-08-21 09:38:10 -04001145 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
1146 // 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 -05001147 std::unique_ptr<GrSurfaceFillContext> tempA;
1148 std::unique_ptr<GrSurfaceFillContext> tempB;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001149
Brian Salomone9ad9982019-07-22 16:17:41 -04001150 // Assume we should ignore the rescale linear request if the surface has no color space since
1151 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon63a0a752020-06-26 13:32:09 -04001152 if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() &&
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001153 !this->colorInfo().colorSpace()->gammaIsLinear()) {
1154 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomone9ad9982019-07-22 16:17:41 -04001155 // We'll fall back to kRGBA_8888 if half float not supported.
Brian Salomonbacbb922021-01-21 19:48:00 -05001156 GrImageInfo ii(GrColorType::kRGBA_F16,
1157 dst->colorInfo().alphaType(),
1158 std::move(cs),
1159 srcRect.size());
1160 auto linearRTC = GrSurfaceFillContext::MakeWithFallback(fContext,
1161 std::move(ii),
1162 SkBackingFit::kApprox,
1163 1,
1164 GrMipmapped::kNo,
1165 GrProtected::kNo,
1166 dst->origin());
Brian Salomone9ad9982019-07-22 16:17:41 -04001167 if (!linearRTC) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001168 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001169 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001170 auto fp = GrTextureEffect::Make(std::move(texView),
1171 this->colorInfo().alphaType(),
1172 SkMatrix::Translate(srcRect.topLeft()),
1173 GrSamplerState::Filter::kNearest,
1174 GrSamplerState::MipmapMode::kNone);
1175 fp = GrColorSpaceXformEffect::Make(std::move(fp),
1176 this->colorInfo(),
1177 linearRTC->colorInfo());
1178 linearRTC->fillWithFP(std::move(fp));
Greg Daniel40903af2020-01-30 14:55:05 -05001179 texView = linearRTC->readSurfaceView();
1180 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -04001181 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001182 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001183 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001184
Brian Salomon1c86b632020-12-11 12:36:01 -05001185 while (srcRect.size() != finalSize) {
1186 SkISize nextDims = finalSize;
Mike Reed1efa14d2021-01-02 21:44:59 -05001187 if (rescaleMode != RescaleMode::kNearest) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001188 if (srcRect.width() > finalSize.width()) {
1189 nextDims.fWidth = std::max((srcRect.width() + 1)/2, finalSize.width());
1190 } else if (srcRect.width() < finalSize.width()) {
1191 nextDims.fWidth = std::min(srcRect.width()*2, finalSize.width());
Brian Salomon59f31b12020-06-04 17:27:15 -04001192 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001193 if (srcRect.height() > finalSize.height()) {
1194 nextDims.fHeight = std::max((srcRect.height() + 1)/2, finalSize.height());
1195 } else if (srcRect.height() < finalSize.height()) {
1196 nextDims.fHeight = std::min(srcRect.height()*2, finalSize.height());
Brian Salomon59f31b12020-06-04 17:27:15 -04001197 }
1198 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001199 auto input = tempA ? tempA.get() : this;
Brian Salomone9ad9982019-07-22 16:17:41 -04001200 sk_sp<GrColorSpaceXform> xform;
Brian Salomonbacbb922021-01-21 19:48:00 -05001201 GrSurfaceFillContext* stepDst;
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001202 SkIRect stepDstRect;
Brian Salomon1c86b632020-12-11 12:36:01 -05001203 if (nextDims == finalSize) {
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001204 stepDst = dst;
1205 stepDstRect = dstRect;
Brian Salomonbacbb922021-01-21 19:48:00 -05001206 xform = GrColorSpaceXform::Make(input->colorInfo(), dst->colorInfo());
Brian Salomon1c86b632020-12-11 12:36:01 -05001207 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -05001208 GrImageInfo nextInfo(input->colorInfo(), nextDims);
1209 tempB = GrSurfaceFillContext::MakeWithFallback(fContext,
1210 nextInfo,
1211 SkBackingFit::kApprox);
Brian Salomon1c86b632020-12-11 12:36:01 -05001212 if (!tempB) {
1213 return false;
1214 }
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001215 stepDst = tempB.get();
1216 stepDstRect = SkIRect::MakeSize(tempB->dimensions());
Brian Salomone9ad9982019-07-22 16:17:41 -04001217 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001218 std::unique_ptr<GrFragmentProcessor> fp;
Mike Reed1efa14d2021-01-02 21:44:59 -05001219 if (rescaleMode == RescaleMode::kRepeatedCubic) {
Brian Salomon59f31b12020-06-04 17:27:15 -04001220 auto dir = GrBicubicEffect::Direction::kXY;
1221 if (nextDims.width() == srcRect.width()) {
1222 dir = GrBicubicEffect::Direction::kY;
1223 } else if (nextDims.height() == srcRect.height()) {
1224 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -04001225 }
Brian Salomon1af72d12020-06-25 10:47:26 -04001226 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
Mike Reed3867c702020-09-01 13:28:10 -04001227 static constexpr auto kKernel = GrBicubicEffect::gCatmullRom;
Brian Salomon1c86b632020-12-11 12:36:01 -05001228 fp = GrBicubicEffect::MakeSubset(std::move(texView),
1229 input->colorInfo().alphaType(),
Brian Salomonbacbb922021-01-21 19:48:00 -05001230 SkMatrix::I(),
Brian Salomon1c86b632020-12-11 12:36:01 -05001231 kWM,
1232 kWM,
1233 SkRect::Make(srcRect),
1234 kKernel,
1235 dir,
1236 *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -04001237 } else {
Mike Reed1efa14d2021-01-02 21:44:59 -05001238 auto filter = rescaleMode == RescaleMode::kNearest ? GrSamplerState::Filter::kNearest
1239 : GrSamplerState::Filter::kLinear;
Brian Salomonbacbb922021-01-21 19:48:00 -05001240 auto srcRectF = SkRect::Make(srcRect);
1241 fp = GrTextureEffect::MakeSubset(std::move(texView),
1242 this->colorInfo().alphaType(),
1243 SkMatrix::I(),
1244 {filter, GrSamplerState::MipmapMode::kNone},
1245 srcRectF,
1246 srcRectF,
1247 *this->caps());
Brian Salomone9ad9982019-07-22 16:17:41 -04001248 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001249 if (xform) {
1250 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
1251 }
1252 stepDst->fillRectToRectWithFP(srcRect, stepDstRect, std::move(fp));
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001253 texView = stepDst->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -04001254 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001255 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomone9ad9982019-07-22 16:17:41 -04001256 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001257 return true;
Brian Salomone9ad9982019-07-22 16:17:41 -04001258}
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001259
1260GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
1261 const SkIRect& rect) {
1262 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1263 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001264 auto direct = fContext->asDirectContext();
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001265 if (!direct) {
1266 return {};
1267 }
1268 auto rtProxy = this->asRenderTargetProxy();
1269 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1270 return {};
1271 }
1272
1273 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001274 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
1275 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001276 // Fail if read color type does not have all of dstCT's color channels and those missing color
1277 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001278 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1279 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
1280 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1281 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001282 return {};
1283 }
1284
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001285 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001286 !supportedRead.fOffsetAlignmentForTransferBuffer) {
1287 return {};
1288 }
1289
1290 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
1291 size_t size = rowBytes * rect.height();
Greg Daniel2e967df2021-02-08 10:38:31 -05001292 // By using kStream_GrAccessPattern here, we are not able to cache and reuse the buffer for
1293 // multiple reads. Switching to kDynamic_GrAccessPattern would allow for this, however doing
1294 // so causes a crash in a chromium test. See skbug.com/11297
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001295 auto buffer = direct->priv().resourceProvider()->createBuffer(
Greg Daniel393debc2021-02-06 03:37:20 +00001296 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001297 if (!buffer) {
1298 return {};
1299 }
1300 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -05001301 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001302 if (flip) {
1303 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1304 this->height() - rect.fTop);
1305 }
Greg Danielbbfec9d2019-08-20 10:56:51 -04001306 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001307 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -04001308 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001309 PixelTransferResult result;
1310 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001311 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -04001312 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001313 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
1314 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001315 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
1316 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001317 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
1318 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -04001319 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001320 };
1321 }
1322 return result;
1323}
Greg Daniel46e366a2019-12-16 14:38:36 -05001324
1325#ifdef SK_DEBUG
1326void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001327 SkASSERT(fReadView.proxy());
1328 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -04001329 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
1330 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
1331 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
1332 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001333 this->onValidate();
1334}
1335#endif