blob: c66b6510fbfaecc6cd7487db599fccb7a7193500 [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"
Brian Salomonf30b1c12019-06-20 12:25:02 -040017#include "src/gpu/GrDataUtils.h"
Adlai Hollera0693042020-10-14 11:23:11 -040018#include "src/gpu/GrDirectContextPriv.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
Adlai Hollerc95b5892020-08-11 12:02:22 -0400135bool GrSurfaceContext::readPixels(GrDirectContext* dContext, const GrImageInfo& origDstInfo,
136 void* dst, size_t rowBytes, SkIPoint pt) {
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");
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400141 if (!fContext->priv().matches(dContext)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400142 return false;
143 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400144
Brian Salomon1d435302019-07-01 13:05:28 -0400145 if (!dst) {
146 return false;
147 }
148
Brian Salomon1047a492019-07-02 12:25:21 -0400149 size_t tightRowBytes = origDstInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400150 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400151 rowBytes = tightRowBytes;
152 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400153 return false;
154 }
155
156 if (!origDstInfo.isValid()) {
157 return false;
158 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400159
160 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
161
Stephen White3c0a50f2020-01-16 18:19:54 -0500162 if (srcProxy->framebufferOnly()) {
163 return false;
164 }
165
Greg Daniel6eb8c242019-06-05 10:22:24 -0400166 // MDB TODO: delay this instantiation until later in the method
Adlai Hollerc95b5892020-08-11 12:02:22 -0400167 if (!srcProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400168 return false;
169 }
170
171 GrSurface* srcSurface = srcProxy->peekSurface();
172
Brian Salomon1d435302019-07-01 13:05:28 -0400173 auto dstInfo = origDstInfo;
174 if (!dstInfo.clip(this->width(), this->height(), &pt, &dst, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400175 return false;
176 }
Brian Salomon1047a492019-07-02 12:25:21 -0400177 // Our tight row bytes may have been changed by clipping.
178 tightRowBytes = dstInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400179
Mike Klein7321e6a2019-12-03 11:08:40 -0600180 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{this->colorInfo(), dstInfo}.flags;
181 bool unpremul = flags.unpremul,
182 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
183 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400184
Adlai Hollerc95b5892020-08-11 12:02:22 -0400185 const GrCaps* caps = dContext->priv().caps();
Robert Phillips07f0e412020-01-17 15:20:00 -0500186 bool srcIsCompressed = caps->isFormatCompressed(srcSurface->backendFormat());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400187 // 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);
Greg Danielc71c7962020-01-14 16:44:18 -0500194 GrColorType srcColorType = this->colorInfo().colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400195 bool canvas2DFastPath = unpremul && !needColorConversion &&
196 (GrColorType::kRGBA_8888 == dstInfo.colorType() ||
197 GrColorType::kBGRA_8888 == dstInfo.colorType()) &&
198 SkToBool(srcProxy->asTextureProxy()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500199 (srcColorType == GrColorType::kRGBA_8888 ||
200 srcColorType == GrColorType::kBGRA_8888) &&
Greg Daniel0258c902019-08-01 13:08:33 -0400201 defaultRGBAFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400202 dContext->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400203
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400204 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400205 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400206 return false;
207 }
208
Brian Salomondc0710f2019-07-01 14:59:32 -0400209 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400210 std::unique_ptr<GrSurfaceContext> tempCtx;
211 if (this->asTextureProxy()) {
212 GrColorType colorType = (canvas2DFastPath || srcIsCompressed)
213 ? GrColorType::kRGBA_8888
214 : this->colorInfo().colorType();
Brian Salomon72c7b982020-10-06 10:07:38 -0400215 tempCtx = GrRenderTargetContext::Make(
Christopher Cameron75ae8fc2020-11-17 22:03:49 -0800216 dContext, colorType, this->colorInfo().refColorSpace(), SkBackingFit::kApprox,
217 dstInfo.dimensions(), 1, GrMipMapped::kNo, GrProtected::kNo,
218 kTopLeft_GrSurfaceOrigin);
Brian Salomon72c7b982020-10-06 10:07:38 -0400219 if (!tempCtx) {
220 return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400221 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400222
223 std::unique_ptr<GrFragmentProcessor> fp;
224 if (canvas2DFastPath) {
225 fp = dContext->priv().createPMToUPMEffect(GrTextureEffect::Make(
226 this->readSurfaceView(), this->colorInfo().alphaType()));
227 if (dstInfo.colorType() == GrColorType::kBGRA_8888) {
228 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
229 dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888);
230 }
231 // The render target context is incorrectly tagged as kPremul even though we're
232 // writing unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type
233 // so we don't double unpremul.
234 dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType);
235 } else {
236 fp = GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType());
237 }
238 if (!fp) {
239 return false;
240 }
241 GrPaint paint;
242 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
243 paint.setColorFragmentProcessor(std::move(fp));
244
245 tempCtx->asRenderTargetContext()->fillRectToRect(
246 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
247 SkRect::MakeWH(dstInfo.width(), dstInfo.height()),
248 SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height()));
249 pt = {0, 0};
Greg Daniel6eb8c242019-06-05 10:22:24 -0400250 } else {
Brian Salomon72c7b982020-10-06 10:07:38 -0400251 auto restrictions = this->caps()->getDstCopyRestrictions(this->asRenderTargetProxy(),
252 this->colorInfo().colorType());
253 sk_sp<GrSurfaceProxy> copy;
254 static constexpr auto kFit = SkBackingFit::kExact;
255 static constexpr auto kBudgeted = SkBudgeted::kYes;
256 static constexpr auto kMipMapped = GrMipMapped::kNo;
257 if (restrictions.fMustCopyWholeSrc) {
258 copy = GrSurfaceProxy::Copy(fContext, srcProxy, this->origin(), kMipMapped, kFit,
259 kBudgeted);
260 } else {
261 auto srcRect = SkIRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height());
262 copy = GrSurfaceProxy::Copy(fContext, srcProxy, this->origin(), kMipMapped, srcRect,
263 kFit, kBudgeted, restrictions.fRectsMustMatch);
264 pt = {0, 0};
265 }
266 if (!copy) {
267 return false;
268 }
269 GrSurfaceProxyView view{std::move(copy), this->origin(), this->readSwizzle()};
270 tempCtx = GrSurfaceContext::Make(dContext,
271 std::move(view),
272 this->colorInfo().colorType(),
273 this->colorInfo().alphaType(),
274 this->colorInfo().refColorSpace());
275 SkASSERT(tempCtx);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400276 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400277 return tempCtx->readPixels(dContext, dstInfo, dst, rowBytes, pt);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400278 }
279
Greg Danielb8d84f82020-02-13 14:25:00 -0500280 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400281
Brian Salomon1d435302019-07-01 13:05:28 -0400282 auto supportedRead = caps->supportedReadPixelsColorType(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400283 this->colorInfo().colorType(), srcProxy->backendFormat(), dstInfo.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400284
Brian Salomon1047a492019-07-02 12:25:21 -0400285 bool makeTight = !caps->readPixelsRowBytesSupport() && tightRowBytes != rowBytes;
286
287 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomon8f8354a2019-07-31 20:12:02 -0400288 (dstInfo.colorType() != supportedRead.fColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400289
Brian Salomonf30b1c12019-06-20 12:25:02 -0400290 std::unique_ptr<char[]> tmpPixels;
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400291 GrImageInfo tmpInfo;
Brian Salomon1d435302019-07-01 13:05:28 -0400292 void* readDst = dst;
293 size_t readRB = rowBytes;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400294 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400295 tmpInfo = {supportedRead.fColorType, this->colorInfo().alphaType(),
296 this->colorInfo().refColorSpace(), dstInfo.width(), dstInfo.height()};
Brian Salomon1d435302019-07-01 13:05:28 -0400297 size_t tmpRB = tmpInfo.minRowBytes();
298 size_t size = tmpRB * tmpInfo.height();
299 // Chrome MSAN bots require the data to be initialized (hence the ()).
John Stilesfbd050b2020-08-03 13:21:46 -0400300 tmpPixels = std::make_unique<char[]>(size);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400301
Brian Salomonf30b1c12019-06-20 12:25:02 -0400302 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400303 readRB = tmpRB;
304 pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400305 }
306
Adlai Hollerc95b5892020-08-11 12:02:22 -0400307 dContext->priv().flushSurface(srcProxy);
308 dContext->submit();
309 if (!dContext->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(),
310 dstInfo.height(), this->colorInfo().colorType(),
311 supportedRead.fColorType, readDst, readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400312 return false;
313 }
314
Greg Daniel6eb8c242019-06-05 10:22:24 -0400315 if (convert) {
Brian Salomon8f8354a2019-07-31 20:12:02 -0400316 return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400317 }
318 return true;
319}
Robert Phillips0d075de2019-03-04 11:08:13 -0500320
Adlai Hollerc95b5892020-08-11 12:02:22 -0400321bool GrSurfaceContext::writePixels(GrDirectContext* dContext, const GrImageInfo& origSrcInfo,
322 const void* src, size_t rowBytes, SkIPoint pt) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400323 ASSERT_SINGLE_OWNER
324 RETURN_FALSE_IF_ABANDONED
325 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400326 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400327
Adlai Hollerc95b5892020-08-11 12:02:22 -0400328 if (!dContext) {
Brian Salomonc320b152018-02-20 14:05:36 -0500329 return false;
330 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500331
Brian Salomon1d435302019-07-01 13:05:28 -0400332 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500333 return false;
334 }
335
Brian Salomon1d435302019-07-01 13:05:28 -0400336 if (!src) {
337 return false;
338 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400339
Brian Salomon1047a492019-07-02 12:25:21 -0400340 size_t tightRowBytes = origSrcInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400341 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400342 rowBytes = tightRowBytes;
343 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400344 return false;
345 }
346
347 if (!origSrcInfo.isValid()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400348 return false;
349 }
350
351 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
Stephen White3c0a50f2020-01-16 18:19:54 -0500352
353 if (dstProxy->framebufferOnly()) {
354 return false;
355 }
356
Adlai Hollerc95b5892020-08-11 12:02:22 -0400357 if (!dstProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400358 return false;
359 }
360
361 GrSurface* dstSurface = dstProxy->peekSurface();
362
Brian Salomon1d435302019-07-01 13:05:28 -0400363 auto srcInfo = origSrcInfo;
364 if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400365 return false;
366 }
Brian Salomon1047a492019-07-02 12:25:21 -0400367 // Our tight row bytes may have been changed by clipping.
368 tightRowBytes = srcInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400369
Mike Klein7321e6a2019-12-03 11:08:40 -0600370 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{srcInfo, this->colorInfo()}.flags;
371 bool unpremul = flags.unpremul,
372 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
373 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400374
Adlai Hollerc95b5892020-08-11 12:02:22 -0400375 const GrCaps* caps = dContext->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400376
377 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
378 GrRenderable::kNo);
379
Greg Danielc71c7962020-01-14 16:44:18 -0500380 GrColorType dstColorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400381 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
382 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400383 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
384 (srcInfo.colorType() == GrColorType::kRGBA_8888 ||
385 srcInfo.colorType() == GrColorType::kBGRA_8888) &&
386 SkToBool(this->asRenderTargetContext()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500387 (dstColorType == GrColorType::kRGBA_8888 ||
388 dstColorType == GrColorType::kBGRA_8888) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400389 rgbaDefaultFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400390 dContext->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400391
392 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
Brian Salomond6287472019-06-24 15:50:07 -0400393 GrColorType colorType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400394
395 GrBackendFormat format;
Brian Salomone7499c72019-06-24 12:12:36 -0400396 SkAlphaType alphaType;
Greg Danielbfa19c42019-12-19 16:41:40 -0500397 GrSwizzle tempReadSwizzle;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400398 if (canvas2DFastPath) {
Brian Salomond6287472019-06-24 15:50:07 -0400399 colorType = GrColorType::kRGBA_8888;
Greg Daniel7bfc9132019-08-14 14:23:53 -0400400 format = rgbaDefaultFormat;
Brian Salomone7499c72019-06-24 12:12:36 -0400401 alphaType = kUnpremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400402 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400403 colorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400404 format = dstProxy->backendFormat().makeTexture2D();
405 if (!format.isValid()) {
406 return false;
407 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400408 alphaType = this->colorInfo().alphaType();
Greg Danielbfa19c42019-12-19 16:41:40 -0500409 tempReadSwizzle = this->readSwizzle();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400410 }
411
Greg Daniel2e52ad12019-06-13 10:04:16 -0400412 // It is more efficient for us to write pixels into a top left origin so we prefer that.
413 // However, if the final proxy isn't a render target then we must use a copy to move the
414 // data into it which requires the origins to match. If the final proxy is a render target
415 // we can use a draw instead which doesn't have this origin restriction. Thus for render
416 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400417 GrSurfaceOrigin tempOrigin =
Greg Danielb8d84f82020-02-13 14:25:00 -0500418 this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : this->origin();
Adlai Hollerc95b5892020-08-11 12:02:22 -0400419 auto tempProxy = dContext->priv().proxyProvider()->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400420 format, srcInfo.dimensions(), GrRenderable::kNo, 1, GrMipmapped::kNo,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400421 SkBackingFit::kApprox, SkBudgeted::kYes, GrProtected::kNo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400422 if (!tempProxy) {
423 return false;
424 }
Greg Daniel3912a4b2020-01-14 09:56:04 -0500425 GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400426 GrSurfaceContext tempCtx(dContext, tempView, colorType, alphaType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500427 this->colorInfo().refColorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400428
429 // In the fast path we always write the srcData to the temp context as though it were RGBA.
430 // When the data is really BGRA the write will cause the R and B channels to be swapped in
431 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
432 // dst.
Brian Salomon1d435302019-07-01 13:05:28 -0400433 if (canvas2DFastPath) {
434 srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888);
435 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400436 if (!tempCtx.writePixels(dContext, srcInfo, src, rowBytes, {0, 0})) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400437 return false;
438 }
439
440 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400441 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400442 if (canvas2DFastPath) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400443 fp = dContext->priv().createUPMToPMEffect(
Greg Danield2ccbb52020-02-05 10:45:39 -0500444 GrTextureEffect::Make(std::move(tempView), alphaType));
Brian Salomon1d435302019-07-01 13:05:28 -0400445 // Important: check the original src color type here!
446 if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400447 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
448 }
449 } else {
Greg Danield2ccbb52020-02-05 10:45:39 -0500450 fp = GrTextureEffect::Make(std::move(tempView), alphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400451 }
452 if (!fp) {
453 return false;
454 }
455 GrPaint paint;
456 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
John Stiles5933d7d2020-07-21 12:28:35 -0400457 paint.setColorFragmentProcessor(std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400458 this->asRenderTargetContext()->fillRectToRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400459 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400460 SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()),
461 SkRect::MakeWH(srcInfo.width(), srcInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400462 } else {
Brian Salomon1d435302019-07-01 13:05:28 -0400463 SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height());
464 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Brian Salomonc5243782020-04-02 12:50:34 -0400465 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400466 return false;
467 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400468 }
469 return true;
470 }
471
Brian Salomon1d435302019-07-01 13:05:28 -0400472 GrColorType allowedColorType =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400473 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400474 dstProxy->backendFormat(),
475 srcInfo.colorType()).fColorType;
Greg Danielb8d84f82020-02-13 14:25:00 -0500476 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon1047a492019-07-02 12:25:21 -0400477 bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes;
478 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomon1d435302019-07-01 13:05:28 -0400479 (srcInfo.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400480
Brian Salomonf30b1c12019-06-20 12:25:02 -0400481 std::unique_ptr<char[]> tmpPixels;
Brian Salomon1d435302019-07-01 13:05:28 -0400482 GrColorType srcColorType = srcInfo.colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400483 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400484 GrImageInfo tmpInfo(allowedColorType, this->colorInfo().alphaType(),
485 this->colorInfo().refColorSpace(), srcInfo.width(), srcInfo.height());
Brian Salomon1d435302019-07-01 13:05:28 -0400486 auto tmpRB = tmpInfo.minRowBytes();
487 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400488
Brian Salomon1d435302019-07-01 13:05:28 -0400489 GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400490
Brian Salomon1d435302019-07-01 13:05:28 -0400491 srcColorType = tmpInfo.colorType();
492 rowBytes = tmpRB;
493 src = tmpPixels.get();
494 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400495 }
496
497 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
498 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
499 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
500 // destination proxy)
501 // TODO: should this policy decision just be moved into the drawing manager?
Adlai Hollerc95b5892020-08-11 12:02:22 -0400502 dContext->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400503
Adlai Hollerc95b5892020-08-11 12:02:22 -0400504 return dContext->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(),
505 srcInfo.height(), this->colorInfo().colorType(),
506 srcColorType, src, rowBytes);
Brian Osman45580d32016-11-23 09:37:01 -0500507}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400508
Adlai Hollerc95b5892020-08-11 12:02:22 -0400509void GrSurfaceContext::asyncRescaleAndReadPixels(GrDirectContext* dContext,
510 const SkImageInfo& info,
Brian Salomon63a0a752020-06-26 13:32:09 -0400511 const SkIRect& srcRect,
512 RescaleGamma rescaleGamma,
513 SkFilterQuality rescaleQuality,
514 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400515 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400516 // We implement this by rendering and we don't currently support rendering kUnpremul.
517 if (info.alphaType() == kUnpremul_SkAlphaType) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400518 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400519 return;
520 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400521 if (!dContext) {
522 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400523 return;
524 }
525 auto rt = this->asRenderTargetProxy();
526 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400527 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400528 return;
529 }
530 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400531 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400532 return;
533 }
534 auto dstCT = SkColorTypeToGrColorType(info.colorType());
535 if (dstCT == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400536 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400537 return;
538 }
539 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
540 auto colorTypeOfFinalContext = this->colorInfo().colorType();
541 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
542 if (needsRescale) {
543 colorTypeOfFinalContext = dstCT;
544 backendFormatOfFinalContext =
545 this->caps()->getDefaultBackendFormat(dstCT, GrRenderable::kYes);
546 }
547 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
548 backendFormatOfFinalContext, dstCT);
549 // Fail if we can't read from the source surface's color type.
550 if (readInfo.fColorType == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400551 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400552 return;
553 }
554 // Fail if read color type does not have all of dstCT's color channels and those missing color
555 // channels are in the src.
556 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
557 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
558 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
559 if ((~legalReadChannels & dstChannels) & srcChannels) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400560 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400561 return;
562 }
563
564 std::unique_ptr<GrRenderTargetContext> tempRTC;
565 int x = srcRect.fLeft;
566 int y = srcRect.fTop;
567 if (needsRescale) {
568 tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
569 rescaleQuality);
570 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400571 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400572 return;
573 }
574 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
575 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
576 x = y = 0;
577 } else {
578 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
579 this->colorInfo().alphaType(),
580 info.colorSpace(),
581 info.alphaType());
582 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
583 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
584 GrSurfaceProxyView texProxyView = this->readSurfaceView();
585 SkRect srcRectToDraw = SkRect::Make(srcRect);
586 // If the src is not texturable first try to make a copy to a texture.
587 if (!texProxyView.asTextureProxy()) {
588 texProxyView =
Brian Salomon7e67dca2020-07-21 09:27:25 -0400589 GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipmapped::kNo, srcRect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400590 SkBackingFit::kApprox, SkBudgeted::kNo);
591 if (!texProxyView) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400592 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400593 return;
594 }
595 SkASSERT(texProxyView.asTextureProxy());
596 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
597 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400598 tempRTC = GrRenderTargetContext::Make(dContext, this->colorInfo().colorType(),
Brian Salomon63a0a752020-06-26 13:32:09 -0400599 info.refColorSpace(), SkBackingFit::kApprox,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400600 srcRect.size(), 1, GrMipmapped::kNo,
Brian Salomon63a0a752020-06-26 13:32:09 -0400601 GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
602 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400603 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400604 return;
605 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400606 tempRTC->drawTexture(nullptr,
607 std::move(texProxyView),
608 this->colorInfo().alphaType(),
609 GrSamplerState::Filter::kNearest,
610 GrSamplerState::MipmapMode::kNone,
611 SkBlendMode::kSrc,
612 SK_PMColor4fWHITE,
613 srcRectToDraw,
614 SkRect::MakeWH(srcRect.width(), srcRect.height()),
615 GrAA::kNo,
616 GrQuadAAFlags::kNone,
617 SkCanvas::kFast_SrcRectConstraint,
618 SkMatrix::I(),
619 std::move(xform));
Brian Salomon63a0a752020-06-26 13:32:09 -0400620 x = y = 0;
621 }
622 }
623 auto rtc = tempRTC ? tempRTC.get() : this;
Adlai Hollerc95b5892020-08-11 12:02:22 -0400624 return rtc->asyncReadPixels(dContext, SkIRect::MakeXYWH(x, y, info.width(), info.height()),
625 info.colorType(), callback, callbackContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400626}
627
628class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult {
629public:
630 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
631 ~AsyncReadResult() override {
632 for (int i = 0; i < fPlanes.count(); ++i) {
633 if (!fPlanes[i].fMappedBuffer) {
634 delete[] static_cast<const char*>(fPlanes[i].fData);
635 } else {
636 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
637 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
638 }
639 }
640 }
641
642 int count() const override { return fPlanes.count(); }
643 const void* data(int i) const override { return fPlanes[i].fData; }
644 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
645
646 bool addTransferResult(const PixelTransferResult& result,
647 SkISize dimensions,
648 size_t rowBytes,
649 GrClientMappedBufferManager* manager) {
650 SkASSERT(!result.fTransferBuffer->isMapped());
651 const void* mappedData = result.fTransferBuffer->map();
652 if (!mappedData) {
653 return false;
654 }
655 if (result.fPixelConverter) {
656 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
657 result.fPixelConverter(convertedData.get(), mappedData);
658 this->addCpuPlane(std::move(convertedData), rowBytes);
659 result.fTransferBuffer->unmap();
660 } else {
661 manager->insert(result.fTransferBuffer);
662 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
663 }
664 return true;
665 }
666
667 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
668 SkASSERT(data);
669 SkASSERT(rowBytes > 0);
670 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
671 }
672
673private:
674 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
675 SkASSERT(data);
676 SkASSERT(rowBytes > 0);
677 SkASSERT(mappedBuffer);
678 SkASSERT(mappedBuffer->isMapped());
679 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
680 }
681
682 struct Plane {
683 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
684 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
685 const void* fData;
686 size_t fRowBytes;
687 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
688 sk_sp<GrGpuBuffer> fMappedBuffer;
689 };
690 SkSTArray<3, Plane> fPlanes;
691 uint32_t fInboxID;
692};
693
Adlai Hollerc95b5892020-08-11 12:02:22 -0400694void GrSurfaceContext::asyncReadPixels(GrDirectContext* dContext,
695 const SkIRect& rect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400696 SkColorType colorType,
697 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400698 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400699 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
700 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
701
Adlai Hollerc95b5892020-08-11 12:02:22 -0400702 if (!dContext || this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
703 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400704 return;
705 }
706
Adlai Hollerc95b5892020-08-11 12:02:22 -0400707 auto mappedBufferManager = dContext->priv().clientMappedBufferManager();
Brian Salomon63a0a752020-06-26 13:32:09 -0400708
709 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
710
711 if (!transferResult.fTransferBuffer) {
712 auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
713 this->colorInfo().refColorSpace());
714 auto result = std::make_unique<AsyncReadResult>(0);
715 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
716 SkPixmap pm(ii, data.get(), ii.minRowBytes());
717 result->addCpuPlane(std::move(data), pm.rowBytes());
718
Adlai Hollerc95b5892020-08-11 12:02:22 -0400719 SkIPoint pt{rect.fLeft, rect.fTop};
720 if (!this->readPixels(dContext, ii, pm.writable_addr(), pm.rowBytes(), pt)) {
721 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400722 return;
723 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400724 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400725 return;
726 }
727
728 struct FinishContext {
729 ReadPixelsCallback* fClientCallback;
730 ReadPixelsContext fClientContext;
731 SkISize fSize;
732 SkColorType fColorType;
733 GrClientMappedBufferManager* fMappedBufferManager;
734 PixelTransferResult fTransferResult;
735 };
736 // Assumption is that the caller would like to flush. We could take a parameter or require an
737 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
738 // callback to GrGpu until after the next flush that flushes our op list, though.
739 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400740 callbackContext,
Brian Salomon63a0a752020-06-26 13:32:09 -0400741 rect.size(),
742 colorType,
743 mappedBufferManager,
744 std::move(transferResult)};
745 auto finishCallback = [](GrGpuFinishedContext c) {
746 const auto* context = reinterpret_cast<const FinishContext*>(c);
747 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
748 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
749 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
750 context->fMappedBufferManager)) {
751 result.reset();
752 }
753 (*context->fClientCallback)(context->fClientContext, std::move(result));
754 delete context;
755 };
756 GrFlushInfo flushInfo;
757 flushInfo.fFinishedContext = finishContext;
758 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -0500759
760 dContext->priv().flushSurface(this->asSurfaceProxy(),
761 SkSurface::BackendSurfaceAccess::kNoAccess,
762 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400763}
764
Adlai Hollerc95b5892020-08-11 12:02:22 -0400765void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(GrDirectContext* dContext,
766 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400767 sk_sp<SkColorSpace> dstColorSpace,
768 const SkIRect& srcRect,
769 SkISize dstSize,
770 RescaleGamma rescaleGamma,
771 SkFilterQuality rescaleQuality,
772 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400773 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400774 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
775 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
776 SkASSERT(!dstSize.isZero());
777 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
778
Adlai Hollerc95b5892020-08-11 12:02:22 -0400779 if (!dContext) {
780 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400781 return;
782 }
783 auto rt = this->asRenderTargetProxy();
784 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400785 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400786 return;
787 }
788 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400789 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400790 return;
791 }
792 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400793 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400794 return;
795 }
796 int x = srcRect.fLeft;
797 int y = srcRect.fTop;
798 bool needsRescale = srcRect.size() != dstSize;
799 GrSurfaceProxyView srcView;
800 if (needsRescale) {
801 // We assume the caller wants kPremul. There is no way to indicate a preference.
802 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
803 dstColorSpace);
804 // TODO: Incorporate the YUV conversion into last pass of rescaling.
805 auto tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
806 rescaleQuality);
807 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400808 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400809 return;
810 }
811 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
812 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
813 x = y = 0;
814 srcView = tempRTC->readSurfaceView();
815 } else {
816 srcView = this->readSurfaceView();
817 if (!srcView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400818 srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipmapped::kNo,
Brian Salomon63a0a752020-06-26 13:32:09 -0400819 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
820 if (!srcView) {
821 // If we can't get a texture copy of the contents then give up.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400822 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400823 return;
824 }
825 SkASSERT(srcView.asTextureProxy());
826 x = y = 0;
827 }
828 // We assume the caller wants kPremul. There is no way to indicate a preference.
829 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
830 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
831 kPremul_SkAlphaType);
832 if (xform) {
833 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
834 auto tempRTC = GrRenderTargetContext::Make(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400835 dContext, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400836 dstSize, 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400837 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400838 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400839 return;
840 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400841 tempRTC->drawTexture(nullptr,
842 std::move(srcView),
843 this->colorInfo().alphaType(),
844 GrSamplerState::Filter::kNearest,
845 GrSamplerState::MipmapMode::kNone,
846 SkBlendMode::kSrc,
847 SK_PMColor4fWHITE,
848 srcRectToDraw,
849 SkRect::Make(srcRect.size()),
850 GrAA::kNo,
851 GrQuadAAFlags::kNone,
852 SkCanvas::kFast_SrcRectConstraint,
853 SkMatrix::I(),
854 std::move(xform));
Brian Salomon63a0a752020-06-26 13:32:09 -0400855 srcView = tempRTC->readSurfaceView();
856 SkASSERT(srcView.asTextureProxy());
857 x = y = 0;
858 }
859 }
860
861 auto yRTC = GrRenderTargetContext::MakeWithFallback(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400862 dContext, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400863 GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400864 int halfW = dstSize.width() /2;
865 int halfH = dstSize.height()/2;
866 auto uRTC = GrRenderTargetContext::MakeWithFallback(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400867 dContext, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH},
868 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400869 auto vRTC = GrRenderTargetContext::MakeWithFallback(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400870 dContext, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH},
871 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400872 if (!yRTC || !uRTC || !vRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400873 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400874 return;
875 }
876
877 float baseM[20];
878 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
879
880 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
881
882 auto texMatrix = SkMatrix::Translate(x, y);
883
884 SkRect dstRectY = SkRect::Make(dstSize);
885 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
886
887 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
888 PixelTransferResult yTransfer, uTransfer, vTransfer;
889
890 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
891 float yM[20];
892 std::fill_n(yM, 15, 0.f);
893 std::copy_n(baseM + 0, 5, yM + 15);
894 GrPaint yPaint;
895 auto yTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
896 auto yColFP = GrColorMatrixFragmentProcessor::Make(std::move(yTexFP), yM,
897 /*unpremulInput=*/false,
898 /*clampRGBOutput=*/true,
899 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400900 yPaint.setColorFragmentProcessor(std::move(yColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400901 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
902 yRTC->fillRectToRect(nullptr, std::move(yPaint), GrAA::kNo, SkMatrix::I(), dstRectY, dstRectY);
903 if (!doSynchronousRead) {
904 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
905 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
906 if (!yTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400907 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400908 return;
909 }
910 }
911
912 texMatrix.preScale(2.f, 2.f);
913 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
914 float uM[20];
915 std::fill_n(uM, 15, 0.f);
916 std::copy_n(baseM + 5, 5, uM + 15);
917 GrPaint uPaint;
918 auto uTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix,
Brian Salomona3b02f52020-07-15 16:02:01 -0400919 GrSamplerState::Filter::kLinear);
Brian Salomon63a0a752020-06-26 13:32:09 -0400920 auto uColFP = GrColorMatrixFragmentProcessor::Make(std::move(uTexFP), uM,
921 /*unpremulInput=*/false,
922 /*clampRGBOutput=*/true,
923 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400924 uPaint.setColorFragmentProcessor(std::move(uColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400925 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
926 uRTC->fillRectToRect(nullptr, std::move(uPaint), GrAA::kNo, SkMatrix::I(), dstRectUV,
927 dstRectUV);
928 if (!doSynchronousRead) {
929 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
930 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
931 if (!uTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400932 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400933 return;
934 }
935 }
936
937 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
938 float vM[20];
939 std::fill_n(vM, 15, 0.f);
940 std::copy_n(baseM + 10, 5, vM + 15);
941 GrPaint vPaint;
942 auto vTexFP = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType(),
Brian Salomona3b02f52020-07-15 16:02:01 -0400943 texMatrix, GrSamplerState::Filter::kLinear);
Brian Salomon63a0a752020-06-26 13:32:09 -0400944 auto vColFP = GrColorMatrixFragmentProcessor::Make(std::move(vTexFP), vM,
945 /*unpremulInput=*/false,
946 /*clampRGBOutput=*/true,
947 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400948 vPaint.setColorFragmentProcessor(std::move(vColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400949 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
950 vRTC->fillRectToRect(nullptr, std::move(vPaint), GrAA::kNo, SkMatrix::I(), dstRectUV,
951 dstRectUV);
952 if (!doSynchronousRead) {
953 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
954 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
955 if (!vTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400956 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400957 return;
958 }
959 }
960
961 if (doSynchronousRead) {
962 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
963 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
964 size_t yRB = yInfo.minRowBytes();
965 size_t uvRB = uvInfo.minRowBytes();
966 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
967 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
968 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400969 if (!yRTC->readPixels(dContext, yInfo, y.get(), yRB, {0, 0}) ||
970 !uRTC->readPixels(dContext, uvInfo, u.get(), uvRB, {0, 0}) ||
971 !vRTC->readPixels(dContext, uvInfo, v.get(), uvRB, {0, 0})) {
972 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400973 return;
974 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400975 auto result = std::make_unique<AsyncReadResult>(dContext->priv().contextID());
Brian Salomon63a0a752020-06-26 13:32:09 -0400976 result->addCpuPlane(std::move(y), yRB );
977 result->addCpuPlane(std::move(u), uvRB);
978 result->addCpuPlane(std::move(v), uvRB);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400979 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400980 return;
981 }
982
983 struct FinishContext {
984 ReadPixelsCallback* fClientCallback;
985 ReadPixelsContext fClientContext;
986 GrClientMappedBufferManager* fMappedBufferManager;
987 SkISize fSize;
988 PixelTransferResult fYTransfer;
989 PixelTransferResult fUTransfer;
990 PixelTransferResult fVTransfer;
991 };
992 // Assumption is that the caller would like to flush. We could take a parameter or require an
993 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
994 // callback to GrGpu until after the next flush that flushes our op list, though.
995 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400996 callbackContext,
997 dContext->priv().clientMappedBufferManager(),
Brian Salomon63a0a752020-06-26 13:32:09 -0400998 dstSize,
999 std::move(yTransfer),
1000 std::move(uTransfer),
1001 std::move(vTransfer)};
1002 auto finishCallback = [](GrGpuFinishedContext c) {
1003 const auto* context = reinterpret_cast<const FinishContext*>(c);
1004 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
1005 auto manager = context->fMappedBufferManager;
1006 size_t rowBytes = SkToSizeT(context->fSize.width());
1007 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
1008 (*context->fClientCallback)(context->fClientContext, nullptr);
1009 delete context;
1010 return;
1011 }
1012 rowBytes /= 2;
1013 SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
1014 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
1015 (*context->fClientCallback)(context->fClientContext, nullptr);
1016 delete context;
1017 return;
1018 }
1019 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
1020 (*context->fClientCallback)(context->fClientContext, nullptr);
1021 delete context;
1022 return;
1023 }
1024 (*context->fClientCallback)(context->fClientContext, std::move(result));
1025 delete context;
1026 };
1027 GrFlushInfo flushInfo;
1028 flushInfo.fFinishedContext = finishContext;
1029 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -05001030 dContext->priv().flushSurface(this->asSurfaceProxy(),
1031 SkSurface::BackendSurfaceAccess::kNoAccess,
1032 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -04001033}
1034
Brian Salomonc5243782020-04-02 12:50:34 -04001035bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001036 ASSERT_SINGLE_OWNER
1037 RETURN_FALSE_IF_ABANDONED
1038 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -04001039 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -04001040
Brian Salomon947efe22019-07-16 15:36:11 -04001041 const GrCaps* caps = fContext->priv().caps();
1042
Greg Daniel46cfbc62019-06-07 11:43:30 -04001043 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -05001044 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -04001045
Stephen White3c0a50f2020-01-16 18:19:54 -05001046 if (this->asSurfaceProxy()->framebufferOnly()) {
1047 return false;
1048 }
1049
Chris Daltonf8e5aad2019-08-02 12:55:23 -06001050 if (!caps->canCopySurface(this->asSurfaceProxy(), src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -04001051 return false;
1052 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001053
Greg Daniel16f5c652019-10-29 11:26:01 -04001054 // The swizzle doesn't matter for copies and it is not used.
1055 return this->drawingManager()->newCopyRenderTask(
Brian Salomonc5243782020-04-02 12:50:34 -04001056 GrSurfaceProxyView(sk_ref_sp(src), this->origin(), GrSwizzle("rgba")), srcRect,
Greg Daniel46e366a2019-12-16 14:38:36 -05001057 this->readSurfaceView(), dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001058}
Greg Daniel46cfbc62019-06-07 11:43:30 -04001059
Brian Salomon63a0a752020-06-26 13:32:09 -04001060std::unique_ptr<GrRenderTargetContext> GrSurfaceContext::rescale(const GrImageInfo& info,
1061 GrSurfaceOrigin origin,
1062 SkIRect srcRect,
1063 RescaleGamma rescaleGamma,
1064 SkFilterQuality rescaleQuality) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001065 auto rtProxy = this->asRenderTargetProxy();
1066 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1067 return nullptr;
1068 }
1069
Stephen White3c0a50f2020-01-16 18:19:54 -05001070 if (this->asSurfaceProxy()->framebufferOnly()) {
1071 return nullptr;
1072 }
1073
Brian Salomone9ad9982019-07-22 16:17:41 -04001074 // We rescale by drawing and don't currently support drawing to a kUnpremul destination.
1075 if (info.alphaType() == kUnpremul_SkAlphaType) {
1076 return nullptr;
1077 }
1078
Greg Daniel40903af2020-01-30 14:55:05 -05001079 GrSurfaceProxyView texView = this->readSurfaceView();
Brian Salomonfc118442019-11-22 19:09:27 -05001080 SkAlphaType srcAlphaType = this->colorInfo().alphaType();
Greg Daniel40903af2020-01-30 14:55:05 -05001081 if (!texView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -04001082 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect,
Brian Salomonc5243782020-04-02 12:50:34 -04001083 SkBackingFit::kApprox, SkBudgeted::kNo);
1084 if (!texView) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001085 return nullptr;
1086 }
Greg Daniel40903af2020-01-30 14:55:05 -05001087 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001088 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001089 }
1090
Brian Salomonbf6b9792019-08-21 09:38:10 -04001091 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
1092 // pass B is moved to A. If 'this' is the input on the first pass then tempA is null.
1093 std::unique_ptr<GrRenderTargetContext> tempA;
1094 std::unique_ptr<GrRenderTargetContext> tempB;
1095
Brian Salomone9ad9982019-07-22 16:17:41 -04001096 // Assume we should ignore the rescale linear request if the surface has no color space since
1097 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon63a0a752020-06-26 13:32:09 -04001098 if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() &&
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001099 !this->colorInfo().colorSpace()->gammaIsLinear()) {
1100 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomonfc118442019-11-22 19:09:27 -05001101 auto xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(), srcAlphaType, cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -04001102 kPremul_SkAlphaType);
1103 // We'll fall back to kRGBA_8888 if half float not supported.
Greg Daniele20fcad2020-01-08 11:52:34 -05001104 auto linearRTC = GrRenderTargetContext::MakeWithFallback(
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001105 fContext, GrColorType::kRGBA_F16, cs, SkBackingFit::kApprox, srcRect.size(), 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001106 GrMipmapped::kNo, GrProtected::kNo, origin);
Brian Salomone9ad9982019-07-22 16:17:41 -04001107 if (!linearRTC) {
1108 return nullptr;
1109 }
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001110 // 1-to-1 draw can always be kFast.
Brian Salomone69b9ef2020-07-22 11:18:06 -04001111 linearRTC->drawTexture(nullptr,
1112 std::move(texView),
1113 srcAlphaType,
1114 GrSamplerState::Filter::kNearest,
1115 GrSamplerState::MipmapMode::kNone,
1116 SkBlendMode::kSrc,
1117 SK_PMColor4fWHITE,
1118 SkRect::Make(srcRect),
1119 SkRect::Make(srcRect.size()),
1120 GrAA::kNo,
1121 GrQuadAAFlags::kNone,
1122 SkCanvas::kFast_SrcRectConstraint,
1123 SkMatrix::I(),
1124 std::move(xform));
Greg Daniel40903af2020-01-30 14:55:05 -05001125 texView = linearRTC->readSurfaceView();
1126 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -04001127 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001128 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001129 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001130
Brian Salomon9c6f6bf2020-05-29 16:37:26 -04001131 while (srcRect.size() != info.dimensions()) {
Brian Salomon59f31b12020-06-04 17:27:15 -04001132 SkISize nextDims = info.dimensions();
1133 if (rescaleQuality != kNone_SkFilterQuality) {
1134 if (srcRect.width() > info.width()) {
1135 nextDims.fWidth = std::max((srcRect.width() + 1)/2, info.width());
1136 } else if (srcRect.width() < info.width()) {
1137 nextDims.fWidth = std::min(srcRect.width()*2, info.width());
1138 }
1139 if (srcRect.height() > info.height()) {
1140 nextDims.fHeight = std::max((srcRect.height() + 1)/2, info.height());
1141 } else if (srcRect.height() < info.height()) {
1142 nextDims.fHeight = std::min(srcRect.height()*2, info.height());
1143 }
1144 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001145 auto input = tempA ? tempA.get() : this;
Brian Salomon59f31b12020-06-04 17:27:15 -04001146 GrColorType colorType = input->colorInfo().colorType();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001147 auto cs = input->colorInfo().refColorSpace();
Brian Salomone9ad9982019-07-22 16:17:41 -04001148 sk_sp<GrColorSpaceXform> xform;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001149 auto prevAlphaType = input->colorInfo().alphaType();
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001150 if (nextDims == info.dimensions()) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001151 // Might as well fold conversion to final info in the last step.
1152 cs = info.refColorSpace();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001153 xform = GrColorSpaceXform::Make(input->colorInfo().colorSpace(),
1154 input->colorInfo().alphaType(), cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -04001155 info.alphaType());
1156 }
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001157 tempB = GrRenderTargetContext::MakeWithFallback(fContext, colorType, std::move(cs),
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001158 SkBackingFit::kApprox, nextDims, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001159 GrMipmapped::kNo, GrProtected::kNo, origin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001160 if (!tempB) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001161 return nullptr;
1162 }
Brian Salomon59f31b12020-06-04 17:27:15 -04001163 auto dstRect = SkRect::Make(nextDims);
1164 if (rescaleQuality == kHigh_SkFilterQuality) {
1165 SkMatrix matrix;
1166 matrix.setScaleTranslate((float)srcRect.width()/nextDims.width(),
1167 (float)srcRect.height()/nextDims.height(),
1168 srcRect.x(),
1169 srcRect.y());
1170 std::unique_ptr<GrFragmentProcessor> fp;
1171 auto dir = GrBicubicEffect::Direction::kXY;
1172 if (nextDims.width() == srcRect.width()) {
1173 dir = GrBicubicEffect::Direction::kY;
1174 } else if (nextDims.height() == srcRect.height()) {
1175 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -04001176 }
Brian Salomon1af72d12020-06-25 10:47:26 -04001177 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
Mike Reed3867c702020-09-01 13:28:10 -04001178 static constexpr auto kKernel = GrBicubicEffect::gCatmullRom;
Brian Salomon59f31b12020-06-04 17:27:15 -04001179 fp = GrBicubicEffect::MakeSubset(std::move(texView), prevAlphaType, matrix, kWM, kWM,
Brian Salomon1af72d12020-06-25 10:47:26 -04001180 SkRect::Make(srcRect), kKernel, dir, *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -04001181 if (xform) {
1182 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -04001183 }
Brian Salomon59f31b12020-06-04 17:27:15 -04001184 GrPaint paint;
John Stiles5933d7d2020-07-21 12:28:35 -04001185 paint.setColorFragmentProcessor(std::move(fp));
Brian Salomon59f31b12020-06-04 17:27:15 -04001186 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
1187 tempB->fillRectToRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect,
1188 dstRect);
1189 } else {
1190 auto filter = rescaleQuality == kNone_SkFilterQuality ? GrSamplerState::Filter::kNearest
Brian Salomona3b02f52020-07-15 16:02:01 -04001191 : GrSamplerState::Filter::kLinear;
Brian Salomon59f31b12020-06-04 17:27:15 -04001192 // Minimizing draw with integer coord src and dev rects can always be kFast.
1193 auto constraint = SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint;
1194 if (nextDims.width() <= srcRect.width() && nextDims.height() <= srcRect.height()) {
1195 constraint = SkCanvas::SrcRectConstraint::kFast_SrcRectConstraint;
1196 }
Brian Salomone69b9ef2020-07-22 11:18:06 -04001197 tempB->drawTexture(nullptr,
1198 std::move(texView),
1199 srcAlphaType,
1200 filter,
1201 GrSamplerState::MipmapMode::kNone,
1202 SkBlendMode::kSrc,
1203 SK_PMColor4fWHITE,
1204 SkRect::Make(srcRect),
1205 dstRect,
1206 GrAA::kNo,
1207 GrQuadAAFlags::kNone,
1208 constraint,
1209 SkMatrix::I(),
1210 std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -04001211 }
Greg Daniel40903af2020-01-30 14:55:05 -05001212 texView = tempB->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -04001213 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001214 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomone9ad9982019-07-22 16:17:41 -04001215 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001216 SkASSERT(tempA);
1217 return tempA;
Brian Salomone9ad9982019-07-22 16:17:41 -04001218}
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001219
1220GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
1221 const SkIRect& rect) {
1222 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1223 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001224 auto direct = fContext->asDirectContext();
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001225 if (!direct) {
1226 return {};
1227 }
1228 auto rtProxy = this->asRenderTargetProxy();
1229 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1230 return {};
1231 }
1232
1233 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001234 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
1235 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001236 // Fail if read color type does not have all of dstCT's color channels and those missing color
1237 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001238 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1239 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
1240 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1241 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001242 return {};
1243 }
1244
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001245 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001246 !supportedRead.fOffsetAlignmentForTransferBuffer) {
1247 return {};
1248 }
1249
1250 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
1251 size_t size = rowBytes * rect.height();
1252 auto buffer = direct->priv().resourceProvider()->createBuffer(
1253 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
1254 if (!buffer) {
1255 return {};
1256 }
1257 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -05001258 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001259 if (flip) {
1260 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1261 this->height() - rect.fTop);
1262 }
Greg Danielbbfec9d2019-08-20 10:56:51 -04001263 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001264 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -04001265 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001266 PixelTransferResult result;
1267 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001268 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -04001269 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001270 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
1271 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001272 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
1273 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001274 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
1275 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -04001276 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001277 };
1278 }
1279 return result;
1280}
Greg Daniel46e366a2019-12-16 14:38:36 -05001281
1282#ifdef SK_DEBUG
1283void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001284 SkASSERT(fReadView.proxy());
1285 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -04001286 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
1287 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
1288 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
1289 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001290 this->onValidate();
1291}
1292#endif