1 /*
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
8 #include "src/gpu/GrSurfaceContext.h"
9
10 #include "include/private/GrRecordingContext.h"
11 #include "src/core/SkAutoPixmapStorage.h"
12 #include "src/gpu/GrAuditTrail.h"
13 #include "src/gpu/GrClip.h"
14 #include "src/gpu/GrContextPriv.h"
15 #include "src/gpu/GrDataUtils.h"
16 #include "src/gpu/GrDrawingManager.h"
17 #include "src/gpu/GrGpu.h"
18 #include "src/gpu/GrOpList.h"
19 #include "src/gpu/GrRecordingContextPriv.h"
20 #include "src/gpu/GrRenderTargetContext.h"
21 #include "src/gpu/GrSurfaceContextPriv.h"
22 #include "src/gpu/GrSurfacePriv.h"
23 #include "src/gpu/GrTextureContext.h"
24 #include "src/gpu/SkGr.h"
25 #include "src/gpu/effects/GrBicubicEffect.h"
26
27 #define ASSERT_SINGLE_OWNER \
28 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
29 #define RETURN_FALSE_IF_ABANDONED if (this->fContext->priv().abandoned()) { return false; }
30
31 // In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
32 // GrOpLists to be picked up and added to by renderTargetContexts lower in the call
33 // stack. When this occurs with a closed GrOpList, a new one will be allocated
34 // when the renderTargetContext attempts to use it (via getOpList).
GrSurfaceContext(GrRecordingContext * context,GrColorType colorType,SkAlphaType alphaType,sk_sp<SkColorSpace> colorSpace)35 GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
36 GrColorType colorType,
37 SkAlphaType alphaType,
38 sk_sp<SkColorSpace> colorSpace)
39 : fContext(context), fColorSpaceInfo(colorType, alphaType, std::move(colorSpace)) {}
40
caps() const41 const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); }
42
auditTrail()43 GrAuditTrail* GrSurfaceContext::auditTrail() {
44 return fContext->priv().auditTrail();
45 }
46
drawingManager()47 GrDrawingManager* GrSurfaceContext::drawingManager() {
48 return fContext->priv().drawingManager();
49 }
50
drawingManager() const51 const GrDrawingManager* GrSurfaceContext::drawingManager() const {
52 return fContext->priv().drawingManager();
53 }
54
55 #ifdef SK_DEBUG
singleOwner()56 GrSingleOwner* GrSurfaceContext::singleOwner() {
57 return fContext->priv().singleOwner();
58 }
59 #endif
60
readPixels(const GrPixelInfo & origDstInfo,void * dst,size_t rowBytes,SkIPoint pt,GrContext * direct)61 bool GrSurfaceContext::readPixels(const GrPixelInfo& origDstInfo, void* dst, size_t rowBytes,
62 SkIPoint pt, GrContext* direct) {
63 ASSERT_SINGLE_OWNER
64 RETURN_FALSE_IF_ABANDONED
65 SkDEBUGCODE(this->validate();)
66 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
67
68 if (!direct && !(direct = fContext->priv().asDirectContext())) {
69 return false;
70 }
71
72 if (!dst) {
73 return false;
74 }
75
76 size_t tightRowBytes = origDstInfo.minRowBytes();
77 if (!rowBytes) {
78 rowBytes = tightRowBytes;
79 } else if (rowBytes < tightRowBytes) {
80 return false;
81 }
82
83 if (!origDstInfo.isValid()) {
84 return false;
85 }
86
87 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
88
89 // MDB TODO: delay this instantiation until later in the method
90 if (!srcProxy->instantiate(direct->priv().resourceProvider())) {
91 return false;
92 }
93
94 GrSurface* srcSurface = srcProxy->peekSurface();
95
96 auto dstInfo = origDstInfo;
97 if (!dstInfo.clip(this->width(), this->height(), &pt, &dst, rowBytes)) {
98 return false;
99 }
100 // Our tight row bytes may have been changed by clipping.
101 tightRowBytes = dstInfo.minRowBytes();
102
103 bool premul = this->colorSpaceInfo().alphaType() == kUnpremul_SkAlphaType &&
104 dstInfo.alphaType() == kPremul_SkAlphaType;
105 bool unpremul = this->colorSpaceInfo().alphaType() == kPremul_SkAlphaType &&
106 dstInfo.alphaType() == kUnpremul_SkAlphaType;
107
108 bool needColorConversion = SkColorSpaceXformSteps::Required(this->colorSpaceInfo().colorSpace(),
109 dstInfo.colorSpace());
110
111 const GrCaps* caps = direct->priv().caps();
112 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
113 // care so much about getImageData performance. However, in order to ensure putImageData/
114 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
115 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
116 // fContext->vaildaPMUPMConversionExists()).
117 GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
118 GrRenderable::kYes);
119 bool canvas2DFastPath = unpremul && !needColorConversion &&
120 (GrColorType::kRGBA_8888 == dstInfo.colorType() ||
121 GrColorType::kBGRA_8888 == dstInfo.colorType()) &&
122 SkToBool(srcProxy->asTextureProxy()) &&
123 (srcProxy->config() == kRGBA_8888_GrPixelConfig ||
124 srcProxy->config() == kBGRA_8888_GrPixelConfig) &&
125 defaultRGBAFormat.isValid() &&
126 direct->priv().validPMUPMConversionExists();
127
128 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
129 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
130 return false;
131 }
132
133 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
134 GrColorType colorType = canvas2DFastPath ? GrColorType::kRGBA_8888
135 : this->colorSpaceInfo().colorType();
136 sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr
137 : this->colorSpaceInfo().refColorSpace();
138
139 sk_sp<GrRenderTargetContext> tempCtx = direct->priv().makeDeferredRenderTargetContext(
140 SkBackingFit::kApprox, dstInfo.width(), dstInfo.height(), colorType, std::move(cs),
141 1, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin, nullptr, SkBudgeted::kYes);
142 if (!tempCtx) {
143 return false;
144 }
145
146 std::unique_ptr<GrFragmentProcessor> fp;
147 if (canvas2DFastPath) {
148 fp = direct->priv().createPMToUPMEffect(
149 GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()),
150 SkMatrix::I()));
151 if (dstInfo.colorType() == GrColorType::kBGRA_8888) {
152 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
153 dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888);
154 }
155 // The render target context is incorrectly tagged as kPremul even though we're writing
156 // unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type so we don't
157 // double unpremul.
158 dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType);
159 } else {
160 fp = GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()), SkMatrix::I());
161 }
162 if (!fp) {
163 return false;
164 }
165 GrPaint paint;
166 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
167 paint.addColorFragmentProcessor(std::move(fp));
168
169 tempCtx->asRenderTargetContext()->fillRectToRect(
170 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
171 SkRect::MakeWH(dstInfo.width(), dstInfo.height()),
172 SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height()));
173
174 return tempCtx->readPixels(dstInfo, dst, rowBytes, {0, 0}, direct);
175 }
176
177 bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin;
178
179 auto supportedRead = caps->supportedReadPixelsColorType(
180 this->colorSpaceInfo().colorType(), srcProxy->backendFormat(), dstInfo.colorType());
181
182 bool makeTight = !caps->readPixelsRowBytesSupport() && tightRowBytes != rowBytes;
183
184 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
185 (dstInfo.colorType() != supportedRead.fColorType);
186
187 std::unique_ptr<char[]> tmpPixels;
188 GrPixelInfo tmpInfo;
189 void* readDst = dst;
190 size_t readRB = rowBytes;
191 if (convert) {
192 tmpInfo = {supportedRead.fColorType, this->colorSpaceInfo().alphaType(),
193 this->colorSpaceInfo().refColorSpace(), dstInfo.width(), dstInfo.height()};
194 size_t tmpRB = tmpInfo.minRowBytes();
195 size_t size = tmpRB * tmpInfo.height();
196 // Chrome MSAN bots require the data to be initialized (hence the ()).
197 tmpPixels.reset(new char[size]());
198
199 readDst = tmpPixels.get();
200 readRB = tmpRB;
201 pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY;
202 }
203
204 direct->priv().flushSurface(srcProxy);
205
206 if (!direct->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(),
207 dstInfo.height(), this->colorSpaceInfo().colorType(),
208 supportedRead.fColorType, readDst, readRB)) {
209 return false;
210 }
211
212 if (convert) {
213 return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip);
214 }
215 return true;
216 }
217
writePixels(const GrPixelInfo & origSrcInfo,const void * src,size_t rowBytes,SkIPoint pt,GrContext * direct)218 bool GrSurfaceContext::writePixels(const GrPixelInfo& origSrcInfo, const void* src, size_t rowBytes,
219 SkIPoint pt, GrContext* direct) {
220 ASSERT_SINGLE_OWNER
221 RETURN_FALSE_IF_ABANDONED
222 SkDEBUGCODE(this->validate();)
223 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
224
225 if (!direct && !(direct = fContext->priv().asDirectContext())) {
226 return false;
227 }
228
229 if (this->asSurfaceProxy()->readOnly()) {
230 return false;
231 }
232
233 if (!src) {
234 return false;
235 }
236
237 size_t tightRowBytes = origSrcInfo.minRowBytes();
238 if (!rowBytes) {
239 rowBytes = tightRowBytes;
240 } else if (rowBytes < tightRowBytes) {
241 return false;
242 }
243
244 if (!origSrcInfo.isValid()) {
245 return false;
246 }
247
248 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
249 if (!dstProxy->instantiate(direct->priv().resourceProvider())) {
250 return false;
251 }
252
253 GrSurface* dstSurface = dstProxy->peekSurface();
254
255 auto srcInfo = origSrcInfo;
256 if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) {
257 return false;
258 }
259 // Our tight row bytes may have been changed by clipping.
260 tightRowBytes = srcInfo.minRowBytes();
261
262 bool premul = this->colorSpaceInfo().alphaType() == kPremul_SkAlphaType &&
263 srcInfo.alphaType() == kUnpremul_SkAlphaType;
264 bool unpremul = this->colorSpaceInfo().alphaType() == kUnpremul_SkAlphaType &&
265 srcInfo.alphaType() == kPremul_SkAlphaType;
266
267 bool needColorConversion = SkColorSpaceXformSteps::Required(
268 srcInfo.colorSpace(), this->colorSpaceInfo().colorSpace());
269
270 const GrCaps* caps = direct->priv().caps();
271
272 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
273 GrRenderable::kNo);
274
275 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
276 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
277 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
278 (srcInfo.colorType() == GrColorType::kRGBA_8888 ||
279 srcInfo.colorType() == GrColorType::kBGRA_8888) &&
280 SkToBool(this->asRenderTargetContext()) &&
281 (dstProxy->config() == kRGBA_8888_GrPixelConfig ||
282 dstProxy->config() == kBGRA_8888_GrPixelConfig) &&
283 rgbaDefaultFormat.isValid() &&
284 direct->priv().validPMUPMConversionExists();
285
286 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
287 GrSurfaceDesc desc;
288 desc.fWidth = srcInfo.width();
289 desc.fHeight = srcInfo.height();
290 GrColorType colorType;
291
292 GrBackendFormat format;
293 SkAlphaType alphaType;
294 if (canvas2DFastPath) {
295 desc.fConfig = kRGBA_8888_GrPixelConfig;
296 colorType = GrColorType::kRGBA_8888;
297 format = rgbaDefaultFormat;
298 alphaType = kUnpremul_SkAlphaType;
299 } else {
300 desc.fConfig = dstProxy->config();
301 colorType = this->colorSpaceInfo().colorType();
302 format = dstProxy->backendFormat().makeTexture2D();
303 if (!format.isValid()) {
304 return false;
305 }
306 alphaType = this->colorSpaceInfo().alphaType();
307 }
308
309 // It is more efficient for us to write pixels into a top left origin so we prefer that.
310 // However, if the final proxy isn't a render target then we must use a copy to move the
311 // data into it which requires the origins to match. If the final proxy is a render target
312 // we can use a draw instead which doesn't have this origin restriction. Thus for render
313 // targets we will use top left and otherwise we will make the origins match.
314 GrSurfaceOrigin tempOrigin =
315 this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : dstProxy->origin();
316 auto tempProxy = direct->priv().proxyProvider()->createProxy(
317 format, desc, GrRenderable::kNo, 1, tempOrigin, SkBackingFit::kApprox,
318 SkBudgeted::kYes, GrProtected::kNo);
319
320 if (!tempProxy) {
321 return false;
322 }
323 auto tempCtx = direct->priv().drawingManager()->makeTextureContext(
324 tempProxy, colorType, alphaType, this->colorSpaceInfo().refColorSpace());
325 if (!tempCtx) {
326 return false;
327 }
328
329 // In the fast path we always write the srcData to the temp context as though it were RGBA.
330 // When the data is really BGRA the write will cause the R and B channels to be swapped in
331 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
332 // dst.
333 if (canvas2DFastPath) {
334 srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888);
335 }
336 if (!tempCtx->writePixels(srcInfo, src, rowBytes, {0, 0}, direct)) {
337 return false;
338 }
339
340 if (this->asRenderTargetContext()) {
341 std::unique_ptr<GrFragmentProcessor> fp;
342 if (canvas2DFastPath) {
343 fp = direct->priv().createUPMToPMEffect(
344 GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()));
345 // Important: check the original src color type here!
346 if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) {
347 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
348 }
349 } else {
350 fp = GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I());
351 }
352 if (!fp) {
353 return false;
354 }
355 GrPaint paint;
356 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
357 paint.addColorFragmentProcessor(std::move(fp));
358 this->asRenderTargetContext()->fillRectToRect(
359 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
360 SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()),
361 SkRect::MakeWH(srcInfo.width(), srcInfo.height()));
362 } else {
363 SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height());
364 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
365 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
366 return false;
367 }
368 }
369 return true;
370 }
371
372 GrColorType allowedColorType =
373 caps->supportedWritePixelsColorType(this->colorSpaceInfo().colorType(),
374 dstProxy->backendFormat(),
375 srcInfo.colorType()).fColorType;
376 bool flip = dstProxy->origin() == kBottomLeft_GrSurfaceOrigin;
377 bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes;
378 bool convert = premul || unpremul || needColorConversion || makeTight ||
379 (srcInfo.colorType() != allowedColorType) || flip;
380
381 std::unique_ptr<char[]> tmpPixels;
382 GrColorType srcColorType = srcInfo.colorType();
383 if (convert) {
384 GrPixelInfo tmpInfo(allowedColorType, this->colorSpaceInfo().alphaType(),
385 this->colorSpaceInfo().refColorSpace(), srcInfo.width(),
386 srcInfo.height());
387 auto tmpRB = tmpInfo.minRowBytes();
388 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
389
390 GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip);
391
392 srcColorType = tmpInfo.colorType();
393 rowBytes = tmpRB;
394 src = tmpPixels.get();
395 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
396 }
397
398 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
399 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
400 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
401 // destination proxy)
402 // TODO: should this policy decision just be moved into the drawing manager?
403 direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
404
405 return direct->priv().getGpu()->writePixels(
406 dstSurface, pt.fX, pt.fY, srcInfo.width(), srcInfo.height(),
407 this->colorSpaceInfo().colorType(), srcColorType, src, rowBytes);
408 }
409
copy(GrSurfaceProxy * src,const SkIRect & srcRect,const SkIPoint & dstPoint)410 bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
411 ASSERT_SINGLE_OWNER
412 RETURN_FALSE_IF_ABANDONED
413 SkDEBUGCODE(this->validate();)
414 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
415
416 const GrCaps* caps = fContext->priv().caps();
417
418 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
419 SkASSERT(src->origin() == this->asSurfaceProxy()->origin());
420 SkASSERT(caps->makeConfigSpecific(src->config(), src->backendFormat()) ==
421 caps->makeConfigSpecific(this->asSurfaceProxy()->config(),
422 this->asSurfaceProxy()->backendFormat()));
423
424 if (!caps->canCopySurface(this->asSurfaceProxy(), src, srcRect, dstPoint)) {
425 return false;
426 }
427
428 return this->getOpList()->copySurface(fContext, src, srcRect, dstPoint);
429 }
430
rescale(const SkImageInfo & info,const SkIRect & srcRect,SkSurface::RescaleGamma rescaleGamma,SkFilterQuality rescaleQuality)431 sk_sp<GrRenderTargetContext> GrSurfaceContext::rescale(const SkImageInfo& info,
432 const SkIRect& srcRect,
433 SkSurface::RescaleGamma rescaleGamma,
434 SkFilterQuality rescaleQuality) {
435 auto direct = fContext->priv().asDirectContext();
436 if (!direct) {
437 return nullptr;
438 }
439 auto rtProxy = this->asRenderTargetProxy();
440 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
441 return nullptr;
442 }
443
444 // We rescale by drawing and don't currently support drawing to a kUnpremul destination.
445 if (info.alphaType() == kUnpremul_SkAlphaType) {
446 return nullptr;
447 }
448
449 int srcW = srcRect.width();
450 int srcH = srcRect.height();
451 int srcX = srcRect.fLeft;
452 int srcY = srcRect.fTop;
453 sk_sp<GrTextureProxy> texProxy = sk_ref_sp(this->asTextureProxy());
454 SkCanvas::SrcRectConstraint constraint = SkCanvas::kStrict_SrcRectConstraint;
455 if (!texProxy) {
456 texProxy = GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), GrMipMapped::kNo, srcRect,
457 SkBackingFit::kApprox, SkBudgeted::kNo);
458 if (!texProxy) {
459 return nullptr;
460 }
461 srcX = 0;
462 srcY = 0;
463 constraint = SkCanvas::kFast_SrcRectConstraint;
464 }
465
466 float sx = (float)info.width() / srcW;
467 float sy = (float)info.height() / srcH;
468
469 // How many bilerp/bicubic steps to do in X and Y. + means upscaling, - means downscaling.
470 int stepsX;
471 int stepsY;
472 if (rescaleQuality > kNone_SkFilterQuality) {
473 stepsX = static_cast<int>((sx > 1.f) ? ceil(log2f(sx)) : floor(log2f(sx)));
474 stepsY = static_cast<int>((sy > 1.f) ? ceil(log2f(sy)) : floor(log2f(sy)));
475 } else {
476 stepsX = sx != 1.f;
477 stepsY = sy != 1.f;
478 }
479 SkASSERT(stepsX || stepsY);
480 auto currCtx = sk_ref_sp(this);
481 // Assume we should ignore the rescale linear request if the surface has no color space since
482 // it's unclear how we'd linearize from an unknown color space.
483 if (rescaleGamma == SkSurface::kLinear && this->colorSpaceInfo().colorSpace() &&
484 !this->colorSpaceInfo().colorSpace()->gammaIsLinear()) {
485 auto cs = this->colorSpaceInfo().colorSpace()->makeLinearGamma();
486 auto xform = GrColorSpaceXform::Make(this->colorSpaceInfo().colorSpace(),
487 this->colorSpaceInfo().alphaType(), cs.get(),
488 kPremul_SkAlphaType);
489 // We'll fall back to kRGBA_8888 if half float not supported.
490 auto linearRTC = fContext->priv().makeDeferredRenderTargetContextWithFallback(
491 SkBackingFit::kExact, srcW, srcH, GrColorType::kRGBA_F16, cs, 1, GrMipMapped::kNo,
492 kTopLeft_GrSurfaceOrigin);
493 if (!linearRTC) {
494 return nullptr;
495 }
496 linearRTC->drawTexture(GrNoClip(), texProxy, GrSamplerState::Filter::kNearest,
497 SkBlendMode::kSrc, SK_PMColor4fWHITE, SkRect::Make(srcRect),
498 SkRect::MakeWH(srcW, srcH), GrAA::kNo, GrQuadAAFlags::kNone,
499 constraint, SkMatrix::I(), std::move(xform));
500 texProxy = linearRTC->asTextureProxyRef();
501 currCtx = std::move(linearRTC);
502 srcX = 0;
503 srcY = 0;
504 constraint = SkCanvas::kFast_SrcRectConstraint;
505 }
506 while (stepsX || stepsY) {
507 int nextW = info.width();
508 int nextH = info.height();
509 if (stepsX < 0) {
510 nextW = info.width() << (-stepsX - 1);
511 stepsX++;
512 } else if (stepsX != 0) {
513 if (stepsX > 1) {
514 nextW = srcW * 2;
515 }
516 --stepsX;
517 }
518 if (stepsY < 0) {
519 nextH = info.height() << (-stepsY - 1);
520 stepsY++;
521 } else if (stepsY != 0) {
522 if (stepsY > 1) {
523 nextH = srcH * 2;
524 }
525 --stepsY;
526 }
527 GrColorType colorType = currCtx->colorSpaceInfo().colorType();
528 auto cs = currCtx->colorSpaceInfo().refColorSpace();
529 sk_sp<GrColorSpaceXform> xform;
530 auto prevAlphaType = currCtx->colorSpaceInfo().alphaType();
531 if (!stepsX && !stepsY) {
532 // Might as well fold conversion to final info in the last step.
533 cs = info.refColorSpace();
534 colorType = SkColorTypeToGrColorType(info.colorType());
535 xform = GrColorSpaceXform::Make(currCtx->colorSpaceInfo().colorSpace(),
536 currCtx->colorSpaceInfo().alphaType(), cs.get(),
537 info.alphaType());
538 }
539 auto currRTC = fContext->priv().makeDeferredRenderTargetContextWithFallback(
540 SkBackingFit::kExact, nextW, nextH, colorType, std::move(cs), 1, GrMipMapped::kNo,
541 kTopLeft_GrSurfaceOrigin);
542 currCtx = currRTC;
543 if (!currCtx) {
544 return nullptr;
545 }
546 auto dstRect = SkRect::MakeWH(nextW, nextH);
547 if (rescaleQuality == kHigh_SkFilterQuality) {
548 SkMatrix matrix;
549 matrix.setScaleTranslate((float)srcW / nextW, (float)srcH / nextH, srcX, srcY);
550 std::unique_ptr<GrFragmentProcessor> fp;
551 auto dir = GrBicubicEffect::Direction::kXY;
552 if (nextW == srcW) {
553 dir = GrBicubicEffect::Direction::kY;
554 } else if (nextH == srcH) {
555 dir = GrBicubicEffect::Direction::kX;
556 }
557 if (srcW != texProxy->width() || srcH != texProxy->height()) {
558 auto domain = GrTextureDomain::MakeTexelDomain(
559 SkIRect::MakeXYWH(srcX, srcY, srcW, srcH), GrTextureDomain::kClamp_Mode);
560 fp = GrBicubicEffect::Make(texProxy, matrix, domain, dir, prevAlphaType);
561 } else {
562 fp = GrBicubicEffect::Make(texProxy, matrix, dir, prevAlphaType);
563 }
564 if (xform) {
565 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
566 }
567 GrPaint paint;
568 paint.addColorFragmentProcessor(std::move(fp));
569 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
570 currRTC->fillRectToRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect,
571 dstRect);
572 } else {
573 auto filter = rescaleQuality == kNone_SkFilterQuality ? GrSamplerState::Filter::kNearest
574 : GrSamplerState::Filter::kBilerp;
575 auto srcSubset = SkRect::MakeXYWH(srcX, srcY, srcW, srcH);
576 currRTC->drawTexture(GrNoClip(), texProxy, filter, SkBlendMode::kSrc, SK_PMColor4fWHITE,
577 srcSubset, dstRect, GrAA::kNo, GrQuadAAFlags::kNone, constraint,
578 SkMatrix::I(), std::move(xform));
579 }
580 texProxy = currCtx->asTextureProxyRef();
581 srcX = srcY = 0;
582 srcW = nextW;
583 srcH = nextH;
584 constraint = SkCanvas::kFast_SrcRectConstraint;
585 }
586 SkASSERT(currCtx);
587 return sk_ref_sp(currCtx->asRenderTargetContext());
588 }
589
transferPixels(GrColorType dstCT,const SkIRect & rect)590 GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
591 const SkIRect& rect) {
592 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
593 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
594 auto direct = fContext->priv().asDirectContext();
595 if (!direct) {
596 return {};
597 }
598 auto rtProxy = this->asRenderTargetProxy();
599 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
600 return {};
601 }
602
603 auto proxy = this->asSurfaceProxy();
604 auto supportedRead = this->caps()->supportedReadPixelsColorType(
605 this->colorSpaceInfo().colorType(), proxy->backendFormat(), dstCT);
606 // Fail if read color type does not have all of dstCT's color channels and those missing color
607 // channels are in the src.
608 uint32_t dstComponents = GrColorTypeComponentFlags(dstCT);
609 uint32_t legalReadComponents = GrColorTypeComponentFlags(supportedRead.fColorType);
610 uint32_t srcComponents = GrColorTypeComponentFlags(this->colorSpaceInfo().colorType());
611 if ((~legalReadComponents & dstComponents) & srcComponents) {
612 return {};
613 }
614
615 if (!this->caps()->transferBufferSupport() ||
616 !supportedRead.fOffsetAlignmentForTransferBuffer) {
617 return {};
618 }
619
620 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
621 size_t size = rowBytes * rect.height();
622 auto buffer = direct->priv().resourceProvider()->createBuffer(
623 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
624 if (!buffer) {
625 return {};
626 }
627 auto srcRect = rect;
628 bool flip = proxy->origin() == kBottomLeft_GrSurfaceOrigin;
629 if (flip) {
630 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
631 this->height() - rect.fTop);
632 }
633 this->getOpList()->transferFrom(fContext, srcRect, this->colorSpaceInfo().colorType(),
634 supportedRead.fColorType, buffer, 0);
635 PixelTransferResult result;
636 result.fTransferBuffer = std::move(buffer);
637 auto at = this->colorSpaceInfo().alphaType();
638 if (supportedRead.fColorType != dstCT || flip) {
639 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
640 void* dst, const void* src) {
641 GrPixelInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
642 GrPixelInfo dstInfo(dstCT, at, nullptr, w, h);
643 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
644 srcInfo, src, srcInfo.minRowBytes(),
645 /* flipY = */ false);
646 };
647 }
648 return result;
649 }
650