blob: ab40c07e4c436505a77a337ca8070c5d03ce8f05 [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 Salomon1c86b632020-12-11 12:36:01 -050017#include "src/gpu/GrColorSpaceXform.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040018#include "src/gpu/GrDataUtils.h"
Adlai Hollera0693042020-10-14 11:23:11 -040019#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrDrawingManager.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040021#include "src/gpu/GrGpu.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040022#include "src/gpu/GrImageInfo.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040023#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050025#include "src/gpu/GrSurfaceDrawContext.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,
Brian Salomon14f99fc2020-12-07 12:19:47 -050035 const GrColorInfo& info) {
Greg Daniele20fcad2020-01-08 11:52:34 -050036 // It is probably not necessary to check if the context is abandoned here since uses of the
37 // GrSurfaceContext which need the context will mostly likely fail later on without an issue.
38 // However having this hear adds some reassurance in case there is a path doesn't handle an
39 // abandoned context correctly. It also lets us early out of some extra work.
Robert Phillips9eb00022020-06-30 15:30:12 -040040 if (context->abandoned()) {
Greg Daniele20fcad2020-01-08 11:52:34 -050041 return nullptr;
42 }
Greg Daniel3912a4b2020-01-14 09:56:04 -050043 GrSurfaceProxy* proxy = readView.proxy();
Greg Danielbfa19c42019-12-19 16:41:40 -050044 SkASSERT(proxy && proxy->asTextureProxy());
45
Greg Danielbfa19c42019-12-19 16:41:40 -050046 std::unique_ptr<GrSurfaceContext> surfaceContext;
Greg Daniel3912a4b2020-01-14 09:56:04 -050047 if (proxy->asRenderTargetProxy()) {
Brian Salomon14f99fc2020-12-07 12:19:47 -050048 SkASSERT(info.alphaType() == kPremul_SkAlphaType ||
49 info.alphaType() == kOpaque_SkAlphaType);
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;
Brian Salomon14f99fc2020-12-07 12:19:47 -050053 if (info.colorType() != GrColorType::kUnknown) {
54 writeSwizzle = context->priv().caps()->getWriteSwizzle(proxy->backendFormat(),
55 info.colorType());
Brian Salomonc5243782020-04-02 12:50:34 -040056 }
Brian Salomon8afde5f2020-04-01 16:22:00 -040057 GrSurfaceProxyView writeView(readView.refProxy(), readView.origin(), writeSwizzle);
Brian Salomoneebe7352020-12-09 16:37:04 -050058 surfaceContext = std::make_unique<GrSurfaceDrawContext>(context,
59 std::move(readView),
60 std::move(writeView),
61 info.colorType(),
62 info.refColorSpace(),
63 /*surface props*/ nullptr);
Greg Danielbfa19c42019-12-19 16:41:40 -050064 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -050065 surfaceContext = std::make_unique<GrSurfaceContext>(context, std::move(readView), info);
Greg Danielbfa19c42019-12-19 16:41:40 -050066 }
Robert Phillips07f0e412020-01-17 15:20:00 -050067 SkDEBUGCODE(surfaceContext->validate();)
Greg Danielbfa19c42019-12-19 16:41:40 -050068 return surfaceContext;
69}
70
Brian Salomona56a7462020-02-07 14:17:25 -050071std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
Brian Salomon14f99fc2020-12-07 12:19:47 -050072 const GrImageInfo& info,
Brian Salomona56a7462020-02-07 14:17:25 -050073 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050074 SkBackingFit fit,
Brian Salomon14f99fc2020-12-07 12:19:47 -050075 GrSurfaceOrigin origin,
76 GrRenderable renderable,
77 int sampleCount,
78 GrMipmapped mipmapped,
79 GrProtected isProtected,
Brian Salomona56a7462020-02-07 14:17:25 -050080 SkBudgeted budgeted) {
Brian Salomon14f99fc2020-12-07 12:19:47 -050081 SkASSERT(context);
82 SkASSERT(renderable == GrRenderable::kYes || sampleCount == 1);
83 if (context->abandoned()) {
84 return nullptr;
Brian Salomond005b692020-04-01 15:47:05 -040085 }
Brian Salomon14f99fc2020-12-07 12:19:47 -050086 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(format,
87 info.dimensions(),
88 renderable,
89 sampleCount,
90 mipmapped,
91 fit,
92 budgeted,
93 isProtected);
Greg Danielbfa19c42019-12-19 16:41:40 -050094 if (!proxy) {
95 return nullptr;
96 }
97
Brian Salomon14f99fc2020-12-07 12:19:47 -050098 GrSwizzle swizzle;
99 if (info.colorType() != GrColorType::kUnknown &&
100 !context->priv().caps()->isFormatCompressed(format)) {
101 swizzle = context->priv().caps()->getReadSwizzle(format, info.colorType());
102 }
103
Greg Daniel3912a4b2020-01-14 09:56:04 -0500104 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500105 return GrSurfaceContext::Make(context, std::move(view), info.colorInfo());
106}
107
108std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
109 const GrImageInfo& info,
110 SkBackingFit fit,
111 GrSurfaceOrigin origin,
112 GrRenderable renderable,
113 int sampleCount,
114 GrMipmapped mipmapped,
115 GrProtected isProtected,
116 SkBudgeted budgeted) {
117 GrBackendFormat format = context->priv().caps()->getDefaultBackendFormat(info.colorType(),
118 renderable);
119 return Make(context,
120 info,
121 format,
122 fit,
123 origin,
124 renderable,
125 sampleCount,
126 mipmapped,
127 isProtected,
128 budgeted);
Greg Danielbfa19c42019-12-19 16:41:40 -0500129}
130
Greg Danielf41b2bd2019-08-22 16:19:24 -0400131// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
132// GrOpsTasks to be picked up and added to by renderTargetContexts lower in the call
133// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500134// when the surfaceDrawContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500135GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500136 GrSurfaceProxyView readView,
Brian Salomon14f99fc2020-12-07 12:19:47 -0500137 const GrColorInfo& info)
138 : fContext(context), fReadView(std::move(readView)), fColorInfo(info) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400139 SkASSERT(!context->abandoned());
Greg Daniele20fcad2020-01-08 11:52:34 -0500140}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400141
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400142const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); }
143
Robert Phillips0d075de2019-03-04 11:08:13 -0500144GrAuditTrail* GrSurfaceContext::auditTrail() {
145 return fContext->priv().auditTrail();
146}
147
148GrDrawingManager* GrSurfaceContext::drawingManager() {
149 return fContext->priv().drawingManager();
150}
151
152const GrDrawingManager* GrSurfaceContext::drawingManager() const {
153 return fContext->priv().drawingManager();
154}
155
156#ifdef SK_DEBUG
Brian Salomon70fe17e2020-11-30 14:33:58 -0500157GrSingleOwner* GrSurfaceContext::singleOwner() const { return fContext->priv().singleOwner(); }
Robert Phillips0d075de2019-03-04 11:08:13 -0500158#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400159
Adlai Hollerc95b5892020-08-11 12:02:22 -0400160bool GrSurfaceContext::readPixels(GrDirectContext* dContext, const GrImageInfo& origDstInfo,
161 void* dst, size_t rowBytes, SkIPoint pt) {
Brian Salomon1d435302019-07-01 13:05:28 -0400162 ASSERT_SINGLE_OWNER
163 RETURN_FALSE_IF_ABANDONED
164 SkDEBUGCODE(this->validate();)
165 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400166 if (!fContext->priv().matches(dContext)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400167 return false;
168 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400169
Brian Salomon1d435302019-07-01 13:05:28 -0400170 if (!dst) {
171 return false;
172 }
173
Brian Salomon1047a492019-07-02 12:25:21 -0400174 size_t tightRowBytes = origDstInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400175 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400176 rowBytes = tightRowBytes;
177 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400178 return false;
179 }
180
181 if (!origDstInfo.isValid()) {
182 return false;
183 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400184
185 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
186
Stephen White3c0a50f2020-01-16 18:19:54 -0500187 if (srcProxy->framebufferOnly()) {
188 return false;
189 }
190
Greg Daniel6eb8c242019-06-05 10:22:24 -0400191 // MDB TODO: delay this instantiation until later in the method
Adlai Hollerc95b5892020-08-11 12:02:22 -0400192 if (!srcProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400193 return false;
194 }
195
196 GrSurface* srcSurface = srcProxy->peekSurface();
197
Brian Salomon1d435302019-07-01 13:05:28 -0400198 auto dstInfo = origDstInfo;
199 if (!dstInfo.clip(this->width(), this->height(), &pt, &dst, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400200 return false;
201 }
Brian Salomon1047a492019-07-02 12:25:21 -0400202 // Our tight row bytes may have been changed by clipping.
203 tightRowBytes = dstInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400204
Mike Klein7321e6a2019-12-03 11:08:40 -0600205 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{this->colorInfo(), dstInfo}.flags;
206 bool unpremul = flags.unpremul,
207 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
208 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400209
Adlai Hollerc95b5892020-08-11 12:02:22 -0400210 const GrCaps* caps = dContext->priv().caps();
Robert Phillips07f0e412020-01-17 15:20:00 -0500211 bool srcIsCompressed = caps->isFormatCompressed(srcSurface->backendFormat());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400212 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
213 // care so much about getImageData performance. However, in order to ensure putImageData/
214 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
215 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
216 // fContext->vaildaPMUPMConversionExists()).
Greg Daniel0258c902019-08-01 13:08:33 -0400217 GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
218 GrRenderable::kYes);
Greg Danielc71c7962020-01-14 16:44:18 -0500219 GrColorType srcColorType = this->colorInfo().colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400220 bool canvas2DFastPath = unpremul && !needColorConversion &&
221 (GrColorType::kRGBA_8888 == dstInfo.colorType() ||
222 GrColorType::kBGRA_8888 == dstInfo.colorType()) &&
223 SkToBool(srcProxy->asTextureProxy()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500224 (srcColorType == GrColorType::kRGBA_8888 ||
225 srcColorType == GrColorType::kBGRA_8888) &&
Greg Daniel0258c902019-08-01 13:08:33 -0400226 defaultRGBAFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400227 dContext->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400228
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400229 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400230 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400231 return false;
232 }
233
Brian Salomondc0710f2019-07-01 14:59:32 -0400234 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400235 std::unique_ptr<GrSurfaceContext> tempCtx;
236 if (this->asTextureProxy()) {
237 GrColorType colorType = (canvas2DFastPath || srcIsCompressed)
238 ? GrColorType::kRGBA_8888
239 : this->colorInfo().colorType();
Brian Salomoneebe7352020-12-09 16:37:04 -0500240 tempCtx = GrSurfaceDrawContext::Make(
Christopher Cameron75ae8fc2020-11-17 22:03:49 -0800241 dContext, colorType, this->colorInfo().refColorSpace(), SkBackingFit::kApprox,
242 dstInfo.dimensions(), 1, GrMipMapped::kNo, GrProtected::kNo,
243 kTopLeft_GrSurfaceOrigin);
Brian Salomon72c7b982020-10-06 10:07:38 -0400244 if (!tempCtx) {
245 return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400246 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400247
248 std::unique_ptr<GrFragmentProcessor> fp;
249 if (canvas2DFastPath) {
250 fp = dContext->priv().createPMToUPMEffect(GrTextureEffect::Make(
251 this->readSurfaceView(), this->colorInfo().alphaType()));
252 if (dstInfo.colorType() == GrColorType::kBGRA_8888) {
253 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
254 dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888);
255 }
256 // The render target context is incorrectly tagged as kPremul even though we're
257 // writing unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type
258 // so we don't double unpremul.
259 dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType);
260 } else {
261 fp = GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType());
262 }
263 if (!fp) {
264 return false;
265 }
266 GrPaint paint;
267 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
268 paint.setColorFragmentProcessor(std::move(fp));
269
270 tempCtx->asRenderTargetContext()->fillRectToRect(
271 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
272 SkRect::MakeWH(dstInfo.width(), dstInfo.height()),
273 SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height()));
274 pt = {0, 0};
Greg Daniel6eb8c242019-06-05 10:22:24 -0400275 } else {
Brian Salomon72c7b982020-10-06 10:07:38 -0400276 auto restrictions = this->caps()->getDstCopyRestrictions(this->asRenderTargetProxy(),
277 this->colorInfo().colorType());
278 sk_sp<GrSurfaceProxy> copy;
279 static constexpr auto kFit = SkBackingFit::kExact;
280 static constexpr auto kBudgeted = SkBudgeted::kYes;
281 static constexpr auto kMipMapped = GrMipMapped::kNo;
282 if (restrictions.fMustCopyWholeSrc) {
283 copy = GrSurfaceProxy::Copy(fContext, srcProxy, this->origin(), kMipMapped, kFit,
284 kBudgeted);
285 } else {
286 auto srcRect = SkIRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height());
287 copy = GrSurfaceProxy::Copy(fContext, srcProxy, this->origin(), kMipMapped, srcRect,
288 kFit, kBudgeted, restrictions.fRectsMustMatch);
289 pt = {0, 0};
290 }
291 if (!copy) {
292 return false;
293 }
294 GrSurfaceProxyView view{std::move(copy), this->origin(), this->readSwizzle()};
Brian Salomon14f99fc2020-12-07 12:19:47 -0500295 tempCtx = GrSurfaceContext::Make(dContext, std::move(view), this->colorInfo());
Brian Salomon72c7b982020-10-06 10:07:38 -0400296 SkASSERT(tempCtx);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400297 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400298 return tempCtx->readPixels(dContext, dstInfo, dst, rowBytes, pt);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400299 }
300
Greg Danielb8d84f82020-02-13 14:25:00 -0500301 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400302
Brian Salomon1d435302019-07-01 13:05:28 -0400303 auto supportedRead = caps->supportedReadPixelsColorType(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400304 this->colorInfo().colorType(), srcProxy->backendFormat(), dstInfo.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400305
Brian Salomon1047a492019-07-02 12:25:21 -0400306 bool makeTight = !caps->readPixelsRowBytesSupport() && tightRowBytes != rowBytes;
307
308 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomon8f8354a2019-07-31 20:12:02 -0400309 (dstInfo.colorType() != supportedRead.fColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400310
Brian Salomonf30b1c12019-06-20 12:25:02 -0400311 std::unique_ptr<char[]> tmpPixels;
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400312 GrImageInfo tmpInfo;
Brian Salomon1d435302019-07-01 13:05:28 -0400313 void* readDst = dst;
314 size_t readRB = rowBytes;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400315 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400316 tmpInfo = {supportedRead.fColorType, this->colorInfo().alphaType(),
317 this->colorInfo().refColorSpace(), dstInfo.width(), dstInfo.height()};
Brian Salomon1d435302019-07-01 13:05:28 -0400318 size_t tmpRB = tmpInfo.minRowBytes();
319 size_t size = tmpRB * tmpInfo.height();
320 // Chrome MSAN bots require the data to be initialized (hence the ()).
John Stilesfbd050b2020-08-03 13:21:46 -0400321 tmpPixels = std::make_unique<char[]>(size);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400322
Brian Salomonf30b1c12019-06-20 12:25:02 -0400323 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400324 readRB = tmpRB;
325 pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400326 }
327
Adlai Hollerc95b5892020-08-11 12:02:22 -0400328 dContext->priv().flushSurface(srcProxy);
329 dContext->submit();
330 if (!dContext->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(),
331 dstInfo.height(), this->colorInfo().colorType(),
332 supportedRead.fColorType, readDst, readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400333 return false;
334 }
335
Greg Daniel6eb8c242019-06-05 10:22:24 -0400336 if (convert) {
Brian Salomon8f8354a2019-07-31 20:12:02 -0400337 return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400338 }
339 return true;
340}
Robert Phillips0d075de2019-03-04 11:08:13 -0500341
Adlai Hollerc95b5892020-08-11 12:02:22 -0400342bool GrSurfaceContext::writePixels(GrDirectContext* dContext, const GrImageInfo& origSrcInfo,
343 const void* src, size_t rowBytes, SkIPoint pt) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400344 ASSERT_SINGLE_OWNER
345 RETURN_FALSE_IF_ABANDONED
346 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400347 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400348
Adlai Hollerc95b5892020-08-11 12:02:22 -0400349 if (!dContext) {
Brian Salomonc320b152018-02-20 14:05:36 -0500350 return false;
351 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500352
Brian Salomon1d435302019-07-01 13:05:28 -0400353 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500354 return false;
355 }
356
Brian Salomon1d435302019-07-01 13:05:28 -0400357 if (!src) {
358 return false;
359 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400360
Brian Salomon1047a492019-07-02 12:25:21 -0400361 size_t tightRowBytes = origSrcInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400362 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400363 rowBytes = tightRowBytes;
364 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400365 return false;
366 }
367
368 if (!origSrcInfo.isValid()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400369 return false;
370 }
371
372 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
Stephen White3c0a50f2020-01-16 18:19:54 -0500373
374 if (dstProxy->framebufferOnly()) {
375 return false;
376 }
377
Adlai Hollerc95b5892020-08-11 12:02:22 -0400378 if (!dstProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400379 return false;
380 }
381
382 GrSurface* dstSurface = dstProxy->peekSurface();
383
Brian Salomon1d435302019-07-01 13:05:28 -0400384 auto srcInfo = origSrcInfo;
385 if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400386 return false;
387 }
Brian Salomon1047a492019-07-02 12:25:21 -0400388 // Our tight row bytes may have been changed by clipping.
389 tightRowBytes = srcInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400390
Mike Klein7321e6a2019-12-03 11:08:40 -0600391 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{srcInfo, this->colorInfo()}.flags;
392 bool unpremul = flags.unpremul,
393 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
394 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400395
Adlai Hollerc95b5892020-08-11 12:02:22 -0400396 const GrCaps* caps = dContext->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400397
398 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
399 GrRenderable::kNo);
400
Greg Danielc71c7962020-01-14 16:44:18 -0500401 GrColorType dstColorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400402 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
403 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400404 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
405 (srcInfo.colorType() == GrColorType::kRGBA_8888 ||
406 srcInfo.colorType() == GrColorType::kBGRA_8888) &&
407 SkToBool(this->asRenderTargetContext()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500408 (dstColorType == GrColorType::kRGBA_8888 ||
409 dstColorType == GrColorType::kBGRA_8888) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400410 rgbaDefaultFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400411 dContext->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400412
413 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500414 GrColorInfo tempColorInfo;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400415 GrBackendFormat format;
Greg Danielbfa19c42019-12-19 16:41:40 -0500416 GrSwizzle tempReadSwizzle;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400417 if (canvas2DFastPath) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500418 tempColorInfo = {GrColorType::kRGBA_8888,
419 kUnpremul_SkAlphaType,
420 this->colorInfo().refColorSpace()};
Greg Daniel7bfc9132019-08-14 14:23:53 -0400421 format = rgbaDefaultFormat;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400422 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500423 tempColorInfo = this->colorInfo();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400424 format = dstProxy->backendFormat().makeTexture2D();
425 if (!format.isValid()) {
426 return false;
427 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500428 tempReadSwizzle = this->readSwizzle();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400429 }
430
Greg Daniel2e52ad12019-06-13 10:04:16 -0400431 // It is more efficient for us to write pixels into a top left origin so we prefer that.
432 // However, if the final proxy isn't a render target then we must use a copy to move the
433 // data into it which requires the origins to match. If the final proxy is a render target
434 // we can use a draw instead which doesn't have this origin restriction. Thus for render
435 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400436 GrSurfaceOrigin tempOrigin =
Greg Danielb8d84f82020-02-13 14:25:00 -0500437 this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : this->origin();
Adlai Hollerc95b5892020-08-11 12:02:22 -0400438 auto tempProxy = dContext->priv().proxyProvider()->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400439 format, srcInfo.dimensions(), GrRenderable::kNo, 1, GrMipmapped::kNo,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400440 SkBackingFit::kApprox, SkBudgeted::kYes, GrProtected::kNo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400441 if (!tempProxy) {
442 return false;
443 }
Greg Daniel3912a4b2020-01-14 09:56:04 -0500444 GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500445 GrSurfaceContext tempCtx(dContext, tempView, tempColorInfo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400446
447 // In the fast path we always write the srcData to the temp context as though it were RGBA.
448 // When the data is really BGRA the write will cause the R and B channels to be swapped in
449 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
450 // dst.
Brian Salomon1d435302019-07-01 13:05:28 -0400451 if (canvas2DFastPath) {
452 srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888);
453 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400454 if (!tempCtx.writePixels(dContext, srcInfo, src, rowBytes, {0, 0})) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400455 return false;
456 }
457
458 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400459 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400460 if (canvas2DFastPath) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400461 fp = dContext->priv().createUPMToPMEffect(
Brian Salomon14f99fc2020-12-07 12:19:47 -0500462 GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType()));
Brian Salomon1d435302019-07-01 13:05:28 -0400463 // Important: check the original src color type here!
464 if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400465 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
466 }
467 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500468 fp = GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400469 }
470 if (!fp) {
471 return false;
472 }
473 GrPaint paint;
474 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
John Stiles5933d7d2020-07-21 12:28:35 -0400475 paint.setColorFragmentProcessor(std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400476 this->asRenderTargetContext()->fillRectToRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400477 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400478 SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()),
479 SkRect::MakeWH(srcInfo.width(), srcInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400480 } else {
Brian Salomon1d435302019-07-01 13:05:28 -0400481 SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height());
482 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Brian Salomonc5243782020-04-02 12:50:34 -0400483 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400484 return false;
485 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400486 }
487 return true;
488 }
489
Brian Salomon1d435302019-07-01 13:05:28 -0400490 GrColorType allowedColorType =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400491 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400492 dstProxy->backendFormat(),
493 srcInfo.colorType()).fColorType;
Greg Danielb8d84f82020-02-13 14:25:00 -0500494 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon1047a492019-07-02 12:25:21 -0400495 bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes;
496 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomon1d435302019-07-01 13:05:28 -0400497 (srcInfo.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400498
Brian Salomonf30b1c12019-06-20 12:25:02 -0400499 std::unique_ptr<char[]> tmpPixels;
Brian Salomon1d435302019-07-01 13:05:28 -0400500 GrColorType srcColorType = srcInfo.colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400501 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400502 GrImageInfo tmpInfo(allowedColorType, this->colorInfo().alphaType(),
503 this->colorInfo().refColorSpace(), srcInfo.width(), srcInfo.height());
Brian Salomon1d435302019-07-01 13:05:28 -0400504 auto tmpRB = tmpInfo.minRowBytes();
505 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400506
Brian Salomon1d435302019-07-01 13:05:28 -0400507 GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400508
Brian Salomon1d435302019-07-01 13:05:28 -0400509 srcColorType = tmpInfo.colorType();
510 rowBytes = tmpRB;
511 src = tmpPixels.get();
512 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400513 }
514
515 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
516 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
517 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
518 // destination proxy)
519 // TODO: should this policy decision just be moved into the drawing manager?
Adlai Hollerc95b5892020-08-11 12:02:22 -0400520 dContext->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400521
Adlai Hollerc95b5892020-08-11 12:02:22 -0400522 return dContext->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(),
523 srcInfo.height(), this->colorInfo().colorType(),
524 srcColorType, src, rowBytes);
Brian Osman45580d32016-11-23 09:37:01 -0500525}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400526
Adlai Hollerc95b5892020-08-11 12:02:22 -0400527void GrSurfaceContext::asyncRescaleAndReadPixels(GrDirectContext* dContext,
528 const SkImageInfo& info,
Brian Salomon63a0a752020-06-26 13:32:09 -0400529 const SkIRect& srcRect,
530 RescaleGamma rescaleGamma,
531 SkFilterQuality rescaleQuality,
532 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400533 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400534 // We implement this by rendering and we don't currently support rendering kUnpremul.
535 if (info.alphaType() == kUnpremul_SkAlphaType) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400536 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400537 return;
538 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400539 if (!dContext) {
540 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400541 return;
542 }
543 auto rt = this->asRenderTargetProxy();
544 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400545 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400546 return;
547 }
548 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400549 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400550 return;
551 }
552 auto dstCT = SkColorTypeToGrColorType(info.colorType());
553 if (dstCT == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400554 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400555 return;
556 }
557 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
558 auto colorTypeOfFinalContext = this->colorInfo().colorType();
559 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
560 if (needsRescale) {
561 colorTypeOfFinalContext = dstCT;
562 backendFormatOfFinalContext =
563 this->caps()->getDefaultBackendFormat(dstCT, GrRenderable::kYes);
564 }
565 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
566 backendFormatOfFinalContext, dstCT);
567 // Fail if we can't read from the source surface's color type.
568 if (readInfo.fColorType == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400569 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400570 return;
571 }
572 // Fail if read color type does not have all of dstCT's color channels and those missing color
573 // channels are in the src.
574 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
575 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
576 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
577 if ((~legalReadChannels & dstChannels) & srcChannels) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400578 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400579 return;
580 }
581
Brian Salomoneebe7352020-12-09 16:37:04 -0500582 std::unique_ptr<GrSurfaceDrawContext> tempRTC;
Brian Salomon63a0a752020-06-26 13:32:09 -0400583 int x = srcRect.fLeft;
584 int y = srcRect.fTop;
585 if (needsRescale) {
586 tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
587 rescaleQuality);
588 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400589 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400590 return;
591 }
592 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
593 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
594 x = y = 0;
595 } else {
596 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
597 this->colorInfo().alphaType(),
598 info.colorSpace(),
599 info.alphaType());
600 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
601 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
602 GrSurfaceProxyView texProxyView = this->readSurfaceView();
603 SkRect srcRectToDraw = SkRect::Make(srcRect);
604 // If the src is not texturable first try to make a copy to a texture.
605 if (!texProxyView.asTextureProxy()) {
606 texProxyView =
Brian Salomon7e67dca2020-07-21 09:27:25 -0400607 GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipmapped::kNo, srcRect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400608 SkBackingFit::kApprox, SkBudgeted::kNo);
609 if (!texProxyView) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400610 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400611 return;
612 }
613 SkASSERT(texProxyView.asTextureProxy());
614 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
615 }
Brian Salomoneebe7352020-12-09 16:37:04 -0500616 tempRTC = GrSurfaceDrawContext::Make(dContext, this->colorInfo().colorType(),
617 info.refColorSpace(), SkBackingFit::kApprox,
618 srcRect.size(), 1, GrMipmapped::kNo,
619 GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400620 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400621 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400622 return;
623 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400624 tempRTC->drawTexture(nullptr,
625 std::move(texProxyView),
626 this->colorInfo().alphaType(),
627 GrSamplerState::Filter::kNearest,
628 GrSamplerState::MipmapMode::kNone,
629 SkBlendMode::kSrc,
630 SK_PMColor4fWHITE,
631 srcRectToDraw,
632 SkRect::MakeWH(srcRect.width(), srcRect.height()),
633 GrAA::kNo,
634 GrQuadAAFlags::kNone,
635 SkCanvas::kFast_SrcRectConstraint,
636 SkMatrix::I(),
637 std::move(xform));
Brian Salomon63a0a752020-06-26 13:32:09 -0400638 x = y = 0;
639 }
640 }
641 auto rtc = tempRTC ? tempRTC.get() : this;
Adlai Hollerc95b5892020-08-11 12:02:22 -0400642 return rtc->asyncReadPixels(dContext, SkIRect::MakeXYWH(x, y, info.width(), info.height()),
643 info.colorType(), callback, callbackContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400644}
645
646class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult {
647public:
648 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
649 ~AsyncReadResult() override {
650 for (int i = 0; i < fPlanes.count(); ++i) {
651 if (!fPlanes[i].fMappedBuffer) {
652 delete[] static_cast<const char*>(fPlanes[i].fData);
653 } else {
654 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
655 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
656 }
657 }
658 }
659
660 int count() const override { return fPlanes.count(); }
661 const void* data(int i) const override { return fPlanes[i].fData; }
662 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
663
664 bool addTransferResult(const PixelTransferResult& result,
665 SkISize dimensions,
666 size_t rowBytes,
667 GrClientMappedBufferManager* manager) {
668 SkASSERT(!result.fTransferBuffer->isMapped());
669 const void* mappedData = result.fTransferBuffer->map();
670 if (!mappedData) {
671 return false;
672 }
673 if (result.fPixelConverter) {
674 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
675 result.fPixelConverter(convertedData.get(), mappedData);
676 this->addCpuPlane(std::move(convertedData), rowBytes);
677 result.fTransferBuffer->unmap();
678 } else {
679 manager->insert(result.fTransferBuffer);
680 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
681 }
682 return true;
683 }
684
685 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
686 SkASSERT(data);
687 SkASSERT(rowBytes > 0);
688 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
689 }
690
691private:
692 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
693 SkASSERT(data);
694 SkASSERT(rowBytes > 0);
695 SkASSERT(mappedBuffer);
696 SkASSERT(mappedBuffer->isMapped());
697 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
698 }
699
700 struct Plane {
701 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
702 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
703 const void* fData;
704 size_t fRowBytes;
705 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
706 sk_sp<GrGpuBuffer> fMappedBuffer;
707 };
708 SkSTArray<3, Plane> fPlanes;
709 uint32_t fInboxID;
710};
711
Adlai Hollerc95b5892020-08-11 12:02:22 -0400712void GrSurfaceContext::asyncReadPixels(GrDirectContext* dContext,
713 const SkIRect& rect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400714 SkColorType colorType,
715 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400716 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400717 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
718 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
719
Adlai Hollerc95b5892020-08-11 12:02:22 -0400720 if (!dContext || this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
721 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400722 return;
723 }
724
Adlai Hollerc95b5892020-08-11 12:02:22 -0400725 auto mappedBufferManager = dContext->priv().clientMappedBufferManager();
Brian Salomon63a0a752020-06-26 13:32:09 -0400726
727 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
728
729 if (!transferResult.fTransferBuffer) {
730 auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
731 this->colorInfo().refColorSpace());
732 auto result = std::make_unique<AsyncReadResult>(0);
733 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
734 SkPixmap pm(ii, data.get(), ii.minRowBytes());
735 result->addCpuPlane(std::move(data), pm.rowBytes());
736
Adlai Hollerc95b5892020-08-11 12:02:22 -0400737 SkIPoint pt{rect.fLeft, rect.fTop};
738 if (!this->readPixels(dContext, ii, pm.writable_addr(), pm.rowBytes(), pt)) {
739 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400740 return;
741 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400742 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400743 return;
744 }
745
746 struct FinishContext {
747 ReadPixelsCallback* fClientCallback;
748 ReadPixelsContext fClientContext;
749 SkISize fSize;
750 SkColorType fColorType;
751 GrClientMappedBufferManager* fMappedBufferManager;
752 PixelTransferResult fTransferResult;
753 };
754 // Assumption is that the caller would like to flush. We could take a parameter or require an
755 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
756 // callback to GrGpu until after the next flush that flushes our op list, though.
757 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400758 callbackContext,
Brian Salomon63a0a752020-06-26 13:32:09 -0400759 rect.size(),
760 colorType,
761 mappedBufferManager,
762 std::move(transferResult)};
763 auto finishCallback = [](GrGpuFinishedContext c) {
764 const auto* context = reinterpret_cast<const FinishContext*>(c);
765 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
766 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
767 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
768 context->fMappedBufferManager)) {
769 result.reset();
770 }
771 (*context->fClientCallback)(context->fClientContext, std::move(result));
772 delete context;
773 };
774 GrFlushInfo flushInfo;
775 flushInfo.fFinishedContext = finishContext;
776 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -0500777
778 dContext->priv().flushSurface(this->asSurfaceProxy(),
779 SkSurface::BackendSurfaceAccess::kNoAccess,
780 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400781}
782
Adlai Hollerc95b5892020-08-11 12:02:22 -0400783void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(GrDirectContext* dContext,
784 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400785 sk_sp<SkColorSpace> dstColorSpace,
786 const SkIRect& srcRect,
787 SkISize dstSize,
788 RescaleGamma rescaleGamma,
789 SkFilterQuality rescaleQuality,
790 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400791 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400792 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
793 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
794 SkASSERT(!dstSize.isZero());
795 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
796
Adlai Hollerc95b5892020-08-11 12:02:22 -0400797 if (!dContext) {
798 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400799 return;
800 }
801 auto rt = this->asRenderTargetProxy();
802 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400803 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400804 return;
805 }
806 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400807 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400808 return;
809 }
810 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400811 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400812 return;
813 }
814 int x = srcRect.fLeft;
815 int y = srcRect.fTop;
816 bool needsRescale = srcRect.size() != dstSize;
817 GrSurfaceProxyView srcView;
818 if (needsRescale) {
819 // We assume the caller wants kPremul. There is no way to indicate a preference.
820 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
821 dstColorSpace);
822 // TODO: Incorporate the YUV conversion into last pass of rescaling.
823 auto tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
824 rescaleQuality);
825 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400826 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400827 return;
828 }
829 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
830 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
831 x = y = 0;
832 srcView = tempRTC->readSurfaceView();
833 } else {
834 srcView = this->readSurfaceView();
835 if (!srcView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400836 srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipmapped::kNo,
Brian Salomon63a0a752020-06-26 13:32:09 -0400837 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
838 if (!srcView) {
839 // If we can't get a texture copy of the contents then give up.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400840 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400841 return;
842 }
843 SkASSERT(srcView.asTextureProxy());
844 x = y = 0;
845 }
846 // We assume the caller wants kPremul. There is no way to indicate a preference.
847 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
848 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
849 kPremul_SkAlphaType);
850 if (xform) {
851 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
Brian Salomoneebe7352020-12-09 16:37:04 -0500852 auto tempRTC = GrSurfaceDrawContext::Make(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400853 dContext, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400854 dstSize, 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400855 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400856 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400857 return;
858 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400859 tempRTC->drawTexture(nullptr,
860 std::move(srcView),
861 this->colorInfo().alphaType(),
862 GrSamplerState::Filter::kNearest,
863 GrSamplerState::MipmapMode::kNone,
864 SkBlendMode::kSrc,
865 SK_PMColor4fWHITE,
866 srcRectToDraw,
867 SkRect::Make(srcRect.size()),
868 GrAA::kNo,
869 GrQuadAAFlags::kNone,
870 SkCanvas::kFast_SrcRectConstraint,
871 SkMatrix::I(),
872 std::move(xform));
Brian Salomon63a0a752020-06-26 13:32:09 -0400873 srcView = tempRTC->readSurfaceView();
874 SkASSERT(srcView.asTextureProxy());
875 x = y = 0;
876 }
877 }
878
Brian Salomoneebe7352020-12-09 16:37:04 -0500879 auto yRTC = GrSurfaceDrawContext::MakeWithFallback(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400880 dContext, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400881 GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400882 int halfW = dstSize.width() /2;
883 int halfH = dstSize.height()/2;
Brian Salomoneebe7352020-12-09 16:37:04 -0500884 auto uRTC = GrSurfaceDrawContext::MakeWithFallback(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400885 dContext, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH},
886 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomoneebe7352020-12-09 16:37:04 -0500887 auto vRTC = GrSurfaceDrawContext::MakeWithFallback(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400888 dContext, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH},
889 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400890 if (!yRTC || !uRTC || !vRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400891 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400892 return;
893 }
894
895 float baseM[20];
896 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
897
898 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
899
900 auto texMatrix = SkMatrix::Translate(x, y);
901
902 SkRect dstRectY = SkRect::Make(dstSize);
903 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
904
905 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
906 PixelTransferResult yTransfer, uTransfer, vTransfer;
907
908 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
909 float yM[20];
910 std::fill_n(yM, 15, 0.f);
911 std::copy_n(baseM + 0, 5, yM + 15);
912 GrPaint yPaint;
913 auto yTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
914 auto yColFP = GrColorMatrixFragmentProcessor::Make(std::move(yTexFP), yM,
915 /*unpremulInput=*/false,
916 /*clampRGBOutput=*/true,
917 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400918 yPaint.setColorFragmentProcessor(std::move(yColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400919 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
920 yRTC->fillRectToRect(nullptr, std::move(yPaint), GrAA::kNo, SkMatrix::I(), dstRectY, dstRectY);
921 if (!doSynchronousRead) {
922 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
923 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
924 if (!yTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400925 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400926 return;
927 }
928 }
929
930 texMatrix.preScale(2.f, 2.f);
931 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
932 float uM[20];
933 std::fill_n(uM, 15, 0.f);
934 std::copy_n(baseM + 5, 5, uM + 15);
935 GrPaint uPaint;
936 auto uTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix,
Brian Salomona3b02f52020-07-15 16:02:01 -0400937 GrSamplerState::Filter::kLinear);
Brian Salomon63a0a752020-06-26 13:32:09 -0400938 auto uColFP = GrColorMatrixFragmentProcessor::Make(std::move(uTexFP), uM,
939 /*unpremulInput=*/false,
940 /*clampRGBOutput=*/true,
941 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400942 uPaint.setColorFragmentProcessor(std::move(uColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400943 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
944 uRTC->fillRectToRect(nullptr, std::move(uPaint), GrAA::kNo, SkMatrix::I(), dstRectUV,
945 dstRectUV);
946 if (!doSynchronousRead) {
947 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
948 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
949 if (!uTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400950 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400951 return;
952 }
953 }
954
955 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
956 float vM[20];
957 std::fill_n(vM, 15, 0.f);
958 std::copy_n(baseM + 10, 5, vM + 15);
959 GrPaint vPaint;
960 auto vTexFP = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType(),
Brian Salomona3b02f52020-07-15 16:02:01 -0400961 texMatrix, GrSamplerState::Filter::kLinear);
Brian Salomon63a0a752020-06-26 13:32:09 -0400962 auto vColFP = GrColorMatrixFragmentProcessor::Make(std::move(vTexFP), vM,
963 /*unpremulInput=*/false,
964 /*clampRGBOutput=*/true,
965 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400966 vPaint.setColorFragmentProcessor(std::move(vColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400967 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
968 vRTC->fillRectToRect(nullptr, std::move(vPaint), GrAA::kNo, SkMatrix::I(), dstRectUV,
969 dstRectUV);
970 if (!doSynchronousRead) {
971 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
972 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
973 if (!vTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400974 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400975 return;
976 }
977 }
978
979 if (doSynchronousRead) {
980 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
981 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
982 size_t yRB = yInfo.minRowBytes();
983 size_t uvRB = uvInfo.minRowBytes();
984 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
985 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
986 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400987 if (!yRTC->readPixels(dContext, yInfo, y.get(), yRB, {0, 0}) ||
988 !uRTC->readPixels(dContext, uvInfo, u.get(), uvRB, {0, 0}) ||
989 !vRTC->readPixels(dContext, uvInfo, v.get(), uvRB, {0, 0})) {
990 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400991 return;
992 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400993 auto result = std::make_unique<AsyncReadResult>(dContext->priv().contextID());
Brian Salomon63a0a752020-06-26 13:32:09 -0400994 result->addCpuPlane(std::move(y), yRB );
995 result->addCpuPlane(std::move(u), uvRB);
996 result->addCpuPlane(std::move(v), uvRB);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400997 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400998 return;
999 }
1000
1001 struct FinishContext {
1002 ReadPixelsCallback* fClientCallback;
1003 ReadPixelsContext fClientContext;
1004 GrClientMappedBufferManager* fMappedBufferManager;
1005 SkISize fSize;
1006 PixelTransferResult fYTransfer;
1007 PixelTransferResult fUTransfer;
1008 PixelTransferResult fVTransfer;
1009 };
1010 // Assumption is that the caller would like to flush. We could take a parameter or require an
1011 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1012 // callback to GrGpu until after the next flush that flushes our op list, though.
1013 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -04001014 callbackContext,
1015 dContext->priv().clientMappedBufferManager(),
Brian Salomon63a0a752020-06-26 13:32:09 -04001016 dstSize,
1017 std::move(yTransfer),
1018 std::move(uTransfer),
1019 std::move(vTransfer)};
1020 auto finishCallback = [](GrGpuFinishedContext c) {
1021 const auto* context = reinterpret_cast<const FinishContext*>(c);
1022 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
1023 auto manager = context->fMappedBufferManager;
1024 size_t rowBytes = SkToSizeT(context->fSize.width());
1025 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
1026 (*context->fClientCallback)(context->fClientContext, nullptr);
1027 delete context;
1028 return;
1029 }
1030 rowBytes /= 2;
1031 SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
1032 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
1033 (*context->fClientCallback)(context->fClientContext, nullptr);
1034 delete context;
1035 return;
1036 }
1037 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
1038 (*context->fClientCallback)(context->fClientContext, nullptr);
1039 delete context;
1040 return;
1041 }
1042 (*context->fClientCallback)(context->fClientContext, std::move(result));
1043 delete context;
1044 };
1045 GrFlushInfo flushInfo;
1046 flushInfo.fFinishedContext = finishContext;
1047 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -05001048 dContext->priv().flushSurface(this->asSurfaceProxy(),
1049 SkSurface::BackendSurfaceAccess::kNoAccess,
1050 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -04001051}
1052
Brian Salomonc5243782020-04-02 12:50:34 -04001053bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001054 ASSERT_SINGLE_OWNER
1055 RETURN_FALSE_IF_ABANDONED
1056 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -04001057 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -04001058
Brian Salomon947efe22019-07-16 15:36:11 -04001059 const GrCaps* caps = fContext->priv().caps();
1060
Greg Daniel46cfbc62019-06-07 11:43:30 -04001061 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -05001062 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -04001063
Stephen White3c0a50f2020-01-16 18:19:54 -05001064 if (this->asSurfaceProxy()->framebufferOnly()) {
1065 return false;
1066 }
1067
Chris Daltonf8e5aad2019-08-02 12:55:23 -06001068 if (!caps->canCopySurface(this->asSurfaceProxy(), src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -04001069 return false;
1070 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001071
Greg Daniel16f5c652019-10-29 11:26:01 -04001072 // The swizzle doesn't matter for copies and it is not used.
1073 return this->drawingManager()->newCopyRenderTask(
Brian Salomonc5243782020-04-02 12:50:34 -04001074 GrSurfaceProxyView(sk_ref_sp(src), this->origin(), GrSwizzle("rgba")), srcRect,
Greg Daniel46e366a2019-12-16 14:38:36 -05001075 this->readSurfaceView(), dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001076}
Greg Daniel46cfbc62019-06-07 11:43:30 -04001077
Brian Salomoneebe7352020-12-09 16:37:04 -05001078std::unique_ptr<GrSurfaceDrawContext> GrSurfaceContext::rescale(const GrImageInfo& info,
1079 GrSurfaceOrigin origin,
1080 SkIRect srcRect,
1081 RescaleGamma rescaleGamma,
1082 SkFilterQuality rescaleQuality) {
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001083 // We rescale by drawing and currently only support drawing to premul.
Brian Salomon1c86b632020-12-11 12:36:01 -05001084 if (info.alphaType() != kPremul_SkAlphaType) {
1085 return nullptr;
1086 }
1087 auto sdc = GrSurfaceDrawContext::MakeWithFallback(fContext,
1088 info.colorType(),
1089 info.refColorSpace(),
1090 SkBackingFit::kExact,
1091 info.dimensions(),
1092 1,
1093 GrMipmapped::kNo,
1094 this->asSurfaceProxy()->isProtected(),
1095 origin);
1096 if (!sdc || !this->rescaleInto(sdc.get(),
1097 SkIRect::MakeSize(sdc->dimensions()),
1098 srcRect,
1099 rescaleGamma,
1100 rescaleQuality)) {
1101 return nullptr;
1102 }
1103 return sdc;
1104}
1105
1106bool GrSurfaceContext::rescaleInto(GrSurfaceDrawContext* dst,
1107 SkIRect dstRect,
1108 SkIRect srcRect,
1109 RescaleGamma rescaleGamma,
1110 SkFilterQuality rescaleQuality) {
1111 SkASSERT(dst);
1112 if (!SkIRect::MakeSize(dst->dimensions()).contains((dstRect))) {
1113 return false;
1114 }
1115
Brian Salomone9ad9982019-07-22 16:17:41 -04001116 auto rtProxy = this->asRenderTargetProxy();
1117 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001118 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001119 }
1120
Stephen White3c0a50f2020-01-16 18:19:54 -05001121 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001122 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001123 }
1124
Greg Daniel40903af2020-01-30 14:55:05 -05001125 GrSurfaceProxyView texView = this->readSurfaceView();
Brian Salomonfc118442019-11-22 19:09:27 -05001126 SkAlphaType srcAlphaType = this->colorInfo().alphaType();
Greg Daniel40903af2020-01-30 14:55:05 -05001127 if (!texView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -04001128 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect,
Brian Salomonc5243782020-04-02 12:50:34 -04001129 SkBackingFit::kApprox, SkBudgeted::kNo);
1130 if (!texView) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001131 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001132 }
Greg Daniel40903af2020-01-30 14:55:05 -05001133 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001134 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001135 }
1136
Brian Salomon1c86b632020-12-11 12:36:01 -05001137 SkISize finalSize = dstRect.size();
1138
Brian Salomonbf6b9792019-08-21 09:38:10 -04001139 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
1140 // pass B is moved to A. If 'this' is the input on the first pass then tempA is null.
Brian Salomoneebe7352020-12-09 16:37:04 -05001141 std::unique_ptr<GrSurfaceDrawContext> tempA;
1142 std::unique_ptr<GrSurfaceDrawContext> tempB;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001143
Brian Salomone9ad9982019-07-22 16:17:41 -04001144 // Assume we should ignore the rescale linear request if the surface has no color space since
1145 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon63a0a752020-06-26 13:32:09 -04001146 if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() &&
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001147 !this->colorInfo().colorSpace()->gammaIsLinear()) {
1148 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomonfc118442019-11-22 19:09:27 -05001149 auto xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(), srcAlphaType, cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -04001150 kPremul_SkAlphaType);
1151 // We'll fall back to kRGBA_8888 if half float not supported.
Brian Salomoneebe7352020-12-09 16:37:04 -05001152 auto linearRTC = GrSurfaceDrawContext::MakeWithFallback(
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001153 fContext, GrColorType::kRGBA_F16, cs, SkBackingFit::kApprox, srcRect.size(), 1,
Brian Salomon1c86b632020-12-11 12:36:01 -05001154 GrMipmapped::kNo, GrProtected::kNo, dst->origin());
Brian Salomone9ad9982019-07-22 16:17:41 -04001155 if (!linearRTC) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001156 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001157 }
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001158 // 1-to-1 draw can always be kFast.
Brian Salomone69b9ef2020-07-22 11:18:06 -04001159 linearRTC->drawTexture(nullptr,
1160 std::move(texView),
1161 srcAlphaType,
1162 GrSamplerState::Filter::kNearest,
1163 GrSamplerState::MipmapMode::kNone,
1164 SkBlendMode::kSrc,
1165 SK_PMColor4fWHITE,
1166 SkRect::Make(srcRect),
1167 SkRect::Make(srcRect.size()),
1168 GrAA::kNo,
1169 GrQuadAAFlags::kNone,
1170 SkCanvas::kFast_SrcRectConstraint,
1171 SkMatrix::I(),
1172 std::move(xform));
Greg Daniel40903af2020-01-30 14:55:05 -05001173 texView = linearRTC->readSurfaceView();
1174 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -04001175 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001176 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001177 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001178
Brian Salomon1c86b632020-12-11 12:36:01 -05001179 while (srcRect.size() != finalSize) {
1180 SkISize nextDims = finalSize;
Brian Salomon59f31b12020-06-04 17:27:15 -04001181 if (rescaleQuality != kNone_SkFilterQuality) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001182 if (srcRect.width() > finalSize.width()) {
1183 nextDims.fWidth = std::max((srcRect.width() + 1)/2, finalSize.width());
1184 } else if (srcRect.width() < finalSize.width()) {
1185 nextDims.fWidth = std::min(srcRect.width()*2, finalSize.width());
Brian Salomon59f31b12020-06-04 17:27:15 -04001186 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001187 if (srcRect.height() > finalSize.height()) {
1188 nextDims.fHeight = std::max((srcRect.height() + 1)/2, finalSize.height());
1189 } else if (srcRect.height() < finalSize.height()) {
1190 nextDims.fHeight = std::min(srcRect.height()*2, finalSize.height());
Brian Salomon59f31b12020-06-04 17:27:15 -04001191 }
1192 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001193 auto input = tempA ? tempA.get() : this;
Brian Salomone9ad9982019-07-22 16:17:41 -04001194 sk_sp<GrColorSpaceXform> xform;
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001195 GrSurfaceDrawContext* stepDst;
1196 SkIRect stepDstRect;
Brian Salomon1c86b632020-12-11 12:36:01 -05001197 if (nextDims == finalSize) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001198 // Might as well fold conversion to final info in the last step.
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001199 xform = GrColorSpaceXform::Make(input->colorInfo().colorSpace(),
Brian Salomon1c86b632020-12-11 12:36:01 -05001200 input->colorInfo().alphaType(),
1201 dst->colorInfo().colorSpace(),
1202 dst->colorInfo().alphaType());
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001203 stepDst = dst;
1204 stepDstRect = dstRect;
Brian Salomon1c86b632020-12-11 12:36:01 -05001205 } else {
1206 tempB = GrSurfaceDrawContext::MakeWithFallback(fContext,
1207 input->colorInfo().colorType(),
1208 input->colorInfo().refColorSpace(),
1209 SkBackingFit::kApprox,
1210 nextDims,
1211 1,
1212 GrMipmapped::kNo,
1213 GrProtected::kNo,
1214 dst->origin());
1215 if (!tempB) {
1216 return false;
1217 }
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001218 stepDst = tempB.get();
1219 stepDstRect = SkIRect::MakeSize(tempB->dimensions());
Brian Salomone9ad9982019-07-22 16:17:41 -04001220 }
Brian Salomon59f31b12020-06-04 17:27:15 -04001221 if (rescaleQuality == kHigh_SkFilterQuality) {
1222 SkMatrix matrix;
1223 matrix.setScaleTranslate((float)srcRect.width()/nextDims.width(),
1224 (float)srcRect.height()/nextDims.height(),
1225 srcRect.x(),
1226 srcRect.y());
1227 std::unique_ptr<GrFragmentProcessor> fp;
1228 auto dir = GrBicubicEffect::Direction::kXY;
1229 if (nextDims.width() == srcRect.width()) {
1230 dir = GrBicubicEffect::Direction::kY;
1231 } else if (nextDims.height() == srcRect.height()) {
1232 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -04001233 }
Brian Salomon1af72d12020-06-25 10:47:26 -04001234 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
Mike Reed3867c702020-09-01 13:28:10 -04001235 static constexpr auto kKernel = GrBicubicEffect::gCatmullRom;
Brian Salomon1c86b632020-12-11 12:36:01 -05001236 fp = GrBicubicEffect::MakeSubset(std::move(texView),
1237 input->colorInfo().alphaType(),
1238 matrix,
1239 kWM,
1240 kWM,
1241 SkRect::Make(srcRect),
1242 kKernel,
1243 dir,
1244 *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -04001245 if (xform) {
1246 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -04001247 }
Brian Salomon59f31b12020-06-04 17:27:15 -04001248 GrPaint paint;
John Stiles5933d7d2020-07-21 12:28:35 -04001249 paint.setColorFragmentProcessor(std::move(fp));
Brian Salomon59f31b12020-06-04 17:27:15 -04001250 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001251 stepDst->fillRectToRect(nullptr,
1252 std::move(paint),
1253 GrAA::kNo,
1254 SkMatrix::I(),
1255 SkRect::Make(stepDstRect),
1256 SkRect::Make(stepDstRect));
Brian Salomon59f31b12020-06-04 17:27:15 -04001257 } else {
1258 auto filter = rescaleQuality == kNone_SkFilterQuality ? GrSamplerState::Filter::kNearest
Brian Salomona3b02f52020-07-15 16:02:01 -04001259 : GrSamplerState::Filter::kLinear;
Brian Salomon59f31b12020-06-04 17:27:15 -04001260 // Minimizing draw with integer coord src and dev rects can always be kFast.
1261 auto constraint = SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint;
1262 if (nextDims.width() <= srcRect.width() && nextDims.height() <= srcRect.height()) {
1263 constraint = SkCanvas::SrcRectConstraint::kFast_SrcRectConstraint;
1264 }
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001265 stepDst->drawTexture(nullptr,
1266 std::move(texView),
1267 srcAlphaType,
1268 filter,
1269 GrSamplerState::MipmapMode::kNone,
1270 SkBlendMode::kSrc,
1271 SK_PMColor4fWHITE,
1272 SkRect::Make(srcRect),
1273 SkRect::Make(stepDstRect),
1274 GrAA::kNo,
1275 GrQuadAAFlags::kNone,
1276 constraint,
1277 SkMatrix::I(),
1278 std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -04001279 }
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001280 texView = stepDst->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -04001281 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001282 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomone9ad9982019-07-22 16:17:41 -04001283 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001284 return true;
Brian Salomone9ad9982019-07-22 16:17:41 -04001285}
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001286
1287GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
1288 const SkIRect& rect) {
1289 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1290 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001291 auto direct = fContext->asDirectContext();
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001292 if (!direct) {
1293 return {};
1294 }
1295 auto rtProxy = this->asRenderTargetProxy();
1296 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1297 return {};
1298 }
1299
1300 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001301 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
1302 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001303 // Fail if read color type does not have all of dstCT's color channels and those missing color
1304 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001305 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1306 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
1307 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1308 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001309 return {};
1310 }
1311
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001312 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001313 !supportedRead.fOffsetAlignmentForTransferBuffer) {
1314 return {};
1315 }
1316
1317 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
1318 size_t size = rowBytes * rect.height();
1319 auto buffer = direct->priv().resourceProvider()->createBuffer(
1320 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
1321 if (!buffer) {
1322 return {};
1323 }
1324 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -05001325 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001326 if (flip) {
1327 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1328 this->height() - rect.fTop);
1329 }
Greg Danielbbfec9d2019-08-20 10:56:51 -04001330 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001331 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -04001332 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001333 PixelTransferResult result;
1334 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001335 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -04001336 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001337 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
1338 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001339 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
1340 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001341 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
1342 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -04001343 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001344 };
1345 }
1346 return result;
1347}
Greg Daniel46e366a2019-12-16 14:38:36 -05001348
1349#ifdef SK_DEBUG
1350void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001351 SkASSERT(fReadView.proxy());
1352 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -04001353 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
1354 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
1355 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
1356 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001357 this->onValidate();
1358}
1359#endif