blob: d8067d488ca7769f9284e9d0dbb1d431d6f2c24c [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
64std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -050065 GrRecordingContext* context,
66 const SkISize& dimensions,
67 const GrBackendFormat& format,
68 GrRenderable renderable,
69 int renderTargetSampleCnt,
70 GrMipMapped mipMapped,
71 GrProtected isProtected,
72 GrSurfaceOrigin origin,
73 GrColorType colorType,
74 SkAlphaType alphaType,
75 sk_sp<SkColorSpace> colorSpace,
76 SkBackingFit fit,
Greg Danielbfa19c42019-12-19 16:41:40 -050077 SkBudgeted budgeted) {
Greg Danielbfa19c42019-12-19 16:41:40 -050078 GrSurfaceDesc desc;
79 desc.fWidth = dimensions.width();
80 desc.fHeight = dimensions.height();
Greg Danielbfa19c42019-12-19 16:41:40 -050081
Greg Daniel47c20e82020-01-21 14:29:57 -050082 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(format, colorType);
83
Greg Danielbfa19c42019-12-19 16:41:40 -050084 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Greg Daniel47c20e82020-01-21 14:29:57 -050085 format, desc, swizzle, renderable, renderTargetSampleCnt, origin, mipMapped, fit,
86 budgeted, 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
96
Greg Danielf41b2bd2019-08-22 16:19:24 -040097// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
98// GrOpsTasks to be picked up and added to by renderTargetContexts lower in the call
99// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
100// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500101GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500102 GrSurfaceProxyView readView,
Brian Salomond6287472019-06-24 15:50:07 -0400103 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -0400104 SkAlphaType alphaType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500105 sk_sp<SkColorSpace> colorSpace)
Greg Daniel901b98e2019-10-22 09:54:02 -0400106 : fContext(context)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500107 , fReadView(std::move(readView))
108 , fColorInfo(colorType, alphaType, std::move(colorSpace)) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500109 SkASSERT(!context->priv().abandoned());
110}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400111
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400112const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); }
113
Robert Phillips0d075de2019-03-04 11:08:13 -0500114GrAuditTrail* GrSurfaceContext::auditTrail() {
115 return fContext->priv().auditTrail();
116}
117
118GrDrawingManager* GrSurfaceContext::drawingManager() {
119 return fContext->priv().drawingManager();
120}
121
122const GrDrawingManager* GrSurfaceContext::drawingManager() const {
123 return fContext->priv().drawingManager();
124}
125
126#ifdef SK_DEBUG
127GrSingleOwner* GrSurfaceContext::singleOwner() {
128 return fContext->priv().singleOwner();
129}
130#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400131
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400132bool GrSurfaceContext::readPixels(const GrImageInfo& origDstInfo, void* dst, size_t rowBytes,
Brian Salomon1d435302019-07-01 13:05:28 -0400133 SkIPoint pt, GrContext* direct) {
134 ASSERT_SINGLE_OWNER
135 RETURN_FALSE_IF_ABANDONED
136 SkDEBUGCODE(this->validate();)
137 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Greg Daniel6eb8c242019-06-05 10:22:24 -0400138
Brian Salomon1d435302019-07-01 13:05:28 -0400139 if (!direct && !(direct = fContext->priv().asDirectContext())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400140 return false;
141 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400142
Brian Salomon1d435302019-07-01 13:05:28 -0400143 if (!dst) {
144 return false;
145 }
146
Brian Salomon1047a492019-07-02 12:25:21 -0400147 size_t tightRowBytes = origDstInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400148 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400149 rowBytes = tightRowBytes;
150 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400151 return false;
152 }
153
154 if (!origDstInfo.isValid()) {
155 return false;
156 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400157
158 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
159
Stephen White3c0a50f2020-01-16 18:19:54 -0500160 if (srcProxy->framebufferOnly()) {
161 return false;
162 }
163
Greg Daniel6eb8c242019-06-05 10:22:24 -0400164 // MDB TODO: delay this instantiation until later in the method
165 if (!srcProxy->instantiate(direct->priv().resourceProvider())) {
166 return false;
167 }
168
169 GrSurface* srcSurface = srcProxy->peekSurface();
170
Brian Salomon1d435302019-07-01 13:05:28 -0400171 auto dstInfo = origDstInfo;
172 if (!dstInfo.clip(this->width(), this->height(), &pt, &dst, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400173 return false;
174 }
Brian Salomon1047a492019-07-02 12:25:21 -0400175 // Our tight row bytes may have been changed by clipping.
176 tightRowBytes = dstInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400177
Mike Klein7321e6a2019-12-03 11:08:40 -0600178 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{this->colorInfo(), dstInfo}.flags;
179 bool unpremul = flags.unpremul,
180 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
181 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400182
183 const GrCaps* caps = direct->priv().caps();
Robert Phillips07f0e412020-01-17 15:20:00 -0500184 bool srcIsCompressed = caps->isFormatCompressed(srcSurface->backendFormat());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400185 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
186 // care so much about getImageData performance. However, in order to ensure putImageData/
187 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
188 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
189 // fContext->vaildaPMUPMConversionExists()).
Greg Daniel0258c902019-08-01 13:08:33 -0400190 GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
191 GrRenderable::kYes);
Greg Danielc71c7962020-01-14 16:44:18 -0500192 GrColorType srcColorType = this->colorInfo().colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400193 bool canvas2DFastPath = unpremul && !needColorConversion &&
194 (GrColorType::kRGBA_8888 == dstInfo.colorType() ||
195 GrColorType::kBGRA_8888 == dstInfo.colorType()) &&
196 SkToBool(srcProxy->asTextureProxy()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500197 (srcColorType == GrColorType::kRGBA_8888 ||
198 srcColorType == GrColorType::kBGRA_8888) &&
Greg Daniel0258c902019-08-01 13:08:33 -0400199 defaultRGBAFormat.isValid() &&
Brian Salomon1d435302019-07-01 13:05:28 -0400200 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400201
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400202 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400203 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400204 return false;
205 }
206
Brian Salomondc0710f2019-07-01 14:59:32 -0400207 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Robert Phillips07f0e412020-01-17 15:20:00 -0500208 GrColorType colorType = (canvas2DFastPath || srcIsCompressed)
209 ? GrColorType::kRGBA_8888 : this->colorInfo().colorType();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400210 sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorInfo().refColorSpace();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400211
Greg Daniele20fcad2020-01-08 11:52:34 -0500212 auto tempCtx = GrRenderTargetContext::Make(
213 direct, colorType, std::move(cs), SkBackingFit::kApprox, dstInfo.dimensions(),
214 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400215 if (!tempCtx) {
216 return false;
217 }
218
219 std::unique_ptr<GrFragmentProcessor> fp;
220 if (canvas2DFastPath) {
Greg Danield2ccbb52020-02-05 10:45:39 -0500221 fp = direct->priv().createPMToUPMEffect(
222 GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType()));
Brian Salomon1d435302019-07-01 13:05:28 -0400223 if (dstInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400224 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
Brian Salomon1d435302019-07-01 13:05:28 -0400225 dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400226 }
Brian Salomon1d435302019-07-01 13:05:28 -0400227 // The render target context is incorrectly tagged as kPremul even though we're writing
228 // unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type so we don't
229 // double unpremul.
230 dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400231 } else {
Greg Danield2ccbb52020-02-05 10:45:39 -0500232 fp = GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400233 }
234 if (!fp) {
235 return false;
236 }
237 GrPaint paint;
238 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
239 paint.addColorFragmentProcessor(std::move(fp));
240
241 tempCtx->asRenderTargetContext()->fillRectToRect(
242 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400243 SkRect::MakeWH(dstInfo.width(), dstInfo.height()),
244 SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400245
Brian Salomon1d435302019-07-01 13:05:28 -0400246 return tempCtx->readPixels(dstInfo, dst, rowBytes, {0, 0}, direct);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400247 }
248
Greg Daniel6eb8c242019-06-05 10:22:24 -0400249 bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400250
Brian Salomon1d435302019-07-01 13:05:28 -0400251 auto supportedRead = caps->supportedReadPixelsColorType(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400252 this->colorInfo().colorType(), srcProxy->backendFormat(), dstInfo.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400253
Brian Salomon1047a492019-07-02 12:25:21 -0400254 bool makeTight = !caps->readPixelsRowBytesSupport() && tightRowBytes != rowBytes;
255
256 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomon8f8354a2019-07-31 20:12:02 -0400257 (dstInfo.colorType() != supportedRead.fColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400258
Brian Salomonf30b1c12019-06-20 12:25:02 -0400259 std::unique_ptr<char[]> tmpPixels;
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400260 GrImageInfo tmpInfo;
Brian Salomon1d435302019-07-01 13:05:28 -0400261 void* readDst = dst;
262 size_t readRB = rowBytes;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400263 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400264 tmpInfo = {supportedRead.fColorType, this->colorInfo().alphaType(),
265 this->colorInfo().refColorSpace(), dstInfo.width(), dstInfo.height()};
Brian Salomon1d435302019-07-01 13:05:28 -0400266 size_t tmpRB = tmpInfo.minRowBytes();
267 size_t size = tmpRB * tmpInfo.height();
268 // Chrome MSAN bots require the data to be initialized (hence the ()).
269 tmpPixels.reset(new char[size]());
Brian Salomonf30b1c12019-06-20 12:25:02 -0400270
Brian Salomonf30b1c12019-06-20 12:25:02 -0400271 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400272 readRB = tmpRB;
273 pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400274 }
275
276 direct->priv().flushSurface(srcProxy);
277
Brian Salomon1d435302019-07-01 13:05:28 -0400278 if (!direct->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400279 dstInfo.height(), this->colorInfo().colorType(),
Brian Salomonf77c1462019-08-01 15:19:29 -0400280 supportedRead.fColorType, readDst, readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400281 return false;
282 }
283
Greg Daniel6eb8c242019-06-05 10:22:24 -0400284 if (convert) {
Brian Salomon8f8354a2019-07-31 20:12:02 -0400285 return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400286 }
287 return true;
288}
Robert Phillips0d075de2019-03-04 11:08:13 -0500289
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400290bool GrSurfaceContext::writePixels(const GrImageInfo& origSrcInfo, const void* src, size_t rowBytes,
Brian Salomon1d435302019-07-01 13:05:28 -0400291 SkIPoint pt, GrContext* direct) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400292 ASSERT_SINGLE_OWNER
293 RETURN_FALSE_IF_ABANDONED
294 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400295 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400296
Brian Salomon1d435302019-07-01 13:05:28 -0400297 if (!direct && !(direct = fContext->priv().asDirectContext())) {
Brian Salomonc320b152018-02-20 14:05:36 -0500298 return false;
299 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500300
Brian Salomon1d435302019-07-01 13:05:28 -0400301 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500302 return false;
303 }
304
Brian Salomon1d435302019-07-01 13:05:28 -0400305 if (!src) {
306 return false;
307 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400308
Brian Salomon1047a492019-07-02 12:25:21 -0400309 size_t tightRowBytes = origSrcInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400310 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400311 rowBytes = tightRowBytes;
312 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400313 return false;
314 }
315
316 if (!origSrcInfo.isValid()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400317 return false;
318 }
319
320 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
Stephen White3c0a50f2020-01-16 18:19:54 -0500321
322 if (dstProxy->framebufferOnly()) {
323 return false;
324 }
325
Greg Daniel6eb8c242019-06-05 10:22:24 -0400326 if (!dstProxy->instantiate(direct->priv().resourceProvider())) {
327 return false;
328 }
329
330 GrSurface* dstSurface = dstProxy->peekSurface();
331
Brian Salomon1d435302019-07-01 13:05:28 -0400332 auto srcInfo = origSrcInfo;
333 if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400334 return false;
335 }
Brian Salomon1047a492019-07-02 12:25:21 -0400336 // Our tight row bytes may have been changed by clipping.
337 tightRowBytes = srcInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400338
Mike Klein7321e6a2019-12-03 11:08:40 -0600339 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{srcInfo, this->colorInfo()}.flags;
340 bool unpremul = flags.unpremul,
341 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
342 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400343
344 const GrCaps* caps = direct->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400345
346 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
347 GrRenderable::kNo);
348
Greg Danielc71c7962020-01-14 16:44:18 -0500349 GrColorType dstColorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400350 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
351 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400352 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
353 (srcInfo.colorType() == GrColorType::kRGBA_8888 ||
354 srcInfo.colorType() == GrColorType::kBGRA_8888) &&
355 SkToBool(this->asRenderTargetContext()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500356 (dstColorType == GrColorType::kRGBA_8888 ||
357 dstColorType == GrColorType::kBGRA_8888) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400358 rgbaDefaultFormat.isValid() &&
Brian Salomon1d435302019-07-01 13:05:28 -0400359 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400360
361 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
362 GrSurfaceDesc desc;
Brian Salomon1d435302019-07-01 13:05:28 -0400363 desc.fWidth = srcInfo.width();
364 desc.fHeight = srcInfo.height();
Brian Salomond6287472019-06-24 15:50:07 -0400365 GrColorType colorType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400366
367 GrBackendFormat format;
Brian Salomone7499c72019-06-24 12:12:36 -0400368 SkAlphaType alphaType;
Greg Danielbfa19c42019-12-19 16:41:40 -0500369 GrSwizzle tempReadSwizzle;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400370 if (canvas2DFastPath) {
Brian Salomond6287472019-06-24 15:50:07 -0400371 colorType = GrColorType::kRGBA_8888;
Greg Daniel7bfc9132019-08-14 14:23:53 -0400372 format = rgbaDefaultFormat;
Brian Salomone7499c72019-06-24 12:12:36 -0400373 alphaType = kUnpremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400374 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400375 colorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400376 format = dstProxy->backendFormat().makeTexture2D();
377 if (!format.isValid()) {
378 return false;
379 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400380 alphaType = this->colorInfo().alphaType();
Greg Danielbfa19c42019-12-19 16:41:40 -0500381 tempReadSwizzle = this->readSwizzle();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400382 }
383
Greg Daniel2e52ad12019-06-13 10:04:16 -0400384 // It is more efficient for us to write pixels into a top left origin so we prefer that.
385 // However, if the final proxy isn't a render target then we must use a copy to move the
386 // data into it which requires the origins to match. If the final proxy is a render target
387 // we can use a draw instead which doesn't have this origin restriction. Thus for render
388 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400389 GrSurfaceOrigin tempOrigin =
390 this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : dstProxy->origin();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400391 auto tempProxy = direct->priv().proxyProvider()->createProxy(
Greg Daniel47c20e82020-01-21 14:29:57 -0500392 format, desc, tempReadSwizzle, GrRenderable::kNo, 1, tempOrigin, GrMipMapped::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400393 SkBackingFit::kApprox, SkBudgeted::kYes, GrProtected::kNo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400394 if (!tempProxy) {
395 return false;
396 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500397 SkASSERT(tempProxy->textureSwizzle() == tempReadSwizzle);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500398 GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle);
Greg Danield2ccbb52020-02-05 10:45:39 -0500399 GrSurfaceContext tempCtx(direct, tempView, colorType, alphaType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500400 this->colorInfo().refColorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400401
402 // In the fast path we always write the srcData to the temp context as though it were RGBA.
403 // When the data is really BGRA the write will cause the R and B channels to be swapped in
404 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
405 // dst.
Brian Salomon1d435302019-07-01 13:05:28 -0400406 if (canvas2DFastPath) {
407 srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888);
408 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500409 if (!tempCtx.writePixels(srcInfo, src, rowBytes, {0, 0}, direct)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400410 return false;
411 }
412
413 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400414 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400415 if (canvas2DFastPath) {
Brian Salomonb8f098d2020-01-07 11:15:44 -0500416 fp = direct->priv().createUPMToPMEffect(
Greg Danield2ccbb52020-02-05 10:45:39 -0500417 GrTextureEffect::Make(std::move(tempView), alphaType));
Brian Salomon1d435302019-07-01 13:05:28 -0400418 // Important: check the original src color type here!
419 if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400420 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
421 }
422 } else {
Greg Danield2ccbb52020-02-05 10:45:39 -0500423 fp = GrTextureEffect::Make(std::move(tempView), alphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400424 }
425 if (!fp) {
426 return false;
427 }
428 GrPaint paint;
429 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
430 paint.addColorFragmentProcessor(std::move(fp));
431 this->asRenderTargetContext()->fillRectToRect(
432 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400433 SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()),
434 SkRect::MakeWH(srcInfo.width(), srcInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400435 } else {
Brian Salomon1d435302019-07-01 13:05:28 -0400436 SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height());
437 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400438 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400439 return false;
440 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400441 }
442 return true;
443 }
444
Brian Salomon1d435302019-07-01 13:05:28 -0400445 GrColorType allowedColorType =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400446 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400447 dstProxy->backendFormat(),
448 srcInfo.colorType()).fColorType;
Brian Salomon1d435302019-07-01 13:05:28 -0400449 bool flip = dstProxy->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon1047a492019-07-02 12:25:21 -0400450 bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes;
451 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomon1d435302019-07-01 13:05:28 -0400452 (srcInfo.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400453
Brian Salomonf30b1c12019-06-20 12:25:02 -0400454 std::unique_ptr<char[]> tmpPixels;
Brian Salomon1d435302019-07-01 13:05:28 -0400455 GrColorType srcColorType = srcInfo.colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400456 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400457 GrImageInfo tmpInfo(allowedColorType, this->colorInfo().alphaType(),
458 this->colorInfo().refColorSpace(), srcInfo.width(), srcInfo.height());
Brian Salomon1d435302019-07-01 13:05:28 -0400459 auto tmpRB = tmpInfo.minRowBytes();
460 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400461
Brian Salomon1d435302019-07-01 13:05:28 -0400462 GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400463
Brian Salomon1d435302019-07-01 13:05:28 -0400464 srcColorType = tmpInfo.colorType();
465 rowBytes = tmpRB;
466 src = tmpPixels.get();
467 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400468 }
469
470 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
471 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
472 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
473 // destination proxy)
474 // TODO: should this policy decision just be moved into the drawing manager?
475 direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
476
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400477 return direct->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(),
478 srcInfo.height(), this->colorInfo().colorType(),
479 srcColorType, src, rowBytes);
Brian Osman45580d32016-11-23 09:37:01 -0500480}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400481
482bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
483 ASSERT_SINGLE_OWNER
484 RETURN_FALSE_IF_ABANDONED
485 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -0400486 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -0400487
Brian Salomon947efe22019-07-16 15:36:11 -0400488 const GrCaps* caps = fContext->priv().caps();
489
Greg Daniel46cfbc62019-06-07 11:43:30 -0400490 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
491 SkASSERT(src->origin() == this->asSurfaceProxy()->origin());
Greg Danielc71c7962020-01-14 16:44:18 -0500492 SkASSERT(src->textureSwizzle() == this->asSurfaceProxy()->textureSwizzle());
493 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -0400494
Stephen White3c0a50f2020-01-16 18:19:54 -0500495 if (this->asSurfaceProxy()->framebufferOnly()) {
496 return false;
497 }
498
Chris Daltonf8e5aad2019-08-02 12:55:23 -0600499 if (!caps->canCopySurface(this->asSurfaceProxy(), src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -0400500 return false;
501 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400502
Greg Daniel16f5c652019-10-29 11:26:01 -0400503 // The swizzle doesn't matter for copies and it is not used.
504 return this->drawingManager()->newCopyRenderTask(
505 GrSurfaceProxyView(sk_ref_sp(src), src->origin(), GrSwizzle()), srcRect,
Greg Daniel46e366a2019-12-16 14:38:36 -0500506 this->readSurfaceView(), dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400507}
Greg Daniel46cfbc62019-06-07 11:43:30 -0400508
Brian Salomonbf6b9792019-08-21 09:38:10 -0400509std::unique_ptr<GrRenderTargetContext> GrSurfaceContext::rescale(
510 const SkImageInfo& info,
511 const SkIRect& srcRect,
512 SkSurface::RescaleGamma rescaleGamma,
513 SkFilterQuality rescaleQuality) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400514 auto direct = fContext->priv().asDirectContext();
515 if (!direct) {
516 return nullptr;
517 }
518 auto rtProxy = this->asRenderTargetProxy();
519 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
520 return nullptr;
521 }
522
Stephen White3c0a50f2020-01-16 18:19:54 -0500523 if (this->asSurfaceProxy()->framebufferOnly()) {
524 return nullptr;
525 }
526
Brian Salomone9ad9982019-07-22 16:17:41 -0400527 // We rescale by drawing and don't currently support drawing to a kUnpremul destination.
528 if (info.alphaType() == kUnpremul_SkAlphaType) {
529 return nullptr;
530 }
531
532 int srcW = srcRect.width();
533 int srcH = srcRect.height();
534 int srcX = srcRect.fLeft;
535 int srcY = srcRect.fTop;
Greg Daniel40903af2020-01-30 14:55:05 -0500536 GrSurfaceProxyView texView = this->readSurfaceView();
Brian Salomone9ad9982019-07-22 16:17:41 -0400537 SkCanvas::SrcRectConstraint constraint = SkCanvas::kStrict_SrcRectConstraint;
Greg Danielc594e622019-10-15 14:01:49 -0400538 GrColorType srcColorType = this->colorInfo().colorType();
Brian Salomonfc118442019-11-22 19:09:27 -0500539 SkAlphaType srcAlphaType = this->colorInfo().alphaType();
Greg Daniel40903af2020-01-30 14:55:05 -0500540 if (!texView.asTextureProxy()) {
541 texView = GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), this->origin(),
542 srcColorType, GrMipMapped::kNo, srcRect,
543 SkBackingFit::kApprox, SkBudgeted::kNo);
544 if (!texView.proxy()) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400545 return nullptr;
546 }
Greg Daniel40903af2020-01-30 14:55:05 -0500547 SkASSERT(texView.asTextureProxy());
Brian Salomone9ad9982019-07-22 16:17:41 -0400548 srcX = 0;
549 srcY = 0;
550 constraint = SkCanvas::kFast_SrcRectConstraint;
551 }
552
553 float sx = (float)info.width() / srcW;
554 float sy = (float)info.height() / srcH;
555
556 // How many bilerp/bicubic steps to do in X and Y. + means upscaling, - means downscaling.
557 int stepsX;
558 int stepsY;
559 if (rescaleQuality > kNone_SkFilterQuality) {
560 stepsX = static_cast<int>((sx > 1.f) ? ceil(log2f(sx)) : floor(log2f(sx)));
561 stepsY = static_cast<int>((sy > 1.f) ? ceil(log2f(sy)) : floor(log2f(sy)));
562 } else {
563 stepsX = sx != 1.f;
564 stepsY = sy != 1.f;
565 }
566 SkASSERT(stepsX || stepsY);
Greg Danielc594e622019-10-15 14:01:49 -0400567
Brian Salomonbf6b9792019-08-21 09:38:10 -0400568 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
569 // pass B is moved to A. If 'this' is the input on the first pass then tempA is null.
570 std::unique_ptr<GrRenderTargetContext> tempA;
571 std::unique_ptr<GrRenderTargetContext> tempB;
572
Brian Salomone9ad9982019-07-22 16:17:41 -0400573 // Assume we should ignore the rescale linear request if the surface has no color space since
574 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400575 if (rescaleGamma == SkSurface::kLinear && this->colorInfo().colorSpace() &&
576 !this->colorInfo().colorSpace()->gammaIsLinear()) {
577 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomonfc118442019-11-22 19:09:27 -0500578 auto xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(), srcAlphaType, cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -0400579 kPremul_SkAlphaType);
580 // We'll fall back to kRGBA_8888 if half float not supported.
Greg Daniele20fcad2020-01-08 11:52:34 -0500581 auto linearRTC = GrRenderTargetContext::MakeWithFallback(
582 fContext, GrColorType::kRGBA_F16, cs, SkBackingFit::kExact, {srcW, srcH}, 1,
583 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomone9ad9982019-07-22 16:17:41 -0400584 if (!linearRTC) {
585 return nullptr;
586 }
Greg Daniel40903af2020-01-30 14:55:05 -0500587 linearRTC->drawTexture(GrNoClip(), std::move(texView), srcAlphaType,
Brian Salomonfc118442019-11-22 19:09:27 -0500588 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
589 SK_PMColor4fWHITE, SkRect::Make(srcRect), SkRect::MakeWH(srcW, srcH),
590 GrAA::kNo, GrQuadAAFlags::kNone, constraint, SkMatrix::I(),
591 std::move(xform));
Greg Daniel40903af2020-01-30 14:55:05 -0500592 texView = linearRTC->readSurfaceView();
593 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -0400594 tempA = std::move(linearRTC);
Brian Salomone9ad9982019-07-22 16:17:41 -0400595 srcX = 0;
596 srcY = 0;
597 constraint = SkCanvas::kFast_SrcRectConstraint;
598 }
599 while (stepsX || stepsY) {
600 int nextW = info.width();
601 int nextH = info.height();
602 if (stepsX < 0) {
603 nextW = info.width() << (-stepsX - 1);
604 stepsX++;
605 } else if (stepsX != 0) {
606 if (stepsX > 1) {
607 nextW = srcW * 2;
608 }
609 --stepsX;
610 }
611 if (stepsY < 0) {
612 nextH = info.height() << (-stepsY - 1);
613 stepsY++;
614 } else if (stepsY != 0) {
615 if (stepsY > 1) {
616 nextH = srcH * 2;
617 }
618 --stepsY;
619 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400620 auto input = tempA ? tempA.get() : this;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400621 GrColorType colorType = input->colorInfo().colorType();
622 auto cs = input->colorInfo().refColorSpace();
Brian Salomone9ad9982019-07-22 16:17:41 -0400623 sk_sp<GrColorSpaceXform> xform;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400624 auto prevAlphaType = input->colorInfo().alphaType();
Brian Salomone9ad9982019-07-22 16:17:41 -0400625 if (!stepsX && !stepsY) {
626 // Might as well fold conversion to final info in the last step.
627 cs = info.refColorSpace();
628 colorType = SkColorTypeToGrColorType(info.colorType());
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400629 xform = GrColorSpaceXform::Make(input->colorInfo().colorSpace(),
630 input->colorInfo().alphaType(), cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -0400631 info.alphaType());
632 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500633 tempB = GrRenderTargetContext::MakeWithFallback(
634 fContext, colorType, std::move(cs), SkBackingFit::kExact, {nextW, nextH}, 1,
635 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400636 if (!tempB) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400637 return nullptr;
638 }
639 auto dstRect = SkRect::MakeWH(nextW, nextH);
640 if (rescaleQuality == kHigh_SkFilterQuality) {
641 SkMatrix matrix;
642 matrix.setScaleTranslate((float)srcW / nextW, (float)srcH / nextH, srcX, srcY);
643 std::unique_ptr<GrFragmentProcessor> fp;
644 auto dir = GrBicubicEffect::Direction::kXY;
645 if (nextW == srcW) {
646 dir = GrBicubicEffect::Direction::kY;
647 } else if (nextH == srcH) {
648 dir = GrBicubicEffect::Direction::kX;
649 }
Greg Daniel40903af2020-01-30 14:55:05 -0500650 if (srcW != texView.proxy()->width() || srcH != texView.proxy()->height()) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400651 auto domain = GrTextureDomain::MakeTexelDomain(
652 SkIRect::MakeXYWH(srcX, srcY, srcW, srcH), GrTextureDomain::kClamp_Mode);
Greg Daniel40903af2020-01-30 14:55:05 -0500653 fp = GrBicubicEffect::Make(texView.detachProxy(), matrix, domain, dir,
654 prevAlphaType);
Brian Salomone9ad9982019-07-22 16:17:41 -0400655 } else {
Greg Daniel40903af2020-01-30 14:55:05 -0500656 fp = GrBicubicEffect::Make(texView.detachProxy(), matrix, dir, prevAlphaType);
Brian Salomone9ad9982019-07-22 16:17:41 -0400657 }
658 if (xform) {
659 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
660 }
661 GrPaint paint;
662 paint.addColorFragmentProcessor(std::move(fp));
663 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400664 tempB->fillRectToRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect,
Brian Salomone9ad9982019-07-22 16:17:41 -0400665 dstRect);
666 } else {
667 auto filter = rescaleQuality == kNone_SkFilterQuality ? GrSamplerState::Filter::kNearest
668 : GrSamplerState::Filter::kBilerp;
669 auto srcSubset = SkRect::MakeXYWH(srcX, srcY, srcW, srcH);
Greg Daniel40903af2020-01-30 14:55:05 -0500670 tempB->drawTexture(GrNoClip(), std::move(texView), srcAlphaType, filter,
Brian Salomonfc118442019-11-22 19:09:27 -0500671 SkBlendMode::kSrc, SK_PMColor4fWHITE, srcSubset, dstRect, GrAA::kNo,
Greg Danielc594e622019-10-15 14:01:49 -0400672 GrQuadAAFlags::kNone, constraint, SkMatrix::I(), std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -0400673 }
Greg Daniel40903af2020-01-30 14:55:05 -0500674 texView = tempB->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -0400675 tempA = std::move(tempB);
Brian Salomone9ad9982019-07-22 16:17:41 -0400676 srcX = srcY = 0;
677 srcW = nextW;
678 srcH = nextH;
679 constraint = SkCanvas::kFast_SrcRectConstraint;
680 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400681 SkASSERT(tempA);
682 return tempA;
Brian Salomone9ad9982019-07-22 16:17:41 -0400683}
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400684
685GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
686 const SkIRect& rect) {
687 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
688 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
689 auto direct = fContext->priv().asDirectContext();
690 if (!direct) {
691 return {};
692 }
693 auto rtProxy = this->asRenderTargetProxy();
694 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
695 return {};
696 }
697
698 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400699 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
700 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400701 // Fail if read color type does not have all of dstCT's color channels and those missing color
702 // channels are in the src.
703 uint32_t dstComponents = GrColorTypeComponentFlags(dstCT);
704 uint32_t legalReadComponents = GrColorTypeComponentFlags(supportedRead.fColorType);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400705 uint32_t srcComponents = GrColorTypeComponentFlags(this->colorInfo().colorType());
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400706 if ((~legalReadComponents & dstComponents) & srcComponents) {
707 return {};
708 }
709
Brian Salomonfb28c6f2020-01-10 13:04:45 -0500710 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400711 !supportedRead.fOffsetAlignmentForTransferBuffer) {
712 return {};
713 }
714
715 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
716 size_t size = rowBytes * rect.height();
717 auto buffer = direct->priv().resourceProvider()->createBuffer(
718 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
719 if (!buffer) {
720 return {};
721 }
722 auto srcRect = rect;
723 bool flip = proxy->origin() == kBottomLeft_GrSurfaceOrigin;
724 if (flip) {
725 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
726 this->height() - rect.fTop);
727 }
Greg Danielbbfec9d2019-08-20 10:56:51 -0400728 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400729 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -0400730 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400731 PixelTransferResult result;
732 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400733 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -0400734 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400735 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
736 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400737 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
738 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400739 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
740 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -0400741 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400742 };
743 }
744 return result;
745}
Greg Daniel46e366a2019-12-16 14:38:36 -0500746
747#ifdef SK_DEBUG
748void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500749 SkASSERT(fReadView.proxy());
750 fReadView.proxy()->validate(fContext);
Greg Daniel46e366a2019-12-16 14:38:36 -0500751 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
Greg Daniel3912a4b2020-01-14 09:56:04 -0500752 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
Greg Daniel46e366a2019-12-16 14:38:36 -0500753
754 this->onValidate();
755}
756#endif
757