blob: 53d49229bd3f0049239bd6ca78d8591bed9dab44 [file] [log] [blame]
Brian Osman45580d32016-11-23 09:37:01 -05001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Greg Daniel46cfbc62019-06-07 11:43:30 -04008#include "src/gpu/GrSurfaceContext.h"
9
John Stilesfbd050b2020-08-03 13:21:46 -040010#include <memory>
11
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040012#include "include/gpu/GrDirectContext.h"
13#include "include/gpu/GrRecordingContext.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040014#include "src/core/SkAutoPixmapStorage.h"
Brian Salomon63a0a752020-06-26 13:32:09 -040015#include "src/core/SkYUVMath.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040016#include "src/gpu/GrAuditTrail.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040017#include "src/gpu/GrDataUtils.h"
Adlai Hollera0693042020-10-14 11:23:11 -040018#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrDrawingManager.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040020#include "src/gpu/GrGpu.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040021#include "src/gpu/GrImageInfo.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040022#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrRecordingContextPriv.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040024#include "src/gpu/GrRenderTargetContext.h"
Greg Daniel46cfbc62019-06-07 11:43:30 -040025#include "src/gpu/GrSurfaceContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/SkGr.h"
Brian Salomone9ad9982019-07-22 16:17:41 -040027#include "src/gpu/effects/GrBicubicEffect.h"
Brian Salomon63a0a752020-06-26 13:32:09 -040028#include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h"
Brian Osman45580d32016-11-23 09:37:01 -050029
Adlai Holler33dbd652020-06-01 12:35:42 -040030#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
Robert Phillips9eb00022020-06-30 15:30:12 -040031#define RETURN_FALSE_IF_ABANDONED if (this->fContext->abandoned()) { return false; }
Brian Osman45580d32016-11-23 09:37:01 -050032
Greg Danielbfa19c42019-12-19 16:41:40 -050033std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -050034 GrSurfaceProxyView readView,
Greg Danielbfa19c42019-12-19 16:41:40 -050035 GrColorType colorType,
36 SkAlphaType alphaType,
37 sk_sp<SkColorSpace> colorSpace) {
Greg Daniele20fcad2020-01-08 11:52:34 -050038 // It is probably not necessary to check if the context is abandoned here since uses of the
39 // GrSurfaceContext which need the context will mostly likely fail later on without an issue.
40 // However having this hear adds some reassurance in case there is a path doesn't handle an
41 // abandoned context correctly. It also lets us early out of some extra work.
Robert Phillips9eb00022020-06-30 15:30:12 -040042 if (context->abandoned()) {
Greg Daniele20fcad2020-01-08 11:52:34 -050043 return nullptr;
44 }
Greg Daniel3912a4b2020-01-14 09:56:04 -050045 GrSurfaceProxy* proxy = readView.proxy();
Greg Danielbfa19c42019-12-19 16:41:40 -050046 SkASSERT(proxy && proxy->asTextureProxy());
47
Greg Danielbfa19c42019-12-19 16:41:40 -050048 std::unique_ptr<GrSurfaceContext> surfaceContext;
Greg Daniel3912a4b2020-01-14 09:56:04 -050049 if (proxy->asRenderTargetProxy()) {
Greg Danielbfa19c42019-12-19 16:41:40 -050050 SkASSERT(kPremul_SkAlphaType == alphaType || kOpaque_SkAlphaType == alphaType);
Brian Salomon8afde5f2020-04-01 16:22:00 -040051 // Will we ever want a swizzle that is not the default write swizzle for the format and
Greg Danielbfa19c42019-12-19 16:41:40 -050052 // colorType here? If so we will need to manually pass that in.
Brian Salomonc5243782020-04-02 12:50:34 -040053 GrSwizzle writeSwizzle;
54 if (colorType != GrColorType::kUnknown) {
55 writeSwizzle =
56 context->priv().caps()->getWriteSwizzle(proxy->backendFormat(), colorType);
57 }
Brian Salomon8afde5f2020-04-01 16:22:00 -040058 GrSurfaceProxyView writeView(readView.refProxy(), readView.origin(), writeSwizzle);
John Stilesfbd050b2020-08-03 13:21:46 -040059 surfaceContext = std::make_unique<GrRenderTargetContext>(context, std::move(readView),
Brian Salomon8afde5f2020-04-01 16:22:00 -040060 std::move(writeView), colorType,
John Stilesfbd050b2020-08-03 13:21:46 -040061 std::move(colorSpace), nullptr);
Greg Danielbfa19c42019-12-19 16:41:40 -050062 } else {
John Stilesfbd050b2020-08-03 13:21:46 -040063 surfaceContext = std::make_unique<GrSurfaceContext>(context, std::move(readView), colorType,
64 alphaType, std::move(colorSpace));
Greg Danielbfa19c42019-12-19 16:41:40 -050065 }
Robert Phillips07f0e412020-01-17 15:20:00 -050066 SkDEBUGCODE(surfaceContext->validate();)
Greg Danielbfa19c42019-12-19 16:41:40 -050067 return surfaceContext;
68}
69
Brian Salomona56a7462020-02-07 14:17:25 -050070std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context,
71 SkISize dimensions,
72 const GrBackendFormat& format,
73 GrRenderable renderable,
74 int renderTargetSampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -040075 GrMipmapped mipMapped,
Brian Salomona56a7462020-02-07 14:17:25 -050076 GrProtected isProtected,
77 GrSurfaceOrigin origin,
78 GrColorType colorType,
79 SkAlphaType alphaType,
80 sk_sp<SkColorSpace> colorSpace,
81 SkBackingFit fit,
82 SkBudgeted budgeted) {
Brian Salomonc5243782020-04-02 12:50:34 -040083 GrSwizzle swizzle;
84 if (colorType != GrColorType::kUnknown && !context->priv().caps()->isFormatCompressed(format)) {
Brian Salomond005b692020-04-01 15:47:05 -040085 swizzle = context->priv().caps()->getReadSwizzle(format, colorType);
86 }
Greg Daniel47c20e82020-01-21 14:29:57 -050087
Greg Danielbfa19c42019-12-19 16:41:40 -050088 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -040089 format, dimensions, renderable, renderTargetSampleCnt, mipMapped, fit, budgeted,
90 isProtected);
Greg Danielbfa19c42019-12-19 16:41:40 -050091 if (!proxy) {
92 return nullptr;
93 }
94
Greg Daniel3912a4b2020-01-14 09:56:04 -050095 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
96 return GrSurfaceContext::Make(context, std::move(view), colorType, alphaType,
Greg Danielbfa19c42019-12-19 16:41:40 -050097 std::move(colorSpace));
98}
99
Greg Danielf41b2bd2019-08-22 16:19:24 -0400100// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
101// GrOpsTasks to be picked up and added to by renderTargetContexts lower in the call
102// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
103// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500104GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500105 GrSurfaceProxyView readView,
Brian Salomond6287472019-06-24 15:50:07 -0400106 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -0400107 SkAlphaType alphaType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500108 sk_sp<SkColorSpace> colorSpace)
Greg Daniel901b98e2019-10-22 09:54:02 -0400109 : fContext(context)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500110 , fReadView(std::move(readView))
111 , fColorInfo(colorType, alphaType, std::move(colorSpace)) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400112 SkASSERT(!context->abandoned());
Greg Daniele20fcad2020-01-08 11:52:34 -0500113}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400114
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400115const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); }
116
Robert Phillips0d075de2019-03-04 11:08:13 -0500117GrAuditTrail* GrSurfaceContext::auditTrail() {
118 return fContext->priv().auditTrail();
119}
120
121GrDrawingManager* GrSurfaceContext::drawingManager() {
122 return fContext->priv().drawingManager();
123}
124
125const GrDrawingManager* GrSurfaceContext::drawingManager() const {
126 return fContext->priv().drawingManager();
127}
128
129#ifdef SK_DEBUG
130GrSingleOwner* GrSurfaceContext::singleOwner() {
131 return fContext->priv().singleOwner();
132}
133#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400134
Adlai Hollerc95b5892020-08-11 12:02:22 -0400135bool GrSurfaceContext::readPixels(GrDirectContext* dContext, const GrImageInfo& origDstInfo,
136 void* dst, size_t rowBytes, SkIPoint pt) {
Brian Salomon1d435302019-07-01 13:05:28 -0400137 ASSERT_SINGLE_OWNER
138 RETURN_FALSE_IF_ABANDONED
139 SkDEBUGCODE(this->validate();)
140 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400141 if (!fContext->priv().matches(dContext)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400142 return false;
143 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400144
Brian Salomon1d435302019-07-01 13:05:28 -0400145 if (!dst) {
146 return false;
147 }
148
Brian Salomon1047a492019-07-02 12:25:21 -0400149 size_t tightRowBytes = origDstInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400150 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400151 rowBytes = tightRowBytes;
152 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400153 return false;
154 }
155
156 if (!origDstInfo.isValid()) {
157 return false;
158 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400159
160 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
161
Stephen White3c0a50f2020-01-16 18:19:54 -0500162 if (srcProxy->framebufferOnly()) {
163 return false;
164 }
165
Greg Daniel6eb8c242019-06-05 10:22:24 -0400166 // MDB TODO: delay this instantiation until later in the method
Adlai Hollerc95b5892020-08-11 12:02:22 -0400167 if (!srcProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400168 return false;
169 }
170
171 GrSurface* srcSurface = srcProxy->peekSurface();
172
Brian Salomon1d435302019-07-01 13:05:28 -0400173 auto dstInfo = origDstInfo;
174 if (!dstInfo.clip(this->width(), this->height(), &pt, &dst, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400175 return false;
176 }
Brian Salomon1047a492019-07-02 12:25:21 -0400177 // Our tight row bytes may have been changed by clipping.
178 tightRowBytes = dstInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400179
Mike Klein7321e6a2019-12-03 11:08:40 -0600180 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{this->colorInfo(), dstInfo}.flags;
181 bool unpremul = flags.unpremul,
182 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
183 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400184
Adlai Hollerc95b5892020-08-11 12:02:22 -0400185 const GrCaps* caps = dContext->priv().caps();
Robert Phillips07f0e412020-01-17 15:20:00 -0500186 bool srcIsCompressed = caps->isFormatCompressed(srcSurface->backendFormat());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400187 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
188 // care so much about getImageData performance. However, in order to ensure putImageData/
189 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
190 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
191 // fContext->vaildaPMUPMConversionExists()).
Greg Daniel0258c902019-08-01 13:08:33 -0400192 GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
193 GrRenderable::kYes);
Greg Danielc71c7962020-01-14 16:44:18 -0500194 GrColorType srcColorType = this->colorInfo().colorType();
Brian Salomon1d435302019-07-01 13:05:28 -0400195 bool canvas2DFastPath = unpremul && !needColorConversion &&
196 (GrColorType::kRGBA_8888 == dstInfo.colorType() ||
197 GrColorType::kBGRA_8888 == dstInfo.colorType()) &&
198 SkToBool(srcProxy->asTextureProxy()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500199 (srcColorType == GrColorType::kRGBA_8888 ||
200 srcColorType == GrColorType::kBGRA_8888) &&
Greg Daniel0258c902019-08-01 13:08:33 -0400201 defaultRGBAFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400202 dContext->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400203
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400204 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400205 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400206 return false;
207 }
208
Brian Salomondc0710f2019-07-01 14:59:32 -0400209 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Brian Salomon72c7b982020-10-06 10:07:38 -0400210 std::unique_ptr<GrSurfaceContext> tempCtx;
211 if (this->asTextureProxy()) {
212 GrColorType colorType = (canvas2DFastPath || srcIsCompressed)
213 ? GrColorType::kRGBA_8888
214 : this->colorInfo().colorType();
215 sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorInfo().refColorSpace();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400216
Brian Salomon72c7b982020-10-06 10:07:38 -0400217 tempCtx = GrRenderTargetContext::Make(
218 dContext, colorType, std::move(cs), SkBackingFit::kApprox, dstInfo.dimensions(),
219 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
220 if (!tempCtx) {
221 return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400222 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400223
224 std::unique_ptr<GrFragmentProcessor> fp;
225 if (canvas2DFastPath) {
226 fp = dContext->priv().createPMToUPMEffect(GrTextureEffect::Make(
227 this->readSurfaceView(), this->colorInfo().alphaType()));
228 if (dstInfo.colorType() == GrColorType::kBGRA_8888) {
229 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
230 dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888);
231 }
232 // The render target context is incorrectly tagged as kPremul even though we're
233 // writing unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type
234 // so we don't double unpremul.
235 dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType);
236 } else {
237 fp = GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType());
238 }
239 if (!fp) {
240 return false;
241 }
242 GrPaint paint;
243 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
244 paint.setColorFragmentProcessor(std::move(fp));
245
246 tempCtx->asRenderTargetContext()->fillRectToRect(
247 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
248 SkRect::MakeWH(dstInfo.width(), dstInfo.height()),
249 SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height()));
250 pt = {0, 0};
Greg Daniel6eb8c242019-06-05 10:22:24 -0400251 } else {
Brian Salomon72c7b982020-10-06 10:07:38 -0400252 auto restrictions = this->caps()->getDstCopyRestrictions(this->asRenderTargetProxy(),
253 this->colorInfo().colorType());
254 sk_sp<GrSurfaceProxy> copy;
255 static constexpr auto kFit = SkBackingFit::kExact;
256 static constexpr auto kBudgeted = SkBudgeted::kYes;
257 static constexpr auto kMipMapped = GrMipMapped::kNo;
258 if (restrictions.fMustCopyWholeSrc) {
259 copy = GrSurfaceProxy::Copy(fContext, srcProxy, this->origin(), kMipMapped, kFit,
260 kBudgeted);
261 } else {
262 auto srcRect = SkIRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height());
263 copy = GrSurfaceProxy::Copy(fContext, srcProxy, this->origin(), kMipMapped, srcRect,
264 kFit, kBudgeted, restrictions.fRectsMustMatch);
265 pt = {0, 0};
266 }
267 if (!copy) {
268 return false;
269 }
270 GrSurfaceProxyView view{std::move(copy), this->origin(), this->readSwizzle()};
271 tempCtx = GrSurfaceContext::Make(dContext,
272 std::move(view),
273 this->colorInfo().colorType(),
274 this->colorInfo().alphaType(),
275 this->colorInfo().refColorSpace());
276 SkASSERT(tempCtx);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400277 }
Brian Salomon72c7b982020-10-06 10:07:38 -0400278 return tempCtx->readPixels(dContext, dstInfo, dst, rowBytes, pt);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400279 }
280
Greg Danielb8d84f82020-02-13 14:25:00 -0500281 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400282
Brian Salomon1d435302019-07-01 13:05:28 -0400283 auto supportedRead = caps->supportedReadPixelsColorType(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400284 this->colorInfo().colorType(), srcProxy->backendFormat(), dstInfo.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400285
Brian Salomon1047a492019-07-02 12:25:21 -0400286 bool makeTight = !caps->readPixelsRowBytesSupport() && tightRowBytes != rowBytes;
287
288 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomon8f8354a2019-07-31 20:12:02 -0400289 (dstInfo.colorType() != supportedRead.fColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400290
Brian Salomonf30b1c12019-06-20 12:25:02 -0400291 std::unique_ptr<char[]> tmpPixels;
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400292 GrImageInfo tmpInfo;
Brian Salomon1d435302019-07-01 13:05:28 -0400293 void* readDst = dst;
294 size_t readRB = rowBytes;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400295 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400296 tmpInfo = {supportedRead.fColorType, this->colorInfo().alphaType(),
297 this->colorInfo().refColorSpace(), dstInfo.width(), dstInfo.height()};
Brian Salomon1d435302019-07-01 13:05:28 -0400298 size_t tmpRB = tmpInfo.minRowBytes();
299 size_t size = tmpRB * tmpInfo.height();
300 // Chrome MSAN bots require the data to be initialized (hence the ()).
John Stilesfbd050b2020-08-03 13:21:46 -0400301 tmpPixels = std::make_unique<char[]>(size);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400302
Brian Salomonf30b1c12019-06-20 12:25:02 -0400303 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400304 readRB = tmpRB;
305 pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400306 }
307
Adlai Hollerc95b5892020-08-11 12:02:22 -0400308 dContext->priv().flushSurface(srcProxy);
309 dContext->submit();
310 if (!dContext->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(),
311 dstInfo.height(), this->colorInfo().colorType(),
312 supportedRead.fColorType, readDst, readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400313 return false;
314 }
315
Greg Daniel6eb8c242019-06-05 10:22:24 -0400316 if (convert) {
Brian Salomon8f8354a2019-07-31 20:12:02 -0400317 return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400318 }
319 return true;
320}
Robert Phillips0d075de2019-03-04 11:08:13 -0500321
Adlai Hollerc95b5892020-08-11 12:02:22 -0400322bool GrSurfaceContext::writePixels(GrDirectContext* dContext, const GrImageInfo& origSrcInfo,
323 const void* src, size_t rowBytes, SkIPoint pt) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400324 ASSERT_SINGLE_OWNER
325 RETURN_FALSE_IF_ABANDONED
326 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400327 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400328
Adlai Hollerc95b5892020-08-11 12:02:22 -0400329 if (!dContext) {
Brian Salomonc320b152018-02-20 14:05:36 -0500330 return false;
331 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500332
Brian Salomon1d435302019-07-01 13:05:28 -0400333 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500334 return false;
335 }
336
Brian Salomon1d435302019-07-01 13:05:28 -0400337 if (!src) {
338 return false;
339 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400340
Brian Salomon1047a492019-07-02 12:25:21 -0400341 size_t tightRowBytes = origSrcInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400342 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400343 rowBytes = tightRowBytes;
344 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400345 return false;
346 }
347
348 if (!origSrcInfo.isValid()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400349 return false;
350 }
351
352 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
Stephen White3c0a50f2020-01-16 18:19:54 -0500353
354 if (dstProxy->framebufferOnly()) {
355 return false;
356 }
357
Adlai Hollerc95b5892020-08-11 12:02:22 -0400358 if (!dstProxy->instantiate(dContext->priv().resourceProvider())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400359 return false;
360 }
361
362 GrSurface* dstSurface = dstProxy->peekSurface();
363
Brian Salomon1d435302019-07-01 13:05:28 -0400364 auto srcInfo = origSrcInfo;
365 if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400366 return false;
367 }
Brian Salomon1047a492019-07-02 12:25:21 -0400368 // Our tight row bytes may have been changed by clipping.
369 tightRowBytes = srcInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400370
Mike Klein7321e6a2019-12-03 11:08:40 -0600371 SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{srcInfo, this->colorInfo()}.flags;
372 bool unpremul = flags.unpremul,
373 needColorConversion = flags.linearize || flags.gamut_transform || flags.encode,
374 premul = flags.premul;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400375
Adlai Hollerc95b5892020-08-11 12:02:22 -0400376 const GrCaps* caps = dContext->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400377
378 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
379 GrRenderable::kNo);
380
Greg Danielc71c7962020-01-14 16:44:18 -0500381 GrColorType dstColorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400382 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
383 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400384 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
385 (srcInfo.colorType() == GrColorType::kRGBA_8888 ||
386 srcInfo.colorType() == GrColorType::kBGRA_8888) &&
387 SkToBool(this->asRenderTargetContext()) &&
Greg Danielc71c7962020-01-14 16:44:18 -0500388 (dstColorType == GrColorType::kRGBA_8888 ||
389 dstColorType == GrColorType::kBGRA_8888) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400390 rgbaDefaultFormat.isValid() &&
Adlai Hollerc95b5892020-08-11 12:02:22 -0400391 dContext->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400392
393 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
Brian Salomond6287472019-06-24 15:50:07 -0400394 GrColorType colorType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400395
396 GrBackendFormat format;
Brian Salomone7499c72019-06-24 12:12:36 -0400397 SkAlphaType alphaType;
Greg Danielbfa19c42019-12-19 16:41:40 -0500398 GrSwizzle tempReadSwizzle;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400399 if (canvas2DFastPath) {
Brian Salomond6287472019-06-24 15:50:07 -0400400 colorType = GrColorType::kRGBA_8888;
Greg Daniel7bfc9132019-08-14 14:23:53 -0400401 format = rgbaDefaultFormat;
Brian Salomone7499c72019-06-24 12:12:36 -0400402 alphaType = kUnpremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400403 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400404 colorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400405 format = dstProxy->backendFormat().makeTexture2D();
406 if (!format.isValid()) {
407 return false;
408 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400409 alphaType = this->colorInfo().alphaType();
Greg Danielbfa19c42019-12-19 16:41:40 -0500410 tempReadSwizzle = this->readSwizzle();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400411 }
412
Greg Daniel2e52ad12019-06-13 10:04:16 -0400413 // It is more efficient for us to write pixels into a top left origin so we prefer that.
414 // However, if the final proxy isn't a render target then we must use a copy to move the
415 // data into it which requires the origins to match. If the final proxy is a render target
416 // we can use a draw instead which doesn't have this origin restriction. Thus for render
417 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400418 GrSurfaceOrigin tempOrigin =
Greg Danielb8d84f82020-02-13 14:25:00 -0500419 this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : this->origin();
Adlai Hollerc95b5892020-08-11 12:02:22 -0400420 auto tempProxy = dContext->priv().proxyProvider()->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400421 format, srcInfo.dimensions(), GrRenderable::kNo, 1, GrMipmapped::kNo,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400422 SkBackingFit::kApprox, SkBudgeted::kYes, GrProtected::kNo);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400423 if (!tempProxy) {
424 return false;
425 }
Greg Daniel3912a4b2020-01-14 09:56:04 -0500426 GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400427 GrSurfaceContext tempCtx(dContext, tempView, colorType, alphaType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500428 this->colorInfo().refColorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400429
430 // In the fast path we always write the srcData to the temp context as though it were RGBA.
431 // When the data is really BGRA the write will cause the R and B channels to be swapped in
432 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
433 // dst.
Brian Salomon1d435302019-07-01 13:05:28 -0400434 if (canvas2DFastPath) {
435 srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888);
436 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400437 if (!tempCtx.writePixels(dContext, srcInfo, src, rowBytes, {0, 0})) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400438 return false;
439 }
440
441 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400442 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400443 if (canvas2DFastPath) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400444 fp = dContext->priv().createUPMToPMEffect(
Greg Danield2ccbb52020-02-05 10:45:39 -0500445 GrTextureEffect::Make(std::move(tempView), alphaType));
Brian Salomon1d435302019-07-01 13:05:28 -0400446 // Important: check the original src color type here!
447 if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400448 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
449 }
450 } else {
Greg Danield2ccbb52020-02-05 10:45:39 -0500451 fp = GrTextureEffect::Make(std::move(tempView), alphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400452 }
453 if (!fp) {
454 return false;
455 }
456 GrPaint paint;
457 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
John Stiles5933d7d2020-07-21 12:28:35 -0400458 paint.setColorFragmentProcessor(std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400459 this->asRenderTargetContext()->fillRectToRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400460 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400461 SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()),
462 SkRect::MakeWH(srcInfo.width(), srcInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400463 } else {
Brian Salomon1d435302019-07-01 13:05:28 -0400464 SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height());
465 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Brian Salomonc5243782020-04-02 12:50:34 -0400466 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400467 return false;
468 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400469 }
470 return true;
471 }
472
Brian Salomon1d435302019-07-01 13:05:28 -0400473 GrColorType allowedColorType =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400474 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400475 dstProxy->backendFormat(),
476 srcInfo.colorType()).fColorType;
Greg Danielb8d84f82020-02-13 14:25:00 -0500477 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon1047a492019-07-02 12:25:21 -0400478 bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes;
479 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomon1d435302019-07-01 13:05:28 -0400480 (srcInfo.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400481
Brian Salomonf30b1c12019-06-20 12:25:02 -0400482 std::unique_ptr<char[]> tmpPixels;
Brian Salomon1d435302019-07-01 13:05:28 -0400483 GrColorType srcColorType = srcInfo.colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400484 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400485 GrImageInfo tmpInfo(allowedColorType, this->colorInfo().alphaType(),
486 this->colorInfo().refColorSpace(), srcInfo.width(), srcInfo.height());
Brian Salomon1d435302019-07-01 13:05:28 -0400487 auto tmpRB = tmpInfo.minRowBytes();
488 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400489
Brian Salomon1d435302019-07-01 13:05:28 -0400490 GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400491
Brian Salomon1d435302019-07-01 13:05:28 -0400492 srcColorType = tmpInfo.colorType();
493 rowBytes = tmpRB;
494 src = tmpPixels.get();
495 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400496 }
497
498 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
499 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
500 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
501 // destination proxy)
502 // TODO: should this policy decision just be moved into the drawing manager?
Adlai Hollerc95b5892020-08-11 12:02:22 -0400503 dContext->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400504
Adlai Hollerc95b5892020-08-11 12:02:22 -0400505 return dContext->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(),
506 srcInfo.height(), this->colorInfo().colorType(),
507 srcColorType, src, rowBytes);
Brian Osman45580d32016-11-23 09:37:01 -0500508}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400509
Adlai Hollerc95b5892020-08-11 12:02:22 -0400510void GrSurfaceContext::asyncRescaleAndReadPixels(GrDirectContext* dContext,
511 const SkImageInfo& info,
Brian Salomon63a0a752020-06-26 13:32:09 -0400512 const SkIRect& srcRect,
513 RescaleGamma rescaleGamma,
514 SkFilterQuality rescaleQuality,
515 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400516 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400517 // We implement this by rendering and we don't currently support rendering kUnpremul.
518 if (info.alphaType() == kUnpremul_SkAlphaType) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400519 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400520 return;
521 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400522 if (!dContext) {
523 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400524 return;
525 }
526 auto rt = this->asRenderTargetProxy();
527 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400528 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400529 return;
530 }
531 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400532 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400533 return;
534 }
535 auto dstCT = SkColorTypeToGrColorType(info.colorType());
536 if (dstCT == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400537 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400538 return;
539 }
540 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
541 auto colorTypeOfFinalContext = this->colorInfo().colorType();
542 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
543 if (needsRescale) {
544 colorTypeOfFinalContext = dstCT;
545 backendFormatOfFinalContext =
546 this->caps()->getDefaultBackendFormat(dstCT, GrRenderable::kYes);
547 }
548 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
549 backendFormatOfFinalContext, dstCT);
550 // Fail if we can't read from the source surface's color type.
551 if (readInfo.fColorType == GrColorType::kUnknown) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400552 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400553 return;
554 }
555 // Fail if read color type does not have all of dstCT's color channels and those missing color
556 // channels are in the src.
557 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
558 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
559 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
560 if ((~legalReadChannels & dstChannels) & srcChannels) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400561 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400562 return;
563 }
564
565 std::unique_ptr<GrRenderTargetContext> tempRTC;
566 int x = srcRect.fLeft;
567 int y = srcRect.fTop;
568 if (needsRescale) {
569 tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
570 rescaleQuality);
571 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400572 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400573 return;
574 }
575 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
576 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
577 x = y = 0;
578 } else {
579 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
580 this->colorInfo().alphaType(),
581 info.colorSpace(),
582 info.alphaType());
583 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
584 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
585 GrSurfaceProxyView texProxyView = this->readSurfaceView();
586 SkRect srcRectToDraw = SkRect::Make(srcRect);
587 // If the src is not texturable first try to make a copy to a texture.
588 if (!texProxyView.asTextureProxy()) {
589 texProxyView =
Brian Salomon7e67dca2020-07-21 09:27:25 -0400590 GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipmapped::kNo, srcRect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400591 SkBackingFit::kApprox, SkBudgeted::kNo);
592 if (!texProxyView) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400593 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400594 return;
595 }
596 SkASSERT(texProxyView.asTextureProxy());
597 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
598 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400599 tempRTC = GrRenderTargetContext::Make(dContext, this->colorInfo().colorType(),
Brian Salomon63a0a752020-06-26 13:32:09 -0400600 info.refColorSpace(), SkBackingFit::kApprox,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400601 srcRect.size(), 1, GrMipmapped::kNo,
Brian Salomon63a0a752020-06-26 13:32:09 -0400602 GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
603 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400604 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400605 return;
606 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400607 tempRTC->drawTexture(nullptr,
608 std::move(texProxyView),
609 this->colorInfo().alphaType(),
610 GrSamplerState::Filter::kNearest,
611 GrSamplerState::MipmapMode::kNone,
612 SkBlendMode::kSrc,
613 SK_PMColor4fWHITE,
614 srcRectToDraw,
615 SkRect::MakeWH(srcRect.width(), srcRect.height()),
616 GrAA::kNo,
617 GrQuadAAFlags::kNone,
618 SkCanvas::kFast_SrcRectConstraint,
619 SkMatrix::I(),
620 std::move(xform));
Brian Salomon63a0a752020-06-26 13:32:09 -0400621 x = y = 0;
622 }
623 }
624 auto rtc = tempRTC ? tempRTC.get() : this;
Adlai Hollerc95b5892020-08-11 12:02:22 -0400625 return rtc->asyncReadPixels(dContext, SkIRect::MakeXYWH(x, y, info.width(), info.height()),
626 info.colorType(), callback, callbackContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400627}
628
629class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult {
630public:
631 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
632 ~AsyncReadResult() override {
633 for (int i = 0; i < fPlanes.count(); ++i) {
634 if (!fPlanes[i].fMappedBuffer) {
635 delete[] static_cast<const char*>(fPlanes[i].fData);
636 } else {
637 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
638 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
639 }
640 }
641 }
642
643 int count() const override { return fPlanes.count(); }
644 const void* data(int i) const override { return fPlanes[i].fData; }
645 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
646
647 bool addTransferResult(const PixelTransferResult& result,
648 SkISize dimensions,
649 size_t rowBytes,
650 GrClientMappedBufferManager* manager) {
651 SkASSERT(!result.fTransferBuffer->isMapped());
652 const void* mappedData = result.fTransferBuffer->map();
653 if (!mappedData) {
654 return false;
655 }
656 if (result.fPixelConverter) {
657 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
658 result.fPixelConverter(convertedData.get(), mappedData);
659 this->addCpuPlane(std::move(convertedData), rowBytes);
660 result.fTransferBuffer->unmap();
661 } else {
662 manager->insert(result.fTransferBuffer);
663 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
664 }
665 return true;
666 }
667
668 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
669 SkASSERT(data);
670 SkASSERT(rowBytes > 0);
671 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
672 }
673
674private:
675 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
676 SkASSERT(data);
677 SkASSERT(rowBytes > 0);
678 SkASSERT(mappedBuffer);
679 SkASSERT(mappedBuffer->isMapped());
680 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
681 }
682
683 struct Plane {
684 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
685 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
686 const void* fData;
687 size_t fRowBytes;
688 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
689 sk_sp<GrGpuBuffer> fMappedBuffer;
690 };
691 SkSTArray<3, Plane> fPlanes;
692 uint32_t fInboxID;
693};
694
Adlai Hollerc95b5892020-08-11 12:02:22 -0400695void GrSurfaceContext::asyncReadPixels(GrDirectContext* dContext,
696 const SkIRect& rect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400697 SkColorType colorType,
698 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400699 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400700 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
701 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
702
Adlai Hollerc95b5892020-08-11 12:02:22 -0400703 if (!dContext || this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
704 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400705 return;
706 }
707
Adlai Hollerc95b5892020-08-11 12:02:22 -0400708 auto mappedBufferManager = dContext->priv().clientMappedBufferManager();
Brian Salomon63a0a752020-06-26 13:32:09 -0400709
710 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
711
712 if (!transferResult.fTransferBuffer) {
713 auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
714 this->colorInfo().refColorSpace());
715 auto result = std::make_unique<AsyncReadResult>(0);
716 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
717 SkPixmap pm(ii, data.get(), ii.minRowBytes());
718 result->addCpuPlane(std::move(data), pm.rowBytes());
719
Adlai Hollerc95b5892020-08-11 12:02:22 -0400720 SkIPoint pt{rect.fLeft, rect.fTop};
721 if (!this->readPixels(dContext, ii, pm.writable_addr(), pm.rowBytes(), pt)) {
722 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400723 return;
724 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400725 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400726 return;
727 }
728
729 struct FinishContext {
730 ReadPixelsCallback* fClientCallback;
731 ReadPixelsContext fClientContext;
732 SkISize fSize;
733 SkColorType fColorType;
734 GrClientMappedBufferManager* fMappedBufferManager;
735 PixelTransferResult fTransferResult;
736 };
737 // Assumption is that the caller would like to flush. We could take a parameter or require an
738 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
739 // callback to GrGpu until after the next flush that flushes our op list, though.
740 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400741 callbackContext,
Brian Salomon63a0a752020-06-26 13:32:09 -0400742 rect.size(),
743 colorType,
744 mappedBufferManager,
745 std::move(transferResult)};
746 auto finishCallback = [](GrGpuFinishedContext c) {
747 const auto* context = reinterpret_cast<const FinishContext*>(c);
748 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
749 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
750 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
751 context->fMappedBufferManager)) {
752 result.reset();
753 }
754 (*context->fClientCallback)(context->fClientContext, std::move(result));
755 delete context;
756 };
757 GrFlushInfo flushInfo;
758 flushInfo.fFinishedContext = finishContext;
759 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -0500760
761 dContext->priv().flushSurface(this->asSurfaceProxy(),
762 SkSurface::BackendSurfaceAccess::kNoAccess,
763 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -0400764}
765
Adlai Hollerc95b5892020-08-11 12:02:22 -0400766void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(GrDirectContext* dContext,
767 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400768 sk_sp<SkColorSpace> dstColorSpace,
769 const SkIRect& srcRect,
770 SkISize dstSize,
771 RescaleGamma rescaleGamma,
772 SkFilterQuality rescaleQuality,
773 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400774 ReadPixelsContext callbackContext) {
Brian Salomon63a0a752020-06-26 13:32:09 -0400775 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
776 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
777 SkASSERT(!dstSize.isZero());
778 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
779
Adlai Hollerc95b5892020-08-11 12:02:22 -0400780 if (!dContext) {
781 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400782 return;
783 }
784 auto rt = this->asRenderTargetProxy();
785 if (rt && rt->wrapsVkSecondaryCB()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400786 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400787 return;
788 }
789 if (rt && rt->framebufferOnly()) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400790 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400791 return;
792 }
793 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400794 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400795 return;
796 }
797 int x = srcRect.fLeft;
798 int y = srcRect.fTop;
799 bool needsRescale = srcRect.size() != dstSize;
800 GrSurfaceProxyView srcView;
801 if (needsRescale) {
802 // We assume the caller wants kPremul. There is no way to indicate a preference.
803 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
804 dstColorSpace);
805 // TODO: Incorporate the YUV conversion into last pass of rescaling.
806 auto tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
807 rescaleQuality);
808 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400809 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400810 return;
811 }
812 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
813 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
814 x = y = 0;
815 srcView = tempRTC->readSurfaceView();
816 } else {
817 srcView = this->readSurfaceView();
818 if (!srcView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400819 srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipmapped::kNo,
Brian Salomon63a0a752020-06-26 13:32:09 -0400820 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
821 if (!srcView) {
822 // If we can't get a texture copy of the contents then give up.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400823 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400824 return;
825 }
826 SkASSERT(srcView.asTextureProxy());
827 x = y = 0;
828 }
829 // We assume the caller wants kPremul. There is no way to indicate a preference.
830 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
831 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
832 kPremul_SkAlphaType);
833 if (xform) {
834 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
835 auto tempRTC = GrRenderTargetContext::Make(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400836 dContext, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400837 dstSize, 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400838 if (!tempRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400839 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400840 return;
841 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400842 tempRTC->drawTexture(nullptr,
843 std::move(srcView),
844 this->colorInfo().alphaType(),
845 GrSamplerState::Filter::kNearest,
846 GrSamplerState::MipmapMode::kNone,
847 SkBlendMode::kSrc,
848 SK_PMColor4fWHITE,
849 srcRectToDraw,
850 SkRect::Make(srcRect.size()),
851 GrAA::kNo,
852 GrQuadAAFlags::kNone,
853 SkCanvas::kFast_SrcRectConstraint,
854 SkMatrix::I(),
855 std::move(xform));
Brian Salomon63a0a752020-06-26 13:32:09 -0400856 srcView = tempRTC->readSurfaceView();
857 SkASSERT(srcView.asTextureProxy());
858 x = y = 0;
859 }
860 }
861
862 auto yRTC = GrRenderTargetContext::MakeWithFallback(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400863 dContext, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400864 GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400865 int halfW = dstSize.width() /2;
866 int halfH = dstSize.height()/2;
867 auto uRTC = GrRenderTargetContext::MakeWithFallback(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400868 dContext, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH},
869 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400870 auto vRTC = GrRenderTargetContext::MakeWithFallback(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400871 dContext, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH},
872 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon63a0a752020-06-26 13:32:09 -0400873 if (!yRTC || !uRTC || !vRTC) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400874 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400875 return;
876 }
877
878 float baseM[20];
879 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
880
881 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
882
883 auto texMatrix = SkMatrix::Translate(x, y);
884
885 SkRect dstRectY = SkRect::Make(dstSize);
886 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
887
888 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
889 PixelTransferResult yTransfer, uTransfer, vTransfer;
890
891 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
892 float yM[20];
893 std::fill_n(yM, 15, 0.f);
894 std::copy_n(baseM + 0, 5, yM + 15);
895 GrPaint yPaint;
896 auto yTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
897 auto yColFP = GrColorMatrixFragmentProcessor::Make(std::move(yTexFP), yM,
898 /*unpremulInput=*/false,
899 /*clampRGBOutput=*/true,
900 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400901 yPaint.setColorFragmentProcessor(std::move(yColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400902 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
903 yRTC->fillRectToRect(nullptr, std::move(yPaint), GrAA::kNo, SkMatrix::I(), dstRectY, dstRectY);
904 if (!doSynchronousRead) {
905 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
906 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
907 if (!yTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400908 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400909 return;
910 }
911 }
912
913 texMatrix.preScale(2.f, 2.f);
914 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
915 float uM[20];
916 std::fill_n(uM, 15, 0.f);
917 std::copy_n(baseM + 5, 5, uM + 15);
918 GrPaint uPaint;
919 auto uTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix,
Brian Salomona3b02f52020-07-15 16:02:01 -0400920 GrSamplerState::Filter::kLinear);
Brian Salomon63a0a752020-06-26 13:32:09 -0400921 auto uColFP = GrColorMatrixFragmentProcessor::Make(std::move(uTexFP), uM,
922 /*unpremulInput=*/false,
923 /*clampRGBOutput=*/true,
924 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400925 uPaint.setColorFragmentProcessor(std::move(uColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400926 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
927 uRTC->fillRectToRect(nullptr, std::move(uPaint), GrAA::kNo, SkMatrix::I(), dstRectUV,
928 dstRectUV);
929 if (!doSynchronousRead) {
930 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
931 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
932 if (!uTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400933 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400934 return;
935 }
936 }
937
938 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
939 float vM[20];
940 std::fill_n(vM, 15, 0.f);
941 std::copy_n(baseM + 10, 5, vM + 15);
942 GrPaint vPaint;
943 auto vTexFP = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType(),
Brian Salomona3b02f52020-07-15 16:02:01 -0400944 texMatrix, GrSamplerState::Filter::kLinear);
Brian Salomon63a0a752020-06-26 13:32:09 -0400945 auto vColFP = GrColorMatrixFragmentProcessor::Make(std::move(vTexFP), vM,
946 /*unpremulInput=*/false,
947 /*clampRGBOutput=*/true,
948 /*premulOutput=*/false);
John Stiles5933d7d2020-07-21 12:28:35 -0400949 vPaint.setColorFragmentProcessor(std::move(vColFP));
Brian Salomon63a0a752020-06-26 13:32:09 -0400950 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
951 vRTC->fillRectToRect(nullptr, std::move(vPaint), GrAA::kNo, SkMatrix::I(), dstRectUV,
952 dstRectUV);
953 if (!doSynchronousRead) {
954 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
955 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
956 if (!vTransfer.fTransferBuffer) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400957 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400958 return;
959 }
960 }
961
962 if (doSynchronousRead) {
963 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
964 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
965 size_t yRB = yInfo.minRowBytes();
966 size_t uvRB = uvInfo.minRowBytes();
967 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
968 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
969 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400970 if (!yRTC->readPixels(dContext, yInfo, y.get(), yRB, {0, 0}) ||
971 !uRTC->readPixels(dContext, uvInfo, u.get(), uvRB, {0, 0}) ||
972 !vRTC->readPixels(dContext, uvInfo, v.get(), uvRB, {0, 0})) {
973 callback(callbackContext, nullptr);
Brian Salomon63a0a752020-06-26 13:32:09 -0400974 return;
975 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400976 auto result = std::make_unique<AsyncReadResult>(dContext->priv().contextID());
Brian Salomon63a0a752020-06-26 13:32:09 -0400977 result->addCpuPlane(std::move(y), yRB );
978 result->addCpuPlane(std::move(u), uvRB);
979 result->addCpuPlane(std::move(v), uvRB);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400980 callback(callbackContext, std::move(result));
Brian Salomon63a0a752020-06-26 13:32:09 -0400981 return;
982 }
983
984 struct FinishContext {
985 ReadPixelsCallback* fClientCallback;
986 ReadPixelsContext fClientContext;
987 GrClientMappedBufferManager* fMappedBufferManager;
988 SkISize fSize;
989 PixelTransferResult fYTransfer;
990 PixelTransferResult fUTransfer;
991 PixelTransferResult fVTransfer;
992 };
993 // Assumption is that the caller would like to flush. We could take a parameter or require an
994 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
995 // callback to GrGpu until after the next flush that flushes our op list, though.
996 auto* finishContext = new FinishContext{callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400997 callbackContext,
998 dContext->priv().clientMappedBufferManager(),
Brian Salomon63a0a752020-06-26 13:32:09 -0400999 dstSize,
1000 std::move(yTransfer),
1001 std::move(uTransfer),
1002 std::move(vTransfer)};
1003 auto finishCallback = [](GrGpuFinishedContext c) {
1004 const auto* context = reinterpret_cast<const FinishContext*>(c);
1005 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
1006 auto manager = context->fMappedBufferManager;
1007 size_t rowBytes = SkToSizeT(context->fSize.width());
1008 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
1009 (*context->fClientCallback)(context->fClientContext, nullptr);
1010 delete context;
1011 return;
1012 }
1013 rowBytes /= 2;
1014 SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2};
1015 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
1016 (*context->fClientCallback)(context->fClientContext, nullptr);
1017 delete context;
1018 return;
1019 }
1020 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
1021 (*context->fClientCallback)(context->fClientContext, nullptr);
1022 delete context;
1023 return;
1024 }
1025 (*context->fClientCallback)(context->fClientContext, std::move(result));
1026 delete context;
1027 };
1028 GrFlushInfo flushInfo;
1029 flushInfo.fFinishedContext = finishContext;
1030 flushInfo.fFinishedProc = finishCallback;
Robert Phillips80bfda82020-11-12 09:23:36 -05001031 dContext->priv().flushSurface(this->asSurfaceProxy(),
1032 SkSurface::BackendSurfaceAccess::kNoAccess,
1033 flushInfo);
Brian Salomon63a0a752020-06-26 13:32:09 -04001034}
1035
Brian Salomonc5243782020-04-02 12:50:34 -04001036bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001037 ASSERT_SINGLE_OWNER
1038 RETURN_FALSE_IF_ABANDONED
1039 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -04001040 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -04001041
Brian Salomon947efe22019-07-16 15:36:11 -04001042 const GrCaps* caps = fContext->priv().caps();
1043
Greg Daniel46cfbc62019-06-07 11:43:30 -04001044 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
Greg Danielc71c7962020-01-14 16:44:18 -05001045 SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat());
Greg Daniel46cfbc62019-06-07 11:43:30 -04001046
Stephen White3c0a50f2020-01-16 18:19:54 -05001047 if (this->asSurfaceProxy()->framebufferOnly()) {
1048 return false;
1049 }
1050
Chris Daltonf8e5aad2019-08-02 12:55:23 -06001051 if (!caps->canCopySurface(this->asSurfaceProxy(), src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -04001052 return false;
1053 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001054
Greg Daniel16f5c652019-10-29 11:26:01 -04001055 // The swizzle doesn't matter for copies and it is not used.
1056 return this->drawingManager()->newCopyRenderTask(
Brian Salomonc5243782020-04-02 12:50:34 -04001057 GrSurfaceProxyView(sk_ref_sp(src), this->origin(), GrSwizzle("rgba")), srcRect,
Greg Daniel46e366a2019-12-16 14:38:36 -05001058 this->readSurfaceView(), dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001059}
Greg Daniel46cfbc62019-06-07 11:43:30 -04001060
Brian Salomon63a0a752020-06-26 13:32:09 -04001061std::unique_ptr<GrRenderTargetContext> GrSurfaceContext::rescale(const GrImageInfo& info,
1062 GrSurfaceOrigin origin,
1063 SkIRect srcRect,
1064 RescaleGamma rescaleGamma,
1065 SkFilterQuality rescaleQuality) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001066 auto rtProxy = this->asRenderTargetProxy();
1067 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1068 return nullptr;
1069 }
1070
Stephen White3c0a50f2020-01-16 18:19:54 -05001071 if (this->asSurfaceProxy()->framebufferOnly()) {
1072 return nullptr;
1073 }
1074
Brian Salomone9ad9982019-07-22 16:17:41 -04001075 // We rescale by drawing and don't currently support drawing to a kUnpremul destination.
1076 if (info.alphaType() == kUnpremul_SkAlphaType) {
1077 return nullptr;
1078 }
1079
Greg Daniel40903af2020-01-30 14:55:05 -05001080 GrSurfaceProxyView texView = this->readSurfaceView();
Brian Salomonfc118442019-11-22 19:09:27 -05001081 SkAlphaType srcAlphaType = this->colorInfo().alphaType();
Greg Daniel40903af2020-01-30 14:55:05 -05001082 if (!texView.asTextureProxy()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -04001083 texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect,
Brian Salomonc5243782020-04-02 12:50:34 -04001084 SkBackingFit::kApprox, SkBudgeted::kNo);
1085 if (!texView) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001086 return nullptr;
1087 }
Greg Daniel40903af2020-01-30 14:55:05 -05001088 SkASSERT(texView.asTextureProxy());
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001089 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001090 }
1091
Brian Salomonbf6b9792019-08-21 09:38:10 -04001092 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
1093 // pass B is moved to A. If 'this' is the input on the first pass then tempA is null.
1094 std::unique_ptr<GrRenderTargetContext> tempA;
1095 std::unique_ptr<GrRenderTargetContext> tempB;
1096
Brian Salomone9ad9982019-07-22 16:17:41 -04001097 // Assume we should ignore the rescale linear request if the surface has no color space since
1098 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon63a0a752020-06-26 13:32:09 -04001099 if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() &&
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001100 !this->colorInfo().colorSpace()->gammaIsLinear()) {
1101 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
Brian Salomonfc118442019-11-22 19:09:27 -05001102 auto xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(), srcAlphaType, cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -04001103 kPremul_SkAlphaType);
1104 // We'll fall back to kRGBA_8888 if half float not supported.
Greg Daniele20fcad2020-01-08 11:52:34 -05001105 auto linearRTC = GrRenderTargetContext::MakeWithFallback(
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001106 fContext, GrColorType::kRGBA_F16, cs, SkBackingFit::kApprox, srcRect.size(), 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001107 GrMipmapped::kNo, GrProtected::kNo, origin);
Brian Salomone9ad9982019-07-22 16:17:41 -04001108 if (!linearRTC) {
1109 return nullptr;
1110 }
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001111 // 1-to-1 draw can always be kFast.
Brian Salomone69b9ef2020-07-22 11:18:06 -04001112 linearRTC->drawTexture(nullptr,
1113 std::move(texView),
1114 srcAlphaType,
1115 GrSamplerState::Filter::kNearest,
1116 GrSamplerState::MipmapMode::kNone,
1117 SkBlendMode::kSrc,
1118 SK_PMColor4fWHITE,
1119 SkRect::Make(srcRect),
1120 SkRect::Make(srcRect.size()),
1121 GrAA::kNo,
1122 GrQuadAAFlags::kNone,
1123 SkCanvas::kFast_SrcRectConstraint,
1124 SkMatrix::I(),
1125 std::move(xform));
Greg Daniel40903af2020-01-30 14:55:05 -05001126 texView = linearRTC->readSurfaceView();
1127 SkASSERT(texView.asTextureProxy());
Brian Salomonbf6b9792019-08-21 09:38:10 -04001128 tempA = std::move(linearRTC);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001129 srcRect = SkIRect::MakeSize(srcRect.size());
Brian Salomone9ad9982019-07-22 16:17:41 -04001130 }
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001131
Brian Salomon9c6f6bf2020-05-29 16:37:26 -04001132 while (srcRect.size() != info.dimensions()) {
Brian Salomon59f31b12020-06-04 17:27:15 -04001133 SkISize nextDims = info.dimensions();
1134 if (rescaleQuality != kNone_SkFilterQuality) {
1135 if (srcRect.width() > info.width()) {
1136 nextDims.fWidth = std::max((srcRect.width() + 1)/2, info.width());
1137 } else if (srcRect.width() < info.width()) {
1138 nextDims.fWidth = std::min(srcRect.width()*2, info.width());
1139 }
1140 if (srcRect.height() > info.height()) {
1141 nextDims.fHeight = std::max((srcRect.height() + 1)/2, info.height());
1142 } else if (srcRect.height() < info.height()) {
1143 nextDims.fHeight = std::min(srcRect.height()*2, info.height());
1144 }
1145 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001146 auto input = tempA ? tempA.get() : this;
Brian Salomon59f31b12020-06-04 17:27:15 -04001147 GrColorType colorType = input->colorInfo().colorType();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001148 auto cs = input->colorInfo().refColorSpace();
Brian Salomone9ad9982019-07-22 16:17:41 -04001149 sk_sp<GrColorSpaceXform> xform;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001150 auto prevAlphaType = input->colorInfo().alphaType();
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001151 if (nextDims == info.dimensions()) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001152 // Might as well fold conversion to final info in the last step.
1153 cs = info.refColorSpace();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001154 xform = GrColorSpaceXform::Make(input->colorInfo().colorSpace(),
1155 input->colorInfo().alphaType(), cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -04001156 info.alphaType());
1157 }
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001158 tempB = GrRenderTargetContext::MakeWithFallback(fContext, colorType, std::move(cs),
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001159 SkBackingFit::kApprox, nextDims, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001160 GrMipmapped::kNo, GrProtected::kNo, origin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001161 if (!tempB) {
Brian Salomone9ad9982019-07-22 16:17:41 -04001162 return nullptr;
1163 }
Brian Salomon59f31b12020-06-04 17:27:15 -04001164 auto dstRect = SkRect::Make(nextDims);
1165 if (rescaleQuality == kHigh_SkFilterQuality) {
1166 SkMatrix matrix;
1167 matrix.setScaleTranslate((float)srcRect.width()/nextDims.width(),
1168 (float)srcRect.height()/nextDims.height(),
1169 srcRect.x(),
1170 srcRect.y());
1171 std::unique_ptr<GrFragmentProcessor> fp;
1172 auto dir = GrBicubicEffect::Direction::kXY;
1173 if (nextDims.width() == srcRect.width()) {
1174 dir = GrBicubicEffect::Direction::kY;
1175 } else if (nextDims.height() == srcRect.height()) {
1176 dir = GrBicubicEffect::Direction::kX;
Brian Salomone9ad9982019-07-22 16:17:41 -04001177 }
Brian Salomon1af72d12020-06-25 10:47:26 -04001178 static constexpr auto kWM = GrSamplerState::WrapMode::kClamp;
Mike Reed3867c702020-09-01 13:28:10 -04001179 static constexpr auto kKernel = GrBicubicEffect::gCatmullRom;
Brian Salomon59f31b12020-06-04 17:27:15 -04001180 fp = GrBicubicEffect::MakeSubset(std::move(texView), prevAlphaType, matrix, kWM, kWM,
Brian Salomon1af72d12020-06-25 10:47:26 -04001181 SkRect::Make(srcRect), kKernel, dir, *this->caps());
Brian Salomon59f31b12020-06-04 17:27:15 -04001182 if (xform) {
1183 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -04001184 }
Brian Salomon59f31b12020-06-04 17:27:15 -04001185 GrPaint paint;
John Stiles5933d7d2020-07-21 12:28:35 -04001186 paint.setColorFragmentProcessor(std::move(fp));
Brian Salomon59f31b12020-06-04 17:27:15 -04001187 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
1188 tempB->fillRectToRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect,
1189 dstRect);
1190 } else {
1191 auto filter = rescaleQuality == kNone_SkFilterQuality ? GrSamplerState::Filter::kNearest
Brian Salomona3b02f52020-07-15 16:02:01 -04001192 : GrSamplerState::Filter::kLinear;
Brian Salomon59f31b12020-06-04 17:27:15 -04001193 // Minimizing draw with integer coord src and dev rects can always be kFast.
1194 auto constraint = SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint;
1195 if (nextDims.width() <= srcRect.width() && nextDims.height() <= srcRect.height()) {
1196 constraint = SkCanvas::SrcRectConstraint::kFast_SrcRectConstraint;
1197 }
Brian Salomone69b9ef2020-07-22 11:18:06 -04001198 tempB->drawTexture(nullptr,
1199 std::move(texView),
1200 srcAlphaType,
1201 filter,
1202 GrSamplerState::MipmapMode::kNone,
1203 SkBlendMode::kSrc,
1204 SK_PMColor4fWHITE,
1205 SkRect::Make(srcRect),
1206 dstRect,
1207 GrAA::kNo,
1208 GrQuadAAFlags::kNone,
1209 constraint,
1210 SkMatrix::I(),
1211 std::move(xform));
Brian Salomone9ad9982019-07-22 16:17:41 -04001212 }
Greg Daniel40903af2020-01-30 14:55:05 -05001213 texView = tempB->readSurfaceView();
Brian Salomonbf6b9792019-08-21 09:38:10 -04001214 tempA = std::move(tempB);
Brian Salomonafd8a6c2020-05-21 12:10:54 -04001215 srcRect = SkIRect::MakeSize(nextDims);
Brian Salomone9ad9982019-07-22 16:17:41 -04001216 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001217 SkASSERT(tempA);
1218 return tempA;
Brian Salomone9ad9982019-07-22 16:17:41 -04001219}
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001220
1221GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
1222 const SkIRect& rect) {
1223 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1224 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001225 auto direct = fContext->asDirectContext();
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001226 if (!direct) {
1227 return {};
1228 }
1229 auto rtProxy = this->asRenderTargetProxy();
1230 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
1231 return {};
1232 }
1233
1234 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001235 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
1236 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001237 // Fail if read color type does not have all of dstCT's color channels and those missing color
1238 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001239 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1240 uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType);
1241 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1242 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001243 return {};
1244 }
1245
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001246 if (!this->caps()->transferFromSurfaceToBufferSupport() ||
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001247 !supportedRead.fOffsetAlignmentForTransferBuffer) {
1248 return {};
1249 }
1250
1251 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
1252 size_t size = rowBytes * rect.height();
1253 auto buffer = direct->priv().resourceProvider()->createBuffer(
1254 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
1255 if (!buffer) {
1256 return {};
1257 }
1258 auto srcRect = rect;
Greg Danielb8d84f82020-02-13 14:25:00 -05001259 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001260 if (flip) {
1261 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1262 this->height() - rect.fTop);
1263 }
Greg Danielbbfec9d2019-08-20 10:56:51 -04001264 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001265 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -04001266 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001267 PixelTransferResult result;
1268 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001269 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -04001270 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001271 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
1272 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001273 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
1274 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001275 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
1276 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -04001277 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -04001278 };
1279 }
1280 return result;
1281}
Greg Daniel46e366a2019-12-16 14:38:36 -05001282
1283#ifdef SK_DEBUG
1284void GrSurfaceContext::validate() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001285 SkASSERT(fReadView.proxy());
1286 fReadView.proxy()->validate(fContext);
Brian Salomonc5243782020-04-02 12:50:34 -04001287 if (this->colorInfo().colorType() != GrColorType::kUnknown) {
1288 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
1289 this->colorInfo().colorType(), fReadView.proxy()->backendFormat()));
1290 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001291 this->onValidate();
1292}
1293#endif