blob: 199493c7dfedffb5341849ccccb29ec9e2857459 [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"
Greg Daniel6eb8c242019-06-05 10:22:24 -040024#include "src/gpu/GrSurfacePriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/SkGr.h"
Brian Salomone9ad9982019-07-22 16:17:41 -040026#include "src/gpu/effects/GrBicubicEffect.h"
Brian Salomon63a0a752020-06-26 13:32:09 -040027#include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h"
Brian Osman45580d32016-11-23 09:37:01 -050028
Adlai Holler33dbd652020-06-01 12:35:42 -040029#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
Robert Phillips9eb00022020-06-30 15:30:12 -040030#define RETURN_FALSE_IF_ABANDONED if (this->fContext->abandoned()) { return false; }
Brian Osman45580d32016-11-23 09:37:01 -050031
Greg Danielbfa19c42019-12-19 16:41:40 -050032std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -050033 GrSurfaceProxyView readView,
Greg Danielbfa19c42019-12-19 16:41:40 -050034 GrColorType colorType,
35 SkAlphaType alphaType,
36 sk_sp<SkColorSpace> colorSpace) {
Greg Daniele20fcad2020-01-08 11:52:34 -050037 // It is probably not necessary to check if the context is abandoned here since uses of the
38 // GrSurfaceContext which need the context will mostly likely fail later on without an issue.
39 // However having this hear adds some reassurance in case there is a path doesn't handle an
40 // abandoned context correctly. It also lets us early out of some extra work.
Robert Phillips9eb00022020-06-30 15:30:12 -040041 if (context->abandoned()) {
Greg Daniele20fcad2020-01-08 11:52:34 -050042 return nullptr;
43 }
Greg Daniel3912a4b2020-01-14 09:56:04 -050044 GrSurfaceProxy* proxy = readView.proxy();
Greg Danielbfa19c42019-12-19 16:41:40 -050045 SkASSERT(proxy && proxy->asTextureProxy());
46
Greg Danielbfa19c42019-12-19 16:41:40 -050047 std::unique_ptr<GrSurfaceContext> surfaceContext;
Greg Daniel3912a4b2020-01-14 09:56:04 -050048 if (proxy->asRenderTargetProxy()) {
Greg Danielbfa19c42019-12-19 16:41:40 -050049 SkASSERT(kPremul_SkAlphaType == alphaType || kOpaque_SkAlphaType == alphaType);
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;
53 if (colorType != GrColorType::kUnknown) {
54 writeSwizzle =
55 context->priv().caps()->getWriteSwizzle(proxy->backendFormat(), colorType);
56 }
Brian Salomon8afde5f2020-04-01 16:22:00 -040057 GrSurfaceProxyView writeView(readView.refProxy(), readView.origin(), writeSwizzle);
Greg Daniel3912a4b2020-01-14 09:56:04 -050058 surfaceContext.reset(new GrRenderTargetContext(context, std::move(readView),
Brian Salomon8afde5f2020-04-01 16:22:00 -040059 std::move(writeView), colorType,
Greg Danielbfa19c42019-12-19 16:41:40 -050060 std::move(colorSpace), nullptr));
61 } else {
Greg Daniel3912a4b2020-01-14 09:56:04 -050062 surfaceContext.reset(new GrSurfaceContext(context, std::move(readView), colorType,
63 alphaType, std::move(colorSpace)));
Greg Danielbfa19c42019-12-19 16:41:40 -050064 }
Robert Phillips07f0e412020-01-17 15:20:00 -050065 SkDEBUGCODE(surfaceContext->validate();)
Greg Danielbfa19c42019-12-19 16:41:40 -050066 return surfaceContext;
67}
68
Brian Salomona56a7462020-02-07 14:17:25 -050069std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
70 SkISize dimensions,
71 const GrBackendFormat& format,
72 GrRenderable renderable,
73 int renderTargetSampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -040074 GrMipmapped mipMapped,
Brian Salomona56a7462020-02-07 14:17:25 -050075 GrProtected isProtected,
76 GrSurfaceOrigin origin,
77 GrColorType colorType,
78 SkAlphaType alphaType,
79 sk_sp<SkColorSpace> colorSpace,
80 SkBackingFit fit,
81 SkBudgeted budgeted) {
Brian Salomonc5243782020-04-02 12:50:34 -040082 GrSwizzle swizzle;
83 if (colorType != GrColorType::kUnknown && !context->priv().caps()->isFormatCompressed(format)) {
Brian Salomond005b692020-04-01 15:47:05 -040084 swizzle = context->priv().caps()->getReadSwizzle(format, colorType);
85 }
Greg Daniel47c20e82020-01-21 14:29:57 -050086
Greg Danielbfa19c42019-12-19 16:41:40 -050087 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -040088 format, dimensions, renderable, renderTargetSampleCnt, mipMapped, fit, budgeted,
89 isProtected);
Greg Danielbfa19c42019-12-19 16:41:40 -050090 if (!proxy) {
91 return nullptr;
92 }
93
Greg Daniel3912a4b2020-01-14 09:56:04 -050094 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
95 return GrSurfaceContext::Make(context, std::move(view), colorType, alphaType,
Greg Danielbfa19c42019-12-19 16:41:40 -050096 std::move(colorSpace));
97}
98
Greg Danielf41b2bd2019-08-22 16:19:24 -040099// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
100// GrOpsTasks to be picked up and added to by renderTargetContexts lower in the call
101// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
102// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500103GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500104 GrSurfaceProxyView readView,
Brian Salomond6287472019-06-24 15:50:07 -0400105 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -0400106 SkAlphaType alphaType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500107 sk_sp<SkColorSpace> colorSpace)
Greg Daniel901b98e2019-10-22 09:54:02 -0400108 : fContext(context)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500109 , fReadView(std::move(readView))
110 , fColorInfo(colorType, alphaType, std::move(colorSpace)) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400111 SkASSERT(!context->abandoned());
Greg Daniele20fcad2020-01-08 11:52:34 -0500112}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400113
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400114const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); }
115
Robert Phillips0d075de2019-03-04 11:08:13 -0500116GrAuditTrail* GrSurfaceContext::auditTrail() {
117 return fContext->priv().auditTrail();
118}
119
120GrDrawingManager* GrSurfaceContext::drawingManager() {
121 return fContext->priv().drawingManager();
122}
123
124const GrDrawingManager* GrSurfaceContext::drawingManager() const {
125 return fContext->priv().drawingManager();
126}
127
128#ifdef SK_DEBUG
129GrSingleOwner* GrSurfaceContext::singleOwner() {
130 return fContext->priv().singleOwner();
131}
132#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400133
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400134bool GrSurfaceContext::readPixels(const GrImageInfo& origDstInfo, void* dst, size_t rowBytes,
Robert Phillips44333c52020-06-30 13:28:00 -0400135 SkIPoint pt, GrDirectContext* direct) {
Brian Salomon1d435302019-07-01 13:05:28 -0400136 ASSERT_SINGLE_OWNER
137 RETURN_FALSE_IF_ABANDONED
138 SkDEBUGCODE(this->validate();)
139 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Greg Daniel6eb8c242019-06-05 10:22:24 -0400140
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400141 if (!direct && !(direct = fContext->asDirectContext())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400142 return false;
143 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400144
Brian Salomon1d435302019-07-01 13:05:28 -0400145 if (!dst) {
146 return false;
147 }
148
Brian Salomon1047a492019-07-02 12:25:21 -0400149 size_t tightRowBytes = origDstInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400150 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400151 rowBytes = tightRowBytes;
152 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400153 return false;
154 }
155
156 if (!origDstInfo.isValid()) {
157 return false;
158 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400159
160 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
161
Stephen White3c0a50f2020-01-16 18:19:54 -0500162 if (srcProxy->framebufferOnly()) {
163 return false;
164 }
165
Greg Daniel6eb8c242019-06-05 10:22:24 -0400166 // MDB TODO: delay this instantiation until later in the method
167 if (!srcProxy->instantiate(direct->priv().resourceProvider())) {
168 return false;
169 }
170
171 GrSurface* srcSurface = srcProxy->peekSurface();
172
Brian Salomon1d435302019-07-01 13:05:28 -0400173 auto dstInfo = origDstInfo;
174 if (!dstInfo.clip(this->width(), this->height(), &pt, &dst, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400175 return false;
176 }
Brian Salomon1047a492019-07-02 12:25:21 -0400177 // Our tight row bytes may have been changed by clipping.
178 tightRowBytes = dstInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400179
Mike Klein7321e6a2019-12-03 11:08:40 -0600180 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{this->colorInfo(), dstInfo}.flags;
181 bool unpremul = flags.unpremul,
182 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
183 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400184
185 const GrCaps* caps = direct->priv().caps();
Robert Phillips07f0e412020-01-17 15:20:00 -0500186 bool srcIsCompressed = caps->isFormatCompressed(srcSurface->backendFormat());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400187 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
188 // care so much about getImageData performance. However, in order to ensure putImageData/
189 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
190 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
191 // fContext->vaildaPMUPMConversionExists()).
Greg Daniel0258c902019-08-01 13:08:33 -0400192 GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
193 GrRenderable::kYes);
Greg Danielc71c7962020-01-14 16:44:18 -0500194 GrColorType srcColorType = this->colorInfo().colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400195 bool canvas2DFastPath = unpremul && !needColorConversion &&
196 (GrColorType::kRGBA_8888 == dstInfo.colorType() ||
197 GrColorType::kBGRA_8888 == dstInfo.colorType()) &&
198 SkToBool(srcProxy->asTextureProxy()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500199 (srcColorType == GrColorType::kRGBA_8888 ||
200 srcColorType == GrColorType::kBGRA_8888) &&
Greg Daniel0258c902019-08-01 13:08:33 -0400201 defaultRGBAFormat.isValid() &&
Brian Salomon1d435302019-07-01 13:05:28 -0400202 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400203
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400204 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400205 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400206 return false;
207 }
208
Brian Salomondc0710f2019-07-01 14:59:32 -0400209 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Robert Phillips07f0e412020-01-17 15:20:00 -0500210 GrColorType colorType = (canvas2DFastPath || srcIsCompressed)
211 ? GrColorType::kRGBA_8888 : this->colorInfo().colorType();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400212 sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorInfo().refColorSpace();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400213
Greg Daniele20fcad2020-01-08 11:52:34 -0500214 auto tempCtx = GrRenderTargetContext::Make(
215 direct, colorType, std::move(cs), SkBackingFit::kApprox, dstInfo.dimensions(),
Brian Salomon7e67dca2020-07-21 09:27:25 -0400216 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400217 if (!tempCtx) {
218 return false;
219 }
220
221 std::unique_ptr<GrFragmentProcessor> fp;
222 if (canvas2DFastPath) {
Greg Danield2ccbb52020-02-05 10:45:39 -0500223 fp = direct->priv().createPMToUPMEffect(
224 GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType()));
Brian Salomon1d435302019-07-01 13:05:28 -0400225 if (dstInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400226 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
Brian Salomon1d435302019-07-01 13:05:28 -0400227 dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400228 }
Brian Salomon1d435302019-07-01 13:05:28 -0400229 // The render target context is incorrectly tagged as kPremul even though we're writing
230 // unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type so we don't
231 // double unpremul.
232 dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400233 } else {
Greg Danield2ccbb52020-02-05 10:45:39 -0500234 fp = GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400235 }
236 if (!fp) {
237 return false;
238 }
239 GrPaint paint;
240 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
John Stiles5933d7d2020-07-21 12:28:35 -0400241 paint.setColorFragmentProcessor(std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400242
243 tempCtx->asRenderTargetContext()->fillRectToRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400244 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400245 SkRect::MakeWH(dstInfo.width(), dstInfo.height()),
246 SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400247
Brian Salomon1d435302019-07-01 13:05:28 -0400248 return tempCtx->readPixels(dstInfo, dst, rowBytes, {0, 0}, direct);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400249 }
250
Greg Danielb8d84f82020-02-13 14:25:00 -0500251 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400252
Brian Salomon1d435302019-07-01 13:05:28 -0400253 auto supportedRead = caps->supportedReadPixelsColorType(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400254 this->colorInfo().colorType(), srcProxy->backendFormat(), dstInfo.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400255
Brian Salomon1047a492019-07-02 12:25:21 -0400256 bool makeTight = !caps->readPixelsRowBytesSupport() && tightRowBytes != rowBytes;
257
258 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomon8f8354a2019-07-31 20:12:02 -0400259 (dstInfo.colorType() != supportedRead.fColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400260
Brian Salomonf30b1c12019-06-20 12:25:02 -0400261 std::unique_ptr<char[]> tmpPixels;
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400262 GrImageInfo tmpInfo;
Brian Salomon1d435302019-07-01 13:05:28 -0400263 void* readDst = dst;
264 size_t readRB = rowBytes;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400265 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400266 tmpInfo = {supportedRead.fColorType, this->colorInfo().alphaType(),
267 this->colorInfo().refColorSpace(), dstInfo.width(), dstInfo.height()};
Brian Salomon1d435302019-07-01 13:05:28 -0400268 size_t tmpRB = tmpInfo.minRowBytes();
269 size_t size = tmpRB * tmpInfo.height();
270 // Chrome MSAN bots require the data to be initialized (hence the ()).
271 tmpPixels.reset(new char[size]());
Brian Salomonf30b1c12019-06-20 12:25:02 -0400272
Brian Salomonf30b1c12019-06-20 12:25:02 -0400273 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400274 readRB = tmpRB;
275 pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400276 }
277
Greg Daniel55f040b2020-02-13 15:38:32 +0000278 direct->priv().flushSurface(srcProxy);
Greg Daniel04283f32020-05-20 13:16:00 -0400279 direct->submit();
Brian Salomon1d435302019-07-01 13:05:28 -0400280 if (!direct->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400281 dstInfo.height(), this->colorInfo().colorType(),
Brian Salomonf77c1462019-08-01 15:19:29 -0400282 supportedRead.fColorType, readDst, readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400283 return false;
284 }
285
Greg Daniel6eb8c242019-06-05 10:22:24 -0400286 if (convert) {
Brian Salomon8f8354a2019-07-31 20:12:02 -0400287 return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400288 }
289 return true;
290}
Robert Phillips0d075de2019-03-04 11:08:13 -0500291
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400292bool GrSurfaceContext::writePixels(const GrImageInfo& origSrcInfo, const void* src, size_t rowBytes,
Robert Phillips44333c52020-06-30 13:28:00 -0400293 SkIPoint pt, GrDirectContext* direct) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400294 ASSERT_SINGLE_OWNER
295 RETURN_FALSE_IF_ABANDONED
296 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400297 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400298
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400299 if (!direct && !(direct = fContext->asDirectContext())) {
Brian Salomonc320b152018-02-20 14:05:36 -0500300 return false;
301 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500302
Brian Salomon1d435302019-07-01 13:05:28 -0400303 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500304 return false;
305 }
306
Brian Salomon1d435302019-07-01 13:05:28 -0400307 if (!src) {
308 return false;
309 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400310
Brian Salomon1047a492019-07-02 12:25:21 -0400311 size_t tightRowBytes = origSrcInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400312 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400313 rowBytes = tightRowBytes;
314 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400315 return false;
316 }
317
318 if (!origSrcInfo.isValid()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400319 return false;
320 }
321
322 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
Stephen White3c0a50f2020-01-16 18:19:54 -0500323
324 if (dstProxy->framebufferOnly()) {
325 return false;
326 }
327
Greg Daniel6eb8c242019-06-05 10:22:24 -0400328 if (!dstProxy->instantiate(direct->priv().resourceProvider())) {
329 return false;
330 }
331
332 GrSurface* dstSurface = dstProxy->peekSurface();
333
Brian Salomon1d435302019-07-01 13:05:28 -0400334 auto srcInfo = origSrcInfo;
335 if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400336 return false;
337 }
Brian Salomon1047a492019-07-02 12:25:21 -0400338 // Our tight row bytes may have been changed by clipping.
339 tightRowBytes = srcInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400340
Mike Klein7321e6a2019-12-03 11:08:40 -0600341 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{srcInfo, this->colorInfo()}.flags;
342 bool unpremul = flags.unpremul,
343 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
344 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400345
346 const GrCaps* caps = direct->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400347
348 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
349 GrRenderable::kNo);
350
Greg Danielc71c7962020-01-14 16:44:18 -0500351 GrColorType dstColorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400352 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
353 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400354 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
355 (srcInfo.colorType() == GrColorType::kRGBA_8888 ||
356 srcInfo.colorType() == GrColorType::kBGRA_8888) &&
357 SkToBool(this->asRenderTargetContext()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500358 (dstColorType == GrColorType::kRGBA_8888 ||
359 dstColorType == GrColorType::kBGRA_8888) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400360 rgbaDefaultFormat.isValid() &&
Brian Salomon1d435302019-07-01 13:05:28 -0400361 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400362
363 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
Brian Salomond6287472019-06-24 15:50:07 -0400364 GrColorType colorType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400365
366 GrBackendFormat format;
Brian Salomone7499c72019-06-24 12:12:36 -0400367 SkAlphaType alphaType;
Greg Danielbfa19c42019-12-19 16:41:40 -0500368 GrSwizzle tempReadSwizzle;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400369 if (canvas2DFastPath) {
Brian Salomond6287472019-06-24 15:50:07 -0400370 colorType = GrColorType::kRGBA_8888;
Greg Daniel7bfc9132019-08-14 14:23:53 -0400371 format = rgbaDefaultFormat;
Brian Salomone7499c72019-06-24 12:12:36 -0400372 alphaType = kUnpremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400373 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400374 colorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400375 format = dstProxy->backendFormat().makeTexture2D();
376 if (!format.isValid()) {
377 return false;
378 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400379 alphaType = this->colorInfo().alphaType();
Greg Danielbfa19c42019-12-19 16:41:40 -0500380 tempReadSwizzle = this->readSwizzle();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400381 }
382
Greg Daniel2e52ad12019-06-13 10:04:16 -0400383 // It is more efficient for us to write pixels into a top left origin so we prefer that.
384 // However, if the final proxy isn't a render target then we must use a copy to move the
385 // data into it which requires the origins to match. If the final proxy is a render target
386 // we can use a draw instead which doesn't have this origin restriction. Thus for render
387 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400388 GrSurfaceOrigin tempOrigin =
Greg Danielb8d84f82020-02-13 14:25:00 -0500389 this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : this->origin();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400390 auto tempProxy = direct->priv().proxyProvider()->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400391 format, srcInfo.dimensions(), GrRenderable::kNo, 1, GrMipmapped::kNo,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400392 SkBackingFit::kApprox, SkBudgeted::kYes, GrProtected::kNo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400393 if (!tempProxy) {
394 return false;
395 }
Greg Daniel3912a4b2020-01-14 09:56:04 -0500396 GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle);
Greg Danield2ccbb52020-02-05 10:45:39 -0500397 GrSurfaceContext tempCtx(direct, tempView, colorType, alphaType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500398 this->colorInfo().refColorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400399
400 // In the fast path we always write the srcData to the temp context as though it were RGBA.
401 // When the data is really BGRA the write will cause the R and B channels to be swapped in
402 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
403 // dst.
Brian Salomon1d435302019-07-01 13:05:28 -0400404 if (canvas2DFastPath) {
405 srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888);
406 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500407 if (!tempCtx.writePixels(srcInfo, src, rowBytes, {0, 0}, direct)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400408 return false;
409 }
410
411 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400412 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400413 if (canvas2DFastPath) {
Brian Salomonb8f098d2020-01-07 11:15:44 -0500414 fp = direct->priv().createUPMToPMEffect(
Greg Danield2ccbb52020-02-05 10:45:39 -0500415 GrTextureEffect::Make(std::move(tempView), alphaType));
Brian Salomon1d435302019-07-01 13:05:28 -0400416 // Important: check the original src color type here!
417 if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400418 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
419 }
420 } else {
Greg Danield2ccbb52020-02-05 10:45:39 -0500421 fp = GrTextureEffect::Make(std::move(tempView), alphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400422 }
423 if (!fp) {
424 return false;
425 }
426 GrPaint paint;
427 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
John Stiles5933d7d2020-07-21 12:28:35 -0400428 paint.setColorFragmentProcessor(std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400429 this->asRenderTargetContext()->fillRectToRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400430 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400431 SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()),
432 SkRect::MakeWH(srcInfo.width(), srcInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400433 } else {
Brian Salomon1d435302019-07-01 13:05:28 -0400434 SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height());
435 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Brian Salomonc5243782020-04-02 12:50:34 -0400436 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400437 return false;
438 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400439 }
440 return true;
441 }
442
Brian Salomon1d435302019-07-01 13:05:28 -0400443 GrColorType allowedColorType =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400444 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400445 dstProxy->backendFormat(),
446 srcInfo.colorType()).fColorType;
Greg Danielb8d84f82020-02-13 14:25:00 -0500447 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon1047a492019-07-02 12:25:21 -0400448 bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes;
449 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomon1d435302019-07-01 13:05:28 -0400450 (srcInfo.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400451
Brian Salomonf30b1c12019-06-20 12:25:02 -0400452 std::unique_ptr<char[]> tmpPixels;
Brian Salomon1d435302019-07-01 13:05:28 -0400453 GrColorType srcColorType = srcInfo.colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400454 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400455 GrImageInfo tmpInfo(allowedColorType, this->colorInfo().alphaType(),
456 this->colorInfo().refColorSpace(), srcInfo.width(), srcInfo.height());
Brian Salomon1d435302019-07-01 13:05:28 -0400457 auto tmpRB = tmpInfo.minRowBytes();
458 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400459
Brian Salomon1d435302019-07-01 13:05:28 -0400460 GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400461
Brian Salomon1d435302019-07-01 13:05:28 -0400462 srcColorType = tmpInfo.colorType();
463 rowBytes = tmpRB;
464 src = tmpPixels.get();
465 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400466 }
467
468 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
469 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
470 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
471 // destination proxy)
472 // TODO: should this policy decision just be moved into the drawing manager?
Greg Daniel55f040b2020-02-13 15:38:32 +0000473 direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400474
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400475 return direct->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(),
476 srcInfo.height(), this->colorInfo().colorType(),
477 srcColorType, src, rowBytes);
Brian Osman45580d32016-11-23 09:37:01 -0500478}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400479
Brian Salomon63a0a752020-06-26 13:32:09 -0400480void GrSurfaceContext::asyncRescaleAndReadPixels(const SkImageInfo& info,
481 const SkIRect& srcRect,
482 RescaleGamma rescaleGamma,
483 SkFilterQuality rescaleQuality,
484 ReadPixelsCallback callback,
485 ReadPixelsContext context) {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400486 auto direct = fContext->asDirectContext();
Brian Salomon63a0a752020-06-26 13:32:09 -0400487
488 // We implement this by rendering and we don't currently support rendering kUnpremul.
489 if (info.alphaType() == kUnpremul_SkAlphaType) {
490 callback(context, nullptr);
491 return;
492 }
493 if (!direct) {
494 callback(context, nullptr);
495 return;
496 }
497 auto rt = this->asRenderTargetProxy();
498 if (rt && rt->wrapsVkSecondaryCB()) {
499 callback(context, nullptr);
500 return;
501 }
502 if (rt && rt->framebufferOnly()) {
503 callback(context, nullptr);
504 return;
505 }
506 auto dstCT = SkColorTypeToGrColorType(info.colorType());
507 if (dstCT == GrColorType::kUnknown) {
508 callback(context, nullptr);
509 return;
510 }
511 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
512 auto colorTypeOfFinalContext = this->colorInfo().colorType();
513 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
514 if (needsRescale) {
515 colorTypeOfFinalContext = dstCT;
516 backendFormatOfFinalContext =
517 this->caps()->getDefaultBackendFormat(dstCT, GrRenderable::kYes);
518 }
519 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
520 backendFormatOfFinalContext, dstCT);
521 // Fail if we can't read from the source surface's color type.
522 if (readInfo.fColorType == GrColorType::kUnknown) {
523 callback(context, nullptr);
524 return;
525 }
526 // Fail if read color type does not have all of dstCT's color channels and those missing color
527 // channels are in the src.
528 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
529 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
530 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
531 if ((~legalReadChannels & dstChannels) & srcChannels) {
532 callback(context, nullptr);
533 return;
534 }
535
536 std::unique_ptr<GrRenderTargetContext> tempRTC;
537 int x = srcRect.fLeft;
538 int y = srcRect.fTop;
539 if (needsRescale) {
540 tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
541 rescaleQuality);
542 if (!tempRTC) {
543 callback(context, nullptr);
544 return;
545 }
546 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
547 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
548 x = y = 0;
549 } else {
550 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
551 this->colorInfo().alphaType(),
552 info.colorSpace(),
553 info.alphaType());
554 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
555 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
556 GrSurfaceProxyView texProxyView = this->readSurfaceView();
557 SkRect srcRectToDraw = SkRect::Make(srcRect);
558 // If the src is not texturable first try to make a copy to a texture.
559 if (!texProxyView.asTextureProxy()) {
560 texProxyView =
Brian Salomon7e67dca2020-07-21 09:27:25 -0400561 GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipmapped::kNo, srcRect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400562 SkBackingFit::kApprox, SkBudgeted::kNo);
563 if (!texProxyView) {
564 callback(context, nullptr);
565 return;
566 }
567 SkASSERT(texProxyView.asTextureProxy());
568 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
569 }
570 tempRTC = GrRenderTargetContext::Make(direct, this->colorInfo().colorType(),
571 info.refColorSpace(), SkBackingFit::kApprox,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400572 srcRect.size(), 1, GrMipmapped::kNo,
Brian Salomon63a0a752020-06-26 13:32:09 -0400573 GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
574 if (!tempRTC) {
575 callback(context, nullptr);
576 return;
577 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400578 tempRTC->drawTexture(nullptr,
579 std::move(texProxyView),
580 this->colorInfo().alphaType(),
581 GrSamplerState::Filter::kNearest,
582 GrSamplerState::MipmapMode::kNone,
583 SkBlendMode::kSrc,
584 SK_PMColor4fWHITE,
585 srcRectToDraw,
586 SkRect::MakeWH(srcRect.width(), srcRect.height()),
587 GrAA::kNo,
588 GrQuadAAFlags::kNone,
589 SkCanvas::kFast_SrcRectConstraint,
590 SkMatrix::I(),
591 std::move(xform));
Brian Salomon63a0a752020-06-26 13:32:09 -0400592 x = y = 0;
593 }
594 }
595 auto rtc = tempRTC ? tempRTC.get() : this;
596 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
597 info.colorType(), callback, context);
598}
599
600class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult {
601public:
602 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
603 ~AsyncReadResult() override {
604 for (int i = 0; i < fPlanes.count(); ++i) {
605 if (!fPlanes[i].fMappedBuffer) {
606 delete[] static_cast<const char*>(fPlanes[i].fData);
607 } else {
608 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
609 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
610 }
611 }
612 }
613
614 int count() const override { return fPlanes.count(); }
615 const void* data(int i) const override { return fPlanes[i].fData; }
616 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
617
618 bool addTransferResult(const PixelTransferResult& result,
619 SkISize dimensions,
620 size_t rowBytes,
621 GrClientMappedBufferManager* manager) {
622 SkASSERT(!result.fTransferBuffer->isMapped());
623 const void* mappedData = result.fTransferBuffer->map();
624 if (!mappedData) {
625 return false;
626 }
627 if (result.fPixelConverter) {
628 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
629 result.fPixelConverter(convertedData.get(), mappedData);
630 this->addCpuPlane(std::move(convertedData), rowBytes);
631 result.fTransferBuffer->unmap();
632 } else {
633 manager->insert(result.fTransferBuffer);
634 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
635 }
636 return true;
637 }
638
639 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
640 SkASSERT(data);
641 SkASSERT(rowBytes > 0);
642 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
643 }
644
645private:
646 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
647 SkASSERT(data);
648 SkASSERT(rowBytes > 0);
649 SkASSERT(mappedBuffer);
650 SkASSERT(mappedBuffer->isMapped());
651 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
652 }
653
654 struct Plane {
655 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
656 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
657 const void* fData;
658 size_t fRowBytes;
659 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
660 sk_sp<GrGpuBuffer> fMappedBuffer;
661 };
662 SkSTArray<3, Plane> fPlanes;
663 uint32_t fInboxID;
664};
665
666void GrSurfaceContext::asyncReadPixels(const SkIRect& rect,
667 SkColorType colorType,
668 ReadPixelsCallback callback,
669 ReadPixelsContext context) {
670 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
671 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
672
673 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
674 callback(context, nullptr);
675 return;
676 }
677
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400678 auto directContext = fContext->asDirectContext();
Brian Salomon63a0a752020-06-26 13:32:09 -0400679 SkASSERT(directContext);
680 auto mappedBufferManager = directContext->priv().clientMappedBufferManager();
681
682 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
683
684 if (!transferResult.fTransferBuffer) {
685 auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
686 this->colorInfo().refColorSpace());
687 auto result = std::make_unique<AsyncReadResult>(0);
688 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
689 SkPixmap pm(ii, data.get(), ii.minRowBytes());
690 result->addCpuPlane(std::move(data), pm.rowBytes());
691
692 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
693 callback(context, nullptr);
694 return;
695 }
696 callback(context, std::move(result));
697 return;
698 }
699
700 struct FinishContext {
701 ReadPixelsCallback* fClientCallback;
702 ReadPixelsContext fClientContext;
703 SkISize fSize;
704 SkColorType fColorType;
705 GrClientMappedBufferManager* fMappedBufferManager;
706 PixelTransferResult fTransferResult;
707 };
708 // Assumption is that the caller would like to flush. We could take a parameter or require an
709 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
710 // callback to GrGpu until after the next flush that flushes our op list, though.
711 auto* finishContext = new FinishContext{callback,
712 context,
713 rect.size(),
714 colorType,
715 mappedBufferManager,
716 std::move(transferResult)};
717 auto finishCallback = [](GrGpuFinishedContext c) {
718 const auto* context = reinterpret_cast<const FinishContext*>(c);
719 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
720 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
721 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
722 context->fMappedBufferManager)) {
723 result.reset();
724 }
725 (*context->fClientCallback)(context->fClientContext, std::move(result));
726 delete context;
727 };
728 GrFlushInfo flushInfo;
729 flushInfo.fFinishedContext = finishContext;
730 flushInfo.fFinishedProc = finishCallback;
731 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo, nullptr);
732}
733
734void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
735 sk_sp<SkColorSpace> dstColorSpace,
736 const SkIRect& srcRect,
737 SkISize dstSize,
738 RescaleGamma rescaleGamma,
739 SkFilterQuality rescaleQuality,
740 ReadPixelsCallback callback,
741 ReadPixelsContext context) {
742 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
743 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
744 SkASSERT(!dstSize.isZero());
745 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
746
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400747 auto direct = fContext->asDirectContext();
Brian Salomon63a0a752020-06-26 13:32:09 -0400748 if (!direct) {
749 callback(context, nullptr);
750 return;
751 }
752 auto rt = this->asRenderTargetProxy();
753 if (rt && rt->wrapsVkSecondaryCB()) {
754 callback(context, nullptr);
755 return;
756 }
757 if (rt && rt->framebufferOnly()) {
758 callback(context, nullptr);
759 return;
760 }
761 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
762 callback(context, nullptr);
763 return;
764 }
765 int x = srcRect.fLeft;
766 int y = srcRect.fTop;
767 bool needsRescale = srcRect.size() != dstSize;
768 GrSurfaceProxyView srcView;
769 if (needsRescale) {
770 // We assume the caller wants kPremul. There is no way to indicate a preference.
771 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
772 dstColorSpace);
773 // TODO: Incorporate the YUV conversion into last pass of rescaling.
774 auto tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
775 rescaleQuality);
776 if (!tempRTC) {
777 callback(context, nullptr);
778 return;
779 }
780 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
781 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
782 x = y = 0;
783 srcView = tempRTC->readSurfaceView();
784 } else {
785 srcView = this->readSurfaceView();
786 if (!srcView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400787 srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipmapped::kNo,
Brian Salomon63a0a752020-06-26 13:32:09 -0400788 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
789 if (!srcView) {
790 // If we can't get a texture copy of the contents then give up.
791 callback(context, nullptr);
792 return;
793 }
794 SkASSERT(srcView.asTextureProxy());
795 x = y = 0;
796 }
797 // We assume the caller wants kPremul. There is no way to indicate a preference.
798 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
799 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
800 kPremul_SkAlphaType);
801 if (xform) {
802 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
803 auto tempRTC = GrRenderTargetContext::Make(
804 direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400805 dstSize, 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400806 if (!tempRTC) {
807 callback(context, nullptr);
808 return;
809 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400810 tempRTC->drawTexture(nullptr,
811 std::move(srcView),
812 this->colorInfo().alphaType(),
813 GrSamplerState::Filter::kNearest,
814 GrSamplerState::MipmapMode::kNone,
815 SkBlendMode::kSrc,
816 SK_PMColor4fWHITE,
817 srcRectToDraw,
818 SkRect::Make(srcRect.size()),
819 GrAA::kNo,
820 GrQuadAAFlags::kNone,
821 SkCanvas::kFast_SrcRectConstraint,
822 SkMatrix::I(),
823 std::move(xform));
Brian Salomon63a0a752020-06-26 13:32:09 -0400824 srcView = tempRTC->readSurfaceView();
825 SkASSERT(srcView.asTextureProxy());
826 x = y = 0;
827 }
828 }
829
830 auto yRTC = GrRenderTargetContext::MakeWithFallback(
831 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400832 GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400833 int halfW = dstSize.width() /2;
834 int halfH = dstSize.height()/2;
835 auto uRTC = GrRenderTargetContext::MakeWithFallback(
836 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400837 GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400838 auto vRTC = GrRenderTargetContext::MakeWithFallback(
839 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400840 GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400841 if (!yRTC || !uRTC || !vRTC) {
842 callback(context, nullptr);
843 return;
844 }
845
846 float baseM[20];
847 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
848
849 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
850
851 auto texMatrix = SkMatrix::Translate(x, y);
852
853 SkRect dstRectY = SkRect::Make(dstSize);
854 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
855
856 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
857 PixelTransferResult yTransfer, uTransfer, vTransfer;
858
859 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
860 float yM[20];
861 std::fill_n(yM, 15, 0.f);
862 std::copy_n(baseM + 0, 5, yM + 15);
863 GrPaint yPaint;
864 auto yTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
865 auto yColFP = GrColorMatrixFragmentProcessor::Make(std::move(yTexFP), yM,
866 /*unpremulInput=*/false,
867 /*clampRGBOutput=*/true,
868 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400869 yPaint.setColorFragmentProcessor(std::move(yColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400870 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
871 yRTC->fillRectToRect(nullptr, std::move(yPaint), GrAA::kNo, SkMatrix::I(), dstRectY, dstRectY);
872 if (!doSynchronousRead) {
873 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
874 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
875 if (!yTransfer.fTransferBuffer) {
876 callback(context, nullptr);
877 return;
878 }
879 }
880
881 texMatrix.preScale(2.f, 2.f);
882 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
883 float uM[20];
884 std::fill_n(uM, 15, 0.f);
885 std::copy_n(baseM + 5, 5, uM + 15);
886 GrPaint uPaint;
887 auto uTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix,
Brian Salomona3b02f52020-07-15 16:02:01 -0400888 GrSamplerState::Filter::kLinear);
Brian Salomon63a0a752020-06-26 13:32:09 -0400889 auto uColFP = GrColorMatrixFragmentProcessor::Make(std::move(uTexFP), uM,
890 /*unpremulInput=*/false,
891 /*clampRGBOutput=*/true,
892 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400893 uPaint.setColorFragmentProcessor(std::move(uColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400894 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
895 uRTC->fillRectToRect(nullptr, std::move(uPaint), GrAA::kNo, SkMatrix::I(), dstRectUV,
896 dstRectUV);
897 if (!doSynchronousRead) {
898 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
899 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
900 if (!uTransfer.fTransferBuffer) {
901 callback(context, nullptr);
902 return;
903 }
904 }
905
906 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
907 float vM[20];
908 std::fill_n(vM, 15, 0.f);
909 std::copy_n(baseM + 10, 5, vM + 15);
910 GrPaint vPaint;
911 auto vTexFP = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType(),
Brian Salomona3b02f52020-07-15 16:02:01 -0400912 texMatrix, GrSamplerState::Filter::kLinear);
Brian Salomon63a0a752020-06-26 13:32:09 -0400913 auto vColFP = GrColorMatrixFragmentProcessor::Make(std::move(vTexFP), vM,
914 /*unpremulInput=*/false,
915 /*clampRGBOutput=*/true,
916 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400917 vPaint.setColorFragmentProcessor(std::move(vColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400918 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
919 vRTC->fillRectToRect(nullptr, std::move(vPaint), GrAA::kNo, SkMatrix::I(), dstRectUV,
920 dstRectUV);
921 if (!doSynchronousRead) {
922 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
923 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
924 if (!vTransfer.fTransferBuffer) {
925 callback(context, nullptr);
926 return;
927 }
928 }
929
930 if (doSynchronousRead) {
931 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
932 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
933 size_t yRB = yInfo.minRowBytes();
934 size_t uvRB = uvInfo.minRowBytes();
935 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
936 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
937 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
938 if (!yRTC->readPixels(yInfo, y.get(), yRB, {0, 0}, direct) ||
939 !uRTC->readPixels(uvInfo, u.get(), uvRB, {0, 0}, direct) ||
940 !vRTC->readPixels(uvInfo, v.get(), uvRB, {0, 0}, direct)) {
941 callback(context, nullptr);
942 return;
943 }
944 auto result = std::make_unique<AsyncReadResult>(direct->priv().contextID());
945 result->addCpuPlane(std::move(y), yRB );
946 result->addCpuPlane(std::move(u), uvRB);
947 result->addCpuPlane(std::move(v), uvRB);
948 callback(context, std::move(result));
949 return;
950 }
951
952 struct FinishContext {
953 ReadPixelsCallback* fClientCallback;
954 ReadPixelsContext fClientContext;
955 GrClientMappedBufferManager* fMappedBufferManager;
956 SkISize fSize;
957 PixelTransferResult fYTransfer;
958 PixelTransferResult fUTransfer;
959 PixelTransferResult fVTransfer;
960 };
961 // Assumption is that the caller would like to flush. We could take a parameter or require an
962 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
963 // callback to GrGpu until after the next flush that flushes our op list, though.
964 auto* finishContext = new FinishContext{callback,
965 context,
966 direct->priv().clientMappedBufferManager(),
967 dstSize,
968 std::move(yTransfer),
969 std::move(uTransfer),
970 std::move(vTransfer)};
971 auto finishCallback = [](GrGpuFinishedContext c) {
972 const auto* context = reinterpret_cast<const FinishContext*>(c);
973 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
974 auto manager = context->fMappedBufferManager;
975 size_t rowBytes = SkToSizeT(context->fSize.width());
976 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
977 (*context->fClientCallback)(context->fClientContext, nullptr);
978 delete context;
979 return;
980 }
981 rowBytes /= 2;
982 SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
983 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
984 (*context->fClientCallback)(context->fClientContext, nullptr);
985 delete context;
986 return;
987 }
988 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
989 (*context->fClientCallback)(context->fClientContext, nullptr);
990 delete context;
991 return;
992 }
993 (*context->fClientCallback)(context->fClientContext, std::move(result));
994 delete context;
995 };
996 GrFlushInfo flushInfo;
997 flushInfo.fFinishedContext = finishContext;
998 flushInfo.fFinishedProc = finishCallback;
999 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo, nullptr);
1000}
1001
Brian Salomonc5243782020-04-02 12:50:34 -04001002bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001003 ASSERT_SINGLE_OWNER
1004 RETURN_FALSE_IF_ABANDONED
1005 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -04001006 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -04001007
Brian Salomon947efe22019-07-16 15:36:11 -04001008 const GrCaps* caps = fContext->priv().caps();
1009
Greg Daniel46cfbc62019-06-07 11:43:30 -04001010 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -05001011 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -04001012
Stephen White3c0a50f2020-01-16 18:19:54 -05001013 if (this->asSurfaceProxy()->framebufferOnly()) {
1014 return false;
1015 }
1016
Chris Daltonf8e5aad2019-08-02 12:55:23 -06001017 if (!caps->canCopySurface(this->asSurfaceProxy(), src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -04001018 return false;
1019 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001020
Greg Daniel16f5c652019-10-29 11:26:01 -04001021 // The swizzle doesn't matter for copies and it is not used.
1022 return this->drawingManager()->newCopyRenderTask(
Brian Salomonc5243782020-04-02 12:50:34 -04001023 GrSurfaceProxyView(sk_ref_sp(src), this->origin(), GrSwizzle("rgba")), srcRect,
Greg Daniel46e366a2019-12-16 14:38:36 -05001024 this->readSurfaceView(), dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001025}
Greg Daniel46cfbc62019-06-07 11:43:30 -04001026
Brian Salomon63a0a752020-06-26 13:32:09 -04001027std::unique_ptr<GrRenderTargetContext> GrSurfaceContext::rescale(const GrImageInfo& info,
1028 GrSurfaceOrigin origin,
1029 SkIRect srcRect,
1030 RescaleGamma rescaleGamma,
1031 SkFilterQuality rescaleQuality) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001032 auto rtProxy = this->asRenderTargetProxy();
1033 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1034 return nullptr;
1035 }
1036
Stephen White3c0a50f2020-01-16 18:19:54 -05001037 if (this->asSurfaceProxy()->framebufferOnly()) {
1038 return nullptr;
1039 }
1040
Brian Salomone9ad9982019-07-22 16:17:41 -04001041 // We rescale by drawing and don't currently support drawing to a kUnpremul destination.
1042 if (info.alphaType() == kUnpremul_SkAlphaType) {
1043 return nullptr;
1044 }
1045
Greg Daniel40903af2020-01-30 14:55:05 -05001046 GrSurfaceProxyView texView = this->readSurfaceView();
Brian Salomonfc118442019-11-22 19:09:27 -05001047 SkAlphaType srcAlphaType = this->colorInfo().alphaType();
Greg Daniel40903af2020-01-30 14:55:05 -05001048 if (!texView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -04001049 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect,
Brian Salomonc5243782020-04-02 12:50:34 -04001050 SkBackingFit::kApprox, SkBudgeted::kNo);
1051 if (!texView) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001052 return nullptr;
1053 }
Greg Daniel40903af2020-01-30 14:55:05 -05001054 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001055 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001056 }
1057
Brian Salomonbf6b9792019-08-21 09:38:10 -04001058 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
1059 // pass B is moved to A. If 'this' is the input on the first pass then tempA is null.
1060 std::unique_ptr<GrRenderTargetContext> tempA;
1061 std::unique_ptr<GrRenderTargetContext> tempB;
1062
Brian Salomone9ad9982019-07-22 16:17:41 -04001063 // Assume we should ignore the rescale linear request if the surface has no color space since
1064 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon63a0a752020-06-26 13:32:09 -04001065 if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() &&
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001066 !this->colorInfo().colorSpace()->gammaIsLinear()) {
1067 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomonfc118442019-11-22 19:09:27 -05001068 auto xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(), srcAlphaType, cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -04001069 kPremul_SkAlphaType);
1070 // We'll fall back to kRGBA_8888 if half float not supported.
Greg Daniele20fcad2020-01-08 11:52:34 -05001071 auto linearRTC = GrRenderTargetContext::MakeWithFallback(
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001072 fContext, GrColorType::kRGBA_F16, cs, SkBackingFit::kApprox, srcRect.size(), 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001073 GrMipmapped::kNo, GrProtected::kNo, origin);
Brian Salomone9ad9982019-07-22 16:17:41 -04001074 if (!linearRTC) {
1075 return nullptr;
1076 }
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001077 // 1-to-1 draw can always be kFast.
Brian Salomone69b9ef2020-07-22 11:18:06 -04001078 linearRTC->drawTexture(nullptr,
1079 std::move(texView),
1080 srcAlphaType,
1081 GrSamplerState::Filter::kNearest,
1082 GrSamplerState::MipmapMode::kNone,
1083 SkBlendMode::kSrc,
1084 SK_PMColor4fWHITE,
1085 SkRect::Make(srcRect),
1086 SkRect::Make(srcRect.size()),
1087 GrAA::kNo,
1088 GrQuadAAFlags::kNone,
1089 SkCanvas::kFast_SrcRectConstraint,
1090 SkMatrix::I(),
1091 std::move(xform));
Greg Daniel40903af2020-01-30 14:55:05 -05001092 texView = linearRTC->readSurfaceView();
1093 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -04001094 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001095 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001096 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001097
Brian Salomon9c6f6bf2020-05-29 16:37:26 -04001098 while (srcRect.size() != info.dimensions()) {
Brian Salomon59f31b12020-06-04 17:27:15 -04001099 SkISize nextDims = info.dimensions();
1100 if (rescaleQuality != kNone_SkFilterQuality) {
1101 if (srcRect.width() > info.width()) {
1102 nextDims.fWidth = std::max((srcRect.width() + 1)/2, info.width());
1103 } else if (srcRect.width() < info.width()) {
1104 nextDims.fWidth = std::min(srcRect.width()*2, info.width());
1105 }
1106 if (srcRect.height() > info.height()) {
1107 nextDims.fHeight = std::max((srcRect.height() + 1)/2, info.height());
1108 } else if (srcRect.height() < info.height()) {
1109 nextDims.fHeight = std::min(srcRect.height()*2, info.height());
1110 }
1111 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001112 auto input = tempA ? tempA.get() : this;
Brian Salomon59f31b12020-06-04 17:27:15 -04001113 GrColorType colorType = input->colorInfo().colorType();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001114 auto cs = input->colorInfo().refColorSpace();
Brian Salomone9ad9982019-07-22 16:17:41 -04001115 sk_sp<GrColorSpaceXform> xform;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001116 auto prevAlphaType = input->colorInfo().alphaType();
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001117 if (nextDims == info.dimensions()) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001118 // Might as well fold conversion to final info in the last step.
1119 cs = info.refColorSpace();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001120 xform = GrColorSpaceXform::Make(input->colorInfo().colorSpace(),
1121 input->colorInfo().alphaType(), cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -04001122 info.alphaType());
1123 }
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001124 tempB = GrRenderTargetContext::MakeWithFallback(fContext, colorType, std::move(cs),
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001125 SkBackingFit::kApprox, nextDims, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001126 GrMipmapped::kNo, GrProtected::kNo, origin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001127 if (!tempB) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001128 return nullptr;
1129 }
Brian Salomon59f31b12020-06-04 17:27:15 -04001130 auto dstRect = SkRect::Make(nextDims);
1131 if (rescaleQuality == kHigh_SkFilterQuality) {
1132 SkMatrix matrix;
1133 matrix.setScaleTranslate((float)srcRect.width()/nextDims.width(),
1134 (float)srcRect.height()/nextDims.height(),
1135 srcRect.x(),
1136 srcRect.y());
1137 std::unique_ptr<GrFragmentProcessor> fp;
1138 auto dir = GrBicubicEffect::Direction::kXY;
1139 if (nextDims.width() == srcRect.width()) {
1140 dir = GrBicubicEffect::Direction::kY;
1141 } else if (nextDims.height() == srcRect.height()) {
1142 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -04001143 }
Brian Salomon1af72d12020-06-25 10:47:26 -04001144 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
1145 static constexpr auto kKernel = GrBicubicEffect::Kernel::kCatmullRom;
Brian Salomon59f31b12020-06-04 17:27:15 -04001146 fp = GrBicubicEffect::MakeSubset(std::move(texView), prevAlphaType, matrix, kWM, kWM,
Brian Salomon1af72d12020-06-25 10:47:26 -04001147 SkRect::Make(srcRect), kKernel, dir, *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -04001148 if (xform) {
1149 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -04001150 }
Brian Salomon59f31b12020-06-04 17:27:15 -04001151 GrPaint paint;
John Stiles5933d7d2020-07-21 12:28:35 -04001152 paint.setColorFragmentProcessor(std::move(fp));
Brian Salomon59f31b12020-06-04 17:27:15 -04001153 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
1154 tempB->fillRectToRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect,
1155 dstRect);
1156 } else {
1157 auto filter = rescaleQuality == kNone_SkFilterQuality ? GrSamplerState::Filter::kNearest
Brian Salomona3b02f52020-07-15 16:02:01 -04001158 : GrSamplerState::Filter::kLinear;
Brian Salomon59f31b12020-06-04 17:27:15 -04001159 // Minimizing draw with integer coord src and dev rects can always be kFast.
1160 auto constraint = SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint;
1161 if (nextDims.width() <= srcRect.width() && nextDims.height() <= srcRect.height()) {
1162 constraint = SkCanvas::SrcRectConstraint::kFast_SrcRectConstraint;
1163 }
Brian Salomone69b9ef2020-07-22 11:18:06 -04001164 tempB->drawTexture(nullptr,
1165 std::move(texView),
1166 srcAlphaType,
1167 filter,
1168 GrSamplerState::MipmapMode::kNone,
1169 SkBlendMode::kSrc,
1170 SK_PMColor4fWHITE,
1171 SkRect::Make(srcRect),
1172 dstRect,
1173 GrAA::kNo,
1174 GrQuadAAFlags::kNone,
1175 constraint,
1176 SkMatrix::I(),
1177 std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -04001178 }
Greg Daniel40903af2020-01-30 14:55:05 -05001179 texView = tempB->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -04001180 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001181 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomone9ad9982019-07-22 16:17:41 -04001182 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001183 SkASSERT(tempA);
1184 return tempA;
Brian Salomone9ad9982019-07-22 16:17:41 -04001185}
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001186
Brian Salomon63a0a752020-06-26 13:32:09 -04001187GrSemaphoresSubmitted GrSurfaceContext::flush(SkSurface::BackendSurfaceAccess access,
1188 const GrFlushInfo& info,
1189 const GrBackendSurfaceMutableState* newState) {
1190 ASSERT_SINGLE_OWNER
Robert Phillips9eb00022020-06-30 15:30:12 -04001191 if (fContext->abandoned()) {
Brian Salomon63a0a752020-06-26 13:32:09 -04001192 if (info.fSubmittedProc) {
1193 info.fSubmittedProc(info.fSubmittedContext, false);
1194 }
1195 if (info.fFinishedProc) {
1196 info.fFinishedProc(info.fFinishedContext);
1197 }
1198 return GrSemaphoresSubmitted::kNo;
1199 }
1200 SkDEBUGCODE(this->validate();)
1201 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
1202
1203 return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info, newState);
1204}
1205
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001206GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
1207 const SkIRect& rect) {
1208 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1209 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001210 auto direct = fContext->asDirectContext();
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001211 if (!direct) {
1212 return {};
1213 }
1214 auto rtProxy = this->asRenderTargetProxy();
1215 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1216 return {};
1217 }
1218
1219 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001220 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
1221 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001222 // Fail if read color type does not have all of dstCT's color channels and those missing color
1223 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001224 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1225 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
1226 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1227 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001228 return {};
1229 }
1230
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001231 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001232 !supportedRead.fOffsetAlignmentForTransferBuffer) {
1233 return {};
1234 }
1235
1236 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
1237 size_t size = rowBytes * rect.height();
1238 auto buffer = direct->priv().resourceProvider()->createBuffer(
1239 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
1240 if (!buffer) {
1241 return {};
1242 }
1243 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -05001244 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001245 if (flip) {
1246 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1247 this->height() - rect.fTop);
1248 }
Greg Danielbbfec9d2019-08-20 10:56:51 -04001249 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001250 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -04001251 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001252 PixelTransferResult result;
1253 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001254 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -04001255 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001256 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
1257 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001258 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
1259 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001260 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
1261 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -04001262 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001263 };
1264 }
1265 return result;
1266}
Greg Daniel46e366a2019-12-16 14:38:36 -05001267
1268#ifdef SK_DEBUG
1269void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001270 SkASSERT(fReadView.proxy());
1271 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -04001272 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
1273 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
1274 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
1275 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001276 this->onValidate();
1277}
1278#endif