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