blob: 6722d10a17f77e9225ee4b903d58f2cfdbd8bb75 [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,
31 sk_sp<GrSurfaceProxy> proxy,
32 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 Danielbfa19c42019-12-19 16:41:40 -050042 SkASSERT(proxy && proxy->asTextureProxy());
43
44 // TODO: These should be passed in directly or as GrSurfaceProxyView
45 GrSurfaceOrigin origin = proxy->origin();
46 GrSwizzle readSwizzle = proxy->textureSwizzle();
47
48 std::unique_ptr<GrSurfaceContext> surfaceContext;
49 if (GrRenderTargetProxy* rtProxy = proxy->asRenderTargetProxy()) {
50 SkASSERT(kPremul_SkAlphaType == alphaType || kOpaque_SkAlphaType == alphaType);
51 // Will we ever want a swizzle that is not the default output swizzle for the format and
52 // colorType here? If so we will need to manually pass that in.
53 GrSwizzle outSwizzle = context->priv().caps()->getOutputSwizzle(proxy->backendFormat(),
54 colorType);
55 surfaceContext.reset(new GrRenderTargetContext(context, sk_ref_sp(rtProxy), colorType,
56 origin, readSwizzle, outSwizzle,
57 std::move(colorSpace), nullptr));
58 } else {
59 surfaceContext.reset(new GrSurfaceContext(context, std::move(proxy), colorType, alphaType,
60 std::move(colorSpace), origin, readSwizzle));
61 }
62 return surfaceContext;
63}
64
65std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -050066 GrRecordingContext* context,
67 const 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,
Greg Danielbfa19c42019-12-19 16:41:40 -050078 SkBudgeted budgeted) {
79 auto config = context->priv().caps()->getConfigFromBackendFormat(format, colorType);
80 if (config == kUnknown_GrPixelConfig) {
81 return nullptr;
82 }
83 GrSurfaceDesc desc;
84 desc.fWidth = dimensions.width();
85 desc.fHeight = dimensions.height();
86 desc.fConfig = config;
87
88 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
89 format, desc, renderable, renderTargetSampleCnt, origin, mipMapped, fit, budgeted,
90 isProtected);
91 if (!proxy) {
92 return nullptr;
93 }
94
95 return GrSurfaceContext::Make(context, std::move(proxy), colorType, alphaType,
96 std::move(colorSpace));
97}
98
99
Greg Danielf41b2bd2019-08-22 16:19:24 -0400100// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
101// GrOpsTasks to be picked up and added to by renderTargetContexts lower in the call
102// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
103// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500104GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Greg Daniel46e366a2019-12-16 14:38:36 -0500105 sk_sp<GrSurfaceProxy> proxy,
Brian Salomond6287472019-06-24 15:50:07 -0400106 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -0400107 SkAlphaType alphaType,
Greg Daniel901b98e2019-10-22 09:54:02 -0400108 sk_sp<SkColorSpace> colorSpace,
109 GrSurfaceOrigin origin,
Greg Daniel46e366a2019-12-16 14:38:36 -0500110 GrSwizzle readSwizzle)
Greg Daniel901b98e2019-10-22 09:54:02 -0400111 : fContext(context)
Greg Daniel46e366a2019-12-16 14:38:36 -0500112 , fSurfaceProxy(std::move(proxy))
Greg Daniel901b98e2019-10-22 09:54:02 -0400113 , fOrigin(origin)
114 , fColorInfo(colorType, alphaType, std::move(colorSpace))
Greg Daniele20fcad2020-01-08 11:52:34 -0500115 , fReadSwizzle(readSwizzle) {
116 SkASSERT(!context->priv().abandoned());
117}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400118
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400119const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); }
120
Robert Phillips0d075de2019-03-04 11:08:13 -0500121GrAuditTrail* GrSurfaceContext::auditTrail() {
122 return fContext->priv().auditTrail();
123}
124
125GrDrawingManager* GrSurfaceContext::drawingManager() {
126 return fContext->priv().drawingManager();
127}
128
129const GrDrawingManager* GrSurfaceContext::drawingManager() const {
130 return fContext->priv().drawingManager();
131}
132
133#ifdef SK_DEBUG
134GrSingleOwner* GrSurfaceContext::singleOwner() {
135 return fContext->priv().singleOwner();
136}
137#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400138
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400139bool GrSurfaceContext::readPixels(const GrImageInfo& origDstInfo, void* dst, size_t rowBytes,
Brian Salomon1d435302019-07-01 13:05:28 -0400140 SkIPoint pt, GrContext* direct) {
141 ASSERT_SINGLE_OWNER
142 RETURN_FALSE_IF_ABANDONED
143 SkDEBUGCODE(this->validate();)
144 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Greg Daniel6eb8c242019-06-05 10:22:24 -0400145
Brian Salomon1d435302019-07-01 13:05:28 -0400146 if (!direct && !(direct = fContext->priv().asDirectContext())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400147 return false;
148 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400149
Brian Salomon1d435302019-07-01 13:05:28 -0400150 if (!dst) {
151 return false;
152 }
153
Brian Salomon1047a492019-07-02 12:25:21 -0400154 size_t tightRowBytes = origDstInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400155 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400156 rowBytes = tightRowBytes;
157 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400158 return false;
159 }
160
161 if (!origDstInfo.isValid()) {
162 return false;
163 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400164
165 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
166
167 // MDB TODO: delay this instantiation until later in the method
168 if (!srcProxy->instantiate(direct->priv().resourceProvider())) {
169 return false;
170 }
171
172 GrSurface* srcSurface = srcProxy->peekSurface();
173
Brian Salomon1d435302019-07-01 13:05:28 -0400174 auto dstInfo = origDstInfo;
175 if (!dstInfo.clip(this->width(), this->height(), &pt, &dst, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400176 return false;
177 }
Brian Salomon1047a492019-07-02 12:25:21 -0400178 // Our tight row bytes may have been changed by clipping.
179 tightRowBytes = dstInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400180
Mike Klein7321e6a2019-12-03 11:08:40 -0600181 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{this->colorInfo(), dstInfo}.flags;
182 bool unpremul = flags.unpremul,
183 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
184 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400185
186 const GrCaps* caps = direct->priv().caps();
187 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
188 // care so much about getImageData performance. However, in order to ensure putImageData/
189 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
190 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
191 // fContext->vaildaPMUPMConversionExists()).
Greg Daniel0258c902019-08-01 13:08:33 -0400192 GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
193 GrRenderable::kYes);
Brian Salomon1d435302019-07-01 13:05:28 -0400194 bool canvas2DFastPath = unpremul && !needColorConversion &&
195 (GrColorType::kRGBA_8888 == dstInfo.colorType() ||
196 GrColorType::kBGRA_8888 == dstInfo.colorType()) &&
197 SkToBool(srcProxy->asTextureProxy()) &&
198 (srcProxy->config() == kRGBA_8888_GrPixelConfig ||
199 srcProxy->config() == kBGRA_8888_GrPixelConfig) &&
Greg Daniel0258c902019-08-01 13:08:33 -0400200 defaultRGBAFormat.isValid() &&
Brian Salomon1d435302019-07-01 13:05:28 -0400201 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400202
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400203 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400204 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400205 return false;
206 }
207
Brian Salomondc0710f2019-07-01 14:59:32 -0400208 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400209 GrColorType colorType =
210 canvas2DFastPath ? GrColorType::kRGBA_8888 : this->colorInfo().colorType();
211 sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorInfo().refColorSpace();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400212
Greg Daniele20fcad2020-01-08 11:52:34 -0500213 auto tempCtx = GrRenderTargetContext::Make(
214 direct, colorType, std::move(cs), SkBackingFit::kApprox, dstInfo.dimensions(),
215 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400216 if (!tempCtx) {
217 return false;
218 }
219
220 std::unique_ptr<GrFragmentProcessor> fp;
221 if (canvas2DFastPath) {
Brian Salomonbfb72112020-01-13 10:51:50 -0500222 fp = direct->priv().createPMToUPMEffect(GrTextureEffect::Make(
223 sk_ref_sp(srcProxy->asTextureProxy()), this->colorInfo().alphaType()));
Brian Salomon1d435302019-07-01 13:05:28 -0400224 if (dstInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400225 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
Brian Salomon1d435302019-07-01 13:05:28 -0400226 dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400227 }
Brian Salomon1d435302019-07-01 13:05:28 -0400228 // The render target context is incorrectly tagged as kPremul even though we're writing
229 // unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type so we don't
230 // double unpremul.
231 dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400232 } else {
Brian Salomonb8f098d2020-01-07 11:15:44 -0500233 fp = GrTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()),
Brian Salomonbfb72112020-01-13 10:51:50 -0500234 this->colorInfo().alphaType());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400235 }
236 if (!fp) {
237 return false;
238 }
239 GrPaint paint;
240 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
241 paint.addColorFragmentProcessor(std::move(fp));
242
243 tempCtx->asRenderTargetContext()->fillRectToRect(
244 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400245 SkRect::MakeWH(dstInfo.width(), dstInfo.height()),
246 SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400247
Brian Salomon1d435302019-07-01 13:05:28 -0400248 return tempCtx->readPixels(dstInfo, dst, rowBytes, {0, 0}, direct);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400249 }
250
Greg Daniel6eb8c242019-06-05 10:22:24 -0400251 bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400252
Brian Salomon1d435302019-07-01 13:05:28 -0400253 auto supportedRead = caps->supportedReadPixelsColorType(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400254 this->colorInfo().colorType(), srcProxy->backendFormat(), dstInfo.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400255
Brian Salomon1047a492019-07-02 12:25:21 -0400256 bool makeTight = !caps->readPixelsRowBytesSupport() && tightRowBytes != rowBytes;
257
258 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomon8f8354a2019-07-31 20:12:02 -0400259 (dstInfo.colorType() != supportedRead.fColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400260
Brian Salomonf30b1c12019-06-20 12:25:02 -0400261 std::unique_ptr<char[]> tmpPixels;
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400262 GrImageInfo tmpInfo;
Brian Salomon1d435302019-07-01 13:05:28 -0400263 void* readDst = dst;
264 size_t readRB = rowBytes;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400265 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400266 tmpInfo = {supportedRead.fColorType, this->colorInfo().alphaType(),
267 this->colorInfo().refColorSpace(), dstInfo.width(), dstInfo.height()};
Brian Salomon1d435302019-07-01 13:05:28 -0400268 size_t tmpRB = tmpInfo.minRowBytes();
269 size_t size = tmpRB * tmpInfo.height();
270 // Chrome MSAN bots require the data to be initialized (hence the ()).
271 tmpPixels.reset(new char[size]());
Brian Salomonf30b1c12019-06-20 12:25:02 -0400272
Brian Salomonf30b1c12019-06-20 12:25:02 -0400273 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400274 readRB = tmpRB;
275 pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400276 }
277
278 direct->priv().flushSurface(srcProxy);
279
Brian Salomon1d435302019-07-01 13:05:28 -0400280 if (!direct->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400281 dstInfo.height(), this->colorInfo().colorType(),
Brian Salomonf77c1462019-08-01 15:19:29 -0400282 supportedRead.fColorType, readDst, readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400283 return false;
284 }
285
Greg Daniel6eb8c242019-06-05 10:22:24 -0400286 if (convert) {
Brian Salomon8f8354a2019-07-31 20:12:02 -0400287 return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400288 }
289 return true;
290}
Robert Phillips0d075de2019-03-04 11:08:13 -0500291
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400292bool GrSurfaceContext::writePixels(const GrImageInfo& origSrcInfo, const void* src, size_t rowBytes,
Brian Salomon1d435302019-07-01 13:05:28 -0400293 SkIPoint pt, GrContext* direct) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400294 ASSERT_SINGLE_OWNER
295 RETURN_FALSE_IF_ABANDONED
296 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400297 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400298
Brian Salomon1d435302019-07-01 13:05:28 -0400299 if (!direct && !(direct = fContext->priv().asDirectContext())) {
Brian Salomonc320b152018-02-20 14:05:36 -0500300 return false;
301 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500302
Brian Salomon1d435302019-07-01 13:05:28 -0400303 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500304 return false;
305 }
306
Brian Salomon1d435302019-07-01 13:05:28 -0400307 if (!src) {
308 return false;
309 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400310
Brian Salomon1047a492019-07-02 12:25:21 -0400311 size_t tightRowBytes = origSrcInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400312 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400313 rowBytes = tightRowBytes;
314 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400315 return false;
316 }
317
318 if (!origSrcInfo.isValid()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400319 return false;
320 }
321
322 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
323 if (!dstProxy->instantiate(direct->priv().resourceProvider())) {
324 return false;
325 }
326
327 GrSurface* dstSurface = dstProxy->peekSurface();
328
Brian Salomon1d435302019-07-01 13:05:28 -0400329 auto srcInfo = origSrcInfo;
330 if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400331 return false;
332 }
Brian Salomon1047a492019-07-02 12:25:21 -0400333 // Our tight row bytes may have been changed by clipping.
334 tightRowBytes = srcInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400335
Mike Klein7321e6a2019-12-03 11:08:40 -0600336 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{srcInfo, this->colorInfo()}.flags;
337 bool unpremul = flags.unpremul,
338 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
339 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400340
341 const GrCaps* caps = direct->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400342
343 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
344 GrRenderable::kNo);
345
Greg Daniel6eb8c242019-06-05 10:22:24 -0400346 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
347 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400348 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
349 (srcInfo.colorType() == GrColorType::kRGBA_8888 ||
350 srcInfo.colorType() == GrColorType::kBGRA_8888) &&
351 SkToBool(this->asRenderTargetContext()) &&
352 (dstProxy->config() == kRGBA_8888_GrPixelConfig ||
353 dstProxy->config() == kBGRA_8888_GrPixelConfig) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400354 rgbaDefaultFormat.isValid() &&
Brian Salomon1d435302019-07-01 13:05:28 -0400355 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400356
357 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
358 GrSurfaceDesc desc;
Brian Salomon1d435302019-07-01 13:05:28 -0400359 desc.fWidth = srcInfo.width();
360 desc.fHeight = srcInfo.height();
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) {
367 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomond6287472019-06-24 15:50:07 -0400368 colorType = GrColorType::kRGBA_8888;
Greg Daniel7bfc9132019-08-14 14:23:53 -0400369 format = rgbaDefaultFormat;
Brian Salomone7499c72019-06-24 12:12:36 -0400370 alphaType = kUnpremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400371 } else {
372 desc.fConfig = dstProxy->config();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400373 colorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400374 format = dstProxy->backendFormat().makeTexture2D();
375 if (!format.isValid()) {
376 return false;
377 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400378 alphaType = this->colorInfo().alphaType();
Greg Danielbfa19c42019-12-19 16:41:40 -0500379 tempReadSwizzle = this->readSwizzle();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400380 }
381
Greg Daniel2e52ad12019-06-13 10:04:16 -0400382 // It is more efficient for us to write pixels into a top left origin so we prefer that.
383 // However, if the final proxy isn't a render target then we must use a copy to move the
384 // data into it which requires the origins to match. If the final proxy is a render target
385 // we can use a draw instead which doesn't have this origin restriction. Thus for render
386 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400387 GrSurfaceOrigin tempOrigin =
388 this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : dstProxy->origin();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400389 auto tempProxy = direct->priv().proxyProvider()->createProxy(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400390 format, desc, GrRenderable::kNo, 1, tempOrigin, GrMipMapped::kNo,
391 SkBackingFit::kApprox, SkBudgeted::kYes, GrProtected::kNo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400392 if (!tempProxy) {
393 return false;
394 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500395 SkASSERT(tempProxy->textureSwizzle() == tempReadSwizzle);
396 GrSurfaceContext tempCtx(direct, tempProxy, colorType, alphaType,
397 this->colorInfo().refColorSpace(), tempOrigin, tempReadSwizzle);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400398
399 // In the fast path we always write the srcData to the temp context as though it were RGBA.
400 // When the data is really BGRA the write will cause the R and B channels to be swapped in
401 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
402 // dst.
Brian Salomon1d435302019-07-01 13:05:28 -0400403 if (canvas2DFastPath) {
404 srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888);
405 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500406 if (!tempCtx.writePixels(srcInfo, src, rowBytes, {0, 0}, direct)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400407 return false;
408 }
409
410 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400411 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400412 if (canvas2DFastPath) {
Brian Salomonb8f098d2020-01-07 11:15:44 -0500413 fp = direct->priv().createUPMToPMEffect(
Brian Salomonbfb72112020-01-13 10:51:50 -0500414 GrTextureEffect::Make(std::move(tempProxy), alphaType));
Brian Salomon1d435302019-07-01 13:05:28 -0400415 // Important: check the original src color type here!
416 if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400417 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
418 }
419 } else {
Brian Salomonbfb72112020-01-13 10:51:50 -0500420 fp = GrTextureEffect::Make(std::move(tempProxy), alphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400421 }
422 if (!fp) {
423 return false;
424 }
425 GrPaint paint;
426 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
427 paint.addColorFragmentProcessor(std::move(fp));
428 this->asRenderTargetContext()->fillRectToRect(
429 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400430 SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()),
431 SkRect::MakeWH(srcInfo.width(), srcInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400432 } else {
Brian Salomon1d435302019-07-01 13:05:28 -0400433 SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height());
434 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400435 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400436 return false;
437 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400438 }
439 return true;
440 }
441
Brian Salomon1d435302019-07-01 13:05:28 -0400442 GrColorType allowedColorType =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400443 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400444 dstProxy->backendFormat(),
445 srcInfo.colorType()).fColorType;
Brian Salomon1d435302019-07-01 13:05:28 -0400446 bool flip = dstProxy->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon1047a492019-07-02 12:25:21 -0400447 bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes;
448 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomon1d435302019-07-01 13:05:28 -0400449 (srcInfo.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400450
Brian Salomonf30b1c12019-06-20 12:25:02 -0400451 std::unique_ptr<char[]> tmpPixels;
Brian Salomon1d435302019-07-01 13:05:28 -0400452 GrColorType srcColorType = srcInfo.colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400453 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400454 GrImageInfo tmpInfo(allowedColorType, this->colorInfo().alphaType(),
455 this->colorInfo().refColorSpace(), srcInfo.width(), srcInfo.height());
Brian Salomon1d435302019-07-01 13:05:28 -0400456 auto tmpRB = tmpInfo.minRowBytes();
457 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400458
Brian Salomon1d435302019-07-01 13:05:28 -0400459 GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400460
Brian Salomon1d435302019-07-01 13:05:28 -0400461 srcColorType = tmpInfo.colorType();
462 rowBytes = tmpRB;
463 src = tmpPixels.get();
464 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400465 }
466
467 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
468 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
469 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
470 // destination proxy)
471 // TODO: should this policy decision just be moved into the drawing manager?
472 direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
473
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400474 return direct->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(),
475 srcInfo.height(), this->colorInfo().colorType(),
476 srcColorType, src, rowBytes);
Brian Osman45580d32016-11-23 09:37:01 -0500477}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400478
479bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
480 ASSERT_SINGLE_OWNER
481 RETURN_FALSE_IF_ABANDONED
482 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -0400483 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -0400484
Brian Salomon947efe22019-07-16 15:36:11 -0400485 const GrCaps* caps = fContext->priv().caps();
486
Greg Daniel46cfbc62019-06-07 11:43:30 -0400487 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
488 SkASSERT(src->origin() == this->asSurfaceProxy()->origin());
Brian Salomon947efe22019-07-16 15:36:11 -0400489 SkASSERT(caps->makeConfigSpecific(src->config(), src->backendFormat()) ==
490 caps->makeConfigSpecific(this->asSurfaceProxy()->config(),
491 this->asSurfaceProxy()->backendFormat()));
Greg Daniel46cfbc62019-06-07 11:43:30 -0400492
Chris Daltonf8e5aad2019-08-02 12:55:23 -0600493 if (!caps->canCopySurface(this->asSurfaceProxy(), src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -0400494 return false;
495 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400496
Greg Daniel16f5c652019-10-29 11:26:01 -0400497 // The swizzle doesn't matter for copies and it is not used.
498 return this->drawingManager()->newCopyRenderTask(
499 GrSurfaceProxyView(sk_ref_sp(src), src->origin(), GrSwizzle()), srcRect,
Greg Daniel46e366a2019-12-16 14:38:36 -0500500 this->readSurfaceView(), dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400501}
Greg Daniel46cfbc62019-06-07 11:43:30 -0400502
Brian Salomonbf6b9792019-08-21 09:38:10 -0400503std::unique_ptr<GrRenderTargetContext> GrSurfaceContext::rescale(
504 const SkImageInfo& info,
505 const SkIRect& srcRect,
506 SkSurface::RescaleGamma rescaleGamma,
507 SkFilterQuality rescaleQuality) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400508 auto direct = fContext->priv().asDirectContext();
509 if (!direct) {
510 return nullptr;
511 }
512 auto rtProxy = this->asRenderTargetProxy();
513 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
514 return nullptr;
515 }
516
517 // 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;
526 sk_sp<GrTextureProxy> texProxy = sk_ref_sp(this->asTextureProxy());
527 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();
Brian Salomone9ad9982019-07-22 16:17:41 -0400530 if (!texProxy) {
Brian Salomonfc118442019-11-22 19:09:27 -0500531 texProxy = GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), GrMipMapped::kNo, srcRect,
532 SkBackingFit::kApprox, SkBudgeted::kNo);
Brian Salomone9ad9982019-07-22 16:17:41 -0400533 if (!texProxy) {
534 return nullptr;
535 }
536 srcX = 0;
537 srcY = 0;
538 constraint = SkCanvas::kFast_SrcRectConstraint;
539 }
540
541 float sx = (float)info.width() / srcW;
542 float sy = (float)info.height() / srcH;
543
544 // How many bilerp/bicubic steps to do in X and Y. + means upscaling, - means downscaling.
545 int stepsX;
546 int stepsY;
547 if (rescaleQuality > kNone_SkFilterQuality) {
548 stepsX = static_cast<int>((sx > 1.f) ? ceil(log2f(sx)) : floor(log2f(sx)));
549 stepsY = static_cast<int>((sy > 1.f) ? ceil(log2f(sy)) : floor(log2f(sy)));
550 } else {
551 stepsX = sx != 1.f;
552 stepsY = sy != 1.f;
553 }
554 SkASSERT(stepsX || stepsY);
Greg Danielc594e622019-10-15 14:01:49 -0400555
Brian Salomonbf6b9792019-08-21 09:38:10 -0400556 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
557 // pass B is moved to A. If 'this' is the input on the first pass then tempA is null.
558 std::unique_ptr<GrRenderTargetContext> tempA;
559 std::unique_ptr<GrRenderTargetContext> tempB;
560
Brian Salomone9ad9982019-07-22 16:17:41 -0400561 // Assume we should ignore the rescale linear request if the surface has no color space since
562 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400563 if (rescaleGamma == SkSurface::kLinear && this->colorInfo().colorSpace() &&
564 !this->colorInfo().colorSpace()->gammaIsLinear()) {
565 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomonfc118442019-11-22 19:09:27 -0500566 auto xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(), srcAlphaType, cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -0400567 kPremul_SkAlphaType);
568 // We'll fall back to kRGBA_8888 if half float not supported.
Greg Daniele20fcad2020-01-08 11:52:34 -0500569 auto linearRTC = GrRenderTargetContext::MakeWithFallback(
570 fContext, GrColorType::kRGBA_F16, cs, SkBackingFit::kExact, {srcW, srcH}, 1,
571 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomone9ad9982019-07-22 16:17:41 -0400572 if (!linearRTC) {
573 return nullptr;
574 }
Brian Salomonfc118442019-11-22 19:09:27 -0500575 linearRTC->drawTexture(GrNoClip(), texProxy, srcColorType, srcAlphaType,
576 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
577 SK_PMColor4fWHITE, SkRect::Make(srcRect), SkRect::MakeWH(srcW, srcH),
578 GrAA::kNo, GrQuadAAFlags::kNone, constraint, SkMatrix::I(),
579 std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -0400580 texProxy = linearRTC->asTextureProxyRef();
Brian Salomonbf6b9792019-08-21 09:38:10 -0400581 tempA = std::move(linearRTC);
Brian Salomone9ad9982019-07-22 16:17:41 -0400582 srcX = 0;
583 srcY = 0;
584 constraint = SkCanvas::kFast_SrcRectConstraint;
585 }
586 while (stepsX || stepsY) {
587 int nextW = info.width();
588 int nextH = info.height();
589 if (stepsX < 0) {
590 nextW = info.width() << (-stepsX - 1);
591 stepsX++;
592 } else if (stepsX != 0) {
593 if (stepsX > 1) {
594 nextW = srcW * 2;
595 }
596 --stepsX;
597 }
598 if (stepsY < 0) {
599 nextH = info.height() << (-stepsY - 1);
600 stepsY++;
601 } else if (stepsY != 0) {
602 if (stepsY > 1) {
603 nextH = srcH * 2;
604 }
605 --stepsY;
606 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400607 auto input = tempA ? tempA.get() : this;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400608 GrColorType colorType = input->colorInfo().colorType();
609 auto cs = input->colorInfo().refColorSpace();
Brian Salomone9ad9982019-07-22 16:17:41 -0400610 sk_sp<GrColorSpaceXform> xform;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400611 auto prevAlphaType = input->colorInfo().alphaType();
Brian Salomone9ad9982019-07-22 16:17:41 -0400612 if (!stepsX && !stepsY) {
613 // Might as well fold conversion to final info in the last step.
614 cs = info.refColorSpace();
615 colorType = SkColorTypeToGrColorType(info.colorType());
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400616 xform = GrColorSpaceXform::Make(input->colorInfo().colorSpace(),
617 input->colorInfo().alphaType(), cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -0400618 info.alphaType());
619 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500620 tempB = GrRenderTargetContext::MakeWithFallback(
621 fContext, colorType, std::move(cs), SkBackingFit::kExact, {nextW, nextH}, 1,
622 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400623 if (!tempB) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400624 return nullptr;
625 }
626 auto dstRect = SkRect::MakeWH(nextW, nextH);
627 if (rescaleQuality == kHigh_SkFilterQuality) {
628 SkMatrix matrix;
629 matrix.setScaleTranslate((float)srcW / nextW, (float)srcH / nextH, srcX, srcY);
630 std::unique_ptr<GrFragmentProcessor> fp;
631 auto dir = GrBicubicEffect::Direction::kXY;
632 if (nextW == srcW) {
633 dir = GrBicubicEffect::Direction::kY;
634 } else if (nextH == srcH) {
635 dir = GrBicubicEffect::Direction::kX;
636 }
637 if (srcW != texProxy->width() || srcH != texProxy->height()) {
638 auto domain = GrTextureDomain::MakeTexelDomain(
639 SkIRect::MakeXYWH(srcX, srcY, srcW, srcH), GrTextureDomain::kClamp_Mode);
Brian Salomonfc118442019-11-22 19:09:27 -0500640 fp = GrBicubicEffect::Make(texProxy, matrix, domain, dir, prevAlphaType);
Brian Salomone9ad9982019-07-22 16:17:41 -0400641 } else {
Brian Salomonfc118442019-11-22 19:09:27 -0500642 fp = GrBicubicEffect::Make(texProxy, matrix, dir, prevAlphaType);
Brian Salomone9ad9982019-07-22 16:17:41 -0400643 }
644 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);
Brian Salomonfc118442019-11-22 19:09:27 -0500656 tempB->drawTexture(GrNoClip(), texProxy, srcColorType, srcAlphaType, filter,
657 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 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400660 texProxy = tempB->asTextureProxyRef();
661 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.
689 uint32_t dstComponents = GrColorTypeComponentFlags(dstCT);
690 uint32_t legalReadComponents = GrColorTypeComponentFlags(supportedRead.fColorType);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400691 uint32_t srcComponents = GrColorTypeComponentFlags(this->colorInfo().colorType());
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400692 if ((~legalReadComponents & dstComponents) & srcComponents) {
693 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;
709 bool flip = proxy->origin() == kBottomLeft_GrSurfaceOrigin;
710 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 {
735 SkASSERT(fSurfaceProxy);
736 fSurfaceProxy->validate(fContext);
737 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
738 this->colorInfo().colorType(), fSurfaceProxy->backendFormat()));
739
740 this->onValidate();
741}
742#endif
743