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" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrOpList.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/gpu/GrRecordingContextPriv.h" |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrRenderTargetContext.h" |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 21 | #include "src/gpu/GrSurfaceContextPriv.h" |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 22 | #include "src/gpu/GrSurfacePriv.h" |
| 23 | #include "src/gpu/GrTextureContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/gpu/SkGr.h" |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 25 | #include "src/gpu/effects/GrBicubicEffect.h" |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 26 | |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 27 | #define ASSERT_SINGLE_OWNER \ |
| 28 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());) |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 29 | #define RETURN_FALSE_IF_ABANDONED if (this->fContext->priv().abandoned()) { return false; } |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 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). |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 35 | GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context, |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 36 | GrColorType colorType, |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 37 | SkAlphaType alphaType, |
Brian Salomon | bd3d8d3 | 2019-07-02 09:16:28 -0400 | [diff] [blame] | 38 | sk_sp<SkColorSpace> colorSpace) |
| 39 | : fContext(context), fColorSpaceInfo(colorType, alphaType, std::move(colorSpace)) {} |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 40 | |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 41 | const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); } |
| 42 | |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 43 | GrAuditTrail* GrSurfaceContext::auditTrail() { |
| 44 | return fContext->priv().auditTrail(); |
| 45 | } |
| 46 | |
| 47 | GrDrawingManager* GrSurfaceContext::drawingManager() { |
| 48 | return fContext->priv().drawingManager(); |
| 49 | } |
| 50 | |
| 51 | const GrDrawingManager* GrSurfaceContext::drawingManager() const { |
| 52 | return fContext->priv().drawingManager(); |
| 53 | } |
| 54 | |
| 55 | #ifdef SK_DEBUG |
| 56 | GrSingleOwner* GrSurfaceContext::singleOwner() { |
| 57 | return fContext->priv().singleOwner(); |
| 58 | } |
| 59 | #endif |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 60 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 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"); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 67 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 68 | if (!direct && !(direct = fContext->priv().asDirectContext())) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 69 | return false; |
| 70 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 71 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 72 | if (!dst) { |
| 73 | return false; |
| 74 | } |
| 75 | |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 76 | size_t tightRowBytes = origDstInfo.minRowBytes(); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 77 | if (!rowBytes) { |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 78 | rowBytes = tightRowBytes; |
| 79 | } else if (rowBytes < tightRowBytes) { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 80 | return false; |
| 81 | } |
| 82 | |
| 83 | if (!origDstInfo.isValid()) { |
| 84 | return false; |
| 85 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 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 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 96 | auto dstInfo = origDstInfo; |
| 97 | if (!dstInfo.clip(this->width(), this->height(), &pt, &dst, rowBytes)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 98 | return false; |
| 99 | } |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 100 | // Our tight row bytes may have been changed by clipping. |
| 101 | tightRowBytes = dstInfo.minRowBytes(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 102 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 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; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 107 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 108 | bool needColorConversion = SkColorSpaceXformSteps::Required(this->colorSpaceInfo().colorSpace(), |
| 109 | dstInfo.colorSpace()); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 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()). |
Greg Daniel | 0258c90 | 2019-08-01 13:08:33 -0400 | [diff] [blame] | 117 | GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888, |
| 118 | GrRenderable::kYes); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 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) && |
Greg Daniel | 0258c90 | 2019-08-01 13:08:33 -0400 | [diff] [blame] | 125 | defaultRGBAFormat.isValid() && |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 126 | direct->priv().validPMUPMConversionExists(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 127 | |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 128 | auto readFlag = caps->surfaceSupportsReadPixels(srcSurface); |
Brian Salomon | dc0710f | 2019-07-01 14:59:32 -0400 | [diff] [blame] | 129 | if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) { |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 130 | return false; |
| 131 | } |
| 132 | |
Brian Salomon | dc0710f | 2019-07-01 14:59:32 -0400 | [diff] [blame] | 133 | if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) { |
Brian Salomon | 27ae52c | 2019-07-03 11:27:44 -0400 | [diff] [blame] | 134 | GrColorType colorType = canvas2DFastPath ? GrColorType::kRGBA_8888 |
| 135 | : this->colorSpaceInfo().colorType(); |
| 136 | sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr |
| 137 | : this->colorSpaceInfo().refColorSpace(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 138 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 139 | auto tempCtx = direct->priv().makeDeferredRenderTargetContext( |
Brian Salomon | 27ae52c | 2019-07-03 11:27:44 -0400 | [diff] [blame] | 140 | SkBackingFit::kApprox, dstInfo.width(), dstInfo.height(), colorType, std::move(cs), |
| 141 | 1, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin, nullptr, SkBudgeted::kYes); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 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())); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 151 | if (dstInfo.colorType() == GrColorType::kBGRA_8888) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 152 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA()); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 153 | dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 154 | } |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 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); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 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(), |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 171 | SkRect::MakeWH(dstInfo.width(), dstInfo.height()), |
| 172 | SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height())); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 173 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 174 | return tempCtx->readPixels(dstInfo, dst, rowBytes, {0, 0}, direct); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 175 | } |
| 176 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 177 | bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 178 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 179 | auto supportedRead = caps->supportedReadPixelsColorType( |
Greg Daniel | 00fb724 | 2019-07-18 14:28:01 -0400 | [diff] [blame] | 180 | this->colorSpaceInfo().colorType(), srcProxy->backendFormat(), dstInfo.colorType()); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 181 | |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 182 | bool makeTight = !caps->readPixelsRowBytesSupport() && tightRowBytes != rowBytes; |
| 183 | |
| 184 | bool convert = unpremul || premul || needColorConversion || flip || makeTight || |
Brian Salomon | 8f8354a | 2019-07-31 20:12:02 -0400 | [diff] [blame] | 185 | (dstInfo.colorType() != supportedRead.fColorType); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 186 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 187 | std::unique_ptr<char[]> tmpPixels; |
| 188 | GrPixelInfo tmpInfo; |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 189 | void* readDst = dst; |
| 190 | size_t readRB = rowBytes; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 191 | if (convert) { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 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]()); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 198 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 199 | readDst = tmpPixels.get(); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 200 | readRB = tmpRB; |
| 201 | pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | direct->priv().flushSurface(srcProxy); |
| 205 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 206 | if (!direct->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(), |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 207 | dstInfo.height(), this->colorSpaceInfo().colorType(), |
| 208 | supportedRead.fColorType, readDst, readRB)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 209 | return false; |
| 210 | } |
| 211 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 212 | if (convert) { |
Brian Salomon | 8f8354a | 2019-07-31 20:12:02 -0400 | [diff] [blame] | 213 | return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 214 | } |
| 215 | return true; |
| 216 | } |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 217 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 218 | bool GrSurfaceContext::writePixels(const GrPixelInfo& origSrcInfo, const void* src, size_t rowBytes, |
| 219 | SkIPoint pt, GrContext* direct) { |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 220 | ASSERT_SINGLE_OWNER |
| 221 | RETURN_FALSE_IF_ABANDONED |
| 222 | SkDEBUGCODE(this->validate();) |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 223 | GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels"); |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 224 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 225 | if (!direct && !(direct = fContext->priv().asDirectContext())) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 226 | return false; |
| 227 | } |
Robert Phillips | 6a6de56 | 2019-02-15 15:19:15 -0500 | [diff] [blame] | 228 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 229 | if (this->asSurfaceProxy()->readOnly()) { |
Robert Phillips | 6a6de56 | 2019-02-15 15:19:15 -0500 | [diff] [blame] | 230 | return false; |
| 231 | } |
| 232 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 233 | if (!src) { |
| 234 | return false; |
| 235 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 236 | |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 237 | size_t tightRowBytes = origSrcInfo.minRowBytes(); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 238 | if (!rowBytes) { |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 239 | rowBytes = tightRowBytes; |
| 240 | } else if (rowBytes < tightRowBytes) { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 241 | return false; |
| 242 | } |
| 243 | |
| 244 | if (!origSrcInfo.isValid()) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 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 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 255 | auto srcInfo = origSrcInfo; |
| 256 | if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 257 | return false; |
| 258 | } |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 259 | // Our tight row bytes may have been changed by clipping. |
| 260 | tightRowBytes = srcInfo.minRowBytes(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 261 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 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; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 266 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 267 | bool needColorConversion = SkColorSpaceXformSteps::Required( |
| 268 | srcInfo.colorSpace(), this->colorSpaceInfo().colorSpace()); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 269 | |
| 270 | const GrCaps* caps = direct->priv().caps(); |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 271 | |
| 272 | auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888, |
| 273 | GrRenderable::kNo); |
| 274 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 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. |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 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) && |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 283 | rgbaDefaultFormat.isValid() && |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 284 | direct->priv().validPMUPMConversionExists(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 285 | |
| 286 | if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) { |
| 287 | GrSurfaceDesc desc; |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 288 | desc.fWidth = srcInfo.width(); |
| 289 | desc.fHeight = srcInfo.height(); |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 290 | GrColorType colorType; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 291 | |
| 292 | GrBackendFormat format; |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 293 | SkAlphaType alphaType; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 294 | if (canvas2DFastPath) { |
| 295 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 296 | colorType = GrColorType::kRGBA_8888; |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 297 | format = rgbaDefaultFormat; |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 298 | alphaType = kUnpremul_SkAlphaType; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 299 | } else { |
| 300 | desc.fConfig = dstProxy->config(); |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 301 | colorType = this->colorSpaceInfo().colorType(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 302 | format = dstProxy->backendFormat().makeTexture2D(); |
| 303 | if (!format.isValid()) { |
| 304 | return false; |
| 305 | } |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 306 | alphaType = this->colorSpaceInfo().alphaType(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 307 | } |
| 308 | |
Greg Daniel | 2e52ad1 | 2019-06-13 10:04:16 -0400 | [diff] [blame] | 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. |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 314 | GrSurfaceOrigin tempOrigin = |
| 315 | this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : dstProxy->origin(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 316 | auto tempProxy = direct->priv().proxyProvider()->createProxy( |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 317 | format, desc, GrRenderable::kNo, 1, tempOrigin, SkBackingFit::kApprox, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame] | 318 | SkBudgeted::kYes, GrProtected::kNo); |
Greg Daniel | 2e52ad1 | 2019-06-13 10:04:16 -0400 | [diff] [blame] | 319 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 320 | if (!tempProxy) { |
| 321 | return false; |
| 322 | } |
| 323 | auto tempCtx = direct->priv().drawingManager()->makeTextureContext( |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 324 | tempProxy, colorType, alphaType, this->colorSpaceInfo().refColorSpace()); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 325 | if (!tempCtx) { |
| 326 | return false; |
| 327 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 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. |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 333 | if (canvas2DFastPath) { |
| 334 | srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888); |
| 335 | } |
| 336 | if (!tempCtx->writePixels(srcInfo, src, rowBytes, {0, 0}, direct)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 337 | return false; |
| 338 | } |
| 339 | |
| 340 | if (this->asRenderTargetContext()) { |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 341 | std::unique_ptr<GrFragmentProcessor> fp; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 342 | if (canvas2DFastPath) { |
| 343 | fp = direct->priv().createUPMToPMEffect( |
| 344 | GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I())); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 345 | // Important: check the original src color type here! |
| 346 | if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 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(), |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 360 | SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()), |
| 361 | SkRect::MakeWH(srcInfo.width(), srcInfo.height())); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 362 | } else { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 363 | SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height()); |
| 364 | SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 365 | if (!this->copy(tempProxy.get(), srcRect, dstPoint)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 366 | return false; |
| 367 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 368 | } |
| 369 | return true; |
| 370 | } |
| 371 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 372 | GrColorType allowedColorType = |
Brian Salomon | 01915c0 | 2019-08-02 09:57:21 -0400 | [diff] [blame] | 373 | caps->supportedWritePixelsColorType(this->colorSpaceInfo().colorType(), |
| 374 | dstProxy->backendFormat(), |
| 375 | srcInfo.colorType()).fColorType; |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 376 | bool flip = dstProxy->origin() == kBottomLeft_GrSurfaceOrigin; |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 377 | bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes; |
| 378 | bool convert = premul || unpremul || needColorConversion || makeTight || |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 379 | (srcInfo.colorType() != allowedColorType) || flip; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 380 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 381 | std::unique_ptr<char[]> tmpPixels; |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 382 | GrColorType srcColorType = srcInfo.colorType(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 383 | if (convert) { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 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()]); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 389 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 390 | GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 391 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 392 | srcColorType = tmpInfo.colorType(); |
| 393 | rowBytes = tmpRB; |
| 394 | src = tmpPixels.get(); |
| 395 | pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 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 | |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 405 | return direct->priv().getGpu()->writePixels( |
| 406 | dstSurface, pt.fX, pt.fY, srcInfo.width(), srcInfo.height(), |
| 407 | this->colorSpaceInfo().colorType(), srcColorType, src, rowBytes); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 408 | } |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 409 | |
| 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();) |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 414 | GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy"); |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 415 | |
Brian Salomon | 947efe2 | 2019-07-16 15:36:11 -0400 | [diff] [blame] | 416 | const GrCaps* caps = fContext->priv().caps(); |
| 417 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 418 | SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal); |
| 419 | SkASSERT(src->origin() == this->asSurfaceProxy()->origin()); |
Brian Salomon | 947efe2 | 2019-07-16 15:36:11 -0400 | [diff] [blame] | 420 | SkASSERT(caps->makeConfigSpecific(src->config(), src->backendFormat()) == |
| 421 | caps->makeConfigSpecific(this->asSurfaceProxy()->config(), |
| 422 | this->asSurfaceProxy()->backendFormat())); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 423 | |
Chris Dalton | f8e5aad | 2019-08-02 12:55:23 -0600 | [diff] [blame] | 424 | if (!caps->canCopySurface(this->asSurfaceProxy(), src, srcRect, dstPoint)) { |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 425 | return false; |
| 426 | } |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 427 | |
Greg Daniel | e227fe4 | 2019-08-21 13:52:24 -0400 | [diff] [blame^] | 428 | return this->drawingManager()->newCopyRenderTask(sk_ref_sp(src), srcRect, |
| 429 | this->asSurfaceProxyRef(), dstPoint); |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 430 | } |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 431 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 432 | std::unique_ptr<GrRenderTargetContext> GrSurfaceContext::rescale( |
| 433 | const SkImageInfo& info, |
| 434 | const SkIRect& srcRect, |
| 435 | SkSurface::RescaleGamma rescaleGamma, |
| 436 | SkFilterQuality rescaleQuality) { |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 437 | auto direct = fContext->priv().asDirectContext(); |
| 438 | if (!direct) { |
| 439 | return nullptr; |
| 440 | } |
| 441 | auto rtProxy = this->asRenderTargetProxy(); |
| 442 | if (rtProxy && rtProxy->wrapsVkSecondaryCB()) { |
| 443 | return nullptr; |
| 444 | } |
| 445 | |
| 446 | // We rescale by drawing and don't currently support drawing to a kUnpremul destination. |
| 447 | if (info.alphaType() == kUnpremul_SkAlphaType) { |
| 448 | return nullptr; |
| 449 | } |
| 450 | |
| 451 | int srcW = srcRect.width(); |
| 452 | int srcH = srcRect.height(); |
| 453 | int srcX = srcRect.fLeft; |
| 454 | int srcY = srcRect.fTop; |
| 455 | sk_sp<GrTextureProxy> texProxy = sk_ref_sp(this->asTextureProxy()); |
| 456 | SkCanvas::SrcRectConstraint constraint = SkCanvas::kStrict_SrcRectConstraint; |
| 457 | if (!texProxy) { |
| 458 | texProxy = GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), GrMipMapped::kNo, srcRect, |
| 459 | SkBackingFit::kApprox, SkBudgeted::kNo); |
| 460 | if (!texProxy) { |
| 461 | return nullptr; |
| 462 | } |
| 463 | srcX = 0; |
| 464 | srcY = 0; |
| 465 | constraint = SkCanvas::kFast_SrcRectConstraint; |
| 466 | } |
| 467 | |
| 468 | float sx = (float)info.width() / srcW; |
| 469 | float sy = (float)info.height() / srcH; |
| 470 | |
| 471 | // How many bilerp/bicubic steps to do in X and Y. + means upscaling, - means downscaling. |
| 472 | int stepsX; |
| 473 | int stepsY; |
| 474 | if (rescaleQuality > kNone_SkFilterQuality) { |
| 475 | stepsX = static_cast<int>((sx > 1.f) ? ceil(log2f(sx)) : floor(log2f(sx))); |
| 476 | stepsY = static_cast<int>((sy > 1.f) ? ceil(log2f(sy)) : floor(log2f(sy))); |
| 477 | } else { |
| 478 | stepsX = sx != 1.f; |
| 479 | stepsY = sy != 1.f; |
| 480 | } |
| 481 | SkASSERT(stepsX || stepsY); |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 482 | // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the |
| 483 | // pass B is moved to A. If 'this' is the input on the first pass then tempA is null. |
| 484 | std::unique_ptr<GrRenderTargetContext> tempA; |
| 485 | std::unique_ptr<GrRenderTargetContext> tempB; |
| 486 | |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 487 | // Assume we should ignore the rescale linear request if the surface has no color space since |
| 488 | // it's unclear how we'd linearize from an unknown color space. |
| 489 | if (rescaleGamma == SkSurface::kLinear && this->colorSpaceInfo().colorSpace() && |
| 490 | !this->colorSpaceInfo().colorSpace()->gammaIsLinear()) { |
| 491 | auto cs = this->colorSpaceInfo().colorSpace()->makeLinearGamma(); |
| 492 | auto xform = GrColorSpaceXform::Make(this->colorSpaceInfo().colorSpace(), |
| 493 | this->colorSpaceInfo().alphaType(), cs.get(), |
| 494 | kPremul_SkAlphaType); |
| 495 | // We'll fall back to kRGBA_8888 if half float not supported. |
| 496 | auto linearRTC = fContext->priv().makeDeferredRenderTargetContextWithFallback( |
| 497 | SkBackingFit::kExact, srcW, srcH, GrColorType::kRGBA_F16, cs, 1, GrMipMapped::kNo, |
| 498 | kTopLeft_GrSurfaceOrigin); |
| 499 | if (!linearRTC) { |
| 500 | return nullptr; |
| 501 | } |
| 502 | linearRTC->drawTexture(GrNoClip(), texProxy, GrSamplerState::Filter::kNearest, |
| 503 | SkBlendMode::kSrc, SK_PMColor4fWHITE, SkRect::Make(srcRect), |
| 504 | SkRect::MakeWH(srcW, srcH), GrAA::kNo, GrQuadAAFlags::kNone, |
| 505 | constraint, SkMatrix::I(), std::move(xform)); |
| 506 | texProxy = linearRTC->asTextureProxyRef(); |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 507 | tempA = std::move(linearRTC); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 508 | srcX = 0; |
| 509 | srcY = 0; |
| 510 | constraint = SkCanvas::kFast_SrcRectConstraint; |
| 511 | } |
| 512 | while (stepsX || stepsY) { |
| 513 | int nextW = info.width(); |
| 514 | int nextH = info.height(); |
| 515 | if (stepsX < 0) { |
| 516 | nextW = info.width() << (-stepsX - 1); |
| 517 | stepsX++; |
| 518 | } else if (stepsX != 0) { |
| 519 | if (stepsX > 1) { |
| 520 | nextW = srcW * 2; |
| 521 | } |
| 522 | --stepsX; |
| 523 | } |
| 524 | if (stepsY < 0) { |
| 525 | nextH = info.height() << (-stepsY - 1); |
| 526 | stepsY++; |
| 527 | } else if (stepsY != 0) { |
| 528 | if (stepsY > 1) { |
| 529 | nextH = srcH * 2; |
| 530 | } |
| 531 | --stepsY; |
| 532 | } |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 533 | auto input = tempA ? tempA.get() : this; |
| 534 | GrColorType colorType = input->colorSpaceInfo().colorType(); |
| 535 | auto cs = input->colorSpaceInfo().refColorSpace(); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 536 | sk_sp<GrColorSpaceXform> xform; |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 537 | auto prevAlphaType = input->colorSpaceInfo().alphaType(); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 538 | if (!stepsX && !stepsY) { |
| 539 | // Might as well fold conversion to final info in the last step. |
| 540 | cs = info.refColorSpace(); |
| 541 | colorType = SkColorTypeToGrColorType(info.colorType()); |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 542 | xform = GrColorSpaceXform::Make(input->colorSpaceInfo().colorSpace(), |
| 543 | input->colorSpaceInfo().alphaType(), cs.get(), |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 544 | info.alphaType()); |
| 545 | } |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 546 | tempB = fContext->priv().makeDeferredRenderTargetContextWithFallback( |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 547 | SkBackingFit::kExact, nextW, nextH, colorType, std::move(cs), 1, GrMipMapped::kNo, |
| 548 | kTopLeft_GrSurfaceOrigin); |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 549 | if (!tempB) { |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 550 | return nullptr; |
| 551 | } |
| 552 | auto dstRect = SkRect::MakeWH(nextW, nextH); |
| 553 | if (rescaleQuality == kHigh_SkFilterQuality) { |
| 554 | SkMatrix matrix; |
| 555 | matrix.setScaleTranslate((float)srcW / nextW, (float)srcH / nextH, srcX, srcY); |
| 556 | std::unique_ptr<GrFragmentProcessor> fp; |
| 557 | auto dir = GrBicubicEffect::Direction::kXY; |
| 558 | if (nextW == srcW) { |
| 559 | dir = GrBicubicEffect::Direction::kY; |
| 560 | } else if (nextH == srcH) { |
| 561 | dir = GrBicubicEffect::Direction::kX; |
| 562 | } |
| 563 | if (srcW != texProxy->width() || srcH != texProxy->height()) { |
| 564 | auto domain = GrTextureDomain::MakeTexelDomain( |
| 565 | SkIRect::MakeXYWH(srcX, srcY, srcW, srcH), GrTextureDomain::kClamp_Mode); |
| 566 | fp = GrBicubicEffect::Make(texProxy, matrix, domain, dir, prevAlphaType); |
| 567 | } else { |
| 568 | fp = GrBicubicEffect::Make(texProxy, matrix, dir, prevAlphaType); |
| 569 | } |
| 570 | if (xform) { |
| 571 | fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform)); |
| 572 | } |
| 573 | GrPaint paint; |
| 574 | paint.addColorFragmentProcessor(std::move(fp)); |
| 575 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 576 | tempB->fillRectToRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect, |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 577 | dstRect); |
| 578 | } else { |
| 579 | auto filter = rescaleQuality == kNone_SkFilterQuality ? GrSamplerState::Filter::kNearest |
| 580 | : GrSamplerState::Filter::kBilerp; |
| 581 | auto srcSubset = SkRect::MakeXYWH(srcX, srcY, srcW, srcH); |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 582 | tempB->drawTexture(GrNoClip(), texProxy, filter, SkBlendMode::kSrc, SK_PMColor4fWHITE, |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 583 | srcSubset, dstRect, GrAA::kNo, GrQuadAAFlags::kNone, constraint, |
| 584 | SkMatrix::I(), std::move(xform)); |
| 585 | } |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 586 | texProxy = tempB->asTextureProxyRef(); |
| 587 | tempA = std::move(tempB); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 588 | srcX = srcY = 0; |
| 589 | srcW = nextW; |
| 590 | srcH = nextH; |
| 591 | constraint = SkCanvas::kFast_SrcRectConstraint; |
| 592 | } |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 593 | SkASSERT(tempA); |
| 594 | return tempA; |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 595 | } |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 596 | |
| 597 | GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT, |
| 598 | const SkIRect& rect) { |
| 599 | SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width()); |
| 600 | SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height()); |
| 601 | auto direct = fContext->priv().asDirectContext(); |
| 602 | if (!direct) { |
| 603 | return {}; |
| 604 | } |
| 605 | auto rtProxy = this->asRenderTargetProxy(); |
| 606 | if (rtProxy && rtProxy->wrapsVkSecondaryCB()) { |
| 607 | return {}; |
| 608 | } |
| 609 | |
| 610 | auto proxy = this->asSurfaceProxy(); |
| 611 | auto supportedRead = this->caps()->supportedReadPixelsColorType( |
| 612 | this->colorSpaceInfo().colorType(), proxy->backendFormat(), dstCT); |
| 613 | // Fail if read color type does not have all of dstCT's color channels and those missing color |
| 614 | // channels are in the src. |
| 615 | uint32_t dstComponents = GrColorTypeComponentFlags(dstCT); |
| 616 | uint32_t legalReadComponents = GrColorTypeComponentFlags(supportedRead.fColorType); |
| 617 | uint32_t srcComponents = GrColorTypeComponentFlags(this->colorSpaceInfo().colorType()); |
| 618 | if ((~legalReadComponents & dstComponents) & srcComponents) { |
| 619 | return {}; |
| 620 | } |
| 621 | |
| 622 | if (!this->caps()->transferBufferSupport() || |
| 623 | !supportedRead.fOffsetAlignmentForTransferBuffer) { |
| 624 | return {}; |
| 625 | } |
| 626 | |
| 627 | size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width(); |
| 628 | size_t size = rowBytes * rect.height(); |
| 629 | auto buffer = direct->priv().resourceProvider()->createBuffer( |
| 630 | size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern); |
| 631 | if (!buffer) { |
| 632 | return {}; |
| 633 | } |
| 634 | auto srcRect = rect; |
| 635 | bool flip = proxy->origin() == kBottomLeft_GrSurfaceOrigin; |
| 636 | if (flip) { |
| 637 | srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight, |
| 638 | this->height() - rect.fTop); |
| 639 | } |
Greg Daniel | bbfec9d | 2019-08-20 10:56:51 -0400 | [diff] [blame] | 640 | this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect, |
| 641 | this->colorSpaceInfo().colorType(), |
| 642 | supportedRead.fColorType, buffer, 0); |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 643 | PixelTransferResult result; |
| 644 | result.fTransferBuffer = std::move(buffer); |
| 645 | auto at = this->colorSpaceInfo().alphaType(); |
Brian Salomon | 8f8354a | 2019-07-31 20:12:02 -0400 | [diff] [blame] | 646 | if (supportedRead.fColorType != dstCT || flip) { |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 647 | result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at]( |
| 648 | void* dst, const void* src) { |
| 649 | GrPixelInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h); |
| 650 | GrPixelInfo dstInfo(dstCT, at, nullptr, w, h); |
| 651 | GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(), |
| 652 | srcInfo, src, srcInfo.minRowBytes(), |
Brian Salomon | 8f8354a | 2019-07-31 20:12:02 -0400 | [diff] [blame] | 653 | /* flipY = */ false); |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 654 | }; |
| 655 | } |
| 656 | return result; |
| 657 | } |