blob: 5e51da4aaca2540abadce378becc27a7bcffdf83 [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
John Stilesfbd050b2020-08-03 13:21:46 -040010#include <memory>
11
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040012#include "include/gpu/GrDirectContext.h"
13#include "include/gpu/GrRecordingContext.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040014#include "src/core/SkAutoPixmapStorage.h"
Brian Salomon63a0a752020-06-26 13:32:09 -040015#include "src/core/SkYUVMath.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040016#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrContextPriv.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040018#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrDrawingManager.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040020#include "src/gpu/GrGpu.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040021#include "src/gpu/GrImageInfo.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040022#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrRecordingContextPriv.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040024#include "src/gpu/GrRenderTargetContext.h"
Greg Daniel46cfbc62019-06-07 11:43:30 -040025#include "src/gpu/GrSurfaceContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/SkGr.h"
Brian Salomone9ad9982019-07-22 16:17:41 -040027#include "src/gpu/effects/GrBicubicEffect.h"
Brian Salomon63a0a752020-06-26 13:32:09 -040028#include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h"
Brian Osman45580d32016-11-23 09:37:01 -050029
Adlai Holler33dbd652020-06-01 12:35:42 -040030#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
Robert Phillips9eb00022020-06-30 15:30:12 -040031#define RETURN_FALSE_IF_ABANDONED if (this->fContext->abandoned()) { return false; }
Brian Osman45580d32016-11-23 09:37:01 -050032
Greg Danielbfa19c42019-12-19 16:41:40 -050033std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -050034 GrSurfaceProxyView readView,
Greg Danielbfa19c42019-12-19 16:41:40 -050035 GrColorType colorType,
36 SkAlphaType alphaType,
37 sk_sp<SkColorSpace> colorSpace) {
Greg Daniele20fcad2020-01-08 11:52:34 -050038 // It is probably not necessary to check if the context is abandoned here since uses of the
39 // GrSurfaceContext which need the context will mostly likely fail later on without an issue.
40 // However having this hear adds some reassurance in case there is a path doesn't handle an
41 // abandoned context correctly. It also lets us early out of some extra work.
Robert Phillips9eb00022020-06-30 15:30:12 -040042 if (context->abandoned()) {
Greg Daniele20fcad2020-01-08 11:52:34 -050043 return nullptr;
44 }
Greg Daniel3912a4b2020-01-14 09:56:04 -050045 GrSurfaceProxy* proxy = readView.proxy();
Greg Danielbfa19c42019-12-19 16:41:40 -050046 SkASSERT(proxy && proxy->asTextureProxy());
47
Greg Danielbfa19c42019-12-19 16:41:40 -050048 std::unique_ptr<GrSurfaceContext> surfaceContext;
Greg Daniel3912a4b2020-01-14 09:56:04 -050049 if (proxy->asRenderTargetProxy()) {
Greg Danielbfa19c42019-12-19 16:41:40 -050050 SkASSERT(kPremul_SkAlphaType == alphaType || kOpaque_SkAlphaType == alphaType);
Brian Salomon8afde5f2020-04-01 16:22:00 -040051 // Will we ever want a swizzle that is not the default write swizzle for the format and
Greg Danielbfa19c42019-12-19 16:41:40 -050052 // colorType here? If so we will need to manually pass that in.
Brian Salomonc5243782020-04-02 12:50:34 -040053 GrSwizzle writeSwizzle;
54 if (colorType != GrColorType::kUnknown) {
55 writeSwizzle =
56 context->priv().caps()->getWriteSwizzle(proxy->backendFormat(), colorType);
57 }
Brian Salomon8afde5f2020-04-01 16:22:00 -040058 GrSurfaceProxyView writeView(readView.refProxy(), readView.origin(), writeSwizzle);
John Stilesfbd050b2020-08-03 13:21:46 -040059 surfaceContext = std::make_unique<GrRenderTargetContext>(context, std::move(readView),
Brian Salomon8afde5f2020-04-01 16:22:00 -040060 std::move(writeView), colorType,
John Stilesfbd050b2020-08-03 13:21:46 -040061 std::move(colorSpace), nullptr);
Greg Danielbfa19c42019-12-19 16:41:40 -050062 } else {
John Stilesfbd050b2020-08-03 13:21:46 -040063 surfaceContext = std::make_unique<GrSurfaceContext>(context, std::move(readView), colorType,
64 alphaType, std::move(colorSpace));
Greg Danielbfa19c42019-12-19 16:41:40 -050065 }
Robert Phillips07f0e412020-01-17 15:20:00 -050066 SkDEBUGCODE(surfaceContext->validate();)
Greg Danielbfa19c42019-12-19 16:41:40 -050067 return surfaceContext;
68}
69
Brian Salomona56a7462020-02-07 14:17:25 -050070std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
71 SkISize dimensions,
72 const GrBackendFormat& format,
73 GrRenderable renderable,
74 int renderTargetSampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -040075 GrMipmapped mipMapped,
Brian Salomona56a7462020-02-07 14:17:25 -050076 GrProtected isProtected,
77 GrSurfaceOrigin origin,
78 GrColorType colorType,
79 SkAlphaType alphaType,
80 sk_sp<SkColorSpace> colorSpace,
81 SkBackingFit fit,
82 SkBudgeted budgeted) {
Brian Salomonc5243782020-04-02 12:50:34 -040083 GrSwizzle swizzle;
84 if (colorType != GrColorType::kUnknown && !context->priv().caps()->isFormatCompressed(format)) {
Brian Salomond005b692020-04-01 15:47:05 -040085 swizzle = context->priv().caps()->getReadSwizzle(format, colorType);
86 }
Greg Daniel47c20e82020-01-21 14:29:57 -050087
Greg Danielbfa19c42019-12-19 16:41:40 -050088 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -040089 format, dimensions, renderable, renderTargetSampleCnt, mipMapped, fit, budgeted,
90 isProtected);
Greg Danielbfa19c42019-12-19 16:41:40 -050091 if (!proxy) {
92 return nullptr;
93 }
94
Greg Daniel3912a4b2020-01-14 09:56:04 -050095 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
96 return GrSurfaceContext::Make(context, std::move(view), colorType, alphaType,
Greg Danielbfa19c42019-12-19 16:41:40 -050097 std::move(colorSpace));
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 Daniel3912a4b2020-01-14 09:56:04 -0500105 GrSurfaceProxyView readView,
Brian Salomond6287472019-06-24 15:50:07 -0400106 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -0400107 SkAlphaType alphaType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500108 sk_sp<SkColorSpace> colorSpace)
Greg Daniel901b98e2019-10-22 09:54:02 -0400109 : fContext(context)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500110 , fReadView(std::move(readView))
111 , fColorInfo(colorType, alphaType, std::move(colorSpace)) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400112 SkASSERT(!context->abandoned());
Greg Daniele20fcad2020-01-08 11:52:34 -0500113}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400114
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400115const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); }
116
Robert Phillips0d075de2019-03-04 11:08:13 -0500117GrAuditTrail* GrSurfaceContext::auditTrail() {
118 return fContext->priv().auditTrail();
119}
120
121GrDrawingManager* GrSurfaceContext::drawingManager() {
122 return fContext->priv().drawingManager();
123}
124
125const GrDrawingManager* GrSurfaceContext::drawingManager() const {
126 return fContext->priv().drawingManager();
127}
128
129#ifdef SK_DEBUG
130GrSingleOwner* GrSurfaceContext::singleOwner() {
131 return fContext->priv().singleOwner();
132}
133#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400134
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400135bool GrSurfaceContext::readPixels(const GrImageInfo& origDstInfo, void* dst, size_t rowBytes,
Robert Phillips44333c52020-06-30 13:28:00 -0400136 SkIPoint pt, GrDirectContext* direct) {
Brian Salomon1d435302019-07-01 13:05:28 -0400137 ASSERT_SINGLE_OWNER
138 RETURN_FALSE_IF_ABANDONED
139 SkDEBUGCODE(this->validate();)
140 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Greg Daniel6eb8c242019-06-05 10:22:24 -0400141
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400142 if (!direct && !(direct = fContext->asDirectContext())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400143 return false;
144 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400145
Brian Salomon1d435302019-07-01 13:05:28 -0400146 if (!dst) {
147 return false;
148 }
149
Brian Salomon1047a492019-07-02 12:25:21 -0400150 size_t tightRowBytes = origDstInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400151 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400152 rowBytes = tightRowBytes;
153 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400154 return false;
155 }
156
157 if (!origDstInfo.isValid()) {
158 return false;
159 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400160
161 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
162
Stephen White3c0a50f2020-01-16 18:19:54 -0500163 if (srcProxy->framebufferOnly()) {
164 return false;
165 }
166
Greg Daniel6eb8c242019-06-05 10:22:24 -0400167 // 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();
Robert Phillips07f0e412020-01-17 15:20:00 -0500187 bool srcIsCompressed = caps->isFormatCompressed(srcSurface->backendFormat());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400188 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
189 // care so much about getImageData performance. However, in order to ensure putImageData/
190 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
191 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
192 // fContext->vaildaPMUPMConversionExists()).
Greg Daniel0258c902019-08-01 13:08:33 -0400193 GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
194 GrRenderable::kYes);
Greg Danielc71c7962020-01-14 16:44:18 -0500195 GrColorType srcColorType = this->colorInfo().colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400196 bool canvas2DFastPath = unpremul && !needColorConversion &&
197 (GrColorType::kRGBA_8888 == dstInfo.colorType() ||
198 GrColorType::kBGRA_8888 == dstInfo.colorType()) &&
199 SkToBool(srcProxy->asTextureProxy()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500200 (srcColorType == GrColorType::kRGBA_8888 ||
201 srcColorType == GrColorType::kBGRA_8888) &&
Greg Daniel0258c902019-08-01 13:08:33 -0400202 defaultRGBAFormat.isValid() &&
Brian Salomon1d435302019-07-01 13:05:28 -0400203 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400204
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400205 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400206 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400207 return false;
208 }
209
Brian Salomondc0710f2019-07-01 14:59:32 -0400210 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Robert Phillips07f0e412020-01-17 15:20:00 -0500211 GrColorType colorType = (canvas2DFastPath || srcIsCompressed)
212 ? GrColorType::kRGBA_8888 : this->colorInfo().colorType();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400213 sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorInfo().refColorSpace();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400214
Greg Daniele20fcad2020-01-08 11:52:34 -0500215 auto tempCtx = GrRenderTargetContext::Make(
216 direct, colorType, std::move(cs), SkBackingFit::kApprox, dstInfo.dimensions(),
Brian Salomon7e67dca2020-07-21 09:27:25 -0400217 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400218 if (!tempCtx) {
219 return false;
220 }
221
222 std::unique_ptr<GrFragmentProcessor> fp;
223 if (canvas2DFastPath) {
Greg Danield2ccbb52020-02-05 10:45:39 -0500224 fp = direct->priv().createPMToUPMEffect(
225 GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType()));
Brian Salomon1d435302019-07-01 13:05:28 -0400226 if (dstInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400227 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
Brian Salomon1d435302019-07-01 13:05:28 -0400228 dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400229 }
Brian Salomon1d435302019-07-01 13:05:28 -0400230 // The render target context is incorrectly tagged as kPremul even though we're writing
231 // unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type so we don't
232 // double unpremul.
233 dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400234 } else {
Greg Danield2ccbb52020-02-05 10:45:39 -0500235 fp = GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400236 }
237 if (!fp) {
238 return false;
239 }
240 GrPaint paint;
241 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
John Stiles5933d7d2020-07-21 12:28:35 -0400242 paint.setColorFragmentProcessor(std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400243
244 tempCtx->asRenderTargetContext()->fillRectToRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400245 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400246 SkRect::MakeWH(dstInfo.width(), dstInfo.height()),
247 SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400248
Brian Salomon1d435302019-07-01 13:05:28 -0400249 return tempCtx->readPixels(dstInfo, dst, rowBytes, {0, 0}, direct);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400250 }
251
Greg Danielb8d84f82020-02-13 14:25:00 -0500252 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400253
Brian Salomon1d435302019-07-01 13:05:28 -0400254 auto supportedRead = caps->supportedReadPixelsColorType(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400255 this->colorInfo().colorType(), srcProxy->backendFormat(), dstInfo.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400256
Brian Salomon1047a492019-07-02 12:25:21 -0400257 bool makeTight = !caps->readPixelsRowBytesSupport() && tightRowBytes != rowBytes;
258
259 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomon8f8354a2019-07-31 20:12:02 -0400260 (dstInfo.colorType() != supportedRead.fColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400261
Brian Salomonf30b1c12019-06-20 12:25:02 -0400262 std::unique_ptr<char[]> tmpPixels;
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400263 GrImageInfo tmpInfo;
Brian Salomon1d435302019-07-01 13:05:28 -0400264 void* readDst = dst;
265 size_t readRB = rowBytes;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400266 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400267 tmpInfo = {supportedRead.fColorType, this->colorInfo().alphaType(),
268 this->colorInfo().refColorSpace(), dstInfo.width(), dstInfo.height()};
Brian Salomon1d435302019-07-01 13:05:28 -0400269 size_t tmpRB = tmpInfo.minRowBytes();
270 size_t size = tmpRB * tmpInfo.height();
271 // Chrome MSAN bots require the data to be initialized (hence the ()).
John Stilesfbd050b2020-08-03 13:21:46 -0400272 tmpPixels = std::make_unique<char[]>(size);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400273
Brian Salomonf30b1c12019-06-20 12:25:02 -0400274 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400275 readRB = tmpRB;
276 pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400277 }
278
Greg Daniel55f040b2020-02-13 15:38:32 +0000279 direct->priv().flushSurface(srcProxy);
Greg Daniel04283f32020-05-20 13:16:00 -0400280 direct->submit();
Brian Salomon1d435302019-07-01 13:05:28 -0400281 if (!direct->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400282 dstInfo.height(), this->colorInfo().colorType(),
Brian Salomonf77c1462019-08-01 15:19:29 -0400283 supportedRead.fColorType, readDst, readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400284 return false;
285 }
286
Greg Daniel6eb8c242019-06-05 10:22:24 -0400287 if (convert) {
Brian Salomon8f8354a2019-07-31 20:12:02 -0400288 return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400289 }
290 return true;
291}
Robert Phillips0d075de2019-03-04 11:08:13 -0500292
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400293bool GrSurfaceContext::writePixels(const GrImageInfo& origSrcInfo, const void* src, size_t rowBytes,
Robert Phillips44333c52020-06-30 13:28:00 -0400294 SkIPoint pt, GrDirectContext* direct) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400295 ASSERT_SINGLE_OWNER
296 RETURN_FALSE_IF_ABANDONED
297 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400298 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400299
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400300 if (!direct && !(direct = fContext->asDirectContext())) {
Brian Salomonc320b152018-02-20 14:05:36 -0500301 return false;
302 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500303
Brian Salomon1d435302019-07-01 13:05:28 -0400304 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500305 return false;
306 }
307
Brian Salomon1d435302019-07-01 13:05:28 -0400308 if (!src) {
309 return false;
310 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400311
Brian Salomon1047a492019-07-02 12:25:21 -0400312 size_t tightRowBytes = origSrcInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400313 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400314 rowBytes = tightRowBytes;
315 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400316 return false;
317 }
318
319 if (!origSrcInfo.isValid()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400320 return false;
321 }
322
323 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
Stephen White3c0a50f2020-01-16 18:19:54 -0500324
325 if (dstProxy->framebufferOnly()) {
326 return false;
327 }
328
Greg Daniel6eb8c242019-06-05 10:22:24 -0400329 if (!dstProxy->instantiate(direct->priv().resourceProvider())) {
330 return false;
331 }
332
333 GrSurface* dstSurface = dstProxy->peekSurface();
334
Brian Salomon1d435302019-07-01 13:05:28 -0400335 auto srcInfo = origSrcInfo;
336 if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400337 return false;
338 }
Brian Salomon1047a492019-07-02 12:25:21 -0400339 // Our tight row bytes may have been changed by clipping.
340 tightRowBytes = srcInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400341
Mike Klein7321e6a2019-12-03 11:08:40 -0600342 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{srcInfo, this->colorInfo()}.flags;
343 bool unpremul = flags.unpremul,
344 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
345 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400346
347 const GrCaps* caps = direct->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400348
349 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
350 GrRenderable::kNo);
351
Greg Danielc71c7962020-01-14 16:44:18 -0500352 GrColorType dstColorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400353 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
354 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400355 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
356 (srcInfo.colorType() == GrColorType::kRGBA_8888 ||
357 srcInfo.colorType() == GrColorType::kBGRA_8888) &&
358 SkToBool(this->asRenderTargetContext()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500359 (dstColorType == GrColorType::kRGBA_8888 ||
360 dstColorType == GrColorType::kBGRA_8888) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400361 rgbaDefaultFormat.isValid() &&
Brian Salomon1d435302019-07-01 13:05:28 -0400362 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400363
364 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
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 =
Greg Danielb8d84f82020-02-13 14:25:00 -0500390 this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : this->origin();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400391 auto tempProxy = direct->priv().proxyProvider()->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400392 format, srcInfo.dimensions(), GrRenderable::kNo, 1, GrMipmapped::kNo,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400393 SkBackingFit::kApprox, SkBudgeted::kYes, GrProtected::kNo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400394 if (!tempProxy) {
395 return false;
396 }
Greg Daniel3912a4b2020-01-14 09:56:04 -0500397 GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle);
Greg Danield2ccbb52020-02-05 10:45:39 -0500398 GrSurfaceContext tempCtx(direct, tempView, colorType, alphaType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500399 this->colorInfo().refColorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400400
401 // In the fast path we always write the srcData to the temp context as though it were RGBA.
402 // When the data is really BGRA the write will cause the R and B channels to be swapped in
403 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
404 // dst.
Brian Salomon1d435302019-07-01 13:05:28 -0400405 if (canvas2DFastPath) {
406 srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888);
407 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500408 if (!tempCtx.writePixels(srcInfo, src, rowBytes, {0, 0}, direct)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400409 return false;
410 }
411
412 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400413 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400414 if (canvas2DFastPath) {
Brian Salomonb8f098d2020-01-07 11:15:44 -0500415 fp = direct->priv().createUPMToPMEffect(
Greg Danield2ccbb52020-02-05 10:45:39 -0500416 GrTextureEffect::Make(std::move(tempView), alphaType));
Brian Salomon1d435302019-07-01 13:05:28 -0400417 // Important: check the original src color type here!
418 if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400419 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
420 }
421 } else {
Greg Danield2ccbb52020-02-05 10:45:39 -0500422 fp = GrTextureEffect::Make(std::move(tempView), alphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400423 }
424 if (!fp) {
425 return false;
426 }
427 GrPaint paint;
428 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
John Stiles5933d7d2020-07-21 12:28:35 -0400429 paint.setColorFragmentProcessor(std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400430 this->asRenderTargetContext()->fillRectToRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400431 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400432 SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()),
433 SkRect::MakeWH(srcInfo.width(), srcInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400434 } else {
Brian Salomon1d435302019-07-01 13:05:28 -0400435 SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height());
436 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Brian Salomonc5243782020-04-02 12:50:34 -0400437 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400438 return false;
439 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400440 }
441 return true;
442 }
443
Brian Salomon1d435302019-07-01 13:05:28 -0400444 GrColorType allowedColorType =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400445 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400446 dstProxy->backendFormat(),
447 srcInfo.colorType()).fColorType;
Greg Danielb8d84f82020-02-13 14:25:00 -0500448 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon1047a492019-07-02 12:25:21 -0400449 bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes;
450 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomon1d435302019-07-01 13:05:28 -0400451 (srcInfo.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400452
Brian Salomonf30b1c12019-06-20 12:25:02 -0400453 std::unique_ptr<char[]> tmpPixels;
Brian Salomon1d435302019-07-01 13:05:28 -0400454 GrColorType srcColorType = srcInfo.colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400455 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400456 GrImageInfo tmpInfo(allowedColorType, this->colorInfo().alphaType(),
457 this->colorInfo().refColorSpace(), srcInfo.width(), srcInfo.height());
Brian Salomon1d435302019-07-01 13:05:28 -0400458 auto tmpRB = tmpInfo.minRowBytes();
459 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400460
Brian Salomon1d435302019-07-01 13:05:28 -0400461 GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400462
Brian Salomon1d435302019-07-01 13:05:28 -0400463 srcColorType = tmpInfo.colorType();
464 rowBytes = tmpRB;
465 src = tmpPixels.get();
466 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400467 }
468
469 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
470 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
471 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
472 // destination proxy)
473 // TODO: should this policy decision just be moved into the drawing manager?
Greg Daniel55f040b2020-02-13 15:38:32 +0000474 direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400475
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400476 return direct->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(),
477 srcInfo.height(), this->colorInfo().colorType(),
478 srcColorType, src, rowBytes);
Brian Osman45580d32016-11-23 09:37:01 -0500479}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400480
Brian Salomon63a0a752020-06-26 13:32:09 -0400481void GrSurfaceContext::asyncRescaleAndReadPixels(const SkImageInfo& info,
482 const SkIRect& srcRect,
483 RescaleGamma rescaleGamma,
484 SkFilterQuality rescaleQuality,
485 ReadPixelsCallback callback,
486 ReadPixelsContext context) {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400487 auto direct = fContext->asDirectContext();
Brian Salomon63a0a752020-06-26 13:32:09 -0400488
489 // We implement this by rendering and we don't currently support rendering kUnpremul.
490 if (info.alphaType() == kUnpremul_SkAlphaType) {
491 callback(context, nullptr);
492 return;
493 }
494 if (!direct) {
495 callback(context, nullptr);
496 return;
497 }
498 auto rt = this->asRenderTargetProxy();
499 if (rt && rt->wrapsVkSecondaryCB()) {
500 callback(context, nullptr);
501 return;
502 }
503 if (rt && rt->framebufferOnly()) {
504 callback(context, nullptr);
505 return;
506 }
507 auto dstCT = SkColorTypeToGrColorType(info.colorType());
508 if (dstCT == GrColorType::kUnknown) {
509 callback(context, nullptr);
510 return;
511 }
512 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
513 auto colorTypeOfFinalContext = this->colorInfo().colorType();
514 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
515 if (needsRescale) {
516 colorTypeOfFinalContext = dstCT;
517 backendFormatOfFinalContext =
518 this->caps()->getDefaultBackendFormat(dstCT, GrRenderable::kYes);
519 }
520 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
521 backendFormatOfFinalContext, dstCT);
522 // Fail if we can't read from the source surface's color type.
523 if (readInfo.fColorType == GrColorType::kUnknown) {
524 callback(context, nullptr);
525 return;
526 }
527 // Fail if read color type does not have all of dstCT's color channels and those missing color
528 // channels are in the src.
529 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
530 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
531 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
532 if ((~legalReadChannels & dstChannels) & srcChannels) {
533 callback(context, nullptr);
534 return;
535 }
536
537 std::unique_ptr<GrRenderTargetContext> tempRTC;
538 int x = srcRect.fLeft;
539 int y = srcRect.fTop;
540 if (needsRescale) {
541 tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
542 rescaleQuality);
543 if (!tempRTC) {
544 callback(context, nullptr);
545 return;
546 }
547 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
548 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
549 x = y = 0;
550 } else {
551 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
552 this->colorInfo().alphaType(),
553 info.colorSpace(),
554 info.alphaType());
555 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
556 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
557 GrSurfaceProxyView texProxyView = this->readSurfaceView();
558 SkRect srcRectToDraw = SkRect::Make(srcRect);
559 // If the src is not texturable first try to make a copy to a texture.
560 if (!texProxyView.asTextureProxy()) {
561 texProxyView =
Brian Salomon7e67dca2020-07-21 09:27:25 -0400562 GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipmapped::kNo, srcRect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400563 SkBackingFit::kApprox, SkBudgeted::kNo);
564 if (!texProxyView) {
565 callback(context, nullptr);
566 return;
567 }
568 SkASSERT(texProxyView.asTextureProxy());
569 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
570 }
571 tempRTC = GrRenderTargetContext::Make(direct, this->colorInfo().colorType(),
572 info.refColorSpace(), SkBackingFit::kApprox,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400573 srcRect.size(), 1, GrMipmapped::kNo,
Brian Salomon63a0a752020-06-26 13:32:09 -0400574 GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
575 if (!tempRTC) {
576 callback(context, nullptr);
577 return;
578 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400579 tempRTC->drawTexture(nullptr,
580 std::move(texProxyView),
581 this->colorInfo().alphaType(),
582 GrSamplerState::Filter::kNearest,
583 GrSamplerState::MipmapMode::kNone,
584 SkBlendMode::kSrc,
585 SK_PMColor4fWHITE,
586 srcRectToDraw,
587 SkRect::MakeWH(srcRect.width(), srcRect.height()),
588 GrAA::kNo,
589 GrQuadAAFlags::kNone,
590 SkCanvas::kFast_SrcRectConstraint,
591 SkMatrix::I(),
592 std::move(xform));
Brian Salomon63a0a752020-06-26 13:32:09 -0400593 x = y = 0;
594 }
595 }
596 auto rtc = tempRTC ? tempRTC.get() : this;
597 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
598 info.colorType(), callback, context);
599}
600
601class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult {
602public:
603 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
604 ~AsyncReadResult() override {
605 for (int i = 0; i < fPlanes.count(); ++i) {
606 if (!fPlanes[i].fMappedBuffer) {
607 delete[] static_cast<const char*>(fPlanes[i].fData);
608 } else {
609 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
610 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
611 }
612 }
613 }
614
615 int count() const override { return fPlanes.count(); }
616 const void* data(int i) const override { return fPlanes[i].fData; }
617 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
618
619 bool addTransferResult(const PixelTransferResult& result,
620 SkISize dimensions,
621 size_t rowBytes,
622 GrClientMappedBufferManager* manager) {
623 SkASSERT(!result.fTransferBuffer->isMapped());
624 const void* mappedData = result.fTransferBuffer->map();
625 if (!mappedData) {
626 return false;
627 }
628 if (result.fPixelConverter) {
629 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
630 result.fPixelConverter(convertedData.get(), mappedData);
631 this->addCpuPlane(std::move(convertedData), rowBytes);
632 result.fTransferBuffer->unmap();
633 } else {
634 manager->insert(result.fTransferBuffer);
635 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
636 }
637 return true;
638 }
639
640 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
641 SkASSERT(data);
642 SkASSERT(rowBytes > 0);
643 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
644 }
645
646private:
647 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
648 SkASSERT(data);
649 SkASSERT(rowBytes > 0);
650 SkASSERT(mappedBuffer);
651 SkASSERT(mappedBuffer->isMapped());
652 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
653 }
654
655 struct Plane {
656 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
657 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
658 const void* fData;
659 size_t fRowBytes;
660 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
661 sk_sp<GrGpuBuffer> fMappedBuffer;
662 };
663 SkSTArray<3, Plane> fPlanes;
664 uint32_t fInboxID;
665};
666
667void GrSurfaceContext::asyncReadPixels(const SkIRect& rect,
668 SkColorType colorType,
669 ReadPixelsCallback callback,
670 ReadPixelsContext context) {
671 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
672 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
673
674 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
675 callback(context, nullptr);
676 return;
677 }
678
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400679 auto directContext = fContext->asDirectContext();
Brian Salomon63a0a752020-06-26 13:32:09 -0400680 SkASSERT(directContext);
681 auto mappedBufferManager = directContext->priv().clientMappedBufferManager();
682
683 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
684
685 if (!transferResult.fTransferBuffer) {
686 auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
687 this->colorInfo().refColorSpace());
688 auto result = std::make_unique<AsyncReadResult>(0);
689 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
690 SkPixmap pm(ii, data.get(), ii.minRowBytes());
691 result->addCpuPlane(std::move(data), pm.rowBytes());
692
693 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
694 callback(context, nullptr);
695 return;
696 }
697 callback(context, std::move(result));
698 return;
699 }
700
701 struct FinishContext {
702 ReadPixelsCallback* fClientCallback;
703 ReadPixelsContext fClientContext;
704 SkISize fSize;
705 SkColorType fColorType;
706 GrClientMappedBufferManager* fMappedBufferManager;
707 PixelTransferResult fTransferResult;
708 };
709 // Assumption is that the caller would like to flush. We could take a parameter or require an
710 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
711 // callback to GrGpu until after the next flush that flushes our op list, though.
712 auto* finishContext = new FinishContext{callback,
713 context,
714 rect.size(),
715 colorType,
716 mappedBufferManager,
717 std::move(transferResult)};
718 auto finishCallback = [](GrGpuFinishedContext c) {
719 const auto* context = reinterpret_cast<const FinishContext*>(c);
720 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
721 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
722 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
723 context->fMappedBufferManager)) {
724 result.reset();
725 }
726 (*context->fClientCallback)(context->fClientContext, std::move(result));
727 delete context;
728 };
729 GrFlushInfo flushInfo;
730 flushInfo.fFinishedContext = finishContext;
731 flushInfo.fFinishedProc = finishCallback;
732 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo, nullptr);
733}
734
735void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
736 sk_sp<SkColorSpace> dstColorSpace,
737 const SkIRect& srcRect,
738 SkISize dstSize,
739 RescaleGamma rescaleGamma,
740 SkFilterQuality rescaleQuality,
741 ReadPixelsCallback callback,
742 ReadPixelsContext context) {
743 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
744 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
745 SkASSERT(!dstSize.isZero());
746 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
747
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400748 auto direct = fContext->asDirectContext();
Brian Salomon63a0a752020-06-26 13:32:09 -0400749 if (!direct) {
750 callback(context, nullptr);
751 return;
752 }
753 auto rt = this->asRenderTargetProxy();
754 if (rt && rt->wrapsVkSecondaryCB()) {
755 callback(context, nullptr);
756 return;
757 }
758 if (rt && rt->framebufferOnly()) {
759 callback(context, nullptr);
760 return;
761 }
762 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
763 callback(context, nullptr);
764 return;
765 }
766 int x = srcRect.fLeft;
767 int y = srcRect.fTop;
768 bool needsRescale = srcRect.size() != dstSize;
769 GrSurfaceProxyView srcView;
770 if (needsRescale) {
771 // We assume the caller wants kPremul. There is no way to indicate a preference.
772 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
773 dstColorSpace);
774 // TODO: Incorporate the YUV conversion into last pass of rescaling.
775 auto tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
776 rescaleQuality);
777 if (!tempRTC) {
778 callback(context, nullptr);
779 return;
780 }
781 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
782 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
783 x = y = 0;
784 srcView = tempRTC->readSurfaceView();
785 } else {
786 srcView = this->readSurfaceView();
787 if (!srcView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400788 srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipmapped::kNo,
Brian Salomon63a0a752020-06-26 13:32:09 -0400789 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
790 if (!srcView) {
791 // If we can't get a texture copy of the contents then give up.
792 callback(context, nullptr);
793 return;
794 }
795 SkASSERT(srcView.asTextureProxy());
796 x = y = 0;
797 }
798 // We assume the caller wants kPremul. There is no way to indicate a preference.
799 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
800 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
801 kPremul_SkAlphaType);
802 if (xform) {
803 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
804 auto tempRTC = GrRenderTargetContext::Make(
805 direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400806 dstSize, 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400807 if (!tempRTC) {
808 callback(context, nullptr);
809 return;
810 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400811 tempRTC->drawTexture(nullptr,
812 std::move(srcView),
813 this->colorInfo().alphaType(),
814 GrSamplerState::Filter::kNearest,
815 GrSamplerState::MipmapMode::kNone,
816 SkBlendMode::kSrc,
817 SK_PMColor4fWHITE,
818 srcRectToDraw,
819 SkRect::Make(srcRect.size()),
820 GrAA::kNo,
821 GrQuadAAFlags::kNone,
822 SkCanvas::kFast_SrcRectConstraint,
823 SkMatrix::I(),
824 std::move(xform));
Brian Salomon63a0a752020-06-26 13:32:09 -0400825 srcView = tempRTC->readSurfaceView();
826 SkASSERT(srcView.asTextureProxy());
827 x = y = 0;
828 }
829 }
830
831 auto yRTC = GrRenderTargetContext::MakeWithFallback(
832 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400833 GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400834 int halfW = dstSize.width() /2;
835 int halfH = dstSize.height()/2;
836 auto uRTC = GrRenderTargetContext::MakeWithFallback(
837 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400838 GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400839 auto vRTC = GrRenderTargetContext::MakeWithFallback(
840 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400841 GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400842 if (!yRTC || !uRTC || !vRTC) {
843 callback(context, nullptr);
844 return;
845 }
846
847 float baseM[20];
848 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
849
850 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
851
852 auto texMatrix = SkMatrix::Translate(x, y);
853
854 SkRect dstRectY = SkRect::Make(dstSize);
855 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
856
857 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
858 PixelTransferResult yTransfer, uTransfer, vTransfer;
859
860 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
861 float yM[20];
862 std::fill_n(yM, 15, 0.f);
863 std::copy_n(baseM + 0, 5, yM + 15);
864 GrPaint yPaint;
865 auto yTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
866 auto yColFP = GrColorMatrixFragmentProcessor::Make(std::move(yTexFP), yM,
867 /*unpremulInput=*/false,
868 /*clampRGBOutput=*/true,
869 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400870 yPaint.setColorFragmentProcessor(std::move(yColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400871 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
872 yRTC->fillRectToRect(nullptr, std::move(yPaint), GrAA::kNo, SkMatrix::I(), dstRectY, dstRectY);
873 if (!doSynchronousRead) {
874 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
875 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
876 if (!yTransfer.fTransferBuffer) {
877 callback(context, nullptr);
878 return;
879 }
880 }
881
882 texMatrix.preScale(2.f, 2.f);
883 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
884 float uM[20];
885 std::fill_n(uM, 15, 0.f);
886 std::copy_n(baseM + 5, 5, uM + 15);
887 GrPaint uPaint;
888 auto uTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix,
Brian Salomona3b02f52020-07-15 16:02:01 -0400889 GrSamplerState::Filter::kLinear);
Brian Salomon63a0a752020-06-26 13:32:09 -0400890 auto uColFP = GrColorMatrixFragmentProcessor::Make(std::move(uTexFP), uM,
891 /*unpremulInput=*/false,
892 /*clampRGBOutput=*/true,
893 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400894 uPaint.setColorFragmentProcessor(std::move(uColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400895 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
896 uRTC->fillRectToRect(nullptr, std::move(uPaint), GrAA::kNo, SkMatrix::I(), dstRectUV,
897 dstRectUV);
898 if (!doSynchronousRead) {
899 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
900 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
901 if (!uTransfer.fTransferBuffer) {
902 callback(context, nullptr);
903 return;
904 }
905 }
906
907 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
908 float vM[20];
909 std::fill_n(vM, 15, 0.f);
910 std::copy_n(baseM + 10, 5, vM + 15);
911 GrPaint vPaint;
912 auto vTexFP = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType(),
Brian Salomona3b02f52020-07-15 16:02:01 -0400913 texMatrix, GrSamplerState::Filter::kLinear);
Brian Salomon63a0a752020-06-26 13:32:09 -0400914 auto vColFP = GrColorMatrixFragmentProcessor::Make(std::move(vTexFP), vM,
915 /*unpremulInput=*/false,
916 /*clampRGBOutput=*/true,
917 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400918 vPaint.setColorFragmentProcessor(std::move(vColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400919 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
920 vRTC->fillRectToRect(nullptr, std::move(vPaint), GrAA::kNo, SkMatrix::I(), dstRectUV,
921 dstRectUV);
922 if (!doSynchronousRead) {
923 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
924 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
925 if (!vTransfer.fTransferBuffer) {
926 callback(context, nullptr);
927 return;
928 }
929 }
930
931 if (doSynchronousRead) {
932 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
933 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
934 size_t yRB = yInfo.minRowBytes();
935 size_t uvRB = uvInfo.minRowBytes();
936 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
937 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
938 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
939 if (!yRTC->readPixels(yInfo, y.get(), yRB, {0, 0}, direct) ||
940 !uRTC->readPixels(uvInfo, u.get(), uvRB, {0, 0}, direct) ||
941 !vRTC->readPixels(uvInfo, v.get(), uvRB, {0, 0}, direct)) {
942 callback(context, nullptr);
943 return;
944 }
945 auto result = std::make_unique<AsyncReadResult>(direct->priv().contextID());
946 result->addCpuPlane(std::move(y), yRB );
947 result->addCpuPlane(std::move(u), uvRB);
948 result->addCpuPlane(std::move(v), uvRB);
949 callback(context, std::move(result));
950 return;
951 }
952
953 struct FinishContext {
954 ReadPixelsCallback* fClientCallback;
955 ReadPixelsContext fClientContext;
956 GrClientMappedBufferManager* fMappedBufferManager;
957 SkISize fSize;
958 PixelTransferResult fYTransfer;
959 PixelTransferResult fUTransfer;
960 PixelTransferResult fVTransfer;
961 };
962 // Assumption is that the caller would like to flush. We could take a parameter or require an
963 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
964 // callback to GrGpu until after the next flush that flushes our op list, though.
965 auto* finishContext = new FinishContext{callback,
966 context,
967 direct->priv().clientMappedBufferManager(),
968 dstSize,
969 std::move(yTransfer),
970 std::move(uTransfer),
971 std::move(vTransfer)};
972 auto finishCallback = [](GrGpuFinishedContext c) {
973 const auto* context = reinterpret_cast<const FinishContext*>(c);
974 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
975 auto manager = context->fMappedBufferManager;
976 size_t rowBytes = SkToSizeT(context->fSize.width());
977 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
978 (*context->fClientCallback)(context->fClientContext, nullptr);
979 delete context;
980 return;
981 }
982 rowBytes /= 2;
983 SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
984 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
985 (*context->fClientCallback)(context->fClientContext, nullptr);
986 delete context;
987 return;
988 }
989 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
990 (*context->fClientCallback)(context->fClientContext, nullptr);
991 delete context;
992 return;
993 }
994 (*context->fClientCallback)(context->fClientContext, std::move(result));
995 delete context;
996 };
997 GrFlushInfo flushInfo;
998 flushInfo.fFinishedContext = finishContext;
999 flushInfo.fFinishedProc = finishCallback;
1000 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo, nullptr);
1001}
1002
Brian Salomonc5243782020-04-02 12:50:34 -04001003bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001004 ASSERT_SINGLE_OWNER
1005 RETURN_FALSE_IF_ABANDONED
1006 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -04001007 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -04001008
Brian Salomon947efe22019-07-16 15:36:11 -04001009 const GrCaps* caps = fContext->priv().caps();
1010
Greg Daniel46cfbc62019-06-07 11:43:30 -04001011 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -05001012 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -04001013
Stephen White3c0a50f2020-01-16 18:19:54 -05001014 if (this->asSurfaceProxy()->framebufferOnly()) {
1015 return false;
1016 }
1017
Chris Daltonf8e5aad2019-08-02 12:55:23 -06001018 if (!caps->canCopySurface(this->asSurfaceProxy(), src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -04001019 return false;
1020 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001021
Greg Daniel16f5c652019-10-29 11:26:01 -04001022 // The swizzle doesn't matter for copies and it is not used.
1023 return this->drawingManager()->newCopyRenderTask(
Brian Salomonc5243782020-04-02 12:50:34 -04001024 GrSurfaceProxyView(sk_ref_sp(src), this->origin(), GrSwizzle("rgba")), srcRect,
Greg Daniel46e366a2019-12-16 14:38:36 -05001025 this->readSurfaceView(), dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001026}
Greg Daniel46cfbc62019-06-07 11:43:30 -04001027
Brian Salomon63a0a752020-06-26 13:32:09 -04001028std::unique_ptr<GrRenderTargetContext> GrSurfaceContext::rescale(const GrImageInfo& info,
1029 GrSurfaceOrigin origin,
1030 SkIRect srcRect,
1031 RescaleGamma rescaleGamma,
1032 SkFilterQuality rescaleQuality) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001033 auto rtProxy = this->asRenderTargetProxy();
1034 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1035 return nullptr;
1036 }
1037
Stephen White3c0a50f2020-01-16 18:19:54 -05001038 if (this->asSurfaceProxy()->framebufferOnly()) {
1039 return nullptr;
1040 }
1041
Brian Salomone9ad9982019-07-22 16:17:41 -04001042 // We rescale by drawing and don't currently support drawing to a kUnpremul destination.
1043 if (info.alphaType() == kUnpremul_SkAlphaType) {
1044 return nullptr;
1045 }
1046
Greg Daniel40903af2020-01-30 14:55:05 -05001047 GrSurfaceProxyView texView = this->readSurfaceView();
Brian Salomonfc118442019-11-22 19:09:27 -05001048 SkAlphaType srcAlphaType = this->colorInfo().alphaType();
Greg Daniel40903af2020-01-30 14:55:05 -05001049 if (!texView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -04001050 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect,
Brian Salomonc5243782020-04-02 12:50:34 -04001051 SkBackingFit::kApprox, SkBudgeted::kNo);
1052 if (!texView) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001053 return nullptr;
1054 }
Greg Daniel40903af2020-01-30 14:55:05 -05001055 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001056 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001057 }
1058
Brian Salomonbf6b9792019-08-21 09:38:10 -04001059 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
1060 // pass B is moved to A. If 'this' is the input on the first pass then tempA is null.
1061 std::unique_ptr<GrRenderTargetContext> tempA;
1062 std::unique_ptr<GrRenderTargetContext> tempB;
1063
Brian Salomone9ad9982019-07-22 16:17:41 -04001064 // Assume we should ignore the rescale linear request if the surface has no color space since
1065 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon63a0a752020-06-26 13:32:09 -04001066 if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() &&
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001067 !this->colorInfo().colorSpace()->gammaIsLinear()) {
1068 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomonfc118442019-11-22 19:09:27 -05001069 auto xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(), srcAlphaType, cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -04001070 kPremul_SkAlphaType);
1071 // We'll fall back to kRGBA_8888 if half float not supported.
Greg Daniele20fcad2020-01-08 11:52:34 -05001072 auto linearRTC = GrRenderTargetContext::MakeWithFallback(
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001073 fContext, GrColorType::kRGBA_F16, cs, SkBackingFit::kApprox, srcRect.size(), 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001074 GrMipmapped::kNo, GrProtected::kNo, origin);
Brian Salomone9ad9982019-07-22 16:17:41 -04001075 if (!linearRTC) {
1076 return nullptr;
1077 }
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001078 // 1-to-1 draw can always be kFast.
Brian Salomone69b9ef2020-07-22 11:18:06 -04001079 linearRTC->drawTexture(nullptr,
1080 std::move(texView),
1081 srcAlphaType,
1082 GrSamplerState::Filter::kNearest,
1083 GrSamplerState::MipmapMode::kNone,
1084 SkBlendMode::kSrc,
1085 SK_PMColor4fWHITE,
1086 SkRect::Make(srcRect),
1087 SkRect::Make(srcRect.size()),
1088 GrAA::kNo,
1089 GrQuadAAFlags::kNone,
1090 SkCanvas::kFast_SrcRectConstraint,
1091 SkMatrix::I(),
1092 std::move(xform));
Greg Daniel40903af2020-01-30 14:55:05 -05001093 texView = linearRTC->readSurfaceView();
1094 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -04001095 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001096 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001097 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001098
Brian Salomon9c6f6bf2020-05-29 16:37:26 -04001099 while (srcRect.size() != info.dimensions()) {
Brian Salomon59f31b12020-06-04 17:27:15 -04001100 SkISize nextDims = info.dimensions();
1101 if (rescaleQuality != kNone_SkFilterQuality) {
1102 if (srcRect.width() > info.width()) {
1103 nextDims.fWidth = std::max((srcRect.width() + 1)/2, info.width());
1104 } else if (srcRect.width() < info.width()) {
1105 nextDims.fWidth = std::min(srcRect.width()*2, info.width());
1106 }
1107 if (srcRect.height() > info.height()) {
1108 nextDims.fHeight = std::max((srcRect.height() + 1)/2, info.height());
1109 } else if (srcRect.height() < info.height()) {
1110 nextDims.fHeight = std::min(srcRect.height()*2, info.height());
1111 }
1112 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001113 auto input = tempA ? tempA.get() : this;
Brian Salomon59f31b12020-06-04 17:27:15 -04001114 GrColorType colorType = input->colorInfo().colorType();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001115 auto cs = input->colorInfo().refColorSpace();
Brian Salomone9ad9982019-07-22 16:17:41 -04001116 sk_sp<GrColorSpaceXform> xform;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001117 auto prevAlphaType = input->colorInfo().alphaType();
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001118 if (nextDims == info.dimensions()) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001119 // Might as well fold conversion to final info in the last step.
1120 cs = info.refColorSpace();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001121 xform = GrColorSpaceXform::Make(input->colorInfo().colorSpace(),
1122 input->colorInfo().alphaType(), cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -04001123 info.alphaType());
1124 }
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001125 tempB = GrRenderTargetContext::MakeWithFallback(fContext, colorType, std::move(cs),
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001126 SkBackingFit::kApprox, nextDims, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001127 GrMipmapped::kNo, GrProtected::kNo, origin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001128 if (!tempB) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001129 return nullptr;
1130 }
Brian Salomon59f31b12020-06-04 17:27:15 -04001131 auto dstRect = SkRect::Make(nextDims);
1132 if (rescaleQuality == kHigh_SkFilterQuality) {
1133 SkMatrix matrix;
1134 matrix.setScaleTranslate((float)srcRect.width()/nextDims.width(),
1135 (float)srcRect.height()/nextDims.height(),
1136 srcRect.x(),
1137 srcRect.y());
1138 std::unique_ptr<GrFragmentProcessor> fp;
1139 auto dir = GrBicubicEffect::Direction::kXY;
1140 if (nextDims.width() == srcRect.width()) {
1141 dir = GrBicubicEffect::Direction::kY;
1142 } else if (nextDims.height() == srcRect.height()) {
1143 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -04001144 }
Brian Salomon1af72d12020-06-25 10:47:26 -04001145 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
1146 static constexpr auto kKernel = GrBicubicEffect::Kernel::kCatmullRom;
Brian Salomon59f31b12020-06-04 17:27:15 -04001147 fp = GrBicubicEffect::MakeSubset(std::move(texView), prevAlphaType, matrix, kWM, kWM,
Brian Salomon1af72d12020-06-25 10:47:26 -04001148 SkRect::Make(srcRect), kKernel, dir, *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -04001149 if (xform) {
1150 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -04001151 }
Brian Salomon59f31b12020-06-04 17:27:15 -04001152 GrPaint paint;
John Stiles5933d7d2020-07-21 12:28:35 -04001153 paint.setColorFragmentProcessor(std::move(fp));
Brian Salomon59f31b12020-06-04 17:27:15 -04001154 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
1155 tempB->fillRectToRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect,
1156 dstRect);
1157 } else {
1158 auto filter = rescaleQuality == kNone_SkFilterQuality ? GrSamplerState::Filter::kNearest
Brian Salomona3b02f52020-07-15 16:02:01 -04001159 : GrSamplerState::Filter::kLinear;
Brian Salomon59f31b12020-06-04 17:27:15 -04001160 // Minimizing draw with integer coord src and dev rects can always be kFast.
1161 auto constraint = SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint;
1162 if (nextDims.width() <= srcRect.width() && nextDims.height() <= srcRect.height()) {
1163 constraint = SkCanvas::SrcRectConstraint::kFast_SrcRectConstraint;
1164 }
Brian Salomone69b9ef2020-07-22 11:18:06 -04001165 tempB->drawTexture(nullptr,
1166 std::move(texView),
1167 srcAlphaType,
1168 filter,
1169 GrSamplerState::MipmapMode::kNone,
1170 SkBlendMode::kSrc,
1171 SK_PMColor4fWHITE,
1172 SkRect::Make(srcRect),
1173 dstRect,
1174 GrAA::kNo,
1175 GrQuadAAFlags::kNone,
1176 constraint,
1177 SkMatrix::I(),
1178 std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -04001179 }
Greg Daniel40903af2020-01-30 14:55:05 -05001180 texView = tempB->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -04001181 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001182 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomone9ad9982019-07-22 16:17:41 -04001183 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001184 SkASSERT(tempA);
1185 return tempA;
Brian Salomone9ad9982019-07-22 16:17:41 -04001186}
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001187
Brian Salomon63a0a752020-06-26 13:32:09 -04001188GrSemaphoresSubmitted GrSurfaceContext::flush(SkSurface::BackendSurfaceAccess access,
1189 const GrFlushInfo& info,
1190 const GrBackendSurfaceMutableState* newState) {
1191 ASSERT_SINGLE_OWNER
Robert Phillips9eb00022020-06-30 15:30:12 -04001192 if (fContext->abandoned()) {
Brian Salomon63a0a752020-06-26 13:32:09 -04001193 if (info.fSubmittedProc) {
1194 info.fSubmittedProc(info.fSubmittedContext, false);
1195 }
1196 if (info.fFinishedProc) {
1197 info.fFinishedProc(info.fFinishedContext);
1198 }
1199 return GrSemaphoresSubmitted::kNo;
1200 }
1201 SkDEBUGCODE(this->validate();)
1202 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
1203
1204 return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info, newState);
1205}
1206
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001207GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
1208 const SkIRect& rect) {
1209 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1210 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001211 auto direct = fContext->asDirectContext();
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001212 if (!direct) {
1213 return {};
1214 }
1215 auto rtProxy = this->asRenderTargetProxy();
1216 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1217 return {};
1218 }
1219
1220 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001221 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
1222 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001223 // Fail if read color type does not have all of dstCT's color channels and those missing color
1224 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001225 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1226 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
1227 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1228 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001229 return {};
1230 }
1231
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001232 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001233 !supportedRead.fOffsetAlignmentForTransferBuffer) {
1234 return {};
1235 }
1236
1237 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
1238 size_t size = rowBytes * rect.height();
1239 auto buffer = direct->priv().resourceProvider()->createBuffer(
1240 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
1241 if (!buffer) {
1242 return {};
1243 }
1244 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -05001245 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001246 if (flip) {
1247 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1248 this->height() - rect.fTop);
1249 }
Greg Danielbbfec9d2019-08-20 10:56:51 -04001250 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001251 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -04001252 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001253 PixelTransferResult result;
1254 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001255 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -04001256 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001257 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
1258 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001259 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
1260 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001261 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
1262 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -04001263 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001264 };
1265 }
1266 return result;
1267}
Greg Daniel46e366a2019-12-16 14:38:36 -05001268
1269#ifdef SK_DEBUG
1270void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001271 SkASSERT(fReadView.proxy());
1272 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -04001273 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
1274 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
1275 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
1276 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001277 this->onValidate();
1278}
1279#endif