blob: e3b1e28cb1b3ae7f5bdc070d9763b831f84a682a [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrContextPriv.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040014#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrDrawingManager.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040016#include "src/gpu/GrGpu.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040017#include "src/gpu/GrImageInfo.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040018#include "src/gpu/GrProxyProvider.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
Adlai Holler33dbd652020-06-01 12:35:42 -040026#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
Robert Phillips69893702019-02-22 11:16:30 -050027#define RETURN_FALSE_IF_ABANDONED if (this->fContext->priv().abandoned()) { return false; }
Brian Osman45580d32016-11-23 09:37:01 -050028
Greg Danielbfa19c42019-12-19 16:41:40 -050029std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -050030 GrSurfaceProxyView readView,
Greg Danielbfa19c42019-12-19 16:41:40 -050031 GrColorType colorType,
32 SkAlphaType alphaType,
33 sk_sp<SkColorSpace> colorSpace) {
Greg Daniele20fcad2020-01-08 11:52:34 -050034 // It is probably not necessary to check if the context is abandoned here since uses of the
35 // GrSurfaceContext which need the context will mostly likely fail later on without an issue.
36 // However having this hear adds some reassurance in case there is a path doesn't handle an
37 // abandoned context correctly. It also lets us early out of some extra work.
38 if (context->priv().abandoned()) {
39 return nullptr;
40 }
Greg Daniel3912a4b2020-01-14 09:56:04 -050041 GrSurfaceProxy* proxy = readView.proxy();
Greg Danielbfa19c42019-12-19 16:41:40 -050042 SkASSERT(proxy && proxy->asTextureProxy());
43
Greg Danielbfa19c42019-12-19 16:41:40 -050044 std::unique_ptr<GrSurfaceContext> surfaceContext;
Greg Daniel3912a4b2020-01-14 09:56:04 -050045 if (proxy->asRenderTargetProxy()) {
Greg Danielbfa19c42019-12-19 16:41:40 -050046 SkASSERT(kPremul_SkAlphaType == alphaType || kOpaque_SkAlphaType == alphaType);
Brian Salomon8afde5f2020-04-01 16:22:00 -040047 // Will we ever want a swizzle that is not the default write swizzle for the format and
Greg Danielbfa19c42019-12-19 16:41:40 -050048 // colorType here? If so we will need to manually pass that in.
Brian Salomonc5243782020-04-02 12:50:34 -040049 GrSwizzle writeSwizzle;
50 if (colorType != GrColorType::kUnknown) {
51 writeSwizzle =
52 context->priv().caps()->getWriteSwizzle(proxy->backendFormat(), colorType);
53 }
Brian Salomon8afde5f2020-04-01 16:22:00 -040054 GrSurfaceProxyView writeView(readView.refProxy(), readView.origin(), writeSwizzle);
Greg Daniel3912a4b2020-01-14 09:56:04 -050055 surfaceContext.reset(new GrRenderTargetContext(context, std::move(readView),
Brian Salomon8afde5f2020-04-01 16:22:00 -040056 std::move(writeView), colorType,
Greg Danielbfa19c42019-12-19 16:41:40 -050057 std::move(colorSpace), nullptr));
58 } else {
Greg Daniel3912a4b2020-01-14 09:56:04 -050059 surfaceContext.reset(new GrSurfaceContext(context, std::move(readView), colorType,
60 alphaType, std::move(colorSpace)));
Greg Danielbfa19c42019-12-19 16:41:40 -050061 }
Robert Phillips07f0e412020-01-17 15:20:00 -050062 SkDEBUGCODE(surfaceContext->validate();)
Greg Danielbfa19c42019-12-19 16:41:40 -050063 return surfaceContext;
64}
65
Brian Salomona56a7462020-02-07 14:17:25 -050066std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
67 SkISize dimensions,
68 const GrBackendFormat& format,
69 GrRenderable renderable,
70 int renderTargetSampleCnt,
71 GrMipMapped mipMapped,
72 GrProtected isProtected,
73 GrSurfaceOrigin origin,
74 GrColorType colorType,
75 SkAlphaType alphaType,
76 sk_sp<SkColorSpace> colorSpace,
77 SkBackingFit fit,
78 SkBudgeted budgeted) {
Brian Salomonc5243782020-04-02 12:50:34 -040079 GrSwizzle swizzle;
80 if (colorType != GrColorType::kUnknown && !context->priv().caps()->isFormatCompressed(format)) {
Brian Salomond005b692020-04-01 15:47:05 -040081 swizzle = context->priv().caps()->getReadSwizzle(format, colorType);
82 }
Greg Daniel47c20e82020-01-21 14:29:57 -050083
Greg Danielbfa19c42019-12-19 16:41:40 -050084 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -040085 format, dimensions, renderable, renderTargetSampleCnt, mipMapped, fit, budgeted,
86 isProtected);
Greg Danielbfa19c42019-12-19 16:41:40 -050087 if (!proxy) {
88 return nullptr;
89 }
90
Greg Daniel3912a4b2020-01-14 09:56:04 -050091 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
92 return GrSurfaceContext::Make(context, std::move(view), colorType, alphaType,
Greg Danielbfa19c42019-12-19 16:41:40 -050093 std::move(colorSpace));
94}
95
Greg Danielf41b2bd2019-08-22 16:19:24 -040096// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
97// GrOpsTasks to be picked up and added to by renderTargetContexts lower in the call
98// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
99// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500100GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500101 GrSurfaceProxyView readView,
Brian Salomond6287472019-06-24 15:50:07 -0400102 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -0400103 SkAlphaType alphaType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500104 sk_sp<SkColorSpace> colorSpace)
Greg Daniel901b98e2019-10-22 09:54:02 -0400105 : fContext(context)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500106 , fReadView(std::move(readView))
107 , fColorInfo(colorType, alphaType, std::move(colorSpace)) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500108 SkASSERT(!context->priv().abandoned());
109}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400110
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400111const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); }
112
Robert Phillips0d075de2019-03-04 11:08:13 -0500113GrAuditTrail* GrSurfaceContext::auditTrail() {
114 return fContext->priv().auditTrail();
115}
116
117GrDrawingManager* GrSurfaceContext::drawingManager() {
118 return fContext->priv().drawingManager();
119}
120
121const GrDrawingManager* GrSurfaceContext::drawingManager() const {
122 return fContext->priv().drawingManager();
123}
124
125#ifdef SK_DEBUG
126GrSingleOwner* GrSurfaceContext::singleOwner() {
127 return fContext->priv().singleOwner();
128}
129#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400130
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400131bool GrSurfaceContext::readPixels(const GrImageInfo& origDstInfo, void* dst, size_t rowBytes,
Brian Salomon1d435302019-07-01 13:05:28 -0400132 SkIPoint pt, GrContext* direct) {
133 ASSERT_SINGLE_OWNER
134 RETURN_FALSE_IF_ABANDONED
135 SkDEBUGCODE(this->validate();)
136 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Greg Daniel6eb8c242019-06-05 10:22:24 -0400137
Brian Salomon1d435302019-07-01 13:05:28 -0400138 if (!direct && !(direct = fContext->priv().asDirectContext())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400139 return false;
140 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400141
Brian Salomon1d435302019-07-01 13:05:28 -0400142 if (!dst) {
143 return false;
144 }
145
Brian Salomon1047a492019-07-02 12:25:21 -0400146 size_t tightRowBytes = origDstInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400147 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400148 rowBytes = tightRowBytes;
149 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400150 return false;
151 }
152
153 if (!origDstInfo.isValid()) {
154 return false;
155 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400156
157 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
158
Stephen White3c0a50f2020-01-16 18:19:54 -0500159 if (srcProxy->framebufferOnly()) {
160 return false;
161 }
162
Greg Daniel6eb8c242019-06-05 10:22:24 -0400163 // MDB TODO: delay this instantiation until later in the method
164 if (!srcProxy->instantiate(direct->priv().resourceProvider())) {
165 return false;
166 }
167
168 GrSurface* srcSurface = srcProxy->peekSurface();
169
Brian Salomon1d435302019-07-01 13:05:28 -0400170 auto dstInfo = origDstInfo;
171 if (!dstInfo.clip(this->width(), this->height(), &pt, &dst, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400172 return false;
173 }
Brian Salomon1047a492019-07-02 12:25:21 -0400174 // Our tight row bytes may have been changed by clipping.
175 tightRowBytes = dstInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400176
Mike Klein7321e6a2019-12-03 11:08:40 -0600177 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{this->colorInfo(), dstInfo}.flags;
178 bool unpremul = flags.unpremul,
179 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
180 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400181
182 const GrCaps* caps = direct->priv().caps();
Robert Phillips07f0e412020-01-17 15:20:00 -0500183 bool srcIsCompressed = caps->isFormatCompressed(srcSurface->backendFormat());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400184 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
185 // care so much about getImageData performance. However, in order to ensure putImageData/
186 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
187 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
188 // fContext->vaildaPMUPMConversionExists()).
Greg Daniel0258c902019-08-01 13:08:33 -0400189 GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
190 GrRenderable::kYes);
Greg Danielc71c7962020-01-14 16:44:18 -0500191 GrColorType srcColorType = this->colorInfo().colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400192 bool canvas2DFastPath = unpremul && !needColorConversion &&
193 (GrColorType::kRGBA_8888 == dstInfo.colorType() ||
194 GrColorType::kBGRA_8888 == dstInfo.colorType()) &&
195 SkToBool(srcProxy->asTextureProxy()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500196 (srcColorType == GrColorType::kRGBA_8888 ||
197 srcColorType == GrColorType::kBGRA_8888) &&
Greg Daniel0258c902019-08-01 13:08:33 -0400198 defaultRGBAFormat.isValid() &&
Brian Salomon1d435302019-07-01 13:05:28 -0400199 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400200
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400201 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400202 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400203 return false;
204 }
205
Brian Salomondc0710f2019-07-01 14:59:32 -0400206 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Robert Phillips07f0e412020-01-17 15:20:00 -0500207 GrColorType colorType = (canvas2DFastPath || srcIsCompressed)
208 ? GrColorType::kRGBA_8888 : this->colorInfo().colorType();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400209 sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorInfo().refColorSpace();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400210
Greg Daniele20fcad2020-01-08 11:52:34 -0500211 auto tempCtx = GrRenderTargetContext::Make(
212 direct, colorType, std::move(cs), SkBackingFit::kApprox, dstInfo.dimensions(),
213 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400214 if (!tempCtx) {
215 return false;
216 }
217
218 std::unique_ptr<GrFragmentProcessor> fp;
219 if (canvas2DFastPath) {
Greg Danield2ccbb52020-02-05 10:45:39 -0500220 fp = direct->priv().createPMToUPMEffect(
221 GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType()));
Brian Salomon1d435302019-07-01 13:05:28 -0400222 if (dstInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400223 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
Brian Salomon1d435302019-07-01 13:05:28 -0400224 dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400225 }
Brian Salomon1d435302019-07-01 13:05:28 -0400226 // The render target context is incorrectly tagged as kPremul even though we're writing
227 // unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type so we don't
228 // double unpremul.
229 dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400230 } else {
Greg Danield2ccbb52020-02-05 10:45:39 -0500231 fp = GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400232 }
233 if (!fp) {
234 return false;
235 }
236 GrPaint paint;
237 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
238 paint.addColorFragmentProcessor(std::move(fp));
239
240 tempCtx->asRenderTargetContext()->fillRectToRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400241 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400242 SkRect::MakeWH(dstInfo.width(), dstInfo.height()),
243 SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400244
Brian Salomon1d435302019-07-01 13:05:28 -0400245 return tempCtx->readPixels(dstInfo, dst, rowBytes, {0, 0}, direct);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400246 }
247
Greg Danielb8d84f82020-02-13 14:25:00 -0500248 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400249
Brian Salomon1d435302019-07-01 13:05:28 -0400250 auto supportedRead = caps->supportedReadPixelsColorType(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400251 this->colorInfo().colorType(), srcProxy->backendFormat(), dstInfo.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400252
Brian Salomon1047a492019-07-02 12:25:21 -0400253 bool makeTight = !caps->readPixelsRowBytesSupport() && tightRowBytes != rowBytes;
254
255 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomon8f8354a2019-07-31 20:12:02 -0400256 (dstInfo.colorType() != supportedRead.fColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400257
Brian Salomonf30b1c12019-06-20 12:25:02 -0400258 std::unique_ptr<char[]> tmpPixels;
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400259 GrImageInfo tmpInfo;
Brian Salomon1d435302019-07-01 13:05:28 -0400260 void* readDst = dst;
261 size_t readRB = rowBytes;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400262 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400263 tmpInfo = {supportedRead.fColorType, this->colorInfo().alphaType(),
264 this->colorInfo().refColorSpace(), dstInfo.width(), dstInfo.height()};
Brian Salomon1d435302019-07-01 13:05:28 -0400265 size_t tmpRB = tmpInfo.minRowBytes();
266 size_t size = tmpRB * tmpInfo.height();
267 // Chrome MSAN bots require the data to be initialized (hence the ()).
268 tmpPixels.reset(new char[size]());
Brian Salomonf30b1c12019-06-20 12:25:02 -0400269
Brian Salomonf30b1c12019-06-20 12:25:02 -0400270 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400271 readRB = tmpRB;
272 pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400273 }
274
Greg Daniel55f040b2020-02-13 15:38:32 +0000275 direct->priv().flushSurface(srcProxy);
Greg Daniel04283f32020-05-20 13:16:00 -0400276 direct->submit();
Brian Salomon1d435302019-07-01 13:05:28 -0400277 if (!direct->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400278 dstInfo.height(), this->colorInfo().colorType(),
Brian Salomonf77c1462019-08-01 15:19:29 -0400279 supportedRead.fColorType, readDst, readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400280 return false;
281 }
282
Greg Daniel6eb8c242019-06-05 10:22:24 -0400283 if (convert) {
Brian Salomon8f8354a2019-07-31 20:12:02 -0400284 return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400285 }
286 return true;
287}
Robert Phillips0d075de2019-03-04 11:08:13 -0500288
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400289bool GrSurfaceContext::writePixels(const GrImageInfo& origSrcInfo, const void* src, size_t rowBytes,
Brian Salomon1d435302019-07-01 13:05:28 -0400290 SkIPoint pt, GrContext* direct) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400291 ASSERT_SINGLE_OWNER
292 RETURN_FALSE_IF_ABANDONED
293 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400294 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400295
Brian Salomon1d435302019-07-01 13:05:28 -0400296 if (!direct && !(direct = fContext->priv().asDirectContext())) {
Brian Salomonc320b152018-02-20 14:05:36 -0500297 return false;
298 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500299
Brian Salomon1d435302019-07-01 13:05:28 -0400300 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500301 return false;
302 }
303
Brian Salomon1d435302019-07-01 13:05:28 -0400304 if (!src) {
305 return false;
306 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400307
Brian Salomon1047a492019-07-02 12:25:21 -0400308 size_t tightRowBytes = origSrcInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400309 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400310 rowBytes = tightRowBytes;
311 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400312 return false;
313 }
314
315 if (!origSrcInfo.isValid()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400316 return false;
317 }
318
319 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
Stephen White3c0a50f2020-01-16 18:19:54 -0500320
321 if (dstProxy->framebufferOnly()) {
322 return false;
323 }
324
Greg Daniel6eb8c242019-06-05 10:22:24 -0400325 if (!dstProxy->instantiate(direct->priv().resourceProvider())) {
326 return false;
327 }
328
329 GrSurface* dstSurface = dstProxy->peekSurface();
330
Brian Salomon1d435302019-07-01 13:05:28 -0400331 auto srcInfo = origSrcInfo;
332 if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400333 return false;
334 }
Brian Salomon1047a492019-07-02 12:25:21 -0400335 // Our tight row bytes may have been changed by clipping.
336 tightRowBytes = srcInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400337
Mike Klein7321e6a2019-12-03 11:08:40 -0600338 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{srcInfo, this->colorInfo()}.flags;
339 bool unpremul = flags.unpremul,
340 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
341 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400342
343 const GrCaps* caps = direct->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400344
345 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
346 GrRenderable::kNo);
347
Greg Danielc71c7962020-01-14 16:44:18 -0500348 GrColorType dstColorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400349 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
350 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400351 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
352 (srcInfo.colorType() == GrColorType::kRGBA_8888 ||
353 srcInfo.colorType() == GrColorType::kBGRA_8888) &&
354 SkToBool(this->asRenderTargetContext()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500355 (dstColorType == GrColorType::kRGBA_8888 ||
356 dstColorType == GrColorType::kBGRA_8888) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400357 rgbaDefaultFormat.isValid() &&
Brian Salomon1d435302019-07-01 13:05:28 -0400358 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400359
360 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
Brian Salomond6287472019-06-24 15:50:07 -0400361 GrColorType colorType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400362
363 GrBackendFormat format;
Brian Salomone7499c72019-06-24 12:12:36 -0400364 SkAlphaType alphaType;
Greg Danielbfa19c42019-12-19 16:41:40 -0500365 GrSwizzle tempReadSwizzle;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400366 if (canvas2DFastPath) {
Brian Salomond6287472019-06-24 15:50:07 -0400367 colorType = GrColorType::kRGBA_8888;
Greg Daniel7bfc9132019-08-14 14:23:53 -0400368 format = rgbaDefaultFormat;
Brian Salomone7499c72019-06-24 12:12:36 -0400369 alphaType = kUnpremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400370 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400371 colorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400372 format = dstProxy->backendFormat().makeTexture2D();
373 if (!format.isValid()) {
374 return false;
375 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400376 alphaType = this->colorInfo().alphaType();
Greg Danielbfa19c42019-12-19 16:41:40 -0500377 tempReadSwizzle = this->readSwizzle();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400378 }
379
Greg Daniel2e52ad12019-06-13 10:04:16 -0400380 // It is more efficient for us to write pixels into a top left origin so we prefer that.
381 // However, if the final proxy isn't a render target then we must use a copy to move the
382 // data into it which requires the origins to match. If the final proxy is a render target
383 // we can use a draw instead which doesn't have this origin restriction. Thus for render
384 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400385 GrSurfaceOrigin tempOrigin =
Greg Danielb8d84f82020-02-13 14:25:00 -0500386 this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : this->origin();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400387 auto tempProxy = direct->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400388 format, srcInfo.dimensions(), GrRenderable::kNo, 1, GrMipMapped::kNo,
389 SkBackingFit::kApprox, SkBudgeted::kYes, GrProtected::kNo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400390 if (!tempProxy) {
391 return false;
392 }
Greg Daniel3912a4b2020-01-14 09:56:04 -0500393 GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle);
Greg Danield2ccbb52020-02-05 10:45:39 -0500394 GrSurfaceContext tempCtx(direct, tempView, colorType, alphaType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500395 this->colorInfo().refColorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400396
397 // In the fast path we always write the srcData to the temp context as though it were RGBA.
398 // When the data is really BGRA the write will cause the R and B channels to be swapped in
399 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
400 // dst.
Brian Salomon1d435302019-07-01 13:05:28 -0400401 if (canvas2DFastPath) {
402 srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888);
403 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500404 if (!tempCtx.writePixels(srcInfo, src, rowBytes, {0, 0}, direct)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400405 return false;
406 }
407
408 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400409 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400410 if (canvas2DFastPath) {
Brian Salomonb8f098d2020-01-07 11:15:44 -0500411 fp = direct->priv().createUPMToPMEffect(
Greg Danield2ccbb52020-02-05 10:45:39 -0500412 GrTextureEffect::Make(std::move(tempView), alphaType));
Brian Salomon1d435302019-07-01 13:05:28 -0400413 // Important: check the original src color type here!
414 if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400415 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
416 }
417 } else {
Greg Danield2ccbb52020-02-05 10:45:39 -0500418 fp = GrTextureEffect::Make(std::move(tempView), alphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400419 }
420 if (!fp) {
421 return false;
422 }
423 GrPaint paint;
424 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
425 paint.addColorFragmentProcessor(std::move(fp));
426 this->asRenderTargetContext()->fillRectToRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400427 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400428 SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()),
429 SkRect::MakeWH(srcInfo.width(), srcInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400430 } else {
Brian Salomon1d435302019-07-01 13:05:28 -0400431 SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height());
432 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Brian Salomonc5243782020-04-02 12:50:34 -0400433 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400434 return false;
435 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400436 }
437 return true;
438 }
439
Brian Salomon1d435302019-07-01 13:05:28 -0400440 GrColorType allowedColorType =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400441 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400442 dstProxy->backendFormat(),
443 srcInfo.colorType()).fColorType;
Greg Danielb8d84f82020-02-13 14:25:00 -0500444 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon1047a492019-07-02 12:25:21 -0400445 bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes;
446 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomon1d435302019-07-01 13:05:28 -0400447 (srcInfo.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400448
Brian Salomonf30b1c12019-06-20 12:25:02 -0400449 std::unique_ptr<char[]> tmpPixels;
Brian Salomon1d435302019-07-01 13:05:28 -0400450 GrColorType srcColorType = srcInfo.colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400451 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400452 GrImageInfo tmpInfo(allowedColorType, this->colorInfo().alphaType(),
453 this->colorInfo().refColorSpace(), srcInfo.width(), srcInfo.height());
Brian Salomon1d435302019-07-01 13:05:28 -0400454 auto tmpRB = tmpInfo.minRowBytes();
455 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400456
Brian Salomon1d435302019-07-01 13:05:28 -0400457 GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400458
Brian Salomon1d435302019-07-01 13:05:28 -0400459 srcColorType = tmpInfo.colorType();
460 rowBytes = tmpRB;
461 src = tmpPixels.get();
462 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400463 }
464
465 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
466 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
467 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
468 // destination proxy)
469 // TODO: should this policy decision just be moved into the drawing manager?
Greg Daniel55f040b2020-02-13 15:38:32 +0000470 direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400471
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400472 return direct->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(),
473 srcInfo.height(), this->colorInfo().colorType(),
474 srcColorType, src, rowBytes);
Brian Osman45580d32016-11-23 09:37:01 -0500475}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400476
Brian Salomonc5243782020-04-02 12:50:34 -0400477bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400478 ASSERT_SINGLE_OWNER
479 RETURN_FALSE_IF_ABANDONED
480 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -0400481 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -0400482
Brian Salomon947efe22019-07-16 15:36:11 -0400483 const GrCaps* caps = fContext->priv().caps();
484
Greg Daniel46cfbc62019-06-07 11:43:30 -0400485 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -0500486 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
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(
Brian Salomonc5243782020-04-02 12:50:34 -0400498 GrSurfaceProxyView(sk_ref_sp(src), this->origin(), GrSwizzle("rgba")), 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(
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400503 const GrImageInfo& info,
504 GrSurfaceOrigin origin,
Brian Salomonafd8a6c2020-05-21 12:10:54 -0400505 SkIRect srcRect,
Brian Salomonbf6b9792019-08-21 09:38:10 -0400506 SkSurface::RescaleGamma rescaleGamma,
507 SkFilterQuality rescaleQuality) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400508 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
Greg Daniel40903af2020-01-30 14:55:05 -0500522 GrSurfaceProxyView texView = this->readSurfaceView();
Brian Salomonfc118442019-11-22 19:09:27 -0500523 SkAlphaType srcAlphaType = this->colorInfo().alphaType();
Greg Daniel40903af2020-01-30 14:55:05 -0500524 if (!texView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -0400525 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipMapped::kNo, srcRect,
526 SkBackingFit::kApprox, SkBudgeted::kNo);
527 if (!texView) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400528 return nullptr;
529 }
Greg Daniel40903af2020-01-30 14:55:05 -0500530 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -0400531 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -0400532 }
533
Brian Salomonbf6b9792019-08-21 09:38:10 -0400534 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
535 // pass B is moved to A. If 'this' is the input on the first pass then tempA is null.
536 std::unique_ptr<GrRenderTargetContext> tempA;
537 std::unique_ptr<GrRenderTargetContext> tempB;
538
Brian Salomone9ad9982019-07-22 16:17:41 -0400539 // Assume we should ignore the rescale linear request if the surface has no color space since
540 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400541 if (rescaleGamma == SkSurface::kLinear && this->colorInfo().colorSpace() &&
542 !this->colorInfo().colorSpace()->gammaIsLinear()) {
543 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomonfc118442019-11-22 19:09:27 -0500544 auto xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(), srcAlphaType, cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -0400545 kPremul_SkAlphaType);
546 // We'll fall back to kRGBA_8888 if half float not supported.
Greg Daniele20fcad2020-01-08 11:52:34 -0500547 auto linearRTC = GrRenderTargetContext::MakeWithFallback(
Brian Salomonafd8a6c2020-05-21 12:10:54 -0400548 fContext, GrColorType::kRGBA_F16, cs, SkBackingFit::kApprox, srcRect.size(), 1,
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400549 GrMipMapped::kNo, GrProtected::kNo, origin);
Brian Salomone9ad9982019-07-22 16:17:41 -0400550 if (!linearRTC) {
551 return nullptr;
552 }
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400553 // 1-to-1 draw can always be kFast.
Michael Ludwig7c12e282020-05-29 09:54:07 -0400554 linearRTC->drawTexture(nullptr, std::move(texView), srcAlphaType,
Brian Salomonfc118442019-11-22 19:09:27 -0500555 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
Brian Salomonafd8a6c2020-05-21 12:10:54 -0400556 SK_PMColor4fWHITE, SkRect::Make(srcRect),
557 SkRect::Make(srcRect.size()), GrAA::kNo, GrQuadAAFlags::kNone,
558 SkCanvas::kFast_SrcRectConstraint, SkMatrix::I(), std::move(xform));
Greg Daniel40903af2020-01-30 14:55:05 -0500559 texView = linearRTC->readSurfaceView();
560 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -0400561 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -0400562 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -0400563 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -0400564
Brian Salomon9c6f6bf2020-05-29 16:37:26 -0400565 while (srcRect.size() != info.dimensions()) {
Brian Salomon59f31b12020-06-04 17:27:15 -0400566 SkISize nextDims = info.dimensions();
567 if (rescaleQuality != kNone_SkFilterQuality) {
568 if (srcRect.width() > info.width()) {
569 nextDims.fWidth = std::max((srcRect.width() + 1)/2, info.width());
570 } else if (srcRect.width() < info.width()) {
571 nextDims.fWidth = std::min(srcRect.width()*2, info.width());
572 }
573 if (srcRect.height() > info.height()) {
574 nextDims.fHeight = std::max((srcRect.height() + 1)/2, info.height());
575 } else if (srcRect.height() < info.height()) {
576 nextDims.fHeight = std::min(srcRect.height()*2, info.height());
577 }
578 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400579 auto input = tempA ? tempA.get() : this;
Brian Salomon59f31b12020-06-04 17:27:15 -0400580 GrColorType colorType = input->colorInfo().colorType();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400581 auto cs = input->colorInfo().refColorSpace();
Brian Salomone9ad9982019-07-22 16:17:41 -0400582 sk_sp<GrColorSpaceXform> xform;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400583 auto prevAlphaType = input->colorInfo().alphaType();
Brian Salomonafd8a6c2020-05-21 12:10:54 -0400584 if (nextDims == info.dimensions()) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400585 // Might as well fold conversion to final info in the last step.
586 cs = info.refColorSpace();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400587 xform = GrColorSpaceXform::Make(input->colorInfo().colorSpace(),
588 input->colorInfo().alphaType(), cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -0400589 info.alphaType());
590 }
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400591 tempB = GrRenderTargetContext::MakeWithFallback(fContext, colorType, std::move(cs),
Brian Salomonafd8a6c2020-05-21 12:10:54 -0400592 SkBackingFit::kApprox, nextDims, 1,
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400593 GrMipMapped::kNo, GrProtected::kNo, origin);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400594 if (!tempB) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400595 return nullptr;
596 }
Brian Salomon59f31b12020-06-04 17:27:15 -0400597 auto dstRect = SkRect::Make(nextDims);
598 if (rescaleQuality == kHigh_SkFilterQuality) {
599 SkMatrix matrix;
600 matrix.setScaleTranslate((float)srcRect.width()/nextDims.width(),
601 (float)srcRect.height()/nextDims.height(),
602 srcRect.x(),
603 srcRect.y());
604 std::unique_ptr<GrFragmentProcessor> fp;
605 auto dir = GrBicubicEffect::Direction::kXY;
606 if (nextDims.width() == srcRect.width()) {
607 dir = GrBicubicEffect::Direction::kY;
608 } else if (nextDims.height() == srcRect.height()) {
609 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -0400610 }
Brian Salomon1af72d12020-06-25 10:47:26 -0400611 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
612 static constexpr auto kKernel = GrBicubicEffect::Kernel::kCatmullRom;
Brian Salomon59f31b12020-06-04 17:27:15 -0400613 fp = GrBicubicEffect::MakeSubset(std::move(texView), prevAlphaType, matrix, kWM, kWM,
Brian Salomon1af72d12020-06-25 10:47:26 -0400614 SkRect::Make(srcRect), kKernel, dir, *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -0400615 if (xform) {
616 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -0400617 }
Brian Salomon59f31b12020-06-04 17:27:15 -0400618 GrPaint paint;
619 paint.addColorFragmentProcessor(std::move(fp));
620 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
621 tempB->fillRectToRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect,
622 dstRect);
623 } else {
624 auto filter = rescaleQuality == kNone_SkFilterQuality ? GrSamplerState::Filter::kNearest
625 : GrSamplerState::Filter::kBilerp;
626 // Minimizing draw with integer coord src and dev rects can always be kFast.
627 auto constraint = SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint;
628 if (nextDims.width() <= srcRect.width() && nextDims.height() <= srcRect.height()) {
629 constraint = SkCanvas::SrcRectConstraint::kFast_SrcRectConstraint;
630 }
631 tempB->drawTexture(nullptr, std::move(texView), srcAlphaType, filter, SkBlendMode::kSrc,
632 SK_PMColor4fWHITE, SkRect::Make(srcRect), dstRect, GrAA::kNo,
633 GrQuadAAFlags::kNone, constraint, SkMatrix::I(), std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -0400634 }
Greg Daniel40903af2020-01-30 14:55:05 -0500635 texView = tempB->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -0400636 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -0400637 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomone9ad9982019-07-22 16:17:41 -0400638 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400639 SkASSERT(tempA);
640 return tempA;
Brian Salomone9ad9982019-07-22 16:17:41 -0400641}
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400642
643GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
644 const SkIRect& rect) {
645 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
646 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
647 auto direct = fContext->priv().asDirectContext();
648 if (!direct) {
649 return {};
650 }
651 auto rtProxy = this->asRenderTargetProxy();
652 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
653 return {};
654 }
655
656 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400657 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
658 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400659 // Fail if read color type does not have all of dstCT's color channels and those missing color
660 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -0400661 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
662 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
663 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
664 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400665 return {};
666 }
667
Brian Salomonfb28c6f2020-01-10 13:04:45 -0500668 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400669 !supportedRead.fOffsetAlignmentForTransferBuffer) {
670 return {};
671 }
672
673 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
674 size_t size = rowBytes * rect.height();
675 auto buffer = direct->priv().resourceProvider()->createBuffer(
676 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
677 if (!buffer) {
678 return {};
679 }
680 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -0500681 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400682 if (flip) {
683 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
684 this->height() - rect.fTop);
685 }
Greg Danielbbfec9d2019-08-20 10:56:51 -0400686 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400687 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -0400688 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400689 PixelTransferResult result;
690 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400691 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -0400692 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400693 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
694 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400695 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
696 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400697 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
698 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -0400699 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400700 };
701 }
702 return result;
703}
Greg Daniel46e366a2019-12-16 14:38:36 -0500704
705#ifdef SK_DEBUG
706void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500707 SkASSERT(fReadView.proxy());
708 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -0400709 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
710 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
711 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
712 }
Greg Daniel46e366a2019-12-16 14:38:36 -0500713 this->onValidate();
714}
715#endif