blob: 6a6fd2d6f69fdecb1a78cb521c4e3490597d5ca3 [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 Salomon2c673402021-04-02 14:36:58 -040015#include "src/core/SkMipmap.h"
Brian Salomon63a0a752020-06-26 13:32:09 -040016#include "src/core/SkYUVMath.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040017#include "src/gpu/GrAuditTrail.h"
Brian Salomon1c86b632020-12-11 12:36:01 -050018#include "src/gpu/GrColorSpaceXform.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040019#include "src/gpu/GrDataUtils.h"
Adlai Hollera0693042020-10-14 11:23:11 -040020#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrDrawingManager.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040022#include "src/gpu/GrGpu.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040023#include "src/gpu/GrImageInfo.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040024#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050026#include "src/gpu/GrSurfaceDrawContext.h"
Brian Salomonbacbb922021-01-21 19:48:00 -050027#include "src/gpu/GrSurfaceFillContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/SkGr.h"
Brian Salomone9ad9982019-07-22 16:17:41 -040029#include "src/gpu/effects/GrBicubicEffect.h"
Brian Osman45580d32016-11-23 09:37:01 -050030
Brian Salomond63638b2021-03-05 14:00:07 -050031#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
32#define RETURN_FALSE_IF_ABANDONED if (this->fContext->abandoned()) { return false; }
33#define RETURN_NULLPTR_IF_ABANDONED if (this->fContext->abandoned()) { return nullptr; }
Brian Osman45580d32016-11-23 09:37:01 -050034
Greg Danielbfa19c42019-12-19 16:41:40 -050035std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -050036 GrSurfaceProxyView readView,
Brian Salomon14f99fc2020-12-07 12:19:47 -050037 const GrColorInfo& info) {
Greg Daniele20fcad2020-01-08 11:52:34 -050038 // It is probably not necessary to check if the context is abandoned here since uses of the
39 // GrSurfaceContext which need the context will mostly likely fail later on without an issue.
40 // However having this hear adds some reassurance in case there is a path doesn't handle an
41 // abandoned context correctly. It also lets us early out of some extra work.
Robert Phillips9eb00022020-06-30 15:30:12 -040042 if (context->abandoned()) {
Greg Daniele20fcad2020-01-08 11:52:34 -050043 return nullptr;
44 }
Greg Daniel3912a4b2020-01-14 09:56:04 -050045 GrSurfaceProxy* proxy = readView.proxy();
Greg Danielbfa19c42019-12-19 16:41:40 -050046 SkASSERT(proxy && proxy->asTextureProxy());
47
Greg Danielbfa19c42019-12-19 16:41:40 -050048 std::unique_ptr<GrSurfaceContext> surfaceContext;
Greg Daniel3912a4b2020-01-14 09:56:04 -050049 if (proxy->asRenderTargetProxy()) {
Brian Salomon8afde5f2020-04-01 16:22:00 -040050 // Will we ever want a swizzle that is not the default write swizzle for the format and
Greg Danielbfa19c42019-12-19 16:41:40 -050051 // colorType here? If so we will need to manually pass that in.
Brian Salomonc5243782020-04-02 12:50:34 -040052 GrSwizzle writeSwizzle;
Brian Salomon14f99fc2020-12-07 12:19:47 -050053 if (info.colorType() != GrColorType::kUnknown) {
54 writeSwizzle = context->priv().caps()->getWriteSwizzle(proxy->backendFormat(),
55 info.colorType());
Brian Salomonc5243782020-04-02 12:50:34 -040056 }
Brian Salomon8afde5f2020-04-01 16:22:00 -040057 GrSurfaceProxyView writeView(readView.refProxy(), readView.origin(), writeSwizzle);
Brian Salomon590f5672020-12-16 11:44:47 -050058 if (info.alphaType() == kPremul_SkAlphaType || info.alphaType() == kOpaque_SkAlphaType) {
59 surfaceContext = std::make_unique<GrSurfaceDrawContext>(context,
60 std::move(readView),
61 std::move(writeView),
62 info.colorType(),
63 info.refColorSpace(),
Chris Daltonf5b87f92021-04-19 17:27:09 -060064 SkSurfaceProps());
Brian Salomon590f5672020-12-16 11:44:47 -050065 } else {
66 surfaceContext = std::make_unique<GrSurfaceFillContext>(context,
67 std::move(readView),
68 std::move(writeView),
69 info);
70 }
Greg Danielbfa19c42019-12-19 16:41:40 -050071 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -050072 surfaceContext = std::make_unique<GrSurfaceContext>(context, std::move(readView), info);
Greg Danielbfa19c42019-12-19 16:41:40 -050073 }
Robert Phillips07f0e412020-01-17 15:20:00 -050074 SkDEBUGCODE(surfaceContext->validate();)
Greg Danielbfa19c42019-12-19 16:41:40 -050075 return surfaceContext;
76}
77
Brian Salomona56a7462020-02-07 14:17:25 -050078std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
Brian Salomon14f99fc2020-12-07 12:19:47 -050079 const GrImageInfo& info,
Brian Salomona56a7462020-02-07 14:17:25 -050080 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050081 SkBackingFit fit,
Brian Salomon14f99fc2020-12-07 12:19:47 -050082 GrSurfaceOrigin origin,
83 GrRenderable renderable,
84 int sampleCount,
85 GrMipmapped mipmapped,
86 GrProtected isProtected,
Brian Salomona56a7462020-02-07 14:17:25 -050087 SkBudgeted budgeted) {
Brian Salomon14f99fc2020-12-07 12:19:47 -050088 SkASSERT(context);
89 SkASSERT(renderable == GrRenderable::kYes || sampleCount == 1);
90 if (context->abandoned()) {
91 return nullptr;
Brian Salomond005b692020-04-01 15:47:05 -040092 }
Brian Salomon14f99fc2020-12-07 12:19:47 -050093 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(format,
94 info.dimensions(),
95 renderable,
96 sampleCount,
97 mipmapped,
98 fit,
99 budgeted,
100 isProtected);
Greg Danielbfa19c42019-12-19 16:41:40 -0500101 if (!proxy) {
102 return nullptr;
103 }
104
Brian Salomon14f99fc2020-12-07 12:19:47 -0500105 GrSwizzle swizzle;
106 if (info.colorType() != GrColorType::kUnknown &&
107 !context->priv().caps()->isFormatCompressed(format)) {
108 swizzle = context->priv().caps()->getReadSwizzle(format, info.colorType());
109 }
110
Greg Daniel3912a4b2020-01-14 09:56:04 -0500111 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500112 return GrSurfaceContext::Make(context, std::move(view), info.colorInfo());
113}
114
115std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
116 const GrImageInfo& info,
117 SkBackingFit fit,
118 GrSurfaceOrigin origin,
119 GrRenderable renderable,
120 int sampleCount,
121 GrMipmapped mipmapped,
122 GrProtected isProtected,
123 SkBudgeted budgeted) {
124 GrBackendFormat format = context->priv().caps()->getDefaultBackendFormat(info.colorType(),
125 renderable);
126 return Make(context,
127 info,
128 format,
129 fit,
130 origin,
131 renderable,
132 sampleCount,
133 mipmapped,
134 isProtected,
135 budgeted);
Greg Danielbfa19c42019-12-19 16:41:40 -0500136}
137
Robert Phillips69893702019-02-22 11:16:30 -0500138GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500139 GrSurfaceProxyView readView,
Brian Salomon14f99fc2020-12-07 12:19:47 -0500140 const GrColorInfo& info)
141 : fContext(context), fReadView(std::move(readView)), fColorInfo(info) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400142 SkASSERT(!context->abandoned());
Greg Daniele20fcad2020-01-08 11:52:34 -0500143}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400144
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400145const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); }
146
Robert Phillips0d075de2019-03-04 11:08:13 -0500147GrAuditTrail* GrSurfaceContext::auditTrail() {
148 return fContext->priv().auditTrail();
149}
150
151GrDrawingManager* GrSurfaceContext::drawingManager() {
152 return fContext->priv().drawingManager();
153}
154
155const GrDrawingManager* GrSurfaceContext::drawingManager() const {
156 return fContext->priv().drawingManager();
157}
158
159#ifdef SK_DEBUG
Brian Salomon70fe17e2020-11-30 14:33:58 -0500160GrSingleOwner* GrSurfaceContext::singleOwner() const { return fContext->priv().singleOwner(); }
Robert Phillips0d075de2019-03-04 11:08:13 -0500161#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400162
Brian Salomondd4087d2020-12-23 20:36:44 -0500163static bool alpha_types_compatible(SkAlphaType srcAlphaType, SkAlphaType dstAlphaType) {
164 // If both alpha types are kUnknown things make sense. If not, it's too underspecified.
165 return (srcAlphaType == kUnknown_SkAlphaType) == (dstAlphaType == kUnknown_SkAlphaType);
166}
167
168bool GrSurfaceContext::readPixels(GrDirectContext* dContext, GrPixmap dst, SkIPoint pt) {
Brian Salomon1d435302019-07-01 13:05:28 -0400169 ASSERT_SINGLE_OWNER
170 RETURN_FALSE_IF_ABANDONED
171 SkDEBUGCODE(this->validate();)
172 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400173 if (!fContext->priv().matches(dContext)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400174 return false;
175 }
Brian Salomon46f63232021-01-25 10:13:35 -0500176
177 if (dst.colorType() == GrColorType::kUnknown) {
178 return false;
179 }
180
Brian Salomonc24c8ef2021-02-01 13:32:30 -0500181 if (dst.rowBytes() % dst.info().bpp()) {
182 return false;
183 }
184
Brian Salomondd4087d2020-12-23 20:36:44 -0500185 dst = dst.clip(this->dimensions(), &pt);
186 if (!dst.hasPixels()) {
Brian Salomon1d435302019-07-01 13:05:28 -0400187 return false;
188 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500189 if (!alpha_types_compatible(this->colorInfo().alphaType(), dst.alphaType())) {
Brian Salomon1d435302019-07-01 13:05:28 -0400190 return false;
191 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500192 // We allow unknown alpha types but only if both src and dst are unknown. Otherwise, it's too
193 // weird to reason about what should be expected.
Greg Daniel6eb8c242019-06-05 10:22:24 -0400194
Brian Salomon982127b2021-01-21 10:43:35 -0500195 sk_sp<GrSurfaceProxy> srcProxy = this->asSurfaceProxyRef();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400196
Stephen White3c0a50f2020-01-16 18:19:54 -0500197 if (srcProxy->framebufferOnly()) {
198 return false;
199 }
200
Greg Daniel6eb8c242019-06-05 10:22:24 -0400201 // MDB TODO: delay this instantiation until later in the method
Adlai Hollerc95b5892020-08-11 12:02:22 -0400202 if (!srcProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400203 return false;
204 }
205
206 GrSurface* srcSurface = srcProxy->peekSurface();
207
Brian Salomondd4087d2020-12-23 20:36:44 -0500208 SkColorSpaceXformSteps::Flags flags =
209 SkColorSpaceXformSteps{this->colorInfo(), dst.info()}.flags;
Mike Klein7321e6a2019-12-03 11:08:40 -0600210 bool unpremul = flags.unpremul,
211 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
212 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400213
Adlai Hollerc95b5892020-08-11 12:02:22 -0400214 const GrCaps* caps = dContext->priv().caps();
Robert Phillips07f0e412020-01-17 15:20:00 -0500215 bool srcIsCompressed = caps->isFormatCompressed(srcSurface->backendFormat());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400216 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
217 // care so much about getImageData performance. However, in order to ensure putImageData/
218 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
219 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
220 // fContext->vaildaPMUPMConversionExists()).
Greg Daniel0258c902019-08-01 13:08:33 -0400221 GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
222 GrRenderable::kYes);
Greg Danielc71c7962020-01-14 16:44:18 -0500223 GrColorType srcColorType = this->colorInfo().colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400224 bool canvas2DFastPath = unpremul && !needColorConversion &&
Brian Salomondd4087d2020-12-23 20:36:44 -0500225 (GrColorType::kRGBA_8888 == dst.colorType() ||
226 GrColorType::kBGRA_8888 == dst.colorType()) &&
Brian Salomon1d435302019-07-01 13:05:28 -0400227 SkToBool(srcProxy->asTextureProxy()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500228 (srcColorType == GrColorType::kRGBA_8888 ||
229 srcColorType == GrColorType::kBGRA_8888) &&
Greg Daniel0258c902019-08-01 13:08:33 -0400230 defaultRGBAFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400231 dContext->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400232
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400233 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400234 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400235 return false;
236 }
237
Brian Salomondc0710f2019-07-01 14:59:32 -0400238 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400239 std::unique_ptr<GrSurfaceContext> tempCtx;
240 if (this->asTextureProxy()) {
241 GrColorType colorType = (canvas2DFastPath || srcIsCompressed)
242 ? GrColorType::kRGBA_8888
243 : this->colorInfo().colorType();
Brian Salomondd4087d2020-12-23 20:36:44 -0500244 SkAlphaType alphaType = canvas2DFastPath ? dst.alphaType()
Brian Salomon590f5672020-12-16 11:44:47 -0500245 : this->colorInfo().alphaType();
246 GrImageInfo tempInfo(colorType,
247 alphaType,
248 this->colorInfo().refColorSpace(),
Brian Salomondd4087d2020-12-23 20:36:44 -0500249 dst.dimensions());
Brian Salomon590f5672020-12-16 11:44:47 -0500250 auto sfc = GrSurfaceFillContext::Make(dContext, tempInfo, SkBackingFit::kApprox);
251 if (!sfc) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400252 return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400253 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400254
255 std::unique_ptr<GrFragmentProcessor> fp;
256 if (canvas2DFastPath) {
257 fp = dContext->priv().createPMToUPMEffect(GrTextureEffect::Make(
258 this->readSurfaceView(), this->colorInfo().alphaType()));
Brian Salomondd4087d2020-12-23 20:36:44 -0500259 if (dst.colorType() == GrColorType::kBGRA_8888) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400260 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
Brian Salomondd4087d2020-12-23 20:36:44 -0500261 dst = GrPixmap(dst.info().makeColorType(GrColorType::kRGBA_8888),
262 dst.addr(),
263 dst.rowBytes());
Brian Salomon72c7b982020-10-06 10:07:38 -0400264 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400265 } else {
266 fp = GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType());
267 }
268 if (!fp) {
269 return false;
270 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500271 sfc->fillRectToRectWithFP(SkIRect::MakePtSize(pt, dst.dimensions()),
272 SkIRect::MakeSize(dst.dimensions()),
Brian Salomon590f5672020-12-16 11:44:47 -0500273 std::move(fp));
Brian Salomon72c7b982020-10-06 10:07:38 -0400274 pt = {0, 0};
Brian Salomon590f5672020-12-16 11:44:47 -0500275 tempCtx = std::move(sfc);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400276 } else {
Brian Salomon72c7b982020-10-06 10:07:38 -0400277 auto restrictions = this->caps()->getDstCopyRestrictions(this->asRenderTargetProxy(),
278 this->colorInfo().colorType());
279 sk_sp<GrSurfaceProxy> copy;
280 static constexpr auto kFit = SkBackingFit::kExact;
281 static constexpr auto kBudgeted = SkBudgeted::kYes;
282 static constexpr auto kMipMapped = GrMipMapped::kNo;
283 if (restrictions.fMustCopyWholeSrc) {
Brian Salomon982127b2021-01-21 10:43:35 -0500284 copy = GrSurfaceProxy::Copy(fContext,
285 std::move(srcProxy),
286 this->origin(),
287 kMipMapped,
288 kFit,
Brian Salomon72c7b982020-10-06 10:07:38 -0400289 kBudgeted);
290 } else {
Brian Salomondd4087d2020-12-23 20:36:44 -0500291 auto srcRect = SkIRect::MakePtSize(pt, dst.dimensions());
Brian Salomon982127b2021-01-21 10:43:35 -0500292 copy = GrSurfaceProxy::Copy(fContext,
293 std::move(srcProxy),
294 this->origin(),
295 kMipMapped,
296 srcRect,
297 kFit,
298 kBudgeted,
299 restrictions.fRectsMustMatch);
Brian Salomon72c7b982020-10-06 10:07:38 -0400300 pt = {0, 0};
301 }
302 if (!copy) {
303 return false;
304 }
305 GrSurfaceProxyView view{std::move(copy), this->origin(), this->readSwizzle()};
Brian Salomon14f99fc2020-12-07 12:19:47 -0500306 tempCtx = GrSurfaceContext::Make(dContext, std::move(view), this->colorInfo());
Brian Salomon72c7b982020-10-06 10:07:38 -0400307 SkASSERT(tempCtx);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400308 }
Brian Salomondd4087d2020-12-23 20:36:44 -0500309 return tempCtx->readPixels(dContext, dst, pt);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400310 }
311
Greg Danielb8d84f82020-02-13 14:25:00 -0500312 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400313
Brian Salomon1d435302019-07-01 13:05:28 -0400314 auto supportedRead = caps->supportedReadPixelsColorType(
Brian Salomondd4087d2020-12-23 20:36:44 -0500315 this->colorInfo().colorType(), srcProxy->backendFormat(), dst.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400316
Brian Salomondd4087d2020-12-23 20:36:44 -0500317 bool makeTight =
318 !caps->readPixelsRowBytesSupport() && dst.rowBytes() != dst.info().minRowBytes();
Brian Salomon1047a492019-07-02 12:25:21 -0400319
320 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomondd4087d2020-12-23 20:36:44 -0500321 (dst.colorType() != supportedRead.fColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400322
Brian Salomonf30b1c12019-06-20 12:25:02 -0400323 std::unique_ptr<char[]> tmpPixels;
Brian Salomondd4087d2020-12-23 20:36:44 -0500324 GrPixmap tmp;
325 void* readDst = dst.addr();
326 size_t readRB = dst.rowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400327 if (convert) {
Brian Salomondd4087d2020-12-23 20:36:44 -0500328 GrImageInfo tmpInfo(supportedRead.fColorType,
329 this->colorInfo().alphaType(),
330 this->colorInfo().refColorSpace(),
331 dst.dimensions());
Brian Salomon1d435302019-07-01 13:05:28 -0400332 size_t tmpRB = tmpInfo.minRowBytes();
333 size_t size = tmpRB * tmpInfo.height();
334 // Chrome MSAN bots require the data to be initialized (hence the ()).
John Stilesfbd050b2020-08-03 13:21:46 -0400335 tmpPixels = std::make_unique<char[]>(size);
Brian Salomondd4087d2020-12-23 20:36:44 -0500336 tmp = {tmpInfo, tmpPixels.get(), tmpRB};
Brian Salomonf30b1c12019-06-20 12:25:02 -0400337
Brian Salomonf30b1c12019-06-20 12:25:02 -0400338 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400339 readRB = tmpRB;
Brian Salomondd4087d2020-12-23 20:36:44 -0500340 pt.fY = flip ? srcSurface->height() - pt.fY - dst.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400341 }
342
Brian Salomon982127b2021-01-21 10:43:35 -0500343 dContext->priv().flushSurface(srcProxy.get());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400344 dContext->submit();
Brian Salomone2078f12021-05-24 12:40:46 -0400345 if (!dContext->priv().getGpu()->readPixels(srcSurface,
346 SkIRect::MakePtSize(pt, dst.dimensions()),
Brian Salomondd4087d2020-12-23 20:36:44 -0500347 this->colorInfo().colorType(),
Brian Salomone2078f12021-05-24 12:40:46 -0400348 supportedRead.fColorType,
349 readDst,
350 readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400351 return false;
352 }
353
Brian Salomondd4087d2020-12-23 20:36:44 -0500354 if (tmp.hasPixels()) {
355 return GrConvertPixels(dst, tmp, flip);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400356 }
357 return true;
358}
Robert Phillips0d075de2019-03-04 11:08:13 -0500359
Brian Salomonea1d39b2021-04-01 17:06:52 -0400360bool GrSurfaceContext::writePixels(GrDirectContext* dContext,
361 GrCPixmap src,
Brian Salomon75ee7372021-04-06 15:04:35 -0400362 SkIPoint dstPt) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400363 ASSERT_SINGLE_OWNER
364 RETURN_FALSE_IF_ABANDONED
365 SkDEBUGCODE(this->validate();)
Brian Salomon2c673402021-04-02 14:36:58 -0400366
367 src = src.clip(this->dimensions(), &dstPt);
368 if (!src.hasPixels()) {
369 return false;
370 }
371 if (!src.info().bpp() || src.rowBytes() % src.info().bpp()) {
372 return false;
373 }
Brian Salomon75ee7372021-04-06 15:04:35 -0400374 return this->internalWritePixels(dContext, &src, 1, dstPt);
Brian Salomon2c673402021-04-02 14:36:58 -0400375}
376
377bool GrSurfaceContext::writePixels(GrDirectContext* dContext,
378 const GrCPixmap src[],
Brian Salomon75ee7372021-04-06 15:04:35 -0400379 int numLevels) {
Brian Salomon2c673402021-04-02 14:36:58 -0400380 ASSERT_SINGLE_OWNER
381 RETURN_FALSE_IF_ABANDONED
382 SkDEBUGCODE(this->validate();)
383
384 SkASSERT(dContext);
385 SkASSERT(numLevels >= 1);
386 SkASSERT(src);
387
388 if (numLevels == 1) {
389 if (src->dimensions() != this->dimensions()) {
390 return false;
391 }
Brian Salomon75ee7372021-04-06 15:04:35 -0400392 return this->writePixels(dContext, src[0], {0, 0});
Brian Salomon2c673402021-04-02 14:36:58 -0400393 }
394 if (!this->asTextureProxy() || this->asTextureProxy()->proxyMipmapped() == GrMipmapped::kNo) {
395 return false;
396 }
397
398 SkISize dims = this->dimensions();
399 if (numLevels != SkMipmap::ComputeLevelCount(dims) + 1) {
400 return false;
401 }
402 for (int i = 0; i < numLevels; ++i) {
403 if (src[i].colorInfo() != src[0].colorInfo()) {
404 return false;
405 }
406 if (dims != src[i].dimensions()) {
407 return false;
408 }
409 if (!src[i].info().bpp() || src[i].rowBytes() % src[i].info().bpp()) {
410 return false;
411 }
412 dims = {std::max(1, dims.width()/2), std::max(1, dims.height()/2)};
413 }
Brian Salomon75ee7372021-04-06 15:04:35 -0400414 return this->internalWritePixels(dContext, src, numLevels, {0, 0});
Brian Salomon2c673402021-04-02 14:36:58 -0400415}
416
417bool GrSurfaceContext::internalWritePixels(GrDirectContext* dContext,
418 const GrCPixmap src[],
419 int numLevels,
Brian Salomon75ee7372021-04-06 15:04:35 -0400420 SkIPoint pt) {
Brian Salomon2c673402021-04-02 14:36:58 -0400421 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::internalWritePixels");
422
423 SkASSERT(numLevels >= 1);
424 SkASSERT(src);
425
426 // We can either write to a subset or write MIP levels, but not both.
427 SkASSERT((src[0].dimensions() == this->dimensions() && pt.isZero()) || numLevels == 1);
428 SkASSERT(numLevels == 1 ||
Brian Salomon7cde6c32021-04-02 15:53:42 -0400429 (this->asTextureProxy() && this->asTextureProxy()->mipmapped() == GrMipmapped::kYes));
Brian Salomon2c673402021-04-02 14:36:58 -0400430 // Our public caller should have clipped to the bounds of the surface already.
Brian Salomonea1d39b2021-04-01 17:06:52 -0400431 SkASSERT(SkIRect::MakeSize(this->dimensions()).contains(
432 SkIRect::MakePtSize(pt, src[0].dimensions())));
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400433
Adlai Hollerc95b5892020-08-11 12:02:22 -0400434 if (!dContext) {
Brian Salomonc320b152018-02-20 14:05:36 -0500435 return false;
436 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500437
Brian Salomon1d435302019-07-01 13:05:28 -0400438 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500439 return false;
440 }
441
Brian Salomon2c673402021-04-02 14:36:58 -0400442 if (src[0].colorType() == GrColorType::kUnknown) {
Brian Salomon46f63232021-01-25 10:13:35 -0500443 return false;
444 }
445
Brian Salomon2c673402021-04-02 14:36:58 -0400446 if (!alpha_types_compatible(src[0].alphaType(), this->colorInfo().alphaType())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400447 return false;
448 }
449
450 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
Stephen White3c0a50f2020-01-16 18:19:54 -0500451
452 if (dstProxy->framebufferOnly()) {
453 return false;
454 }
455
Adlai Hollerc95b5892020-08-11 12:02:22 -0400456 if (!dstProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400457 return false;
458 }
459
460 GrSurface* dstSurface = dstProxy->peekSurface();
461
Brian Salomondd4087d2020-12-23 20:36:44 -0500462 SkColorSpaceXformSteps::Flags flags =
Brian Salomon2c673402021-04-02 14:36:58 -0400463 SkColorSpaceXformSteps{src[0].colorInfo(), this->colorInfo()}.flags;
Mike Klein7321e6a2019-12-03 11:08:40 -0600464 bool unpremul = flags.unpremul,
465 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
466 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400467
Adlai Hollerc95b5892020-08-11 12:02:22 -0400468 const GrCaps* caps = dContext->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400469
470 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
471 GrRenderable::kNo);
472
Greg Danielc71c7962020-01-14 16:44:18 -0500473 GrColorType dstColorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400474 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
475 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400476 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
Brian Salomon2c673402021-04-02 14:36:58 -0400477 (src[0].colorType() == GrColorType::kRGBA_8888 ||
478 src[0].colorType() == GrColorType::kBGRA_8888) &&
Brian Salomon590f5672020-12-16 11:44:47 -0500479 this->asFillContext() &&
Greg Danielc71c7962020-01-14 16:44:18 -0500480 (dstColorType == GrColorType::kRGBA_8888 ||
481 dstColorType == GrColorType::kBGRA_8888) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400482 rgbaDefaultFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400483 dContext->priv().validPMUPMConversionExists();
Brian Salomon2c673402021-04-02 14:36:58 -0400484 // Drawing code path doesn't support writing to levels and doesn't support inserting layout
485 // transitions.
Brian Salomon75ee7372021-04-06 15:04:35 -0400486 if ((!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) && numLevels == 1) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500487 GrColorInfo tempColorInfo;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400488 GrBackendFormat format;
Greg Danielbfa19c42019-12-19 16:41:40 -0500489 GrSwizzle tempReadSwizzle;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400490 if (canvas2DFastPath) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500491 tempColorInfo = {GrColorType::kRGBA_8888,
492 kUnpremul_SkAlphaType,
493 this->colorInfo().refColorSpace()};
Greg Daniel7bfc9132019-08-14 14:23:53 -0400494 format = rgbaDefaultFormat;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400495 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500496 tempColorInfo = this->colorInfo();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400497 format = dstProxy->backendFormat().makeTexture2D();
498 if (!format.isValid()) {
499 return false;
500 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500501 tempReadSwizzle = this->readSwizzle();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400502 }
503
Greg Daniel2e52ad12019-06-13 10:04:16 -0400504 // It is more efficient for us to write pixels into a top left origin so we prefer that.
505 // However, if the final proxy isn't a render target then we must use a copy to move the
506 // data into it which requires the origins to match. If the final proxy is a render target
507 // we can use a draw instead which doesn't have this origin restriction. Thus for render
508 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400509 GrSurfaceOrigin tempOrigin =
Brian Salomon590f5672020-12-16 11:44:47 -0500510 this->asFillContext() ? kTopLeft_GrSurfaceOrigin : this->origin();
Brian Salomon2c673402021-04-02 14:36:58 -0400511 auto tempProxy = dContext->priv().proxyProvider()->createProxy(format,
512 src[0].dimensions(),
513 GrRenderable::kNo,
514 1,
515 GrMipmapped::kNo,
516 SkBackingFit::kApprox,
517 SkBudgeted::kYes,
518 GrProtected::kNo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400519 if (!tempProxy) {
520 return false;
521 }
Greg Daniel3912a4b2020-01-14 09:56:04 -0500522 GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500523 GrSurfaceContext tempCtx(dContext, tempView, tempColorInfo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400524
525 // In the fast path we always write the srcData to the temp context as though it were RGBA.
526 // When the data is really BGRA the write will cause the R and B channels to be swapped in
527 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
528 // dst.
Brian Salomon2c673402021-04-02 14:36:58 -0400529 GrCPixmap origSrcBase = src[0];
530 GrCPixmap srcBase = origSrcBase;
Brian Salomon1d435302019-07-01 13:05:28 -0400531 if (canvas2DFastPath) {
Brian Salomon2c673402021-04-02 14:36:58 -0400532 srcBase = GrCPixmap(origSrcBase.info().makeColorType(GrColorType::kRGBA_8888),
533 origSrcBase.addr(),
534 origSrcBase.rowBytes());
Brian Salomon1d435302019-07-01 13:05:28 -0400535 }
Brian Salomon2c673402021-04-02 14:36:58 -0400536 if (!tempCtx.writePixels(dContext, srcBase, {0, 0})) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400537 return false;
538 }
539
Brian Salomon590f5672020-12-16 11:44:47 -0500540 if (this->asFillContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400541 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400542 if (canvas2DFastPath) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400543 fp = dContext->priv().createUPMToPMEffect(
Brian Salomon14f99fc2020-12-07 12:19:47 -0500544 GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType()));
Brian Salomon1d435302019-07-01 13:05:28 -0400545 // Important: check the original src color type here!
Brian Salomon2c673402021-04-02 14:36:58 -0400546 if (origSrcBase.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400547 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
548 }
549 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500550 fp = GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400551 }
552 if (!fp) {
553 return false;
554 }
Brian Salomon2c673402021-04-02 14:36:58 -0400555 this->asFillContext()->fillRectToRectWithFP(
556 SkIRect::MakeSize(srcBase.dimensions()),
557 SkIRect::MakePtSize(pt, srcBase.dimensions()),
558 std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400559 } else {
Brian Salomon2c673402021-04-02 14:36:58 -0400560 SkIRect srcRect = SkIRect::MakeSize(srcBase.dimensions());
Brian Salomon1d435302019-07-01 13:05:28 -0400561 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Brian Salomon982127b2021-01-21 10:43:35 -0500562 if (!this->copy(std::move(tempProxy), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400563 return false;
564 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400565 }
566 return true;
567 }
568
Brian Salomon2c673402021-04-02 14:36:58 -0400569 GrColorType srcColorType = src[0].colorType();
570 auto [allowedColorType, _] =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400571 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400572 dstProxy->backendFormat(),
Brian Salomon2c673402021-04-02 14:36:58 -0400573 srcColorType);
Greg Danielb8d84f82020-02-13 14:25:00 -0500574 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400575
Brian Salomon2c673402021-04-02 14:36:58 -0400576 bool convertAll = premul ||
577 unpremul ||
578 needColorConversion ||
579 flip ||
580 (srcColorType != allowedColorType);
581 bool mustBeTight = !caps->writePixelsRowBytesSupport();
582 size_t tmpSize = 0;
583 if (mustBeTight || convertAll) {
584 for (int i = 0; i < numLevels; ++i) {
585 if (convertAll || (mustBeTight && src[i].rowBytes() != src[i].info().minRowBytes())) {
586 tmpSize += src[i].info().makeColorType(allowedColorType).minRowBytes()*
587 src[i].height();
588 }
589 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400590 }
591
Brian Salomon2c673402021-04-02 14:36:58 -0400592 auto tmpData = tmpSize ? SkData::MakeUninitialized(tmpSize) : nullptr;
593 void* tmp = tmpSize ? tmpData->writable_data() : nullptr;
594 SkAutoSTArray<15, GrMipLevel> srcLevels(numLevels);
595 bool ownAllStorage = true;
596 for (int i = 0; i < numLevels; ++i) {
597 if (convertAll || (mustBeTight && src[i].rowBytes() != src[i].info().minRowBytes())) {
598 GrImageInfo tmpInfo(allowedColorType,
599 this->colorInfo().alphaType(),
600 this->colorInfo().refColorSpace(),
601 src[i].dimensions());
602 auto tmpRB = tmpInfo.minRowBytes();
603 GrPixmap tmpPM(tmpInfo, tmp, tmpRB);
604 SkAssertResult(GrConvertPixels(tmpPM, src[i], flip));
605 srcLevels[i] = {tmpPM.addr(), tmpPM.rowBytes(), tmpData};
606 tmp = SkTAddOffset<void>(tmp, tmpRB*tmpPM.height());
607 } else {
608 srcLevels[i] = {src[i].addr(), src[i].rowBytes(), src[i].pixelStorage()};
609 ownAllStorage &= src[i].ownsPixels();
610 }
611 }
612 pt.fY = flip ? dstSurface->height() - pt.fY - src[0].height() : pt.fY;
613
614 if (!dContext->priv().drawingManager()->newWritePixelsTask(
Brian Salomon974c8212021-04-05 16:24:00 -0400615 sk_ref_sp(dstProxy),
616 SkIRect::MakePtSize(pt, src[0].dimensions()),
617 allowedColorType,
618 this->colorInfo().colorType(),
619 srcLevels.begin(),
Brian Salomon75ee7372021-04-06 15:04:35 -0400620 numLevels)) {
Brian Salomon2c673402021-04-02 14:36:58 -0400621 return false;
622 }
623 if (numLevels > 1) {
624 dstProxy->asTextureProxy()->markMipmapsClean();
625 }
626 if (!ownAllStorage) {
627 // If any pixmap doesn't own its pixels then we must flush so that the pixels are pushed to
628 // the GPU before we return.
Brian Salomon24949422021-02-11 09:39:46 -0500629 dContext->priv().flushSurface(dstProxy);
630 }
Brian Salomon2c673402021-04-02 14:36:58 -0400631 return true;
Brian Osman45580d32016-11-23 09:37:01 -0500632}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400633
Adlai Hollerc95b5892020-08-11 12:02:22 -0400634void GrSurfaceContext::asyncRescaleAndReadPixels(GrDirectContext* dContext,
635 const SkImageInfo& info,
Brian Salomon63a0a752020-06-26 13:32:09 -0400636 const SkIRect& srcRect,
637 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500638 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400639 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400640 ReadPixelsContext callbackContext) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400641 if (!dContext) {
642 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400643 return;
644 }
645 auto rt = this->asRenderTargetProxy();
646 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400647 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400648 return;
649 }
650 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400651 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400652 return;
653 }
654 auto dstCT = SkColorTypeToGrColorType(info.colorType());
655 if (dstCT == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400656 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400657 return;
658 }
Brian Salomon827bb722021-05-06 16:24:21 -0400659 bool needsRescale = srcRect.size() != info.dimensions() ||
660 this->origin() == kBottomLeft_GrSurfaceOrigin ||
661 this->colorInfo().alphaType() != info.alphaType() ||
662 !SkColorSpace::Equals(this->colorInfo().colorSpace(), info.colorSpace());
Brian Salomon63a0a752020-06-26 13:32:09 -0400663 auto colorTypeOfFinalContext = this->colorInfo().colorType();
664 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
665 if (needsRescale) {
666 colorTypeOfFinalContext = dstCT;
667 backendFormatOfFinalContext =
668 this->caps()->getDefaultBackendFormat(dstCT, GrRenderable::kYes);
669 }
670 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonbacbb922021-01-21 19:48:00 -0500671 backendFormatOfFinalContext,
672 dstCT);
Brian Salomon63a0a752020-06-26 13:32:09 -0400673 // Fail if we can't read from the source surface's color type.
674 if (readInfo.fColorType == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400675 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400676 return;
677 }
678 // Fail if read color type does not have all of dstCT's color channels and those missing color
679 // channels are in the src.
680 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
681 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
682 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
683 if ((~legalReadChannels & dstChannels) & srcChannels) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400684 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400685 return;
686 }
687
Brian Salomonbacbb922021-01-21 19:48:00 -0500688 std::unique_ptr<GrSurfaceFillContext> tempFC;
Brian Salomon63a0a752020-06-26 13:32:09 -0400689 int x = srcRect.fLeft;
690 int y = srcRect.fTop;
691 if (needsRescale) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500692 tempFC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma, rescaleMode);
693 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400694 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400695 return;
696 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500697 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
698 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400699 x = y = 0;
Brian Salomon63a0a752020-06-26 13:32:09 -0400700 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500701 auto srcCtx = tempFC ? tempFC.get() : this;
702 return srcCtx->asyncReadPixels(dContext,
703 SkIRect::MakePtSize({x, y}, info.dimensions()),
704 info.colorType(),
705 callback,
706 callbackContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400707}
708
709class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult {
710public:
Robert Phillips82ad7af2021-03-11 16:00:10 -0500711 AsyncReadResult(GrDirectContext::DirectContextID intendedRecipient)
712 : fIntendedRecipient(intendedRecipient) {
713 }
714
Brian Salomon63a0a752020-06-26 13:32:09 -0400715 ~AsyncReadResult() override {
716 for (int i = 0; i < fPlanes.count(); ++i) {
Robert Phillips82ad7af2021-03-11 16:00:10 -0500717 fPlanes[i].releaseMappedBuffer(fIntendedRecipient);
Brian Salomon63a0a752020-06-26 13:32:09 -0400718 }
719 }
720
721 int count() const override { return fPlanes.count(); }
Brian Salomonbe1084b2021-01-26 13:29:30 -0500722 const void* data(int i) const override { return fPlanes[i].data(); }
723 size_t rowBytes(int i) const override { return fPlanes[i].rowBytes(); }
Brian Salomon63a0a752020-06-26 13:32:09 -0400724
725 bool addTransferResult(const PixelTransferResult& result,
726 SkISize dimensions,
727 size_t rowBytes,
728 GrClientMappedBufferManager* manager) {
729 SkASSERT(!result.fTransferBuffer->isMapped());
730 const void* mappedData = result.fTransferBuffer->map();
731 if (!mappedData) {
732 return false;
733 }
734 if (result.fPixelConverter) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500735 size_t size = rowBytes*dimensions.height();
736 sk_sp<SkData> data = SkData::MakeUninitialized(size);
737 result.fPixelConverter(data->writable_data(), mappedData);
738 this->addCpuPlane(std::move(data), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400739 result.fTransferBuffer->unmap();
740 } else {
741 manager->insert(result.fTransferBuffer);
742 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
743 }
744 return true;
745 }
746
Brian Salomonbe1084b2021-01-26 13:29:30 -0500747 void addCpuPlane(sk_sp<SkData> data, size_t rowBytes) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400748 SkASSERT(data);
749 SkASSERT(rowBytes > 0);
Brian Salomonbe1084b2021-01-26 13:29:30 -0500750 fPlanes.emplace_back(std::move(data), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400751 }
752
753private:
754 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
755 SkASSERT(data);
756 SkASSERT(rowBytes > 0);
757 SkASSERT(mappedBuffer);
758 SkASSERT(mappedBuffer->isMapped());
Brian Salomonbe1084b2021-01-26 13:29:30 -0500759 fPlanes.emplace_back(std::move(mappedBuffer), rowBytes);
Brian Salomon63a0a752020-06-26 13:32:09 -0400760 }
761
Brian Salomonbe1084b2021-01-26 13:29:30 -0500762 class Plane {
763 public:
764 Plane(sk_sp<GrGpuBuffer> buffer, size_t rowBytes)
765 : fMappedBuffer(std::move(buffer)), fRowBytes(rowBytes) {}
766 Plane(sk_sp<SkData> data, size_t rowBytes) : fData(std::move(data)), fRowBytes(rowBytes) {}
767
768 Plane(const Plane&) = delete;
769 Plane(Plane&&) = default;
770
771 ~Plane() { SkASSERT(!fMappedBuffer); }
772
773 Plane& operator=(const Plane&) = delete;
774 Plane& operator=(Plane&&) = default;
775
Robert Phillips82ad7af2021-03-11 16:00:10 -0500776 void releaseMappedBuffer(GrDirectContext::DirectContextID intendedRecipient) {
Brian Salomonbe1084b2021-01-26 13:29:30 -0500777 if (fMappedBuffer) {
778 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
Robert Phillips82ad7af2021-03-11 16:00:10 -0500779 {std::move(fMappedBuffer), intendedRecipient});
Brian Salomonbe1084b2021-01-26 13:29:30 -0500780 }
781 }
782
783 const void* data() const {
784 if (fMappedBuffer) {
785 SkASSERT(!fData);
786 SkASSERT(fMappedBuffer->isMapped());
787 return fMappedBuffer->map();
788 }
789 SkASSERT(fData);
790 return fData->data();
791 }
792
793 size_t rowBytes() const { return fRowBytes; }
794
795 private:
796 sk_sp<SkData> fData;
Brian Salomon1eea1ea2021-01-26 18:12:25 +0000797 sk_sp<GrGpuBuffer> fMappedBuffer;
Brian Salomonbe1084b2021-01-26 13:29:30 -0500798 size_t fRowBytes;
Brian Salomon63a0a752020-06-26 13:32:09 -0400799 };
800 SkSTArray<3, Plane> fPlanes;
Robert Phillips82ad7af2021-03-11 16:00:10 -0500801 GrDirectContext::DirectContextID fIntendedRecipient;
Brian Salomon63a0a752020-06-26 13:32:09 -0400802};
803
Adlai Hollerc95b5892020-08-11 12:02:22 -0400804void GrSurfaceContext::asyncReadPixels(GrDirectContext* dContext,
805 const SkIRect& rect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400806 SkColorType colorType,
807 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400808 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400809 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
810 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
811
Adlai Hollerc95b5892020-08-11 12:02:22 -0400812 if (!dContext || this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
813 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400814 return;
815 }
816
Adlai Hollerc95b5892020-08-11 12:02:22 -0400817 auto mappedBufferManager = dContext->priv().clientMappedBufferManager();
Brian Salomon63a0a752020-06-26 13:32:09 -0400818
819 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
820
821 if (!transferResult.fTransferBuffer) {
822 auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
823 this->colorInfo().refColorSpace());
Robert Phillips82ad7af2021-03-11 16:00:10 -0500824 static const GrDirectContext::DirectContextID kInvalid;
825 auto result = std::make_unique<AsyncReadResult>(kInvalid);
Brian Salomonbe1084b2021-01-26 13:29:30 -0500826 GrPixmap pm = GrPixmap::Allocate(ii);
827 result->addCpuPlane(pm.pixelStorage(), pm.rowBytes());
Brian Salomon63a0a752020-06-26 13:32:09 -0400828
Adlai Hollerc95b5892020-08-11 12:02:22 -0400829 SkIPoint pt{rect.fLeft, rect.fTop};
Brian Salomondd4087d2020-12-23 20:36:44 -0500830 if (!this->readPixels(dContext, pm, pt)) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400831 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400832 return;
833 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400834 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400835 return;
836 }
837
838 struct FinishContext {
839 ReadPixelsCallback* fClientCallback;
840 ReadPixelsContext fClientContext;
841 SkISize fSize;
842 SkColorType fColorType;
Jim Van Vertha655f0d2021-05-18 15:03:27 -0400843 size_t fBufferAlignment;
Brian Salomon63a0a752020-06-26 13:32:09 -0400844 GrClientMappedBufferManager* fMappedBufferManager;
845 PixelTransferResult fTransferResult;
846 };
847 // Assumption is that the caller would like to flush. We could take a parameter or require an
848 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
849 // callback to GrGpu until after the next flush that flushes our op list, though.
850 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400851 callbackContext,
Brian Salomon63a0a752020-06-26 13:32:09 -0400852 rect.size(),
853 colorType,
Jim Van Vertha655f0d2021-05-18 15:03:27 -0400854 this->caps()->transferBufferAlignment(),
Brian Salomon63a0a752020-06-26 13:32:09 -0400855 mappedBufferManager,
856 std::move(transferResult)};
857 auto finishCallback = [](GrGpuFinishedContext c) {
858 const auto* context = reinterpret_cast<const FinishContext*>(c);
Robert Phillips82ad7af2021-03-11 16:00:10 -0500859 auto manager = context->fMappedBufferManager;
860 auto result = std::make_unique<AsyncReadResult>(manager->owningDirectContext());
Jim Van Vertha655f0d2021-05-18 15:03:27 -0400861 size_t rowBytes =
862 GrAlignTo(context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType),
863 context->fBufferAlignment);
Brian Salomon63a0a752020-06-26 13:32:09 -0400864 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
Robert Phillips82ad7af2021-03-11 16:00:10 -0500865 manager)) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400866 result.reset();
867 }
868 (*context->fClientCallback)(context->fClientContext, std::move(result));
869 delete context;
870 };
871 GrFlushInfo flushInfo;
872 flushInfo.fFinishedContext = finishContext;
873 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -0500874
875 dContext->priv().flushSurface(this->asSurfaceProxy(),
876 SkSurface::BackendSurfaceAccess::kNoAccess,
877 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400878}
879
Adlai Hollerc95b5892020-08-11 12:02:22 -0400880void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(GrDirectContext* dContext,
881 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400882 sk_sp<SkColorSpace> dstColorSpace,
883 const SkIRect& srcRect,
884 SkISize dstSize,
885 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500886 RescaleMode rescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400887 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400888 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400889 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
890 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
891 SkASSERT(!dstSize.isZero());
892 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
893
Adlai Hollerc95b5892020-08-11 12:02:22 -0400894 if (!dContext) {
895 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400896 return;
897 }
898 auto rt = this->asRenderTargetProxy();
899 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400900 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400901 return;
902 }
903 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400904 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400905 return;
906 }
907 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400908 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400909 return;
910 }
911 int x = srcRect.fLeft;
912 int y = srcRect.fTop;
Brian Salomon827bb722021-05-06 16:24:21 -0400913 bool needsRescale = srcRect.size() != dstSize ||
914 !SkColorSpace::Equals(this->colorInfo().colorSpace(), dstColorSpace.get());
915 GrSurfaceProxyView srcView = this->readSurfaceView();
Brian Salomon63a0a752020-06-26 13:32:09 -0400916 if (needsRescale) {
Brian Salomon827bb722021-05-06 16:24:21 -0400917 auto info = SkImageInfo::Make(dstSize,
918 kRGBA_8888_SkColorType,
919 this->colorInfo().alphaType(),
920 dstColorSpace);
Brian Salomon63a0a752020-06-26 13:32:09 -0400921 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomonbacbb922021-01-21 19:48:00 -0500922 auto tempFC = this->rescale(info,
923 kTopLeft_GrSurfaceOrigin,
924 srcRect,
925 rescaleGamma,
926 rescaleMode);
927 if (!tempFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400928 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400929 return;
930 }
Brian Salomonbacbb922021-01-21 19:48:00 -0500931 SkASSERT(SkColorSpace::Equals(tempFC->colorInfo().colorSpace(), info.colorSpace()));
932 SkASSERT(tempFC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400933 x = y = 0;
Brian Salomonbacbb922021-01-21 19:48:00 -0500934 srcView = tempFC->readSurfaceView();
Brian Salomon827bb722021-05-06 16:24:21 -0400935 } else if (!srcView.asTextureProxy()) {
936 srcView = GrSurfaceProxyView::Copy(fContext,
937 std::move(srcView),
938 GrMipmapped::kNo,
939 srcRect,
940 SkBackingFit::kApprox,
941 SkBudgeted::kYes);
942 if (!srcView) {
943 // If we can't get a texture copy of the contents then give up.
944 callback(callbackContext, nullptr);
945 return;
Brian Salomon63a0a752020-06-26 13:32:09 -0400946 }
Brian Salomon827bb722021-05-06 16:24:21 -0400947 SkASSERT(srcView.asTextureProxy());
948 x = y = 0;
Brian Salomon63a0a752020-06-26 13:32:09 -0400949 }
950
Brian Salomonbacbb922021-01-21 19:48:00 -0500951 auto yInfo = SkImageInfo::MakeA8(dstSize);
952 auto yFC = GrSurfaceFillContext::MakeWithFallback(dContext, yInfo, SkBackingFit::kApprox);
953
954 auto uvInfo = yInfo.makeWH(yInfo.width()/2, yInfo.height()/2);
955 auto uFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
956 auto vFC = GrSurfaceFillContext::MakeWithFallback(dContext, uvInfo, SkBackingFit::kApprox);
957
958 if (!yFC || !uFC || !vFC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400959 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400960 return;
961 }
962
963 float baseM[20];
964 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
965
966 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
967
968 auto texMatrix = SkMatrix::Translate(x, y);
969
Brian Salomon63a0a752020-06-26 13:32:09 -0400970 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
971 PixelTransferResult yTransfer, uTransfer, vTransfer;
972
973 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
974 float yM[20];
975 std::fill_n(yM, 15, 0.f);
976 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500977
978 auto yFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
Brian Osman48f83fd2021-06-23 15:51:26 +0000979 yFP = GrFragmentProcessor::ColorMatrix(std::move(yFP),
980 yM,
981 /*unpremulInput=*/false,
982 /*clampRGBOutput=*/true,
983 /*premulOutput=*/false);
Brian Salomonbacbb922021-01-21 19:48:00 -0500984 yFC->fillWithFP(std::move(yFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400985 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -0500986 yTransfer = yFC->transferPixels(GrColorType::kAlpha_8,
987 SkIRect::MakeSize(yFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -0400988 if (!yTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400989 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400990 return;
991 }
992 }
993
994 texMatrix.preScale(2.f, 2.f);
995 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
996 float uM[20];
997 std::fill_n(uM, 15, 0.f);
998 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -0500999
1000 auto uFP = GrTextureEffect::Make(srcView,
1001 this->colorInfo().alphaType(),
1002 texMatrix,
1003 GrSamplerState::Filter::kLinear);
Brian Osman48f83fd2021-06-23 15:51:26 +00001004 uFP = GrFragmentProcessor::ColorMatrix(std::move(uFP),
1005 uM,
1006 /*unpremulInput=*/false,
1007 /*clampRGBOutput=*/true,
1008 /*premulOutput=*/false);
Brian Salomonbacbb922021-01-21 19:48:00 -05001009 uFC->fillWithFP(std::move(uFP));
Brian Salomon63a0a752020-06-26 13:32:09 -04001010 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001011 uTransfer = uFC->transferPixels(GrColorType::kAlpha_8,
1012 SkIRect::MakeSize(uFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -04001013 if (!uTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001014 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001015 return;
1016 }
1017 }
1018
1019 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
1020 float vM[20];
1021 std::fill_n(vM, 15, 0.f);
1022 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomonbacbb922021-01-21 19:48:00 -05001023 auto vFP = GrTextureEffect::Make(std::move(srcView),
1024 this->colorInfo().alphaType(),
1025 texMatrix,
1026 GrSamplerState::Filter::kLinear);
Brian Osman48f83fd2021-06-23 15:51:26 +00001027 vFP = GrFragmentProcessor::ColorMatrix(std::move(vFP),
1028 vM,
1029 /*unpremulInput=*/false,
1030 /*clampRGBOutput=*/true,
1031 /*premulOutput=*/false);
Brian Salomonbacbb922021-01-21 19:48:00 -05001032 vFC->fillWithFP(std::move(vFP));
1033
Brian Salomon63a0a752020-06-26 13:32:09 -04001034 if (!doSynchronousRead) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001035 vTransfer = vFC->transferPixels(GrColorType::kAlpha_8,
1036 SkIRect::MakeSize(vFC->dimensions()));
Brian Salomon63a0a752020-06-26 13:32:09 -04001037 if (!vTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001038 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001039 return;
1040 }
1041 }
1042
1043 if (doSynchronousRead) {
Brian Salomonbe1084b2021-01-26 13:29:30 -05001044 GrPixmap yPmp = GrPixmap::Allocate(yInfo);
1045 GrPixmap uPmp = GrPixmap::Allocate(uvInfo);
1046 GrPixmap vPmp = GrPixmap::Allocate(uvInfo);
Brian Salomonbacbb922021-01-21 19:48:00 -05001047 if (!yFC->readPixels(dContext, yPmp, {0, 0}) ||
1048 !uFC->readPixels(dContext, uPmp, {0, 0}) ||
1049 !vFC->readPixels(dContext, vPmp, {0, 0})) {
Adlai Hollerc95b5892020-08-11 12:02:22 -04001050 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -04001051 return;
1052 }
Robert Phillips82ad7af2021-03-11 16:00:10 -05001053 auto result = std::make_unique<AsyncReadResult>(dContext->directContextID());
Brian Salomonbe1084b2021-01-26 13:29:30 -05001054 result->addCpuPlane(yPmp.pixelStorage(), yPmp.rowBytes());
1055 result->addCpuPlane(uPmp.pixelStorage(), uPmp.rowBytes());
1056 result->addCpuPlane(vPmp.pixelStorage(), vPmp.rowBytes());
Adlai Hollerc95b5892020-08-11 12:02:22 -04001057 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -04001058 return;
1059 }
1060
1061 struct FinishContext {
1062 ReadPixelsCallback* fClientCallback;
1063 ReadPixelsContext fClientContext;
1064 GrClientMappedBufferManager* fMappedBufferManager;
1065 SkISize fSize;
Jim Van Vertha655f0d2021-05-18 15:03:27 -04001066 size_t fBufferAlignment;
Brian Salomon63a0a752020-06-26 13:32:09 -04001067 PixelTransferResult fYTransfer;
1068 PixelTransferResult fUTransfer;
1069 PixelTransferResult fVTransfer;
1070 };
1071 // Assumption is that the caller would like to flush. We could take a parameter or require an
1072 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1073 // callback to GrGpu until after the next flush that flushes our op list, though.
1074 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -04001075 callbackContext,
1076 dContext->priv().clientMappedBufferManager(),
Brian Salomon63a0a752020-06-26 13:32:09 -04001077 dstSize,
Jim Van Vertha655f0d2021-05-18 15:03:27 -04001078 this->caps()->transferBufferAlignment(),
Brian Salomon63a0a752020-06-26 13:32:09 -04001079 std::move(yTransfer),
1080 std::move(uTransfer),
1081 std::move(vTransfer)};
1082 auto finishCallback = [](GrGpuFinishedContext c) {
1083 const auto* context = reinterpret_cast<const FinishContext*>(c);
Brian Salomon63a0a752020-06-26 13:32:09 -04001084 auto manager = context->fMappedBufferManager;
Robert Phillips82ad7af2021-03-11 16:00:10 -05001085 auto result = std::make_unique<AsyncReadResult>(manager->owningDirectContext());
Brian Salomon63a0a752020-06-26 13:32:09 -04001086 size_t rowBytes = SkToSizeT(context->fSize.width());
Jim Van Vertha655f0d2021-05-18 15:03:27 -04001087 rowBytes = GrAlignTo(rowBytes, context->fBufferAlignment);
Brian Salomon63a0a752020-06-26 13:32:09 -04001088 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
1089 (*context->fClientCallback)(context->fClientContext, nullptr);
1090 delete context;
1091 return;
1092 }
Jim Van Vertha655f0d2021-05-18 15:03:27 -04001093 rowBytes = SkToSizeT(context->fSize.width()) / 2;
1094 rowBytes = GrAlignTo(rowBytes, context->fBufferAlignment);
Brian Salomon63a0a752020-06-26 13:32:09 -04001095 SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
1096 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
1097 (*context->fClientCallback)(context->fClientContext, nullptr);
1098 delete context;
1099 return;
1100 }
1101 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
1102 (*context->fClientCallback)(context->fClientContext, nullptr);
1103 delete context;
1104 return;
1105 }
1106 (*context->fClientCallback)(context->fClientContext, std::move(result));
1107 delete context;
1108 };
1109 GrFlushInfo flushInfo;
1110 flushInfo.fFinishedContext = finishContext;
1111 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -05001112 dContext->priv().flushSurface(this->asSurfaceProxy(),
1113 SkSurface::BackendSurfaceAccess::kNoAccess,
1114 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -04001115}
1116
Brian Salomond63638b2021-03-05 14:00:07 -05001117sk_sp<GrRenderTask> GrSurfaceContext::copy(sk_sp<GrSurfaceProxy> src,
1118 SkIRect srcRect,
1119 SkIPoint dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001120 ASSERT_SINGLE_OWNER
Brian Salomond63638b2021-03-05 14:00:07 -05001121 RETURN_NULLPTR_IF_ABANDONED
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001122 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -04001123 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -04001124
Brian Salomon947efe22019-07-16 15:36:11 -04001125 const GrCaps* caps = fContext->priv().caps();
1126
Greg Daniel46cfbc62019-06-07 11:43:30 -04001127 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -05001128 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -04001129
Stephen White3c0a50f2020-01-16 18:19:54 -05001130 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomond63638b2021-03-05 14:00:07 -05001131 return nullptr;
Stephen White3c0a50f2020-01-16 18:19:54 -05001132 }
1133
Brian Salomon982127b2021-01-21 10:43:35 -05001134 if (!caps->canCopySurface(this->asSurfaceProxy(), src.get(), srcRect, dstPoint)) {
Brian Salomond63638b2021-03-05 14:00:07 -05001135 return nullptr;
Greg Daniel25af6712018-04-25 10:44:38 -04001136 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001137
Brian Salomon982127b2021-01-21 10:43:35 -05001138 return this->drawingManager()->newCopyRenderTask(std::move(src),
1139 srcRect,
1140 this->asSurfaceProxyRef(),
Brian Salomon0f9f8002021-01-22 16:30:50 -05001141 dstPoint,
1142 this->origin());
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001143}
Greg Daniel46cfbc62019-06-07 11:43:30 -04001144
Brian Salomonbacbb922021-01-21 19:48:00 -05001145std::unique_ptr<GrSurfaceFillContext> GrSurfaceContext::rescale(const GrImageInfo& info,
Brian Salomoneebe7352020-12-09 16:37:04 -05001146 GrSurfaceOrigin origin,
1147 SkIRect srcRect,
1148 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001149 RescaleMode rescaleMode) {
Brian Salomonbacbb922021-01-21 19:48:00 -05001150 auto sfc = GrSurfaceFillContext::MakeWithFallback(fContext,
1151 info,
Brian Salomon1c86b632020-12-11 12:36:01 -05001152 SkBackingFit::kExact,
Brian Salomon1c86b632020-12-11 12:36:01 -05001153 1,
1154 GrMipmapped::kNo,
1155 this->asSurfaceProxy()->isProtected(),
1156 origin);
Brian Salomonbacbb922021-01-21 19:48:00 -05001157 if (!sfc || !this->rescaleInto(sfc.get(),
1158 SkIRect::MakeSize(sfc->dimensions()),
Brian Salomon1c86b632020-12-11 12:36:01 -05001159 srcRect,
1160 rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001161 rescaleMode)) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001162 return nullptr;
1163 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001164 return sfc;
Brian Salomon1c86b632020-12-11 12:36:01 -05001165}
1166
Brian Salomonbacbb922021-01-21 19:48:00 -05001167bool GrSurfaceContext::rescaleInto(GrSurfaceFillContext* dst,
Brian Salomon1c86b632020-12-11 12:36:01 -05001168 SkIRect dstRect,
1169 SkIRect srcRect,
1170 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -05001171 RescaleMode rescaleMode) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001172 SkASSERT(dst);
1173 if (!SkIRect::MakeSize(dst->dimensions()).contains((dstRect))) {
1174 return false;
1175 }
1176
Brian Salomone9ad9982019-07-22 16:17:41 -04001177 auto rtProxy = this->asRenderTargetProxy();
1178 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001179 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001180 }
1181
Stephen White3c0a50f2020-01-16 18:19:54 -05001182 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001183 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001184 }
1185
Greg Daniel40903af2020-01-30 14:55:05 -05001186 GrSurfaceProxyView texView = this->readSurfaceView();
Greg Daniel40903af2020-01-30 14:55:05 -05001187 if (!texView.asTextureProxy()) {
Brian Salomon827bb722021-05-06 16:24:21 -04001188 // TODO: If copying supported specifying a renderable copy then we could return the copy
1189 // when there are no other conversions.
Brian Salomon7e67dca2020-07-21 09:27:25 -04001190 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect,
Brian Salomonc5243782020-04-02 12:50:34 -04001191 SkBackingFit::kApprox, SkBudgeted::kNo);
1192 if (!texView) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001193 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001194 }
Greg Daniel40903af2020-01-30 14:55:05 -05001195 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001196 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001197 }
1198
Brian Salomon1c86b632020-12-11 12:36:01 -05001199 SkISize finalSize = dstRect.size();
Brian Salomon827bb722021-05-06 16:24:21 -04001200 if (finalSize == srcRect.size()) {
1201 rescaleGamma = RescaleGamma::kSrc;
1202 rescaleMode = RescaleMode::kNearest;
1203 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001204
Brian Salomonbf6b9792019-08-21 09:38:10 -04001205 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
1206 // 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 -05001207 std::unique_ptr<GrSurfaceFillContext> tempA;
1208 std::unique_ptr<GrSurfaceFillContext> tempB;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001209
Brian Salomone9ad9982019-07-22 16:17:41 -04001210 // Assume we should ignore the rescale linear request if the surface has no color space since
1211 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon63a0a752020-06-26 13:32:09 -04001212 if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() &&
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001213 !this->colorInfo().colorSpace()->gammaIsLinear()) {
1214 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomone9ad9982019-07-22 16:17:41 -04001215 // We'll fall back to kRGBA_8888 if half float not supported.
Brian Salomonbacbb922021-01-21 19:48:00 -05001216 GrImageInfo ii(GrColorType::kRGBA_F16,
1217 dst->colorInfo().alphaType(),
1218 std::move(cs),
1219 srcRect.size());
1220 auto linearRTC = GrSurfaceFillContext::MakeWithFallback(fContext,
1221 std::move(ii),
1222 SkBackingFit::kApprox,
1223 1,
1224 GrMipmapped::kNo,
1225 GrProtected::kNo,
1226 dst->origin());
Brian Salomone9ad9982019-07-22 16:17:41 -04001227 if (!linearRTC) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001228 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001229 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001230 auto fp = GrTextureEffect::Make(std::move(texView),
1231 this->colorInfo().alphaType(),
1232 SkMatrix::Translate(srcRect.topLeft()),
1233 GrSamplerState::Filter::kNearest,
1234 GrSamplerState::MipmapMode::kNone);
1235 fp = GrColorSpaceXformEffect::Make(std::move(fp),
1236 this->colorInfo(),
1237 linearRTC->colorInfo());
1238 linearRTC->fillWithFP(std::move(fp));
Greg Daniel40903af2020-01-30 14:55:05 -05001239 texView = linearRTC->readSurfaceView();
1240 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -04001241 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001242 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001243 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001244
Brian Salomon827bb722021-05-06 16:24:21 -04001245 do {
Brian Salomon1c86b632020-12-11 12:36:01 -05001246 SkISize nextDims = finalSize;
Mike Reed1efa14d2021-01-02 21:44:59 -05001247 if (rescaleMode != RescaleMode::kNearest) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001248 if (srcRect.width() > finalSize.width()) {
1249 nextDims.fWidth = std::max((srcRect.width() + 1)/2, finalSize.width());
1250 } else if (srcRect.width() < finalSize.width()) {
1251 nextDims.fWidth = std::min(srcRect.width()*2, finalSize.width());
Brian Salomon59f31b12020-06-04 17:27:15 -04001252 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001253 if (srcRect.height() > finalSize.height()) {
1254 nextDims.fHeight = std::max((srcRect.height() + 1)/2, finalSize.height());
1255 } else if (srcRect.height() < finalSize.height()) {
1256 nextDims.fHeight = std::min(srcRect.height()*2, finalSize.height());
Brian Salomon59f31b12020-06-04 17:27:15 -04001257 }
1258 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001259 auto input = tempA ? tempA.get() : this;
Brian Salomone9ad9982019-07-22 16:17:41 -04001260 sk_sp<GrColorSpaceXform> xform;
Brian Salomonbacbb922021-01-21 19:48:00 -05001261 GrSurfaceFillContext* stepDst;
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001262 SkIRect stepDstRect;
Brian Salomon1c86b632020-12-11 12:36:01 -05001263 if (nextDims == finalSize) {
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001264 stepDst = dst;
1265 stepDstRect = dstRect;
Brian Salomonbacbb922021-01-21 19:48:00 -05001266 xform = GrColorSpaceXform::Make(input->colorInfo(), dst->colorInfo());
Brian Salomon1c86b632020-12-11 12:36:01 -05001267 } else {
Brian Salomonbacbb922021-01-21 19:48:00 -05001268 GrImageInfo nextInfo(input->colorInfo(), nextDims);
1269 tempB = GrSurfaceFillContext::MakeWithFallback(fContext,
1270 nextInfo,
1271 SkBackingFit::kApprox);
Brian Salomon1c86b632020-12-11 12:36:01 -05001272 if (!tempB) {
1273 return false;
1274 }
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001275 stepDst = tempB.get();
1276 stepDstRect = SkIRect::MakeSize(tempB->dimensions());
Brian Salomone9ad9982019-07-22 16:17:41 -04001277 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001278 std::unique_ptr<GrFragmentProcessor> fp;
Mike Reed1efa14d2021-01-02 21:44:59 -05001279 if (rescaleMode == RescaleMode::kRepeatedCubic) {
Brian Salomon59f31b12020-06-04 17:27:15 -04001280 auto dir = GrBicubicEffect::Direction::kXY;
1281 if (nextDims.width() == srcRect.width()) {
1282 dir = GrBicubicEffect::Direction::kY;
1283 } else if (nextDims.height() == srcRect.height()) {
1284 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -04001285 }
Brian Salomon1af72d12020-06-25 10:47:26 -04001286 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
Mike Reed3867c702020-09-01 13:28:10 -04001287 static constexpr auto kKernel = GrBicubicEffect::gCatmullRom;
Brian Salomon1c86b632020-12-11 12:36:01 -05001288 fp = GrBicubicEffect::MakeSubset(std::move(texView),
1289 input->colorInfo().alphaType(),
Brian Salomonbacbb922021-01-21 19:48:00 -05001290 SkMatrix::I(),
Brian Salomon1c86b632020-12-11 12:36:01 -05001291 kWM,
1292 kWM,
1293 SkRect::Make(srcRect),
1294 kKernel,
1295 dir,
1296 *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -04001297 } else {
Mike Reed1efa14d2021-01-02 21:44:59 -05001298 auto filter = rescaleMode == RescaleMode::kNearest ? GrSamplerState::Filter::kNearest
1299 : GrSamplerState::Filter::kLinear;
Brian Salomonbacbb922021-01-21 19:48:00 -05001300 auto srcRectF = SkRect::Make(srcRect);
1301 fp = GrTextureEffect::MakeSubset(std::move(texView),
1302 this->colorInfo().alphaType(),
1303 SkMatrix::I(),
1304 {filter, GrSamplerState::MipmapMode::kNone},
1305 srcRectF,
1306 srcRectF,
1307 *this->caps());
Brian Salomone9ad9982019-07-22 16:17:41 -04001308 }
Brian Salomonbacbb922021-01-21 19:48:00 -05001309 if (xform) {
1310 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
1311 }
1312 stepDst->fillRectToRectWithFP(srcRect, stepDstRect, std::move(fp));
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001313 texView = stepDst->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -04001314 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001315 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomon827bb722021-05-06 16:24:21 -04001316 } while (srcRect.size() != finalSize);
Brian Salomon1c86b632020-12-11 12:36:01 -05001317 return true;
Brian Salomone9ad9982019-07-22 16:17:41 -04001318}
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001319
1320GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
1321 const SkIRect& rect) {
1322 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1323 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001324 auto direct = fContext->asDirectContext();
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001325 if (!direct) {
1326 return {};
1327 }
1328 auto rtProxy = this->asRenderTargetProxy();
1329 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1330 return {};
1331 }
1332
1333 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001334 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
1335 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001336 // Fail if read color type does not have all of dstCT's color channels and those missing color
1337 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001338 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1339 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
1340 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1341 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001342 return {};
1343 }
1344
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001345 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001346 !supportedRead.fOffsetAlignmentForTransferBuffer) {
1347 return {};
1348 }
1349
1350 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
Jim Van Vertha655f0d2021-05-18 15:03:27 -04001351 rowBytes = GrAlignTo(rowBytes, this->caps()->transferBufferAlignment());
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001352 size_t size = rowBytes * rect.height();
Greg Daniel2e967df2021-02-08 10:38:31 -05001353 // By using kStream_GrAccessPattern here, we are not able to cache and reuse the buffer for
1354 // multiple reads. Switching to kDynamic_GrAccessPattern would allow for this, however doing
1355 // so causes a crash in a chromium test. See skbug.com/11297
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001356 auto buffer = direct->priv().resourceProvider()->createBuffer(
Greg Daniel393debc2021-02-06 03:37:20 +00001357 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001358 if (!buffer) {
1359 return {};
1360 }
1361 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -05001362 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001363 if (flip) {
1364 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1365 this->height() - rect.fTop);
1366 }
Greg Danielbbfec9d2019-08-20 10:56:51 -04001367 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001368 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -04001369 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001370 PixelTransferResult result;
1371 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001372 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -04001373 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001374 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
1375 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001376 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
1377 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon5392c942021-03-30 16:14:37 -04001378 GrConvertPixels( GrPixmap(dstInfo, dst, dstInfo.minRowBytes()),
1379 GrCPixmap(srcInfo, src, srcInfo.minRowBytes()));
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001380 };
1381 }
1382 return result;
1383}
Greg Daniel46e366a2019-12-16 14:38:36 -05001384
1385#ifdef SK_DEBUG
1386void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001387 SkASSERT(fReadView.proxy());
1388 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -04001389 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
1390 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
1391 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
1392 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001393 this->onValidate();
1394}
1395#endif