blob: 7e18008420a56078ae6feb3a2ea36662f7481fdf [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/GrRecordingContext.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040011#include "src/core/SkAutoPixmapStorage.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040012#include "src/gpu/GrAuditTrail.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040013#include "src/gpu/GrClip.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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrRecordingContextPriv.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040020#include "src/gpu/GrRenderTargetContext.h"
Greg Daniel46cfbc62019-06-07 11:43:30 -040021#include "src/gpu/GrSurfaceContextPriv.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040022#include "src/gpu/GrSurfacePriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/SkGr.h"
Brian Salomone9ad9982019-07-22 16:17:41 -040024#include "src/gpu/effects/GrBicubicEffect.h"
Brian Osman45580d32016-11-23 09:37:01 -050025
Robert Phillips2de8cfa2017-06-28 10:33:41 -040026#define ASSERT_SINGLE_OWNER \
27 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -050028#define RETURN_FALSE_IF_ABANDONED if (this->fContext->priv().abandoned()) { return false; }
Brian Osman45580d32016-11-23 09:37:01 -050029
Greg Danielbfa19c42019-12-19 16:41:40 -050030std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -050031 GrSurfaceProxyView readView,
Greg Danielbfa19c42019-12-19 16:41:40 -050032 GrColorType colorType,
33 SkAlphaType alphaType,
34 sk_sp<SkColorSpace> colorSpace) {
Greg Daniele20fcad2020-01-08 11:52:34 -050035 // It is probably not necessary to check if the context is abandoned here since uses of the
36 // GrSurfaceContext which need the context will mostly likely fail later on without an issue.
37 // However having this hear adds some reassurance in case there is a path doesn't handle an
38 // abandoned context correctly. It also lets us early out of some extra work.
39 if (context->priv().abandoned()) {
40 return nullptr;
41 }
Greg Daniel3912a4b2020-01-14 09:56:04 -050042 GrSurfaceProxy* proxy = readView.proxy();
Greg Danielbfa19c42019-12-19 16:41:40 -050043 SkASSERT(proxy && proxy->asTextureProxy());
44
Greg Danielbfa19c42019-12-19 16:41:40 -050045 std::unique_ptr<GrSurfaceContext> surfaceContext;
Greg Daniel3912a4b2020-01-14 09:56:04 -050046 if (proxy->asRenderTargetProxy()) {
Greg Danielbfa19c42019-12-19 16:41:40 -050047 SkASSERT(kPremul_SkAlphaType == alphaType || kOpaque_SkAlphaType == alphaType);
48 // Will we ever want a swizzle that is not the default output swizzle for the format and
49 // colorType here? If so we will need to manually pass that in.
50 GrSwizzle outSwizzle = context->priv().caps()->getOutputSwizzle(proxy->backendFormat(),
51 colorType);
Greg Danielc61d7e32020-02-04 14:27:45 -050052 GrSurfaceProxyView outputView(readView.refProxy(), readView.origin(), outSwizzle);
Greg Daniel3912a4b2020-01-14 09:56:04 -050053 surfaceContext.reset(new GrRenderTargetContext(context, std::move(readView),
54 std::move(outputView), colorType,
Greg Danielbfa19c42019-12-19 16:41:40 -050055 std::move(colorSpace), nullptr));
56 } else {
Greg Daniel3912a4b2020-01-14 09:56:04 -050057 surfaceContext.reset(new GrSurfaceContext(context, std::move(readView), colorType,
58 alphaType, std::move(colorSpace)));
Greg Danielbfa19c42019-12-19 16:41:40 -050059 }
Robert Phillips07f0e412020-01-17 15:20:00 -050060 SkDEBUGCODE(surfaceContext->validate();)
Greg Danielbfa19c42019-12-19 16:41:40 -050061 return surfaceContext;
62}
63
Brian Salomona56a7462020-02-07 14:17:25 -050064std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
65 SkISize dimensions,
66 const GrBackendFormat& format,
67 GrRenderable renderable,
68 int renderTargetSampleCnt,
69 GrMipMapped mipMapped,
70 GrProtected isProtected,
71 GrSurfaceOrigin origin,
72 GrColorType colorType,
73 SkAlphaType alphaType,
74 sk_sp<SkColorSpace> colorSpace,
75 SkBackingFit fit,
76 SkBudgeted budgeted) {
Brian Salomonb16f30b2020-03-31 09:17:56 -040077 GrSwizzle swizzle("rgba");
78 if (!context->priv().caps()->isFormatCompressed(format)) {
79 swizzle = context->priv().caps()->getReadSwizzle(format, colorType);
80 }
Greg Daniel47c20e82020-01-21 14:29:57 -050081
Greg Danielbfa19c42019-12-19 16:41:40 -050082 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -040083 format, dimensions, renderable, renderTargetSampleCnt, mipMapped, fit, budgeted,
84 isProtected);
Greg Danielbfa19c42019-12-19 16:41:40 -050085 if (!proxy) {
86 return nullptr;
87 }
88
Greg Daniel3912a4b2020-01-14 09:56:04 -050089 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
90 return GrSurfaceContext::Make(context, std::move(view), colorType, alphaType,
Greg Danielbfa19c42019-12-19 16:41:40 -050091 std::move(colorSpace));
92}
93
Greg Danielf41b2bd2019-08-22 16:19:24 -040094// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
95// GrOpsTasks to be picked up and added to by renderTargetContexts lower in the call
96// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
97// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -050098GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -050099 GrSurfaceProxyView readView,
Brian Salomond6287472019-06-24 15:50:07 -0400100 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -0400101 SkAlphaType alphaType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500102 sk_sp<SkColorSpace> colorSpace)
Greg Daniel901b98e2019-10-22 09:54:02 -0400103 : fContext(context)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500104 , fReadView(std::move(readView))
105 , fColorInfo(colorType, alphaType, std::move(colorSpace)) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500106 SkASSERT(!context->priv().abandoned());
107}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400108
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400109const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); }
110
Robert Phillips0d075de2019-03-04 11:08:13 -0500111GrAuditTrail* GrSurfaceContext::auditTrail() {
112 return fContext->priv().auditTrail();
113}
114
115GrDrawingManager* GrSurfaceContext::drawingManager() {
116 return fContext->priv().drawingManager();
117}
118
119const GrDrawingManager* GrSurfaceContext::drawingManager() const {
120 return fContext->priv().drawingManager();
121}
122
123#ifdef SK_DEBUG
124GrSingleOwner* GrSurfaceContext::singleOwner() {
125 return fContext->priv().singleOwner();
126}
127#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400128
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400129bool GrSurfaceContext::readPixels(const GrImageInfo& origDstInfo, void* dst, size_t rowBytes,
Brian Salomon1d435302019-07-01 13:05:28 -0400130 SkIPoint pt, GrContext* direct) {
131 ASSERT_SINGLE_OWNER
132 RETURN_FALSE_IF_ABANDONED
133 SkDEBUGCODE(this->validate();)
134 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Greg Daniel6eb8c242019-06-05 10:22:24 -0400135
Brian Salomon1d435302019-07-01 13:05:28 -0400136 if (!direct && !(direct = fContext->priv().asDirectContext())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400137 return false;
138 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400139
Brian Salomon1d435302019-07-01 13:05:28 -0400140 if (!dst) {
141 return false;
142 }
143
Brian Salomon1047a492019-07-02 12:25:21 -0400144 size_t tightRowBytes = origDstInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400145 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400146 rowBytes = tightRowBytes;
147 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400148 return false;
149 }
150
151 if (!origDstInfo.isValid()) {
152 return false;
153 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400154
155 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
156
Stephen White3c0a50f2020-01-16 18:19:54 -0500157 if (srcProxy->framebufferOnly()) {
158 return false;
159 }
160
Greg Daniel6eb8c242019-06-05 10:22:24 -0400161 // MDB TODO: delay this instantiation until later in the method
162 if (!srcProxy->instantiate(direct->priv().resourceProvider())) {
163 return false;
164 }
165
166 GrSurface* srcSurface = srcProxy->peekSurface();
167
Brian Salomon1d435302019-07-01 13:05:28 -0400168 auto dstInfo = origDstInfo;
169 if (!dstInfo.clip(this->width(), this->height(), &pt, &dst, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400170 return false;
171 }
Brian Salomon1047a492019-07-02 12:25:21 -0400172 // Our tight row bytes may have been changed by clipping.
173 tightRowBytes = dstInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400174
Mike Klein7321e6a2019-12-03 11:08:40 -0600175 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{this->colorInfo(), dstInfo}.flags;
176 bool unpremul = flags.unpremul,
177 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
178 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400179
180 const GrCaps* caps = direct->priv().caps();
Robert Phillips07f0e412020-01-17 15:20:00 -0500181 bool srcIsCompressed = caps->isFormatCompressed(srcSurface->backendFormat());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400182 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
183 // care so much about getImageData performance. However, in order to ensure putImageData/
184 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
185 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
186 // fContext->vaildaPMUPMConversionExists()).
Greg Daniel0258c902019-08-01 13:08:33 -0400187 GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
188 GrRenderable::kYes);
Greg Danielc71c7962020-01-14 16:44:18 -0500189 GrColorType srcColorType = this->colorInfo().colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400190 bool canvas2DFastPath = unpremul && !needColorConversion &&
191 (GrColorType::kRGBA_8888 == dstInfo.colorType() ||
192 GrColorType::kBGRA_8888 == dstInfo.colorType()) &&
193 SkToBool(srcProxy->asTextureProxy()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500194 (srcColorType == GrColorType::kRGBA_8888 ||
195 srcColorType == GrColorType::kBGRA_8888) &&
Greg Daniel0258c902019-08-01 13:08:33 -0400196 defaultRGBAFormat.isValid() &&
Brian Salomon1d435302019-07-01 13:05:28 -0400197 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400198
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400199 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400200 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400201 return false;
202 }
203
Brian Salomondc0710f2019-07-01 14:59:32 -0400204 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Robert Phillips07f0e412020-01-17 15:20:00 -0500205 GrColorType colorType = (canvas2DFastPath || srcIsCompressed)
206 ? GrColorType::kRGBA_8888 : this->colorInfo().colorType();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400207 sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorInfo().refColorSpace();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400208
Greg Daniele20fcad2020-01-08 11:52:34 -0500209 auto tempCtx = GrRenderTargetContext::Make(
210 direct, colorType, std::move(cs), SkBackingFit::kApprox, dstInfo.dimensions(),
211 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400212 if (!tempCtx) {
213 return false;
214 }
215
216 std::unique_ptr<GrFragmentProcessor> fp;
217 if (canvas2DFastPath) {
Greg Danield2ccbb52020-02-05 10:45:39 -0500218 fp = direct->priv().createPMToUPMEffect(
219 GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType()));
Brian Salomon1d435302019-07-01 13:05:28 -0400220 if (dstInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400221 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
Brian Salomon1d435302019-07-01 13:05:28 -0400222 dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400223 }
Brian Salomon1d435302019-07-01 13:05:28 -0400224 // The render target context is incorrectly tagged as kPremul even though we're writing
225 // unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type so we don't
226 // double unpremul.
227 dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400228 } else {
Greg Danield2ccbb52020-02-05 10:45:39 -0500229 fp = GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400230 }
231 if (!fp) {
232 return false;
233 }
234 GrPaint paint;
235 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
236 paint.addColorFragmentProcessor(std::move(fp));
237
238 tempCtx->asRenderTargetContext()->fillRectToRect(
239 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400240 SkRect::MakeWH(dstInfo.width(), dstInfo.height()),
241 SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400242
Brian Salomon1d435302019-07-01 13:05:28 -0400243 return tempCtx->readPixels(dstInfo, dst, rowBytes, {0, 0}, direct);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400244 }
245
Greg Danielb8d84f82020-02-13 14:25:00 -0500246 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400247
Brian Salomon1d435302019-07-01 13:05:28 -0400248 auto supportedRead = caps->supportedReadPixelsColorType(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400249 this->colorInfo().colorType(), srcProxy->backendFormat(), dstInfo.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400250
Brian Salomon1047a492019-07-02 12:25:21 -0400251 bool makeTight = !caps->readPixelsRowBytesSupport() && tightRowBytes != rowBytes;
252
253 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomon8f8354a2019-07-31 20:12:02 -0400254 (dstInfo.colorType() != supportedRead.fColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400255
Brian Salomonf30b1c12019-06-20 12:25:02 -0400256 std::unique_ptr<char[]> tmpPixels;
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400257 GrImageInfo tmpInfo;
Brian Salomon1d435302019-07-01 13:05:28 -0400258 void* readDst = dst;
259 size_t readRB = rowBytes;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400260 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400261 tmpInfo = {supportedRead.fColorType, this->colorInfo().alphaType(),
262 this->colorInfo().refColorSpace(), dstInfo.width(), dstInfo.height()};
Brian Salomon1d435302019-07-01 13:05:28 -0400263 size_t tmpRB = tmpInfo.minRowBytes();
264 size_t size = tmpRB * tmpInfo.height();
265 // Chrome MSAN bots require the data to be initialized (hence the ()).
266 tmpPixels.reset(new char[size]());
Brian Salomonf30b1c12019-06-20 12:25:02 -0400267
Brian Salomonf30b1c12019-06-20 12:25:02 -0400268 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400269 readRB = tmpRB;
270 pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400271 }
272
Greg Daniel55f040b2020-02-13 15:38:32 +0000273 direct->priv().flushSurface(srcProxy);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400274
Brian Salomon1d435302019-07-01 13:05:28 -0400275 if (!direct->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400276 dstInfo.height(), this->colorInfo().colorType(),
Brian Salomonf77c1462019-08-01 15:19:29 -0400277 supportedRead.fColorType, readDst, readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400278 return false;
279 }
280
Greg Daniel6eb8c242019-06-05 10:22:24 -0400281 if (convert) {
Brian Salomon8f8354a2019-07-31 20:12:02 -0400282 return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400283 }
284 return true;
285}
Robert Phillips0d075de2019-03-04 11:08:13 -0500286
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400287bool GrSurfaceContext::writePixels(const GrImageInfo& origSrcInfo, const void* src, size_t rowBytes,
Brian Salomon1d435302019-07-01 13:05:28 -0400288 SkIPoint pt, GrContext* direct) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400289 ASSERT_SINGLE_OWNER
290 RETURN_FALSE_IF_ABANDONED
291 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400292 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400293
Brian Salomon1d435302019-07-01 13:05:28 -0400294 if (!direct && !(direct = fContext->priv().asDirectContext())) {
Brian Salomonc320b152018-02-20 14:05:36 -0500295 return false;
296 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500297
Brian Salomon1d435302019-07-01 13:05:28 -0400298 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500299 return false;
300 }
301
Brian Salomon1d435302019-07-01 13:05:28 -0400302 if (!src) {
303 return false;
304 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400305
Brian Salomon1047a492019-07-02 12:25:21 -0400306 size_t tightRowBytes = origSrcInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400307 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400308 rowBytes = tightRowBytes;
309 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400310 return false;
311 }
312
313 if (!origSrcInfo.isValid()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400314 return false;
315 }
316
317 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
Stephen White3c0a50f2020-01-16 18:19:54 -0500318
319 if (dstProxy->framebufferOnly()) {
320 return false;
321 }
322
Greg Daniel6eb8c242019-06-05 10:22:24 -0400323 if (!dstProxy->instantiate(direct->priv().resourceProvider())) {
324 return false;
325 }
326
327 GrSurface* dstSurface = dstProxy->peekSurface();
328
Brian Salomon1d435302019-07-01 13:05:28 -0400329 auto srcInfo = origSrcInfo;
330 if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400331 return false;
332 }
Brian Salomon1047a492019-07-02 12:25:21 -0400333 // Our tight row bytes may have been changed by clipping.
334 tightRowBytes = srcInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400335
Mike Klein7321e6a2019-12-03 11:08:40 -0600336 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{srcInfo, this->colorInfo()}.flags;
337 bool unpremul = flags.unpremul,
338 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
339 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400340
341 const GrCaps* caps = direct->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400342
343 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
344 GrRenderable::kNo);
345
Greg Danielc71c7962020-01-14 16:44:18 -0500346 GrColorType dstColorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400347 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
348 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400349 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
350 (srcInfo.colorType() == GrColorType::kRGBA_8888 ||
351 srcInfo.colorType() == GrColorType::kBGRA_8888) &&
352 SkToBool(this->asRenderTargetContext()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500353 (dstColorType == GrColorType::kRGBA_8888 ||
354 dstColorType == GrColorType::kBGRA_8888) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400355 rgbaDefaultFormat.isValid() &&
Brian Salomon1d435302019-07-01 13:05:28 -0400356 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400357
358 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
Brian Salomond6287472019-06-24 15:50:07 -0400359 GrColorType colorType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400360
361 GrBackendFormat format;
Brian Salomone7499c72019-06-24 12:12:36 -0400362 SkAlphaType alphaType;
Greg Danielbfa19c42019-12-19 16:41:40 -0500363 GrSwizzle tempReadSwizzle;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400364 if (canvas2DFastPath) {
Brian Salomond6287472019-06-24 15:50:07 -0400365 colorType = GrColorType::kRGBA_8888;
Greg Daniel7bfc9132019-08-14 14:23:53 -0400366 format = rgbaDefaultFormat;
Brian Salomone7499c72019-06-24 12:12:36 -0400367 alphaType = kUnpremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400368 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400369 colorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400370 format = dstProxy->backendFormat().makeTexture2D();
371 if (!format.isValid()) {
372 return false;
373 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400374 alphaType = this->colorInfo().alphaType();
Greg Danielbfa19c42019-12-19 16:41:40 -0500375 tempReadSwizzle = this->readSwizzle();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400376 }
377
Greg Daniel2e52ad12019-06-13 10:04:16 -0400378 // It is more efficient for us to write pixels into a top left origin so we prefer that.
379 // However, if the final proxy isn't a render target then we must use a copy to move the
380 // data into it which requires the origins to match. If the final proxy is a render target
381 // we can use a draw instead which doesn't have this origin restriction. Thus for render
382 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400383 GrSurfaceOrigin tempOrigin =
Greg Danielb8d84f82020-02-13 14:25:00 -0500384 this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : this->origin();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400385 auto tempProxy = direct->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400386 format, srcInfo.dimensions(), GrRenderable::kNo, 1, GrMipMapped::kNo,
387 SkBackingFit::kApprox, SkBudgeted::kYes, GrProtected::kNo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400388 if (!tempProxy) {
389 return false;
390 }
Greg Daniel3912a4b2020-01-14 09:56:04 -0500391 GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle);
Greg Danield2ccbb52020-02-05 10:45:39 -0500392 GrSurfaceContext tempCtx(direct, tempView, colorType, alphaType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500393 this->colorInfo().refColorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400394
395 // In the fast path we always write the srcData to the temp context as though it were RGBA.
396 // When the data is really BGRA the write will cause the R and B channels to be swapped in
397 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
398 // dst.
Brian Salomon1d435302019-07-01 13:05:28 -0400399 if (canvas2DFastPath) {
400 srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888);
401 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500402 if (!tempCtx.writePixels(srcInfo, src, rowBytes, {0, 0}, direct)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400403 return false;
404 }
405
406 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400407 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400408 if (canvas2DFastPath) {
Brian Salomonb8f098d2020-01-07 11:15:44 -0500409 fp = direct->priv().createUPMToPMEffect(
Greg Danield2ccbb52020-02-05 10:45:39 -0500410 GrTextureEffect::Make(std::move(tempView), alphaType));
Brian Salomon1d435302019-07-01 13:05:28 -0400411 // Important: check the original src color type here!
412 if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400413 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
414 }
415 } else {
Greg Danield2ccbb52020-02-05 10:45:39 -0500416 fp = GrTextureEffect::Make(std::move(tempView), alphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400417 }
418 if (!fp) {
419 return false;
420 }
421 GrPaint paint;
422 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
423 paint.addColorFragmentProcessor(std::move(fp));
424 this->asRenderTargetContext()->fillRectToRect(
425 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400426 SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()),
427 SkRect::MakeWH(srcInfo.width(), srcInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400428 } else {
Brian Salomon1d435302019-07-01 13:05:28 -0400429 SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height());
430 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Greg Daniel573312e2020-02-07 17:22:35 -0500431 if (!this->copy(tempProxy.get(), tempOrigin, srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400432 return false;
433 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400434 }
435 return true;
436 }
437
Brian Salomon1d435302019-07-01 13:05:28 -0400438 GrColorType allowedColorType =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400439 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400440 dstProxy->backendFormat(),
441 srcInfo.colorType()).fColorType;
Greg Danielb8d84f82020-02-13 14:25:00 -0500442 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon1047a492019-07-02 12:25:21 -0400443 bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes;
444 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomon1d435302019-07-01 13:05:28 -0400445 (srcInfo.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400446
Brian Salomonf30b1c12019-06-20 12:25:02 -0400447 std::unique_ptr<char[]> tmpPixels;
Brian Salomon1d435302019-07-01 13:05:28 -0400448 GrColorType srcColorType = srcInfo.colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400449 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400450 GrImageInfo tmpInfo(allowedColorType, this->colorInfo().alphaType(),
451 this->colorInfo().refColorSpace(), srcInfo.width(), srcInfo.height());
Brian Salomon1d435302019-07-01 13:05:28 -0400452 auto tmpRB = tmpInfo.minRowBytes();
453 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400454
Brian Salomon1d435302019-07-01 13:05:28 -0400455 GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400456
Brian Salomon1d435302019-07-01 13:05:28 -0400457 srcColorType = tmpInfo.colorType();
458 rowBytes = tmpRB;
459 src = tmpPixels.get();
460 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400461 }
462
463 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
464 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
465 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
466 // destination proxy)
467 // TODO: should this policy decision just be moved into the drawing manager?
Greg Daniel55f040b2020-02-13 15:38:32 +0000468 direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400469
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400470 return direct->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(),
471 srcInfo.height(), this->colorInfo().colorType(),
472 srcColorType, src, rowBytes);
Brian Osman45580d32016-11-23 09:37:01 -0500473}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400474
Greg Daniel573312e2020-02-07 17:22:35 -0500475bool GrSurfaceContext::copy(GrSurfaceProxy* src, GrSurfaceOrigin origin, const SkIRect& srcRect,
476 const SkIPoint& dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400477 ASSERT_SINGLE_OWNER
478 RETURN_FALSE_IF_ABANDONED
479 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -0400480 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -0400481
Brian Salomon947efe22019-07-16 15:36:11 -0400482 const GrCaps* caps = fContext->priv().caps();
483
Greg Daniel46cfbc62019-06-07 11:43:30 -0400484 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -0500485 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel573312e2020-02-07 17:22:35 -0500486 SkASSERT(origin == this->origin());
Greg Daniel46cfbc62019-06-07 11:43:30 -0400487
Stephen White3c0a50f2020-01-16 18:19:54 -0500488 if (this->asSurfaceProxy()->framebufferOnly()) {
489 return false;
490 }
491
Chris Daltonf8e5aad2019-08-02 12:55:23 -0600492 if (!caps->canCopySurface(this->asSurfaceProxy(), src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -0400493 return false;
494 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400495
Greg Daniel16f5c652019-10-29 11:26:01 -0400496 // The swizzle doesn't matter for copies and it is not used.
497 return this->drawingManager()->newCopyRenderTask(
Greg Daniel573312e2020-02-07 17:22:35 -0500498 GrSurfaceProxyView(sk_ref_sp(src), origin, GrSwizzle()), srcRect,
Greg Daniel46e366a2019-12-16 14:38:36 -0500499 this->readSurfaceView(), dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400500}
Greg Daniel46cfbc62019-06-07 11:43:30 -0400501
Brian Salomonbf6b9792019-08-21 09:38:10 -0400502std::unique_ptr<GrRenderTargetContext> GrSurfaceContext::rescale(
503 const SkImageInfo& info,
504 const SkIRect& srcRect,
505 SkSurface::RescaleGamma rescaleGamma,
506 SkFilterQuality rescaleQuality) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400507 auto direct = fContext->priv().asDirectContext();
508 if (!direct) {
509 return nullptr;
510 }
511 auto rtProxy = this->asRenderTargetProxy();
512 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
513 return nullptr;
514 }
515
Stephen White3c0a50f2020-01-16 18:19:54 -0500516 if (this->asSurfaceProxy()->framebufferOnly()) {
517 return nullptr;
518 }
519
Brian Salomone9ad9982019-07-22 16:17:41 -0400520 // We rescale by drawing and don't currently support drawing to a kUnpremul destination.
521 if (info.alphaType() == kUnpremul_SkAlphaType) {
522 return nullptr;
523 }
524
525 int srcW = srcRect.width();
526 int srcH = srcRect.height();
527 int srcX = srcRect.fLeft;
528 int srcY = srcRect.fTop;
Greg Daniel40903af2020-01-30 14:55:05 -0500529 GrSurfaceProxyView texView = this->readSurfaceView();
Brian Salomone9ad9982019-07-22 16:17:41 -0400530 SkCanvas::SrcRectConstraint constraint = SkCanvas::kStrict_SrcRectConstraint;
Greg Danielc594e622019-10-15 14:01:49 -0400531 GrColorType srcColorType = this->colorInfo().colorType();
Brian Salomonfc118442019-11-22 19:09:27 -0500532 SkAlphaType srcAlphaType = this->colorInfo().alphaType();
Greg Daniel40903af2020-01-30 14:55:05 -0500533 if (!texView.asTextureProxy()) {
534 texView = GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), this->origin(),
535 srcColorType, GrMipMapped::kNo, srcRect,
536 SkBackingFit::kApprox, SkBudgeted::kNo);
537 if (!texView.proxy()) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400538 return nullptr;
539 }
Greg Daniel40903af2020-01-30 14:55:05 -0500540 SkASSERT(texView.asTextureProxy());
Brian Salomone9ad9982019-07-22 16:17:41 -0400541 srcX = 0;
542 srcY = 0;
543 constraint = SkCanvas::kFast_SrcRectConstraint;
544 }
545
546 float sx = (float)info.width() / srcW;
547 float sy = (float)info.height() / srcH;
548
549 // How many bilerp/bicubic steps to do in X and Y. + means upscaling, - means downscaling.
550 int stepsX;
551 int stepsY;
552 if (rescaleQuality > kNone_SkFilterQuality) {
553 stepsX = static_cast<int>((sx > 1.f) ? ceil(log2f(sx)) : floor(log2f(sx)));
554 stepsY = static_cast<int>((sy > 1.f) ? ceil(log2f(sy)) : floor(log2f(sy)));
555 } else {
556 stepsX = sx != 1.f;
557 stepsY = sy != 1.f;
558 }
559 SkASSERT(stepsX || stepsY);
Greg Danielc594e622019-10-15 14:01:49 -0400560
Brian Salomonbf6b9792019-08-21 09:38:10 -0400561 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
562 // pass B is moved to A. If 'this' is the input on the first pass then tempA is null.
563 std::unique_ptr<GrRenderTargetContext> tempA;
564 std::unique_ptr<GrRenderTargetContext> tempB;
565
Brian Salomone9ad9982019-07-22 16:17:41 -0400566 // Assume we should ignore the rescale linear request if the surface has no color space since
567 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400568 if (rescaleGamma == SkSurface::kLinear && this->colorInfo().colorSpace() &&
569 !this->colorInfo().colorSpace()->gammaIsLinear()) {
570 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomonfc118442019-11-22 19:09:27 -0500571 auto xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(), srcAlphaType, cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -0400572 kPremul_SkAlphaType);
573 // We'll fall back to kRGBA_8888 if half float not supported.
Greg Daniele20fcad2020-01-08 11:52:34 -0500574 auto linearRTC = GrRenderTargetContext::MakeWithFallback(
575 fContext, GrColorType::kRGBA_F16, cs, SkBackingFit::kExact, {srcW, srcH}, 1,
576 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomone9ad9982019-07-22 16:17:41 -0400577 if (!linearRTC) {
578 return nullptr;
579 }
Greg Daniel40903af2020-01-30 14:55:05 -0500580 linearRTC->drawTexture(GrNoClip(), std::move(texView), srcAlphaType,
Brian Salomonfc118442019-11-22 19:09:27 -0500581 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
582 SK_PMColor4fWHITE, SkRect::Make(srcRect), SkRect::MakeWH(srcW, srcH),
583 GrAA::kNo, GrQuadAAFlags::kNone, constraint, SkMatrix::I(),
584 std::move(xform));
Greg Daniel40903af2020-01-30 14:55:05 -0500585 texView = linearRTC->readSurfaceView();
586 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -0400587 tempA = std::move(linearRTC);
Brian Salomone9ad9982019-07-22 16:17:41 -0400588 srcX = 0;
589 srcY = 0;
590 constraint = SkCanvas::kFast_SrcRectConstraint;
591 }
592 while (stepsX || stepsY) {
593 int nextW = info.width();
594 int nextH = info.height();
595 if (stepsX < 0) {
596 nextW = info.width() << (-stepsX - 1);
597 stepsX++;
598 } else if (stepsX != 0) {
599 if (stepsX > 1) {
600 nextW = srcW * 2;
601 }
602 --stepsX;
603 }
604 if (stepsY < 0) {
605 nextH = info.height() << (-stepsY - 1);
606 stepsY++;
607 } else if (stepsY != 0) {
608 if (stepsY > 1) {
609 nextH = srcH * 2;
610 }
611 --stepsY;
612 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400613 auto input = tempA ? tempA.get() : this;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400614 GrColorType colorType = input->colorInfo().colorType();
615 auto cs = input->colorInfo().refColorSpace();
Brian Salomone9ad9982019-07-22 16:17:41 -0400616 sk_sp<GrColorSpaceXform> xform;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400617 auto prevAlphaType = input->colorInfo().alphaType();
Brian Salomone9ad9982019-07-22 16:17:41 -0400618 if (!stepsX && !stepsY) {
619 // Might as well fold conversion to final info in the last step.
620 cs = info.refColorSpace();
621 colorType = SkColorTypeToGrColorType(info.colorType());
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400622 xform = GrColorSpaceXform::Make(input->colorInfo().colorSpace(),
623 input->colorInfo().alphaType(), cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -0400624 info.alphaType());
625 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500626 tempB = GrRenderTargetContext::MakeWithFallback(
627 fContext, colorType, std::move(cs), SkBackingFit::kExact, {nextW, nextH}, 1,
628 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400629 if (!tempB) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400630 return nullptr;
631 }
632 auto dstRect = SkRect::MakeWH(nextW, nextH);
633 if (rescaleQuality == kHigh_SkFilterQuality) {
634 SkMatrix matrix;
635 matrix.setScaleTranslate((float)srcW / nextW, (float)srcH / nextH, srcX, srcY);
636 std::unique_ptr<GrFragmentProcessor> fp;
637 auto dir = GrBicubicEffect::Direction::kXY;
638 if (nextW == srcW) {
639 dir = GrBicubicEffect::Direction::kY;
640 } else if (nextH == srcH) {
641 dir = GrBicubicEffect::Direction::kX;
642 }
Brian Salomond0d033a2020-02-18 16:59:28 -0500643 static constexpr GrSamplerState::WrapMode kWM = GrSamplerState::WrapMode::kClamp;
644 auto subset = SkRect::MakeXYWH(srcX, srcY, srcW, srcH);
645 fp = GrBicubicEffect::MakeSubset(std::move(texView), prevAlphaType, matrix, kWM, kWM,
646 subset, dir, *this->caps());
Brian Salomone9ad9982019-07-22 16:17:41 -0400647 if (xform) {
648 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
649 }
650 GrPaint paint;
651 paint.addColorFragmentProcessor(std::move(fp));
652 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400653 tempB->fillRectToRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect,
Brian Salomone9ad9982019-07-22 16:17:41 -0400654 dstRect);
655 } else {
656 auto filter = rescaleQuality == kNone_SkFilterQuality ? GrSamplerState::Filter::kNearest
657 : GrSamplerState::Filter::kBilerp;
658 auto srcSubset = SkRect::MakeXYWH(srcX, srcY, srcW, srcH);
Greg Daniel40903af2020-01-30 14:55:05 -0500659 tempB->drawTexture(GrNoClip(), std::move(texView), srcAlphaType, filter,
Brian Salomonfc118442019-11-22 19:09:27 -0500660 SkBlendMode::kSrc, SK_PMColor4fWHITE, srcSubset, dstRect, GrAA::kNo,
Greg Danielc594e622019-10-15 14:01:49 -0400661 GrQuadAAFlags::kNone, constraint, SkMatrix::I(), std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -0400662 }
Greg Daniel40903af2020-01-30 14:55:05 -0500663 texView = tempB->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -0400664 tempA = std::move(tempB);
Brian Salomone9ad9982019-07-22 16:17:41 -0400665 srcX = srcY = 0;
666 srcW = nextW;
667 srcH = nextH;
668 constraint = SkCanvas::kFast_SrcRectConstraint;
669 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400670 SkASSERT(tempA);
671 return tempA;
Brian Salomone9ad9982019-07-22 16:17:41 -0400672}
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400673
674GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
675 const SkIRect& rect) {
676 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
677 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
678 auto direct = fContext->priv().asDirectContext();
679 if (!direct) {
680 return {};
681 }
682 auto rtProxy = this->asRenderTargetProxy();
683 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
684 return {};
685 }
686
687 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400688 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
689 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400690 // Fail if read color type does not have all of dstCT's color channels and those missing color
691 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -0400692 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
693 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
694 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
695 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400696 return {};
697 }
698
Brian Salomonfb28c6f2020-01-10 13:04:45 -0500699 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400700 !supportedRead.fOffsetAlignmentForTransferBuffer) {
701 return {};
702 }
703
704 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
705 size_t size = rowBytes * rect.height();
706 auto buffer = direct->priv().resourceProvider()->createBuffer(
707 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
708 if (!buffer) {
709 return {};
710 }
711 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -0500712 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400713 if (flip) {
714 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
715 this->height() - rect.fTop);
716 }
Greg Danielbbfec9d2019-08-20 10:56:51 -0400717 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400718 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -0400719 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400720 PixelTransferResult result;
721 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400722 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -0400723 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400724 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
725 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400726 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
727 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400728 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
729 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -0400730 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400731 };
732 }
733 return result;
734}
Greg Daniel46e366a2019-12-16 14:38:36 -0500735
736#ifdef SK_DEBUG
737void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500738 SkASSERT(fReadView.proxy());
739 fReadView.proxy()->validate(fContext);
Greg Daniel46e366a2019-12-16 14:38:36 -0500740 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
Greg Daniel3912a4b2020-01-14 09:56:04 -0500741 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
Greg Daniel46e366a2019-12-16 14:38:36 -0500742
743 this->onValidate();
744}
745#endif
746