blob: d2233b475e81c480eccc4dd252e45128d9247d9f [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
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040010#include "include/gpu/GrDirectContext.h"
11#include "include/gpu/GrRecordingContext.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040012#include "src/core/SkAutoPixmapStorage.h"
Brian Salomon63a0a752020-06-26 13:32:09 -040013#include "src/core/SkYUVMath.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040014#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrContextPriv.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040016#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrDrawingManager.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040018#include "src/gpu/GrGpu.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040019#include "src/gpu/GrImageInfo.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040020#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrRecordingContextPriv.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040022#include "src/gpu/GrRenderTargetContext.h"
Greg Daniel46cfbc62019-06-07 11:43:30 -040023#include "src/gpu/GrSurfaceContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/SkGr.h"
Brian Salomone9ad9982019-07-22 16:17:41 -040025#include "src/gpu/effects/GrBicubicEffect.h"
Brian Salomon63a0a752020-06-26 13:32:09 -040026#include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h"
Brian Osman45580d32016-11-23 09:37:01 -050027
Adlai Holler33dbd652020-06-01 12:35:42 -040028#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
Robert Phillips9eb00022020-06-30 15:30:12 -040029#define RETURN_FALSE_IF_ABANDONED if (this->fContext->abandoned()) { return false; }
Brian Osman45580d32016-11-23 09:37:01 -050030
Greg Danielbfa19c42019-12-19 16:41:40 -050031std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -050032 GrSurfaceProxyView readView,
Greg Danielbfa19c42019-12-19 16:41:40 -050033 GrColorType colorType,
34 SkAlphaType alphaType,
35 sk_sp<SkColorSpace> colorSpace) {
Greg Daniele20fcad2020-01-08 11:52:34 -050036 // It is probably not necessary to check if the context is abandoned here since uses of the
37 // GrSurfaceContext which need the context will mostly likely fail later on without an issue.
38 // However having this hear adds some reassurance in case there is a path doesn't handle an
39 // abandoned context correctly. It also lets us early out of some extra work.
Robert Phillips9eb00022020-06-30 15:30:12 -040040 if (context->abandoned()) {
Greg Daniele20fcad2020-01-08 11:52:34 -050041 return nullptr;
42 }
Greg Daniel3912a4b2020-01-14 09:56:04 -050043 GrSurfaceProxy* proxy = readView.proxy();
Greg Danielbfa19c42019-12-19 16:41:40 -050044 SkASSERT(proxy && proxy->asTextureProxy());
45
Greg Danielbfa19c42019-12-19 16:41:40 -050046 std::unique_ptr<GrSurfaceContext> surfaceContext;
Greg Daniel3912a4b2020-01-14 09:56:04 -050047 if (proxy->asRenderTargetProxy()) {
Greg Danielbfa19c42019-12-19 16:41:40 -050048 SkASSERT(kPremul_SkAlphaType == alphaType || kOpaque_SkAlphaType == alphaType);
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;
52 if (colorType != GrColorType::kUnknown) {
53 writeSwizzle =
54 context->priv().caps()->getWriteSwizzle(proxy->backendFormat(), colorType);
55 }
Brian Salomon8afde5f2020-04-01 16:22:00 -040056 GrSurfaceProxyView writeView(readView.refProxy(), readView.origin(), writeSwizzle);
Greg Daniel3912a4b2020-01-14 09:56:04 -050057 surfaceContext.reset(new GrRenderTargetContext(context, std::move(readView),
Brian Salomon8afde5f2020-04-01 16:22:00 -040058 std::move(writeView), colorType,
Greg Danielbfa19c42019-12-19 16:41:40 -050059 std::move(colorSpace), nullptr));
60 } else {
Greg Daniel3912a4b2020-01-14 09:56:04 -050061 surfaceContext.reset(new GrSurfaceContext(context, std::move(readView), colorType,
62 alphaType, std::move(colorSpace)));
Greg Danielbfa19c42019-12-19 16:41:40 -050063 }
Robert Phillips07f0e412020-01-17 15:20:00 -050064 SkDEBUGCODE(surfaceContext->validate();)
Greg Danielbfa19c42019-12-19 16:41:40 -050065 return surfaceContext;
66}
67
Brian Salomona56a7462020-02-07 14:17:25 -050068std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
69 SkISize dimensions,
70 const GrBackendFormat& format,
71 GrRenderable renderable,
72 int renderTargetSampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -040073 GrMipmapped mipMapped,
Brian Salomona56a7462020-02-07 14:17:25 -050074 GrProtected isProtected,
75 GrSurfaceOrigin origin,
76 GrColorType colorType,
77 SkAlphaType alphaType,
78 sk_sp<SkColorSpace> colorSpace,
79 SkBackingFit fit,
80 SkBudgeted budgeted) {
Brian Salomonc5243782020-04-02 12:50:34 -040081 GrSwizzle swizzle;
82 if (colorType != GrColorType::kUnknown && !context->priv().caps()->isFormatCompressed(format)) {
Brian Salomond005b692020-04-01 15:47:05 -040083 swizzle = context->priv().caps()->getReadSwizzle(format, colorType);
84 }
Greg Daniel47c20e82020-01-21 14:29:57 -050085
Greg Danielbfa19c42019-12-19 16:41:40 -050086 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -040087 format, dimensions, renderable, renderTargetSampleCnt, mipMapped, fit, budgeted,
88 isProtected);
Greg Danielbfa19c42019-12-19 16:41:40 -050089 if (!proxy) {
90 return nullptr;
91 }
92
Greg Daniel3912a4b2020-01-14 09:56:04 -050093 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
94 return GrSurfaceContext::Make(context, std::move(view), colorType, alphaType,
Greg Danielbfa19c42019-12-19 16:41:40 -050095 std::move(colorSpace));
96}
97
Greg Danielf41b2bd2019-08-22 16:19:24 -040098// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
99// GrOpsTasks to be picked up and added to by renderTargetContexts lower in the call
100// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
101// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500102GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500103 GrSurfaceProxyView readView,
Brian Salomond6287472019-06-24 15:50:07 -0400104 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -0400105 SkAlphaType alphaType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500106 sk_sp<SkColorSpace> colorSpace)
Greg Daniel901b98e2019-10-22 09:54:02 -0400107 : fContext(context)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500108 , fReadView(std::move(readView))
109 , fColorInfo(colorType, alphaType, std::move(colorSpace)) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400110 SkASSERT(!context->abandoned());
Greg Daniele20fcad2020-01-08 11:52:34 -0500111}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400112
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400113const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); }
114
Robert Phillips0d075de2019-03-04 11:08:13 -0500115GrAuditTrail* GrSurfaceContext::auditTrail() {
116 return fContext->priv().auditTrail();
117}
118
119GrDrawingManager* GrSurfaceContext::drawingManager() {
120 return fContext->priv().drawingManager();
121}
122
123const GrDrawingManager* GrSurfaceContext::drawingManager() const {
124 return fContext->priv().drawingManager();
125}
126
127#ifdef SK_DEBUG
128GrSingleOwner* GrSurfaceContext::singleOwner() {
129 return fContext->priv().singleOwner();
130}
131#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400132
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400133bool GrSurfaceContext::readPixels(const GrImageInfo& origDstInfo, void* dst, size_t rowBytes,
Robert Phillips44333c52020-06-30 13:28:00 -0400134 SkIPoint pt, GrDirectContext* direct) {
Brian Salomon1d435302019-07-01 13:05:28 -0400135 ASSERT_SINGLE_OWNER
136 RETURN_FALSE_IF_ABANDONED
137 SkDEBUGCODE(this->validate();)
138 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Greg Daniel6eb8c242019-06-05 10:22:24 -0400139
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400140 if (!direct && !(direct = fContext->asDirectContext())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400141 return false;
142 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400143
Brian Salomon1d435302019-07-01 13:05:28 -0400144 if (!dst) {
145 return false;
146 }
147
Brian Salomon1047a492019-07-02 12:25:21 -0400148 size_t tightRowBytes = origDstInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400149 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400150 rowBytes = tightRowBytes;
151 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400152 return false;
153 }
154
155 if (!origDstInfo.isValid()) {
156 return false;
157 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400158
159 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
160
Stephen White3c0a50f2020-01-16 18:19:54 -0500161 if (srcProxy->framebufferOnly()) {
162 return false;
163 }
164
Greg Daniel6eb8c242019-06-05 10:22:24 -0400165 // MDB TODO: delay this instantiation until later in the method
166 if (!srcProxy->instantiate(direct->priv().resourceProvider())) {
167 return false;
168 }
169
170 GrSurface* srcSurface = srcProxy->peekSurface();
171
Brian Salomon1d435302019-07-01 13:05:28 -0400172 auto dstInfo = origDstInfo;
173 if (!dstInfo.clip(this->width(), this->height(), &pt, &dst, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400174 return false;
175 }
Brian Salomon1047a492019-07-02 12:25:21 -0400176 // Our tight row bytes may have been changed by clipping.
177 tightRowBytes = dstInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400178
Mike Klein7321e6a2019-12-03 11:08:40 -0600179 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{this->colorInfo(), dstInfo}.flags;
180 bool unpremul = flags.unpremul,
181 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
182 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400183
184 const GrCaps* caps = direct->priv().caps();
Robert Phillips07f0e412020-01-17 15:20:00 -0500185 bool srcIsCompressed = caps->isFormatCompressed(srcSurface->backendFormat());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400186 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
187 // care so much about getImageData performance. However, in order to ensure putImageData/
188 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
189 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
190 // fContext->vaildaPMUPMConversionExists()).
Greg Daniel0258c902019-08-01 13:08:33 -0400191 GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
192 GrRenderable::kYes);
Greg Danielc71c7962020-01-14 16:44:18 -0500193 GrColorType srcColorType = this->colorInfo().colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400194 bool canvas2DFastPath = unpremul && !needColorConversion &&
195 (GrColorType::kRGBA_8888 == dstInfo.colorType() ||
196 GrColorType::kBGRA_8888 == dstInfo.colorType()) &&
197 SkToBool(srcProxy->asTextureProxy()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500198 (srcColorType == GrColorType::kRGBA_8888 ||
199 srcColorType == GrColorType::kBGRA_8888) &&
Greg Daniel0258c902019-08-01 13:08:33 -0400200 defaultRGBAFormat.isValid() &&
Brian Salomon1d435302019-07-01 13:05:28 -0400201 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400202
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400203 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400204 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400205 return false;
206 }
207
Brian Salomondc0710f2019-07-01 14:59:32 -0400208 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Robert Phillips07f0e412020-01-17 15:20:00 -0500209 GrColorType colorType = (canvas2DFastPath || srcIsCompressed)
210 ? GrColorType::kRGBA_8888 : this->colorInfo().colorType();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400211 sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorInfo().refColorSpace();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400212
Greg Daniele20fcad2020-01-08 11:52:34 -0500213 auto tempCtx = GrRenderTargetContext::Make(
214 direct, colorType, std::move(cs), SkBackingFit::kApprox, dstInfo.dimensions(),
Brian Salomon7e67dca2020-07-21 09:27:25 -0400215 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400216 if (!tempCtx) {
217 return false;
218 }
219
220 std::unique_ptr<GrFragmentProcessor> fp;
221 if (canvas2DFastPath) {
Greg Danield2ccbb52020-02-05 10:45:39 -0500222 fp = direct->priv().createPMToUPMEffect(
223 GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType()));
Brian Salomon1d435302019-07-01 13:05:28 -0400224 if (dstInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400225 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
Brian Salomon1d435302019-07-01 13:05:28 -0400226 dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400227 }
Brian Salomon1d435302019-07-01 13:05:28 -0400228 // The render target context is incorrectly tagged as kPremul even though we're writing
229 // unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type so we don't
230 // double unpremul.
231 dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400232 } else {
Greg Danield2ccbb52020-02-05 10:45:39 -0500233 fp = GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400234 }
235 if (!fp) {
236 return false;
237 }
238 GrPaint paint;
239 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
John Stiles5933d7d2020-07-21 12:28:35 -0400240 paint.setColorFragmentProcessor(std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400241
242 tempCtx->asRenderTargetContext()->fillRectToRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400243 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400244 SkRect::MakeWH(dstInfo.width(), dstInfo.height()),
245 SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400246
Brian Salomon1d435302019-07-01 13:05:28 -0400247 return tempCtx->readPixels(dstInfo, dst, rowBytes, {0, 0}, direct);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400248 }
249
Greg Danielb8d84f82020-02-13 14:25:00 -0500250 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400251
Brian Salomon1d435302019-07-01 13:05:28 -0400252 auto supportedRead = caps->supportedReadPixelsColorType(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400253 this->colorInfo().colorType(), srcProxy->backendFormat(), dstInfo.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400254
Brian Salomon1047a492019-07-02 12:25:21 -0400255 bool makeTight = !caps->readPixelsRowBytesSupport() && tightRowBytes != rowBytes;
256
257 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomon8f8354a2019-07-31 20:12:02 -0400258 (dstInfo.colorType() != supportedRead.fColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400259
Brian Salomonf30b1c12019-06-20 12:25:02 -0400260 std::unique_ptr<char[]> tmpPixels;
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400261 GrImageInfo tmpInfo;
Brian Salomon1d435302019-07-01 13:05:28 -0400262 void* readDst = dst;
263 size_t readRB = rowBytes;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400264 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400265 tmpInfo = {supportedRead.fColorType, this->colorInfo().alphaType(),
266 this->colorInfo().refColorSpace(), dstInfo.width(), dstInfo.height()};
Brian Salomon1d435302019-07-01 13:05:28 -0400267 size_t tmpRB = tmpInfo.minRowBytes();
268 size_t size = tmpRB * tmpInfo.height();
269 // Chrome MSAN bots require the data to be initialized (hence the ()).
270 tmpPixels.reset(new char[size]());
Brian Salomonf30b1c12019-06-20 12:25:02 -0400271
Brian Salomonf30b1c12019-06-20 12:25:02 -0400272 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400273 readRB = tmpRB;
274 pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400275 }
276
Greg Daniel55f040b2020-02-13 15:38:32 +0000277 direct->priv().flushSurface(srcProxy);
Greg Daniel04283f32020-05-20 13:16:00 -0400278 direct->submit();
Brian Salomon1d435302019-07-01 13:05:28 -0400279 if (!direct->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400280 dstInfo.height(), this->colorInfo().colorType(),
Brian Salomonf77c1462019-08-01 15:19:29 -0400281 supportedRead.fColorType, readDst, readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400282 return false;
283 }
284
Greg Daniel6eb8c242019-06-05 10:22:24 -0400285 if (convert) {
Brian Salomon8f8354a2019-07-31 20:12:02 -0400286 return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400287 }
288 return true;
289}
Robert Phillips0d075de2019-03-04 11:08:13 -0500290
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400291bool GrSurfaceContext::writePixels(const GrImageInfo& origSrcInfo, const void* src, size_t rowBytes,
Robert Phillips44333c52020-06-30 13:28:00 -0400292 SkIPoint pt, GrDirectContext* direct) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400293 ASSERT_SINGLE_OWNER
294 RETURN_FALSE_IF_ABANDONED
295 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400296 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400297
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400298 if (!direct && !(direct = fContext->asDirectContext())) {
Brian Salomonc320b152018-02-20 14:05:36 -0500299 return false;
300 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500301
Brian Salomon1d435302019-07-01 13:05:28 -0400302 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500303 return false;
304 }
305
Brian Salomon1d435302019-07-01 13:05:28 -0400306 if (!src) {
307 return false;
308 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400309
Brian Salomon1047a492019-07-02 12:25:21 -0400310 size_t tightRowBytes = origSrcInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400311 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400312 rowBytes = tightRowBytes;
313 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400314 return false;
315 }
316
317 if (!origSrcInfo.isValid()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400318 return false;
319 }
320
321 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
Stephen White3c0a50f2020-01-16 18:19:54 -0500322
323 if (dstProxy->framebufferOnly()) {
324 return false;
325 }
326
Greg Daniel6eb8c242019-06-05 10:22:24 -0400327 if (!dstProxy->instantiate(direct->priv().resourceProvider())) {
328 return false;
329 }
330
331 GrSurface* dstSurface = dstProxy->peekSurface();
332
Brian Salomon1d435302019-07-01 13:05:28 -0400333 auto srcInfo = origSrcInfo;
334 if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400335 return false;
336 }
Brian Salomon1047a492019-07-02 12:25:21 -0400337 // Our tight row bytes may have been changed by clipping.
338 tightRowBytes = srcInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400339
Mike Klein7321e6a2019-12-03 11:08:40 -0600340 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{srcInfo, this->colorInfo()}.flags;
341 bool unpremul = flags.unpremul,
342 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
343 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400344
345 const GrCaps* caps = direct->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400346
347 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
348 GrRenderable::kNo);
349
Greg Danielc71c7962020-01-14 16:44:18 -0500350 GrColorType dstColorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400351 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
352 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400353 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
354 (srcInfo.colorType() == GrColorType::kRGBA_8888 ||
355 srcInfo.colorType() == GrColorType::kBGRA_8888) &&
356 SkToBool(this->asRenderTargetContext()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500357 (dstColorType == GrColorType::kRGBA_8888 ||
358 dstColorType == GrColorType::kBGRA_8888) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400359 rgbaDefaultFormat.isValid() &&
Brian Salomon1d435302019-07-01 13:05:28 -0400360 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400361
362 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
Brian Salomond6287472019-06-24 15:50:07 -0400363 GrColorType colorType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400364
365 GrBackendFormat format;
Brian Salomone7499c72019-06-24 12:12:36 -0400366 SkAlphaType alphaType;
Greg Danielbfa19c42019-12-19 16:41:40 -0500367 GrSwizzle tempReadSwizzle;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400368 if (canvas2DFastPath) {
Brian Salomond6287472019-06-24 15:50:07 -0400369 colorType = GrColorType::kRGBA_8888;
Greg Daniel7bfc9132019-08-14 14:23:53 -0400370 format = rgbaDefaultFormat;
Brian Salomone7499c72019-06-24 12:12:36 -0400371 alphaType = kUnpremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400372 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400373 colorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400374 format = dstProxy->backendFormat().makeTexture2D();
375 if (!format.isValid()) {
376 return false;
377 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400378 alphaType = this->colorInfo().alphaType();
Greg Danielbfa19c42019-12-19 16:41:40 -0500379 tempReadSwizzle = this->readSwizzle();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400380 }
381
Greg Daniel2e52ad12019-06-13 10:04:16 -0400382 // It is more efficient for us to write pixels into a top left origin so we prefer that.
383 // However, if the final proxy isn't a render target then we must use a copy to move the
384 // data into it which requires the origins to match. If the final proxy is a render target
385 // we can use a draw instead which doesn't have this origin restriction. Thus for render
386 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400387 GrSurfaceOrigin tempOrigin =
Greg Danielb8d84f82020-02-13 14:25:00 -0500388 this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : this->origin();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400389 auto tempProxy = direct->priv().proxyProvider()->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400390 format, srcInfo.dimensions(), GrRenderable::kNo, 1, GrMipmapped::kNo,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400391 SkBackingFit::kApprox, SkBudgeted::kYes, GrProtected::kNo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400392 if (!tempProxy) {
393 return false;
394 }
Greg Daniel3912a4b2020-01-14 09:56:04 -0500395 GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle);
Greg Danield2ccbb52020-02-05 10:45:39 -0500396 GrSurfaceContext tempCtx(direct, tempView, colorType, alphaType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500397 this->colorInfo().refColorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400398
399 // In the fast path we always write the srcData to the temp context as though it were RGBA.
400 // When the data is really BGRA the write will cause the R and B channels to be swapped in
401 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
402 // dst.
Brian Salomon1d435302019-07-01 13:05:28 -0400403 if (canvas2DFastPath) {
404 srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888);
405 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500406 if (!tempCtx.writePixels(srcInfo, src, rowBytes, {0, 0}, direct)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400407 return false;
408 }
409
410 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400411 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400412 if (canvas2DFastPath) {
Brian Salomonb8f098d2020-01-07 11:15:44 -0500413 fp = direct->priv().createUPMToPMEffect(
Greg Danield2ccbb52020-02-05 10:45:39 -0500414 GrTextureEffect::Make(std::move(tempView), alphaType));
Brian Salomon1d435302019-07-01 13:05:28 -0400415 // Important: check the original src color type here!
416 if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400417 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
418 }
419 } else {
Greg Danield2ccbb52020-02-05 10:45:39 -0500420 fp = GrTextureEffect::Make(std::move(tempView), alphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400421 }
422 if (!fp) {
423 return false;
424 }
425 GrPaint paint;
426 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
John Stiles5933d7d2020-07-21 12:28:35 -0400427 paint.setColorFragmentProcessor(std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400428 this->asRenderTargetContext()->fillRectToRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400429 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400430 SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()),
431 SkRect::MakeWH(srcInfo.width(), srcInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400432 } else {
Brian Salomon1d435302019-07-01 13:05:28 -0400433 SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height());
434 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Brian Salomonc5243782020-04-02 12:50:34 -0400435 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400436 return false;
437 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400438 }
439 return true;
440 }
441
Brian Salomon1d435302019-07-01 13:05:28 -0400442 GrColorType allowedColorType =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400443 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400444 dstProxy->backendFormat(),
445 srcInfo.colorType()).fColorType;
Greg Danielb8d84f82020-02-13 14:25:00 -0500446 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon1047a492019-07-02 12:25:21 -0400447 bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes;
448 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomon1d435302019-07-01 13:05:28 -0400449 (srcInfo.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400450
Brian Salomonf30b1c12019-06-20 12:25:02 -0400451 std::unique_ptr<char[]> tmpPixels;
Brian Salomon1d435302019-07-01 13:05:28 -0400452 GrColorType srcColorType = srcInfo.colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400453 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400454 GrImageInfo tmpInfo(allowedColorType, this->colorInfo().alphaType(),
455 this->colorInfo().refColorSpace(), srcInfo.width(), srcInfo.height());
Brian Salomon1d435302019-07-01 13:05:28 -0400456 auto tmpRB = tmpInfo.minRowBytes();
457 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400458
Brian Salomon1d435302019-07-01 13:05:28 -0400459 GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400460
Brian Salomon1d435302019-07-01 13:05:28 -0400461 srcColorType = tmpInfo.colorType();
462 rowBytes = tmpRB;
463 src = tmpPixels.get();
464 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400465 }
466
467 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
468 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
469 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
470 // destination proxy)
471 // TODO: should this policy decision just be moved into the drawing manager?
Greg Daniel55f040b2020-02-13 15:38:32 +0000472 direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400473
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400474 return direct->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(),
475 srcInfo.height(), this->colorInfo().colorType(),
476 srcColorType, src, rowBytes);
Brian Osman45580d32016-11-23 09:37:01 -0500477}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400478
Brian Salomon63a0a752020-06-26 13:32:09 -0400479void GrSurfaceContext::asyncRescaleAndReadPixels(const SkImageInfo& info,
480 const SkIRect& srcRect,
481 RescaleGamma rescaleGamma,
482 SkFilterQuality rescaleQuality,
483 ReadPixelsCallback callback,
484 ReadPixelsContext context) {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400485 auto direct = fContext->asDirectContext();
Brian Salomon63a0a752020-06-26 13:32:09 -0400486
487 // We implement this by rendering and we don't currently support rendering kUnpremul.
488 if (info.alphaType() == kUnpremul_SkAlphaType) {
489 callback(context, nullptr);
490 return;
491 }
492 if (!direct) {
493 callback(context, nullptr);
494 return;
495 }
496 auto rt = this->asRenderTargetProxy();
497 if (rt && rt->wrapsVkSecondaryCB()) {
498 callback(context, nullptr);
499 return;
500 }
501 if (rt && rt->framebufferOnly()) {
502 callback(context, nullptr);
503 return;
504 }
505 auto dstCT = SkColorTypeToGrColorType(info.colorType());
506 if (dstCT == GrColorType::kUnknown) {
507 callback(context, nullptr);
508 return;
509 }
510 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
511 auto colorTypeOfFinalContext = this->colorInfo().colorType();
512 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
513 if (needsRescale) {
514 colorTypeOfFinalContext = dstCT;
515 backendFormatOfFinalContext =
516 this->caps()->getDefaultBackendFormat(dstCT, GrRenderable::kYes);
517 }
518 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
519 backendFormatOfFinalContext, dstCT);
520 // Fail if we can't read from the source surface's color type.
521 if (readInfo.fColorType == GrColorType::kUnknown) {
522 callback(context, nullptr);
523 return;
524 }
525 // Fail if read color type does not have all of dstCT's color channels and those missing color
526 // channels are in the src.
527 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
528 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
529 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
530 if ((~legalReadChannels & dstChannels) & srcChannels) {
531 callback(context, nullptr);
532 return;
533 }
534
535 std::unique_ptr<GrRenderTargetContext> tempRTC;
536 int x = srcRect.fLeft;
537 int y = srcRect.fTop;
538 if (needsRescale) {
539 tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
540 rescaleQuality);
541 if (!tempRTC) {
542 callback(context, nullptr);
543 return;
544 }
545 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
546 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
547 x = y = 0;
548 } else {
549 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
550 this->colorInfo().alphaType(),
551 info.colorSpace(),
552 info.alphaType());
553 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
554 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
555 GrSurfaceProxyView texProxyView = this->readSurfaceView();
556 SkRect srcRectToDraw = SkRect::Make(srcRect);
557 // If the src is not texturable first try to make a copy to a texture.
558 if (!texProxyView.asTextureProxy()) {
559 texProxyView =
Brian Salomon7e67dca2020-07-21 09:27:25 -0400560 GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipmapped::kNo, srcRect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400561 SkBackingFit::kApprox, SkBudgeted::kNo);
562 if (!texProxyView) {
563 callback(context, nullptr);
564 return;
565 }
566 SkASSERT(texProxyView.asTextureProxy());
567 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
568 }
569 tempRTC = GrRenderTargetContext::Make(direct, this->colorInfo().colorType(),
570 info.refColorSpace(), SkBackingFit::kApprox,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400571 srcRect.size(), 1, GrMipmapped::kNo,
Brian Salomon63a0a752020-06-26 13:32:09 -0400572 GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
573 if (!tempRTC) {
574 callback(context, nullptr);
575 return;
576 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400577 tempRTC->drawTexture(nullptr,
578 std::move(texProxyView),
579 this->colorInfo().alphaType(),
580 GrSamplerState::Filter::kNearest,
581 GrSamplerState::MipmapMode::kNone,
582 SkBlendMode::kSrc,
583 SK_PMColor4fWHITE,
584 srcRectToDraw,
585 SkRect::MakeWH(srcRect.width(), srcRect.height()),
586 GrAA::kNo,
587 GrQuadAAFlags::kNone,
588 SkCanvas::kFast_SrcRectConstraint,
589 SkMatrix::I(),
590 std::move(xform));
Brian Salomon63a0a752020-06-26 13:32:09 -0400591 x = y = 0;
592 }
593 }
594 auto rtc = tempRTC ? tempRTC.get() : this;
595 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
596 info.colorType(), callback, context);
597}
598
599class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult {
600public:
601 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
602 ~AsyncReadResult() override {
603 for (int i = 0; i < fPlanes.count(); ++i) {
604 if (!fPlanes[i].fMappedBuffer) {
605 delete[] static_cast<const char*>(fPlanes[i].fData);
606 } else {
607 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
608 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
609 }
610 }
611 }
612
613 int count() const override { return fPlanes.count(); }
614 const void* data(int i) const override { return fPlanes[i].fData; }
615 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
616
617 bool addTransferResult(const PixelTransferResult& result,
618 SkISize dimensions,
619 size_t rowBytes,
620 GrClientMappedBufferManager* manager) {
621 SkASSERT(!result.fTransferBuffer->isMapped());
622 const void* mappedData = result.fTransferBuffer->map();
623 if (!mappedData) {
624 return false;
625 }
626 if (result.fPixelConverter) {
627 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
628 result.fPixelConverter(convertedData.get(), mappedData);
629 this->addCpuPlane(std::move(convertedData), rowBytes);
630 result.fTransferBuffer->unmap();
631 } else {
632 manager->insert(result.fTransferBuffer);
633 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
634 }
635 return true;
636 }
637
638 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
639 SkASSERT(data);
640 SkASSERT(rowBytes > 0);
641 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
642 }
643
644private:
645 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
646 SkASSERT(data);
647 SkASSERT(rowBytes > 0);
648 SkASSERT(mappedBuffer);
649 SkASSERT(mappedBuffer->isMapped());
650 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
651 }
652
653 struct Plane {
654 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
655 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
656 const void* fData;
657 size_t fRowBytes;
658 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
659 sk_sp<GrGpuBuffer> fMappedBuffer;
660 };
661 SkSTArray<3, Plane> fPlanes;
662 uint32_t fInboxID;
663};
664
665void GrSurfaceContext::asyncReadPixels(const SkIRect& rect,
666 SkColorType colorType,
667 ReadPixelsCallback callback,
668 ReadPixelsContext context) {
669 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
670 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
671
672 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
673 callback(context, nullptr);
674 return;
675 }
676
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400677 auto directContext = fContext->asDirectContext();
Brian Salomon63a0a752020-06-26 13:32:09 -0400678 SkASSERT(directContext);
679 auto mappedBufferManager = directContext->priv().clientMappedBufferManager();
680
681 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
682
683 if (!transferResult.fTransferBuffer) {
684 auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
685 this->colorInfo().refColorSpace());
686 auto result = std::make_unique<AsyncReadResult>(0);
687 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
688 SkPixmap pm(ii, data.get(), ii.minRowBytes());
689 result->addCpuPlane(std::move(data), pm.rowBytes());
690
691 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
692 callback(context, nullptr);
693 return;
694 }
695 callback(context, std::move(result));
696 return;
697 }
698
699 struct FinishContext {
700 ReadPixelsCallback* fClientCallback;
701 ReadPixelsContext fClientContext;
702 SkISize fSize;
703 SkColorType fColorType;
704 GrClientMappedBufferManager* fMappedBufferManager;
705 PixelTransferResult fTransferResult;
706 };
707 // Assumption is that the caller would like to flush. We could take a parameter or require an
708 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
709 // callback to GrGpu until after the next flush that flushes our op list, though.
710 auto* finishContext = new FinishContext{callback,
711 context,
712 rect.size(),
713 colorType,
714 mappedBufferManager,
715 std::move(transferResult)};
716 auto finishCallback = [](GrGpuFinishedContext c) {
717 const auto* context = reinterpret_cast<const FinishContext*>(c);
718 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
719 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
720 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
721 context->fMappedBufferManager)) {
722 result.reset();
723 }
724 (*context->fClientCallback)(context->fClientContext, std::move(result));
725 delete context;
726 };
727 GrFlushInfo flushInfo;
728 flushInfo.fFinishedContext = finishContext;
729 flushInfo.fFinishedProc = finishCallback;
730 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo, nullptr);
731}
732
733void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
734 sk_sp<SkColorSpace> dstColorSpace,
735 const SkIRect& srcRect,
736 SkISize dstSize,
737 RescaleGamma rescaleGamma,
738 SkFilterQuality rescaleQuality,
739 ReadPixelsCallback callback,
740 ReadPixelsContext context) {
741 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
742 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
743 SkASSERT(!dstSize.isZero());
744 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
745
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400746 auto direct = fContext->asDirectContext();
Brian Salomon63a0a752020-06-26 13:32:09 -0400747 if (!direct) {
748 callback(context, nullptr);
749 return;
750 }
751 auto rt = this->asRenderTargetProxy();
752 if (rt && rt->wrapsVkSecondaryCB()) {
753 callback(context, nullptr);
754 return;
755 }
756 if (rt && rt->framebufferOnly()) {
757 callback(context, nullptr);
758 return;
759 }
760 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
761 callback(context, nullptr);
762 return;
763 }
764 int x = srcRect.fLeft;
765 int y = srcRect.fTop;
766 bool needsRescale = srcRect.size() != dstSize;
767 GrSurfaceProxyView srcView;
768 if (needsRescale) {
769 // We assume the caller wants kPremul. There is no way to indicate a preference.
770 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
771 dstColorSpace);
772 // TODO: Incorporate the YUV conversion into last pass of rescaling.
773 auto tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
774 rescaleQuality);
775 if (!tempRTC) {
776 callback(context, nullptr);
777 return;
778 }
779 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
780 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
781 x = y = 0;
782 srcView = tempRTC->readSurfaceView();
783 } else {
784 srcView = this->readSurfaceView();
785 if (!srcView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400786 srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipmapped::kNo,
Brian Salomon63a0a752020-06-26 13:32:09 -0400787 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
788 if (!srcView) {
789 // If we can't get a texture copy of the contents then give up.
790 callback(context, nullptr);
791 return;
792 }
793 SkASSERT(srcView.asTextureProxy());
794 x = y = 0;
795 }
796 // We assume the caller wants kPremul. There is no way to indicate a preference.
797 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
798 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
799 kPremul_SkAlphaType);
800 if (xform) {
801 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
802 auto tempRTC = GrRenderTargetContext::Make(
803 direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400804 dstSize, 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400805 if (!tempRTC) {
806 callback(context, nullptr);
807 return;
808 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400809 tempRTC->drawTexture(nullptr,
810 std::move(srcView),
811 this->colorInfo().alphaType(),
812 GrSamplerState::Filter::kNearest,
813 GrSamplerState::MipmapMode::kNone,
814 SkBlendMode::kSrc,
815 SK_PMColor4fWHITE,
816 srcRectToDraw,
817 SkRect::Make(srcRect.size()),
818 GrAA::kNo,
819 GrQuadAAFlags::kNone,
820 SkCanvas::kFast_SrcRectConstraint,
821 SkMatrix::I(),
822 std::move(xform));
Brian Salomon63a0a752020-06-26 13:32:09 -0400823 srcView = tempRTC->readSurfaceView();
824 SkASSERT(srcView.asTextureProxy());
825 x = y = 0;
826 }
827 }
828
829 auto yRTC = GrRenderTargetContext::MakeWithFallback(
830 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400831 GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400832 int halfW = dstSize.width() /2;
833 int halfH = dstSize.height()/2;
834 auto uRTC = GrRenderTargetContext::MakeWithFallback(
835 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400836 GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400837 auto vRTC = GrRenderTargetContext::MakeWithFallback(
838 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400839 GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400840 if (!yRTC || !uRTC || !vRTC) {
841 callback(context, nullptr);
842 return;
843 }
844
845 float baseM[20];
846 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
847
848 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
849
850 auto texMatrix = SkMatrix::Translate(x, y);
851
852 SkRect dstRectY = SkRect::Make(dstSize);
853 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
854
855 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
856 PixelTransferResult yTransfer, uTransfer, vTransfer;
857
858 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
859 float yM[20];
860 std::fill_n(yM, 15, 0.f);
861 std::copy_n(baseM + 0, 5, yM + 15);
862 GrPaint yPaint;
863 auto yTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
864 auto yColFP = GrColorMatrixFragmentProcessor::Make(std::move(yTexFP), yM,
865 /*unpremulInput=*/false,
866 /*clampRGBOutput=*/true,
867 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400868 yPaint.setColorFragmentProcessor(std::move(yColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400869 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
870 yRTC->fillRectToRect(nullptr, std::move(yPaint), GrAA::kNo, SkMatrix::I(), dstRectY, dstRectY);
871 if (!doSynchronousRead) {
872 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
873 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
874 if (!yTransfer.fTransferBuffer) {
875 callback(context, nullptr);
876 return;
877 }
878 }
879
880 texMatrix.preScale(2.f, 2.f);
881 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
882 float uM[20];
883 std::fill_n(uM, 15, 0.f);
884 std::copy_n(baseM + 5, 5, uM + 15);
885 GrPaint uPaint;
886 auto uTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix,
Brian Salomona3b02f52020-07-15 16:02:01 -0400887 GrSamplerState::Filter::kLinear);
Brian Salomon63a0a752020-06-26 13:32:09 -0400888 auto uColFP = GrColorMatrixFragmentProcessor::Make(std::move(uTexFP), uM,
889 /*unpremulInput=*/false,
890 /*clampRGBOutput=*/true,
891 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400892 uPaint.setColorFragmentProcessor(std::move(uColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400893 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
894 uRTC->fillRectToRect(nullptr, std::move(uPaint), GrAA::kNo, SkMatrix::I(), dstRectUV,
895 dstRectUV);
896 if (!doSynchronousRead) {
897 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
898 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
899 if (!uTransfer.fTransferBuffer) {
900 callback(context, nullptr);
901 return;
902 }
903 }
904
905 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
906 float vM[20];
907 std::fill_n(vM, 15, 0.f);
908 std::copy_n(baseM + 10, 5, vM + 15);
909 GrPaint vPaint;
910 auto vTexFP = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType(),
Brian Salomona3b02f52020-07-15 16:02:01 -0400911 texMatrix, GrSamplerState::Filter::kLinear);
Brian Salomon63a0a752020-06-26 13:32:09 -0400912 auto vColFP = GrColorMatrixFragmentProcessor::Make(std::move(vTexFP), vM,
913 /*unpremulInput=*/false,
914 /*clampRGBOutput=*/true,
915 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400916 vPaint.setColorFragmentProcessor(std::move(vColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400917 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
918 vRTC->fillRectToRect(nullptr, std::move(vPaint), GrAA::kNo, SkMatrix::I(), dstRectUV,
919 dstRectUV);
920 if (!doSynchronousRead) {
921 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
922 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
923 if (!vTransfer.fTransferBuffer) {
924 callback(context, nullptr);
925 return;
926 }
927 }
928
929 if (doSynchronousRead) {
930 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
931 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
932 size_t yRB = yInfo.minRowBytes();
933 size_t uvRB = uvInfo.minRowBytes();
934 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
935 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
936 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
937 if (!yRTC->readPixels(yInfo, y.get(), yRB, {0, 0}, direct) ||
938 !uRTC->readPixels(uvInfo, u.get(), uvRB, {0, 0}, direct) ||
939 !vRTC->readPixels(uvInfo, v.get(), uvRB, {0, 0}, direct)) {
940 callback(context, nullptr);
941 return;
942 }
943 auto result = std::make_unique<AsyncReadResult>(direct->priv().contextID());
944 result->addCpuPlane(std::move(y), yRB );
945 result->addCpuPlane(std::move(u), uvRB);
946 result->addCpuPlane(std::move(v), uvRB);
947 callback(context, std::move(result));
948 return;
949 }
950
951 struct FinishContext {
952 ReadPixelsCallback* fClientCallback;
953 ReadPixelsContext fClientContext;
954 GrClientMappedBufferManager* fMappedBufferManager;
955 SkISize fSize;
956 PixelTransferResult fYTransfer;
957 PixelTransferResult fUTransfer;
958 PixelTransferResult fVTransfer;
959 };
960 // Assumption is that the caller would like to flush. We could take a parameter or require an
961 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
962 // callback to GrGpu until after the next flush that flushes our op list, though.
963 auto* finishContext = new FinishContext{callback,
964 context,
965 direct->priv().clientMappedBufferManager(),
966 dstSize,
967 std::move(yTransfer),
968 std::move(uTransfer),
969 std::move(vTransfer)};
970 auto finishCallback = [](GrGpuFinishedContext c) {
971 const auto* context = reinterpret_cast<const FinishContext*>(c);
972 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
973 auto manager = context->fMappedBufferManager;
974 size_t rowBytes = SkToSizeT(context->fSize.width());
975 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
976 (*context->fClientCallback)(context->fClientContext, nullptr);
977 delete context;
978 return;
979 }
980 rowBytes /= 2;
981 SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
982 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
983 (*context->fClientCallback)(context->fClientContext, nullptr);
984 delete context;
985 return;
986 }
987 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
988 (*context->fClientCallback)(context->fClientContext, nullptr);
989 delete context;
990 return;
991 }
992 (*context->fClientCallback)(context->fClientContext, std::move(result));
993 delete context;
994 };
995 GrFlushInfo flushInfo;
996 flushInfo.fFinishedContext = finishContext;
997 flushInfo.fFinishedProc = finishCallback;
998 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo, nullptr);
999}
1000
Brian Salomonc5243782020-04-02 12:50:34 -04001001bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001002 ASSERT_SINGLE_OWNER
1003 RETURN_FALSE_IF_ABANDONED
1004 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -04001005 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -04001006
Brian Salomon947efe22019-07-16 15:36:11 -04001007 const GrCaps* caps = fContext->priv().caps();
1008
Greg Daniel46cfbc62019-06-07 11:43:30 -04001009 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -05001010 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -04001011
Stephen White3c0a50f2020-01-16 18:19:54 -05001012 if (this->asSurfaceProxy()->framebufferOnly()) {
1013 return false;
1014 }
1015
Chris Daltonf8e5aad2019-08-02 12:55:23 -06001016 if (!caps->canCopySurface(this->asSurfaceProxy(), src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -04001017 return false;
1018 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001019
Greg Daniel16f5c652019-10-29 11:26:01 -04001020 // The swizzle doesn't matter for copies and it is not used.
1021 return this->drawingManager()->newCopyRenderTask(
Brian Salomonc5243782020-04-02 12:50:34 -04001022 GrSurfaceProxyView(sk_ref_sp(src), this->origin(), GrSwizzle("rgba")), srcRect,
Greg Daniel46e366a2019-12-16 14:38:36 -05001023 this->readSurfaceView(), dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001024}
Greg Daniel46cfbc62019-06-07 11:43:30 -04001025
Brian Salomon63a0a752020-06-26 13:32:09 -04001026std::unique_ptr<GrRenderTargetContext> GrSurfaceContext::rescale(const GrImageInfo& info,
1027 GrSurfaceOrigin origin,
1028 SkIRect srcRect,
1029 RescaleGamma rescaleGamma,
1030 SkFilterQuality rescaleQuality) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001031 auto rtProxy = this->asRenderTargetProxy();
1032 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1033 return nullptr;
1034 }
1035
Stephen White3c0a50f2020-01-16 18:19:54 -05001036 if (this->asSurfaceProxy()->framebufferOnly()) {
1037 return nullptr;
1038 }
1039
Brian Salomone9ad9982019-07-22 16:17:41 -04001040 // We rescale by drawing and don't currently support drawing to a kUnpremul destination.
1041 if (info.alphaType() == kUnpremul_SkAlphaType) {
1042 return nullptr;
1043 }
1044
Greg Daniel40903af2020-01-30 14:55:05 -05001045 GrSurfaceProxyView texView = this->readSurfaceView();
Brian Salomonfc118442019-11-22 19:09:27 -05001046 SkAlphaType srcAlphaType = this->colorInfo().alphaType();
Greg Daniel40903af2020-01-30 14:55:05 -05001047 if (!texView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -04001048 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect,
Brian Salomonc5243782020-04-02 12:50:34 -04001049 SkBackingFit::kApprox, SkBudgeted::kNo);
1050 if (!texView) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001051 return nullptr;
1052 }
Greg Daniel40903af2020-01-30 14:55:05 -05001053 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001054 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001055 }
1056
Brian Salomonbf6b9792019-08-21 09:38:10 -04001057 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
1058 // pass B is moved to A. If 'this' is the input on the first pass then tempA is null.
1059 std::unique_ptr<GrRenderTargetContext> tempA;
1060 std::unique_ptr<GrRenderTargetContext> tempB;
1061
Brian Salomone9ad9982019-07-22 16:17:41 -04001062 // Assume we should ignore the rescale linear request if the surface has no color space since
1063 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon63a0a752020-06-26 13:32:09 -04001064 if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() &&
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001065 !this->colorInfo().colorSpace()->gammaIsLinear()) {
1066 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomonfc118442019-11-22 19:09:27 -05001067 auto xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(), srcAlphaType, cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -04001068 kPremul_SkAlphaType);
1069 // We'll fall back to kRGBA_8888 if half float not supported.
Greg Daniele20fcad2020-01-08 11:52:34 -05001070 auto linearRTC = GrRenderTargetContext::MakeWithFallback(
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001071 fContext, GrColorType::kRGBA_F16, cs, SkBackingFit::kApprox, srcRect.size(), 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001072 GrMipmapped::kNo, GrProtected::kNo, origin);
Brian Salomone9ad9982019-07-22 16:17:41 -04001073 if (!linearRTC) {
1074 return nullptr;
1075 }
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001076 // 1-to-1 draw can always be kFast.
Brian Salomone69b9ef2020-07-22 11:18:06 -04001077 linearRTC->drawTexture(nullptr,
1078 std::move(texView),
1079 srcAlphaType,
1080 GrSamplerState::Filter::kNearest,
1081 GrSamplerState::MipmapMode::kNone,
1082 SkBlendMode::kSrc,
1083 SK_PMColor4fWHITE,
1084 SkRect::Make(srcRect),
1085 SkRect::Make(srcRect.size()),
1086 GrAA::kNo,
1087 GrQuadAAFlags::kNone,
1088 SkCanvas::kFast_SrcRectConstraint,
1089 SkMatrix::I(),
1090 std::move(xform));
Greg Daniel40903af2020-01-30 14:55:05 -05001091 texView = linearRTC->readSurfaceView();
1092 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -04001093 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001094 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001095 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001096
Brian Salomon9c6f6bf2020-05-29 16:37:26 -04001097 while (srcRect.size() != info.dimensions()) {
Brian Salomon59f31b12020-06-04 17:27:15 -04001098 SkISize nextDims = info.dimensions();
1099 if (rescaleQuality != kNone_SkFilterQuality) {
1100 if (srcRect.width() > info.width()) {
1101 nextDims.fWidth = std::max((srcRect.width() + 1)/2, info.width());
1102 } else if (srcRect.width() < info.width()) {
1103 nextDims.fWidth = std::min(srcRect.width()*2, info.width());
1104 }
1105 if (srcRect.height() > info.height()) {
1106 nextDims.fHeight = std::max((srcRect.height() + 1)/2, info.height());
1107 } else if (srcRect.height() < info.height()) {
1108 nextDims.fHeight = std::min(srcRect.height()*2, info.height());
1109 }
1110 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001111 auto input = tempA ? tempA.get() : this;
Brian Salomon59f31b12020-06-04 17:27:15 -04001112 GrColorType colorType = input->colorInfo().colorType();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001113 auto cs = input->colorInfo().refColorSpace();
Brian Salomone9ad9982019-07-22 16:17:41 -04001114 sk_sp<GrColorSpaceXform> xform;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001115 auto prevAlphaType = input->colorInfo().alphaType();
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001116 if (nextDims == info.dimensions()) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001117 // Might as well fold conversion to final info in the last step.
1118 cs = info.refColorSpace();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001119 xform = GrColorSpaceXform::Make(input->colorInfo().colorSpace(),
1120 input->colorInfo().alphaType(), cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -04001121 info.alphaType());
1122 }
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001123 tempB = GrRenderTargetContext::MakeWithFallback(fContext, colorType, std::move(cs),
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001124 SkBackingFit::kApprox, nextDims, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001125 GrMipmapped::kNo, GrProtected::kNo, origin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001126 if (!tempB) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001127 return nullptr;
1128 }
Brian Salomon59f31b12020-06-04 17:27:15 -04001129 auto dstRect = SkRect::Make(nextDims);
1130 if (rescaleQuality == kHigh_SkFilterQuality) {
1131 SkMatrix matrix;
1132 matrix.setScaleTranslate((float)srcRect.width()/nextDims.width(),
1133 (float)srcRect.height()/nextDims.height(),
1134 srcRect.x(),
1135 srcRect.y());
1136 std::unique_ptr<GrFragmentProcessor> fp;
1137 auto dir = GrBicubicEffect::Direction::kXY;
1138 if (nextDims.width() == srcRect.width()) {
1139 dir = GrBicubicEffect::Direction::kY;
1140 } else if (nextDims.height() == srcRect.height()) {
1141 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -04001142 }
Brian Salomon1af72d12020-06-25 10:47:26 -04001143 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
1144 static constexpr auto kKernel = GrBicubicEffect::Kernel::kCatmullRom;
Brian Salomon59f31b12020-06-04 17:27:15 -04001145 fp = GrBicubicEffect::MakeSubset(std::move(texView), prevAlphaType, matrix, kWM, kWM,
Brian Salomon1af72d12020-06-25 10:47:26 -04001146 SkRect::Make(srcRect), kKernel, dir, *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -04001147 if (xform) {
1148 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -04001149 }
Brian Salomon59f31b12020-06-04 17:27:15 -04001150 GrPaint paint;
John Stiles5933d7d2020-07-21 12:28:35 -04001151 paint.setColorFragmentProcessor(std::move(fp));
Brian Salomon59f31b12020-06-04 17:27:15 -04001152 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
1153 tempB->fillRectToRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect,
1154 dstRect);
1155 } else {
1156 auto filter = rescaleQuality == kNone_SkFilterQuality ? GrSamplerState::Filter::kNearest
Brian Salomona3b02f52020-07-15 16:02:01 -04001157 : GrSamplerState::Filter::kLinear;
Brian Salomon59f31b12020-06-04 17:27:15 -04001158 // Minimizing draw with integer coord src and dev rects can always be kFast.
1159 auto constraint = SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint;
1160 if (nextDims.width() <= srcRect.width() && nextDims.height() <= srcRect.height()) {
1161 constraint = SkCanvas::SrcRectConstraint::kFast_SrcRectConstraint;
1162 }
Brian Salomone69b9ef2020-07-22 11:18:06 -04001163 tempB->drawTexture(nullptr,
1164 std::move(texView),
1165 srcAlphaType,
1166 filter,
1167 GrSamplerState::MipmapMode::kNone,
1168 SkBlendMode::kSrc,
1169 SK_PMColor4fWHITE,
1170 SkRect::Make(srcRect),
1171 dstRect,
1172 GrAA::kNo,
1173 GrQuadAAFlags::kNone,
1174 constraint,
1175 SkMatrix::I(),
1176 std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -04001177 }
Greg Daniel40903af2020-01-30 14:55:05 -05001178 texView = tempB->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -04001179 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001180 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomone9ad9982019-07-22 16:17:41 -04001181 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001182 SkASSERT(tempA);
1183 return tempA;
Brian Salomone9ad9982019-07-22 16:17:41 -04001184}
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001185
Brian Salomon63a0a752020-06-26 13:32:09 -04001186GrSemaphoresSubmitted GrSurfaceContext::flush(SkSurface::BackendSurfaceAccess access,
1187 const GrFlushInfo& info,
1188 const GrBackendSurfaceMutableState* newState) {
1189 ASSERT_SINGLE_OWNER
Robert Phillips9eb00022020-06-30 15:30:12 -04001190 if (fContext->abandoned()) {
Brian Salomon63a0a752020-06-26 13:32:09 -04001191 if (info.fSubmittedProc) {
1192 info.fSubmittedProc(info.fSubmittedContext, false);
1193 }
1194 if (info.fFinishedProc) {
1195 info.fFinishedProc(info.fFinishedContext);
1196 }
1197 return GrSemaphoresSubmitted::kNo;
1198 }
1199 SkDEBUGCODE(this->validate();)
1200 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
1201
1202 return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info, newState);
1203}
1204
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001205GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
1206 const SkIRect& rect) {
1207 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1208 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001209 auto direct = fContext->asDirectContext();
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001210 if (!direct) {
1211 return {};
1212 }
1213 auto rtProxy = this->asRenderTargetProxy();
1214 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1215 return {};
1216 }
1217
1218 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001219 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
1220 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001221 // Fail if read color type does not have all of dstCT's color channels and those missing color
1222 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001223 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1224 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
1225 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1226 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001227 return {};
1228 }
1229
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001230 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001231 !supportedRead.fOffsetAlignmentForTransferBuffer) {
1232 return {};
1233 }
1234
1235 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
1236 size_t size = rowBytes * rect.height();
1237 auto buffer = direct->priv().resourceProvider()->createBuffer(
1238 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
1239 if (!buffer) {
1240 return {};
1241 }
1242 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -05001243 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001244 if (flip) {
1245 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1246 this->height() - rect.fTop);
1247 }
Greg Danielbbfec9d2019-08-20 10:56:51 -04001248 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001249 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -04001250 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001251 PixelTransferResult result;
1252 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001253 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -04001254 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001255 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
1256 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001257 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
1258 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001259 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
1260 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -04001261 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001262 };
1263 }
1264 return result;
1265}
Greg Daniel46e366a2019-12-16 14:38:36 -05001266
1267#ifdef SK_DEBUG
1268void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001269 SkASSERT(fReadView.proxy());
1270 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -04001271 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
1272 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
1273 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
1274 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001275 this->onValidate();
1276}
1277#endif