blob: a43b9cbab7bb1983845cba9a4ed56436a1557a03 [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);
Brian Salomon8afde5f2020-04-01 16:22:00 -040048 // Will we ever want a swizzle that is not the default write swizzle for the format and
Greg Danielbfa19c42019-12-19 16:41:40 -050049 // colorType here? If so we will need to manually pass that in.
Brian Salomon8afde5f2020-04-01 16:22:00 -040050 GrSwizzle writeSwizzle =
Brian Salomon982f5462020-03-30 12:52:33 -040051 context->priv().caps()->getWriteSwizzle(proxy->backendFormat(), colorType);
Brian Salomon8afde5f2020-04-01 16:22:00 -040052 GrSurfaceProxyView writeView(readView.refProxy(), readView.origin(), writeSwizzle);
Greg Daniel3912a4b2020-01-14 09:56:04 -050053 surfaceContext.reset(new GrRenderTargetContext(context, std::move(readView),
Brian Salomon8afde5f2020-04-01 16:22:00 -040054 std::move(writeView), 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 Salomone8d20802020-04-01 09:10:38 -040077 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(format, colorType);
Greg Daniel47c20e82020-01-21 14:29:57 -050078
Greg Danielbfa19c42019-12-19 16:41:40 -050079 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -040080 format, dimensions, renderable, renderTargetSampleCnt, mipMapped, fit, budgeted,
81 isProtected);
Greg Danielbfa19c42019-12-19 16:41:40 -050082 if (!proxy) {
83 return nullptr;
84 }
85
Greg Daniel3912a4b2020-01-14 09:56:04 -050086 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
87 return GrSurfaceContext::Make(context, std::move(view), colorType, alphaType,
Greg Danielbfa19c42019-12-19 16:41:40 -050088 std::move(colorSpace));
89}
90
Greg Danielf41b2bd2019-08-22 16:19:24 -040091// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
92// GrOpsTasks to be picked up and added to by renderTargetContexts lower in the call
93// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
94// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -050095GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -050096 GrSurfaceProxyView readView,
Brian Salomond6287472019-06-24 15:50:07 -040097 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -040098 SkAlphaType alphaType,
Greg Daniel3912a4b2020-01-14 09:56:04 -050099 sk_sp<SkColorSpace> colorSpace)
Greg Daniel901b98e2019-10-22 09:54:02 -0400100 : fContext(context)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500101 , fReadView(std::move(readView))
102 , fColorInfo(colorType, alphaType, std::move(colorSpace)) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500103 SkASSERT(!context->priv().abandoned());
104}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400105
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400106const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); }
107
Robert Phillips0d075de2019-03-04 11:08:13 -0500108GrAuditTrail* GrSurfaceContext::auditTrail() {
109 return fContext->priv().auditTrail();
110}
111
112GrDrawingManager* GrSurfaceContext::drawingManager() {
113 return fContext->priv().drawingManager();
114}
115
116const GrDrawingManager* GrSurfaceContext::drawingManager() const {
117 return fContext->priv().drawingManager();
118}
119
120#ifdef SK_DEBUG
121GrSingleOwner* GrSurfaceContext::singleOwner() {
122 return fContext->priv().singleOwner();
123}
124#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400125
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400126bool GrSurfaceContext::readPixels(const GrImageInfo& origDstInfo, void* dst, size_t rowBytes,
Brian Salomon1d435302019-07-01 13:05:28 -0400127 SkIPoint pt, GrContext* direct) {
128 ASSERT_SINGLE_OWNER
129 RETURN_FALSE_IF_ABANDONED
130 SkDEBUGCODE(this->validate();)
131 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Greg Daniel6eb8c242019-06-05 10:22:24 -0400132
Brian Salomon1d435302019-07-01 13:05:28 -0400133 if (!direct && !(direct = fContext->priv().asDirectContext())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400134 return false;
135 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400136
Brian Salomon1d435302019-07-01 13:05:28 -0400137 if (!dst) {
138 return false;
139 }
140
Brian Salomon1047a492019-07-02 12:25:21 -0400141 size_t tightRowBytes = origDstInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400142 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400143 rowBytes = tightRowBytes;
144 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400145 return false;
146 }
147
148 if (!origDstInfo.isValid()) {
149 return false;
150 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400151
152 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
153
Stephen White3c0a50f2020-01-16 18:19:54 -0500154 if (srcProxy->framebufferOnly()) {
155 return false;
156 }
157
Greg Daniel6eb8c242019-06-05 10:22:24 -0400158 // MDB TODO: delay this instantiation until later in the method
159 if (!srcProxy->instantiate(direct->priv().resourceProvider())) {
160 return false;
161 }
162
163 GrSurface* srcSurface = srcProxy->peekSurface();
164
Brian Salomon1d435302019-07-01 13:05:28 -0400165 auto dstInfo = origDstInfo;
166 if (!dstInfo.clip(this->width(), this->height(), &pt, &dst, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400167 return false;
168 }
Brian Salomon1047a492019-07-02 12:25:21 -0400169 // Our tight row bytes may have been changed by clipping.
170 tightRowBytes = dstInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400171
Mike Klein7321e6a2019-12-03 11:08:40 -0600172 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{this->colorInfo(), dstInfo}.flags;
173 bool unpremul = flags.unpremul,
174 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
175 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400176
177 const GrCaps* caps = direct->priv().caps();
Robert Phillips07f0e412020-01-17 15:20:00 -0500178 bool srcIsCompressed = caps->isFormatCompressed(srcSurface->backendFormat());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400179 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
180 // care so much about getImageData performance. However, in order to ensure putImageData/
181 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
182 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
183 // fContext->vaildaPMUPMConversionExists()).
Greg Daniel0258c902019-08-01 13:08:33 -0400184 GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
185 GrRenderable::kYes);
Greg Danielc71c7962020-01-14 16:44:18 -0500186 GrColorType srcColorType = this->colorInfo().colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400187 bool canvas2DFastPath = unpremul && !needColorConversion &&
188 (GrColorType::kRGBA_8888 == dstInfo.colorType() ||
189 GrColorType::kBGRA_8888 == dstInfo.colorType()) &&
190 SkToBool(srcProxy->asTextureProxy()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500191 (srcColorType == GrColorType::kRGBA_8888 ||
192 srcColorType == GrColorType::kBGRA_8888) &&
Greg Daniel0258c902019-08-01 13:08:33 -0400193 defaultRGBAFormat.isValid() &&
Brian Salomon1d435302019-07-01 13:05:28 -0400194 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400195
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400196 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400197 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400198 return false;
199 }
200
Brian Salomondc0710f2019-07-01 14:59:32 -0400201 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Robert Phillips07f0e412020-01-17 15:20:00 -0500202 GrColorType colorType = (canvas2DFastPath || srcIsCompressed)
203 ? GrColorType::kRGBA_8888 : this->colorInfo().colorType();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400204 sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorInfo().refColorSpace();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400205
Greg Daniele20fcad2020-01-08 11:52:34 -0500206 auto tempCtx = GrRenderTargetContext::Make(
207 direct, colorType, std::move(cs), SkBackingFit::kApprox, dstInfo.dimensions(),
208 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400209 if (!tempCtx) {
210 return false;
211 }
212
213 std::unique_ptr<GrFragmentProcessor> fp;
214 if (canvas2DFastPath) {
Greg Danield2ccbb52020-02-05 10:45:39 -0500215 fp = direct->priv().createPMToUPMEffect(
216 GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType()));
Brian Salomon1d435302019-07-01 13:05:28 -0400217 if (dstInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400218 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
Brian Salomon1d435302019-07-01 13:05:28 -0400219 dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400220 }
Brian Salomon1d435302019-07-01 13:05:28 -0400221 // The render target context is incorrectly tagged as kPremul even though we're writing
222 // unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type so we don't
223 // double unpremul.
224 dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400225 } else {
Greg Danield2ccbb52020-02-05 10:45:39 -0500226 fp = GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400227 }
228 if (!fp) {
229 return false;
230 }
231 GrPaint paint;
232 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
233 paint.addColorFragmentProcessor(std::move(fp));
234
235 tempCtx->asRenderTargetContext()->fillRectToRect(
236 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400237 SkRect::MakeWH(dstInfo.width(), dstInfo.height()),
238 SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400239
Brian Salomon1d435302019-07-01 13:05:28 -0400240 return tempCtx->readPixels(dstInfo, dst, rowBytes, {0, 0}, direct);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400241 }
242
Greg Danielb8d84f82020-02-13 14:25:00 -0500243 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400244
Brian Salomon1d435302019-07-01 13:05:28 -0400245 auto supportedRead = caps->supportedReadPixelsColorType(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400246 this->colorInfo().colorType(), srcProxy->backendFormat(), dstInfo.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400247
Brian Salomon1047a492019-07-02 12:25:21 -0400248 bool makeTight = !caps->readPixelsRowBytesSupport() && tightRowBytes != rowBytes;
249
250 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomon8f8354a2019-07-31 20:12:02 -0400251 (dstInfo.colorType() != supportedRead.fColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400252
Brian Salomonf30b1c12019-06-20 12:25:02 -0400253 std::unique_ptr<char[]> tmpPixels;
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400254 GrImageInfo tmpInfo;
Brian Salomon1d435302019-07-01 13:05:28 -0400255 void* readDst = dst;
256 size_t readRB = rowBytes;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400257 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400258 tmpInfo = {supportedRead.fColorType, this->colorInfo().alphaType(),
259 this->colorInfo().refColorSpace(), dstInfo.width(), dstInfo.height()};
Brian Salomon1d435302019-07-01 13:05:28 -0400260 size_t tmpRB = tmpInfo.minRowBytes();
261 size_t size = tmpRB * tmpInfo.height();
262 // Chrome MSAN bots require the data to be initialized (hence the ()).
263 tmpPixels.reset(new char[size]());
Brian Salomonf30b1c12019-06-20 12:25:02 -0400264
Brian Salomonf30b1c12019-06-20 12:25:02 -0400265 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400266 readRB = tmpRB;
267 pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400268 }
269
Greg Daniel55f040b2020-02-13 15:38:32 +0000270 direct->priv().flushSurface(srcProxy);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400271
Brian Salomon1d435302019-07-01 13:05:28 -0400272 if (!direct->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400273 dstInfo.height(), this->colorInfo().colorType(),
Brian Salomonf77c1462019-08-01 15:19:29 -0400274 supportedRead.fColorType, readDst, readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400275 return false;
276 }
277
Greg Daniel6eb8c242019-06-05 10:22:24 -0400278 if (convert) {
Brian Salomon8f8354a2019-07-31 20:12:02 -0400279 return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400280 }
281 return true;
282}
Robert Phillips0d075de2019-03-04 11:08:13 -0500283
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400284bool GrSurfaceContext::writePixels(const GrImageInfo& origSrcInfo, const void* src, size_t rowBytes,
Brian Salomon1d435302019-07-01 13:05:28 -0400285 SkIPoint pt, GrContext* direct) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400286 ASSERT_SINGLE_OWNER
287 RETURN_FALSE_IF_ABANDONED
288 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400289 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400290
Brian Salomon1d435302019-07-01 13:05:28 -0400291 if (!direct && !(direct = fContext->priv().asDirectContext())) {
Brian Salomonc320b152018-02-20 14:05:36 -0500292 return false;
293 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500294
Brian Salomon1d435302019-07-01 13:05:28 -0400295 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500296 return false;
297 }
298
Brian Salomon1d435302019-07-01 13:05:28 -0400299 if (!src) {
300 return false;
301 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400302
Brian Salomon1047a492019-07-02 12:25:21 -0400303 size_t tightRowBytes = origSrcInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400304 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400305 rowBytes = tightRowBytes;
306 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400307 return false;
308 }
309
310 if (!origSrcInfo.isValid()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400311 return false;
312 }
313
314 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
Stephen White3c0a50f2020-01-16 18:19:54 -0500315
316 if (dstProxy->framebufferOnly()) {
317 return false;
318 }
319
Greg Daniel6eb8c242019-06-05 10:22:24 -0400320 if (!dstProxy->instantiate(direct->priv().resourceProvider())) {
321 return false;
322 }
323
324 GrSurface* dstSurface = dstProxy->peekSurface();
325
Brian Salomon1d435302019-07-01 13:05:28 -0400326 auto srcInfo = origSrcInfo;
327 if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400328 return false;
329 }
Brian Salomon1047a492019-07-02 12:25:21 -0400330 // Our tight row bytes may have been changed by clipping.
331 tightRowBytes = srcInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400332
Mike Klein7321e6a2019-12-03 11:08:40 -0600333 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{srcInfo, this->colorInfo()}.flags;
334 bool unpremul = flags.unpremul,
335 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
336 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400337
338 const GrCaps* caps = direct->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400339
340 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
341 GrRenderable::kNo);
342
Greg Danielc71c7962020-01-14 16:44:18 -0500343 GrColorType dstColorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400344 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
345 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400346 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
347 (srcInfo.colorType() == GrColorType::kRGBA_8888 ||
348 srcInfo.colorType() == GrColorType::kBGRA_8888) &&
349 SkToBool(this->asRenderTargetContext()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500350 (dstColorType == GrColorType::kRGBA_8888 ||
351 dstColorType == GrColorType::kBGRA_8888) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400352 rgbaDefaultFormat.isValid() &&
Brian Salomon1d435302019-07-01 13:05:28 -0400353 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400354
355 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
Brian Salomond6287472019-06-24 15:50:07 -0400356 GrColorType colorType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400357
358 GrBackendFormat format;
Brian Salomone7499c72019-06-24 12:12:36 -0400359 SkAlphaType alphaType;
Greg Danielbfa19c42019-12-19 16:41:40 -0500360 GrSwizzle tempReadSwizzle;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400361 if (canvas2DFastPath) {
Brian Salomond6287472019-06-24 15:50:07 -0400362 colorType = GrColorType::kRGBA_8888;
Greg Daniel7bfc9132019-08-14 14:23:53 -0400363 format = rgbaDefaultFormat;
Brian Salomone7499c72019-06-24 12:12:36 -0400364 alphaType = kUnpremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400365 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400366 colorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400367 format = dstProxy->backendFormat().makeTexture2D();
368 if (!format.isValid()) {
369 return false;
370 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400371 alphaType = this->colorInfo().alphaType();
Greg Danielbfa19c42019-12-19 16:41:40 -0500372 tempReadSwizzle = this->readSwizzle();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400373 }
374
Greg Daniel2e52ad12019-06-13 10:04:16 -0400375 // It is more efficient for us to write pixels into a top left origin so we prefer that.
376 // However, if the final proxy isn't a render target then we must use a copy to move the
377 // data into it which requires the origins to match. If the final proxy is a render target
378 // we can use a draw instead which doesn't have this origin restriction. Thus for render
379 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400380 GrSurfaceOrigin tempOrigin =
Greg Danielb8d84f82020-02-13 14:25:00 -0500381 this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : this->origin();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400382 auto tempProxy = direct->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400383 format, srcInfo.dimensions(), GrRenderable::kNo, 1, GrMipMapped::kNo,
384 SkBackingFit::kApprox, SkBudgeted::kYes, GrProtected::kNo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400385 if (!tempProxy) {
386 return false;
387 }
Greg Daniel3912a4b2020-01-14 09:56:04 -0500388 GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle);
Greg Danield2ccbb52020-02-05 10:45:39 -0500389 GrSurfaceContext tempCtx(direct, tempView, colorType, alphaType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500390 this->colorInfo().refColorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400391
392 // In the fast path we always write the srcData to the temp context as though it were RGBA.
393 // When the data is really BGRA the write will cause the R and B channels to be swapped in
394 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
395 // dst.
Brian Salomon1d435302019-07-01 13:05:28 -0400396 if (canvas2DFastPath) {
397 srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888);
398 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500399 if (!tempCtx.writePixels(srcInfo, src, rowBytes, {0, 0}, direct)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400400 return false;
401 }
402
403 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400404 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400405 if (canvas2DFastPath) {
Brian Salomonb8f098d2020-01-07 11:15:44 -0500406 fp = direct->priv().createUPMToPMEffect(
Greg Danield2ccbb52020-02-05 10:45:39 -0500407 GrTextureEffect::Make(std::move(tempView), alphaType));
Brian Salomon1d435302019-07-01 13:05:28 -0400408 // Important: check the original src color type here!
409 if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400410 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
411 }
412 } else {
Greg Danield2ccbb52020-02-05 10:45:39 -0500413 fp = GrTextureEffect::Make(std::move(tempView), alphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400414 }
415 if (!fp) {
416 return false;
417 }
418 GrPaint paint;
419 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
420 paint.addColorFragmentProcessor(std::move(fp));
421 this->asRenderTargetContext()->fillRectToRect(
422 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400423 SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()),
424 SkRect::MakeWH(srcInfo.width(), srcInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400425 } else {
Brian Salomon1d435302019-07-01 13:05:28 -0400426 SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height());
427 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Greg Daniel573312e2020-02-07 17:22:35 -0500428 if (!this->copy(tempProxy.get(), tempOrigin, srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400429 return false;
430 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400431 }
432 return true;
433 }
434
Brian Salomon1d435302019-07-01 13:05:28 -0400435 GrColorType allowedColorType =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400436 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400437 dstProxy->backendFormat(),
438 srcInfo.colorType()).fColorType;
Greg Danielb8d84f82020-02-13 14:25:00 -0500439 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon1047a492019-07-02 12:25:21 -0400440 bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes;
441 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomon1d435302019-07-01 13:05:28 -0400442 (srcInfo.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400443
Brian Salomonf30b1c12019-06-20 12:25:02 -0400444 std::unique_ptr<char[]> tmpPixels;
Brian Salomon1d435302019-07-01 13:05:28 -0400445 GrColorType srcColorType = srcInfo.colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400446 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400447 GrImageInfo tmpInfo(allowedColorType, this->colorInfo().alphaType(),
448 this->colorInfo().refColorSpace(), srcInfo.width(), srcInfo.height());
Brian Salomon1d435302019-07-01 13:05:28 -0400449 auto tmpRB = tmpInfo.minRowBytes();
450 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400451
Brian Salomon1d435302019-07-01 13:05:28 -0400452 GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400453
Brian Salomon1d435302019-07-01 13:05:28 -0400454 srcColorType = tmpInfo.colorType();
455 rowBytes = tmpRB;
456 src = tmpPixels.get();
457 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400458 }
459
460 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
461 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
462 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
463 // destination proxy)
464 // TODO: should this policy decision just be moved into the drawing manager?
Greg Daniel55f040b2020-02-13 15:38:32 +0000465 direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400466
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400467 return direct->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(),
468 srcInfo.height(), this->colorInfo().colorType(),
469 srcColorType, src, rowBytes);
Brian Osman45580d32016-11-23 09:37:01 -0500470}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400471
Greg Daniel573312e2020-02-07 17:22:35 -0500472bool GrSurfaceContext::copy(GrSurfaceProxy* src, GrSurfaceOrigin origin, const SkIRect& srcRect,
473 const SkIPoint& dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400474 ASSERT_SINGLE_OWNER
475 RETURN_FALSE_IF_ABANDONED
476 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -0400477 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -0400478
Brian Salomon947efe22019-07-16 15:36:11 -0400479 const GrCaps* caps = fContext->priv().caps();
480
Greg Daniel46cfbc62019-06-07 11:43:30 -0400481 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -0500482 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel573312e2020-02-07 17:22:35 -0500483 SkASSERT(origin == this->origin());
Greg Daniel46cfbc62019-06-07 11:43:30 -0400484
Stephen White3c0a50f2020-01-16 18:19:54 -0500485 if (this->asSurfaceProxy()->framebufferOnly()) {
486 return false;
487 }
488
Chris Daltonf8e5aad2019-08-02 12:55:23 -0600489 if (!caps->canCopySurface(this->asSurfaceProxy(), src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -0400490 return false;
491 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400492
Greg Daniel16f5c652019-10-29 11:26:01 -0400493 // The swizzle doesn't matter for copies and it is not used.
494 return this->drawingManager()->newCopyRenderTask(
Greg Daniel573312e2020-02-07 17:22:35 -0500495 GrSurfaceProxyView(sk_ref_sp(src), origin, GrSwizzle()), srcRect,
Greg Daniel46e366a2019-12-16 14:38:36 -0500496 this->readSurfaceView(), dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400497}
Greg Daniel46cfbc62019-06-07 11:43:30 -0400498
Brian Salomonbf6b9792019-08-21 09:38:10 -0400499std::unique_ptr<GrRenderTargetContext> GrSurfaceContext::rescale(
500 const SkImageInfo& info,
501 const SkIRect& srcRect,
502 SkSurface::RescaleGamma rescaleGamma,
503 SkFilterQuality rescaleQuality) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400504 auto direct = fContext->priv().asDirectContext();
505 if (!direct) {
506 return nullptr;
507 }
508 auto rtProxy = this->asRenderTargetProxy();
509 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
510 return nullptr;
511 }
512
Stephen White3c0a50f2020-01-16 18:19:54 -0500513 if (this->asSurfaceProxy()->framebufferOnly()) {
514 return nullptr;
515 }
516
Brian Salomone9ad9982019-07-22 16:17:41 -0400517 // We rescale by drawing and don't currently support drawing to a kUnpremul destination.
518 if (info.alphaType() == kUnpremul_SkAlphaType) {
519 return nullptr;
520 }
521
522 int srcW = srcRect.width();
523 int srcH = srcRect.height();
524 int srcX = srcRect.fLeft;
525 int srcY = srcRect.fTop;
Greg Daniel40903af2020-01-30 14:55:05 -0500526 GrSurfaceProxyView texView = this->readSurfaceView();
Brian Salomone9ad9982019-07-22 16:17:41 -0400527 SkCanvas::SrcRectConstraint constraint = SkCanvas::kStrict_SrcRectConstraint;
Greg Danielc594e622019-10-15 14:01:49 -0400528 GrColorType srcColorType = this->colorInfo().colorType();
Brian Salomonfc118442019-11-22 19:09:27 -0500529 SkAlphaType srcAlphaType = this->colorInfo().alphaType();
Greg Daniel40903af2020-01-30 14:55:05 -0500530 if (!texView.asTextureProxy()) {
531 texView = GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), this->origin(),
532 srcColorType, GrMipMapped::kNo, srcRect,
533 SkBackingFit::kApprox, SkBudgeted::kNo);
534 if (!texView.proxy()) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400535 return nullptr;
536 }
Greg Daniel40903af2020-01-30 14:55:05 -0500537 SkASSERT(texView.asTextureProxy());
Brian Salomone9ad9982019-07-22 16:17:41 -0400538 srcX = 0;
539 srcY = 0;
540 constraint = SkCanvas::kFast_SrcRectConstraint;
541 }
542
543 float sx = (float)info.width() / srcW;
544 float sy = (float)info.height() / srcH;
545
546 // How many bilerp/bicubic steps to do in X and Y. + means upscaling, - means downscaling.
547 int stepsX;
548 int stepsY;
549 if (rescaleQuality > kNone_SkFilterQuality) {
550 stepsX = static_cast<int>((sx > 1.f) ? ceil(log2f(sx)) : floor(log2f(sx)));
551 stepsY = static_cast<int>((sy > 1.f) ? ceil(log2f(sy)) : floor(log2f(sy)));
552 } else {
553 stepsX = sx != 1.f;
554 stepsY = sy != 1.f;
555 }
556 SkASSERT(stepsX || stepsY);
Greg Danielc594e622019-10-15 14:01:49 -0400557
Brian Salomonbf6b9792019-08-21 09:38:10 -0400558 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
559 // pass B is moved to A. If 'this' is the input on the first pass then tempA is null.
560 std::unique_ptr<GrRenderTargetContext> tempA;
561 std::unique_ptr<GrRenderTargetContext> tempB;
562
Brian Salomone9ad9982019-07-22 16:17:41 -0400563 // Assume we should ignore the rescale linear request if the surface has no color space since
564 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400565 if (rescaleGamma == SkSurface::kLinear && this->colorInfo().colorSpace() &&
566 !this->colorInfo().colorSpace()->gammaIsLinear()) {
567 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomonfc118442019-11-22 19:09:27 -0500568 auto xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(), srcAlphaType, cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -0400569 kPremul_SkAlphaType);
570 // We'll fall back to kRGBA_8888 if half float not supported.
Greg Daniele20fcad2020-01-08 11:52:34 -0500571 auto linearRTC = GrRenderTargetContext::MakeWithFallback(
572 fContext, GrColorType::kRGBA_F16, cs, SkBackingFit::kExact, {srcW, srcH}, 1,
573 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomone9ad9982019-07-22 16:17:41 -0400574 if (!linearRTC) {
575 return nullptr;
576 }
Greg Daniel40903af2020-01-30 14:55:05 -0500577 linearRTC->drawTexture(GrNoClip(), std::move(texView), srcAlphaType,
Brian Salomonfc118442019-11-22 19:09:27 -0500578 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
579 SK_PMColor4fWHITE, SkRect::Make(srcRect), SkRect::MakeWH(srcW, srcH),
580 GrAA::kNo, GrQuadAAFlags::kNone, constraint, SkMatrix::I(),
581 std::move(xform));
Greg Daniel40903af2020-01-30 14:55:05 -0500582 texView = linearRTC->readSurfaceView();
583 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -0400584 tempA = std::move(linearRTC);
Brian Salomone9ad9982019-07-22 16:17:41 -0400585 srcX = 0;
586 srcY = 0;
587 constraint = SkCanvas::kFast_SrcRectConstraint;
588 }
589 while (stepsX || stepsY) {
590 int nextW = info.width();
591 int nextH = info.height();
592 if (stepsX < 0) {
593 nextW = info.width() << (-stepsX - 1);
594 stepsX++;
595 } else if (stepsX != 0) {
596 if (stepsX > 1) {
597 nextW = srcW * 2;
598 }
599 --stepsX;
600 }
601 if (stepsY < 0) {
602 nextH = info.height() << (-stepsY - 1);
603 stepsY++;
604 } else if (stepsY != 0) {
605 if (stepsY > 1) {
606 nextH = srcH * 2;
607 }
608 --stepsY;
609 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400610 auto input = tempA ? tempA.get() : this;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400611 GrColorType colorType = input->colorInfo().colorType();
612 auto cs = input->colorInfo().refColorSpace();
Brian Salomone9ad9982019-07-22 16:17:41 -0400613 sk_sp<GrColorSpaceXform> xform;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400614 auto prevAlphaType = input->colorInfo().alphaType();
Brian Salomone9ad9982019-07-22 16:17:41 -0400615 if (!stepsX && !stepsY) {
616 // Might as well fold conversion to final info in the last step.
617 cs = info.refColorSpace();
618 colorType = SkColorTypeToGrColorType(info.colorType());
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400619 xform = GrColorSpaceXform::Make(input->colorInfo().colorSpace(),
620 input->colorInfo().alphaType(), cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -0400621 info.alphaType());
622 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500623 tempB = GrRenderTargetContext::MakeWithFallback(
624 fContext, colorType, std::move(cs), SkBackingFit::kExact, {nextW, nextH}, 1,
625 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400626 if (!tempB) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400627 return nullptr;
628 }
629 auto dstRect = SkRect::MakeWH(nextW, nextH);
630 if (rescaleQuality == kHigh_SkFilterQuality) {
631 SkMatrix matrix;
632 matrix.setScaleTranslate((float)srcW / nextW, (float)srcH / nextH, srcX, srcY);
633 std::unique_ptr<GrFragmentProcessor> fp;
634 auto dir = GrBicubicEffect::Direction::kXY;
635 if (nextW == srcW) {
636 dir = GrBicubicEffect::Direction::kY;
637 } else if (nextH == srcH) {
638 dir = GrBicubicEffect::Direction::kX;
639 }
Brian Salomond0d033a2020-02-18 16:59:28 -0500640 static constexpr GrSamplerState::WrapMode kWM = GrSamplerState::WrapMode::kClamp;
641 auto subset = SkRect::MakeXYWH(srcX, srcY, srcW, srcH);
642 fp = GrBicubicEffect::MakeSubset(std::move(texView), prevAlphaType, matrix, kWM, kWM,
643 subset, dir, *this->caps());
Brian Salomone9ad9982019-07-22 16:17:41 -0400644 if (xform) {
645 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
646 }
647 GrPaint paint;
648 paint.addColorFragmentProcessor(std::move(fp));
649 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400650 tempB->fillRectToRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect,
Brian Salomone9ad9982019-07-22 16:17:41 -0400651 dstRect);
652 } else {
653 auto filter = rescaleQuality == kNone_SkFilterQuality ? GrSamplerState::Filter::kNearest
654 : GrSamplerState::Filter::kBilerp;
655 auto srcSubset = SkRect::MakeXYWH(srcX, srcY, srcW, srcH);
Greg Daniel40903af2020-01-30 14:55:05 -0500656 tempB->drawTexture(GrNoClip(), std::move(texView), srcAlphaType, filter,
Brian Salomonfc118442019-11-22 19:09:27 -0500657 SkBlendMode::kSrc, SK_PMColor4fWHITE, srcSubset, dstRect, GrAA::kNo,
Greg Danielc594e622019-10-15 14:01:49 -0400658 GrQuadAAFlags::kNone, constraint, SkMatrix::I(), std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -0400659 }
Greg Daniel40903af2020-01-30 14:55:05 -0500660 texView = tempB->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -0400661 tempA = std::move(tempB);
Brian Salomone9ad9982019-07-22 16:17:41 -0400662 srcX = srcY = 0;
663 srcW = nextW;
664 srcH = nextH;
665 constraint = SkCanvas::kFast_SrcRectConstraint;
666 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400667 SkASSERT(tempA);
668 return tempA;
Brian Salomone9ad9982019-07-22 16:17:41 -0400669}
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400670
671GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
672 const SkIRect& rect) {
673 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
674 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
675 auto direct = fContext->priv().asDirectContext();
676 if (!direct) {
677 return {};
678 }
679 auto rtProxy = this->asRenderTargetProxy();
680 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
681 return {};
682 }
683
684 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400685 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
686 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400687 // Fail if read color type does not have all of dstCT's color channels and those missing color
688 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -0400689 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
690 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
691 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
692 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400693 return {};
694 }
695
Brian Salomonfb28c6f2020-01-10 13:04:45 -0500696 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400697 !supportedRead.fOffsetAlignmentForTransferBuffer) {
698 return {};
699 }
700
701 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
702 size_t size = rowBytes * rect.height();
703 auto buffer = direct->priv().resourceProvider()->createBuffer(
704 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
705 if (!buffer) {
706 return {};
707 }
708 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -0500709 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400710 if (flip) {
711 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
712 this->height() - rect.fTop);
713 }
Greg Danielbbfec9d2019-08-20 10:56:51 -0400714 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400715 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -0400716 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400717 PixelTransferResult result;
718 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400719 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -0400720 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400721 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
722 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400723 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
724 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400725 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
726 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -0400727 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400728 };
729 }
730 return result;
731}
Greg Daniel46e366a2019-12-16 14:38:36 -0500732
733#ifdef SK_DEBUG
734void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500735 SkASSERT(fReadView.proxy());
736 fReadView.proxy()->validate(fContext);
Greg Daniel46e366a2019-12-16 14:38:36 -0500737 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
Greg Daniel3912a4b2020-01-14 09:56:04 -0500738 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
Greg Daniel46e366a2019-12-16 14:38:36 -0500739
740 this->onValidate();
741}
742#endif
743