blob: b9c944400497b6126137ba25c18e22af33d68102 [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 Salomon8afde5f2020-04-01 16:22:00 -040048 // Will we ever want a swizzle that is not the default write swizzle for the format and
Greg Danielbfa19c42019-12-19 16:41:40 -050049 // colorType here? If so we will need to manually pass that in.
Brian Salomonc5243782020-04-02 12:50:34 -040050 GrSwizzle writeSwizzle;
Brian Salomon14f99fc2020-12-07 12:19:47 -050051 if (info.colorType() != GrColorType::kUnknown) {
52 writeSwizzle = context->priv().caps()->getWriteSwizzle(proxy->backendFormat(),
53 info.colorType());
Brian Salomonc5243782020-04-02 12:50:34 -040054 }
Brian Salomon8afde5f2020-04-01 16:22:00 -040055 GrSurfaceProxyView writeView(readView.refProxy(), readView.origin(), writeSwizzle);
Brian Salomon590f5672020-12-16 11:44:47 -050056 if (info.alphaType() == kPremul_SkAlphaType || info.alphaType() == kOpaque_SkAlphaType) {
57 surfaceContext = std::make_unique<GrSurfaceDrawContext>(context,
58 std::move(readView),
59 std::move(writeView),
60 info.colorType(),
61 info.refColorSpace(),
62 /*surface props*/ nullptr);
63 } else {
64 surfaceContext = std::make_unique<GrSurfaceFillContext>(context,
65 std::move(readView),
66 std::move(writeView),
67 info);
68 }
Greg Danielbfa19c42019-12-19 16:41:40 -050069 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -050070 surfaceContext = std::make_unique<GrSurfaceContext>(context, std::move(readView), info);
Greg Danielbfa19c42019-12-19 16:41:40 -050071 }
Robert Phillips07f0e412020-01-17 15:20:00 -050072 SkDEBUGCODE(surfaceContext->validate();)
Greg Danielbfa19c42019-12-19 16:41:40 -050073 return surfaceContext;
74}
75
Brian Salomona56a7462020-02-07 14:17:25 -050076std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
Brian Salomon14f99fc2020-12-07 12:19:47 -050077 const GrImageInfo& info,
Brian Salomona56a7462020-02-07 14:17:25 -050078 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050079 SkBackingFit fit,
Brian Salomon14f99fc2020-12-07 12:19:47 -050080 GrSurfaceOrigin origin,
81 GrRenderable renderable,
82 int sampleCount,
83 GrMipmapped mipmapped,
84 GrProtected isProtected,
Brian Salomona56a7462020-02-07 14:17:25 -050085 SkBudgeted budgeted) {
Brian Salomon14f99fc2020-12-07 12:19:47 -050086 SkASSERT(context);
87 SkASSERT(renderable == GrRenderable::kYes || sampleCount == 1);
88 if (context->abandoned()) {
89 return nullptr;
Brian Salomond005b692020-04-01 15:47:05 -040090 }
Brian Salomon14f99fc2020-12-07 12:19:47 -050091 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(format,
92 info.dimensions(),
93 renderable,
94 sampleCount,
95 mipmapped,
96 fit,
97 budgeted,
98 isProtected);
Greg Danielbfa19c42019-12-19 16:41:40 -050099 if (!proxy) {
100 return nullptr;
101 }
102
Brian Salomon14f99fc2020-12-07 12:19:47 -0500103 GrSwizzle swizzle;
104 if (info.colorType() != GrColorType::kUnknown &&
105 !context->priv().caps()->isFormatCompressed(format)) {
106 swizzle = context->priv().caps()->getReadSwizzle(format, info.colorType());
107 }
108
Greg Daniel3912a4b2020-01-14 09:56:04 -0500109 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500110 return GrSurfaceContext::Make(context, std::move(view), info.colorInfo());
111}
112
113std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
114 const GrImageInfo& info,
115 SkBackingFit fit,
116 GrSurfaceOrigin origin,
117 GrRenderable renderable,
118 int sampleCount,
119 GrMipmapped mipmapped,
120 GrProtected isProtected,
121 SkBudgeted budgeted) {
122 GrBackendFormat format = context->priv().caps()->getDefaultBackendFormat(info.colorType(),
123 renderable);
124 return Make(context,
125 info,
126 format,
127 fit,
128 origin,
129 renderable,
130 sampleCount,
131 mipmapped,
132 isProtected,
133 budgeted);
Greg Danielbfa19c42019-12-19 16:41:40 -0500134}
135
Greg Danielf41b2bd2019-08-22 16:19:24 -0400136// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
137// GrOpsTasks to be picked up and added to by renderTargetContexts lower in the call
138// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500139// when the surfaceDrawContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500140GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500141 GrSurfaceProxyView readView,
Brian Salomon14f99fc2020-12-07 12:19:47 -0500142 const GrColorInfo& info)
143 : fContext(context), fReadView(std::move(readView)), fColorInfo(info) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400144 SkASSERT(!context->abandoned());
Greg Daniele20fcad2020-01-08 11:52:34 -0500145}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400146
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400147const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); }
148
Robert Phillips0d075de2019-03-04 11:08:13 -0500149GrAuditTrail* GrSurfaceContext::auditTrail() {
150 return fContext->priv().auditTrail();
151}
152
153GrDrawingManager* GrSurfaceContext::drawingManager() {
154 return fContext->priv().drawingManager();
155}
156
157const GrDrawingManager* GrSurfaceContext::drawingManager() const {
158 return fContext->priv().drawingManager();
159}
160
161#ifdef SK_DEBUG
Brian Salomon70fe17e2020-11-30 14:33:58 -0500162GrSingleOwner* GrSurfaceContext::singleOwner() const { return fContext->priv().singleOwner(); }
Robert Phillips0d075de2019-03-04 11:08:13 -0500163#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400164
Adlai Hollerc95b5892020-08-11 12:02:22 -0400165bool GrSurfaceContext::readPixels(GrDirectContext* dContext, const GrImageInfo& origDstInfo,
166 void* dst, size_t rowBytes, SkIPoint pt) {
Brian Salomon1d435302019-07-01 13:05:28 -0400167 ASSERT_SINGLE_OWNER
168 RETURN_FALSE_IF_ABANDONED
169 SkDEBUGCODE(this->validate();)
170 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400171 if (!fContext->priv().matches(dContext)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400172 return false;
173 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400174
Brian Salomon1d435302019-07-01 13:05:28 -0400175 if (!dst) {
176 return false;
177 }
178
Brian Salomon1047a492019-07-02 12:25:21 -0400179 size_t tightRowBytes = origDstInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400180 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400181 rowBytes = tightRowBytes;
182 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400183 return false;
184 }
185
186 if (!origDstInfo.isValid()) {
187 return false;
188 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400189
190 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
191
Stephen White3c0a50f2020-01-16 18:19:54 -0500192 if (srcProxy->framebufferOnly()) {
193 return false;
194 }
195
Greg Daniel6eb8c242019-06-05 10:22:24 -0400196 // MDB TODO: delay this instantiation until later in the method
Adlai Hollerc95b5892020-08-11 12:02:22 -0400197 if (!srcProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400198 return false;
199 }
200
201 GrSurface* srcSurface = srcProxy->peekSurface();
202
Brian Salomon1d435302019-07-01 13:05:28 -0400203 auto dstInfo = origDstInfo;
204 if (!dstInfo.clip(this->width(), this->height(), &pt, &dst, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400205 return false;
206 }
Brian Salomon1047a492019-07-02 12:25:21 -0400207 // Our tight row bytes may have been changed by clipping.
208 tightRowBytes = dstInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400209
Mike Klein7321e6a2019-12-03 11:08:40 -0600210 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{this->colorInfo(), dstInfo}.flags;
211 bool unpremul = flags.unpremul,
212 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
213 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400214
Adlai Hollerc95b5892020-08-11 12:02:22 -0400215 const GrCaps* caps = dContext->priv().caps();
Robert Phillips07f0e412020-01-17 15:20:00 -0500216 bool srcIsCompressed = caps->isFormatCompressed(srcSurface->backendFormat());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400217 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
218 // care so much about getImageData performance. However, in order to ensure putImageData/
219 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
220 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
221 // fContext->vaildaPMUPMConversionExists()).
Greg Daniel0258c902019-08-01 13:08:33 -0400222 GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
223 GrRenderable::kYes);
Greg Danielc71c7962020-01-14 16:44:18 -0500224 GrColorType srcColorType = this->colorInfo().colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400225 bool canvas2DFastPath = unpremul && !needColorConversion &&
226 (GrColorType::kRGBA_8888 == dstInfo.colorType() ||
227 GrColorType::kBGRA_8888 == dstInfo.colorType()) &&
228 SkToBool(srcProxy->asTextureProxy()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500229 (srcColorType == GrColorType::kRGBA_8888 ||
230 srcColorType == GrColorType::kBGRA_8888) &&
Greg Daniel0258c902019-08-01 13:08:33 -0400231 defaultRGBAFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400232 dContext->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400233
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400234 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400235 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400236 return false;
237 }
238
Brian Salomondc0710f2019-07-01 14:59:32 -0400239 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400240 std::unique_ptr<GrSurfaceContext> tempCtx;
241 if (this->asTextureProxy()) {
242 GrColorType colorType = (canvas2DFastPath || srcIsCompressed)
243 ? GrColorType::kRGBA_8888
244 : this->colorInfo().colorType();
Brian Salomon590f5672020-12-16 11:44:47 -0500245 SkAlphaType alphaType = canvas2DFastPath ? dstInfo.alphaType()
246 : this->colorInfo().alphaType();
247 GrImageInfo tempInfo(colorType,
248 alphaType,
249 this->colorInfo().refColorSpace(),
250 dstInfo.dimensions());
251 auto sfc = GrSurfaceFillContext::Make(dContext, tempInfo, SkBackingFit::kApprox);
252 if (!sfc) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400253 return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400254 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400255
256 std::unique_ptr<GrFragmentProcessor> fp;
257 if (canvas2DFastPath) {
258 fp = dContext->priv().createPMToUPMEffect(GrTextureEffect::Make(
259 this->readSurfaceView(), this->colorInfo().alphaType()));
260 if (dstInfo.colorType() == GrColorType::kBGRA_8888) {
261 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
262 dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888);
263 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400264 } else {
265 fp = GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType());
266 }
267 if (!fp) {
268 return false;
269 }
Brian Salomon590f5672020-12-16 11:44:47 -0500270 sfc->fillRectToRectWithFP(SkIRect::MakePtSize(pt, dstInfo.dimensions()),
271 SkIRect::MakeSize(dstInfo.dimensions()),
272 std::move(fp));
Brian Salomon72c7b982020-10-06 10:07:38 -0400273 pt = {0, 0};
Brian Salomon590f5672020-12-16 11:44:47 -0500274 tempCtx = std::move(sfc);
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) &&
Brian Salomon590f5672020-12-16 11:44:47 -0500407 this->asFillContext() &&
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 =
Brian Salomon590f5672020-12-16 11:44:47 -0500437 this->asFillContext() ? 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
Brian Salomon590f5672020-12-16 11:44:47 -0500458 if (this->asFillContext()) {
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 }
Brian Salomon590f5672020-12-16 11:44:47 -0500473 this->asFillContext()->fillRectToRectWithFP(
474 SkIRect::MakeSize(srcInfo.dimensions()),
475 SkIRect::MakePtSize(pt, srcInfo.dimensions()),
476 std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400477 } else {
Brian Salomon1d435302019-07-01 13:05:28 -0400478 SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height());
479 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Brian Salomonc5243782020-04-02 12:50:34 -0400480 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400481 return false;
482 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400483 }
484 return true;
485 }
486
Brian Salomon1d435302019-07-01 13:05:28 -0400487 GrColorType allowedColorType =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400488 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400489 dstProxy->backendFormat(),
490 srcInfo.colorType()).fColorType;
Greg Danielb8d84f82020-02-13 14:25:00 -0500491 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon1047a492019-07-02 12:25:21 -0400492 bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes;
493 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomon1d435302019-07-01 13:05:28 -0400494 (srcInfo.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400495
Brian Salomonf30b1c12019-06-20 12:25:02 -0400496 std::unique_ptr<char[]> tmpPixels;
Brian Salomon1d435302019-07-01 13:05:28 -0400497 GrColorType srcColorType = srcInfo.colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400498 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400499 GrImageInfo tmpInfo(allowedColorType, this->colorInfo().alphaType(),
500 this->colorInfo().refColorSpace(), srcInfo.width(), srcInfo.height());
Brian Salomon1d435302019-07-01 13:05:28 -0400501 auto tmpRB = tmpInfo.minRowBytes();
502 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400503
Brian Salomon1d435302019-07-01 13:05:28 -0400504 GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400505
Brian Salomon1d435302019-07-01 13:05:28 -0400506 srcColorType = tmpInfo.colorType();
507 rowBytes = tmpRB;
508 src = tmpPixels.get();
509 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400510 }
511
512 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
513 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
514 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
515 // destination proxy)
516 // TODO: should this policy decision just be moved into the drawing manager?
Adlai Hollerc95b5892020-08-11 12:02:22 -0400517 dContext->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400518
Adlai Hollerc95b5892020-08-11 12:02:22 -0400519 return dContext->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(),
520 srcInfo.height(), this->colorInfo().colorType(),
521 srcColorType, src, rowBytes);
Brian Osman45580d32016-11-23 09:37:01 -0500522}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400523
Adlai Hollerc95b5892020-08-11 12:02:22 -0400524void GrSurfaceContext::asyncRescaleAndReadPixels(GrDirectContext* dContext,
525 const SkImageInfo& info,
Brian Salomon63a0a752020-06-26 13:32:09 -0400526 const SkIRect& srcRect,
527 RescaleGamma rescaleGamma,
528 SkFilterQuality rescaleQuality,
529 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400530 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400531 // We implement this by rendering and we don't currently support rendering kUnpremul.
532 if (info.alphaType() == kUnpremul_SkAlphaType) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400533 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400534 return;
535 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400536 if (!dContext) {
537 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400538 return;
539 }
540 auto rt = this->asRenderTargetProxy();
541 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400542 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400543 return;
544 }
545 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400546 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400547 return;
548 }
549 auto dstCT = SkColorTypeToGrColorType(info.colorType());
550 if (dstCT == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400551 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400552 return;
553 }
554 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
555 auto colorTypeOfFinalContext = this->colorInfo().colorType();
556 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
557 if (needsRescale) {
558 colorTypeOfFinalContext = dstCT;
559 backendFormatOfFinalContext =
560 this->caps()->getDefaultBackendFormat(dstCT, GrRenderable::kYes);
561 }
562 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
563 backendFormatOfFinalContext, dstCT);
564 // Fail if we can't read from the source surface's color type.
565 if (readInfo.fColorType == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400566 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400567 return;
568 }
569 // Fail if read color type does not have all of dstCT's color channels and those missing color
570 // channels are in the src.
571 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
572 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
573 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
574 if ((~legalReadChannels & dstChannels) & srcChannels) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400575 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400576 return;
577 }
578
Brian Salomoneebe7352020-12-09 16:37:04 -0500579 std::unique_ptr<GrSurfaceDrawContext> tempRTC;
Brian Salomon63a0a752020-06-26 13:32:09 -0400580 int x = srcRect.fLeft;
581 int y = srcRect.fTop;
582 if (needsRescale) {
583 tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
584 rescaleQuality);
585 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400586 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400587 return;
588 }
589 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
590 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
591 x = y = 0;
592 } else {
593 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
594 this->colorInfo().alphaType(),
595 info.colorSpace(),
596 info.alphaType());
597 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
598 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
599 GrSurfaceProxyView texProxyView = this->readSurfaceView();
600 SkRect srcRectToDraw = SkRect::Make(srcRect);
601 // If the src is not texturable first try to make a copy to a texture.
602 if (!texProxyView.asTextureProxy()) {
603 texProxyView =
Brian Salomon7e67dca2020-07-21 09:27:25 -0400604 GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipmapped::kNo, srcRect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400605 SkBackingFit::kApprox, SkBudgeted::kNo);
606 if (!texProxyView) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400607 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400608 return;
609 }
610 SkASSERT(texProxyView.asTextureProxy());
611 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
612 }
Brian Salomoneebe7352020-12-09 16:37:04 -0500613 tempRTC = GrSurfaceDrawContext::Make(dContext, this->colorInfo().colorType(),
614 info.refColorSpace(), SkBackingFit::kApprox,
615 srcRect.size(), 1, GrMipmapped::kNo,
616 GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400617 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400618 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400619 return;
620 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400621 tempRTC->drawTexture(nullptr,
622 std::move(texProxyView),
623 this->colorInfo().alphaType(),
624 GrSamplerState::Filter::kNearest,
625 GrSamplerState::MipmapMode::kNone,
626 SkBlendMode::kSrc,
627 SK_PMColor4fWHITE,
628 srcRectToDraw,
629 SkRect::MakeWH(srcRect.width(), srcRect.height()),
630 GrAA::kNo,
631 GrQuadAAFlags::kNone,
632 SkCanvas::kFast_SrcRectConstraint,
633 SkMatrix::I(),
634 std::move(xform));
Brian Salomon63a0a752020-06-26 13:32:09 -0400635 x = y = 0;
636 }
637 }
638 auto rtc = tempRTC ? tempRTC.get() : this;
Adlai Hollerc95b5892020-08-11 12:02:22 -0400639 return rtc->asyncReadPixels(dContext, SkIRect::MakeXYWH(x, y, info.width(), info.height()),
640 info.colorType(), callback, callbackContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400641}
642
643class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult {
644public:
645 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
646 ~AsyncReadResult() override {
647 for (int i = 0; i < fPlanes.count(); ++i) {
648 if (!fPlanes[i].fMappedBuffer) {
649 delete[] static_cast<const char*>(fPlanes[i].fData);
650 } else {
651 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
652 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
653 }
654 }
655 }
656
657 int count() const override { return fPlanes.count(); }
658 const void* data(int i) const override { return fPlanes[i].fData; }
659 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
660
661 bool addTransferResult(const PixelTransferResult& result,
662 SkISize dimensions,
663 size_t rowBytes,
664 GrClientMappedBufferManager* manager) {
665 SkASSERT(!result.fTransferBuffer->isMapped());
666 const void* mappedData = result.fTransferBuffer->map();
667 if (!mappedData) {
668 return false;
669 }
670 if (result.fPixelConverter) {
671 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
672 result.fPixelConverter(convertedData.get(), mappedData);
673 this->addCpuPlane(std::move(convertedData), rowBytes);
674 result.fTransferBuffer->unmap();
675 } else {
676 manager->insert(result.fTransferBuffer);
677 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
678 }
679 return true;
680 }
681
682 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
683 SkASSERT(data);
684 SkASSERT(rowBytes > 0);
685 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
686 }
687
688private:
689 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
690 SkASSERT(data);
691 SkASSERT(rowBytes > 0);
692 SkASSERT(mappedBuffer);
693 SkASSERT(mappedBuffer->isMapped());
694 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
695 }
696
697 struct Plane {
698 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
699 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
700 const void* fData;
701 size_t fRowBytes;
702 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
703 sk_sp<GrGpuBuffer> fMappedBuffer;
704 };
705 SkSTArray<3, Plane> fPlanes;
706 uint32_t fInboxID;
707};
708
Adlai Hollerc95b5892020-08-11 12:02:22 -0400709void GrSurfaceContext::asyncReadPixels(GrDirectContext* dContext,
710 const SkIRect& rect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400711 SkColorType colorType,
712 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400713 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400714 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
715 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
716
Adlai Hollerc95b5892020-08-11 12:02:22 -0400717 if (!dContext || this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
718 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400719 return;
720 }
721
Adlai Hollerc95b5892020-08-11 12:02:22 -0400722 auto mappedBufferManager = dContext->priv().clientMappedBufferManager();
Brian Salomon63a0a752020-06-26 13:32:09 -0400723
724 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
725
726 if (!transferResult.fTransferBuffer) {
727 auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
728 this->colorInfo().refColorSpace());
729 auto result = std::make_unique<AsyncReadResult>(0);
730 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
731 SkPixmap pm(ii, data.get(), ii.minRowBytes());
732 result->addCpuPlane(std::move(data), pm.rowBytes());
733
Adlai Hollerc95b5892020-08-11 12:02:22 -0400734 SkIPoint pt{rect.fLeft, rect.fTop};
735 if (!this->readPixels(dContext, ii, pm.writable_addr(), pm.rowBytes(), pt)) {
736 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400737 return;
738 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400739 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400740 return;
741 }
742
743 struct FinishContext {
744 ReadPixelsCallback* fClientCallback;
745 ReadPixelsContext fClientContext;
746 SkISize fSize;
747 SkColorType fColorType;
748 GrClientMappedBufferManager* fMappedBufferManager;
749 PixelTransferResult fTransferResult;
750 };
751 // Assumption is that the caller would like to flush. We could take a parameter or require an
752 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
753 // callback to GrGpu until after the next flush that flushes our op list, though.
754 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400755 callbackContext,
Brian Salomon63a0a752020-06-26 13:32:09 -0400756 rect.size(),
757 colorType,
758 mappedBufferManager,
759 std::move(transferResult)};
760 auto finishCallback = [](GrGpuFinishedContext c) {
761 const auto* context = reinterpret_cast<const FinishContext*>(c);
762 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
763 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
764 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
765 context->fMappedBufferManager)) {
766 result.reset();
767 }
768 (*context->fClientCallback)(context->fClientContext, std::move(result));
769 delete context;
770 };
771 GrFlushInfo flushInfo;
772 flushInfo.fFinishedContext = finishContext;
773 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -0500774
775 dContext->priv().flushSurface(this->asSurfaceProxy(),
776 SkSurface::BackendSurfaceAccess::kNoAccess,
777 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400778}
779
Adlai Hollerc95b5892020-08-11 12:02:22 -0400780void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(GrDirectContext* dContext,
781 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400782 sk_sp<SkColorSpace> dstColorSpace,
783 const SkIRect& srcRect,
784 SkISize dstSize,
785 RescaleGamma rescaleGamma,
786 SkFilterQuality rescaleQuality,
787 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400788 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400789 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
790 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
791 SkASSERT(!dstSize.isZero());
792 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
793
Adlai Hollerc95b5892020-08-11 12:02:22 -0400794 if (!dContext) {
795 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400796 return;
797 }
798 auto rt = this->asRenderTargetProxy();
799 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400800 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400801 return;
802 }
803 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400804 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400805 return;
806 }
807 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400808 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400809 return;
810 }
811 int x = srcRect.fLeft;
812 int y = srcRect.fTop;
813 bool needsRescale = srcRect.size() != dstSize;
814 GrSurfaceProxyView srcView;
815 if (needsRescale) {
816 // We assume the caller wants kPremul. There is no way to indicate a preference.
817 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
818 dstColorSpace);
819 // TODO: Incorporate the YUV conversion into last pass of rescaling.
820 auto tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
821 rescaleQuality);
822 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400823 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400824 return;
825 }
826 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
827 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
828 x = y = 0;
829 srcView = tempRTC->readSurfaceView();
830 } else {
831 srcView = this->readSurfaceView();
832 if (!srcView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400833 srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipmapped::kNo,
Brian Salomon63a0a752020-06-26 13:32:09 -0400834 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
835 if (!srcView) {
836 // If we can't get a texture copy of the contents then give up.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400837 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400838 return;
839 }
840 SkASSERT(srcView.asTextureProxy());
841 x = y = 0;
842 }
843 // We assume the caller wants kPremul. There is no way to indicate a preference.
844 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
845 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
846 kPremul_SkAlphaType);
847 if (xform) {
848 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
Brian Salomoneebe7352020-12-09 16:37:04 -0500849 auto tempRTC = GrSurfaceDrawContext::Make(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400850 dContext, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400851 dstSize, 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400852 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400853 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400854 return;
855 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400856 tempRTC->drawTexture(nullptr,
857 std::move(srcView),
858 this->colorInfo().alphaType(),
859 GrSamplerState::Filter::kNearest,
860 GrSamplerState::MipmapMode::kNone,
861 SkBlendMode::kSrc,
862 SK_PMColor4fWHITE,
863 srcRectToDraw,
864 SkRect::Make(srcRect.size()),
865 GrAA::kNo,
866 GrQuadAAFlags::kNone,
867 SkCanvas::kFast_SrcRectConstraint,
868 SkMatrix::I(),
869 std::move(xform));
Brian Salomon63a0a752020-06-26 13:32:09 -0400870 srcView = tempRTC->readSurfaceView();
871 SkASSERT(srcView.asTextureProxy());
872 x = y = 0;
873 }
874 }
875
Brian Salomoneebe7352020-12-09 16:37:04 -0500876 auto yRTC = GrSurfaceDrawContext::MakeWithFallback(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400877 dContext, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400878 GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400879 int halfW = dstSize.width() /2;
880 int halfH = dstSize.height()/2;
Brian Salomoneebe7352020-12-09 16:37:04 -0500881 auto uRTC = GrSurfaceDrawContext::MakeWithFallback(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400882 dContext, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH},
883 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomoneebe7352020-12-09 16:37:04 -0500884 auto vRTC = 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 Salomon63a0a752020-06-26 13:32:09 -0400887 if (!yRTC || !uRTC || !vRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400888 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400889 return;
890 }
891
892 float baseM[20];
893 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
894
895 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
896
897 auto texMatrix = SkMatrix::Translate(x, y);
898
899 SkRect dstRectY = SkRect::Make(dstSize);
900 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
901
902 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
903 PixelTransferResult yTransfer, uTransfer, vTransfer;
904
905 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
906 float yM[20];
907 std::fill_n(yM, 15, 0.f);
908 std::copy_n(baseM + 0, 5, yM + 15);
909 GrPaint yPaint;
910 auto yTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
911 auto yColFP = GrColorMatrixFragmentProcessor::Make(std::move(yTexFP), yM,
912 /*unpremulInput=*/false,
913 /*clampRGBOutput=*/true,
914 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400915 yPaint.setColorFragmentProcessor(std::move(yColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400916 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
917 yRTC->fillRectToRect(nullptr, std::move(yPaint), GrAA::kNo, SkMatrix::I(), dstRectY, dstRectY);
918 if (!doSynchronousRead) {
919 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
920 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
921 if (!yTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400922 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400923 return;
924 }
925 }
926
927 texMatrix.preScale(2.f, 2.f);
928 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
929 float uM[20];
930 std::fill_n(uM, 15, 0.f);
931 std::copy_n(baseM + 5, 5, uM + 15);
932 GrPaint uPaint;
933 auto uTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix,
Brian Salomona3b02f52020-07-15 16:02:01 -0400934 GrSamplerState::Filter::kLinear);
Brian Salomon63a0a752020-06-26 13:32:09 -0400935 auto uColFP = GrColorMatrixFragmentProcessor::Make(std::move(uTexFP), uM,
936 /*unpremulInput=*/false,
937 /*clampRGBOutput=*/true,
938 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400939 uPaint.setColorFragmentProcessor(std::move(uColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400940 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
941 uRTC->fillRectToRect(nullptr, std::move(uPaint), GrAA::kNo, SkMatrix::I(), dstRectUV,
942 dstRectUV);
943 if (!doSynchronousRead) {
944 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
945 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
946 if (!uTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400947 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400948 return;
949 }
950 }
951
952 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
953 float vM[20];
954 std::fill_n(vM, 15, 0.f);
955 std::copy_n(baseM + 10, 5, vM + 15);
956 GrPaint vPaint;
957 auto vTexFP = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType(),
Brian Salomona3b02f52020-07-15 16:02:01 -0400958 texMatrix, GrSamplerState::Filter::kLinear);
Brian Salomon63a0a752020-06-26 13:32:09 -0400959 auto vColFP = GrColorMatrixFragmentProcessor::Make(std::move(vTexFP), vM,
960 /*unpremulInput=*/false,
961 /*clampRGBOutput=*/true,
962 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400963 vPaint.setColorFragmentProcessor(std::move(vColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400964 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
965 vRTC->fillRectToRect(nullptr, std::move(vPaint), GrAA::kNo, SkMatrix::I(), dstRectUV,
966 dstRectUV);
967 if (!doSynchronousRead) {
968 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
969 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
970 if (!vTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400971 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400972 return;
973 }
974 }
975
976 if (doSynchronousRead) {
977 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
978 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
979 size_t yRB = yInfo.minRowBytes();
980 size_t uvRB = uvInfo.minRowBytes();
981 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
982 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
983 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400984 if (!yRTC->readPixels(dContext, yInfo, y.get(), yRB, {0, 0}) ||
985 !uRTC->readPixels(dContext, uvInfo, u.get(), uvRB, {0, 0}) ||
986 !vRTC->readPixels(dContext, uvInfo, v.get(), uvRB, {0, 0})) {
987 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400988 return;
989 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400990 auto result = std::make_unique<AsyncReadResult>(dContext->priv().contextID());
Brian Salomon63a0a752020-06-26 13:32:09 -0400991 result->addCpuPlane(std::move(y), yRB );
992 result->addCpuPlane(std::move(u), uvRB);
993 result->addCpuPlane(std::move(v), uvRB);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400994 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400995 return;
996 }
997
998 struct FinishContext {
999 ReadPixelsCallback* fClientCallback;
1000 ReadPixelsContext fClientContext;
1001 GrClientMappedBufferManager* fMappedBufferManager;
1002 SkISize fSize;
1003 PixelTransferResult fYTransfer;
1004 PixelTransferResult fUTransfer;
1005 PixelTransferResult fVTransfer;
1006 };
1007 // Assumption is that the caller would like to flush. We could take a parameter or require an
1008 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1009 // callback to GrGpu until after the next flush that flushes our op list, though.
1010 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -04001011 callbackContext,
1012 dContext->priv().clientMappedBufferManager(),
Brian Salomon63a0a752020-06-26 13:32:09 -04001013 dstSize,
1014 std::move(yTransfer),
1015 std::move(uTransfer),
1016 std::move(vTransfer)};
1017 auto finishCallback = [](GrGpuFinishedContext c) {
1018 const auto* context = reinterpret_cast<const FinishContext*>(c);
1019 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
1020 auto manager = context->fMappedBufferManager;
1021 size_t rowBytes = SkToSizeT(context->fSize.width());
1022 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
1023 (*context->fClientCallback)(context->fClientContext, nullptr);
1024 delete context;
1025 return;
1026 }
1027 rowBytes /= 2;
1028 SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
1029 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
1030 (*context->fClientCallback)(context->fClientContext, nullptr);
1031 delete context;
1032 return;
1033 }
1034 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
1035 (*context->fClientCallback)(context->fClientContext, nullptr);
1036 delete context;
1037 return;
1038 }
1039 (*context->fClientCallback)(context->fClientContext, std::move(result));
1040 delete context;
1041 };
1042 GrFlushInfo flushInfo;
1043 flushInfo.fFinishedContext = finishContext;
1044 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -05001045 dContext->priv().flushSurface(this->asSurfaceProxy(),
1046 SkSurface::BackendSurfaceAccess::kNoAccess,
1047 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -04001048}
1049
Brian Salomonc5243782020-04-02 12:50:34 -04001050bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001051 ASSERT_SINGLE_OWNER
1052 RETURN_FALSE_IF_ABANDONED
1053 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -04001054 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -04001055
Brian Salomon947efe22019-07-16 15:36:11 -04001056 const GrCaps* caps = fContext->priv().caps();
1057
Greg Daniel46cfbc62019-06-07 11:43:30 -04001058 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -05001059 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -04001060
Stephen White3c0a50f2020-01-16 18:19:54 -05001061 if (this->asSurfaceProxy()->framebufferOnly()) {
1062 return false;
1063 }
1064
Chris Daltonf8e5aad2019-08-02 12:55:23 -06001065 if (!caps->canCopySurface(this->asSurfaceProxy(), src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -04001066 return false;
1067 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001068
Greg Daniel16f5c652019-10-29 11:26:01 -04001069 // The swizzle doesn't matter for copies and it is not used.
1070 return this->drawingManager()->newCopyRenderTask(
Brian Salomonc5243782020-04-02 12:50:34 -04001071 GrSurfaceProxyView(sk_ref_sp(src), this->origin(), GrSwizzle("rgba")), srcRect,
Greg Daniel46e366a2019-12-16 14:38:36 -05001072 this->readSurfaceView(), dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001073}
Greg Daniel46cfbc62019-06-07 11:43:30 -04001074
Brian Salomoneebe7352020-12-09 16:37:04 -05001075std::unique_ptr<GrSurfaceDrawContext> GrSurfaceContext::rescale(const GrImageInfo& info,
1076 GrSurfaceOrigin origin,
1077 SkIRect srcRect,
1078 RescaleGamma rescaleGamma,
1079 SkFilterQuality rescaleQuality) {
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001080 // We rescale by drawing and currently only support drawing to premul.
Brian Salomon1c86b632020-12-11 12:36:01 -05001081 if (info.alphaType() != kPremul_SkAlphaType) {
1082 return nullptr;
1083 }
1084 auto sdc = GrSurfaceDrawContext::MakeWithFallback(fContext,
1085 info.colorType(),
1086 info.refColorSpace(),
1087 SkBackingFit::kExact,
1088 info.dimensions(),
1089 1,
1090 GrMipmapped::kNo,
1091 this->asSurfaceProxy()->isProtected(),
1092 origin);
1093 if (!sdc || !this->rescaleInto(sdc.get(),
1094 SkIRect::MakeSize(sdc->dimensions()),
1095 srcRect,
1096 rescaleGamma,
1097 rescaleQuality)) {
1098 return nullptr;
1099 }
1100 return sdc;
1101}
1102
1103bool GrSurfaceContext::rescaleInto(GrSurfaceDrawContext* dst,
1104 SkIRect dstRect,
1105 SkIRect srcRect,
1106 RescaleGamma rescaleGamma,
1107 SkFilterQuality rescaleQuality) {
1108 SkASSERT(dst);
1109 if (!SkIRect::MakeSize(dst->dimensions()).contains((dstRect))) {
1110 return false;
1111 }
1112
Brian Salomone9ad9982019-07-22 16:17:41 -04001113 auto rtProxy = this->asRenderTargetProxy();
1114 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001115 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001116 }
1117
Stephen White3c0a50f2020-01-16 18:19:54 -05001118 if (this->asSurfaceProxy()->framebufferOnly()) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001119 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001120 }
1121
Greg Daniel40903af2020-01-30 14:55:05 -05001122 GrSurfaceProxyView texView = this->readSurfaceView();
Brian Salomonfc118442019-11-22 19:09:27 -05001123 SkAlphaType srcAlphaType = this->colorInfo().alphaType();
Greg Daniel40903af2020-01-30 14:55:05 -05001124 if (!texView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -04001125 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect,
Brian Salomonc5243782020-04-02 12:50:34 -04001126 SkBackingFit::kApprox, SkBudgeted::kNo);
1127 if (!texView) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001128 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001129 }
Greg Daniel40903af2020-01-30 14:55:05 -05001130 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001131 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001132 }
1133
Brian Salomon1c86b632020-12-11 12:36:01 -05001134 SkISize finalSize = dstRect.size();
1135
Brian Salomonbf6b9792019-08-21 09:38:10 -04001136 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
1137 // 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 -05001138 std::unique_ptr<GrSurfaceDrawContext> tempA;
1139 std::unique_ptr<GrSurfaceDrawContext> tempB;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001140
Brian Salomone9ad9982019-07-22 16:17:41 -04001141 // Assume we should ignore the rescale linear request if the surface has no color space since
1142 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon63a0a752020-06-26 13:32:09 -04001143 if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() &&
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001144 !this->colorInfo().colorSpace()->gammaIsLinear()) {
1145 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomonfc118442019-11-22 19:09:27 -05001146 auto xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(), srcAlphaType, cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -04001147 kPremul_SkAlphaType);
1148 // We'll fall back to kRGBA_8888 if half float not supported.
Brian Salomoneebe7352020-12-09 16:37:04 -05001149 auto linearRTC = GrSurfaceDrawContext::MakeWithFallback(
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001150 fContext, GrColorType::kRGBA_F16, cs, SkBackingFit::kApprox, srcRect.size(), 1,
Brian Salomon1c86b632020-12-11 12:36:01 -05001151 GrMipmapped::kNo, GrProtected::kNo, dst->origin());
Brian Salomone9ad9982019-07-22 16:17:41 -04001152 if (!linearRTC) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001153 return false;
Brian Salomone9ad9982019-07-22 16:17:41 -04001154 }
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001155 // 1-to-1 draw can always be kFast.
Brian Salomone69b9ef2020-07-22 11:18:06 -04001156 linearRTC->drawTexture(nullptr,
1157 std::move(texView),
1158 srcAlphaType,
1159 GrSamplerState::Filter::kNearest,
1160 GrSamplerState::MipmapMode::kNone,
1161 SkBlendMode::kSrc,
1162 SK_PMColor4fWHITE,
1163 SkRect::Make(srcRect),
1164 SkRect::Make(srcRect.size()),
1165 GrAA::kNo,
1166 GrQuadAAFlags::kNone,
1167 SkCanvas::kFast_SrcRectConstraint,
1168 SkMatrix::I(),
1169 std::move(xform));
Greg Daniel40903af2020-01-30 14:55:05 -05001170 texView = linearRTC->readSurfaceView();
1171 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -04001172 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001173 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001174 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001175
Brian Salomon1c86b632020-12-11 12:36:01 -05001176 while (srcRect.size() != finalSize) {
1177 SkISize nextDims = finalSize;
Brian Salomon59f31b12020-06-04 17:27:15 -04001178 if (rescaleQuality != kNone_SkFilterQuality) {
Brian Salomon1c86b632020-12-11 12:36:01 -05001179 if (srcRect.width() > finalSize.width()) {
1180 nextDims.fWidth = std::max((srcRect.width() + 1)/2, finalSize.width());
1181 } else if (srcRect.width() < finalSize.width()) {
1182 nextDims.fWidth = std::min(srcRect.width()*2, finalSize.width());
Brian Salomon59f31b12020-06-04 17:27:15 -04001183 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001184 if (srcRect.height() > finalSize.height()) {
1185 nextDims.fHeight = std::max((srcRect.height() + 1)/2, finalSize.height());
1186 } else if (srcRect.height() < finalSize.height()) {
1187 nextDims.fHeight = std::min(srcRect.height()*2, finalSize.height());
Brian Salomon59f31b12020-06-04 17:27:15 -04001188 }
1189 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001190 auto input = tempA ? tempA.get() : this;
Brian Salomone9ad9982019-07-22 16:17:41 -04001191 sk_sp<GrColorSpaceXform> xform;
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001192 GrSurfaceDrawContext* stepDst;
1193 SkIRect stepDstRect;
Brian Salomon1c86b632020-12-11 12:36:01 -05001194 if (nextDims == finalSize) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001195 // Might as well fold conversion to final info in the last step.
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001196 xform = GrColorSpaceXform::Make(input->colorInfo().colorSpace(),
Brian Salomon1c86b632020-12-11 12:36:01 -05001197 input->colorInfo().alphaType(),
1198 dst->colorInfo().colorSpace(),
1199 dst->colorInfo().alphaType());
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001200 stepDst = dst;
1201 stepDstRect = dstRect;
Brian Salomon1c86b632020-12-11 12:36:01 -05001202 } else {
1203 tempB = GrSurfaceDrawContext::MakeWithFallback(fContext,
1204 input->colorInfo().colorType(),
1205 input->colorInfo().refColorSpace(),
1206 SkBackingFit::kApprox,
1207 nextDims,
1208 1,
1209 GrMipmapped::kNo,
1210 GrProtected::kNo,
1211 dst->origin());
1212 if (!tempB) {
1213 return false;
1214 }
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001215 stepDst = tempB.get();
1216 stepDstRect = SkIRect::MakeSize(tempB->dimensions());
Brian Salomone9ad9982019-07-22 16:17:41 -04001217 }
Brian Salomon59f31b12020-06-04 17:27:15 -04001218 if (rescaleQuality == kHigh_SkFilterQuality) {
1219 SkMatrix matrix;
1220 matrix.setScaleTranslate((float)srcRect.width()/nextDims.width(),
1221 (float)srcRect.height()/nextDims.height(),
1222 srcRect.x(),
1223 srcRect.y());
1224 std::unique_ptr<GrFragmentProcessor> fp;
1225 auto dir = GrBicubicEffect::Direction::kXY;
1226 if (nextDims.width() == srcRect.width()) {
1227 dir = GrBicubicEffect::Direction::kY;
1228 } else if (nextDims.height() == srcRect.height()) {
1229 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -04001230 }
Brian Salomon1af72d12020-06-25 10:47:26 -04001231 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
Mike Reed3867c702020-09-01 13:28:10 -04001232 static constexpr auto kKernel = GrBicubicEffect::gCatmullRom;
Brian Salomon1c86b632020-12-11 12:36:01 -05001233 fp = GrBicubicEffect::MakeSubset(std::move(texView),
1234 input->colorInfo().alphaType(),
1235 matrix,
1236 kWM,
1237 kWM,
1238 SkRect::Make(srcRect),
1239 kKernel,
1240 dir,
1241 *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -04001242 if (xform) {
1243 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -04001244 }
Brian Salomon59f31b12020-06-04 17:27:15 -04001245 GrPaint paint;
John Stiles5933d7d2020-07-21 12:28:35 -04001246 paint.setColorFragmentProcessor(std::move(fp));
Brian Salomon59f31b12020-06-04 17:27:15 -04001247 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001248 stepDst->fillRectToRect(nullptr,
1249 std::move(paint),
1250 GrAA::kNo,
1251 SkMatrix::I(),
1252 SkRect::Make(stepDstRect),
1253 SkRect::Make(stepDstRect));
Brian Salomon59f31b12020-06-04 17:27:15 -04001254 } else {
1255 auto filter = rescaleQuality == kNone_SkFilterQuality ? GrSamplerState::Filter::kNearest
Brian Salomona3b02f52020-07-15 16:02:01 -04001256 : GrSamplerState::Filter::kLinear;
Brian Salomon59f31b12020-06-04 17:27:15 -04001257 // Minimizing draw with integer coord src and dev rects can always be kFast.
1258 auto constraint = SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint;
1259 if (nextDims.width() <= srcRect.width() && nextDims.height() <= srcRect.height()) {
1260 constraint = SkCanvas::SrcRectConstraint::kFast_SrcRectConstraint;
1261 }
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001262 stepDst->drawTexture(nullptr,
1263 std::move(texView),
1264 srcAlphaType,
1265 filter,
1266 GrSamplerState::MipmapMode::kNone,
1267 SkBlendMode::kSrc,
1268 SK_PMColor4fWHITE,
1269 SkRect::Make(srcRect),
1270 SkRect::Make(stepDstRect),
1271 GrAA::kNo,
1272 GrQuadAAFlags::kNone,
1273 constraint,
1274 SkMatrix::I(),
1275 std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -04001276 }
Brian Salomon4bd9fcc2020-12-11 13:52:03 -05001277 texView = stepDst->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -04001278 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001279 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomone9ad9982019-07-22 16:17:41 -04001280 }
Brian Salomon1c86b632020-12-11 12:36:01 -05001281 return true;
Brian Salomone9ad9982019-07-22 16:17:41 -04001282}
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001283
1284GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
1285 const SkIRect& rect) {
1286 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1287 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001288 auto direct = fContext->asDirectContext();
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001289 if (!direct) {
1290 return {};
1291 }
1292 auto rtProxy = this->asRenderTargetProxy();
1293 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1294 return {};
1295 }
1296
1297 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001298 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
1299 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001300 // Fail if read color type does not have all of dstCT's color channels and those missing color
1301 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001302 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1303 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
1304 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1305 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001306 return {};
1307 }
1308
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001309 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001310 !supportedRead.fOffsetAlignmentForTransferBuffer) {
1311 return {};
1312 }
1313
1314 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
1315 size_t size = rowBytes * rect.height();
1316 auto buffer = direct->priv().resourceProvider()->createBuffer(
1317 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
1318 if (!buffer) {
1319 return {};
1320 }
1321 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -05001322 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001323 if (flip) {
1324 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1325 this->height() - rect.fTop);
1326 }
Greg Danielbbfec9d2019-08-20 10:56:51 -04001327 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001328 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -04001329 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001330 PixelTransferResult result;
1331 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001332 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -04001333 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001334 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
1335 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001336 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
1337 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001338 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
1339 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -04001340 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001341 };
1342 }
1343 return result;
1344}
Greg Daniel46e366a2019-12-16 14:38:36 -05001345
1346#ifdef SK_DEBUG
1347void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001348 SkASSERT(fReadView.proxy());
1349 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -04001350 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
1351 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
1352 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
1353 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001354 this->onValidate();
1355}
1356#endif