blob: 38a848217bf0b579780b20b44b88464037d0089c [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 Salomon27efe6c2021-01-22 09:31:12 -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 Salomon27efe6c2021-01-22 09:31:12 -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 Salomon27efe6c2021-01-22 09:31:12 -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 Salomon27efe6c2021-01-22 09:31:12 -0500635 fPlanes[i].releaseMappedBuffer(fInboxID);
Brian Salomon63a0a752020-06-26 13:32:09 -0400636 }
637 }
638
639 int count() const override { return fPlanes.count(); }
Brian Salomon27efe6c2021-01-22 09:31:12 -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 Salomon27efe6c2021-01-22 09:31:12 -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 Salomon27efe6c2021-01-22 09:31:12 -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 Salomon27efe6c2021-01-22 09:31:12 -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 Salomon27efe6c2021-01-22 09:31:12 -0500677 fPlanes.emplace_back(std::move(mappedBuffer), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400678 }
679
Brian Salomon27efe6c2021-01-22 09:31:12 -0500680 class Plane {
681 public:
682 Plane(sk_sp<GrGpuBuffer> buffer, size_t rowBytes)
683 : fMappedBuffer(std::move(buffer)), fRowBytes(rowBytes) {}
684
685 Plane(sk_sp<SkData> data, size_t rowBytes) : fData(std::move(data)), fRowBytes(rowBytes) {}
686
687 ~Plane() { SkASSERT(!fMappedBuffer); }
688
689 void releaseMappedBuffer(uint32_t inboxID) {
690 if (fMappedBuffer) {
691 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
692 {std::move(fMappedBuffer), inboxID});
693 }
694 }
695
696 const void* data() const {
697 if (fMappedBuffer) {
698 SkASSERT(!fData);
699 SkASSERT(fMappedBuffer->isMapped());
700 return fMappedBuffer->map();
701 }
702 SkASSERT(fData);
703 return fData->data();
704 }
705
706 size_t rowBytes() const { return fRowBytes; }
707
708 private:
709 sk_sp<SkData> fData;
Brian Salomon63a0a752020-06-26 13:32:09 -0400710 sk_sp<GrGpuBuffer> fMappedBuffer;
Brian Salomon27efe6c2021-01-22 09:31:12 -0500711 size_t fRowBytes;
Brian Salomon63a0a752020-06-26 13:32:09 -0400712 };
713 SkSTArray<3, Plane> fPlanes;
714 uint32_t fInboxID;
715};
716
Adlai Hollerc95b5892020-08-11 12:02:22 -0400717void GrSurfaceContext::asyncReadPixels(GrDirectContext* dContext,
718 const SkIRect& rect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400719 SkColorType colorType,
720 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400721 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400722 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
723 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
724
Adlai Hollerc95b5892020-08-11 12:02:22 -0400725 if (!dContext || this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
726 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400727 return;
728 }
729
Adlai Hollerc95b5892020-08-11 12:02:22 -0400730 auto mappedBufferManager = dContext->priv().clientMappedBufferManager();
Brian Salomon63a0a752020-06-26 13:32:09 -0400731
732 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
733
734 if (!transferResult.fTransferBuffer) {
735 auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
736 this->colorInfo().refColorSpace());
737 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomon27efe6c2021-01-22 09:31:12 -0500738 GrPixmap pm = GrPixmap::Allocate(ii);
739 result->addCpuPlane(pm.pixelStorage(), pm.rowBytes());
Brian Salomon63a0a752020-06-26 13:32:09 -0400740
Adlai Hollerc95b5892020-08-11 12:02:22 -0400741 SkIPoint pt{rect.fLeft, rect.fTop};
Brian Salomondd4087d2020-12-23 20:36:44 -0500742 if (!this->readPixels(dContext, pm, pt)) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400743 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400744 return;
745 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400746 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400747 return;
748 }
749
750 struct FinishContext {
751 ReadPixelsCallback* fClientCallback;
752 ReadPixelsContext fClientContext;
753 SkISize fSize;
754 SkColorType fColorType;
755 GrClientMappedBufferManager* fMappedBufferManager;
756 PixelTransferResult fTransferResult;
757 };
758 // Assumption is that the caller would like to flush. We could take a parameter or require an
759 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
760 // callback to GrGpu until after the next flush that flushes our op list, though.
761 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400762 callbackContext,
Brian Salomon63a0a752020-06-26 13:32:09 -0400763 rect.size(),
764 colorType,
765 mappedBufferManager,
766 std::move(transferResult)};
767 auto finishCallback = [](GrGpuFinishedContext c) {
768 const auto* context = reinterpret_cast<const FinishContext*>(c);
769 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
770 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
771 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
772 context->fMappedBufferManager)) {
773 result.reset();
774 }
775 (*context->fClientCallback)(context->fClientContext, std::move(result));
776 delete context;
777 };
778 GrFlushInfo flushInfo;
779 flushInfo.fFinishedContext = finishContext;
780 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -0500781
782 dContext->priv().flushSurface(this->asSurfaceProxy(),
783 SkSurface::BackendSurfaceAccess::kNoAccess,
784 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400785}
786
Adlai Hollerc95b5892020-08-11 12:02:22 -0400787void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(GrDirectContext* dContext,
788 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400789 sk_sp<SkColorSpace> dstColorSpace,
790 const SkIRect& srcRect,
791 SkISize dstSize,
792 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500793 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400794 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400795 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400796 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
797 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
798 SkASSERT(!dstSize.isZero());
799 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
800
Adlai Hollerc95b5892020-08-11 12:02:22 -0400801 if (!dContext) {
802 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400803 return;
804 }
805 auto rt = this->asRenderTargetProxy();
806 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400807 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400808 return;
809 }
810 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400811 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400812 return;
813 }
814 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400815 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400816 return;
817 }
818 int x = srcRect.fLeft;
819 int y = srcRect.fTop;
820 bool needsRescale = srcRect.size() != dstSize;
821 GrSurfaceProxyView srcView;
Brian Salomonbacbb922021-01-21 19:48:00 -0500822 auto info = SkImageInfo::Make(dstSize,
823 kRGBA_8888_SkColorType,
824 this->colorInfo().alphaType(),
825 dstColorSpace);
Brian Salomon63a0a752020-06-26 13:32:09 -0400826 if (needsRescale) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400827 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomonbacbb922021-01-21 19:48:00 -0500828 auto tempFC = this->rescale(info,
829 kTopLeft_GrSurfaceOrigin,
830 srcRect,
831 rescaleGamma,
832 rescaleMode);
833 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400834 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400835 return;
836 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500837 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
838 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400839 x = y = 0;
Brian Salomonbacbb922021-01-21 19:48:00 -0500840 srcView = tempFC->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400841 } else {
842 srcView = this->readSurfaceView();
843 if (!srcView.asTextureProxy()) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500844 srcView = GrSurfaceProxyView::Copy(fContext,
845 std::move(srcView),
846 GrMipmapped::kNo,
847 srcRect,
848 SkBackingFit::kApprox,
849 SkBudgeted::kYes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400850 if (!srcView) {
851 // If we can't get a texture copy of the contents then give up.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400852 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400853 return;
854 }
855 SkASSERT(srcView.asTextureProxy());
856 x = y = 0;
857 }
858 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomonbacbb922021-01-21 19:48:00 -0500859 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo(),
860 info.colorInfo());
Brian Salomon63a0a752020-06-26 13:32:09 -0400861 if (xform) {
862 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
Brian Salomonbacbb922021-01-21 19:48:00 -0500863 auto tempFC = GrSurfaceFillContext::Make(dContext,
864 info,
865 SkBackingFit::kApprox,
866 1,
867 GrMipmapped::kNo,
868 GrProtected::kNo,
869 kTopLeft_GrSurfaceOrigin);
870 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400871 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400872 return;
873 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500874 auto fp = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType());
875 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
876 tempFC->fillRectToRectWithFP(srcRectToDraw,
877 SkIRect::MakeSize(tempFC->dimensions()),
878 std::move(fp));
879 srcView = tempFC->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400880 SkASSERT(srcView.asTextureProxy());
881 x = y = 0;
882 }
883 }
884
Brian Salomonbacbb922021-01-21 19:48:00 -0500885 auto yInfo = SkImageInfo::MakeA8(dstSize);
886 auto yFC = GrSurfaceFillContext::MakeWithFallback(dContext, yInfo, SkBackingFit::kApprox);
887
888 auto uvInfo = yInfo.makeWH(yInfo.width()/2, yInfo.height()/2);
889 auto uFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
890 auto vFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
891
892 if (!yFC || !uFC || !vFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400893 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400894 return;
895 }
896
897 float baseM[20];
898 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
899
900 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
901
902 auto texMatrix = SkMatrix::Translate(x, y);
903
Brian Salomon63a0a752020-06-26 13:32:09 -0400904 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
905 PixelTransferResult yTransfer, uTransfer, vTransfer;
906
907 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
908 float yM[20];
909 std::fill_n(yM, 15, 0.f);
910 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500911
912 auto yFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
913 yFP = GrColorMatrixFragmentProcessor::Make(std::move(yFP),
914 yM,
915 /*unpremulInput=*/false,
916 /*clampRGBOutput=*/true,
917 /*premulOutput=*/false);
918 yFC->fillWithFP(std::move(yFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400919 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500920 yTransfer = yFC->transferPixels(GrColorType::kAlpha_8,
921 SkIRect::MakeSize(yFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400922 if (!yTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400923 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400924 return;
925 }
926 }
927
928 texMatrix.preScale(2.f, 2.f);
929 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
930 float uM[20];
931 std::fill_n(uM, 15, 0.f);
932 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500933
934 auto uFP = GrTextureEffect::Make(srcView,
935 this->colorInfo().alphaType(),
936 texMatrix,
937 GrSamplerState::Filter::kLinear);
938 uFP = GrColorMatrixFragmentProcessor::Make(std::move(uFP),
939 uM,
940 /*unpremulInput=*/false,
941 /*clampRGBOutput=*/true,
942 /*premulOutput=*/false);
943 uFC->fillWithFP(std::move(uFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400944 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500945 uTransfer = uFC->transferPixels(GrColorType::kAlpha_8,
946 SkIRect::MakeSize(uFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400947 if (!uTransfer.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 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
954 float vM[20];
955 std::fill_n(vM, 15, 0.f);
956 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500957 auto vFP = GrTextureEffect::Make(std::move(srcView),
958 this->colorInfo().alphaType(),
959 texMatrix,
960 GrSamplerState::Filter::kLinear);
961 vFP = GrColorMatrixFragmentProcessor::Make(std::move(vFP),
962 vM,
963 /*unpremulInput=*/false,
964 /*clampRGBOutput=*/true,
965 /*premulOutput=*/false);
966 vFC->fillWithFP(std::move(vFP));
967
Brian Salomon63a0a752020-06-26 13:32:09 -0400968 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500969 vTransfer = vFC->transferPixels(GrColorType::kAlpha_8,
970 SkIRect::MakeSize(vFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400971 if (!vTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400972 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400973 return;
974 }
975 }
976
977 if (doSynchronousRead) {
Brian Salomon27efe6c2021-01-22 09:31:12 -0500978 GrPixmap yPmp = GrPixmap::Allocate(yInfo);
979 GrPixmap uPmp = GrPixmap::Allocate(uvInfo);
980 GrPixmap vPmp = GrPixmap::Allocate(uvInfo);
Brian Salomonbacbb922021-01-21 19:48:00 -0500981 if (!yFC->readPixels(dContext, yPmp, {0, 0}) ||
982 !uFC->readPixels(dContext, uPmp, {0, 0}) ||
983 !vFC->readPixels(dContext, vPmp, {0, 0})) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400984 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400985 return;
986 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400987 auto result = std::make_unique<AsyncReadResult>(dContext->priv().contextID());
Brian Salomon27efe6c2021-01-22 09:31:12 -0500988 result->addCpuPlane(yPmp.pixelStorage(), yPmp.rowBytes());
989 result->addCpuPlane(uPmp.pixelStorage(), uPmp.rowBytes());
990 result->addCpuPlane(vPmp.pixelStorage(), vPmp.rowBytes());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400991 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400992 return;
993 }
994
995 struct FinishContext {
996 ReadPixelsCallback* fClientCallback;
997 ReadPixelsContext fClientContext;
998 GrClientMappedBufferManager* fMappedBufferManager;
999 SkISize fSize;
1000 PixelTransferResult fYTransfer;
1001 PixelTransferResult fUTransfer;
1002 PixelTransferResult fVTransfer;
1003 };
1004 // Assumption is that the caller would like to flush. We could take a parameter or require an
1005 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1006 // callback to GrGpu until after the next flush that flushes our op list, though.
1007 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -04001008 callbackContext,
1009 dContext->priv().clientMappedBufferManager(),
Brian Salomon63a0a752020-06-26 13:32:09 -04001010 dstSize,
1011 std::move(yTransfer),
1012 std::move(uTransfer),
1013 std::move(vTransfer)};
1014 auto finishCallback = [](GrGpuFinishedContext c) {
1015 const auto* context = reinterpret_cast<const FinishContext*>(c);
1016 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
1017 auto manager = context->fMappedBufferManager;
1018 size_t rowBytes = SkToSizeT(context->fSize.width());
1019 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
1020 (*context->fClientCallback)(context->fClientContext, nullptr);
1021 delete context;
1022 return;
1023 }
1024 rowBytes /= 2;
1025 SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
1026 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
1027 (*context->fClientCallback)(context->fClientContext, nullptr);
1028 delete context;
1029 return;
1030 }
1031 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
1032 (*context->fClientCallback)(context->fClientContext, nullptr);
1033 delete context;
1034 return;
1035 }
1036 (*context->fClientCallback)(context->fClientContext, std::move(result));
1037 delete context;
1038 };
1039 GrFlushInfo flushInfo;
1040 flushInfo.fFinishedContext = finishContext;
1041 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -05001042 dContext->priv().flushSurface(this->asSurfaceProxy(),
1043 SkSurface::BackendSurfaceAccess::kNoAccess,
1044 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -04001045}
1046
Brian Salomon982127b2021-01-21 10:43:35 -05001047bool GrSurfaceContext::copy(sk_sp<GrSurfaceProxy> src, SkIRect srcRect, SkIPoint dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001048 ASSERT_SINGLE_OWNER
1049 RETURN_FALSE_IF_ABANDONED
1050 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -04001051 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -04001052
Brian Salomon947efe22019-07-16 15:36:11 -04001053 const GrCaps* caps = fContext->priv().caps();
1054
Greg Daniel46cfbc62019-06-07 11:43:30 -04001055 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -05001056 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -04001057
Stephen White3c0a50f2020-01-16 18:19:54 -05001058 if (this->asSurfaceProxy()->framebufferOnly()) {
1059 return false;
1060 }
1061
Brian Salomon982127b2021-01-21 10:43:35 -05001062 if (!caps->canCopySurface(this->asSurfaceProxy(), src.get(), srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -04001063 return false;
1064 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001065
Brian Salomon982127b2021-01-21 10:43:35 -05001066 return this->drawingManager()->newCopyRenderTask(std::move(src),
1067 srcRect,
1068 this->asSurfaceProxyRef(),
Brian Salomon0f9f8002021-01-22 16:30:50 -05001069 dstPoint,
1070 this->origin());
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001071}
Greg Daniel46cfbc62019-06-07 11:43:30 -04001072
Brian Salomonbacbb922021-01-21 19:48:00 -05001073std::unique_ptr<GrSurfaceFillContext> GrSurfaceContext::rescale(const GrImageInfo& info,
Brian Salomoneebe7352020-12-09 16:37:04 -05001074 GrSurfaceOrigin origin,
1075 SkIRect srcRect,
1076 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001077 RescaleMode rescaleMode) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001078 auto sfc = GrSurfaceFillContext::MakeWithFallback(fContext,
1079 info,
Brian Salomon1c86b632020-12-11 12:36:01 -05001080 SkBackingFit::kExact,
Brian Salomon1c86b632020-12-11 12:36:01 -05001081 1,
1082 GrMipmapped::kNo,
1083 this->asSurfaceProxy()->isProtected(),
1084 origin);
Brian Salomonbacbb922021-01-21 19:48:00 -05001085 if (!sfc || !this->rescaleInto(sfc.get(),
1086 SkIRect::MakeSize(sfc->dimensions()),
Brian Salomon1c86b632020-12-11 12:36:01 -05001087 srcRect,
1088 rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001089 rescaleMode)) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001090 return nullptr;
1091 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001092 return sfc;
Brian Salomon1c86b632020-12-11 12:36:01 -05001093}
1094
Brian Salomonbacbb922021-01-21 19:48:00 -05001095bool GrSurfaceContext::rescaleInto(GrSurfaceFillContext* dst,
Brian Salomon1c86b632020-12-11 12:36:01 -05001096 SkIRect dstRect,
1097 SkIRect srcRect,
1098 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001099 RescaleMode rescaleMode) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001100 SkASSERT(dst);
1101 if (!SkIRect::MakeSize(dst->dimensions()).contains((dstRect))) {
1102 return false;
1103 }
1104
Brian Salomone9ad9982019-07-22 16:17:41 -04001105 auto rtProxy = this->asRenderTargetProxy();
1106 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001107 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001108 }
1109
Stephen White3c0a50f2020-01-16 18:19:54 -05001110 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001111 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001112 }
1113
Greg Daniel40903af2020-01-30 14:55:05 -05001114 GrSurfaceProxyView texView = this->readSurfaceView();
Greg Daniel40903af2020-01-30 14:55:05 -05001115 if (!texView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -04001116 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect,
Brian Salomonc5243782020-04-02 12:50:34 -04001117 SkBackingFit::kApprox, SkBudgeted::kNo);
1118 if (!texView) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001119 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001120 }
Greg Daniel40903af2020-01-30 14:55:05 -05001121 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001122 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001123 }
1124
Brian Salomon1c86b632020-12-11 12:36:01 -05001125 SkISize finalSize = dstRect.size();
1126
Brian Salomonbf6b9792019-08-21 09:38:10 -04001127 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
1128 // 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 -05001129 std::unique_ptr<GrSurfaceFillContext> tempA;
1130 std::unique_ptr<GrSurfaceFillContext> tempB;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001131
Brian Salomone9ad9982019-07-22 16:17:41 -04001132 // Assume we should ignore the rescale linear request if the surface has no color space since
1133 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon63a0a752020-06-26 13:32:09 -04001134 if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() &&
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001135 !this->colorInfo().colorSpace()->gammaIsLinear()) {
1136 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomone9ad9982019-07-22 16:17:41 -04001137 // We'll fall back to kRGBA_8888 if half float not supported.
Brian Salomonbacbb922021-01-21 19:48:00 -05001138 GrImageInfo ii(GrColorType::kRGBA_F16,
1139 dst->colorInfo().alphaType(),
1140 std::move(cs),
1141 srcRect.size());
1142 auto linearRTC = GrSurfaceFillContext::MakeWithFallback(fContext,
1143 std::move(ii),
1144 SkBackingFit::kApprox,
1145 1,
1146 GrMipmapped::kNo,
1147 GrProtected::kNo,
1148 dst->origin());
Brian Salomone9ad9982019-07-22 16:17:41 -04001149 if (!linearRTC) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001150 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001151 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001152 auto fp = GrTextureEffect::Make(std::move(texView),
1153 this->colorInfo().alphaType(),
1154 SkMatrix::Translate(srcRect.topLeft()),
1155 GrSamplerState::Filter::kNearest,
1156 GrSamplerState::MipmapMode::kNone);
1157 fp = GrColorSpaceXformEffect::Make(std::move(fp),
1158 this->colorInfo(),
1159 linearRTC->colorInfo());
1160 linearRTC->fillWithFP(std::move(fp));
Greg Daniel40903af2020-01-30 14:55:05 -05001161 texView = linearRTC->readSurfaceView();
1162 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -04001163 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001164 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001165 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001166
Brian Salomon1c86b632020-12-11 12:36:01 -05001167 while (srcRect.size() != finalSize) {
1168 SkISize nextDims = finalSize;
Mike Reed1efa14d2021-01-02 21:44:59 -05001169 if (rescaleMode != RescaleMode::kNearest) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001170 if (srcRect.width() > finalSize.width()) {
1171 nextDims.fWidth = std::max((srcRect.width() + 1)/2, finalSize.width());
1172 } else if (srcRect.width() < finalSize.width()) {
1173 nextDims.fWidth = std::min(srcRect.width()*2, finalSize.width());
Brian Salomon59f31b12020-06-04 17:27:15 -04001174 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001175 if (srcRect.height() > finalSize.height()) {
1176 nextDims.fHeight = std::max((srcRect.height() + 1)/2, finalSize.height());
1177 } else if (srcRect.height() < finalSize.height()) {
1178 nextDims.fHeight = std::min(srcRect.height()*2, finalSize.height());
Brian Salomon59f31b12020-06-04 17:27:15 -04001179 }
1180 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001181 auto input = tempA ? tempA.get() : this;
Brian Salomone9ad9982019-07-22 16:17:41 -04001182 sk_sp<GrColorSpaceXform> xform;
Brian Salomonbacbb922021-01-21 19:48:00 -05001183 GrSurfaceFillContext* stepDst;
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001184 SkIRect stepDstRect;
Brian Salomon1c86b632020-12-11 12:36:01 -05001185 if (nextDims == finalSize) {
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001186 stepDst = dst;
1187 stepDstRect = dstRect;
Brian Salomonbacbb922021-01-21 19:48:00 -05001188 xform = GrColorSpaceXform::Make(input->colorInfo(), dst->colorInfo());
Brian Salomon1c86b632020-12-11 12:36:01 -05001189 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -05001190 GrImageInfo nextInfo(input->colorInfo(), nextDims);
1191 tempB = GrSurfaceFillContext::MakeWithFallback(fContext,
1192 nextInfo,
1193 SkBackingFit::kApprox);
Brian Salomon1c86b632020-12-11 12:36:01 -05001194 if (!tempB) {
1195 return false;
1196 }
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001197 stepDst = tempB.get();
1198 stepDstRect = SkIRect::MakeSize(tempB->dimensions());
Brian Salomone9ad9982019-07-22 16:17:41 -04001199 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001200 std::unique_ptr<GrFragmentProcessor> fp;
Mike Reed1efa14d2021-01-02 21:44:59 -05001201 if (rescaleMode == RescaleMode::kRepeatedCubic) {
Brian Salomon59f31b12020-06-04 17:27:15 -04001202 auto dir = GrBicubicEffect::Direction::kXY;
1203 if (nextDims.width() == srcRect.width()) {
1204 dir = GrBicubicEffect::Direction::kY;
1205 } else if (nextDims.height() == srcRect.height()) {
1206 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -04001207 }
Brian Salomon1af72d12020-06-25 10:47:26 -04001208 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
Mike Reed3867c702020-09-01 13:28:10 -04001209 static constexpr auto kKernel = GrBicubicEffect::gCatmullRom;
Brian Salomon1c86b632020-12-11 12:36:01 -05001210 fp = GrBicubicEffect::MakeSubset(std::move(texView),
1211 input->colorInfo().alphaType(),
Brian Salomonbacbb922021-01-21 19:48:00 -05001212 SkMatrix::I(),
Brian Salomon1c86b632020-12-11 12:36:01 -05001213 kWM,
1214 kWM,
1215 SkRect::Make(srcRect),
1216 kKernel,
1217 dir,
1218 *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -04001219 } else {
Mike Reed1efa14d2021-01-02 21:44:59 -05001220 auto filter = rescaleMode == RescaleMode::kNearest ? GrSamplerState::Filter::kNearest
1221 : GrSamplerState::Filter::kLinear;
Brian Salomonbacbb922021-01-21 19:48:00 -05001222 auto srcRectF = SkRect::Make(srcRect);
1223 fp = GrTextureEffect::MakeSubset(std::move(texView),
1224 this->colorInfo().alphaType(),
1225 SkMatrix::I(),
1226 {filter, GrSamplerState::MipmapMode::kNone},
1227 srcRectF,
1228 srcRectF,
1229 *this->caps());
Brian Salomone9ad9982019-07-22 16:17:41 -04001230 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001231 if (xform) {
1232 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
1233 }
1234 stepDst->fillRectToRectWithFP(srcRect, stepDstRect, std::move(fp));
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001235 texView = stepDst->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -04001236 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001237 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomone9ad9982019-07-22 16:17:41 -04001238 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001239 return true;
Brian Salomone9ad9982019-07-22 16:17:41 -04001240}
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001241
1242GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
1243 const SkIRect& rect) {
1244 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1245 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001246 auto direct = fContext->asDirectContext();
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001247 if (!direct) {
1248 return {};
1249 }
1250 auto rtProxy = this->asRenderTargetProxy();
1251 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1252 return {};
1253 }
1254
1255 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001256 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
1257 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001258 // Fail if read color type does not have all of dstCT's color channels and those missing color
1259 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001260 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1261 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
1262 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1263 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001264 return {};
1265 }
1266
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001267 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001268 !supportedRead.fOffsetAlignmentForTransferBuffer) {
1269 return {};
1270 }
1271
1272 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
1273 size_t size = rowBytes * rect.height();
1274 auto buffer = direct->priv().resourceProvider()->createBuffer(
1275 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
1276 if (!buffer) {
1277 return {};
1278 }
1279 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -05001280 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001281 if (flip) {
1282 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1283 this->height() - rect.fTop);
1284 }
Greg Danielbbfec9d2019-08-20 10:56:51 -04001285 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001286 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -04001287 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001288 PixelTransferResult result;
1289 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001290 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -04001291 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001292 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
1293 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001294 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
1295 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001296 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
1297 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -04001298 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001299 };
1300 }
1301 return result;
1302}
Greg Daniel46e366a2019-12-16 14:38:36 -05001303
1304#ifdef SK_DEBUG
1305void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001306 SkASSERT(fReadView.proxy());
1307 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -04001308 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
1309 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
1310 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
1311 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001312 this->onValidate();
1313}
1314#endif