blob: d67fa0f003a0769a6e4450010685093e295fc985 [file] [log] [blame]
Brian Osman45580d32016-11-23 09:37:01 -05001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Greg Daniel46cfbc62019-06-07 11:43:30 -04008#include "src/gpu/GrSurfaceContext.h"
9
John Stilesfbd050b2020-08-03 13:21:46 -040010#include <memory>
11
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040012#include "include/gpu/GrDirectContext.h"
13#include "include/gpu/GrRecordingContext.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040014#include "src/core/SkAutoPixmapStorage.h"
Brian Salomon63a0a752020-06-26 13:32:09 -040015#include "src/core/SkYUVMath.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040016#include "src/gpu/GrAuditTrail.h"
Brian Salomon1c86b632020-12-11 12:36:01 -050017#include "src/gpu/GrColorSpaceXform.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040018#include "src/gpu/GrDataUtils.h"
Adlai Hollera0693042020-10-14 11:23:11 -040019#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrDrawingManager.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040021#include "src/gpu/GrGpu.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040022#include "src/gpu/GrImageInfo.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040023#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050025#include "src/gpu/GrSurfaceDrawContext.h"
Brian Salomonbacbb922021-01-21 19:48:00 -050026#include "src/gpu/GrSurfaceFillContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/SkGr.h"
Brian Salomone9ad9982019-07-22 16:17:41 -040028#include "src/gpu/effects/GrBicubicEffect.h"
Brian Salomon63a0a752020-06-26 13:32:09 -040029#include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h"
Brian Osman45580d32016-11-23 09:37:01 -050030
Adlai Holler33dbd652020-06-01 12:35:42 -040031#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
Robert Phillips9eb00022020-06-30 15:30:12 -040032#define RETURN_FALSE_IF_ABANDONED if (this->fContext->abandoned()) { return false; }
Brian Osman45580d32016-11-23 09:37:01 -050033
Greg Danielbfa19c42019-12-19 16:41:40 -050034std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -050035 GrSurfaceProxyView readView,
Brian Salomon14f99fc2020-12-07 12:19:47 -050036 const GrColorInfo& info) {
Greg Daniele20fcad2020-01-08 11:52:34 -050037 // It is probably not necessary to check if the context is abandoned here since uses of the
38 // GrSurfaceContext which need the context will mostly likely fail later on without an issue.
39 // However having this hear adds some reassurance in case there is a path doesn't handle an
40 // abandoned context correctly. It also lets us early out of some extra work.
Robert Phillips9eb00022020-06-30 15:30:12 -040041 if (context->abandoned()) {
Greg Daniele20fcad2020-01-08 11:52:34 -050042 return nullptr;
43 }
Greg Daniel3912a4b2020-01-14 09:56:04 -050044 GrSurfaceProxy* proxy = readView.proxy();
Greg Danielbfa19c42019-12-19 16:41:40 -050045 SkASSERT(proxy && proxy->asTextureProxy());
46
Greg Danielbfa19c42019-12-19 16:41:40 -050047 std::unique_ptr<GrSurfaceContext> surfaceContext;
Greg Daniel3912a4b2020-01-14 09:56:04 -050048 if (proxy->asRenderTargetProxy()) {
Brian Salomon8afde5f2020-04-01 16:22:00 -040049 // Will we ever want a swizzle that is not the default write swizzle for the format and
Greg Danielbfa19c42019-12-19 16:41:40 -050050 // colorType here? If so we will need to manually pass that in.
Brian Salomonc5243782020-04-02 12:50:34 -040051 GrSwizzle writeSwizzle;
Brian Salomon14f99fc2020-12-07 12:19:47 -050052 if (info.colorType() != GrColorType::kUnknown) {
53 writeSwizzle = context->priv().caps()->getWriteSwizzle(proxy->backendFormat(),
54 info.colorType());
Brian Salomonc5243782020-04-02 12:50:34 -040055 }
Brian Salomon8afde5f2020-04-01 16:22:00 -040056 GrSurfaceProxyView writeView(readView.refProxy(), readView.origin(), writeSwizzle);
Brian Salomon590f5672020-12-16 11:44:47 -050057 if (info.alphaType() == kPremul_SkAlphaType || info.alphaType() == kOpaque_SkAlphaType) {
58 surfaceContext = std::make_unique<GrSurfaceDrawContext>(context,
59 std::move(readView),
60 std::move(writeView),
61 info.colorType(),
62 info.refColorSpace(),
63 /*surface props*/ nullptr);
64 } else {
65 surfaceContext = std::make_unique<GrSurfaceFillContext>(context,
66 std::move(readView),
67 std::move(writeView),
68 info);
69 }
Greg Danielbfa19c42019-12-19 16:41:40 -050070 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -050071 surfaceContext = std::make_unique<GrSurfaceContext>(context, std::move(readView), info);
Greg Danielbfa19c42019-12-19 16:41:40 -050072 }
Robert Phillips07f0e412020-01-17 15:20:00 -050073 SkDEBUGCODE(surfaceContext->validate();)
Greg Danielbfa19c42019-12-19 16:41:40 -050074 return surfaceContext;
75}
76
Brian Salomona56a7462020-02-07 14:17:25 -050077std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
Brian Salomon14f99fc2020-12-07 12:19:47 -050078 const GrImageInfo& info,
Brian Salomona56a7462020-02-07 14:17:25 -050079 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050080 SkBackingFit fit,
Brian Salomon14f99fc2020-12-07 12:19:47 -050081 GrSurfaceOrigin origin,
82 GrRenderable renderable,
83 int sampleCount,
84 GrMipmapped mipmapped,
85 GrProtected isProtected,
Brian Salomona56a7462020-02-07 14:17:25 -050086 SkBudgeted budgeted) {
Brian Salomon14f99fc2020-12-07 12:19:47 -050087 SkASSERT(context);
88 SkASSERT(renderable == GrRenderable::kYes || sampleCount == 1);
89 if (context->abandoned()) {
90 return nullptr;
Brian Salomond005b692020-04-01 15:47:05 -040091 }
Brian Salomon14f99fc2020-12-07 12:19:47 -050092 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(format,
93 info.dimensions(),
94 renderable,
95 sampleCount,
96 mipmapped,
97 fit,
98 budgeted,
99 isProtected);
Greg Danielbfa19c42019-12-19 16:41:40 -0500100 if (!proxy) {
101 return nullptr;
102 }
103
Brian Salomon14f99fc2020-12-07 12:19:47 -0500104 GrSwizzle swizzle;
105 if (info.colorType() != GrColorType::kUnknown &&
106 !context->priv().caps()->isFormatCompressed(format)) {
107 swizzle = context->priv().caps()->getReadSwizzle(format, info.colorType());
108 }
109
Greg Daniel3912a4b2020-01-14 09:56:04 -0500110 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500111 return GrSurfaceContext::Make(context, std::move(view), info.colorInfo());
112}
113
114std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
115 const GrImageInfo& info,
116 SkBackingFit fit,
117 GrSurfaceOrigin origin,
118 GrRenderable renderable,
119 int sampleCount,
120 GrMipmapped mipmapped,
121 GrProtected isProtected,
122 SkBudgeted budgeted) {
123 GrBackendFormat format = context->priv().caps()->getDefaultBackendFormat(info.colorType(),
124 renderable);
125 return Make(context,
126 info,
127 format,
128 fit,
129 origin,
130 renderable,
131 sampleCount,
132 mipmapped,
133 isProtected,
134 budgeted);
Greg Danielbfa19c42019-12-19 16:41:40 -0500135}
136
Robert Phillips69893702019-02-22 11:16:30 -0500137GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500138 GrSurfaceProxyView readView,
Brian Salomon14f99fc2020-12-07 12:19:47 -0500139 const GrColorInfo& info)
140 : fContext(context), fReadView(std::move(readView)), fColorInfo(info) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400141 SkASSERT(!context->abandoned());
Greg Daniele20fcad2020-01-08 11:52:34 -0500142}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400143
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400144const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); }
145
Robert Phillips0d075de2019-03-04 11:08:13 -0500146GrAuditTrail* GrSurfaceContext::auditTrail() {
147 return fContext->priv().auditTrail();
148}
149
150GrDrawingManager* GrSurfaceContext::drawingManager() {
151 return fContext->priv().drawingManager();
152}
153
154const GrDrawingManager* GrSurfaceContext::drawingManager() const {
155 return fContext->priv().drawingManager();
156}
157
158#ifdef SK_DEBUG
Brian Salomon70fe17e2020-11-30 14:33:58 -0500159GrSingleOwner* GrSurfaceContext::singleOwner() const { return fContext->priv().singleOwner(); }
Robert Phillips0d075de2019-03-04 11:08:13 -0500160#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400161
Brian Salomondd4087d2020-12-23 20:36:44 -0500162static bool alpha_types_compatible(SkAlphaType srcAlphaType, SkAlphaType dstAlphaType) {
163 // If both alpha types are kUnknown things make sense. If not, it's too underspecified.
164 return (srcAlphaType == kUnknown_SkAlphaType) == (dstAlphaType == kUnknown_SkAlphaType);
165}
166
167bool GrSurfaceContext::readPixels(GrDirectContext* dContext, GrPixmap dst, SkIPoint pt) {
Brian Salomon1d435302019-07-01 13:05:28 -0400168 ASSERT_SINGLE_OWNER
169 RETURN_FALSE_IF_ABANDONED
170 SkDEBUGCODE(this->validate();)
171 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400172 if (!fContext->priv().matches(dContext)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400173 return false;
174 }
Brian Salomon46f63232021-01-25 10:13:35 -0500175
176 if (dst.colorType() == GrColorType::kUnknown) {
177 return false;
178 }
179
Brian Salomondd4087d2020-12-23 20:36:44 -0500180 dst = dst.clip(this->dimensions(), &pt);
181 if (!dst.hasPixels()) {
Brian Salomon1d435302019-07-01 13:05:28 -0400182 return false;
183 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500184 if (!alpha_types_compatible(this->colorInfo().alphaType(), dst.alphaType())) {
Brian Salomon1d435302019-07-01 13:05:28 -0400185 return false;
186 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500187 // We allow unknown alpha types but only if both src and dst are unknown. Otherwise, it's too
188 // weird to reason about what should be expected.
Greg Daniel6eb8c242019-06-05 10:22:24 -0400189
Brian Salomon982127b2021-01-21 10:43:35 -0500190 sk_sp<GrSurfaceProxy> srcProxy = this->asSurfaceProxyRef();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400191
Stephen White3c0a50f2020-01-16 18:19:54 -0500192 if (srcProxy->framebufferOnly()) {
193 return false;
194 }
195
Greg Daniel6eb8c242019-06-05 10:22:24 -0400196 // MDB TODO: delay this instantiation until later in the method
Adlai Hollerc95b5892020-08-11 12:02:22 -0400197 if (!srcProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400198 return false;
199 }
200
201 GrSurface* srcSurface = srcProxy->peekSurface();
202
Brian Salomondd4087d2020-12-23 20:36:44 -0500203 SkColorSpaceXformSteps::Flags flags =
204 SkColorSpaceXformSteps{this->colorInfo(), dst.info()}.flags;
Mike Klein7321e6a2019-12-03 11:08:40 -0600205 bool unpremul = flags.unpremul,
206 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
207 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400208
Adlai Hollerc95b5892020-08-11 12:02:22 -0400209 const GrCaps* caps = dContext->priv().caps();
Robert Phillips07f0e412020-01-17 15:20:00 -0500210 bool srcIsCompressed = caps->isFormatCompressed(srcSurface->backendFormat());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400211 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
212 // care so much about getImageData performance. However, in order to ensure putImageData/
213 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
214 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
215 // fContext->vaildaPMUPMConversionExists()).
Greg Daniel0258c902019-08-01 13:08:33 -0400216 GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
217 GrRenderable::kYes);
Greg Danielc71c7962020-01-14 16:44:18 -0500218 GrColorType srcColorType = this->colorInfo().colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400219 bool canvas2DFastPath = unpremul && !needColorConversion &&
Brian Salomondd4087d2020-12-23 20:36:44 -0500220 (GrColorType::kRGBA_8888 == dst.colorType() ||
221 GrColorType::kBGRA_8888 == dst.colorType()) &&
Brian Salomon1d435302019-07-01 13:05:28 -0400222 SkToBool(srcProxy->asTextureProxy()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500223 (srcColorType == GrColorType::kRGBA_8888 ||
224 srcColorType == GrColorType::kBGRA_8888) &&
Greg Daniel0258c902019-08-01 13:08:33 -0400225 defaultRGBAFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400226 dContext->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400227
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400228 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400229 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400230 return false;
231 }
232
Brian Salomondc0710f2019-07-01 14:59:32 -0400233 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400234 std::unique_ptr<GrSurfaceContext> tempCtx;
235 if (this->asTextureProxy()) {
236 GrColorType colorType = (canvas2DFastPath || srcIsCompressed)
237 ? GrColorType::kRGBA_8888
238 : this->colorInfo().colorType();
Brian Salomondd4087d2020-12-23 20:36:44 -0500239 SkAlphaType alphaType = canvas2DFastPath ? dst.alphaType()
Brian Salomon590f5672020-12-16 11:44:47 -0500240 : this->colorInfo().alphaType();
241 GrImageInfo tempInfo(colorType,
242 alphaType,
243 this->colorInfo().refColorSpace(),
Brian Salomondd4087d2020-12-23 20:36:44 -0500244 dst.dimensions());
Brian Salomon590f5672020-12-16 11:44:47 -0500245 auto sfc = GrSurfaceFillContext::Make(dContext, tempInfo, SkBackingFit::kApprox);
246 if (!sfc) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400247 return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400248 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400249
250 std::unique_ptr<GrFragmentProcessor> fp;
251 if (canvas2DFastPath) {
252 fp = dContext->priv().createPMToUPMEffect(GrTextureEffect::Make(
253 this->readSurfaceView(), this->colorInfo().alphaType()));
Brian Salomondd4087d2020-12-23 20:36:44 -0500254 if (dst.colorType() == GrColorType::kBGRA_8888) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400255 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
Brian Salomondd4087d2020-12-23 20:36:44 -0500256 dst = GrPixmap(dst.info().makeColorType(GrColorType::kRGBA_8888),
257 dst.addr(),
258 dst.rowBytes());
Brian Salomon72c7b982020-10-06 10:07:38 -0400259 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400260 } else {
261 fp = GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType());
262 }
263 if (!fp) {
264 return false;
265 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500266 sfc->fillRectToRectWithFP(SkIRect::MakePtSize(pt, dst.dimensions()),
267 SkIRect::MakeSize(dst.dimensions()),
Brian Salomon590f5672020-12-16 11:44:47 -0500268 std::move(fp));
Brian Salomon72c7b982020-10-06 10:07:38 -0400269 pt = {0, 0};
Brian Salomon590f5672020-12-16 11:44:47 -0500270 tempCtx = std::move(sfc);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400271 } else {
Brian Salomon72c7b982020-10-06 10:07:38 -0400272 auto restrictions = this->caps()->getDstCopyRestrictions(this->asRenderTargetProxy(),
273 this->colorInfo().colorType());
274 sk_sp<GrSurfaceProxy> copy;
275 static constexpr auto kFit = SkBackingFit::kExact;
276 static constexpr auto kBudgeted = SkBudgeted::kYes;
277 static constexpr auto kMipMapped = GrMipMapped::kNo;
278 if (restrictions.fMustCopyWholeSrc) {
Brian Salomon982127b2021-01-21 10:43:35 -0500279 copy = GrSurfaceProxy::Copy(fContext,
280 std::move(srcProxy),
281 this->origin(),
282 kMipMapped,
283 kFit,
Brian Salomon72c7b982020-10-06 10:07:38 -0400284 kBudgeted);
285 } else {
Brian Salomondd4087d2020-12-23 20:36:44 -0500286 auto srcRect = SkIRect::MakePtSize(pt, dst.dimensions());
Brian Salomon982127b2021-01-21 10:43:35 -0500287 copy = GrSurfaceProxy::Copy(fContext,
288 std::move(srcProxy),
289 this->origin(),
290 kMipMapped,
291 srcRect,
292 kFit,
293 kBudgeted,
294 restrictions.fRectsMustMatch);
Brian Salomon72c7b982020-10-06 10:07:38 -0400295 pt = {0, 0};
296 }
297 if (!copy) {
298 return false;
299 }
300 GrSurfaceProxyView view{std::move(copy), this->origin(), this->readSwizzle()};
Brian Salomon14f99fc2020-12-07 12:19:47 -0500301 tempCtx = GrSurfaceContext::Make(dContext, std::move(view), this->colorInfo());
Brian Salomon72c7b982020-10-06 10:07:38 -0400302 SkASSERT(tempCtx);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400303 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500304 return tempCtx->readPixels(dContext, dst, pt);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400305 }
306
Greg Danielb8d84f82020-02-13 14:25:00 -0500307 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400308
Brian Salomon1d435302019-07-01 13:05:28 -0400309 auto supportedRead = caps->supportedReadPixelsColorType(
Brian Salomondd4087d2020-12-23 20:36:44 -0500310 this->colorInfo().colorType(), srcProxy->backendFormat(), dst.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400311
Brian Salomondd4087d2020-12-23 20:36:44 -0500312 bool makeTight =
313 !caps->readPixelsRowBytesSupport() && dst.rowBytes() != dst.info().minRowBytes();
Brian Salomon1047a492019-07-02 12:25:21 -0400314
315 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomondd4087d2020-12-23 20:36:44 -0500316 (dst.colorType() != supportedRead.fColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400317
Brian Salomonf30b1c12019-06-20 12:25:02 -0400318 std::unique_ptr<char[]> tmpPixels;
Brian Salomondd4087d2020-12-23 20:36:44 -0500319 GrPixmap tmp;
320 void* readDst = dst.addr();
321 size_t readRB = dst.rowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400322 if (convert) {
Brian Salomondd4087d2020-12-23 20:36:44 -0500323 GrImageInfo tmpInfo(supportedRead.fColorType,
324 this->colorInfo().alphaType(),
325 this->colorInfo().refColorSpace(),
326 dst.dimensions());
Brian Salomon1d435302019-07-01 13:05:28 -0400327 size_t tmpRB = tmpInfo.minRowBytes();
328 size_t size = tmpRB * tmpInfo.height();
329 // Chrome MSAN bots require the data to be initialized (hence the ()).
John Stilesfbd050b2020-08-03 13:21:46 -0400330 tmpPixels = std::make_unique<char[]>(size);
Brian Salomondd4087d2020-12-23 20:36:44 -0500331 tmp = {tmpInfo, tmpPixels.get(), tmpRB};
Brian Salomonf30b1c12019-06-20 12:25:02 -0400332
Brian Salomonf30b1c12019-06-20 12:25:02 -0400333 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400334 readRB = tmpRB;
Brian Salomondd4087d2020-12-23 20:36:44 -0500335 pt.fY = flip ? srcSurface->height() - pt.fY - dst.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400336 }
337
Brian Salomon982127b2021-01-21 10:43:35 -0500338 dContext->priv().flushSurface(srcProxy.get());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400339 dContext->submit();
Brian Salomondd4087d2020-12-23 20:36:44 -0500340 if (!dContext->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dst.width(), dst.height(),
341 this->colorInfo().colorType(),
Adlai Hollerc95b5892020-08-11 12:02:22 -0400342 supportedRead.fColorType, readDst, readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400343 return false;
344 }
345
Brian Salomondd4087d2020-12-23 20:36:44 -0500346 if (tmp.hasPixels()) {
347 return GrConvertPixels(dst, tmp, flip);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400348 }
349 return true;
350}
Robert Phillips0d075de2019-03-04 11:08:13 -0500351
Brian Salomondd4087d2020-12-23 20:36:44 -0500352bool GrSurfaceContext::writePixels(GrDirectContext* dContext, GrPixmap src, SkIPoint pt) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400353 ASSERT_SINGLE_OWNER
354 RETURN_FALSE_IF_ABANDONED
355 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400356 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400357
Adlai Hollerc95b5892020-08-11 12:02:22 -0400358 if (!dContext) {
Brian Salomonc320b152018-02-20 14:05:36 -0500359 return false;
360 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500361
Brian Salomon1d435302019-07-01 13:05:28 -0400362 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500363 return false;
364 }
365
Brian Salomon46f63232021-01-25 10:13:35 -0500366 if (src.colorType() == GrColorType::kUnknown) {
367 return false;
368 }
369
Brian Salomondd4087d2020-12-23 20:36:44 -0500370 src = src.clip(this->dimensions(), &pt);
371 if (!src.hasPixels()) {
Brian Salomon1d435302019-07-01 13:05:28 -0400372 return false;
373 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500374 if (!alpha_types_compatible(src.alphaType(), this->colorInfo().alphaType())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400375 return false;
376 }
377
378 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
Stephen White3c0a50f2020-01-16 18:19:54 -0500379
380 if (dstProxy->framebufferOnly()) {
381 return false;
382 }
383
Adlai Hollerc95b5892020-08-11 12:02:22 -0400384 if (!dstProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400385 return false;
386 }
387
388 GrSurface* dstSurface = dstProxy->peekSurface();
389
Brian Salomondd4087d2020-12-23 20:36:44 -0500390 SkColorSpaceXformSteps::Flags flags =
391 SkColorSpaceXformSteps{src.info(), this->colorInfo()}.flags;
Mike Klein7321e6a2019-12-03 11:08:40 -0600392 bool unpremul = flags.unpremul,
393 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
394 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400395
Adlai Hollerc95b5892020-08-11 12:02:22 -0400396 const GrCaps* caps = dContext->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400397
398 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
399 GrRenderable::kNo);
400
Greg Danielc71c7962020-01-14 16:44:18 -0500401 GrColorType dstColorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400402 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
403 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400404 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
Brian Salomondd4087d2020-12-23 20:36:44 -0500405 (src.colorType() == GrColorType::kRGBA_8888 ||
406 src.colorType() == GrColorType::kBGRA_8888) &&
Brian Salomon590f5672020-12-16 11:44:47 -0500407 this->asFillContext() &&
Greg Danielc71c7962020-01-14 16:44:18 -0500408 (dstColorType == GrColorType::kRGBA_8888 ||
409 dstColorType == GrColorType::kBGRA_8888) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400410 rgbaDefaultFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400411 dContext->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400412
413 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500414 GrColorInfo tempColorInfo;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400415 GrBackendFormat format;
Greg Danielbfa19c42019-12-19 16:41:40 -0500416 GrSwizzle tempReadSwizzle;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400417 if (canvas2DFastPath) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500418 tempColorInfo = {GrColorType::kRGBA_8888,
419 kUnpremul_SkAlphaType,
420 this->colorInfo().refColorSpace()};
Greg Daniel7bfc9132019-08-14 14:23:53 -0400421 format = rgbaDefaultFormat;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400422 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500423 tempColorInfo = this->colorInfo();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400424 format = dstProxy->backendFormat().makeTexture2D();
425 if (!format.isValid()) {
426 return false;
427 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500428 tempReadSwizzle = this->readSwizzle();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400429 }
430
Greg Daniel2e52ad12019-06-13 10:04:16 -0400431 // It is more efficient for us to write pixels into a top left origin so we prefer that.
432 // However, if the final proxy isn't a render target then we must use a copy to move the
433 // data into it which requires the origins to match. If the final proxy is a render target
434 // we can use a draw instead which doesn't have this origin restriction. Thus for render
435 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400436 GrSurfaceOrigin tempOrigin =
Brian Salomon590f5672020-12-16 11:44:47 -0500437 this->asFillContext() ? kTopLeft_GrSurfaceOrigin : this->origin();
Adlai Hollerc95b5892020-08-11 12:02:22 -0400438 auto tempProxy = dContext->priv().proxyProvider()->createProxy(
Brian Salomondd4087d2020-12-23 20:36:44 -0500439 format, src.dimensions(), GrRenderable::kNo, 1, GrMipmapped::kNo,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400440 SkBackingFit::kApprox, SkBudgeted::kYes, GrProtected::kNo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400441 if (!tempProxy) {
442 return false;
443 }
Greg Daniel3912a4b2020-01-14 09:56:04 -0500444 GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500445 GrSurfaceContext tempCtx(dContext, tempView, tempColorInfo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400446
447 // In the fast path we always write the srcData to the temp context as though it were RGBA.
448 // When the data is really BGRA the write will cause the R and B channels to be swapped in
449 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
450 // dst.
Brian Salomondd4087d2020-12-23 20:36:44 -0500451 GrColorType origSrcColorType = src.colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400452 if (canvas2DFastPath) {
Brian Salomondd4087d2020-12-23 20:36:44 -0500453 src = {src.info().makeColorType(GrColorType::kRGBA_8888), src.addr(), src.rowBytes()};
Brian Salomon1d435302019-07-01 13:05:28 -0400454 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500455 if (!tempCtx.writePixels(dContext, src, {0, 0})) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400456 return false;
457 }
458
Brian Salomon590f5672020-12-16 11:44:47 -0500459 if (this->asFillContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400460 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400461 if (canvas2DFastPath) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400462 fp = dContext->priv().createUPMToPMEffect(
Brian Salomon14f99fc2020-12-07 12:19:47 -0500463 GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType()));
Brian Salomon1d435302019-07-01 13:05:28 -0400464 // Important: check the original src color type here!
Brian Salomondd4087d2020-12-23 20:36:44 -0500465 if (origSrcColorType == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400466 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
467 }
468 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500469 fp = GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400470 }
471 if (!fp) {
472 return false;
473 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500474 this->asFillContext()->fillRectToRectWithFP(SkIRect::MakeSize(src.dimensions()),
475 SkIRect::MakePtSize(pt, src.dimensions()),
476 std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400477 } else {
Brian Salomondd4087d2020-12-23 20:36:44 -0500478 SkIRect srcRect = SkIRect::MakeSize(src.dimensions());
Brian Salomon1d435302019-07-01 13:05:28 -0400479 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Brian Salomon982127b2021-01-21 10:43:35 -0500480 if (!this->copy(std::move(tempProxy), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400481 return false;
482 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400483 }
484 return true;
485 }
486
Brian Salomon1d435302019-07-01 13:05:28 -0400487 GrColorType allowedColorType =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400488 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400489 dstProxy->backendFormat(),
Brian Salomondd4087d2020-12-23 20:36:44 -0500490 src.colorType()).fColorType;
Greg Danielb8d84f82020-02-13 14:25:00 -0500491 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomondd4087d2020-12-23 20:36:44 -0500492 bool makeTight = !caps->writePixelsRowBytesSupport() &&
493 src.rowBytes() != src.info().minRowBytes();
Brian Salomon1047a492019-07-02 12:25:21 -0400494 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomondd4087d2020-12-23 20:36:44 -0500495 (src.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400496
Brian Salomonbe1084b2021-01-26 13:29:30 -0500497 if (convert || !src.ownsPixels()) {
Brian Salomondd4087d2020-12-23 20:36:44 -0500498 GrImageInfo tmpInfo(allowedColorType,
499 this->colorInfo().alphaType(),
500 this->colorInfo().refColorSpace(),
501 src.dimensions());
Brian Salomonbe1084b2021-01-26 13:29:30 -0500502 GrPixmap tmp = GrPixmap::Allocate(tmpInfo);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400503
Brian Salomondd4087d2020-12-23 20:36:44 -0500504 SkAssertResult(GrConvertPixels(tmp, src, flip));
Brian Salomonf30b1c12019-06-20 12:25:02 -0400505
Brian Salomondd4087d2020-12-23 20:36:44 -0500506 src = tmp;
Brian Salomon1d435302019-07-01 13:05:28 -0400507 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400508 }
509
Brian Salomonbe1084b2021-01-26 13:29:30 -0500510 GrMipLevel level;
511 level.fPixels = src.addr();
512 level.fRowBytes = src.rowBytes();
513 return dContext->priv().drawingManager()->newWritePixelsTask(
514 this->asSurfaceProxyRef(),
515 SkIRect::MakePtSize(pt, src.dimensions()),
516 src.colorType(),
517 dstColorType,
518 &level,
519 1,
520 src.pixelStorage());
Brian Osman45580d32016-11-23 09:37:01 -0500521}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400522
Adlai Hollerc95b5892020-08-11 12:02:22 -0400523void GrSurfaceContext::asyncRescaleAndReadPixels(GrDirectContext* dContext,
524 const SkImageInfo& info,
Brian Salomon63a0a752020-06-26 13:32:09 -0400525 const SkIRect& srcRect,
526 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500527 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400528 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400529 ReadPixelsContext callbackContext) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400530 if (!dContext) {
531 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400532 return;
533 }
534 auto rt = this->asRenderTargetProxy();
535 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400536 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400537 return;
538 }
539 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400540 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400541 return;
542 }
543 auto dstCT = SkColorTypeToGrColorType(info.colorType());
544 if (dstCT == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400545 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400546 return;
547 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500548 bool needsRescale = srcRect.size() != info.dimensions();
Brian Salomon63a0a752020-06-26 13:32:09 -0400549 auto colorTypeOfFinalContext = this->colorInfo().colorType();
550 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
551 if (needsRescale) {
552 colorTypeOfFinalContext = dstCT;
553 backendFormatOfFinalContext =
554 this->caps()->getDefaultBackendFormat(dstCT, GrRenderable::kYes);
555 }
556 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonbacbb922021-01-21 19:48:00 -0500557 backendFormatOfFinalContext,
558 dstCT);
Brian Salomon63a0a752020-06-26 13:32:09 -0400559 // Fail if we can't read from the source surface's color type.
560 if (readInfo.fColorType == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400561 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400562 return;
563 }
564 // Fail if read color type does not have all of dstCT's color channels and those missing color
565 // channels are in the src.
566 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
567 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
568 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
569 if ((~legalReadChannels & dstChannels) & srcChannels) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400570 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400571 return;
572 }
573
Brian Salomonbacbb922021-01-21 19:48:00 -0500574 std::unique_ptr<GrSurfaceFillContext> tempFC;
Brian Salomon63a0a752020-06-26 13:32:09 -0400575 int x = srcRect.fLeft;
576 int y = srcRect.fTop;
577 if (needsRescale) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500578 tempFC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma, rescaleMode);
579 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400580 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400581 return;
582 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500583 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
584 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400585 x = y = 0;
586 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -0500587 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo(),
588 info.colorInfo());
Brian Salomon63a0a752020-06-26 13:32:09 -0400589 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
590 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
591 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomonbacbb922021-01-21 19:48:00 -0500592 SkIRect srcRectToDraw = srcRect;
Brian Salomon63a0a752020-06-26 13:32:09 -0400593 // If the src is not texturable first try to make a copy to a texture.
594 if (!texProxyView.asTextureProxy()) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500595 texProxyView = GrSurfaceProxyView::Copy(fContext,
596 texProxyView,
597 GrMipmapped::kNo,
598 srcRect,
599 SkBackingFit::kApprox,
600 SkBudgeted::kNo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400601 if (!texProxyView) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400602 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400603 return;
604 }
605 SkASSERT(texProxyView.asTextureProxy());
Brian Salomonbacbb922021-01-21 19:48:00 -0500606 srcRectToDraw = SkIRect::MakeSize(srcRect.size());
Brian Salomon63a0a752020-06-26 13:32:09 -0400607 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500608 auto tempInfo = GrImageInfo(info).makeColorType(this->colorInfo().colorType());
609 tempFC = GrSurfaceFillContext::Make(dContext, tempInfo, SkBackingFit::kApprox);
610 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400611 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400612 return;
613 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500614 auto fp = GrTextureEffect::Make(std::move(texProxyView), this->colorInfo().alphaType());
615 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
616 tempFC->fillRectToRectWithFP(srcRectToDraw,
617 SkIRect::MakeSize(tempFC->dimensions()),
618 std::move(fp));
Brian Salomon63a0a752020-06-26 13:32:09 -0400619 x = y = 0;
620 }
621 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500622 auto srcCtx = tempFC ? tempFC.get() : this;
623 return srcCtx->asyncReadPixels(dContext,
624 SkIRect::MakePtSize({x, y}, info.dimensions()),
625 info.colorType(),
626 callback,
627 callbackContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400628}
629
630class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult {
631public:
632 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
633 ~AsyncReadResult() override {
634 for (int i = 0; i < fPlanes.count(); ++i) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500635 fPlanes[i].releaseMappedBuffer(fInboxID);
Brian Salomon63a0a752020-06-26 13:32:09 -0400636 }
637 }
638
639 int count() const override { return fPlanes.count(); }
Brian Salomonbe1084b2021-01-26 13:29:30 -0500640 const void* data(int i) const override { return fPlanes[i].data(); }
641 size_t rowBytes(int i) const override { return fPlanes[i].rowBytes(); }
Brian Salomon63a0a752020-06-26 13:32:09 -0400642
643 bool addTransferResult(const PixelTransferResult& result,
644 SkISize dimensions,
645 size_t rowBytes,
646 GrClientMappedBufferManager* manager) {
647 SkASSERT(!result.fTransferBuffer->isMapped());
648 const void* mappedData = result.fTransferBuffer->map();
649 if (!mappedData) {
650 return false;
651 }
652 if (result.fPixelConverter) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500653 size_t size = rowBytes*dimensions.height();
654 sk_sp<SkData> data = SkData::MakeUninitialized(size);
655 result.fPixelConverter(data->writable_data(), mappedData);
656 this->addCpuPlane(std::move(data), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400657 result.fTransferBuffer->unmap();
658 } else {
659 manager->insert(result.fTransferBuffer);
660 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
661 }
662 return true;
663 }
664
Brian Salomonbe1084b2021-01-26 13:29:30 -0500665 void addCpuPlane(sk_sp<SkData> data, size_t rowBytes) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400666 SkASSERT(data);
667 SkASSERT(rowBytes > 0);
Brian Salomonbe1084b2021-01-26 13:29:30 -0500668 fPlanes.emplace_back(std::move(data), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400669 }
670
671private:
672 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
673 SkASSERT(data);
674 SkASSERT(rowBytes > 0);
675 SkASSERT(mappedBuffer);
676 SkASSERT(mappedBuffer->isMapped());
Brian Salomonbe1084b2021-01-26 13:29:30 -0500677 fPlanes.emplace_back(std::move(mappedBuffer), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400678 }
679
Brian Salomonbe1084b2021-01-26 13:29:30 -0500680 class Plane {
681 public:
682 Plane(sk_sp<GrGpuBuffer> buffer, size_t rowBytes)
683 : fMappedBuffer(std::move(buffer)), fRowBytes(rowBytes) {}
684 Plane(sk_sp<SkData> data, size_t rowBytes) : fData(std::move(data)), fRowBytes(rowBytes) {}
685
686 Plane(const Plane&) = delete;
687 Plane(Plane&&) = default;
688
689 ~Plane() { SkASSERT(!fMappedBuffer); }
690
691 Plane& operator=(const Plane&) = delete;
692 Plane& operator=(Plane&&) = default;
693
694 void releaseMappedBuffer(uint32_t inboxID) {
695 if (fMappedBuffer) {
696 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
697 {std::move(fMappedBuffer), inboxID});
698 }
699 }
700
701 const void* data() const {
702 if (fMappedBuffer) {
703 SkASSERT(!fData);
704 SkASSERT(fMappedBuffer->isMapped());
705 return fMappedBuffer->map();
706 }
707 SkASSERT(fData);
708 return fData->data();
709 }
710
711 size_t rowBytes() const { return fRowBytes; }
712
713 private:
714 sk_sp<SkData> fData;
Brian Salomon1eea1ea2021-01-26 18:12:25 +0000715 sk_sp<GrGpuBuffer> fMappedBuffer;
Brian Salomonbe1084b2021-01-26 13:29:30 -0500716 size_t fRowBytes;
Brian Salomon63a0a752020-06-26 13:32:09 -0400717 };
718 SkSTArray<3, Plane> fPlanes;
719 uint32_t fInboxID;
720};
721
Adlai Hollerc95b5892020-08-11 12:02:22 -0400722void GrSurfaceContext::asyncReadPixels(GrDirectContext* dContext,
723 const SkIRect& rect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400724 SkColorType colorType,
725 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400726 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400727 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
728 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
729
Adlai Hollerc95b5892020-08-11 12:02:22 -0400730 if (!dContext || this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
731 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400732 return;
733 }
734
Adlai Hollerc95b5892020-08-11 12:02:22 -0400735 auto mappedBufferManager = dContext->priv().clientMappedBufferManager();
Brian Salomon63a0a752020-06-26 13:32:09 -0400736
737 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
738
739 if (!transferResult.fTransferBuffer) {
740 auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
741 this->colorInfo().refColorSpace());
742 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomonbe1084b2021-01-26 13:29:30 -0500743 GrPixmap pm = GrPixmap::Allocate(ii);
744 result->addCpuPlane(pm.pixelStorage(), pm.rowBytes());
Brian Salomon63a0a752020-06-26 13:32:09 -0400745
Adlai Hollerc95b5892020-08-11 12:02:22 -0400746 SkIPoint pt{rect.fLeft, rect.fTop};
Brian Salomondd4087d2020-12-23 20:36:44 -0500747 if (!this->readPixels(dContext, pm, pt)) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400748 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400749 return;
750 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400751 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400752 return;
753 }
754
755 struct FinishContext {
756 ReadPixelsCallback* fClientCallback;
757 ReadPixelsContext fClientContext;
758 SkISize fSize;
759 SkColorType fColorType;
760 GrClientMappedBufferManager* fMappedBufferManager;
761 PixelTransferResult fTransferResult;
762 };
763 // Assumption is that the caller would like to flush. We could take a parameter or require an
764 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
765 // callback to GrGpu until after the next flush that flushes our op list, though.
766 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400767 callbackContext,
Brian Salomon63a0a752020-06-26 13:32:09 -0400768 rect.size(),
769 colorType,
770 mappedBufferManager,
771 std::move(transferResult)};
772 auto finishCallback = [](GrGpuFinishedContext c) {
773 const auto* context = reinterpret_cast<const FinishContext*>(c);
774 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
775 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
776 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
777 context->fMappedBufferManager)) {
778 result.reset();
779 }
780 (*context->fClientCallback)(context->fClientContext, std::move(result));
781 delete context;
782 };
783 GrFlushInfo flushInfo;
784 flushInfo.fFinishedContext = finishContext;
785 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -0500786
787 dContext->priv().flushSurface(this->asSurfaceProxy(),
788 SkSurface::BackendSurfaceAccess::kNoAccess,
789 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400790}
791
Adlai Hollerc95b5892020-08-11 12:02:22 -0400792void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(GrDirectContext* dContext,
793 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400794 sk_sp<SkColorSpace> dstColorSpace,
795 const SkIRect& srcRect,
796 SkISize dstSize,
797 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500798 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400799 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400800 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400801 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
802 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
803 SkASSERT(!dstSize.isZero());
804 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
805
Adlai Hollerc95b5892020-08-11 12:02:22 -0400806 if (!dContext) {
807 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400808 return;
809 }
810 auto rt = this->asRenderTargetProxy();
811 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400812 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400813 return;
814 }
815 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400816 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400817 return;
818 }
819 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400820 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400821 return;
822 }
823 int x = srcRect.fLeft;
824 int y = srcRect.fTop;
825 bool needsRescale = srcRect.size() != dstSize;
826 GrSurfaceProxyView srcView;
Brian Salomonbacbb922021-01-21 19:48:00 -0500827 auto info = SkImageInfo::Make(dstSize,
828 kRGBA_8888_SkColorType,
829 this->colorInfo().alphaType(),
830 dstColorSpace);
Brian Salomon63a0a752020-06-26 13:32:09 -0400831 if (needsRescale) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400832 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomonbacbb922021-01-21 19:48:00 -0500833 auto tempFC = this->rescale(info,
834 kTopLeft_GrSurfaceOrigin,
835 srcRect,
836 rescaleGamma,
837 rescaleMode);
838 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400839 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400840 return;
841 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500842 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
843 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400844 x = y = 0;
Brian Salomonbacbb922021-01-21 19:48:00 -0500845 srcView = tempFC->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400846 } else {
847 srcView = this->readSurfaceView();
848 if (!srcView.asTextureProxy()) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500849 srcView = GrSurfaceProxyView::Copy(fContext,
850 std::move(srcView),
851 GrMipmapped::kNo,
852 srcRect,
853 SkBackingFit::kApprox,
854 SkBudgeted::kYes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400855 if (!srcView) {
856 // If we can't get a texture copy of the contents then give up.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400857 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400858 return;
859 }
860 SkASSERT(srcView.asTextureProxy());
861 x = y = 0;
862 }
863 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomonbacbb922021-01-21 19:48:00 -0500864 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo(),
865 info.colorInfo());
Brian Salomon63a0a752020-06-26 13:32:09 -0400866 if (xform) {
867 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
Brian Salomonbacbb922021-01-21 19:48:00 -0500868 auto tempFC = GrSurfaceFillContext::Make(dContext,
869 info,
870 SkBackingFit::kApprox,
871 1,
872 GrMipmapped::kNo,
873 GrProtected::kNo,
874 kTopLeft_GrSurfaceOrigin);
875 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400876 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400877 return;
878 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500879 auto fp = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType());
880 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
881 tempFC->fillRectToRectWithFP(srcRectToDraw,
882 SkIRect::MakeSize(tempFC->dimensions()),
883 std::move(fp));
884 srcView = tempFC->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400885 SkASSERT(srcView.asTextureProxy());
886 x = y = 0;
887 }
888 }
889
Brian Salomonbacbb922021-01-21 19:48:00 -0500890 auto yInfo = SkImageInfo::MakeA8(dstSize);
891 auto yFC = GrSurfaceFillContext::MakeWithFallback(dContext, yInfo, SkBackingFit::kApprox);
892
893 auto uvInfo = yInfo.makeWH(yInfo.width()/2, yInfo.height()/2);
894 auto uFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
895 auto vFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
896
897 if (!yFC || !uFC || !vFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400898 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400899 return;
900 }
901
902 float baseM[20];
903 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
904
905 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
906
907 auto texMatrix = SkMatrix::Translate(x, y);
908
Brian Salomon63a0a752020-06-26 13:32:09 -0400909 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
910 PixelTransferResult yTransfer, uTransfer, vTransfer;
911
912 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
913 float yM[20];
914 std::fill_n(yM, 15, 0.f);
915 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500916
917 auto yFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
918 yFP = GrColorMatrixFragmentProcessor::Make(std::move(yFP),
919 yM,
920 /*unpremulInput=*/false,
921 /*clampRGBOutput=*/true,
922 /*premulOutput=*/false);
923 yFC->fillWithFP(std::move(yFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400924 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500925 yTransfer = yFC->transferPixels(GrColorType::kAlpha_8,
926 SkIRect::MakeSize(yFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400927 if (!yTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400928 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400929 return;
930 }
931 }
932
933 texMatrix.preScale(2.f, 2.f);
934 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
935 float uM[20];
936 std::fill_n(uM, 15, 0.f);
937 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500938
939 auto uFP = GrTextureEffect::Make(srcView,
940 this->colorInfo().alphaType(),
941 texMatrix,
942 GrSamplerState::Filter::kLinear);
943 uFP = GrColorMatrixFragmentProcessor::Make(std::move(uFP),
944 uM,
945 /*unpremulInput=*/false,
946 /*clampRGBOutput=*/true,
947 /*premulOutput=*/false);
948 uFC->fillWithFP(std::move(uFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400949 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500950 uTransfer = uFC->transferPixels(GrColorType::kAlpha_8,
951 SkIRect::MakeSize(uFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400952 if (!uTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400953 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400954 return;
955 }
956 }
957
958 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
959 float vM[20];
960 std::fill_n(vM, 15, 0.f);
961 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500962 auto vFP = GrTextureEffect::Make(std::move(srcView),
963 this->colorInfo().alphaType(),
964 texMatrix,
965 GrSamplerState::Filter::kLinear);
966 vFP = GrColorMatrixFragmentProcessor::Make(std::move(vFP),
967 vM,
968 /*unpremulInput=*/false,
969 /*clampRGBOutput=*/true,
970 /*premulOutput=*/false);
971 vFC->fillWithFP(std::move(vFP));
972
Brian Salomon63a0a752020-06-26 13:32:09 -0400973 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500974 vTransfer = vFC->transferPixels(GrColorType::kAlpha_8,
975 SkIRect::MakeSize(vFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400976 if (!vTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400977 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400978 return;
979 }
980 }
981
982 if (doSynchronousRead) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500983 GrPixmap yPmp = GrPixmap::Allocate(yInfo);
984 GrPixmap uPmp = GrPixmap::Allocate(uvInfo);
985 GrPixmap vPmp = GrPixmap::Allocate(uvInfo);
Brian Salomonbacbb922021-01-21 19:48:00 -0500986 if (!yFC->readPixels(dContext, yPmp, {0, 0}) ||
987 !uFC->readPixels(dContext, uPmp, {0, 0}) ||
988 !vFC->readPixels(dContext, vPmp, {0, 0})) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400989 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400990 return;
991 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400992 auto result = std::make_unique<AsyncReadResult>(dContext->priv().contextID());
Brian Salomonbe1084b2021-01-26 13:29:30 -0500993 result->addCpuPlane(yPmp.pixelStorage(), yPmp.rowBytes());
994 result->addCpuPlane(uPmp.pixelStorage(), uPmp.rowBytes());
995 result->addCpuPlane(vPmp.pixelStorage(), vPmp.rowBytes());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400996 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400997 return;
998 }
999
1000 struct FinishContext {
1001 ReadPixelsCallback* fClientCallback;
1002 ReadPixelsContext fClientContext;
1003 GrClientMappedBufferManager* fMappedBufferManager;
1004 SkISize fSize;
1005 PixelTransferResult fYTransfer;
1006 PixelTransferResult fUTransfer;
1007 PixelTransferResult fVTransfer;
1008 };
1009 // Assumption is that the caller would like to flush. We could take a parameter or require an
1010 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1011 // callback to GrGpu until after the next flush that flushes our op list, though.
1012 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -04001013 callbackContext,
1014 dContext->priv().clientMappedBufferManager(),
Brian Salomon63a0a752020-06-26 13:32:09 -04001015 dstSize,
1016 std::move(yTransfer),
1017 std::move(uTransfer),
1018 std::move(vTransfer)};
1019 auto finishCallback = [](GrGpuFinishedContext c) {
1020 const auto* context = reinterpret_cast<const FinishContext*>(c);
1021 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
1022 auto manager = context->fMappedBufferManager;
1023 size_t rowBytes = SkToSizeT(context->fSize.width());
1024 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
1025 (*context->fClientCallback)(context->fClientContext, nullptr);
1026 delete context;
1027 return;
1028 }
1029 rowBytes /= 2;
1030 SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
1031 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
1032 (*context->fClientCallback)(context->fClientContext, nullptr);
1033 delete context;
1034 return;
1035 }
1036 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
1037 (*context->fClientCallback)(context->fClientContext, nullptr);
1038 delete context;
1039 return;
1040 }
1041 (*context->fClientCallback)(context->fClientContext, std::move(result));
1042 delete context;
1043 };
1044 GrFlushInfo flushInfo;
1045 flushInfo.fFinishedContext = finishContext;
1046 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -05001047 dContext->priv().flushSurface(this->asSurfaceProxy(),
1048 SkSurface::BackendSurfaceAccess::kNoAccess,
1049 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -04001050}
1051
Brian Salomon982127b2021-01-21 10:43:35 -05001052bool GrSurfaceContext::copy(sk_sp<GrSurfaceProxy> src, SkIRect srcRect, SkIPoint dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001053 ASSERT_SINGLE_OWNER
1054 RETURN_FALSE_IF_ABANDONED
1055 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -04001056 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -04001057
Brian Salomon947efe22019-07-16 15:36:11 -04001058 const GrCaps* caps = fContext->priv().caps();
1059
Greg Daniel46cfbc62019-06-07 11:43:30 -04001060 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -05001061 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -04001062
Stephen White3c0a50f2020-01-16 18:19:54 -05001063 if (this->asSurfaceProxy()->framebufferOnly()) {
1064 return false;
1065 }
1066
Brian Salomon982127b2021-01-21 10:43:35 -05001067 if (!caps->canCopySurface(this->asSurfaceProxy(), src.get(), srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -04001068 return false;
1069 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001070
Brian Salomon982127b2021-01-21 10:43:35 -05001071 return this->drawingManager()->newCopyRenderTask(std::move(src),
1072 srcRect,
1073 this->asSurfaceProxyRef(),
Brian Salomon0f9f8002021-01-22 16:30:50 -05001074 dstPoint,
1075 this->origin());
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001076}
Greg Daniel46cfbc62019-06-07 11:43:30 -04001077
Brian Salomonbacbb922021-01-21 19:48:00 -05001078std::unique_ptr<GrSurfaceFillContext> GrSurfaceContext::rescale(const GrImageInfo& info,
Brian Salomoneebe7352020-12-09 16:37:04 -05001079 GrSurfaceOrigin origin,
1080 SkIRect srcRect,
1081 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001082 RescaleMode rescaleMode) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001083 auto sfc = GrSurfaceFillContext::MakeWithFallback(fContext,
1084 info,
Brian Salomon1c86b632020-12-11 12:36:01 -05001085 SkBackingFit::kExact,
Brian Salomon1c86b632020-12-11 12:36:01 -05001086 1,
1087 GrMipmapped::kNo,
1088 this->asSurfaceProxy()->isProtected(),
1089 origin);
Brian Salomonbacbb922021-01-21 19:48:00 -05001090 if (!sfc || !this->rescaleInto(sfc.get(),
1091 SkIRect::MakeSize(sfc->dimensions()),
Brian Salomon1c86b632020-12-11 12:36:01 -05001092 srcRect,
1093 rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001094 rescaleMode)) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001095 return nullptr;
1096 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001097 return sfc;
Brian Salomon1c86b632020-12-11 12:36:01 -05001098}
1099
Brian Salomonbacbb922021-01-21 19:48:00 -05001100bool GrSurfaceContext::rescaleInto(GrSurfaceFillContext* dst,
Brian Salomon1c86b632020-12-11 12:36:01 -05001101 SkIRect dstRect,
1102 SkIRect srcRect,
1103 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001104 RescaleMode rescaleMode) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001105 SkASSERT(dst);
1106 if (!SkIRect::MakeSize(dst->dimensions()).contains((dstRect))) {
1107 return false;
1108 }
1109
Brian Salomone9ad9982019-07-22 16:17:41 -04001110 auto rtProxy = this->asRenderTargetProxy();
1111 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001112 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001113 }
1114
Stephen White3c0a50f2020-01-16 18:19:54 -05001115 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001116 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001117 }
1118
Greg Daniel40903af2020-01-30 14:55:05 -05001119 GrSurfaceProxyView texView = this->readSurfaceView();
Greg Daniel40903af2020-01-30 14:55:05 -05001120 if (!texView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -04001121 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect,
Brian Salomonc5243782020-04-02 12:50:34 -04001122 SkBackingFit::kApprox, SkBudgeted::kNo);
1123 if (!texView) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001124 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001125 }
Greg Daniel40903af2020-01-30 14:55:05 -05001126 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001127 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001128 }
1129
Brian Salomon1c86b632020-12-11 12:36:01 -05001130 SkISize finalSize = dstRect.size();
1131
Brian Salomonbf6b9792019-08-21 09:38:10 -04001132 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
1133 // 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 -05001134 std::unique_ptr<GrSurfaceFillContext> tempA;
1135 std::unique_ptr<GrSurfaceFillContext> tempB;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001136
Brian Salomone9ad9982019-07-22 16:17:41 -04001137 // Assume we should ignore the rescale linear request if the surface has no color space since
1138 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon63a0a752020-06-26 13:32:09 -04001139 if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() &&
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001140 !this->colorInfo().colorSpace()->gammaIsLinear()) {
1141 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomone9ad9982019-07-22 16:17:41 -04001142 // We'll fall back to kRGBA_8888 if half float not supported.
Brian Salomonbacbb922021-01-21 19:48:00 -05001143 GrImageInfo ii(GrColorType::kRGBA_F16,
1144 dst->colorInfo().alphaType(),
1145 std::move(cs),
1146 srcRect.size());
1147 auto linearRTC = GrSurfaceFillContext::MakeWithFallback(fContext,
1148 std::move(ii),
1149 SkBackingFit::kApprox,
1150 1,
1151 GrMipmapped::kNo,
1152 GrProtected::kNo,
1153 dst->origin());
Brian Salomone9ad9982019-07-22 16:17:41 -04001154 if (!linearRTC) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001155 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001156 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001157 auto fp = GrTextureEffect::Make(std::move(texView),
1158 this->colorInfo().alphaType(),
1159 SkMatrix::Translate(srcRect.topLeft()),
1160 GrSamplerState::Filter::kNearest,
1161 GrSamplerState::MipmapMode::kNone);
1162 fp = GrColorSpaceXformEffect::Make(std::move(fp),
1163 this->colorInfo(),
1164 linearRTC->colorInfo());
1165 linearRTC->fillWithFP(std::move(fp));
Greg Daniel40903af2020-01-30 14:55:05 -05001166 texView = linearRTC->readSurfaceView();
1167 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -04001168 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001169 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001170 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001171
Brian Salomon1c86b632020-12-11 12:36:01 -05001172 while (srcRect.size() != finalSize) {
1173 SkISize nextDims = finalSize;
Mike Reed1efa14d2021-01-02 21:44:59 -05001174 if (rescaleMode != RescaleMode::kNearest) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001175 if (srcRect.width() > finalSize.width()) {
1176 nextDims.fWidth = std::max((srcRect.width() + 1)/2, finalSize.width());
1177 } else if (srcRect.width() < finalSize.width()) {
1178 nextDims.fWidth = std::min(srcRect.width()*2, finalSize.width());
Brian Salomon59f31b12020-06-04 17:27:15 -04001179 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001180 if (srcRect.height() > finalSize.height()) {
1181 nextDims.fHeight = std::max((srcRect.height() + 1)/2, finalSize.height());
1182 } else if (srcRect.height() < finalSize.height()) {
1183 nextDims.fHeight = std::min(srcRect.height()*2, finalSize.height());
Brian Salomon59f31b12020-06-04 17:27:15 -04001184 }
1185 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001186 auto input = tempA ? tempA.get() : this;
Brian Salomone9ad9982019-07-22 16:17:41 -04001187 sk_sp<GrColorSpaceXform> xform;
Brian Salomonbacbb922021-01-21 19:48:00 -05001188 GrSurfaceFillContext* stepDst;
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001189 SkIRect stepDstRect;
Brian Salomon1c86b632020-12-11 12:36:01 -05001190 if (nextDims == finalSize) {
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001191 stepDst = dst;
1192 stepDstRect = dstRect;
Brian Salomonbacbb922021-01-21 19:48:00 -05001193 xform = GrColorSpaceXform::Make(input->colorInfo(), dst->colorInfo());
Brian Salomon1c86b632020-12-11 12:36:01 -05001194 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -05001195 GrImageInfo nextInfo(input->colorInfo(), nextDims);
1196 tempB = GrSurfaceFillContext::MakeWithFallback(fContext,
1197 nextInfo,
1198 SkBackingFit::kApprox);
Brian Salomon1c86b632020-12-11 12:36:01 -05001199 if (!tempB) {
1200 return false;
1201 }
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001202 stepDst = tempB.get();
1203 stepDstRect = SkIRect::MakeSize(tempB->dimensions());
Brian Salomone9ad9982019-07-22 16:17:41 -04001204 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001205 std::unique_ptr<GrFragmentProcessor> fp;
Mike Reed1efa14d2021-01-02 21:44:59 -05001206 if (rescaleMode == RescaleMode::kRepeatedCubic) {
Brian Salomon59f31b12020-06-04 17:27:15 -04001207 auto dir = GrBicubicEffect::Direction::kXY;
1208 if (nextDims.width() == srcRect.width()) {
1209 dir = GrBicubicEffect::Direction::kY;
1210 } else if (nextDims.height() == srcRect.height()) {
1211 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -04001212 }
Brian Salomon1af72d12020-06-25 10:47:26 -04001213 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
Mike Reed3867c702020-09-01 13:28:10 -04001214 static constexpr auto kKernel = GrBicubicEffect::gCatmullRom;
Brian Salomon1c86b632020-12-11 12:36:01 -05001215 fp = GrBicubicEffect::MakeSubset(std::move(texView),
1216 input->colorInfo().alphaType(),
Brian Salomonbacbb922021-01-21 19:48:00 -05001217 SkMatrix::I(),
Brian Salomon1c86b632020-12-11 12:36:01 -05001218 kWM,
1219 kWM,
1220 SkRect::Make(srcRect),
1221 kKernel,
1222 dir,
1223 *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -04001224 } else {
Mike Reed1efa14d2021-01-02 21:44:59 -05001225 auto filter = rescaleMode == RescaleMode::kNearest ? GrSamplerState::Filter::kNearest
1226 : GrSamplerState::Filter::kLinear;
Brian Salomonbacbb922021-01-21 19:48:00 -05001227 auto srcRectF = SkRect::Make(srcRect);
1228 fp = GrTextureEffect::MakeSubset(std::move(texView),
1229 this->colorInfo().alphaType(),
1230 SkMatrix::I(),
1231 {filter, GrSamplerState::MipmapMode::kNone},
1232 srcRectF,
1233 srcRectF,
1234 *this->caps());
Brian Salomone9ad9982019-07-22 16:17:41 -04001235 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001236 if (xform) {
1237 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
1238 }
1239 stepDst->fillRectToRectWithFP(srcRect, stepDstRect, std::move(fp));
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001240 texView = stepDst->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -04001241 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001242 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomone9ad9982019-07-22 16:17:41 -04001243 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001244 return true;
Brian Salomone9ad9982019-07-22 16:17:41 -04001245}
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001246
1247GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
1248 const SkIRect& rect) {
1249 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1250 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001251 auto direct = fContext->asDirectContext();
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001252 if (!direct) {
1253 return {};
1254 }
1255 auto rtProxy = this->asRenderTargetProxy();
1256 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1257 return {};
1258 }
1259
1260 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001261 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
1262 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001263 // Fail if read color type does not have all of dstCT's color channels and those missing color
1264 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001265 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1266 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
1267 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1268 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001269 return {};
1270 }
1271
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001272 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001273 !supportedRead.fOffsetAlignmentForTransferBuffer) {
1274 return {};
1275 }
1276
1277 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
1278 size_t size = rowBytes * rect.height();
1279 auto buffer = direct->priv().resourceProvider()->createBuffer(
1280 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
1281 if (!buffer) {
1282 return {};
1283 }
1284 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -05001285 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001286 if (flip) {
1287 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1288 this->height() - rect.fTop);
1289 }
Greg Danielbbfec9d2019-08-20 10:56:51 -04001290 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001291 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -04001292 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001293 PixelTransferResult result;
1294 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001295 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -04001296 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001297 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
1298 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001299 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
1300 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001301 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
1302 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -04001303 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001304 };
1305 }
1306 return result;
1307}
Greg Daniel46e366a2019-12-16 14:38:36 -05001308
1309#ifdef SK_DEBUG
1310void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001311 SkASSERT(fReadView.proxy());
1312 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -04001313 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
1314 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
1315 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
1316 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001317 this->onValidate();
1318}
1319#endif