blob: 294c3b23d19f93322691631a9f3ab6eaf1e6da4a [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
Brian Salomon9c6f6bf2020-05-29 16:37:26 -040010#include "include/effects/SkRuntimeEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/GrRecordingContext.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040012#include "src/core/SkAutoPixmapStorage.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040013#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrContextPriv.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040015#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrDrawingManager.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040017#include "src/gpu/GrGpu.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040018#include "src/gpu/GrImageInfo.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040019#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrRecordingContextPriv.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040021#include "src/gpu/GrRenderTargetContext.h"
Greg Daniel46cfbc62019-06-07 11:43:30 -040022#include "src/gpu/GrSurfaceContextPriv.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040023#include "src/gpu/GrSurfacePriv.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 Salomon9c6f6bf2020-05-29 16:37:26 -040026#include "src/gpu/effects/GrSkSLFP.h"
Brian Osman45580d32016-11-23 09:37:01 -050027
Robert Phillips2de8cfa2017-06-28 10:33:41 -040028#define ASSERT_SINGLE_OWNER \
29 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -050030#define RETURN_FALSE_IF_ABANDONED if (this->fContext->priv().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.
41 if (context->priv().abandoned()) {
42 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,
74 GrMipMapped mipMapped,
75 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)) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500111 SkASSERT(!context->priv().abandoned());
112}
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,
Brian Salomon1d435302019-07-01 13:05:28 -0400135 SkIPoint pt, GrContext* direct) {
136 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
Brian Salomon1d435302019-07-01 13:05:28 -0400141 if (!direct && !(direct = fContext->priv().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(),
216 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);
241 paint.addColorFragmentProcessor(std::move(fp));
242
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,
Brian Salomon1d435302019-07-01 13:05:28 -0400293 SkIPoint pt, GrContext* 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
Brian Salomon1d435302019-07-01 13:05:28 -0400299 if (!direct && !(direct = fContext->priv().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 Salomondf1bd6d2020-03-26 20:37:01 -0400391 format, srcInfo.dimensions(), GrRenderable::kNo, 1, GrMipMapped::kNo,
392 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);
428 paint.addColorFragmentProcessor(std::move(fp));
429 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 Salomonc5243782020-04-02 12:50:34 -0400480bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400481 ASSERT_SINGLE_OWNER
482 RETURN_FALSE_IF_ABANDONED
483 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -0400484 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -0400485
Brian Salomon947efe22019-07-16 15:36:11 -0400486 const GrCaps* caps = fContext->priv().caps();
487
Greg Daniel46cfbc62019-06-07 11:43:30 -0400488 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -0500489 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -0400490
Stephen White3c0a50f2020-01-16 18:19:54 -0500491 if (this->asSurfaceProxy()->framebufferOnly()) {
492 return false;
493 }
494
Chris Daltonf8e5aad2019-08-02 12:55:23 -0600495 if (!caps->canCopySurface(this->asSurfaceProxy(), src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -0400496 return false;
497 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400498
Greg Daniel16f5c652019-10-29 11:26:01 -0400499 // The swizzle doesn't matter for copies and it is not used.
500 return this->drawingManager()->newCopyRenderTask(
Brian Salomonc5243782020-04-02 12:50:34 -0400501 GrSurfaceProxyView(sk_ref_sp(src), this->origin(), GrSwizzle("rgba")), srcRect,
Greg Daniel46e366a2019-12-16 14:38:36 -0500502 this->readSurfaceView(), dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400503}
Greg Daniel46cfbc62019-06-07 11:43:30 -0400504
Brian Salomon9c6f6bf2020-05-29 16:37:26 -0400505template <int N> static std::unique_ptr<GrSkSLFP> make_avg_effect(GrRecordingContext* context) {
506 static sk_sp<SkRuntimeEffect> gEffect;
507 static SkString gName;
508 if (!gEffect) {
509 SkString string;
510 for (int i = 0; i < N; ++i) {
511 string.appendf("in fragmentProcessor child%d;\n", i);
512 }
513 string.append("void main(float2 p, inout half4 color) {\n");
514 for (int i = 0; i < N; ++i) {
515 string.appendf(" color %c= sample(child%d, p);\n", i ? '+' : ' ', i);
516 }
517 string.appendf(" color /= half(%d);\n"
518 "}\n", N);
519 gEffect = std::get<0>(SkRuntimeEffect::Make(std::move(string)));
520 SkASSERT(gEffect);
521 gName.printf("Avg%d", N);
522 }
523 return GrSkSLFP::Make(context, gEffect, gName.c_str(), nullptr);
524}
525
526template <int NX, int NY>
527static std::unique_ptr<GrFragmentProcessor> make_multibilerp_effect(GrRecordingContext* context,
528 GrSurfaceProxyView srcView,
529 SkAlphaType alphaType,
530 const SkIRect& srcRect,
531 const SkISize& dstSize) {
532 auto effect = make_avg_effect<NX*NY>(context);
533
534 // scale factors.
535 float sx = static_cast<float>(srcRect.width()) /dstSize.width(),
536 sy = static_cast<float>(srcRect.height())/dstSize.height();
537 // spacing between bilerp samples.
538 float dx = sx/NX,
539 dy = sy/NY;
540 // offset in src from back projection of dst pixel center to left/upper-most bilerp sample.
541 float x0 = (dx - sx)/2,
542 y0 = (dy - sy)/2;
543
544 const auto& caps = *context->priv().caps();
545 for (int j = 0; j < NY; ++j) {
546 for (int i = 0; i < NX; ++i) {
547 float tx = x0 + i*dx,
548 ty = y0 + j*dy;
549 SkMatrix m = SkMatrix::Scale(sx, sy);
550 m.postTranslate(tx + srcRect.x(), ty + srcRect.y());
551 SkRect domain = SkRect::Make(srcRect).makeInset(sx/2, sy/2).makeOffset(tx, ty);
552 effect->addChild(GrTextureEffect::MakeSubset(srcView, alphaType, m,
553 GrSamplerState::Filter::kBilerp,
554 SkRect::Make(srcRect), domain, caps));
555 }
556 }
557 return std::move(effect);
558}
559
Brian Salomonbf6b9792019-08-21 09:38:10 -0400560std::unique_ptr<GrRenderTargetContext> GrSurfaceContext::rescale(
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400561 const GrImageInfo& info,
562 GrSurfaceOrigin origin,
Brian Salomonafd8a6c2020-05-21 12:10:54 -0400563 SkIRect srcRect,
Brian Salomonbf6b9792019-08-21 09:38:10 -0400564 SkSurface::RescaleGamma rescaleGamma,
565 SkFilterQuality rescaleQuality) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400566 auto rtProxy = this->asRenderTargetProxy();
567 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
568 return nullptr;
569 }
570
Stephen White3c0a50f2020-01-16 18:19:54 -0500571 if (this->asSurfaceProxy()->framebufferOnly()) {
572 return nullptr;
573 }
574
Brian Salomone9ad9982019-07-22 16:17:41 -0400575 // We rescale by drawing and don't currently support drawing to a kUnpremul destination.
576 if (info.alphaType() == kUnpremul_SkAlphaType) {
577 return nullptr;
578 }
579
Greg Daniel40903af2020-01-30 14:55:05 -0500580 GrSurfaceProxyView texView = this->readSurfaceView();
Brian Salomonfc118442019-11-22 19:09:27 -0500581 SkAlphaType srcAlphaType = this->colorInfo().alphaType();
Greg Daniel40903af2020-01-30 14:55:05 -0500582 if (!texView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -0400583 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipMapped::kNo, srcRect,
584 SkBackingFit::kApprox, SkBudgeted::kNo);
585 if (!texView) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400586 return nullptr;
587 }
Greg Daniel40903af2020-01-30 14:55:05 -0500588 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -0400589 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -0400590 }
591
Brian Salomonbf6b9792019-08-21 09:38:10 -0400592 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
593 // pass B is moved to A. If 'this' is the input on the first pass then tempA is null.
594 std::unique_ptr<GrRenderTargetContext> tempA;
595 std::unique_ptr<GrRenderTargetContext> tempB;
596
Brian Salomone9ad9982019-07-22 16:17:41 -0400597 // Assume we should ignore the rescale linear request if the surface has no color space since
598 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400599 if (rescaleGamma == SkSurface::kLinear && this->colorInfo().colorSpace() &&
600 !this->colorInfo().colorSpace()->gammaIsLinear()) {
601 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomonfc118442019-11-22 19:09:27 -0500602 auto xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(), srcAlphaType, cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -0400603 kPremul_SkAlphaType);
604 // We'll fall back to kRGBA_8888 if half float not supported.
Greg Daniele20fcad2020-01-08 11:52:34 -0500605 auto linearRTC = GrRenderTargetContext::MakeWithFallback(
Brian Salomonafd8a6c2020-05-21 12:10:54 -0400606 fContext, GrColorType::kRGBA_F16, cs, SkBackingFit::kApprox, srcRect.size(), 1,
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400607 GrMipMapped::kNo, GrProtected::kNo, origin);
Brian Salomone9ad9982019-07-22 16:17:41 -0400608 if (!linearRTC) {
609 return nullptr;
610 }
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400611 // 1-to-1 draw can always be kFast.
Michael Ludwig7c12e282020-05-29 09:54:07 -0400612 linearRTC->drawTexture(nullptr, std::move(texView), srcAlphaType,
Brian Salomonfc118442019-11-22 19:09:27 -0500613 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
Brian Salomonafd8a6c2020-05-21 12:10:54 -0400614 SK_PMColor4fWHITE, SkRect::Make(srcRect),
615 SkRect::Make(srcRect.size()), GrAA::kNo, GrQuadAAFlags::kNone,
616 SkCanvas::kFast_SrcRectConstraint, SkMatrix::I(), std::move(xform));
Greg Daniel40903af2020-01-30 14:55:05 -0500617 texView = linearRTC->readSurfaceView();
618 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -0400619 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -0400620 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -0400621 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -0400622
Brian Salomon9c6f6bf2020-05-29 16:37:26 -0400623 enum StepType {
624 kNearest,
625 kBilinear,
626 kFourTapBilinear,
627 kBicubic,
628 };
629 auto determineNextStep = [rescaleQuality, finalSize = info.dimensions()](SkISize srcSize) {
630 if (rescaleQuality == kNone_SkFilterQuality) {
Brian Salomon7a34aff2020-05-29 22:47:54 -0400631 return std::make_tuple(StepType::kNearest, finalSize);
Brian Salomone9ad9982019-07-22 16:17:41 -0400632 }
Brian Salomon9c6f6bf2020-05-29 16:37:26 -0400633 SkISize nextSize;
634 if (srcSize.width() > finalSize.width()) {
635 nextSize.fWidth = std::max((srcSize.width() + 1)/2, finalSize.width());
636 } else {
637 nextSize.fWidth = std::min(srcSize.width()*2, finalSize.width());
638 }
639 if (srcSize.height() > finalSize.height()) {
640 nextSize.fHeight = std::max((srcSize.height() + 1)/2, finalSize.height());
641 } else {
642 nextSize.fHeight = std::min(srcSize.height()*2, finalSize.height());
643 }
644 if (rescaleQuality == kHigh_SkFilterQuality) {
Brian Salomon7a34aff2020-05-29 22:47:54 -0400645 return std::make_tuple(StepType::kBicubic, nextSize);
Brian Salomon9c6f6bf2020-05-29 16:37:26 -0400646 }
647 // See if we can do multiple bilinear steps in one.
648 if (nextSize.width() > finalSize.width() && nextSize.height() > finalSize.height()) {
649 nextSize = {std::max((nextSize.width() + 1)/2, finalSize.width()),
650 std::max((nextSize.height() + 1)/2, finalSize.height())};
Brian Salomon7a34aff2020-05-29 22:47:54 -0400651 return std::make_tuple(StepType::kFourTapBilinear, nextSize);
Brian Salomon9c6f6bf2020-05-29 16:37:26 -0400652 }
Brian Salomon7a34aff2020-05-29 22:47:54 -0400653 return std::make_tuple(StepType::kBilinear, nextSize);
Brian Salomon9c6f6bf2020-05-29 16:37:26 -0400654 };
655 while (srcRect.size() != info.dimensions()) {
656 auto [stepType, nextDims] = determineNextStep(srcRect.size());
Brian Salomonbf6b9792019-08-21 09:38:10 -0400657 auto input = tempA ? tempA.get() : this;
Brian Salomon9c6f6bf2020-05-29 16:37:26 -0400658 auto colorType = input->colorInfo().colorType();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400659 auto cs = input->colorInfo().refColorSpace();
Brian Salomone9ad9982019-07-22 16:17:41 -0400660 sk_sp<GrColorSpaceXform> xform;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400661 auto prevAlphaType = input->colorInfo().alphaType();
Brian Salomonafd8a6c2020-05-21 12:10:54 -0400662 if (nextDims == info.dimensions()) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400663 // Might as well fold conversion to final info in the last step.
664 cs = info.refColorSpace();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400665 xform = GrColorSpaceXform::Make(input->colorInfo().colorSpace(),
666 input->colorInfo().alphaType(), cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -0400667 info.alphaType());
668 }
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400669 tempB = GrRenderTargetContext::MakeWithFallback(fContext, colorType, std::move(cs),
Brian Salomonafd8a6c2020-05-21 12:10:54 -0400670 SkBackingFit::kApprox, nextDims, 1,
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400671 GrMipMapped::kNo, GrProtected::kNo, origin);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400672 if (!tempB) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400673 return nullptr;
674 }
Brian Salomon9c6f6bf2020-05-29 16:37:26 -0400675 std::unique_ptr<GrFragmentProcessor> srcFP;
676 switch (stepType) {
677 case StepType::kBicubic: {
678 SkMatrix matrix;
679 matrix.setScaleTranslate((float)srcRect.width() /nextDims.width(),
680 (float)srcRect.height()/nextDims.height(),
681 srcRect.x(),
682 srcRect.y());
683 auto dir = GrBicubicEffect::Direction::kXY;
684 if (nextDims.width() == srcRect.width()) {
685 dir = GrBicubicEffect::Direction::kY;
686 } else if (nextDims.height() == srcRect.height()) {
687 dir = GrBicubicEffect::Direction::kX;
688 }
689 static constexpr GrSamplerState::WrapMode kWM = GrSamplerState::WrapMode::kClamp;
690 srcFP = GrBicubicEffect::MakeSubset(std::move(texView), prevAlphaType, matrix, kWM,
691 kWM, SkRect::Make(srcRect), dir, *this->caps());
692 break;
Brian Salomone9ad9982019-07-22 16:17:41 -0400693 }
Brian Salomon9c6f6bf2020-05-29 16:37:26 -0400694 case StepType::kFourTapBilinear:
695 srcFP = make_multibilerp_effect<2, 2>(fContext, texView, srcAlphaType, srcRect,
696 nextDims);
697 break;
698 default: {
699 auto filter = stepType == StepType::kNearest ? GrSamplerState::Filter::kNearest
700 : GrSamplerState::Filter::kBilerp;
701 float sx = static_cast<float>(srcRect.width()) /nextDims.width(),
702 sy = static_cast<float>(srcRect.height())/nextDims.height();
703 SkMatrix matrix;
704 matrix.setScaleTranslate(sx, sy, srcRect.x(), srcRect.y());
705 SkRect domain = SkRect::Make(srcRect).makeInset(sx/2, sy/2);
706 srcFP = GrTextureEffect::MakeSubset(std::move(texView), srcAlphaType, matrix,
707 filter, SkRect::Make(srcRect), domain,
708 *this->caps());
709 break;
Brian Salomone9ad9982019-07-22 16:17:41 -0400710 }
Brian Salomone9ad9982019-07-22 16:17:41 -0400711 }
Brian Salomon9c6f6bf2020-05-29 16:37:26 -0400712 GrPaint paint;
713 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
714 srcFP = GrColorSpaceXformEffect::Make(std::move(srcFP), std::move(xform));
715 paint.addColorFragmentProcessor(std::move(srcFP));
716 tempB->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
717 SkRect::Make(nextDims));
Greg Daniel40903af2020-01-30 14:55:05 -0500718 texView = tempB->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -0400719 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -0400720 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomone9ad9982019-07-22 16:17:41 -0400721 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400722 SkASSERT(tempA);
723 return tempA;
Brian Salomone9ad9982019-07-22 16:17:41 -0400724}
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400725
726GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
727 const SkIRect& rect) {
728 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
729 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
730 auto direct = fContext->priv().asDirectContext();
731 if (!direct) {
732 return {};
733 }
734 auto rtProxy = this->asRenderTargetProxy();
735 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
736 return {};
737 }
738
739 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400740 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
741 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400742 // Fail if read color type does not have all of dstCT's color channels and those missing color
743 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -0400744 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
745 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
746 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
747 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400748 return {};
749 }
750
Brian Salomonfb28c6f2020-01-10 13:04:45 -0500751 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400752 !supportedRead.fOffsetAlignmentForTransferBuffer) {
753 return {};
754 }
755
756 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
757 size_t size = rowBytes * rect.height();
758 auto buffer = direct->priv().resourceProvider()->createBuffer(
759 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
760 if (!buffer) {
761 return {};
762 }
763 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -0500764 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400765 if (flip) {
766 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
767 this->height() - rect.fTop);
768 }
Greg Danielbbfec9d2019-08-20 10:56:51 -0400769 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400770 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -0400771 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400772 PixelTransferResult result;
773 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400774 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -0400775 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400776 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
777 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400778 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
779 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400780 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
781 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -0400782 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400783 };
784 }
785 return result;
786}
Greg Daniel46e366a2019-12-16 14:38:36 -0500787
788#ifdef SK_DEBUG
789void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500790 SkASSERT(fReadView.proxy());
791 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -0400792 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
793 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
794 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
795 }
Greg Daniel46e366a2019-12-16 14:38:36 -0500796 this->onValidate();
797}
798#endif