blob: 2a79b15a872db49e9c85d9983a2acb49cfa2111b [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/SkGr.h"
Brian Salomone9ad9982019-07-22 16:17:41 -040026#include "src/gpu/effects/GrBicubicEffect.h"
Brian Salomon63a0a752020-06-26 13:32:09 -040027#include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h"
Brian Osman45580d32016-11-23 09:37:01 -050028
Adlai Holler33dbd652020-06-01 12:35:42 -040029#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
Robert Phillips9eb00022020-06-30 15:30:12 -040030#define RETURN_FALSE_IF_ABANDONED if (this->fContext->abandoned()) { return false; }
Brian Osman45580d32016-11-23 09:37:01 -050031
Greg Danielbfa19c42019-12-19 16:41:40 -050032std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -050033 GrSurfaceProxyView readView,
Greg Danielbfa19c42019-12-19 16:41:40 -050034 GrColorType colorType,
35 SkAlphaType alphaType,
36 sk_sp<SkColorSpace> colorSpace) {
Greg Daniele20fcad2020-01-08 11:52:34 -050037 // It is probably not necessary to check if the context is abandoned here since uses of the
38 // GrSurfaceContext which need the context will mostly likely fail later on without an issue.
39 // However having this hear adds some reassurance in case there is a path doesn't handle an
40 // abandoned context correctly. It also lets us early out of some extra work.
Robert Phillips9eb00022020-06-30 15:30:12 -040041 if (context->abandoned()) {
Greg Daniele20fcad2020-01-08 11:52:34 -050042 return nullptr;
43 }
Greg Daniel3912a4b2020-01-14 09:56:04 -050044 GrSurfaceProxy* proxy = readView.proxy();
Greg Danielbfa19c42019-12-19 16:41:40 -050045 SkASSERT(proxy && proxy->asTextureProxy());
46
Greg Danielbfa19c42019-12-19 16:41:40 -050047 std::unique_ptr<GrSurfaceContext> surfaceContext;
Greg Daniel3912a4b2020-01-14 09:56:04 -050048 if (proxy->asRenderTargetProxy()) {
Greg Danielbfa19c42019-12-19 16:41:40 -050049 SkASSERT(kPremul_SkAlphaType == alphaType || kOpaque_SkAlphaType == alphaType);
Brian Salomon8afde5f2020-04-01 16:22:00 -040050 // Will we ever want a swizzle that is not the default write swizzle for the format and
Greg Danielbfa19c42019-12-19 16:41:40 -050051 // colorType here? If so we will need to manually pass that in.
Brian Salomonc5243782020-04-02 12:50:34 -040052 GrSwizzle writeSwizzle;
53 if (colorType != GrColorType::kUnknown) {
54 writeSwizzle =
55 context->priv().caps()->getWriteSwizzle(proxy->backendFormat(), colorType);
56 }
Brian Salomon8afde5f2020-04-01 16:22:00 -040057 GrSurfaceProxyView writeView(readView.refProxy(), readView.origin(), writeSwizzle);
John Stilesfbd050b2020-08-03 13:21:46 -040058 surfaceContext = std::make_unique<GrRenderTargetContext>(context, std::move(readView),
Brian Salomon8afde5f2020-04-01 16:22:00 -040059 std::move(writeView), colorType,
John Stilesfbd050b2020-08-03 13:21:46 -040060 std::move(colorSpace), nullptr);
Greg Danielbfa19c42019-12-19 16:41:40 -050061 } else {
John Stilesfbd050b2020-08-03 13:21:46 -040062 surfaceContext = std::make_unique<GrSurfaceContext>(context, std::move(readView), colorType,
63 alphaType, std::move(colorSpace));
Greg Danielbfa19c42019-12-19 16:41:40 -050064 }
Robert Phillips07f0e412020-01-17 15:20:00 -050065 SkDEBUGCODE(surfaceContext->validate();)
Greg Danielbfa19c42019-12-19 16:41:40 -050066 return surfaceContext;
67}
68
Brian Salomona56a7462020-02-07 14:17:25 -050069std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
70 SkISize dimensions,
71 const GrBackendFormat& format,
72 GrRenderable renderable,
73 int renderTargetSampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -040074 GrMipmapped mipMapped,
Brian Salomona56a7462020-02-07 14:17:25 -050075 GrProtected isProtected,
76 GrSurfaceOrigin origin,
77 GrColorType colorType,
78 SkAlphaType alphaType,
79 sk_sp<SkColorSpace> colorSpace,
80 SkBackingFit fit,
81 SkBudgeted budgeted) {
Brian Salomonc5243782020-04-02 12:50:34 -040082 GrSwizzle swizzle;
83 if (colorType != GrColorType::kUnknown && !context->priv().caps()->isFormatCompressed(format)) {
Brian Salomond005b692020-04-01 15:47:05 -040084 swizzle = context->priv().caps()->getReadSwizzle(format, colorType);
85 }
Greg Daniel47c20e82020-01-21 14:29:57 -050086
Greg Danielbfa19c42019-12-19 16:41:40 -050087 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -040088 format, dimensions, renderable, renderTargetSampleCnt, mipMapped, fit, budgeted,
89 isProtected);
Greg Danielbfa19c42019-12-19 16:41:40 -050090 if (!proxy) {
91 return nullptr;
92 }
93
Greg Daniel3912a4b2020-01-14 09:56:04 -050094 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
95 return GrSurfaceContext::Make(context, std::move(view), colorType, alphaType,
Greg Danielbfa19c42019-12-19 16:41:40 -050096 std::move(colorSpace));
97}
98
Greg Danielf41b2bd2019-08-22 16:19:24 -040099// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
100// GrOpsTasks to be picked up and added to by renderTargetContexts lower in the call
101// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
102// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500103GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500104 GrSurfaceProxyView readView,
Brian Salomond6287472019-06-24 15:50:07 -0400105 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -0400106 SkAlphaType alphaType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500107 sk_sp<SkColorSpace> colorSpace)
Greg Daniel901b98e2019-10-22 09:54:02 -0400108 : fContext(context)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500109 , fReadView(std::move(readView))
110 , fColorInfo(colorType, alphaType, std::move(colorSpace)) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400111 SkASSERT(!context->abandoned());
Greg Daniele20fcad2020-01-08 11:52:34 -0500112}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400113
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400114const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); }
115
Robert Phillips0d075de2019-03-04 11:08:13 -0500116GrAuditTrail* GrSurfaceContext::auditTrail() {
117 return fContext->priv().auditTrail();
118}
119
120GrDrawingManager* GrSurfaceContext::drawingManager() {
121 return fContext->priv().drawingManager();
122}
123
124const GrDrawingManager* GrSurfaceContext::drawingManager() const {
125 return fContext->priv().drawingManager();
126}
127
128#ifdef SK_DEBUG
Brian Salomon70fe17e2020-11-30 14:33:58 -0500129GrSingleOwner* GrSurfaceContext::singleOwner() const { return fContext->priv().singleOwner(); }
Robert Phillips0d075de2019-03-04 11:08:13 -0500130#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400131
Adlai Hollerc95b5892020-08-11 12:02:22 -0400132bool GrSurfaceContext::readPixels(GrDirectContext* dContext, const GrImageInfo& origDstInfo,
133 void* dst, size_t rowBytes, SkIPoint pt) {
Brian Salomon1d435302019-07-01 13:05:28 -0400134 ASSERT_SINGLE_OWNER
135 RETURN_FALSE_IF_ABANDONED
136 SkDEBUGCODE(this->validate();)
137 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400138 if (!fContext->priv().matches(dContext)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400139 return false;
140 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400141
Brian Salomon1d435302019-07-01 13:05:28 -0400142 if (!dst) {
143 return false;
144 }
145
Brian Salomon1047a492019-07-02 12:25:21 -0400146 size_t tightRowBytes = origDstInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400147 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400148 rowBytes = tightRowBytes;
149 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400150 return false;
151 }
152
153 if (!origDstInfo.isValid()) {
154 return false;
155 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400156
157 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
158
Stephen White3c0a50f2020-01-16 18:19:54 -0500159 if (srcProxy->framebufferOnly()) {
160 return false;
161 }
162
Greg Daniel6eb8c242019-06-05 10:22:24 -0400163 // MDB TODO: delay this instantiation until later in the method
Adlai Hollerc95b5892020-08-11 12:02:22 -0400164 if (!srcProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400165 return false;
166 }
167
168 GrSurface* srcSurface = srcProxy->peekSurface();
169
Brian Salomon1d435302019-07-01 13:05:28 -0400170 auto dstInfo = origDstInfo;
171 if (!dstInfo.clip(this->width(), this->height(), &pt, &dst, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400172 return false;
173 }
Brian Salomon1047a492019-07-02 12:25:21 -0400174 // Our tight row bytes may have been changed by clipping.
175 tightRowBytes = dstInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400176
Mike Klein7321e6a2019-12-03 11:08:40 -0600177 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{this->colorInfo(), dstInfo}.flags;
178 bool unpremul = flags.unpremul,
179 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
180 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400181
Adlai Hollerc95b5892020-08-11 12:02:22 -0400182 const GrCaps* caps = dContext->priv().caps();
Robert Phillips07f0e412020-01-17 15:20:00 -0500183 bool srcIsCompressed = caps->isFormatCompressed(srcSurface->backendFormat());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400184 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
185 // care so much about getImageData performance. However, in order to ensure putImageData/
186 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
187 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
188 // fContext->vaildaPMUPMConversionExists()).
Greg Daniel0258c902019-08-01 13:08:33 -0400189 GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
190 GrRenderable::kYes);
Greg Danielc71c7962020-01-14 16:44:18 -0500191 GrColorType srcColorType = this->colorInfo().colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400192 bool canvas2DFastPath = unpremul && !needColorConversion &&
193 (GrColorType::kRGBA_8888 == dstInfo.colorType() ||
194 GrColorType::kBGRA_8888 == dstInfo.colorType()) &&
195 SkToBool(srcProxy->asTextureProxy()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500196 (srcColorType == GrColorType::kRGBA_8888 ||
197 srcColorType == GrColorType::kBGRA_8888) &&
Greg Daniel0258c902019-08-01 13:08:33 -0400198 defaultRGBAFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400199 dContext->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400200
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400201 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400202 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400203 return false;
204 }
205
Brian Salomondc0710f2019-07-01 14:59:32 -0400206 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400207 std::unique_ptr<GrSurfaceContext> tempCtx;
208 if (this->asTextureProxy()) {
209 GrColorType colorType = (canvas2DFastPath || srcIsCompressed)
210 ? GrColorType::kRGBA_8888
211 : this->colorInfo().colorType();
Brian Salomon72c7b982020-10-06 10:07:38 -0400212 tempCtx = GrRenderTargetContext::Make(
Christopher Cameron75ae8fc2020-11-17 22:03:49 -0800213 dContext, colorType, this->colorInfo().refColorSpace(), SkBackingFit::kApprox,
214 dstInfo.dimensions(), 1, GrMipMapped::kNo, GrProtected::kNo,
215 kTopLeft_GrSurfaceOrigin);
Brian Salomon72c7b982020-10-06 10:07:38 -0400216 if (!tempCtx) {
217 return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400218 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400219
220 std::unique_ptr<GrFragmentProcessor> fp;
221 if (canvas2DFastPath) {
222 fp = dContext->priv().createPMToUPMEffect(GrTextureEffect::Make(
223 this->readSurfaceView(), this->colorInfo().alphaType()));
224 if (dstInfo.colorType() == GrColorType::kBGRA_8888) {
225 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
226 dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888);
227 }
228 // The render target context is incorrectly tagged as kPremul even though we're
229 // writing unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type
230 // so we don't double unpremul.
231 dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType);
232 } else {
233 fp = GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType());
234 }
235 if (!fp) {
236 return false;
237 }
238 GrPaint paint;
239 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
240 paint.setColorFragmentProcessor(std::move(fp));
241
242 tempCtx->asRenderTargetContext()->fillRectToRect(
243 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
244 SkRect::MakeWH(dstInfo.width(), dstInfo.height()),
245 SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height()));
246 pt = {0, 0};
Greg Daniel6eb8c242019-06-05 10:22:24 -0400247 } else {
Brian Salomon72c7b982020-10-06 10:07:38 -0400248 auto restrictions = this->caps()->getDstCopyRestrictions(this->asRenderTargetProxy(),
249 this->colorInfo().colorType());
250 sk_sp<GrSurfaceProxy> copy;
251 static constexpr auto kFit = SkBackingFit::kExact;
252 static constexpr auto kBudgeted = SkBudgeted::kYes;
253 static constexpr auto kMipMapped = GrMipMapped::kNo;
254 if (restrictions.fMustCopyWholeSrc) {
255 copy = GrSurfaceProxy::Copy(fContext, srcProxy, this->origin(), kMipMapped, kFit,
256 kBudgeted);
257 } else {
258 auto srcRect = SkIRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height());
259 copy = GrSurfaceProxy::Copy(fContext, srcProxy, this->origin(), kMipMapped, srcRect,
260 kFit, kBudgeted, restrictions.fRectsMustMatch);
261 pt = {0, 0};
262 }
263 if (!copy) {
264 return false;
265 }
266 GrSurfaceProxyView view{std::move(copy), this->origin(), this->readSwizzle()};
267 tempCtx = GrSurfaceContext::Make(dContext,
268 std::move(view),
269 this->colorInfo().colorType(),
270 this->colorInfo().alphaType(),
271 this->colorInfo().refColorSpace());
272 SkASSERT(tempCtx);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400273 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400274 return tempCtx->readPixels(dContext, dstInfo, dst, rowBytes, pt);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400275 }
276
Greg Danielb8d84f82020-02-13 14:25:00 -0500277 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400278
Brian Salomon1d435302019-07-01 13:05:28 -0400279 auto supportedRead = caps->supportedReadPixelsColorType(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400280 this->colorInfo().colorType(), srcProxy->backendFormat(), dstInfo.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400281
Brian Salomon1047a492019-07-02 12:25:21 -0400282 bool makeTight = !caps->readPixelsRowBytesSupport() && tightRowBytes != rowBytes;
283
284 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomon8f8354a2019-07-31 20:12:02 -0400285 (dstInfo.colorType() != supportedRead.fColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400286
Brian Salomonf30b1c12019-06-20 12:25:02 -0400287 std::unique_ptr<char[]> tmpPixels;
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400288 GrImageInfo tmpInfo;
Brian Salomon1d435302019-07-01 13:05:28 -0400289 void* readDst = dst;
290 size_t readRB = rowBytes;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400291 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400292 tmpInfo = {supportedRead.fColorType, this->colorInfo().alphaType(),
293 this->colorInfo().refColorSpace(), dstInfo.width(), dstInfo.height()};
Brian Salomon1d435302019-07-01 13:05:28 -0400294 size_t tmpRB = tmpInfo.minRowBytes();
295 size_t size = tmpRB * tmpInfo.height();
296 // Chrome MSAN bots require the data to be initialized (hence the ()).
John Stilesfbd050b2020-08-03 13:21:46 -0400297 tmpPixels = std::make_unique<char[]>(size);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400298
Brian Salomonf30b1c12019-06-20 12:25:02 -0400299 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400300 readRB = tmpRB;
301 pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400302 }
303
Adlai Hollerc95b5892020-08-11 12:02:22 -0400304 dContext->priv().flushSurface(srcProxy);
305 dContext->submit();
306 if (!dContext->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(),
307 dstInfo.height(), this->colorInfo().colorType(),
308 supportedRead.fColorType, readDst, readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400309 return false;
310 }
311
Greg Daniel6eb8c242019-06-05 10:22:24 -0400312 if (convert) {
Brian Salomon8f8354a2019-07-31 20:12:02 -0400313 return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400314 }
315 return true;
316}
Robert Phillips0d075de2019-03-04 11:08:13 -0500317
Adlai Hollerc95b5892020-08-11 12:02:22 -0400318bool GrSurfaceContext::writePixels(GrDirectContext* dContext, const GrImageInfo& origSrcInfo,
319 const void* src, size_t rowBytes, SkIPoint pt) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400320 ASSERT_SINGLE_OWNER
321 RETURN_FALSE_IF_ABANDONED
322 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400323 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400324
Adlai Hollerc95b5892020-08-11 12:02:22 -0400325 if (!dContext) {
Brian Salomonc320b152018-02-20 14:05:36 -0500326 return false;
327 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500328
Brian Salomon1d435302019-07-01 13:05:28 -0400329 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500330 return false;
331 }
332
Brian Salomon1d435302019-07-01 13:05:28 -0400333 if (!src) {
334 return false;
335 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400336
Brian Salomon1047a492019-07-02 12:25:21 -0400337 size_t tightRowBytes = origSrcInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400338 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400339 rowBytes = tightRowBytes;
340 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400341 return false;
342 }
343
344 if (!origSrcInfo.isValid()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400345 return false;
346 }
347
348 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
Stephen White3c0a50f2020-01-16 18:19:54 -0500349
350 if (dstProxy->framebufferOnly()) {
351 return false;
352 }
353
Adlai Hollerc95b5892020-08-11 12:02:22 -0400354 if (!dstProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400355 return false;
356 }
357
358 GrSurface* dstSurface = dstProxy->peekSurface();
359
Brian Salomon1d435302019-07-01 13:05:28 -0400360 auto srcInfo = origSrcInfo;
361 if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400362 return false;
363 }
Brian Salomon1047a492019-07-02 12:25:21 -0400364 // Our tight row bytes may have been changed by clipping.
365 tightRowBytes = srcInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400366
Mike Klein7321e6a2019-12-03 11:08:40 -0600367 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{srcInfo, this->colorInfo()}.flags;
368 bool unpremul = flags.unpremul,
369 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
370 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400371
Adlai Hollerc95b5892020-08-11 12:02:22 -0400372 const GrCaps* caps = dContext->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400373
374 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
375 GrRenderable::kNo);
376
Greg Danielc71c7962020-01-14 16:44:18 -0500377 GrColorType dstColorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400378 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
379 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400380 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
381 (srcInfo.colorType() == GrColorType::kRGBA_8888 ||
382 srcInfo.colorType() == GrColorType::kBGRA_8888) &&
383 SkToBool(this->asRenderTargetContext()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500384 (dstColorType == GrColorType::kRGBA_8888 ||
385 dstColorType == GrColorType::kBGRA_8888) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400386 rgbaDefaultFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400387 dContext->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400388
389 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
Brian Salomond6287472019-06-24 15:50:07 -0400390 GrColorType colorType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400391
392 GrBackendFormat format;
Brian Salomone7499c72019-06-24 12:12:36 -0400393 SkAlphaType alphaType;
Greg Danielbfa19c42019-12-19 16:41:40 -0500394 GrSwizzle tempReadSwizzle;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400395 if (canvas2DFastPath) {
Brian Salomond6287472019-06-24 15:50:07 -0400396 colorType = GrColorType::kRGBA_8888;
Greg Daniel7bfc9132019-08-14 14:23:53 -0400397 format = rgbaDefaultFormat;
Brian Salomone7499c72019-06-24 12:12:36 -0400398 alphaType = kUnpremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400399 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400400 colorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400401 format = dstProxy->backendFormat().makeTexture2D();
402 if (!format.isValid()) {
403 return false;
404 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400405 alphaType = this->colorInfo().alphaType();
Greg Danielbfa19c42019-12-19 16:41:40 -0500406 tempReadSwizzle = this->readSwizzle();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400407 }
408
Greg Daniel2e52ad12019-06-13 10:04:16 -0400409 // It is more efficient for us to write pixels into a top left origin so we prefer that.
410 // However, if the final proxy isn't a render target then we must use a copy to move the
411 // data into it which requires the origins to match. If the final proxy is a render target
412 // we can use a draw instead which doesn't have this origin restriction. Thus for render
413 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400414 GrSurfaceOrigin tempOrigin =
Greg Danielb8d84f82020-02-13 14:25:00 -0500415 this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : this->origin();
Adlai Hollerc95b5892020-08-11 12:02:22 -0400416 auto tempProxy = dContext->priv().proxyProvider()->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400417 format, srcInfo.dimensions(), GrRenderable::kNo, 1, GrMipmapped::kNo,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400418 SkBackingFit::kApprox, SkBudgeted::kYes, GrProtected::kNo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400419 if (!tempProxy) {
420 return false;
421 }
Greg Daniel3912a4b2020-01-14 09:56:04 -0500422 GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400423 GrSurfaceContext tempCtx(dContext, tempView, colorType, alphaType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500424 this->colorInfo().refColorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400425
426 // In the fast path we always write the srcData to the temp context as though it were RGBA.
427 // When the data is really BGRA the write will cause the R and B channels to be swapped in
428 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
429 // dst.
Brian Salomon1d435302019-07-01 13:05:28 -0400430 if (canvas2DFastPath) {
431 srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888);
432 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400433 if (!tempCtx.writePixels(dContext, srcInfo, src, rowBytes, {0, 0})) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400434 return false;
435 }
436
437 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400438 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400439 if (canvas2DFastPath) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400440 fp = dContext->priv().createUPMToPMEffect(
Greg Danield2ccbb52020-02-05 10:45:39 -0500441 GrTextureEffect::Make(std::move(tempView), alphaType));
Brian Salomon1d435302019-07-01 13:05:28 -0400442 // Important: check the original src color type here!
443 if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400444 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
445 }
446 } else {
Greg Danield2ccbb52020-02-05 10:45:39 -0500447 fp = GrTextureEffect::Make(std::move(tempView), alphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400448 }
449 if (!fp) {
450 return false;
451 }
452 GrPaint paint;
453 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
John Stiles5933d7d2020-07-21 12:28:35 -0400454 paint.setColorFragmentProcessor(std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400455 this->asRenderTargetContext()->fillRectToRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400456 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400457 SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()),
458 SkRect::MakeWH(srcInfo.width(), srcInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400459 } else {
Brian Salomon1d435302019-07-01 13:05:28 -0400460 SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height());
461 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Brian Salomonc5243782020-04-02 12:50:34 -0400462 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400463 return false;
464 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400465 }
466 return true;
467 }
468
Brian Salomon1d435302019-07-01 13:05:28 -0400469 GrColorType allowedColorType =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400470 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400471 dstProxy->backendFormat(),
472 srcInfo.colorType()).fColorType;
Greg Danielb8d84f82020-02-13 14:25:00 -0500473 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon1047a492019-07-02 12:25:21 -0400474 bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes;
475 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomon1d435302019-07-01 13:05:28 -0400476 (srcInfo.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400477
Brian Salomonf30b1c12019-06-20 12:25:02 -0400478 std::unique_ptr<char[]> tmpPixels;
Brian Salomon1d435302019-07-01 13:05:28 -0400479 GrColorType srcColorType = srcInfo.colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400480 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400481 GrImageInfo tmpInfo(allowedColorType, this->colorInfo().alphaType(),
482 this->colorInfo().refColorSpace(), srcInfo.width(), srcInfo.height());
Brian Salomon1d435302019-07-01 13:05:28 -0400483 auto tmpRB = tmpInfo.minRowBytes();
484 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400485
Brian Salomon1d435302019-07-01 13:05:28 -0400486 GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400487
Brian Salomon1d435302019-07-01 13:05:28 -0400488 srcColorType = tmpInfo.colorType();
489 rowBytes = tmpRB;
490 src = tmpPixels.get();
491 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400492 }
493
494 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
495 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
496 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
497 // destination proxy)
498 // TODO: should this policy decision just be moved into the drawing manager?
Adlai Hollerc95b5892020-08-11 12:02:22 -0400499 dContext->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400500
Adlai Hollerc95b5892020-08-11 12:02:22 -0400501 return dContext->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(),
502 srcInfo.height(), this->colorInfo().colorType(),
503 srcColorType, src, rowBytes);
Brian Osman45580d32016-11-23 09:37:01 -0500504}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400505
Adlai Hollerc95b5892020-08-11 12:02:22 -0400506void GrSurfaceContext::asyncRescaleAndReadPixels(GrDirectContext* dContext,
507 const SkImageInfo& info,
Brian Salomon63a0a752020-06-26 13:32:09 -0400508 const SkIRect& srcRect,
509 RescaleGamma rescaleGamma,
510 SkFilterQuality rescaleQuality,
511 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400512 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400513 // We implement this by rendering and we don't currently support rendering kUnpremul.
514 if (info.alphaType() == kUnpremul_SkAlphaType) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400515 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400516 return;
517 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400518 if (!dContext) {
519 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400520 return;
521 }
522 auto rt = this->asRenderTargetProxy();
523 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400524 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400525 return;
526 }
527 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400528 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400529 return;
530 }
531 auto dstCT = SkColorTypeToGrColorType(info.colorType());
532 if (dstCT == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400533 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400534 return;
535 }
536 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
537 auto colorTypeOfFinalContext = this->colorInfo().colorType();
538 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
539 if (needsRescale) {
540 colorTypeOfFinalContext = dstCT;
541 backendFormatOfFinalContext =
542 this->caps()->getDefaultBackendFormat(dstCT, GrRenderable::kYes);
543 }
544 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
545 backendFormatOfFinalContext, dstCT);
546 // Fail if we can't read from the source surface's color type.
547 if (readInfo.fColorType == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400548 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400549 return;
550 }
551 // Fail if read color type does not have all of dstCT's color channels and those missing color
552 // channels are in the src.
553 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
554 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
555 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
556 if ((~legalReadChannels & dstChannels) & srcChannels) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400557 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400558 return;
559 }
560
561 std::unique_ptr<GrRenderTargetContext> tempRTC;
562 int x = srcRect.fLeft;
563 int y = srcRect.fTop;
564 if (needsRescale) {
565 tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
566 rescaleQuality);
567 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400568 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400569 return;
570 }
571 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
572 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
573 x = y = 0;
574 } else {
575 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
576 this->colorInfo().alphaType(),
577 info.colorSpace(),
578 info.alphaType());
579 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
580 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
581 GrSurfaceProxyView texProxyView = this->readSurfaceView();
582 SkRect srcRectToDraw = SkRect::Make(srcRect);
583 // If the src is not texturable first try to make a copy to a texture.
584 if (!texProxyView.asTextureProxy()) {
585 texProxyView =
Brian Salomon7e67dca2020-07-21 09:27:25 -0400586 GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipmapped::kNo, srcRect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400587 SkBackingFit::kApprox, SkBudgeted::kNo);
588 if (!texProxyView) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400589 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400590 return;
591 }
592 SkASSERT(texProxyView.asTextureProxy());
593 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
594 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400595 tempRTC = GrRenderTargetContext::Make(dContext, this->colorInfo().colorType(),
Brian Salomon63a0a752020-06-26 13:32:09 -0400596 info.refColorSpace(), SkBackingFit::kApprox,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400597 srcRect.size(), 1, GrMipmapped::kNo,
Brian Salomon63a0a752020-06-26 13:32:09 -0400598 GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
599 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400600 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400601 return;
602 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400603 tempRTC->drawTexture(nullptr,
604 std::move(texProxyView),
605 this->colorInfo().alphaType(),
606 GrSamplerState::Filter::kNearest,
607 GrSamplerState::MipmapMode::kNone,
608 SkBlendMode::kSrc,
609 SK_PMColor4fWHITE,
610 srcRectToDraw,
611 SkRect::MakeWH(srcRect.width(), srcRect.height()),
612 GrAA::kNo,
613 GrQuadAAFlags::kNone,
614 SkCanvas::kFast_SrcRectConstraint,
615 SkMatrix::I(),
616 std::move(xform));
Brian Salomon63a0a752020-06-26 13:32:09 -0400617 x = y = 0;
618 }
619 }
620 auto rtc = tempRTC ? tempRTC.get() : this;
Adlai Hollerc95b5892020-08-11 12:02:22 -0400621 return rtc->asyncReadPixels(dContext, SkIRect::MakeXYWH(x, y, info.width(), info.height()),
622 info.colorType(), callback, callbackContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400623}
624
625class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult {
626public:
627 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
628 ~AsyncReadResult() override {
629 for (int i = 0; i < fPlanes.count(); ++i) {
630 if (!fPlanes[i].fMappedBuffer) {
631 delete[] static_cast<const char*>(fPlanes[i].fData);
632 } else {
633 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
634 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
635 }
636 }
637 }
638
639 int count() const override { return fPlanes.count(); }
640 const void* data(int i) const override { return fPlanes[i].fData; }
641 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
642
643 bool addTransferResult(const PixelTransferResult& result,
644 SkISize dimensions,
645 size_t rowBytes,
646 GrClientMappedBufferManager* manager) {
647 SkASSERT(!result.fTransferBuffer->isMapped());
648 const void* mappedData = result.fTransferBuffer->map();
649 if (!mappedData) {
650 return false;
651 }
652 if (result.fPixelConverter) {
653 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
654 result.fPixelConverter(convertedData.get(), mappedData);
655 this->addCpuPlane(std::move(convertedData), rowBytes);
656 result.fTransferBuffer->unmap();
657 } else {
658 manager->insert(result.fTransferBuffer);
659 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
660 }
661 return true;
662 }
663
664 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
665 SkASSERT(data);
666 SkASSERT(rowBytes > 0);
667 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
668 }
669
670private:
671 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
672 SkASSERT(data);
673 SkASSERT(rowBytes > 0);
674 SkASSERT(mappedBuffer);
675 SkASSERT(mappedBuffer->isMapped());
676 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
677 }
678
679 struct Plane {
680 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
681 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
682 const void* fData;
683 size_t fRowBytes;
684 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
685 sk_sp<GrGpuBuffer> fMappedBuffer;
686 };
687 SkSTArray<3, Plane> fPlanes;
688 uint32_t fInboxID;
689};
690
Adlai Hollerc95b5892020-08-11 12:02:22 -0400691void GrSurfaceContext::asyncReadPixels(GrDirectContext* dContext,
692 const SkIRect& rect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400693 SkColorType colorType,
694 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400695 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400696 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
697 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
698
Adlai Hollerc95b5892020-08-11 12:02:22 -0400699 if (!dContext || this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
700 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400701 return;
702 }
703
Adlai Hollerc95b5892020-08-11 12:02:22 -0400704 auto mappedBufferManager = dContext->priv().clientMappedBufferManager();
Brian Salomon63a0a752020-06-26 13:32:09 -0400705
706 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
707
708 if (!transferResult.fTransferBuffer) {
709 auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
710 this->colorInfo().refColorSpace());
711 auto result = std::make_unique<AsyncReadResult>(0);
712 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
713 SkPixmap pm(ii, data.get(), ii.minRowBytes());
714 result->addCpuPlane(std::move(data), pm.rowBytes());
715
Adlai Hollerc95b5892020-08-11 12:02:22 -0400716 SkIPoint pt{rect.fLeft, rect.fTop};
717 if (!this->readPixels(dContext, ii, pm.writable_addr(), pm.rowBytes(), pt)) {
718 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400719 return;
720 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400721 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400722 return;
723 }
724
725 struct FinishContext {
726 ReadPixelsCallback* fClientCallback;
727 ReadPixelsContext fClientContext;
728 SkISize fSize;
729 SkColorType fColorType;
730 GrClientMappedBufferManager* fMappedBufferManager;
731 PixelTransferResult fTransferResult;
732 };
733 // Assumption is that the caller would like to flush. We could take a parameter or require an
734 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
735 // callback to GrGpu until after the next flush that flushes our op list, though.
736 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400737 callbackContext,
Brian Salomon63a0a752020-06-26 13:32:09 -0400738 rect.size(),
739 colorType,
740 mappedBufferManager,
741 std::move(transferResult)};
742 auto finishCallback = [](GrGpuFinishedContext c) {
743 const auto* context = reinterpret_cast<const FinishContext*>(c);
744 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
745 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
746 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
747 context->fMappedBufferManager)) {
748 result.reset();
749 }
750 (*context->fClientCallback)(context->fClientContext, std::move(result));
751 delete context;
752 };
753 GrFlushInfo flushInfo;
754 flushInfo.fFinishedContext = finishContext;
755 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -0500756
757 dContext->priv().flushSurface(this->asSurfaceProxy(),
758 SkSurface::BackendSurfaceAccess::kNoAccess,
759 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400760}
761
Adlai Hollerc95b5892020-08-11 12:02:22 -0400762void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(GrDirectContext* dContext,
763 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400764 sk_sp<SkColorSpace> dstColorSpace,
765 const SkIRect& srcRect,
766 SkISize dstSize,
767 RescaleGamma rescaleGamma,
768 SkFilterQuality rescaleQuality,
769 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400770 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400771 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
772 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
773 SkASSERT(!dstSize.isZero());
774 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
775
Adlai Hollerc95b5892020-08-11 12:02:22 -0400776 if (!dContext) {
777 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400778 return;
779 }
780 auto rt = this->asRenderTargetProxy();
781 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400782 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400783 return;
784 }
785 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400786 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400787 return;
788 }
789 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400790 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400791 return;
792 }
793 int x = srcRect.fLeft;
794 int y = srcRect.fTop;
795 bool needsRescale = srcRect.size() != dstSize;
796 GrSurfaceProxyView srcView;
797 if (needsRescale) {
798 // We assume the caller wants kPremul. There is no way to indicate a preference.
799 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
800 dstColorSpace);
801 // TODO: Incorporate the YUV conversion into last pass of rescaling.
802 auto tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
803 rescaleQuality);
804 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400805 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400806 return;
807 }
808 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
809 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
810 x = y = 0;
811 srcView = tempRTC->readSurfaceView();
812 } else {
813 srcView = this->readSurfaceView();
814 if (!srcView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400815 srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipmapped::kNo,
Brian Salomon63a0a752020-06-26 13:32:09 -0400816 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
817 if (!srcView) {
818 // If we can't get a texture copy of the contents then give up.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400819 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400820 return;
821 }
822 SkASSERT(srcView.asTextureProxy());
823 x = y = 0;
824 }
825 // We assume the caller wants kPremul. There is no way to indicate a preference.
826 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
827 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
828 kPremul_SkAlphaType);
829 if (xform) {
830 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
831 auto tempRTC = GrRenderTargetContext::Make(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400832 dContext, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400833 dstSize, 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400834 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400835 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400836 return;
837 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400838 tempRTC->drawTexture(nullptr,
839 std::move(srcView),
840 this->colorInfo().alphaType(),
841 GrSamplerState::Filter::kNearest,
842 GrSamplerState::MipmapMode::kNone,
843 SkBlendMode::kSrc,
844 SK_PMColor4fWHITE,
845 srcRectToDraw,
846 SkRect::Make(srcRect.size()),
847 GrAA::kNo,
848 GrQuadAAFlags::kNone,
849 SkCanvas::kFast_SrcRectConstraint,
850 SkMatrix::I(),
851 std::move(xform));
Brian Salomon63a0a752020-06-26 13:32:09 -0400852 srcView = tempRTC->readSurfaceView();
853 SkASSERT(srcView.asTextureProxy());
854 x = y = 0;
855 }
856 }
857
858 auto yRTC = GrRenderTargetContext::MakeWithFallback(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400859 dContext, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400860 GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400861 int halfW = dstSize.width() /2;
862 int halfH = dstSize.height()/2;
863 auto uRTC = GrRenderTargetContext::MakeWithFallback(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400864 dContext, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH},
865 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400866 auto vRTC = 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 if (!yRTC || !uRTC || !vRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400870 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400871 return;
872 }
873
874 float baseM[20];
875 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
876
877 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
878
879 auto texMatrix = SkMatrix::Translate(x, y);
880
881 SkRect dstRectY = SkRect::Make(dstSize);
882 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
883
884 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
885 PixelTransferResult yTransfer, uTransfer, vTransfer;
886
887 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
888 float yM[20];
889 std::fill_n(yM, 15, 0.f);
890 std::copy_n(baseM + 0, 5, yM + 15);
891 GrPaint yPaint;
892 auto yTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
893 auto yColFP = GrColorMatrixFragmentProcessor::Make(std::move(yTexFP), yM,
894 /*unpremulInput=*/false,
895 /*clampRGBOutput=*/true,
896 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400897 yPaint.setColorFragmentProcessor(std::move(yColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400898 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
899 yRTC->fillRectToRect(nullptr, std::move(yPaint), GrAA::kNo, SkMatrix::I(), dstRectY, dstRectY);
900 if (!doSynchronousRead) {
901 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
902 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
903 if (!yTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400904 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400905 return;
906 }
907 }
908
909 texMatrix.preScale(2.f, 2.f);
910 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
911 float uM[20];
912 std::fill_n(uM, 15, 0.f);
913 std::copy_n(baseM + 5, 5, uM + 15);
914 GrPaint uPaint;
915 auto uTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix,
Brian Salomona3b02f52020-07-15 16:02:01 -0400916 GrSamplerState::Filter::kLinear);
Brian Salomon63a0a752020-06-26 13:32:09 -0400917 auto uColFP = GrColorMatrixFragmentProcessor::Make(std::move(uTexFP), uM,
918 /*unpremulInput=*/false,
919 /*clampRGBOutput=*/true,
920 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400921 uPaint.setColorFragmentProcessor(std::move(uColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400922 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
923 uRTC->fillRectToRect(nullptr, std::move(uPaint), GrAA::kNo, SkMatrix::I(), dstRectUV,
924 dstRectUV);
925 if (!doSynchronousRead) {
926 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
927 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
928 if (!uTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400929 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400930 return;
931 }
932 }
933
934 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
935 float vM[20];
936 std::fill_n(vM, 15, 0.f);
937 std::copy_n(baseM + 10, 5, vM + 15);
938 GrPaint vPaint;
939 auto vTexFP = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType(),
Brian Salomona3b02f52020-07-15 16:02:01 -0400940 texMatrix, GrSamplerState::Filter::kLinear);
Brian Salomon63a0a752020-06-26 13:32:09 -0400941 auto vColFP = GrColorMatrixFragmentProcessor::Make(std::move(vTexFP), vM,
942 /*unpremulInput=*/false,
943 /*clampRGBOutput=*/true,
944 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400945 vPaint.setColorFragmentProcessor(std::move(vColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400946 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
947 vRTC->fillRectToRect(nullptr, std::move(vPaint), GrAA::kNo, SkMatrix::I(), dstRectUV,
948 dstRectUV);
949 if (!doSynchronousRead) {
950 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
951 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
952 if (!vTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400953 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400954 return;
955 }
956 }
957
958 if (doSynchronousRead) {
959 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
960 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
961 size_t yRB = yInfo.minRowBytes();
962 size_t uvRB = uvInfo.minRowBytes();
963 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
964 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
965 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400966 if (!yRTC->readPixels(dContext, yInfo, y.get(), yRB, {0, 0}) ||
967 !uRTC->readPixels(dContext, uvInfo, u.get(), uvRB, {0, 0}) ||
968 !vRTC->readPixels(dContext, uvInfo, v.get(), uvRB, {0, 0})) {
969 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400970 return;
971 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400972 auto result = std::make_unique<AsyncReadResult>(dContext->priv().contextID());
Brian Salomon63a0a752020-06-26 13:32:09 -0400973 result->addCpuPlane(std::move(y), yRB );
974 result->addCpuPlane(std::move(u), uvRB);
975 result->addCpuPlane(std::move(v), uvRB);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400976 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400977 return;
978 }
979
980 struct FinishContext {
981 ReadPixelsCallback* fClientCallback;
982 ReadPixelsContext fClientContext;
983 GrClientMappedBufferManager* fMappedBufferManager;
984 SkISize fSize;
985 PixelTransferResult fYTransfer;
986 PixelTransferResult fUTransfer;
987 PixelTransferResult fVTransfer;
988 };
989 // Assumption is that the caller would like to flush. We could take a parameter or require an
990 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
991 // callback to GrGpu until after the next flush that flushes our op list, though.
992 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400993 callbackContext,
994 dContext->priv().clientMappedBufferManager(),
Brian Salomon63a0a752020-06-26 13:32:09 -0400995 dstSize,
996 std::move(yTransfer),
997 std::move(uTransfer),
998 std::move(vTransfer)};
999 auto finishCallback = [](GrGpuFinishedContext c) {
1000 const auto* context = reinterpret_cast<const FinishContext*>(c);
1001 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
1002 auto manager = context->fMappedBufferManager;
1003 size_t rowBytes = SkToSizeT(context->fSize.width());
1004 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
1005 (*context->fClientCallback)(context->fClientContext, nullptr);
1006 delete context;
1007 return;
1008 }
1009 rowBytes /= 2;
1010 SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
1011 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
1012 (*context->fClientCallback)(context->fClientContext, nullptr);
1013 delete context;
1014 return;
1015 }
1016 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
1017 (*context->fClientCallback)(context->fClientContext, nullptr);
1018 delete context;
1019 return;
1020 }
1021 (*context->fClientCallback)(context->fClientContext, std::move(result));
1022 delete context;
1023 };
1024 GrFlushInfo flushInfo;
1025 flushInfo.fFinishedContext = finishContext;
1026 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -05001027 dContext->priv().flushSurface(this->asSurfaceProxy(),
1028 SkSurface::BackendSurfaceAccess::kNoAccess,
1029 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -04001030}
1031
Brian Salomonc5243782020-04-02 12:50:34 -04001032bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001033 ASSERT_SINGLE_OWNER
1034 RETURN_FALSE_IF_ABANDONED
1035 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -04001036 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -04001037
Brian Salomon947efe22019-07-16 15:36:11 -04001038 const GrCaps* caps = fContext->priv().caps();
1039
Greg Daniel46cfbc62019-06-07 11:43:30 -04001040 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -05001041 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -04001042
Stephen White3c0a50f2020-01-16 18:19:54 -05001043 if (this->asSurfaceProxy()->framebufferOnly()) {
1044 return false;
1045 }
1046
Chris Daltonf8e5aad2019-08-02 12:55:23 -06001047 if (!caps->canCopySurface(this->asSurfaceProxy(), src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -04001048 return false;
1049 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001050
Greg Daniel16f5c652019-10-29 11:26:01 -04001051 // The swizzle doesn't matter for copies and it is not used.
1052 return this->drawingManager()->newCopyRenderTask(
Brian Salomonc5243782020-04-02 12:50:34 -04001053 GrSurfaceProxyView(sk_ref_sp(src), this->origin(), GrSwizzle("rgba")), srcRect,
Greg Daniel46e366a2019-12-16 14:38:36 -05001054 this->readSurfaceView(), dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001055}
Greg Daniel46cfbc62019-06-07 11:43:30 -04001056
Brian Salomon63a0a752020-06-26 13:32:09 -04001057std::unique_ptr<GrRenderTargetContext> GrSurfaceContext::rescale(const GrImageInfo& info,
1058 GrSurfaceOrigin origin,
1059 SkIRect srcRect,
1060 RescaleGamma rescaleGamma,
1061 SkFilterQuality rescaleQuality) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001062 auto rtProxy = this->asRenderTargetProxy();
1063 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1064 return nullptr;
1065 }
1066
Stephen White3c0a50f2020-01-16 18:19:54 -05001067 if (this->asSurfaceProxy()->framebufferOnly()) {
1068 return nullptr;
1069 }
1070
Brian Salomone9ad9982019-07-22 16:17:41 -04001071 // We rescale by drawing and don't currently support drawing to a kUnpremul destination.
1072 if (info.alphaType() == kUnpremul_SkAlphaType) {
1073 return nullptr;
1074 }
1075
Greg Daniel40903af2020-01-30 14:55:05 -05001076 GrSurfaceProxyView texView = this->readSurfaceView();
Brian Salomonfc118442019-11-22 19:09:27 -05001077 SkAlphaType srcAlphaType = this->colorInfo().alphaType();
Greg Daniel40903af2020-01-30 14:55:05 -05001078 if (!texView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -04001079 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect,
Brian Salomonc5243782020-04-02 12:50:34 -04001080 SkBackingFit::kApprox, SkBudgeted::kNo);
1081 if (!texView) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001082 return nullptr;
1083 }
Greg Daniel40903af2020-01-30 14:55:05 -05001084 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001085 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001086 }
1087
Brian Salomonbf6b9792019-08-21 09:38:10 -04001088 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
1089 // pass B is moved to A. If 'this' is the input on the first pass then tempA is null.
1090 std::unique_ptr<GrRenderTargetContext> tempA;
1091 std::unique_ptr<GrRenderTargetContext> tempB;
1092
Brian Salomone9ad9982019-07-22 16:17:41 -04001093 // Assume we should ignore the rescale linear request if the surface has no color space since
1094 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon63a0a752020-06-26 13:32:09 -04001095 if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() &&
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001096 !this->colorInfo().colorSpace()->gammaIsLinear()) {
1097 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomonfc118442019-11-22 19:09:27 -05001098 auto xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(), srcAlphaType, cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -04001099 kPremul_SkAlphaType);
1100 // We'll fall back to kRGBA_8888 if half float not supported.
Greg Daniele20fcad2020-01-08 11:52:34 -05001101 auto linearRTC = GrRenderTargetContext::MakeWithFallback(
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001102 fContext, GrColorType::kRGBA_F16, cs, SkBackingFit::kApprox, srcRect.size(), 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001103 GrMipmapped::kNo, GrProtected::kNo, origin);
Brian Salomone9ad9982019-07-22 16:17:41 -04001104 if (!linearRTC) {
1105 return nullptr;
1106 }
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001107 // 1-to-1 draw can always be kFast.
Brian Salomone69b9ef2020-07-22 11:18:06 -04001108 linearRTC->drawTexture(nullptr,
1109 std::move(texView),
1110 srcAlphaType,
1111 GrSamplerState::Filter::kNearest,
1112 GrSamplerState::MipmapMode::kNone,
1113 SkBlendMode::kSrc,
1114 SK_PMColor4fWHITE,
1115 SkRect::Make(srcRect),
1116 SkRect::Make(srcRect.size()),
1117 GrAA::kNo,
1118 GrQuadAAFlags::kNone,
1119 SkCanvas::kFast_SrcRectConstraint,
1120 SkMatrix::I(),
1121 std::move(xform));
Greg Daniel40903af2020-01-30 14:55:05 -05001122 texView = linearRTC->readSurfaceView();
1123 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -04001124 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001125 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001126 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001127
Brian Salomon9c6f6bf2020-05-29 16:37:26 -04001128 while (srcRect.size() != info.dimensions()) {
Brian Salomon59f31b12020-06-04 17:27:15 -04001129 SkISize nextDims = info.dimensions();
1130 if (rescaleQuality != kNone_SkFilterQuality) {
1131 if (srcRect.width() > info.width()) {
1132 nextDims.fWidth = std::max((srcRect.width() + 1)/2, info.width());
1133 } else if (srcRect.width() < info.width()) {
1134 nextDims.fWidth = std::min(srcRect.width()*2, info.width());
1135 }
1136 if (srcRect.height() > info.height()) {
1137 nextDims.fHeight = std::max((srcRect.height() + 1)/2, info.height());
1138 } else if (srcRect.height() < info.height()) {
1139 nextDims.fHeight = std::min(srcRect.height()*2, info.height());
1140 }
1141 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001142 auto input = tempA ? tempA.get() : this;
Brian Salomon59f31b12020-06-04 17:27:15 -04001143 GrColorType colorType = input->colorInfo().colorType();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001144 auto cs = input->colorInfo().refColorSpace();
Brian Salomone9ad9982019-07-22 16:17:41 -04001145 sk_sp<GrColorSpaceXform> xform;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001146 auto prevAlphaType = input->colorInfo().alphaType();
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001147 if (nextDims == info.dimensions()) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001148 // Might as well fold conversion to final info in the last step.
1149 cs = info.refColorSpace();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001150 xform = GrColorSpaceXform::Make(input->colorInfo().colorSpace(),
1151 input->colorInfo().alphaType(), cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -04001152 info.alphaType());
1153 }
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001154 tempB = GrRenderTargetContext::MakeWithFallback(fContext, colorType, std::move(cs),
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001155 SkBackingFit::kApprox, nextDims, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001156 GrMipmapped::kNo, GrProtected::kNo, origin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001157 if (!tempB) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001158 return nullptr;
1159 }
Brian Salomon59f31b12020-06-04 17:27:15 -04001160 auto dstRect = SkRect::Make(nextDims);
1161 if (rescaleQuality == kHigh_SkFilterQuality) {
1162 SkMatrix matrix;
1163 matrix.setScaleTranslate((float)srcRect.width()/nextDims.width(),
1164 (float)srcRect.height()/nextDims.height(),
1165 srcRect.x(),
1166 srcRect.y());
1167 std::unique_ptr<GrFragmentProcessor> fp;
1168 auto dir = GrBicubicEffect::Direction::kXY;
1169 if (nextDims.width() == srcRect.width()) {
1170 dir = GrBicubicEffect::Direction::kY;
1171 } else if (nextDims.height() == srcRect.height()) {
1172 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -04001173 }
Brian Salomon1af72d12020-06-25 10:47:26 -04001174 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
Mike Reed3867c702020-09-01 13:28:10 -04001175 static constexpr auto kKernel = GrBicubicEffect::gCatmullRom;
Brian Salomon59f31b12020-06-04 17:27:15 -04001176 fp = GrBicubicEffect::MakeSubset(std::move(texView), prevAlphaType, matrix, kWM, kWM,
Brian Salomon1af72d12020-06-25 10:47:26 -04001177 SkRect::Make(srcRect), kKernel, dir, *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -04001178 if (xform) {
1179 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -04001180 }
Brian Salomon59f31b12020-06-04 17:27:15 -04001181 GrPaint paint;
John Stiles5933d7d2020-07-21 12:28:35 -04001182 paint.setColorFragmentProcessor(std::move(fp));
Brian Salomon59f31b12020-06-04 17:27:15 -04001183 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
1184 tempB->fillRectToRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect,
1185 dstRect);
1186 } else {
1187 auto filter = rescaleQuality == kNone_SkFilterQuality ? GrSamplerState::Filter::kNearest
Brian Salomona3b02f52020-07-15 16:02:01 -04001188 : GrSamplerState::Filter::kLinear;
Brian Salomon59f31b12020-06-04 17:27:15 -04001189 // Minimizing draw with integer coord src and dev rects can always be kFast.
1190 auto constraint = SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint;
1191 if (nextDims.width() <= srcRect.width() && nextDims.height() <= srcRect.height()) {
1192 constraint = SkCanvas::SrcRectConstraint::kFast_SrcRectConstraint;
1193 }
Brian Salomone69b9ef2020-07-22 11:18:06 -04001194 tempB->drawTexture(nullptr,
1195 std::move(texView),
1196 srcAlphaType,
1197 filter,
1198 GrSamplerState::MipmapMode::kNone,
1199 SkBlendMode::kSrc,
1200 SK_PMColor4fWHITE,
1201 SkRect::Make(srcRect),
1202 dstRect,
1203 GrAA::kNo,
1204 GrQuadAAFlags::kNone,
1205 constraint,
1206 SkMatrix::I(),
1207 std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -04001208 }
Greg Daniel40903af2020-01-30 14:55:05 -05001209 texView = tempB->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -04001210 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001211 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomone9ad9982019-07-22 16:17:41 -04001212 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001213 SkASSERT(tempA);
1214 return tempA;
Brian Salomone9ad9982019-07-22 16:17:41 -04001215}
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001216
1217GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
1218 const SkIRect& rect) {
1219 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1220 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001221 auto direct = fContext->asDirectContext();
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001222 if (!direct) {
1223 return {};
1224 }
1225 auto rtProxy = this->asRenderTargetProxy();
1226 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1227 return {};
1228 }
1229
1230 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001231 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
1232 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001233 // Fail if read color type does not have all of dstCT's color channels and those missing color
1234 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001235 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1236 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
1237 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1238 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001239 return {};
1240 }
1241
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001242 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001243 !supportedRead.fOffsetAlignmentForTransferBuffer) {
1244 return {};
1245 }
1246
1247 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
1248 size_t size = rowBytes * rect.height();
1249 auto buffer = direct->priv().resourceProvider()->createBuffer(
1250 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
1251 if (!buffer) {
1252 return {};
1253 }
1254 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -05001255 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001256 if (flip) {
1257 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1258 this->height() - rect.fTop);
1259 }
Greg Danielbbfec9d2019-08-20 10:56:51 -04001260 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001261 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -04001262 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001263 PixelTransferResult result;
1264 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001265 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -04001266 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001267 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
1268 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001269 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
1270 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001271 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
1272 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -04001273 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001274 };
1275 }
1276 return result;
1277}
Greg Daniel46e366a2019-12-16 14:38:36 -05001278
1279#ifdef SK_DEBUG
1280void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001281 SkASSERT(fReadView.proxy());
1282 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -04001283 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
1284 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
1285 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
1286 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001287 this->onValidate();
1288}
1289#endif