blob: 7c84826e171451254deb94b01efbcc1d19ba2f83 [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
Michael Ludwiga5c90582021-03-09 15:35:32 -0500375 if (src.rowBytes() % src.info().bpp()) {
376 return false;
377 }
378
Brian Salomondd4087d2020-12-23 20:36:44 -0500379 src = src.clip(this->dimensions(), &pt);
380 if (!src.hasPixels()) {
Brian Salomon1d435302019-07-01 13:05:28 -0400381 return false;
382 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500383 if (!alpha_types_compatible(src.alphaType(), this->colorInfo().alphaType())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400384 return false;
385 }
386
387 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
Stephen White3c0a50f2020-01-16 18:19:54 -0500388
389 if (dstProxy->framebufferOnly()) {
390 return false;
391 }
392
Adlai Hollerc95b5892020-08-11 12:02:22 -0400393 if (!dstProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400394 return false;
395 }
396
397 GrSurface* dstSurface = dstProxy->peekSurface();
398
Brian Salomondd4087d2020-12-23 20:36:44 -0500399 SkColorSpaceXformSteps::Flags flags =
400 SkColorSpaceXformSteps{src.info(), this->colorInfo()}.flags;
Mike Klein7321e6a2019-12-03 11:08:40 -0600401 bool unpremul = flags.unpremul,
402 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
403 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400404
Adlai Hollerc95b5892020-08-11 12:02:22 -0400405 const GrCaps* caps = dContext->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400406
407 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
408 GrRenderable::kNo);
409
Greg Danielc71c7962020-01-14 16:44:18 -0500410 GrColorType dstColorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400411 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
412 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400413 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
Brian Salomondd4087d2020-12-23 20:36:44 -0500414 (src.colorType() == GrColorType::kRGBA_8888 ||
415 src.colorType() == GrColorType::kBGRA_8888) &&
Brian Salomon590f5672020-12-16 11:44:47 -0500416 this->asFillContext() &&
Greg Danielc71c7962020-01-14 16:44:18 -0500417 (dstColorType == GrColorType::kRGBA_8888 ||
418 dstColorType == GrColorType::kBGRA_8888) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400419 rgbaDefaultFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400420 dContext->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400421
422 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500423 GrColorInfo tempColorInfo;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400424 GrBackendFormat format;
Greg Danielbfa19c42019-12-19 16:41:40 -0500425 GrSwizzle tempReadSwizzle;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400426 if (canvas2DFastPath) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500427 tempColorInfo = {GrColorType::kRGBA_8888,
428 kUnpremul_SkAlphaType,
429 this->colorInfo().refColorSpace()};
Greg Daniel7bfc9132019-08-14 14:23:53 -0400430 format = rgbaDefaultFormat;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400431 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500432 tempColorInfo = this->colorInfo();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400433 format = dstProxy->backendFormat().makeTexture2D();
434 if (!format.isValid()) {
435 return false;
436 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500437 tempReadSwizzle = this->readSwizzle();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400438 }
439
Greg Daniel2e52ad12019-06-13 10:04:16 -0400440 // It is more efficient for us to write pixels into a top left origin so we prefer that.
441 // However, if the final proxy isn't a render target then we must use a copy to move the
442 // data into it which requires the origins to match. If the final proxy is a render target
443 // we can use a draw instead which doesn't have this origin restriction. Thus for render
444 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400445 GrSurfaceOrigin tempOrigin =
Brian Salomon590f5672020-12-16 11:44:47 -0500446 this->asFillContext() ? kTopLeft_GrSurfaceOrigin : this->origin();
Adlai Hollerc95b5892020-08-11 12:02:22 -0400447 auto tempProxy = dContext->priv().proxyProvider()->createProxy(
Brian Salomondd4087d2020-12-23 20:36:44 -0500448 format, src.dimensions(), GrRenderable::kNo, 1, GrMipmapped::kNo,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400449 SkBackingFit::kApprox, SkBudgeted::kYes, GrProtected::kNo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400450 if (!tempProxy) {
451 return false;
452 }
Greg Daniel3912a4b2020-01-14 09:56:04 -0500453 GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500454 GrSurfaceContext tempCtx(dContext, tempView, tempColorInfo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400455
456 // In the fast path we always write the srcData to the temp context as though it were RGBA.
457 // When the data is really BGRA the write will cause the R and B channels to be swapped in
458 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
459 // dst.
Brian Salomondd4087d2020-12-23 20:36:44 -0500460 GrColorType origSrcColorType = src.colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400461 if (canvas2DFastPath) {
Brian Salomondd4087d2020-12-23 20:36:44 -0500462 src = {src.info().makeColorType(GrColorType::kRGBA_8888), src.addr(), src.rowBytes()};
Brian Salomon1d435302019-07-01 13:05:28 -0400463 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500464 if (!tempCtx.writePixels(dContext, src, {0, 0})) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400465 return false;
466 }
467
Brian Salomon590f5672020-12-16 11:44:47 -0500468 if (this->asFillContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400469 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400470 if (canvas2DFastPath) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400471 fp = dContext->priv().createUPMToPMEffect(
Brian Salomon14f99fc2020-12-07 12:19:47 -0500472 GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType()));
Brian Salomon1d435302019-07-01 13:05:28 -0400473 // Important: check the original src color type here!
Brian Salomondd4087d2020-12-23 20:36:44 -0500474 if (origSrcColorType == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400475 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
476 }
477 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500478 fp = GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400479 }
480 if (!fp) {
481 return false;
482 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500483 this->asFillContext()->fillRectToRectWithFP(SkIRect::MakeSize(src.dimensions()),
484 SkIRect::MakePtSize(pt, src.dimensions()),
485 std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400486 } else {
Brian Salomondd4087d2020-12-23 20:36:44 -0500487 SkIRect srcRect = SkIRect::MakeSize(src.dimensions());
Brian Salomon1d435302019-07-01 13:05:28 -0400488 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Brian Salomon982127b2021-01-21 10:43:35 -0500489 if (!this->copy(std::move(tempProxy), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400490 return false;
491 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400492 }
493 return true;
494 }
495
Brian Salomon1d435302019-07-01 13:05:28 -0400496 GrColorType allowedColorType =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400497 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400498 dstProxy->backendFormat(),
Brian Salomondd4087d2020-12-23 20:36:44 -0500499 src.colorType()).fColorType;
Greg Danielb8d84f82020-02-13 14:25:00 -0500500 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomondd4087d2020-12-23 20:36:44 -0500501 bool makeTight = !caps->writePixelsRowBytesSupport() &&
502 src.rowBytes() != src.info().minRowBytes();
Brian Salomon1047a492019-07-02 12:25:21 -0400503 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomondd4087d2020-12-23 20:36:44 -0500504 (src.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400505
Brian Salomon24949422021-02-11 09:39:46 -0500506 if (convert) {
Brian Salomondd4087d2020-12-23 20:36:44 -0500507 GrImageInfo tmpInfo(allowedColorType,
508 this->colorInfo().alphaType(),
509 this->colorInfo().refColorSpace(),
510 src.dimensions());
Brian Salomonbe1084b2021-01-26 13:29:30 -0500511 GrPixmap tmp = GrPixmap::Allocate(tmpInfo);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400512
Brian Salomondd4087d2020-12-23 20:36:44 -0500513 SkAssertResult(GrConvertPixels(tmp, src, flip));
Brian Salomonf30b1c12019-06-20 12:25:02 -0400514
Brian Salomondd4087d2020-12-23 20:36:44 -0500515 src = tmp;
Brian Salomon1d435302019-07-01 13:05:28 -0400516 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400517 }
518
Brian Salomonbe1084b2021-01-26 13:29:30 -0500519 GrMipLevel level;
520 level.fPixels = src.addr();
521 level.fRowBytes = src.rowBytes();
Brian Salomon24949422021-02-11 09:39:46 -0500522 bool result = dContext->priv().drawingManager()->newWritePixelsTask(
Brian Salomonbe1084b2021-01-26 13:29:30 -0500523 this->asSurfaceProxyRef(),
524 SkIRect::MakePtSize(pt, src.dimensions()),
525 src.colorType(),
526 dstColorType,
527 &level,
528 1,
529 src.pixelStorage());
Brian Salomon24949422021-02-11 09:39:46 -0500530 if (result && !src.ownsPixels()) {
531 // If the pixmap doesn't own its pixels then we must flush so that they are pushed to
532 // the GPU driver before we return.
533 dContext->priv().flushSurface(dstProxy);
534 }
535 return result;
Brian Osman45580d32016-11-23 09:37:01 -0500536}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400537
Adlai Hollerc95b5892020-08-11 12:02:22 -0400538void GrSurfaceContext::asyncRescaleAndReadPixels(GrDirectContext* dContext,
539 const SkImageInfo& info,
Brian Salomon63a0a752020-06-26 13:32:09 -0400540 const SkIRect& srcRect,
541 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500542 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400543 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400544 ReadPixelsContext callbackContext) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400545 if (!dContext) {
546 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400547 return;
548 }
549 auto rt = this->asRenderTargetProxy();
550 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400551 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400552 return;
553 }
554 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400555 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400556 return;
557 }
558 auto dstCT = SkColorTypeToGrColorType(info.colorType());
559 if (dstCT == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400560 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400561 return;
562 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500563 bool needsRescale = srcRect.size() != info.dimensions();
Brian Salomon63a0a752020-06-26 13:32:09 -0400564 auto colorTypeOfFinalContext = this->colorInfo().colorType();
565 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
566 if (needsRescale) {
567 colorTypeOfFinalContext = dstCT;
568 backendFormatOfFinalContext =
569 this->caps()->getDefaultBackendFormat(dstCT, GrRenderable::kYes);
570 }
571 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonbacbb922021-01-21 19:48:00 -0500572 backendFormatOfFinalContext,
573 dstCT);
Brian Salomon63a0a752020-06-26 13:32:09 -0400574 // Fail if we can't read from the source surface's color type.
575 if (readInfo.fColorType == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400576 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400577 return;
578 }
579 // Fail if read color type does not have all of dstCT's color channels and those missing color
580 // channels are in the src.
581 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
582 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
583 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
584 if ((~legalReadChannels & dstChannels) & srcChannels) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400585 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400586 return;
587 }
588
Brian Salomonbacbb922021-01-21 19:48:00 -0500589 std::unique_ptr<GrSurfaceFillContext> tempFC;
Brian Salomon63a0a752020-06-26 13:32:09 -0400590 int x = srcRect.fLeft;
591 int y = srcRect.fTop;
592 if (needsRescale) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500593 tempFC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma, rescaleMode);
594 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400595 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400596 return;
597 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500598 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
599 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400600 x = y = 0;
601 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -0500602 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo(),
603 info.colorInfo());
Brian Salomon63a0a752020-06-26 13:32:09 -0400604 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
605 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
606 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomonbacbb922021-01-21 19:48:00 -0500607 SkIRect srcRectToDraw = srcRect;
Brian Salomon63a0a752020-06-26 13:32:09 -0400608 // If the src is not texturable first try to make a copy to a texture.
609 if (!texProxyView.asTextureProxy()) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500610 texProxyView = GrSurfaceProxyView::Copy(fContext,
611 texProxyView,
612 GrMipmapped::kNo,
613 srcRect,
614 SkBackingFit::kApprox,
615 SkBudgeted::kNo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400616 if (!texProxyView) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400617 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400618 return;
619 }
620 SkASSERT(texProxyView.asTextureProxy());
Brian Salomonbacbb922021-01-21 19:48:00 -0500621 srcRectToDraw = SkIRect::MakeSize(srcRect.size());
Brian Salomon63a0a752020-06-26 13:32:09 -0400622 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500623 auto tempInfo = GrImageInfo(info).makeColorType(this->colorInfo().colorType());
624 tempFC = GrSurfaceFillContext::Make(dContext, tempInfo, SkBackingFit::kApprox);
625 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400626 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400627 return;
628 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500629 auto fp = GrTextureEffect::Make(std::move(texProxyView), this->colorInfo().alphaType());
630 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
631 tempFC->fillRectToRectWithFP(srcRectToDraw,
632 SkIRect::MakeSize(tempFC->dimensions()),
633 std::move(fp));
Brian Salomon63a0a752020-06-26 13:32:09 -0400634 x = y = 0;
635 }
636 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500637 auto srcCtx = tempFC ? tempFC.get() : this;
638 return srcCtx->asyncReadPixels(dContext,
639 SkIRect::MakePtSize({x, y}, info.dimensions()),
640 info.colorType(),
641 callback,
642 callbackContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400643}
644
645class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult {
646public:
Robert Phillips82ad7af2021-03-11 16:00:10 -0500647 AsyncReadResult(GrDirectContext::DirectContextID intendedRecipient)
648 : fIntendedRecipient(intendedRecipient) {
649 }
650
Brian Salomon63a0a752020-06-26 13:32:09 -0400651 ~AsyncReadResult() override {
652 for (int i = 0; i < fPlanes.count(); ++i) {
Robert Phillips82ad7af2021-03-11 16:00:10 -0500653 fPlanes[i].releaseMappedBuffer(fIntendedRecipient);
Brian Salomon63a0a752020-06-26 13:32:09 -0400654 }
655 }
656
657 int count() const override { return fPlanes.count(); }
Brian Salomonbe1084b2021-01-26 13:29:30 -0500658 const void* data(int i) const override { return fPlanes[i].data(); }
659 size_t rowBytes(int i) const override { return fPlanes[i].rowBytes(); }
Brian Salomon63a0a752020-06-26 13:32:09 -0400660
661 bool addTransferResult(const PixelTransferResult& result,
662 SkISize dimensions,
663 size_t rowBytes,
664 GrClientMappedBufferManager* manager) {
665 SkASSERT(!result.fTransferBuffer->isMapped());
666 const void* mappedData = result.fTransferBuffer->map();
667 if (!mappedData) {
668 return false;
669 }
670 if (result.fPixelConverter) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500671 size_t size = rowBytes*dimensions.height();
672 sk_sp<SkData> data = SkData::MakeUninitialized(size);
673 result.fPixelConverter(data->writable_data(), mappedData);
674 this->addCpuPlane(std::move(data), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400675 result.fTransferBuffer->unmap();
676 } else {
677 manager->insert(result.fTransferBuffer);
678 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
679 }
680 return true;
681 }
682
Brian Salomonbe1084b2021-01-26 13:29:30 -0500683 void addCpuPlane(sk_sp<SkData> data, size_t rowBytes) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400684 SkASSERT(data);
685 SkASSERT(rowBytes > 0);
Brian Salomonbe1084b2021-01-26 13:29:30 -0500686 fPlanes.emplace_back(std::move(data), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400687 }
688
689private:
690 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
691 SkASSERT(data);
692 SkASSERT(rowBytes > 0);
693 SkASSERT(mappedBuffer);
694 SkASSERT(mappedBuffer->isMapped());
Brian Salomonbe1084b2021-01-26 13:29:30 -0500695 fPlanes.emplace_back(std::move(mappedBuffer), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400696 }
697
Brian Salomonbe1084b2021-01-26 13:29:30 -0500698 class Plane {
699 public:
700 Plane(sk_sp<GrGpuBuffer> buffer, size_t rowBytes)
701 : fMappedBuffer(std::move(buffer)), fRowBytes(rowBytes) {}
702 Plane(sk_sp<SkData> data, size_t rowBytes) : fData(std::move(data)), fRowBytes(rowBytes) {}
703
704 Plane(const Plane&) = delete;
705 Plane(Plane&&) = default;
706
707 ~Plane() { SkASSERT(!fMappedBuffer); }
708
709 Plane& operator=(const Plane&) = delete;
710 Plane& operator=(Plane&&) = default;
711
Robert Phillips82ad7af2021-03-11 16:00:10 -0500712 void releaseMappedBuffer(GrDirectContext::DirectContextID intendedRecipient) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500713 if (fMappedBuffer) {
714 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
Robert Phillips82ad7af2021-03-11 16:00:10 -0500715 {std::move(fMappedBuffer), intendedRecipient});
Brian Salomonbe1084b2021-01-26 13:29:30 -0500716 }
717 }
718
719 const void* data() const {
720 if (fMappedBuffer) {
721 SkASSERT(!fData);
722 SkASSERT(fMappedBuffer->isMapped());
723 return fMappedBuffer->map();
724 }
725 SkASSERT(fData);
726 return fData->data();
727 }
728
729 size_t rowBytes() const { return fRowBytes; }
730
731 private:
732 sk_sp<SkData> fData;
Brian Salomon1eea1ea2021-01-26 18:12:25 +0000733 sk_sp<GrGpuBuffer> fMappedBuffer;
Brian Salomonbe1084b2021-01-26 13:29:30 -0500734 size_t fRowBytes;
Brian Salomon63a0a752020-06-26 13:32:09 -0400735 };
736 SkSTArray<3, Plane> fPlanes;
Robert Phillips82ad7af2021-03-11 16:00:10 -0500737 GrDirectContext::DirectContextID fIntendedRecipient;
Brian Salomon63a0a752020-06-26 13:32:09 -0400738};
739
Adlai Hollerc95b5892020-08-11 12:02:22 -0400740void GrSurfaceContext::asyncReadPixels(GrDirectContext* dContext,
741 const SkIRect& rect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400742 SkColorType colorType,
743 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400744 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400745 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
746 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
747
Adlai Hollerc95b5892020-08-11 12:02:22 -0400748 if (!dContext || this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
749 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400750 return;
751 }
752
Adlai Hollerc95b5892020-08-11 12:02:22 -0400753 auto mappedBufferManager = dContext->priv().clientMappedBufferManager();
Brian Salomon63a0a752020-06-26 13:32:09 -0400754
755 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
756
757 if (!transferResult.fTransferBuffer) {
758 auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
759 this->colorInfo().refColorSpace());
Robert Phillips82ad7af2021-03-11 16:00:10 -0500760 static const GrDirectContext::DirectContextID kInvalid;
761 auto result = std::make_unique<AsyncReadResult>(kInvalid);
Brian Salomonbe1084b2021-01-26 13:29:30 -0500762 GrPixmap pm = GrPixmap::Allocate(ii);
763 result->addCpuPlane(pm.pixelStorage(), pm.rowBytes());
Brian Salomon63a0a752020-06-26 13:32:09 -0400764
Adlai Hollerc95b5892020-08-11 12:02:22 -0400765 SkIPoint pt{rect.fLeft, rect.fTop};
Brian Salomondd4087d2020-12-23 20:36:44 -0500766 if (!this->readPixels(dContext, pm, pt)) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400767 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400768 return;
769 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400770 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400771 return;
772 }
773
774 struct FinishContext {
775 ReadPixelsCallback* fClientCallback;
776 ReadPixelsContext fClientContext;
777 SkISize fSize;
778 SkColorType fColorType;
779 GrClientMappedBufferManager* fMappedBufferManager;
780 PixelTransferResult fTransferResult;
781 };
782 // Assumption is that the caller would like to flush. We could take a parameter or require an
783 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
784 // callback to GrGpu until after the next flush that flushes our op list, though.
785 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400786 callbackContext,
Brian Salomon63a0a752020-06-26 13:32:09 -0400787 rect.size(),
788 colorType,
789 mappedBufferManager,
790 std::move(transferResult)};
791 auto finishCallback = [](GrGpuFinishedContext c) {
792 const auto* context = reinterpret_cast<const FinishContext*>(c);
Robert Phillips82ad7af2021-03-11 16:00:10 -0500793 auto manager = context->fMappedBufferManager;
794 auto result = std::make_unique<AsyncReadResult>(manager->owningDirectContext());
Brian Salomon63a0a752020-06-26 13:32:09 -0400795 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
796 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
Robert Phillips82ad7af2021-03-11 16:00:10 -0500797 manager)) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400798 result.reset();
799 }
800 (*context->fClientCallback)(context->fClientContext, std::move(result));
801 delete context;
802 };
803 GrFlushInfo flushInfo;
804 flushInfo.fFinishedContext = finishContext;
805 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -0500806
807 dContext->priv().flushSurface(this->asSurfaceProxy(),
808 SkSurface::BackendSurfaceAccess::kNoAccess,
809 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400810}
811
Adlai Hollerc95b5892020-08-11 12:02:22 -0400812void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(GrDirectContext* dContext,
813 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400814 sk_sp<SkColorSpace> dstColorSpace,
815 const SkIRect& srcRect,
816 SkISize dstSize,
817 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500818 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400819 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400820 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400821 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
822 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
823 SkASSERT(!dstSize.isZero());
824 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
825
Adlai Hollerc95b5892020-08-11 12:02:22 -0400826 if (!dContext) {
827 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400828 return;
829 }
830 auto rt = this->asRenderTargetProxy();
831 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400832 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400833 return;
834 }
835 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400836 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400837 return;
838 }
839 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400840 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400841 return;
842 }
843 int x = srcRect.fLeft;
844 int y = srcRect.fTop;
845 bool needsRescale = srcRect.size() != dstSize;
846 GrSurfaceProxyView srcView;
Brian Salomonbacbb922021-01-21 19:48:00 -0500847 auto info = SkImageInfo::Make(dstSize,
848 kRGBA_8888_SkColorType,
849 this->colorInfo().alphaType(),
850 dstColorSpace);
Brian Salomon63a0a752020-06-26 13:32:09 -0400851 if (needsRescale) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400852 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomonbacbb922021-01-21 19:48:00 -0500853 auto tempFC = this->rescale(info,
854 kTopLeft_GrSurfaceOrigin,
855 srcRect,
856 rescaleGamma,
857 rescaleMode);
858 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400859 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400860 return;
861 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500862 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
863 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400864 x = y = 0;
Brian Salomonbacbb922021-01-21 19:48:00 -0500865 srcView = tempFC->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400866 } else {
867 srcView = this->readSurfaceView();
868 if (!srcView.asTextureProxy()) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500869 srcView = GrSurfaceProxyView::Copy(fContext,
870 std::move(srcView),
871 GrMipmapped::kNo,
872 srcRect,
873 SkBackingFit::kApprox,
874 SkBudgeted::kYes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400875 if (!srcView) {
876 // If we can't get a texture copy of the contents then give up.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400877 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400878 return;
879 }
880 SkASSERT(srcView.asTextureProxy());
881 x = y = 0;
882 }
883 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomonbacbb922021-01-21 19:48:00 -0500884 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo(),
885 info.colorInfo());
Brian Salomon63a0a752020-06-26 13:32:09 -0400886 if (xform) {
887 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
Brian Salomonbacbb922021-01-21 19:48:00 -0500888 auto tempFC = GrSurfaceFillContext::Make(dContext,
889 info,
890 SkBackingFit::kApprox,
891 1,
892 GrMipmapped::kNo,
893 GrProtected::kNo,
894 kTopLeft_GrSurfaceOrigin);
895 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400896 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400897 return;
898 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500899 auto fp = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType());
900 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
901 tempFC->fillRectToRectWithFP(srcRectToDraw,
902 SkIRect::MakeSize(tempFC->dimensions()),
903 std::move(fp));
904 srcView = tempFC->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400905 SkASSERT(srcView.asTextureProxy());
906 x = y = 0;
907 }
908 }
909
Brian Salomonbacbb922021-01-21 19:48:00 -0500910 auto yInfo = SkImageInfo::MakeA8(dstSize);
911 auto yFC = GrSurfaceFillContext::MakeWithFallback(dContext, yInfo, SkBackingFit::kApprox);
912
913 auto uvInfo = yInfo.makeWH(yInfo.width()/2, yInfo.height()/2);
914 auto uFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
915 auto vFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
916
917 if (!yFC || !uFC || !vFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400918 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400919 return;
920 }
921
922 float baseM[20];
923 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
924
925 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
926
927 auto texMatrix = SkMatrix::Translate(x, y);
928
Brian Salomon63a0a752020-06-26 13:32:09 -0400929 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
930 PixelTransferResult yTransfer, uTransfer, vTransfer;
931
932 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
933 float yM[20];
934 std::fill_n(yM, 15, 0.f);
935 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500936
937 auto yFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
938 yFP = GrColorMatrixFragmentProcessor::Make(std::move(yFP),
939 yM,
940 /*unpremulInput=*/false,
941 /*clampRGBOutput=*/true,
942 /*premulOutput=*/false);
943 yFC->fillWithFP(std::move(yFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400944 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500945 yTransfer = yFC->transferPixels(GrColorType::kAlpha_8,
946 SkIRect::MakeSize(yFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400947 if (!yTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400948 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400949 return;
950 }
951 }
952
953 texMatrix.preScale(2.f, 2.f);
954 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
955 float uM[20];
956 std::fill_n(uM, 15, 0.f);
957 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500958
959 auto uFP = GrTextureEffect::Make(srcView,
960 this->colorInfo().alphaType(),
961 texMatrix,
962 GrSamplerState::Filter::kLinear);
963 uFP = GrColorMatrixFragmentProcessor::Make(std::move(uFP),
964 uM,
965 /*unpremulInput=*/false,
966 /*clampRGBOutput=*/true,
967 /*premulOutput=*/false);
968 uFC->fillWithFP(std::move(uFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400969 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500970 uTransfer = uFC->transferPixels(GrColorType::kAlpha_8,
971 SkIRect::MakeSize(uFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400972 if (!uTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400973 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400974 return;
975 }
976 }
977
978 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
979 float vM[20];
980 std::fill_n(vM, 15, 0.f);
981 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500982 auto vFP = GrTextureEffect::Make(std::move(srcView),
983 this->colorInfo().alphaType(),
984 texMatrix,
985 GrSamplerState::Filter::kLinear);
986 vFP = GrColorMatrixFragmentProcessor::Make(std::move(vFP),
987 vM,
988 /*unpremulInput=*/false,
989 /*clampRGBOutput=*/true,
990 /*premulOutput=*/false);
991 vFC->fillWithFP(std::move(vFP));
992
Brian Salomon63a0a752020-06-26 13:32:09 -0400993 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500994 vTransfer = vFC->transferPixels(GrColorType::kAlpha_8,
995 SkIRect::MakeSize(vFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400996 if (!vTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400997 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400998 return;
999 }
1000 }
1001
1002 if (doSynchronousRead) {
Brian Salomonbe1084b2021-01-26 13:29:30 -05001003 GrPixmap yPmp = GrPixmap::Allocate(yInfo);
1004 GrPixmap uPmp = GrPixmap::Allocate(uvInfo);
1005 GrPixmap vPmp = GrPixmap::Allocate(uvInfo);
Brian Salomonbacbb922021-01-21 19:48:00 -05001006 if (!yFC->readPixels(dContext, yPmp, {0, 0}) ||
1007 !uFC->readPixels(dContext, uPmp, {0, 0}) ||
1008 !vFC->readPixels(dContext, vPmp, {0, 0})) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001009 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001010 return;
1011 }
Robert Phillips82ad7af2021-03-11 16:00:10 -05001012 auto result = std::make_unique<AsyncReadResult>(dContext->directContextID());
Brian Salomonbe1084b2021-01-26 13:29:30 -05001013 result->addCpuPlane(yPmp.pixelStorage(), yPmp.rowBytes());
1014 result->addCpuPlane(uPmp.pixelStorage(), uPmp.rowBytes());
1015 result->addCpuPlane(vPmp.pixelStorage(), vPmp.rowBytes());
Adlai Hollerc95b5892020-08-11 12:02:22 -04001016 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -04001017 return;
1018 }
1019
1020 struct FinishContext {
1021 ReadPixelsCallback* fClientCallback;
1022 ReadPixelsContext fClientContext;
1023 GrClientMappedBufferManager* fMappedBufferManager;
1024 SkISize fSize;
1025 PixelTransferResult fYTransfer;
1026 PixelTransferResult fUTransfer;
1027 PixelTransferResult fVTransfer;
1028 };
1029 // Assumption is that the caller would like to flush. We could take a parameter or require an
1030 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1031 // callback to GrGpu until after the next flush that flushes our op list, though.
1032 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -04001033 callbackContext,
1034 dContext->priv().clientMappedBufferManager(),
Brian Salomon63a0a752020-06-26 13:32:09 -04001035 dstSize,
1036 std::move(yTransfer),
1037 std::move(uTransfer),
1038 std::move(vTransfer)};
1039 auto finishCallback = [](GrGpuFinishedContext c) {
1040 const auto* context = reinterpret_cast<const FinishContext*>(c);
Brian Salomon63a0a752020-06-26 13:32:09 -04001041 auto manager = context->fMappedBufferManager;
Robert Phillips82ad7af2021-03-11 16:00:10 -05001042 auto result = std::make_unique<AsyncReadResult>(manager->owningDirectContext());
Brian Salomon63a0a752020-06-26 13:32:09 -04001043 size_t rowBytes = SkToSizeT(context->fSize.width());
1044 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
1045 (*context->fClientCallback)(context->fClientContext, nullptr);
1046 delete context;
1047 return;
1048 }
1049 rowBytes /= 2;
1050 SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
1051 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
1052 (*context->fClientCallback)(context->fClientContext, nullptr);
1053 delete context;
1054 return;
1055 }
1056 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
1057 (*context->fClientCallback)(context->fClientContext, nullptr);
1058 delete context;
1059 return;
1060 }
1061 (*context->fClientCallback)(context->fClientContext, std::move(result));
1062 delete context;
1063 };
1064 GrFlushInfo flushInfo;
1065 flushInfo.fFinishedContext = finishContext;
1066 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -05001067 dContext->priv().flushSurface(this->asSurfaceProxy(),
1068 SkSurface::BackendSurfaceAccess::kNoAccess,
1069 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -04001070}
1071
Brian Salomond63638b2021-03-05 14:00:07 -05001072sk_sp<GrRenderTask> GrSurfaceContext::copy(sk_sp<GrSurfaceProxy> src,
1073 SkIRect srcRect,
1074 SkIPoint dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001075 ASSERT_SINGLE_OWNER
Brian Salomond63638b2021-03-05 14:00:07 -05001076 RETURN_NULLPTR_IF_ABANDONED
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001077 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -04001078 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -04001079
Brian Salomon947efe22019-07-16 15:36:11 -04001080 const GrCaps* caps = fContext->priv().caps();
1081
Greg Daniel46cfbc62019-06-07 11:43:30 -04001082 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -05001083 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -04001084
Stephen White3c0a50f2020-01-16 18:19:54 -05001085 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomond63638b2021-03-05 14:00:07 -05001086 return nullptr;
Stephen White3c0a50f2020-01-16 18:19:54 -05001087 }
1088
Brian Salomon982127b2021-01-21 10:43:35 -05001089 if (!caps->canCopySurface(this->asSurfaceProxy(), src.get(), srcRect, dstPoint)) {
Brian Salomond63638b2021-03-05 14:00:07 -05001090 return nullptr;
Greg Daniel25af6712018-04-25 10:44:38 -04001091 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001092
Brian Salomon982127b2021-01-21 10:43:35 -05001093 return this->drawingManager()->newCopyRenderTask(std::move(src),
1094 srcRect,
1095 this->asSurfaceProxyRef(),
Brian Salomon0f9f8002021-01-22 16:30:50 -05001096 dstPoint,
1097 this->origin());
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001098}
Greg Daniel46cfbc62019-06-07 11:43:30 -04001099
Brian Salomonbacbb922021-01-21 19:48:00 -05001100std::unique_ptr<GrSurfaceFillContext> GrSurfaceContext::rescale(const GrImageInfo& info,
Brian Salomoneebe7352020-12-09 16:37:04 -05001101 GrSurfaceOrigin origin,
1102 SkIRect srcRect,
1103 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001104 RescaleMode rescaleMode) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001105 auto sfc = GrSurfaceFillContext::MakeWithFallback(fContext,
1106 info,
Brian Salomon1c86b632020-12-11 12:36:01 -05001107 SkBackingFit::kExact,
Brian Salomon1c86b632020-12-11 12:36:01 -05001108 1,
1109 GrMipmapped::kNo,
1110 this->asSurfaceProxy()->isProtected(),
1111 origin);
Brian Salomonbacbb922021-01-21 19:48:00 -05001112 if (!sfc || !this->rescaleInto(sfc.get(),
1113 SkIRect::MakeSize(sfc->dimensions()),
Brian Salomon1c86b632020-12-11 12:36:01 -05001114 srcRect,
1115 rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001116 rescaleMode)) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001117 return nullptr;
1118 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001119 return sfc;
Brian Salomon1c86b632020-12-11 12:36:01 -05001120}
1121
Brian Salomonbacbb922021-01-21 19:48:00 -05001122bool GrSurfaceContext::rescaleInto(GrSurfaceFillContext* dst,
Brian Salomon1c86b632020-12-11 12:36:01 -05001123 SkIRect dstRect,
1124 SkIRect srcRect,
1125 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001126 RescaleMode rescaleMode) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001127 SkASSERT(dst);
1128 if (!SkIRect::MakeSize(dst->dimensions()).contains((dstRect))) {
1129 return false;
1130 }
1131
Brian Salomone9ad9982019-07-22 16:17:41 -04001132 auto rtProxy = this->asRenderTargetProxy();
1133 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001134 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001135 }
1136
Stephen White3c0a50f2020-01-16 18:19:54 -05001137 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001138 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001139 }
1140
Greg Daniel40903af2020-01-30 14:55:05 -05001141 GrSurfaceProxyView texView = this->readSurfaceView();
Greg Daniel40903af2020-01-30 14:55:05 -05001142 if (!texView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -04001143 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect,
Brian Salomonc5243782020-04-02 12:50:34 -04001144 SkBackingFit::kApprox, SkBudgeted::kNo);
1145 if (!texView) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001146 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001147 }
Greg Daniel40903af2020-01-30 14:55:05 -05001148 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001149 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001150 }
1151
Brian Salomon1c86b632020-12-11 12:36:01 -05001152 SkISize finalSize = dstRect.size();
1153
Brian Salomonbf6b9792019-08-21 09:38:10 -04001154 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
1155 // 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 -05001156 std::unique_ptr<GrSurfaceFillContext> tempA;
1157 std::unique_ptr<GrSurfaceFillContext> tempB;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001158
Brian Salomone9ad9982019-07-22 16:17:41 -04001159 // Assume we should ignore the rescale linear request if the surface has no color space since
1160 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon63a0a752020-06-26 13:32:09 -04001161 if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() &&
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001162 !this->colorInfo().colorSpace()->gammaIsLinear()) {
1163 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomone9ad9982019-07-22 16:17:41 -04001164 // We'll fall back to kRGBA_8888 if half float not supported.
Brian Salomonbacbb922021-01-21 19:48:00 -05001165 GrImageInfo ii(GrColorType::kRGBA_F16,
1166 dst->colorInfo().alphaType(),
1167 std::move(cs),
1168 srcRect.size());
1169 auto linearRTC = GrSurfaceFillContext::MakeWithFallback(fContext,
1170 std::move(ii),
1171 SkBackingFit::kApprox,
1172 1,
1173 GrMipmapped::kNo,
1174 GrProtected::kNo,
1175 dst->origin());
Brian Salomone9ad9982019-07-22 16:17:41 -04001176 if (!linearRTC) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001177 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001178 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001179 auto fp = GrTextureEffect::Make(std::move(texView),
1180 this->colorInfo().alphaType(),
1181 SkMatrix::Translate(srcRect.topLeft()),
1182 GrSamplerState::Filter::kNearest,
1183 GrSamplerState::MipmapMode::kNone);
1184 fp = GrColorSpaceXformEffect::Make(std::move(fp),
1185 this->colorInfo(),
1186 linearRTC->colorInfo());
1187 linearRTC->fillWithFP(std::move(fp));
Greg Daniel40903af2020-01-30 14:55:05 -05001188 texView = linearRTC->readSurfaceView();
1189 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -04001190 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001191 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001192 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001193
Brian Salomon1c86b632020-12-11 12:36:01 -05001194 while (srcRect.size() != finalSize) {
1195 SkISize nextDims = finalSize;
Mike Reed1efa14d2021-01-02 21:44:59 -05001196 if (rescaleMode != RescaleMode::kNearest) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001197 if (srcRect.width() > finalSize.width()) {
1198 nextDims.fWidth = std::max((srcRect.width() + 1)/2, finalSize.width());
1199 } else if (srcRect.width() < finalSize.width()) {
1200 nextDims.fWidth = std::min(srcRect.width()*2, finalSize.width());
Brian Salomon59f31b12020-06-04 17:27:15 -04001201 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001202 if (srcRect.height() > finalSize.height()) {
1203 nextDims.fHeight = std::max((srcRect.height() + 1)/2, finalSize.height());
1204 } else if (srcRect.height() < finalSize.height()) {
1205 nextDims.fHeight = std::min(srcRect.height()*2, finalSize.height());
Brian Salomon59f31b12020-06-04 17:27:15 -04001206 }
1207 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001208 auto input = tempA ? tempA.get() : this;
Brian Salomone9ad9982019-07-22 16:17:41 -04001209 sk_sp<GrColorSpaceXform> xform;
Brian Salomonbacbb922021-01-21 19:48:00 -05001210 GrSurfaceFillContext* stepDst;
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001211 SkIRect stepDstRect;
Brian Salomon1c86b632020-12-11 12:36:01 -05001212 if (nextDims == finalSize) {
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001213 stepDst = dst;
1214 stepDstRect = dstRect;
Brian Salomonbacbb922021-01-21 19:48:00 -05001215 xform = GrColorSpaceXform::Make(input->colorInfo(), dst->colorInfo());
Brian Salomon1c86b632020-12-11 12:36:01 -05001216 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -05001217 GrImageInfo nextInfo(input->colorInfo(), nextDims);
1218 tempB = GrSurfaceFillContext::MakeWithFallback(fContext,
1219 nextInfo,
1220 SkBackingFit::kApprox);
Brian Salomon1c86b632020-12-11 12:36:01 -05001221 if (!tempB) {
1222 return false;
1223 }
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001224 stepDst = tempB.get();
1225 stepDstRect = SkIRect::MakeSize(tempB->dimensions());
Brian Salomone9ad9982019-07-22 16:17:41 -04001226 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001227 std::unique_ptr<GrFragmentProcessor> fp;
Mike Reed1efa14d2021-01-02 21:44:59 -05001228 if (rescaleMode == RescaleMode::kRepeatedCubic) {
Brian Salomon59f31b12020-06-04 17:27:15 -04001229 auto dir = GrBicubicEffect::Direction::kXY;
1230 if (nextDims.width() == srcRect.width()) {
1231 dir = GrBicubicEffect::Direction::kY;
1232 } else if (nextDims.height() == srcRect.height()) {
1233 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -04001234 }
Brian Salomon1af72d12020-06-25 10:47:26 -04001235 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
Mike Reed3867c702020-09-01 13:28:10 -04001236 static constexpr auto kKernel = GrBicubicEffect::gCatmullRom;
Brian Salomon1c86b632020-12-11 12:36:01 -05001237 fp = GrBicubicEffect::MakeSubset(std::move(texView),
1238 input->colorInfo().alphaType(),
Brian Salomonbacbb922021-01-21 19:48:00 -05001239 SkMatrix::I(),
Brian Salomon1c86b632020-12-11 12:36:01 -05001240 kWM,
1241 kWM,
1242 SkRect::Make(srcRect),
1243 kKernel,
1244 dir,
1245 *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -04001246 } else {
Mike Reed1efa14d2021-01-02 21:44:59 -05001247 auto filter = rescaleMode == RescaleMode::kNearest ? GrSamplerState::Filter::kNearest
1248 : GrSamplerState::Filter::kLinear;
Brian Salomonbacbb922021-01-21 19:48:00 -05001249 auto srcRectF = SkRect::Make(srcRect);
1250 fp = GrTextureEffect::MakeSubset(std::move(texView),
1251 this->colorInfo().alphaType(),
1252 SkMatrix::I(),
1253 {filter, GrSamplerState::MipmapMode::kNone},
1254 srcRectF,
1255 srcRectF,
1256 *this->caps());
Brian Salomone9ad9982019-07-22 16:17:41 -04001257 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001258 if (xform) {
1259 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
1260 }
1261 stepDst->fillRectToRectWithFP(srcRect, stepDstRect, std::move(fp));
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001262 texView = stepDst->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -04001263 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001264 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomone9ad9982019-07-22 16:17:41 -04001265 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001266 return true;
Brian Salomone9ad9982019-07-22 16:17:41 -04001267}
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001268
1269GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
1270 const SkIRect& rect) {
1271 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1272 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001273 auto direct = fContext->asDirectContext();
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001274 if (!direct) {
1275 return {};
1276 }
1277 auto rtProxy = this->asRenderTargetProxy();
1278 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1279 return {};
1280 }
1281
1282 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001283 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
1284 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001285 // Fail if read color type does not have all of dstCT's color channels and those missing color
1286 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001287 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1288 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
1289 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1290 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001291 return {};
1292 }
1293
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001294 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001295 !supportedRead.fOffsetAlignmentForTransferBuffer) {
1296 return {};
1297 }
1298
1299 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
1300 size_t size = rowBytes * rect.height();
Greg Daniel2e967df2021-02-08 10:38:31 -05001301 // By using kStream_GrAccessPattern here, we are not able to cache and reuse the buffer for
1302 // multiple reads. Switching to kDynamic_GrAccessPattern would allow for this, however doing
1303 // so causes a crash in a chromium test. See skbug.com/11297
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001304 auto buffer = direct->priv().resourceProvider()->createBuffer(
Greg Daniel393debc2021-02-06 03:37:20 +00001305 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001306 if (!buffer) {
1307 return {};
1308 }
1309 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -05001310 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001311 if (flip) {
1312 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1313 this->height() - rect.fTop);
1314 }
Greg Danielbbfec9d2019-08-20 10:56:51 -04001315 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001316 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -04001317 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001318 PixelTransferResult result;
1319 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001320 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -04001321 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001322 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
1323 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001324 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
1325 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001326 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
1327 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -04001328 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001329 };
1330 }
1331 return result;
1332}
Greg Daniel46e366a2019-12-16 14:38:36 -05001333
1334#ifdef SK_DEBUG
1335void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001336 SkASSERT(fReadView.proxy());
1337 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -04001338 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
1339 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
1340 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
1341 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001342 this->onValidate();
1343}
1344#endif