blob: aaecb78ba5f8848f53055cdc7b4282ed0798e24e [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 Salomonf30b1c12019-06-20 12:25:02 -0400497 std::unique_ptr<char[]> tmpPixels;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400498 if (convert) {
Brian Salomondd4087d2020-12-23 20:36:44 -0500499 GrImageInfo tmpInfo(allowedColorType,
500 this->colorInfo().alphaType(),
501 this->colorInfo().refColorSpace(),
502 src.dimensions());
Brian Salomon1d435302019-07-01 13:05:28 -0400503 auto tmpRB = tmpInfo.minRowBytes();
504 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
Brian Salomondd4087d2020-12-23 20:36:44 -0500505 GrPixmap tmp(tmpInfo, tmpPixels.get(), tmpRB);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400506
Brian Salomondd4087d2020-12-23 20:36:44 -0500507 SkAssertResult(GrConvertPixels(tmp, src, flip));
Brian Salomonf30b1c12019-06-20 12:25:02 -0400508
Brian Salomondd4087d2020-12-23 20:36:44 -0500509 src = tmp;
Brian Salomon1d435302019-07-01 13:05:28 -0400510 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400511 }
512
513 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
514 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
515 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
516 // destination proxy)
517 // TODO: should this policy decision just be moved into the drawing manager?
Adlai Hollerc95b5892020-08-11 12:02:22 -0400518 dContext->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400519
Brian Salomondd4087d2020-12-23 20:36:44 -0500520 return dContext->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, src.width(),
521 src.height(), this->colorInfo().colorType(),
522 src.colorType(), src.addr(), src.rowBytes());
Brian Osman45580d32016-11-23 09:37:01 -0500523}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400524
Adlai Hollerc95b5892020-08-11 12:02:22 -0400525void GrSurfaceContext::asyncRescaleAndReadPixels(GrDirectContext* dContext,
526 const SkImageInfo& info,
Brian Salomon63a0a752020-06-26 13:32:09 -0400527 const SkIRect& srcRect,
528 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500529 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400530 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400531 ReadPixelsContext callbackContext) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400532 if (!dContext) {
533 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400534 return;
535 }
536 auto rt = this->asRenderTargetProxy();
537 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400538 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400539 return;
540 }
541 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400542 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400543 return;
544 }
545 auto dstCT = SkColorTypeToGrColorType(info.colorType());
546 if (dstCT == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400547 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400548 return;
549 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500550 bool needsRescale = srcRect.size() != info.dimensions();
Brian Salomon63a0a752020-06-26 13:32:09 -0400551 auto colorTypeOfFinalContext = this->colorInfo().colorType();
552 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
553 if (needsRescale) {
554 colorTypeOfFinalContext = dstCT;
555 backendFormatOfFinalContext =
556 this->caps()->getDefaultBackendFormat(dstCT, GrRenderable::kYes);
557 }
558 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonbacbb922021-01-21 19:48:00 -0500559 backendFormatOfFinalContext,
560 dstCT);
Brian Salomon63a0a752020-06-26 13:32:09 -0400561 // Fail if we can't read from the source surface's color type.
562 if (readInfo.fColorType == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400563 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400564 return;
565 }
566 // Fail if read color type does not have all of dstCT's color channels and those missing color
567 // channels are in the src.
568 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
569 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
570 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
571 if ((~legalReadChannels & dstChannels) & srcChannels) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400572 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400573 return;
574 }
575
Brian Salomonbacbb922021-01-21 19:48:00 -0500576 std::unique_ptr<GrSurfaceFillContext> tempFC;
Brian Salomon63a0a752020-06-26 13:32:09 -0400577 int x = srcRect.fLeft;
578 int y = srcRect.fTop;
579 if (needsRescale) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500580 tempFC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma, rescaleMode);
581 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400582 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400583 return;
584 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500585 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
586 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400587 x = y = 0;
588 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -0500589 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo(),
590 info.colorInfo());
Brian Salomon63a0a752020-06-26 13:32:09 -0400591 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
592 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
593 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomonbacbb922021-01-21 19:48:00 -0500594 SkIRect srcRectToDraw = srcRect;
Brian Salomon63a0a752020-06-26 13:32:09 -0400595 // If the src is not texturable first try to make a copy to a texture.
596 if (!texProxyView.asTextureProxy()) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500597 texProxyView = GrSurfaceProxyView::Copy(fContext,
598 texProxyView,
599 GrMipmapped::kNo,
600 srcRect,
601 SkBackingFit::kApprox,
602 SkBudgeted::kNo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400603 if (!texProxyView) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400604 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400605 return;
606 }
607 SkASSERT(texProxyView.asTextureProxy());
Brian Salomonbacbb922021-01-21 19:48:00 -0500608 srcRectToDraw = SkIRect::MakeSize(srcRect.size());
Brian Salomon63a0a752020-06-26 13:32:09 -0400609 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500610 auto tempInfo = GrImageInfo(info).makeColorType(this->colorInfo().colorType());
611 tempFC = GrSurfaceFillContext::Make(dContext, tempInfo, SkBackingFit::kApprox);
612 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400613 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400614 return;
615 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500616 auto fp = GrTextureEffect::Make(std::move(texProxyView), this->colorInfo().alphaType());
617 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
618 tempFC->fillRectToRectWithFP(srcRectToDraw,
619 SkIRect::MakeSize(tempFC->dimensions()),
620 std::move(fp));
Brian Salomon63a0a752020-06-26 13:32:09 -0400621 x = y = 0;
622 }
623 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500624 auto srcCtx = tempFC ? tempFC.get() : this;
625 return srcCtx->asyncReadPixels(dContext,
626 SkIRect::MakePtSize({x, y}, info.dimensions()),
627 info.colorType(),
628 callback,
629 callbackContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400630}
631
632class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult {
633public:
634 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
635 ~AsyncReadResult() override {
636 for (int i = 0; i < fPlanes.count(); ++i) {
637 if (!fPlanes[i].fMappedBuffer) {
638 delete[] static_cast<const char*>(fPlanes[i].fData);
639 } else {
640 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
641 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
642 }
643 }
644 }
645
646 int count() const override { return fPlanes.count(); }
647 const void* data(int i) const override { return fPlanes[i].fData; }
648 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
649
650 bool addTransferResult(const PixelTransferResult& result,
651 SkISize dimensions,
652 size_t rowBytes,
653 GrClientMappedBufferManager* manager) {
654 SkASSERT(!result.fTransferBuffer->isMapped());
655 const void* mappedData = result.fTransferBuffer->map();
656 if (!mappedData) {
657 return false;
658 }
659 if (result.fPixelConverter) {
660 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
661 result.fPixelConverter(convertedData.get(), mappedData);
662 this->addCpuPlane(std::move(convertedData), rowBytes);
663 result.fTransferBuffer->unmap();
664 } else {
665 manager->insert(result.fTransferBuffer);
666 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
667 }
668 return true;
669 }
670
671 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
672 SkASSERT(data);
673 SkASSERT(rowBytes > 0);
674 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
675 }
676
677private:
678 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
679 SkASSERT(data);
680 SkASSERT(rowBytes > 0);
681 SkASSERT(mappedBuffer);
682 SkASSERT(mappedBuffer->isMapped());
683 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
684 }
685
686 struct Plane {
687 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
688 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
689 const void* fData;
690 size_t fRowBytes;
691 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
692 sk_sp<GrGpuBuffer> fMappedBuffer;
693 };
694 SkSTArray<3, Plane> fPlanes;
695 uint32_t fInboxID;
696};
697
Adlai Hollerc95b5892020-08-11 12:02:22 -0400698void GrSurfaceContext::asyncReadPixels(GrDirectContext* dContext,
699 const SkIRect& rect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400700 SkColorType colorType,
701 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400702 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400703 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
704 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
705
Adlai Hollerc95b5892020-08-11 12:02:22 -0400706 if (!dContext || this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
707 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400708 return;
709 }
710
Adlai Hollerc95b5892020-08-11 12:02:22 -0400711 auto mappedBufferManager = dContext->priv().clientMappedBufferManager();
Brian Salomon63a0a752020-06-26 13:32:09 -0400712
713 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
714
715 if (!transferResult.fTransferBuffer) {
716 auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
717 this->colorInfo().refColorSpace());
718 auto result = std::make_unique<AsyncReadResult>(0);
719 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
720 SkPixmap pm(ii, data.get(), ii.minRowBytes());
721 result->addCpuPlane(std::move(data), pm.rowBytes());
722
Adlai Hollerc95b5892020-08-11 12:02:22 -0400723 SkIPoint pt{rect.fLeft, rect.fTop};
Brian Salomondd4087d2020-12-23 20:36:44 -0500724 if (!this->readPixels(dContext, pm, pt)) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400725 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400726 return;
727 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400728 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400729 return;
730 }
731
732 struct FinishContext {
733 ReadPixelsCallback* fClientCallback;
734 ReadPixelsContext fClientContext;
735 SkISize fSize;
736 SkColorType fColorType;
737 GrClientMappedBufferManager* fMappedBufferManager;
738 PixelTransferResult fTransferResult;
739 };
740 // Assumption is that the caller would like to flush. We could take a parameter or require an
741 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
742 // callback to GrGpu until after the next flush that flushes our op list, though.
743 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400744 callbackContext,
Brian Salomon63a0a752020-06-26 13:32:09 -0400745 rect.size(),
746 colorType,
747 mappedBufferManager,
748 std::move(transferResult)};
749 auto finishCallback = [](GrGpuFinishedContext c) {
750 const auto* context = reinterpret_cast<const FinishContext*>(c);
751 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
752 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
753 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
754 context->fMappedBufferManager)) {
755 result.reset();
756 }
757 (*context->fClientCallback)(context->fClientContext, std::move(result));
758 delete context;
759 };
760 GrFlushInfo flushInfo;
761 flushInfo.fFinishedContext = finishContext;
762 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -0500763
764 dContext->priv().flushSurface(this->asSurfaceProxy(),
765 SkSurface::BackendSurfaceAccess::kNoAccess,
766 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400767}
768
Adlai Hollerc95b5892020-08-11 12:02:22 -0400769void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(GrDirectContext* dContext,
770 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400771 sk_sp<SkColorSpace> dstColorSpace,
772 const SkIRect& srcRect,
773 SkISize dstSize,
774 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500775 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400776 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400777 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400778 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
779 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
780 SkASSERT(!dstSize.isZero());
781 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
782
Adlai Hollerc95b5892020-08-11 12:02:22 -0400783 if (!dContext) {
784 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400785 return;
786 }
787 auto rt = this->asRenderTargetProxy();
788 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400789 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400790 return;
791 }
792 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400793 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400794 return;
795 }
796 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400797 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400798 return;
799 }
800 int x = srcRect.fLeft;
801 int y = srcRect.fTop;
802 bool needsRescale = srcRect.size() != dstSize;
803 GrSurfaceProxyView srcView;
Brian Salomonbacbb922021-01-21 19:48:00 -0500804 auto info = SkImageInfo::Make(dstSize,
805 kRGBA_8888_SkColorType,
806 this->colorInfo().alphaType(),
807 dstColorSpace);
Brian Salomon63a0a752020-06-26 13:32:09 -0400808 if (needsRescale) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400809 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomonbacbb922021-01-21 19:48:00 -0500810 auto tempFC = this->rescale(info,
811 kTopLeft_GrSurfaceOrigin,
812 srcRect,
813 rescaleGamma,
814 rescaleMode);
815 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400816 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400817 return;
818 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500819 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
820 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400821 x = y = 0;
Brian Salomonbacbb922021-01-21 19:48:00 -0500822 srcView = tempFC->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400823 } else {
824 srcView = this->readSurfaceView();
825 if (!srcView.asTextureProxy()) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500826 srcView = GrSurfaceProxyView::Copy(fContext,
827 std::move(srcView),
828 GrMipmapped::kNo,
829 srcRect,
830 SkBackingFit::kApprox,
831 SkBudgeted::kYes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400832 if (!srcView) {
833 // If we can't get a texture copy of the contents then give up.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400834 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400835 return;
836 }
837 SkASSERT(srcView.asTextureProxy());
838 x = y = 0;
839 }
840 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomonbacbb922021-01-21 19:48:00 -0500841 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo(),
842 info.colorInfo());
Brian Salomon63a0a752020-06-26 13:32:09 -0400843 if (xform) {
844 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
Brian Salomonbacbb922021-01-21 19:48:00 -0500845 auto tempFC = GrSurfaceFillContext::Make(dContext,
846 info,
847 SkBackingFit::kApprox,
848 1,
849 GrMipmapped::kNo,
850 GrProtected::kNo,
851 kTopLeft_GrSurfaceOrigin);
852 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400853 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400854 return;
855 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500856 auto fp = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType());
857 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
858 tempFC->fillRectToRectWithFP(srcRectToDraw,
859 SkIRect::MakeSize(tempFC->dimensions()),
860 std::move(fp));
861 srcView = tempFC->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400862 SkASSERT(srcView.asTextureProxy());
863 x = y = 0;
864 }
865 }
866
Brian Salomonbacbb922021-01-21 19:48:00 -0500867 auto yInfo = SkImageInfo::MakeA8(dstSize);
868 auto yFC = GrSurfaceFillContext::MakeWithFallback(dContext, yInfo, SkBackingFit::kApprox);
869
870 auto uvInfo = yInfo.makeWH(yInfo.width()/2, yInfo.height()/2);
871 auto uFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
872 auto vFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
873
874 if (!yFC || !uFC || !vFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400875 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400876 return;
877 }
878
879 float baseM[20];
880 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
881
882 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
883
884 auto texMatrix = SkMatrix::Translate(x, y);
885
Brian Salomon63a0a752020-06-26 13:32:09 -0400886 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
887 PixelTransferResult yTransfer, uTransfer, vTransfer;
888
889 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
890 float yM[20];
891 std::fill_n(yM, 15, 0.f);
892 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500893
894 auto yFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
895 yFP = GrColorMatrixFragmentProcessor::Make(std::move(yFP),
896 yM,
897 /*unpremulInput=*/false,
898 /*clampRGBOutput=*/true,
899 /*premulOutput=*/false);
900 yFC->fillWithFP(std::move(yFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400901 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500902 yTransfer = yFC->transferPixels(GrColorType::kAlpha_8,
903 SkIRect::MakeSize(yFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400904 if (!yTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400905 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400906 return;
907 }
908 }
909
910 texMatrix.preScale(2.f, 2.f);
911 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
912 float uM[20];
913 std::fill_n(uM, 15, 0.f);
914 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500915
916 auto uFP = GrTextureEffect::Make(srcView,
917 this->colorInfo().alphaType(),
918 texMatrix,
919 GrSamplerState::Filter::kLinear);
920 uFP = GrColorMatrixFragmentProcessor::Make(std::move(uFP),
921 uM,
922 /*unpremulInput=*/false,
923 /*clampRGBOutput=*/true,
924 /*premulOutput=*/false);
925 uFC->fillWithFP(std::move(uFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400926 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500927 uTransfer = uFC->transferPixels(GrColorType::kAlpha_8,
928 SkIRect::MakeSize(uFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400929 if (!uTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400930 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400931 return;
932 }
933 }
934
935 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
936 float vM[20];
937 std::fill_n(vM, 15, 0.f);
938 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500939 auto vFP = GrTextureEffect::Make(std::move(srcView),
940 this->colorInfo().alphaType(),
941 texMatrix,
942 GrSamplerState::Filter::kLinear);
943 vFP = GrColorMatrixFragmentProcessor::Make(std::move(vFP),
944 vM,
945 /*unpremulInput=*/false,
946 /*clampRGBOutput=*/true,
947 /*premulOutput=*/false);
948 vFC->fillWithFP(std::move(vFP));
949
Brian Salomon63a0a752020-06-26 13:32:09 -0400950 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500951 vTransfer = vFC->transferPixels(GrColorType::kAlpha_8,
952 SkIRect::MakeSize(vFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400953 if (!vTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400954 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400955 return;
956 }
957 }
958
959 if (doSynchronousRead) {
Brian Salomondd4087d2020-12-23 20:36:44 -0500960 auto [yPmp, yStorage] = GrPixmap::Allocate(yInfo);
961 auto [uPmp, uStorage] = GrPixmap::Allocate(uvInfo);
962 auto [vPmp, vStorage] = GrPixmap::Allocate(uvInfo);
Brian Salomonbacbb922021-01-21 19:48:00 -0500963 if (!yFC->readPixels(dContext, yPmp, {0, 0}) ||
964 !uFC->readPixels(dContext, uPmp, {0, 0}) ||
965 !vFC->readPixels(dContext, vPmp, {0, 0})) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400966 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400967 return;
968 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400969 auto result = std::make_unique<AsyncReadResult>(dContext->priv().contextID());
Brian Salomondd4087d2020-12-23 20:36:44 -0500970 result->addCpuPlane(std::move(yStorage), yPmp.rowBytes());
971 result->addCpuPlane(std::move(uStorage), uPmp.rowBytes());
972 result->addCpuPlane(std::move(vStorage), vPmp.rowBytes());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400973 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400974 return;
975 }
976
977 struct FinishContext {
978 ReadPixelsCallback* fClientCallback;
979 ReadPixelsContext fClientContext;
980 GrClientMappedBufferManager* fMappedBufferManager;
981 SkISize fSize;
982 PixelTransferResult fYTransfer;
983 PixelTransferResult fUTransfer;
984 PixelTransferResult fVTransfer;
985 };
986 // Assumption is that the caller would like to flush. We could take a parameter or require an
987 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
988 // callback to GrGpu until after the next flush that flushes our op list, though.
989 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400990 callbackContext,
991 dContext->priv().clientMappedBufferManager(),
Brian Salomon63a0a752020-06-26 13:32:09 -0400992 dstSize,
993 std::move(yTransfer),
994 std::move(uTransfer),
995 std::move(vTransfer)};
996 auto finishCallback = [](GrGpuFinishedContext c) {
997 const auto* context = reinterpret_cast<const FinishContext*>(c);
998 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
999 auto manager = context->fMappedBufferManager;
1000 size_t rowBytes = SkToSizeT(context->fSize.width());
1001 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
1002 (*context->fClientCallback)(context->fClientContext, nullptr);
1003 delete context;
1004 return;
1005 }
1006 rowBytes /= 2;
1007 SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
1008 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
1009 (*context->fClientCallback)(context->fClientContext, nullptr);
1010 delete context;
1011 return;
1012 }
1013 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
1014 (*context->fClientCallback)(context->fClientContext, nullptr);
1015 delete context;
1016 return;
1017 }
1018 (*context->fClientCallback)(context->fClientContext, std::move(result));
1019 delete context;
1020 };
1021 GrFlushInfo flushInfo;
1022 flushInfo.fFinishedContext = finishContext;
1023 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -05001024 dContext->priv().flushSurface(this->asSurfaceProxy(),
1025 SkSurface::BackendSurfaceAccess::kNoAccess,
1026 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -04001027}
1028
Brian Salomon982127b2021-01-21 10:43:35 -05001029bool GrSurfaceContext::copy(sk_sp<GrSurfaceProxy> src, SkIRect srcRect, SkIPoint dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001030 ASSERT_SINGLE_OWNER
1031 RETURN_FALSE_IF_ABANDONED
1032 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -04001033 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -04001034
Brian Salomon947efe22019-07-16 15:36:11 -04001035 const GrCaps* caps = fContext->priv().caps();
1036
Greg Daniel46cfbc62019-06-07 11:43:30 -04001037 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -05001038 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -04001039
Stephen White3c0a50f2020-01-16 18:19:54 -05001040 if (this->asSurfaceProxy()->framebufferOnly()) {
1041 return false;
1042 }
1043
Brian Salomon982127b2021-01-21 10:43:35 -05001044 if (!caps->canCopySurface(this->asSurfaceProxy(), src.get(), srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -04001045 return false;
1046 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001047
Brian Salomon982127b2021-01-21 10:43:35 -05001048 return this->drawingManager()->newCopyRenderTask(std::move(src),
1049 srcRect,
1050 this->asSurfaceProxyRef(),
Brian Salomon0f9f8002021-01-22 16:30:50 -05001051 dstPoint,
1052 this->origin());
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001053}
Greg Daniel46cfbc62019-06-07 11:43:30 -04001054
Brian Salomonbacbb922021-01-21 19:48:00 -05001055std::unique_ptr<GrSurfaceFillContext> GrSurfaceContext::rescale(const GrImageInfo& info,
Brian Salomoneebe7352020-12-09 16:37:04 -05001056 GrSurfaceOrigin origin,
1057 SkIRect srcRect,
1058 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001059 RescaleMode rescaleMode) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001060 auto sfc = GrSurfaceFillContext::MakeWithFallback(fContext,
1061 info,
Brian Salomon1c86b632020-12-11 12:36:01 -05001062 SkBackingFit::kExact,
Brian Salomon1c86b632020-12-11 12:36:01 -05001063 1,
1064 GrMipmapped::kNo,
1065 this->asSurfaceProxy()->isProtected(),
1066 origin);
Brian Salomonbacbb922021-01-21 19:48:00 -05001067 if (!sfc || !this->rescaleInto(sfc.get(),
1068 SkIRect::MakeSize(sfc->dimensions()),
Brian Salomon1c86b632020-12-11 12:36:01 -05001069 srcRect,
1070 rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001071 rescaleMode)) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001072 return nullptr;
1073 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001074 return sfc;
Brian Salomon1c86b632020-12-11 12:36:01 -05001075}
1076
Brian Salomonbacbb922021-01-21 19:48:00 -05001077bool GrSurfaceContext::rescaleInto(GrSurfaceFillContext* dst,
Brian Salomon1c86b632020-12-11 12:36:01 -05001078 SkIRect dstRect,
1079 SkIRect srcRect,
1080 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001081 RescaleMode rescaleMode) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001082 SkASSERT(dst);
1083 if (!SkIRect::MakeSize(dst->dimensions()).contains((dstRect))) {
1084 return false;
1085 }
1086
Brian Salomone9ad9982019-07-22 16:17:41 -04001087 auto rtProxy = this->asRenderTargetProxy();
1088 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001089 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001090 }
1091
Stephen White3c0a50f2020-01-16 18:19:54 -05001092 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001093 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001094 }
1095
Greg Daniel40903af2020-01-30 14:55:05 -05001096 GrSurfaceProxyView texView = this->readSurfaceView();
Greg Daniel40903af2020-01-30 14:55:05 -05001097 if (!texView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -04001098 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect,
Brian Salomonc5243782020-04-02 12:50:34 -04001099 SkBackingFit::kApprox, SkBudgeted::kNo);
1100 if (!texView) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001101 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001102 }
Greg Daniel40903af2020-01-30 14:55:05 -05001103 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001104 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001105 }
1106
Brian Salomon1c86b632020-12-11 12:36:01 -05001107 SkISize finalSize = dstRect.size();
1108
Brian Salomonbf6b9792019-08-21 09:38:10 -04001109 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
1110 // 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 -05001111 std::unique_ptr<GrSurfaceFillContext> tempA;
1112 std::unique_ptr<GrSurfaceFillContext> tempB;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001113
Brian Salomone9ad9982019-07-22 16:17:41 -04001114 // Assume we should ignore the rescale linear request if the surface has no color space since
1115 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon63a0a752020-06-26 13:32:09 -04001116 if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() &&
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001117 !this->colorInfo().colorSpace()->gammaIsLinear()) {
1118 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomone9ad9982019-07-22 16:17:41 -04001119 // We'll fall back to kRGBA_8888 if half float not supported.
Brian Salomonbacbb922021-01-21 19:48:00 -05001120 GrImageInfo ii(GrColorType::kRGBA_F16,
1121 dst->colorInfo().alphaType(),
1122 std::move(cs),
1123 srcRect.size());
1124 auto linearRTC = GrSurfaceFillContext::MakeWithFallback(fContext,
1125 std::move(ii),
1126 SkBackingFit::kApprox,
1127 1,
1128 GrMipmapped::kNo,
1129 GrProtected::kNo,
1130 dst->origin());
Brian Salomone9ad9982019-07-22 16:17:41 -04001131 if (!linearRTC) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001132 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001133 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001134 auto fp = GrTextureEffect::Make(std::move(texView),
1135 this->colorInfo().alphaType(),
1136 SkMatrix::Translate(srcRect.topLeft()),
1137 GrSamplerState::Filter::kNearest,
1138 GrSamplerState::MipmapMode::kNone);
1139 fp = GrColorSpaceXformEffect::Make(std::move(fp),
1140 this->colorInfo(),
1141 linearRTC->colorInfo());
1142 linearRTC->fillWithFP(std::move(fp));
Greg Daniel40903af2020-01-30 14:55:05 -05001143 texView = linearRTC->readSurfaceView();
1144 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -04001145 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001146 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001147 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001148
Brian Salomon1c86b632020-12-11 12:36:01 -05001149 while (srcRect.size() != finalSize) {
1150 SkISize nextDims = finalSize;
Mike Reed1efa14d2021-01-02 21:44:59 -05001151 if (rescaleMode != RescaleMode::kNearest) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001152 if (srcRect.width() > finalSize.width()) {
1153 nextDims.fWidth = std::max((srcRect.width() + 1)/2, finalSize.width());
1154 } else if (srcRect.width() < finalSize.width()) {
1155 nextDims.fWidth = std::min(srcRect.width()*2, finalSize.width());
Brian Salomon59f31b12020-06-04 17:27:15 -04001156 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001157 if (srcRect.height() > finalSize.height()) {
1158 nextDims.fHeight = std::max((srcRect.height() + 1)/2, finalSize.height());
1159 } else if (srcRect.height() < finalSize.height()) {
1160 nextDims.fHeight = std::min(srcRect.height()*2, finalSize.height());
Brian Salomon59f31b12020-06-04 17:27:15 -04001161 }
1162 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001163 auto input = tempA ? tempA.get() : this;
Brian Salomone9ad9982019-07-22 16:17:41 -04001164 sk_sp<GrColorSpaceXform> xform;
Brian Salomonbacbb922021-01-21 19:48:00 -05001165 GrSurfaceFillContext* stepDst;
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001166 SkIRect stepDstRect;
Brian Salomon1c86b632020-12-11 12:36:01 -05001167 if (nextDims == finalSize) {
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001168 stepDst = dst;
1169 stepDstRect = dstRect;
Brian Salomonbacbb922021-01-21 19:48:00 -05001170 xform = GrColorSpaceXform::Make(input->colorInfo(), dst->colorInfo());
Brian Salomon1c86b632020-12-11 12:36:01 -05001171 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -05001172 GrImageInfo nextInfo(input->colorInfo(), nextDims);
1173 tempB = GrSurfaceFillContext::MakeWithFallback(fContext,
1174 nextInfo,
1175 SkBackingFit::kApprox);
Brian Salomon1c86b632020-12-11 12:36:01 -05001176 if (!tempB) {
1177 return false;
1178 }
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001179 stepDst = tempB.get();
1180 stepDstRect = SkIRect::MakeSize(tempB->dimensions());
Brian Salomone9ad9982019-07-22 16:17:41 -04001181 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001182 std::unique_ptr<GrFragmentProcessor> fp;
Mike Reed1efa14d2021-01-02 21:44:59 -05001183 if (rescaleMode == RescaleMode::kRepeatedCubic) {
Brian Salomon59f31b12020-06-04 17:27:15 -04001184 auto dir = GrBicubicEffect::Direction::kXY;
1185 if (nextDims.width() == srcRect.width()) {
1186 dir = GrBicubicEffect::Direction::kY;
1187 } else if (nextDims.height() == srcRect.height()) {
1188 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -04001189 }
Brian Salomon1af72d12020-06-25 10:47:26 -04001190 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
Mike Reed3867c702020-09-01 13:28:10 -04001191 static constexpr auto kKernel = GrBicubicEffect::gCatmullRom;
Brian Salomon1c86b632020-12-11 12:36:01 -05001192 fp = GrBicubicEffect::MakeSubset(std::move(texView),
1193 input->colorInfo().alphaType(),
Brian Salomonbacbb922021-01-21 19:48:00 -05001194 SkMatrix::I(),
Brian Salomon1c86b632020-12-11 12:36:01 -05001195 kWM,
1196 kWM,
1197 SkRect::Make(srcRect),
1198 kKernel,
1199 dir,
1200 *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -04001201 } else {
Mike Reed1efa14d2021-01-02 21:44:59 -05001202 auto filter = rescaleMode == RescaleMode::kNearest ? GrSamplerState::Filter::kNearest
1203 : GrSamplerState::Filter::kLinear;
Brian Salomonbacbb922021-01-21 19:48:00 -05001204 auto srcRectF = SkRect::Make(srcRect);
1205 fp = GrTextureEffect::MakeSubset(std::move(texView),
1206 this->colorInfo().alphaType(),
1207 SkMatrix::I(),
1208 {filter, GrSamplerState::MipmapMode::kNone},
1209 srcRectF,
1210 srcRectF,
1211 *this->caps());
Brian Salomone9ad9982019-07-22 16:17:41 -04001212 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001213 if (xform) {
1214 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
1215 }
1216 stepDst->fillRectToRectWithFP(srcRect, stepDstRect, std::move(fp));
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001217 texView = stepDst->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -04001218 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001219 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomone9ad9982019-07-22 16:17:41 -04001220 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001221 return true;
Brian Salomone9ad9982019-07-22 16:17:41 -04001222}
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001223
1224GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
1225 const SkIRect& rect) {
1226 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1227 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001228 auto direct = fContext->asDirectContext();
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001229 if (!direct) {
1230 return {};
1231 }
1232 auto rtProxy = this->asRenderTargetProxy();
1233 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1234 return {};
1235 }
1236
1237 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001238 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
1239 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001240 // Fail if read color type does not have all of dstCT's color channels and those missing color
1241 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001242 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1243 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
1244 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1245 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001246 return {};
1247 }
1248
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001249 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001250 !supportedRead.fOffsetAlignmentForTransferBuffer) {
1251 return {};
1252 }
1253
1254 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
1255 size_t size = rowBytes * rect.height();
1256 auto buffer = direct->priv().resourceProvider()->createBuffer(
1257 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
1258 if (!buffer) {
1259 return {};
1260 }
1261 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -05001262 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001263 if (flip) {
1264 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1265 this->height() - rect.fTop);
1266 }
Greg Danielbbfec9d2019-08-20 10:56:51 -04001267 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001268 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -04001269 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001270 PixelTransferResult result;
1271 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001272 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -04001273 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001274 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
1275 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001276 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
1277 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001278 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
1279 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -04001280 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001281 };
1282 }
1283 return result;
1284}
Greg Daniel46e366a2019-12-16 14:38:36 -05001285
1286#ifdef SK_DEBUG
1287void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001288 SkASSERT(fReadView.proxy());
1289 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -04001290 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
1291 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
1292 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
1293 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001294 this->onValidate();
1295}
1296#endif