blob: 1eb518665ec25efe110bc7520c2ef92c878ba8b5 [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"
Brian Salomoneebe7352020-12-09 16:37:04 -050024#include "src/gpu/GrSurfaceDrawContext.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,
Brian Salomon14f99fc2020-12-07 12:19:47 -050034 const GrColorInfo& info) {
Greg Daniele20fcad2020-01-08 11:52:34 -050035 // It is probably not necessary to check if the context is abandoned here since uses of the
36 // GrSurfaceContext which need the context will mostly likely fail later on without an issue.
37 // However having this hear adds some reassurance in case there is a path doesn't handle an
38 // abandoned context correctly. It also lets us early out of some extra work.
Robert Phillips9eb00022020-06-30 15:30:12 -040039 if (context->abandoned()) {
Greg Daniele20fcad2020-01-08 11:52:34 -050040 return nullptr;
41 }
Greg Daniel3912a4b2020-01-14 09:56:04 -050042 GrSurfaceProxy* proxy = readView.proxy();
Greg Danielbfa19c42019-12-19 16:41:40 -050043 SkASSERT(proxy && proxy->asTextureProxy());
44
Greg Danielbfa19c42019-12-19 16:41:40 -050045 std::unique_ptr<GrSurfaceContext> surfaceContext;
Greg Daniel3912a4b2020-01-14 09:56:04 -050046 if (proxy->asRenderTargetProxy()) {
Brian Salomon14f99fc2020-12-07 12:19:47 -050047 SkASSERT(info.alphaType() == kPremul_SkAlphaType ||
48 info.alphaType() == kOpaque_SkAlphaType);
Brian Salomon8afde5f2020-04-01 16:22:00 -040049 // Will we ever want a swizzle that is not the default write swizzle for the format and
Greg Danielbfa19c42019-12-19 16:41:40 -050050 // colorType here? If so we will need to manually pass that in.
Brian Salomonc5243782020-04-02 12:50:34 -040051 GrSwizzle writeSwizzle;
Brian Salomon14f99fc2020-12-07 12:19:47 -050052 if (info.colorType() != GrColorType::kUnknown) {
53 writeSwizzle = context->priv().caps()->getWriteSwizzle(proxy->backendFormat(),
54 info.colorType());
Brian Salomonc5243782020-04-02 12:50:34 -040055 }
Brian Salomon8afde5f2020-04-01 16:22:00 -040056 GrSurfaceProxyView writeView(readView.refProxy(), readView.origin(), writeSwizzle);
Brian Salomoneebe7352020-12-09 16:37:04 -050057 surfaceContext = std::make_unique<GrSurfaceDrawContext>(context,
58 std::move(readView),
59 std::move(writeView),
60 info.colorType(),
61 info.refColorSpace(),
62 /*surface props*/ nullptr);
Greg Danielbfa19c42019-12-19 16:41:40 -050063 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -050064 surfaceContext = std::make_unique<GrSurfaceContext>(context, std::move(readView), info);
Greg Danielbfa19c42019-12-19 16:41:40 -050065 }
Robert Phillips07f0e412020-01-17 15:20:00 -050066 SkDEBUGCODE(surfaceContext->validate();)
Greg Danielbfa19c42019-12-19 16:41:40 -050067 return surfaceContext;
68}
69
Brian Salomona56a7462020-02-07 14:17:25 -050070std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
Brian Salomon14f99fc2020-12-07 12:19:47 -050071 const GrImageInfo& info,
Brian Salomona56a7462020-02-07 14:17:25 -050072 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050073 SkBackingFit fit,
Brian Salomon14f99fc2020-12-07 12:19:47 -050074 GrSurfaceOrigin origin,
75 GrRenderable renderable,
76 int sampleCount,
77 GrMipmapped mipmapped,
78 GrProtected isProtected,
Brian Salomona56a7462020-02-07 14:17:25 -050079 SkBudgeted budgeted) {
Brian Salomon14f99fc2020-12-07 12:19:47 -050080 SkASSERT(context);
81 SkASSERT(renderable == GrRenderable::kYes || sampleCount == 1);
82 if (context->abandoned()) {
83 return nullptr;
Brian Salomond005b692020-04-01 15:47:05 -040084 }
Brian Salomon14f99fc2020-12-07 12:19:47 -050085 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(format,
86 info.dimensions(),
87 renderable,
88 sampleCount,
89 mipmapped,
90 fit,
91 budgeted,
92 isProtected);
Greg Danielbfa19c42019-12-19 16:41:40 -050093 if (!proxy) {
94 return nullptr;
95 }
96
Brian Salomon14f99fc2020-12-07 12:19:47 -050097 GrSwizzle swizzle;
98 if (info.colorType() != GrColorType::kUnknown &&
99 !context->priv().caps()->isFormatCompressed(format)) {
100 swizzle = context->priv().caps()->getReadSwizzle(format, info.colorType());
101 }
102
Greg Daniel3912a4b2020-01-14 09:56:04 -0500103 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500104 return GrSurfaceContext::Make(context, std::move(view), info.colorInfo());
105}
106
107std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
108 const GrImageInfo& info,
109 SkBackingFit fit,
110 GrSurfaceOrigin origin,
111 GrRenderable renderable,
112 int sampleCount,
113 GrMipmapped mipmapped,
114 GrProtected isProtected,
115 SkBudgeted budgeted) {
116 GrBackendFormat format = context->priv().caps()->getDefaultBackendFormat(info.colorType(),
117 renderable);
118 return Make(context,
119 info,
120 format,
121 fit,
122 origin,
123 renderable,
124 sampleCount,
125 mipmapped,
126 isProtected,
127 budgeted);
Greg Danielbfa19c42019-12-19 16:41:40 -0500128}
129
Greg Danielf41b2bd2019-08-22 16:19:24 -0400130// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
131// GrOpsTasks to be picked up and added to by renderTargetContexts lower in the call
132// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
133// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500134GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500135 GrSurfaceProxyView readView,
Brian Salomon14f99fc2020-12-07 12:19:47 -0500136 const GrColorInfo& info)
137 : fContext(context), fReadView(std::move(readView)), fColorInfo(info) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400138 SkASSERT(!context->abandoned());
Greg Daniele20fcad2020-01-08 11:52:34 -0500139}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400140
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400141const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); }
142
Robert Phillips0d075de2019-03-04 11:08:13 -0500143GrAuditTrail* GrSurfaceContext::auditTrail() {
144 return fContext->priv().auditTrail();
145}
146
147GrDrawingManager* GrSurfaceContext::drawingManager() {
148 return fContext->priv().drawingManager();
149}
150
151const GrDrawingManager* GrSurfaceContext::drawingManager() const {
152 return fContext->priv().drawingManager();
153}
154
155#ifdef SK_DEBUG
Brian Salomon70fe17e2020-11-30 14:33:58 -0500156GrSingleOwner* GrSurfaceContext::singleOwner() const { return fContext->priv().singleOwner(); }
Robert Phillips0d075de2019-03-04 11:08:13 -0500157#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400158
Adlai Hollerc95b5892020-08-11 12:02:22 -0400159bool GrSurfaceContext::readPixels(GrDirectContext* dContext, const GrImageInfo& origDstInfo,
160 void* dst, size_t rowBytes, SkIPoint pt) {
Brian Salomon1d435302019-07-01 13:05:28 -0400161 ASSERT_SINGLE_OWNER
162 RETURN_FALSE_IF_ABANDONED
163 SkDEBUGCODE(this->validate();)
164 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400165 if (!fContext->priv().matches(dContext)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400166 return false;
167 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400168
Brian Salomon1d435302019-07-01 13:05:28 -0400169 if (!dst) {
170 return false;
171 }
172
Brian Salomon1047a492019-07-02 12:25:21 -0400173 size_t tightRowBytes = origDstInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400174 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400175 rowBytes = tightRowBytes;
176 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400177 return false;
178 }
179
180 if (!origDstInfo.isValid()) {
181 return false;
182 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400183
184 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
185
Stephen White3c0a50f2020-01-16 18:19:54 -0500186 if (srcProxy->framebufferOnly()) {
187 return false;
188 }
189
Greg Daniel6eb8c242019-06-05 10:22:24 -0400190 // MDB TODO: delay this instantiation until later in the method
Adlai Hollerc95b5892020-08-11 12:02:22 -0400191 if (!srcProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400192 return false;
193 }
194
195 GrSurface* srcSurface = srcProxy->peekSurface();
196
Brian Salomon1d435302019-07-01 13:05:28 -0400197 auto dstInfo = origDstInfo;
198 if (!dstInfo.clip(this->width(), this->height(), &pt, &dst, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400199 return false;
200 }
Brian Salomon1047a492019-07-02 12:25:21 -0400201 // Our tight row bytes may have been changed by clipping.
202 tightRowBytes = dstInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400203
Mike Klein7321e6a2019-12-03 11:08:40 -0600204 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{this->colorInfo(), dstInfo}.flags;
205 bool unpremul = flags.unpremul,
206 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
207 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400208
Adlai Hollerc95b5892020-08-11 12:02:22 -0400209 const GrCaps* caps = dContext->priv().caps();
Robert Phillips07f0e412020-01-17 15:20:00 -0500210 bool srcIsCompressed = caps->isFormatCompressed(srcSurface->backendFormat());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400211 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
212 // care so much about getImageData performance. However, in order to ensure putImageData/
213 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
214 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
215 // fContext->vaildaPMUPMConversionExists()).
Greg Daniel0258c902019-08-01 13:08:33 -0400216 GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
217 GrRenderable::kYes);
Greg Danielc71c7962020-01-14 16:44:18 -0500218 GrColorType srcColorType = this->colorInfo().colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400219 bool canvas2DFastPath = unpremul && !needColorConversion &&
220 (GrColorType::kRGBA_8888 == dstInfo.colorType() ||
221 GrColorType::kBGRA_8888 == dstInfo.colorType()) &&
222 SkToBool(srcProxy->asTextureProxy()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500223 (srcColorType == GrColorType::kRGBA_8888 ||
224 srcColorType == GrColorType::kBGRA_8888) &&
Greg Daniel0258c902019-08-01 13:08:33 -0400225 defaultRGBAFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400226 dContext->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400227
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400228 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400229 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400230 return false;
231 }
232
Brian Salomondc0710f2019-07-01 14:59:32 -0400233 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400234 std::unique_ptr<GrSurfaceContext> tempCtx;
235 if (this->asTextureProxy()) {
236 GrColorType colorType = (canvas2DFastPath || srcIsCompressed)
237 ? GrColorType::kRGBA_8888
238 : this->colorInfo().colorType();
Brian Salomoneebe7352020-12-09 16:37:04 -0500239 tempCtx = GrSurfaceDrawContext::Make(
Christopher Cameron75ae8fc2020-11-17 22:03:49 -0800240 dContext, colorType, this->colorInfo().refColorSpace(), SkBackingFit::kApprox,
241 dstInfo.dimensions(), 1, GrMipMapped::kNo, GrProtected::kNo,
242 kTopLeft_GrSurfaceOrigin);
Brian Salomon72c7b982020-10-06 10:07:38 -0400243 if (!tempCtx) {
244 return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400245 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400246
247 std::unique_ptr<GrFragmentProcessor> fp;
248 if (canvas2DFastPath) {
249 fp = dContext->priv().createPMToUPMEffect(GrTextureEffect::Make(
250 this->readSurfaceView(), this->colorInfo().alphaType()));
251 if (dstInfo.colorType() == GrColorType::kBGRA_8888) {
252 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
253 dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888);
254 }
255 // The render target context is incorrectly tagged as kPremul even though we're
256 // writing unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type
257 // so we don't double unpremul.
258 dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType);
259 } else {
260 fp = GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType());
261 }
262 if (!fp) {
263 return false;
264 }
265 GrPaint paint;
266 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
267 paint.setColorFragmentProcessor(std::move(fp));
268
269 tempCtx->asRenderTargetContext()->fillRectToRect(
270 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
271 SkRect::MakeWH(dstInfo.width(), dstInfo.height()),
272 SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height()));
273 pt = {0, 0};
Greg Daniel6eb8c242019-06-05 10:22:24 -0400274 } else {
Brian Salomon72c7b982020-10-06 10:07:38 -0400275 auto restrictions = this->caps()->getDstCopyRestrictions(this->asRenderTargetProxy(),
276 this->colorInfo().colorType());
277 sk_sp<GrSurfaceProxy> copy;
278 static constexpr auto kFit = SkBackingFit::kExact;
279 static constexpr auto kBudgeted = SkBudgeted::kYes;
280 static constexpr auto kMipMapped = GrMipMapped::kNo;
281 if (restrictions.fMustCopyWholeSrc) {
282 copy = GrSurfaceProxy::Copy(fContext, srcProxy, this->origin(), kMipMapped, kFit,
283 kBudgeted);
284 } else {
285 auto srcRect = SkIRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height());
286 copy = GrSurfaceProxy::Copy(fContext, srcProxy, this->origin(), kMipMapped, srcRect,
287 kFit, kBudgeted, restrictions.fRectsMustMatch);
288 pt = {0, 0};
289 }
290 if (!copy) {
291 return false;
292 }
293 GrSurfaceProxyView view{std::move(copy), this->origin(), this->readSwizzle()};
Brian Salomon14f99fc2020-12-07 12:19:47 -0500294 tempCtx = GrSurfaceContext::Make(dContext, std::move(view), this->colorInfo());
Brian Salomon72c7b982020-10-06 10:07:38 -0400295 SkASSERT(tempCtx);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400296 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400297 return tempCtx->readPixels(dContext, dstInfo, dst, rowBytes, pt);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400298 }
299
Greg Danielb8d84f82020-02-13 14:25:00 -0500300 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400301
Brian Salomon1d435302019-07-01 13:05:28 -0400302 auto supportedRead = caps->supportedReadPixelsColorType(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400303 this->colorInfo().colorType(), srcProxy->backendFormat(), dstInfo.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400304
Brian Salomon1047a492019-07-02 12:25:21 -0400305 bool makeTight = !caps->readPixelsRowBytesSupport() && tightRowBytes != rowBytes;
306
307 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomon8f8354a2019-07-31 20:12:02 -0400308 (dstInfo.colorType() != supportedRead.fColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400309
Brian Salomonf30b1c12019-06-20 12:25:02 -0400310 std::unique_ptr<char[]> tmpPixels;
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400311 GrImageInfo tmpInfo;
Brian Salomon1d435302019-07-01 13:05:28 -0400312 void* readDst = dst;
313 size_t readRB = rowBytes;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400314 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400315 tmpInfo = {supportedRead.fColorType, this->colorInfo().alphaType(),
316 this->colorInfo().refColorSpace(), dstInfo.width(), dstInfo.height()};
Brian Salomon1d435302019-07-01 13:05:28 -0400317 size_t tmpRB = tmpInfo.minRowBytes();
318 size_t size = tmpRB * tmpInfo.height();
319 // Chrome MSAN bots require the data to be initialized (hence the ()).
John Stilesfbd050b2020-08-03 13:21:46 -0400320 tmpPixels = std::make_unique<char[]>(size);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400321
Brian Salomonf30b1c12019-06-20 12:25:02 -0400322 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400323 readRB = tmpRB;
324 pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400325 }
326
Adlai Hollerc95b5892020-08-11 12:02:22 -0400327 dContext->priv().flushSurface(srcProxy);
328 dContext->submit();
329 if (!dContext->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(),
330 dstInfo.height(), this->colorInfo().colorType(),
331 supportedRead.fColorType, readDst, readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400332 return false;
333 }
334
Greg Daniel6eb8c242019-06-05 10:22:24 -0400335 if (convert) {
Brian Salomon8f8354a2019-07-31 20:12:02 -0400336 return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400337 }
338 return true;
339}
Robert Phillips0d075de2019-03-04 11:08:13 -0500340
Adlai Hollerc95b5892020-08-11 12:02:22 -0400341bool GrSurfaceContext::writePixels(GrDirectContext* dContext, const GrImageInfo& origSrcInfo,
342 const void* src, size_t rowBytes, SkIPoint pt) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400343 ASSERT_SINGLE_OWNER
344 RETURN_FALSE_IF_ABANDONED
345 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400346 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400347
Adlai Hollerc95b5892020-08-11 12:02:22 -0400348 if (!dContext) {
Brian Salomonc320b152018-02-20 14:05:36 -0500349 return false;
350 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500351
Brian Salomon1d435302019-07-01 13:05:28 -0400352 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500353 return false;
354 }
355
Brian Salomon1d435302019-07-01 13:05:28 -0400356 if (!src) {
357 return false;
358 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400359
Brian Salomon1047a492019-07-02 12:25:21 -0400360 size_t tightRowBytes = origSrcInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400361 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400362 rowBytes = tightRowBytes;
363 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400364 return false;
365 }
366
367 if (!origSrcInfo.isValid()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400368 return false;
369 }
370
371 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
Stephen White3c0a50f2020-01-16 18:19:54 -0500372
373 if (dstProxy->framebufferOnly()) {
374 return false;
375 }
376
Adlai Hollerc95b5892020-08-11 12:02:22 -0400377 if (!dstProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400378 return false;
379 }
380
381 GrSurface* dstSurface = dstProxy->peekSurface();
382
Brian Salomon1d435302019-07-01 13:05:28 -0400383 auto srcInfo = origSrcInfo;
384 if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400385 return false;
386 }
Brian Salomon1047a492019-07-02 12:25:21 -0400387 // Our tight row bytes may have been changed by clipping.
388 tightRowBytes = srcInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400389
Mike Klein7321e6a2019-12-03 11:08:40 -0600390 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{srcInfo, this->colorInfo()}.flags;
391 bool unpremul = flags.unpremul,
392 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
393 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400394
Adlai Hollerc95b5892020-08-11 12:02:22 -0400395 const GrCaps* caps = dContext->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400396
397 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
398 GrRenderable::kNo);
399
Greg Danielc71c7962020-01-14 16:44:18 -0500400 GrColorType dstColorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400401 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
402 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400403 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
404 (srcInfo.colorType() == GrColorType::kRGBA_8888 ||
405 srcInfo.colorType() == GrColorType::kBGRA_8888) &&
406 SkToBool(this->asRenderTargetContext()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500407 (dstColorType == GrColorType::kRGBA_8888 ||
408 dstColorType == GrColorType::kBGRA_8888) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400409 rgbaDefaultFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400410 dContext->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400411
412 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500413 GrColorInfo tempColorInfo;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400414 GrBackendFormat format;
Greg Danielbfa19c42019-12-19 16:41:40 -0500415 GrSwizzle tempReadSwizzle;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400416 if (canvas2DFastPath) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500417 tempColorInfo = {GrColorType::kRGBA_8888,
418 kUnpremul_SkAlphaType,
419 this->colorInfo().refColorSpace()};
Greg Daniel7bfc9132019-08-14 14:23:53 -0400420 format = rgbaDefaultFormat;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400421 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500422 tempColorInfo = this->colorInfo();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400423 format = dstProxy->backendFormat().makeTexture2D();
424 if (!format.isValid()) {
425 return false;
426 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500427 tempReadSwizzle = this->readSwizzle();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400428 }
429
Greg Daniel2e52ad12019-06-13 10:04:16 -0400430 // It is more efficient for us to write pixels into a top left origin so we prefer that.
431 // However, if the final proxy isn't a render target then we must use a copy to move the
432 // data into it which requires the origins to match. If the final proxy is a render target
433 // we can use a draw instead which doesn't have this origin restriction. Thus for render
434 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400435 GrSurfaceOrigin tempOrigin =
Greg Danielb8d84f82020-02-13 14:25:00 -0500436 this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : this->origin();
Adlai Hollerc95b5892020-08-11 12:02:22 -0400437 auto tempProxy = dContext->priv().proxyProvider()->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400438 format, srcInfo.dimensions(), GrRenderable::kNo, 1, GrMipmapped::kNo,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400439 SkBackingFit::kApprox, SkBudgeted::kYes, GrProtected::kNo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400440 if (!tempProxy) {
441 return false;
442 }
Greg Daniel3912a4b2020-01-14 09:56:04 -0500443 GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500444 GrSurfaceContext tempCtx(dContext, tempView, tempColorInfo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400445
446 // In the fast path we always write the srcData to the temp context as though it were RGBA.
447 // When the data is really BGRA the write will cause the R and B channels to be swapped in
448 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
449 // dst.
Brian Salomon1d435302019-07-01 13:05:28 -0400450 if (canvas2DFastPath) {
451 srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888);
452 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400453 if (!tempCtx.writePixels(dContext, srcInfo, src, rowBytes, {0, 0})) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400454 return false;
455 }
456
457 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400458 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400459 if (canvas2DFastPath) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400460 fp = dContext->priv().createUPMToPMEffect(
Brian Salomon14f99fc2020-12-07 12:19:47 -0500461 GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType()));
Brian Salomon1d435302019-07-01 13:05:28 -0400462 // Important: check the original src color type here!
463 if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400464 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
465 }
466 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500467 fp = GrTextureEffect::Make(std::move(tempView), tempColorInfo.alphaType());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400468 }
469 if (!fp) {
470 return false;
471 }
472 GrPaint paint;
473 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
John Stiles5933d7d2020-07-21 12:28:35 -0400474 paint.setColorFragmentProcessor(std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400475 this->asRenderTargetContext()->fillRectToRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400476 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400477 SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()),
478 SkRect::MakeWH(srcInfo.width(), srcInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400479 } else {
Brian Salomon1d435302019-07-01 13:05:28 -0400480 SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height());
481 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Brian Salomonc5243782020-04-02 12:50:34 -0400482 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400483 return false;
484 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400485 }
486 return true;
487 }
488
Brian Salomon1d435302019-07-01 13:05:28 -0400489 GrColorType allowedColorType =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400490 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400491 dstProxy->backendFormat(),
492 srcInfo.colorType()).fColorType;
Greg Danielb8d84f82020-02-13 14:25:00 -0500493 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon1047a492019-07-02 12:25:21 -0400494 bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes;
495 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomon1d435302019-07-01 13:05:28 -0400496 (srcInfo.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400497
Brian Salomonf30b1c12019-06-20 12:25:02 -0400498 std::unique_ptr<char[]> tmpPixels;
Brian Salomon1d435302019-07-01 13:05:28 -0400499 GrColorType srcColorType = srcInfo.colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400500 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400501 GrImageInfo tmpInfo(allowedColorType, this->colorInfo().alphaType(),
502 this->colorInfo().refColorSpace(), srcInfo.width(), srcInfo.height());
Brian Salomon1d435302019-07-01 13:05:28 -0400503 auto tmpRB = tmpInfo.minRowBytes();
504 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400505
Brian Salomon1d435302019-07-01 13:05:28 -0400506 GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400507
Brian Salomon1d435302019-07-01 13:05:28 -0400508 srcColorType = tmpInfo.colorType();
509 rowBytes = tmpRB;
510 src = tmpPixels.get();
511 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400512 }
513
514 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
515 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
516 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
517 // destination proxy)
518 // TODO: should this policy decision just be moved into the drawing manager?
Adlai Hollerc95b5892020-08-11 12:02:22 -0400519 dContext->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400520
Adlai Hollerc95b5892020-08-11 12:02:22 -0400521 return dContext->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(),
522 srcInfo.height(), this->colorInfo().colorType(),
523 srcColorType, src, rowBytes);
Brian Osman45580d32016-11-23 09:37:01 -0500524}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400525
Adlai Hollerc95b5892020-08-11 12:02:22 -0400526void GrSurfaceContext::asyncRescaleAndReadPixels(GrDirectContext* dContext,
527 const SkImageInfo& info,
Brian Salomon63a0a752020-06-26 13:32:09 -0400528 const SkIRect& srcRect,
529 RescaleGamma rescaleGamma,
530 SkFilterQuality rescaleQuality,
531 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400532 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400533 // We implement this by rendering and we don't currently support rendering kUnpremul.
534 if (info.alphaType() == kUnpremul_SkAlphaType) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400535 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400536 return;
537 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400538 if (!dContext) {
539 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400540 return;
541 }
542 auto rt = this->asRenderTargetProxy();
543 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400544 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400545 return;
546 }
547 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400548 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400549 return;
550 }
551 auto dstCT = SkColorTypeToGrColorType(info.colorType());
552 if (dstCT == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400553 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400554 return;
555 }
556 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
557 auto colorTypeOfFinalContext = this->colorInfo().colorType();
558 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
559 if (needsRescale) {
560 colorTypeOfFinalContext = dstCT;
561 backendFormatOfFinalContext =
562 this->caps()->getDefaultBackendFormat(dstCT, GrRenderable::kYes);
563 }
564 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
565 backendFormatOfFinalContext, dstCT);
566 // Fail if we can't read from the source surface's color type.
567 if (readInfo.fColorType == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400568 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400569 return;
570 }
571 // Fail if read color type does not have all of dstCT's color channels and those missing color
572 // channels are in the src.
573 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
574 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
575 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
576 if ((~legalReadChannels & dstChannels) & srcChannels) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400577 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400578 return;
579 }
580
Brian Salomoneebe7352020-12-09 16:37:04 -0500581 std::unique_ptr<GrSurfaceDrawContext> tempRTC;
Brian Salomon63a0a752020-06-26 13:32:09 -0400582 int x = srcRect.fLeft;
583 int y = srcRect.fTop;
584 if (needsRescale) {
585 tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
586 rescaleQuality);
587 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400588 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400589 return;
590 }
591 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
592 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
593 x = y = 0;
594 } else {
595 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
596 this->colorInfo().alphaType(),
597 info.colorSpace(),
598 info.alphaType());
599 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
600 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
601 GrSurfaceProxyView texProxyView = this->readSurfaceView();
602 SkRect srcRectToDraw = SkRect::Make(srcRect);
603 // If the src is not texturable first try to make a copy to a texture.
604 if (!texProxyView.asTextureProxy()) {
605 texProxyView =
Brian Salomon7e67dca2020-07-21 09:27:25 -0400606 GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipmapped::kNo, srcRect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400607 SkBackingFit::kApprox, SkBudgeted::kNo);
608 if (!texProxyView) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400609 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400610 return;
611 }
612 SkASSERT(texProxyView.asTextureProxy());
613 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
614 }
Brian Salomoneebe7352020-12-09 16:37:04 -0500615 tempRTC = GrSurfaceDrawContext::Make(dContext, this->colorInfo().colorType(),
616 info.refColorSpace(), SkBackingFit::kApprox,
617 srcRect.size(), 1, GrMipmapped::kNo,
618 GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400619 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400620 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400621 return;
622 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400623 tempRTC->drawTexture(nullptr,
624 std::move(texProxyView),
625 this->colorInfo().alphaType(),
626 GrSamplerState::Filter::kNearest,
627 GrSamplerState::MipmapMode::kNone,
628 SkBlendMode::kSrc,
629 SK_PMColor4fWHITE,
630 srcRectToDraw,
631 SkRect::MakeWH(srcRect.width(), srcRect.height()),
632 GrAA::kNo,
633 GrQuadAAFlags::kNone,
634 SkCanvas::kFast_SrcRectConstraint,
635 SkMatrix::I(),
636 std::move(xform));
Brian Salomon63a0a752020-06-26 13:32:09 -0400637 x = y = 0;
638 }
639 }
640 auto rtc = tempRTC ? tempRTC.get() : this;
Adlai Hollerc95b5892020-08-11 12:02:22 -0400641 return rtc->asyncReadPixels(dContext, SkIRect::MakeXYWH(x, y, info.width(), info.height()),
642 info.colorType(), callback, callbackContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400643}
644
645class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult {
646public:
647 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
648 ~AsyncReadResult() override {
649 for (int i = 0; i < fPlanes.count(); ++i) {
650 if (!fPlanes[i].fMappedBuffer) {
651 delete[] static_cast<const char*>(fPlanes[i].fData);
652 } else {
653 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
654 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
655 }
656 }
657 }
658
659 int count() const override { return fPlanes.count(); }
660 const void* data(int i) const override { return fPlanes[i].fData; }
661 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
662
663 bool addTransferResult(const PixelTransferResult& result,
664 SkISize dimensions,
665 size_t rowBytes,
666 GrClientMappedBufferManager* manager) {
667 SkASSERT(!result.fTransferBuffer->isMapped());
668 const void* mappedData = result.fTransferBuffer->map();
669 if (!mappedData) {
670 return false;
671 }
672 if (result.fPixelConverter) {
673 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
674 result.fPixelConverter(convertedData.get(), mappedData);
675 this->addCpuPlane(std::move(convertedData), rowBytes);
676 result.fTransferBuffer->unmap();
677 } else {
678 manager->insert(result.fTransferBuffer);
679 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
680 }
681 return true;
682 }
683
684 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
685 SkASSERT(data);
686 SkASSERT(rowBytes > 0);
687 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
688 }
689
690private:
691 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
692 SkASSERT(data);
693 SkASSERT(rowBytes > 0);
694 SkASSERT(mappedBuffer);
695 SkASSERT(mappedBuffer->isMapped());
696 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
697 }
698
699 struct Plane {
700 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
701 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
702 const void* fData;
703 size_t fRowBytes;
704 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
705 sk_sp<GrGpuBuffer> fMappedBuffer;
706 };
707 SkSTArray<3, Plane> fPlanes;
708 uint32_t fInboxID;
709};
710
Adlai Hollerc95b5892020-08-11 12:02:22 -0400711void GrSurfaceContext::asyncReadPixels(GrDirectContext* dContext,
712 const SkIRect& rect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400713 SkColorType colorType,
714 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400715 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400716 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
717 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
718
Adlai Hollerc95b5892020-08-11 12:02:22 -0400719 if (!dContext || this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
720 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400721 return;
722 }
723
Adlai Hollerc95b5892020-08-11 12:02:22 -0400724 auto mappedBufferManager = dContext->priv().clientMappedBufferManager();
Brian Salomon63a0a752020-06-26 13:32:09 -0400725
726 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
727
728 if (!transferResult.fTransferBuffer) {
729 auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
730 this->colorInfo().refColorSpace());
731 auto result = std::make_unique<AsyncReadResult>(0);
732 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
733 SkPixmap pm(ii, data.get(), ii.minRowBytes());
734 result->addCpuPlane(std::move(data), pm.rowBytes());
735
Adlai Hollerc95b5892020-08-11 12:02:22 -0400736 SkIPoint pt{rect.fLeft, rect.fTop};
737 if (!this->readPixels(dContext, ii, pm.writable_addr(), pm.rowBytes(), pt)) {
738 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400739 return;
740 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400741 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400742 return;
743 }
744
745 struct FinishContext {
746 ReadPixelsCallback* fClientCallback;
747 ReadPixelsContext fClientContext;
748 SkISize fSize;
749 SkColorType fColorType;
750 GrClientMappedBufferManager* fMappedBufferManager;
751 PixelTransferResult fTransferResult;
752 };
753 // Assumption is that the caller would like to flush. We could take a parameter or require an
754 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
755 // callback to GrGpu until after the next flush that flushes our op list, though.
756 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400757 callbackContext,
Brian Salomon63a0a752020-06-26 13:32:09 -0400758 rect.size(),
759 colorType,
760 mappedBufferManager,
761 std::move(transferResult)};
762 auto finishCallback = [](GrGpuFinishedContext c) {
763 const auto* context = reinterpret_cast<const FinishContext*>(c);
764 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
765 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
766 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
767 context->fMappedBufferManager)) {
768 result.reset();
769 }
770 (*context->fClientCallback)(context->fClientContext, std::move(result));
771 delete context;
772 };
773 GrFlushInfo flushInfo;
774 flushInfo.fFinishedContext = finishContext;
775 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -0500776
777 dContext->priv().flushSurface(this->asSurfaceProxy(),
778 SkSurface::BackendSurfaceAccess::kNoAccess,
779 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400780}
781
Adlai Hollerc95b5892020-08-11 12:02:22 -0400782void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(GrDirectContext* dContext,
783 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400784 sk_sp<SkColorSpace> dstColorSpace,
785 const SkIRect& srcRect,
786 SkISize dstSize,
787 RescaleGamma rescaleGamma,
788 SkFilterQuality rescaleQuality,
789 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400790 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400791 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
792 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
793 SkASSERT(!dstSize.isZero());
794 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
795
Adlai Hollerc95b5892020-08-11 12:02:22 -0400796 if (!dContext) {
797 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400798 return;
799 }
800 auto rt = this->asRenderTargetProxy();
801 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400802 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400803 return;
804 }
805 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400806 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400807 return;
808 }
809 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400810 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400811 return;
812 }
813 int x = srcRect.fLeft;
814 int y = srcRect.fTop;
815 bool needsRescale = srcRect.size() != dstSize;
816 GrSurfaceProxyView srcView;
817 if (needsRescale) {
818 // We assume the caller wants kPremul. There is no way to indicate a preference.
819 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
820 dstColorSpace);
821 // TODO: Incorporate the YUV conversion into last pass of rescaling.
822 auto tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
823 rescaleQuality);
824 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400825 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400826 return;
827 }
828 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
829 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
830 x = y = 0;
831 srcView = tempRTC->readSurfaceView();
832 } else {
833 srcView = this->readSurfaceView();
834 if (!srcView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400835 srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipmapped::kNo,
Brian Salomon63a0a752020-06-26 13:32:09 -0400836 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
837 if (!srcView) {
838 // If we can't get a texture copy of the contents then give up.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400839 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400840 return;
841 }
842 SkASSERT(srcView.asTextureProxy());
843 x = y = 0;
844 }
845 // We assume the caller wants kPremul. There is no way to indicate a preference.
846 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
847 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
848 kPremul_SkAlphaType);
849 if (xform) {
850 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
Brian Salomoneebe7352020-12-09 16:37:04 -0500851 auto tempRTC = GrSurfaceDrawContext::Make(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400852 dContext, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400853 dstSize, 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400854 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400855 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400856 return;
857 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400858 tempRTC->drawTexture(nullptr,
859 std::move(srcView),
860 this->colorInfo().alphaType(),
861 GrSamplerState::Filter::kNearest,
862 GrSamplerState::MipmapMode::kNone,
863 SkBlendMode::kSrc,
864 SK_PMColor4fWHITE,
865 srcRectToDraw,
866 SkRect::Make(srcRect.size()),
867 GrAA::kNo,
868 GrQuadAAFlags::kNone,
869 SkCanvas::kFast_SrcRectConstraint,
870 SkMatrix::I(),
871 std::move(xform));
Brian Salomon63a0a752020-06-26 13:32:09 -0400872 srcView = tempRTC->readSurfaceView();
873 SkASSERT(srcView.asTextureProxy());
874 x = y = 0;
875 }
876 }
877
Brian Salomoneebe7352020-12-09 16:37:04 -0500878 auto yRTC = GrSurfaceDrawContext::MakeWithFallback(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400879 dContext, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400880 GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400881 int halfW = dstSize.width() /2;
882 int halfH = dstSize.height()/2;
Brian Salomoneebe7352020-12-09 16:37:04 -0500883 auto uRTC = GrSurfaceDrawContext::MakeWithFallback(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400884 dContext, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH},
885 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomoneebe7352020-12-09 16:37:04 -0500886 auto vRTC = GrSurfaceDrawContext::MakeWithFallback(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400887 dContext, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH},
888 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400889 if (!yRTC || !uRTC || !vRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400890 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400891 return;
892 }
893
894 float baseM[20];
895 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
896
897 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
898
899 auto texMatrix = SkMatrix::Translate(x, y);
900
901 SkRect dstRectY = SkRect::Make(dstSize);
902 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
903
904 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
905 PixelTransferResult yTransfer, uTransfer, vTransfer;
906
907 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
908 float yM[20];
909 std::fill_n(yM, 15, 0.f);
910 std::copy_n(baseM + 0, 5, yM + 15);
911 GrPaint yPaint;
912 auto yTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
913 auto yColFP = GrColorMatrixFragmentProcessor::Make(std::move(yTexFP), yM,
914 /*unpremulInput=*/false,
915 /*clampRGBOutput=*/true,
916 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400917 yPaint.setColorFragmentProcessor(std::move(yColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400918 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
919 yRTC->fillRectToRect(nullptr, std::move(yPaint), GrAA::kNo, SkMatrix::I(), dstRectY, dstRectY);
920 if (!doSynchronousRead) {
921 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
922 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
923 if (!yTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400924 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400925 return;
926 }
927 }
928
929 texMatrix.preScale(2.f, 2.f);
930 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
931 float uM[20];
932 std::fill_n(uM, 15, 0.f);
933 std::copy_n(baseM + 5, 5, uM + 15);
934 GrPaint uPaint;
935 auto uTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix,
Brian Salomona3b02f52020-07-15 16:02:01 -0400936 GrSamplerState::Filter::kLinear);
Brian Salomon63a0a752020-06-26 13:32:09 -0400937 auto uColFP = GrColorMatrixFragmentProcessor::Make(std::move(uTexFP), uM,
938 /*unpremulInput=*/false,
939 /*clampRGBOutput=*/true,
940 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400941 uPaint.setColorFragmentProcessor(std::move(uColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400942 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
943 uRTC->fillRectToRect(nullptr, std::move(uPaint), GrAA::kNo, SkMatrix::I(), dstRectUV,
944 dstRectUV);
945 if (!doSynchronousRead) {
946 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
947 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
948 if (!uTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400949 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400950 return;
951 }
952 }
953
954 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
955 float vM[20];
956 std::fill_n(vM, 15, 0.f);
957 std::copy_n(baseM + 10, 5, vM + 15);
958 GrPaint vPaint;
959 auto vTexFP = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType(),
Brian Salomona3b02f52020-07-15 16:02:01 -0400960 texMatrix, GrSamplerState::Filter::kLinear);
Brian Salomon63a0a752020-06-26 13:32:09 -0400961 auto vColFP = GrColorMatrixFragmentProcessor::Make(std::move(vTexFP), vM,
962 /*unpremulInput=*/false,
963 /*clampRGBOutput=*/true,
964 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400965 vPaint.setColorFragmentProcessor(std::move(vColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400966 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
967 vRTC->fillRectToRect(nullptr, std::move(vPaint), GrAA::kNo, SkMatrix::I(), dstRectUV,
968 dstRectUV);
969 if (!doSynchronousRead) {
970 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
971 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
972 if (!vTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400973 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400974 return;
975 }
976 }
977
978 if (doSynchronousRead) {
979 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
980 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
981 size_t yRB = yInfo.minRowBytes();
982 size_t uvRB = uvInfo.minRowBytes();
983 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
984 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
985 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400986 if (!yRTC->readPixels(dContext, yInfo, y.get(), yRB, {0, 0}) ||
987 !uRTC->readPixels(dContext, uvInfo, u.get(), uvRB, {0, 0}) ||
988 !vRTC->readPixels(dContext, uvInfo, v.get(), uvRB, {0, 0})) {
989 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400990 return;
991 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400992 auto result = std::make_unique<AsyncReadResult>(dContext->priv().contextID());
Brian Salomon63a0a752020-06-26 13:32:09 -0400993 result->addCpuPlane(std::move(y), yRB );
994 result->addCpuPlane(std::move(u), uvRB);
995 result->addCpuPlane(std::move(v), uvRB);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400996 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400997 return;
998 }
999
1000 struct FinishContext {
1001 ReadPixelsCallback* fClientCallback;
1002 ReadPixelsContext fClientContext;
1003 GrClientMappedBufferManager* fMappedBufferManager;
1004 SkISize fSize;
1005 PixelTransferResult fYTransfer;
1006 PixelTransferResult fUTransfer;
1007 PixelTransferResult fVTransfer;
1008 };
1009 // Assumption is that the caller would like to flush. We could take a parameter or require an
1010 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1011 // callback to GrGpu until after the next flush that flushes our op list, though.
1012 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -04001013 callbackContext,
1014 dContext->priv().clientMappedBufferManager(),
Brian Salomon63a0a752020-06-26 13:32:09 -04001015 dstSize,
1016 std::move(yTransfer),
1017 std::move(uTransfer),
1018 std::move(vTransfer)};
1019 auto finishCallback = [](GrGpuFinishedContext c) {
1020 const auto* context = reinterpret_cast<const FinishContext*>(c);
1021 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
1022 auto manager = context->fMappedBufferManager;
1023 size_t rowBytes = SkToSizeT(context->fSize.width());
1024 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
1025 (*context->fClientCallback)(context->fClientContext, nullptr);
1026 delete context;
1027 return;
1028 }
1029 rowBytes /= 2;
1030 SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
1031 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
1032 (*context->fClientCallback)(context->fClientContext, nullptr);
1033 delete context;
1034 return;
1035 }
1036 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
1037 (*context->fClientCallback)(context->fClientContext, nullptr);
1038 delete context;
1039 return;
1040 }
1041 (*context->fClientCallback)(context->fClientContext, std::move(result));
1042 delete context;
1043 };
1044 GrFlushInfo flushInfo;
1045 flushInfo.fFinishedContext = finishContext;
1046 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -05001047 dContext->priv().flushSurface(this->asSurfaceProxy(),
1048 SkSurface::BackendSurfaceAccess::kNoAccess,
1049 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -04001050}
1051
Brian Salomonc5243782020-04-02 12:50:34 -04001052bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001053 ASSERT_SINGLE_OWNER
1054 RETURN_FALSE_IF_ABANDONED
1055 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -04001056 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -04001057
Brian Salomon947efe22019-07-16 15:36:11 -04001058 const GrCaps* caps = fContext->priv().caps();
1059
Greg Daniel46cfbc62019-06-07 11:43:30 -04001060 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -05001061 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -04001062
Stephen White3c0a50f2020-01-16 18:19:54 -05001063 if (this->asSurfaceProxy()->framebufferOnly()) {
1064 return false;
1065 }
1066
Chris Daltonf8e5aad2019-08-02 12:55:23 -06001067 if (!caps->canCopySurface(this->asSurfaceProxy(), src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -04001068 return false;
1069 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001070
Greg Daniel16f5c652019-10-29 11:26:01 -04001071 // The swizzle doesn't matter for copies and it is not used.
1072 return this->drawingManager()->newCopyRenderTask(
Brian Salomonc5243782020-04-02 12:50:34 -04001073 GrSurfaceProxyView(sk_ref_sp(src), this->origin(), GrSwizzle("rgba")), srcRect,
Greg Daniel46e366a2019-12-16 14:38:36 -05001074 this->readSurfaceView(), dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001075}
Greg Daniel46cfbc62019-06-07 11:43:30 -04001076
Brian Salomoneebe7352020-12-09 16:37:04 -05001077std::unique_ptr<GrSurfaceDrawContext> GrSurfaceContext::rescale(const GrImageInfo& info,
1078 GrSurfaceOrigin origin,
1079 SkIRect srcRect,
1080 RescaleGamma rescaleGamma,
1081 SkFilterQuality rescaleQuality) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001082 auto rtProxy = this->asRenderTargetProxy();
1083 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1084 return nullptr;
1085 }
1086
Stephen White3c0a50f2020-01-16 18:19:54 -05001087 if (this->asSurfaceProxy()->framebufferOnly()) {
1088 return nullptr;
1089 }
1090
Brian Salomone9ad9982019-07-22 16:17:41 -04001091 // We rescale by drawing and don't currently support drawing to a kUnpremul destination.
1092 if (info.alphaType() == kUnpremul_SkAlphaType) {
1093 return nullptr;
1094 }
1095
Greg Daniel40903af2020-01-30 14:55:05 -05001096 GrSurfaceProxyView texView = this->readSurfaceView();
Brian Salomonfc118442019-11-22 19:09:27 -05001097 SkAlphaType srcAlphaType = this->colorInfo().alphaType();
Greg Daniel40903af2020-01-30 14:55:05 -05001098 if (!texView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -04001099 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect,
Brian Salomonc5243782020-04-02 12:50:34 -04001100 SkBackingFit::kApprox, SkBudgeted::kNo);
1101 if (!texView) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001102 return nullptr;
1103 }
Greg Daniel40903af2020-01-30 14:55:05 -05001104 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001105 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001106 }
1107
Brian Salomonbf6b9792019-08-21 09:38:10 -04001108 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
1109 // 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 -05001110 std::unique_ptr<GrSurfaceDrawContext> tempA;
1111 std::unique_ptr<GrSurfaceDrawContext> tempB;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001112
Brian Salomone9ad9982019-07-22 16:17:41 -04001113 // Assume we should ignore the rescale linear request if the surface has no color space since
1114 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon63a0a752020-06-26 13:32:09 -04001115 if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() &&
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001116 !this->colorInfo().colorSpace()->gammaIsLinear()) {
1117 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomonfc118442019-11-22 19:09:27 -05001118 auto xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(), srcAlphaType, cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -04001119 kPremul_SkAlphaType);
1120 // We'll fall back to kRGBA_8888 if half float not supported.
Brian Salomoneebe7352020-12-09 16:37:04 -05001121 auto linearRTC = GrSurfaceDrawContext::MakeWithFallback(
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001122 fContext, GrColorType::kRGBA_F16, cs, SkBackingFit::kApprox, srcRect.size(), 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001123 GrMipmapped::kNo, GrProtected::kNo, origin);
Brian Salomone9ad9982019-07-22 16:17:41 -04001124 if (!linearRTC) {
1125 return nullptr;
1126 }
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001127 // 1-to-1 draw can always be kFast.
Brian Salomone69b9ef2020-07-22 11:18:06 -04001128 linearRTC->drawTexture(nullptr,
1129 std::move(texView),
1130 srcAlphaType,
1131 GrSamplerState::Filter::kNearest,
1132 GrSamplerState::MipmapMode::kNone,
1133 SkBlendMode::kSrc,
1134 SK_PMColor4fWHITE,
1135 SkRect::Make(srcRect),
1136 SkRect::Make(srcRect.size()),
1137 GrAA::kNo,
1138 GrQuadAAFlags::kNone,
1139 SkCanvas::kFast_SrcRectConstraint,
1140 SkMatrix::I(),
1141 std::move(xform));
Greg Daniel40903af2020-01-30 14:55:05 -05001142 texView = linearRTC->readSurfaceView();
1143 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -04001144 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001145 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001146 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001147
Brian Salomon9c6f6bf2020-05-29 16:37:26 -04001148 while (srcRect.size() != info.dimensions()) {
Brian Salomon59f31b12020-06-04 17:27:15 -04001149 SkISize nextDims = info.dimensions();
1150 if (rescaleQuality != kNone_SkFilterQuality) {
1151 if (srcRect.width() > info.width()) {
1152 nextDims.fWidth = std::max((srcRect.width() + 1)/2, info.width());
1153 } else if (srcRect.width() < info.width()) {
1154 nextDims.fWidth = std::min(srcRect.width()*2, info.width());
1155 }
1156 if (srcRect.height() > info.height()) {
1157 nextDims.fHeight = std::max((srcRect.height() + 1)/2, info.height());
1158 } else if (srcRect.height() < info.height()) {
1159 nextDims.fHeight = std::min(srcRect.height()*2, info.height());
1160 }
1161 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001162 auto input = tempA ? tempA.get() : this;
Brian Salomon59f31b12020-06-04 17:27:15 -04001163 GrColorType colorType = input->colorInfo().colorType();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001164 auto cs = input->colorInfo().refColorSpace();
Brian Salomone9ad9982019-07-22 16:17:41 -04001165 sk_sp<GrColorSpaceXform> xform;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001166 auto prevAlphaType = input->colorInfo().alphaType();
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001167 if (nextDims == info.dimensions()) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001168 // Might as well fold conversion to final info in the last step.
1169 cs = info.refColorSpace();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001170 xform = GrColorSpaceXform::Make(input->colorInfo().colorSpace(),
1171 input->colorInfo().alphaType(), cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -04001172 info.alphaType());
1173 }
Brian Salomoneebe7352020-12-09 16:37:04 -05001174 tempB = GrSurfaceDrawContext::MakeWithFallback(fContext, colorType, std::move(cs),
1175 SkBackingFit::kApprox, nextDims, 1,
1176 GrMipmapped::kNo, GrProtected::kNo, origin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001177 if (!tempB) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001178 return nullptr;
1179 }
Brian Salomon59f31b12020-06-04 17:27:15 -04001180 auto dstRect = SkRect::Make(nextDims);
1181 if (rescaleQuality == kHigh_SkFilterQuality) {
1182 SkMatrix matrix;
1183 matrix.setScaleTranslate((float)srcRect.width()/nextDims.width(),
1184 (float)srcRect.height()/nextDims.height(),
1185 srcRect.x(),
1186 srcRect.y());
1187 std::unique_ptr<GrFragmentProcessor> fp;
1188 auto dir = GrBicubicEffect::Direction::kXY;
1189 if (nextDims.width() == srcRect.width()) {
1190 dir = GrBicubicEffect::Direction::kY;
1191 } else if (nextDims.height() == srcRect.height()) {
1192 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -04001193 }
Brian Salomon1af72d12020-06-25 10:47:26 -04001194 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
Mike Reed3867c702020-09-01 13:28:10 -04001195 static constexpr auto kKernel = GrBicubicEffect::gCatmullRom;
Brian Salomon59f31b12020-06-04 17:27:15 -04001196 fp = GrBicubicEffect::MakeSubset(std::move(texView), prevAlphaType, matrix, kWM, kWM,
Brian Salomon1af72d12020-06-25 10:47:26 -04001197 SkRect::Make(srcRect), kKernel, dir, *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -04001198 if (xform) {
1199 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -04001200 }
Brian Salomon59f31b12020-06-04 17:27:15 -04001201 GrPaint paint;
John Stiles5933d7d2020-07-21 12:28:35 -04001202 paint.setColorFragmentProcessor(std::move(fp));
Brian Salomon59f31b12020-06-04 17:27:15 -04001203 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
1204 tempB->fillRectToRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect,
1205 dstRect);
1206 } else {
1207 auto filter = rescaleQuality == kNone_SkFilterQuality ? GrSamplerState::Filter::kNearest
Brian Salomona3b02f52020-07-15 16:02:01 -04001208 : GrSamplerState::Filter::kLinear;
Brian Salomon59f31b12020-06-04 17:27:15 -04001209 // Minimizing draw with integer coord src and dev rects can always be kFast.
1210 auto constraint = SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint;
1211 if (nextDims.width() <= srcRect.width() && nextDims.height() <= srcRect.height()) {
1212 constraint = SkCanvas::SrcRectConstraint::kFast_SrcRectConstraint;
1213 }
Brian Salomone69b9ef2020-07-22 11:18:06 -04001214 tempB->drawTexture(nullptr,
1215 std::move(texView),
1216 srcAlphaType,
1217 filter,
1218 GrSamplerState::MipmapMode::kNone,
1219 SkBlendMode::kSrc,
1220 SK_PMColor4fWHITE,
1221 SkRect::Make(srcRect),
1222 dstRect,
1223 GrAA::kNo,
1224 GrQuadAAFlags::kNone,
1225 constraint,
1226 SkMatrix::I(),
1227 std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -04001228 }
Greg Daniel40903af2020-01-30 14:55:05 -05001229 texView = tempB->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -04001230 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001231 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomone9ad9982019-07-22 16:17:41 -04001232 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001233 SkASSERT(tempA);
1234 return tempA;
Brian Salomone9ad9982019-07-22 16:17:41 -04001235}
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001236
1237GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
1238 const SkIRect& rect) {
1239 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1240 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001241 auto direct = fContext->asDirectContext();
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001242 if (!direct) {
1243 return {};
1244 }
1245 auto rtProxy = this->asRenderTargetProxy();
1246 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1247 return {};
1248 }
1249
1250 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001251 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
1252 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001253 // Fail if read color type does not have all of dstCT's color channels and those missing color
1254 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001255 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1256 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
1257 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1258 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001259 return {};
1260 }
1261
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001262 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001263 !supportedRead.fOffsetAlignmentForTransferBuffer) {
1264 return {};
1265 }
1266
1267 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
1268 size_t size = rowBytes * rect.height();
1269 auto buffer = direct->priv().resourceProvider()->createBuffer(
1270 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
1271 if (!buffer) {
1272 return {};
1273 }
1274 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -05001275 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001276 if (flip) {
1277 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1278 this->height() - rect.fTop);
1279 }
Greg Danielbbfec9d2019-08-20 10:56:51 -04001280 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001281 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -04001282 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001283 PixelTransferResult result;
1284 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001285 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -04001286 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001287 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
1288 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001289 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
1290 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001291 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
1292 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -04001293 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001294 };
1295 }
1296 return result;
1297}
Greg Daniel46e366a2019-12-16 14:38:36 -05001298
1299#ifdef SK_DEBUG
1300void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001301 SkASSERT(fReadView.proxy());
1302 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -04001303 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
1304 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
1305 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
1306 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001307 this->onValidate();
1308}
1309#endif