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 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 | |
| 30 | // In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress |
| 31 | // GrOpLists to be picked up and added to by renderTargetContexts lower in the call |
| 32 | // stack. When this occurs with a closed GrOpList, a new one will be allocated |
| 33 | // when the renderTargetContext attempts to use it (via getOpList). |
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 | |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 40 | GrAuditTrail* GrSurfaceContext::auditTrail() { |
| 41 | return fContext->priv().auditTrail(); |
| 42 | } |
| 43 | |
| 44 | GrDrawingManager* GrSurfaceContext::drawingManager() { |
| 45 | return fContext->priv().drawingManager(); |
| 46 | } |
| 47 | |
| 48 | const GrDrawingManager* GrSurfaceContext::drawingManager() const { |
| 49 | return fContext->priv().drawingManager(); |
| 50 | } |
| 51 | |
| 52 | #ifdef SK_DEBUG |
| 53 | GrSingleOwner* GrSurfaceContext::singleOwner() { |
| 54 | return fContext->priv().singleOwner(); |
| 55 | } |
| 56 | #endif |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 57 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 58 | bool GrSurfaceContext::readPixels(const GrPixelInfo& origDstInfo, void* dst, size_t rowBytes, |
| 59 | SkIPoint pt, GrContext* direct) { |
| 60 | ASSERT_SINGLE_OWNER |
| 61 | RETURN_FALSE_IF_ABANDONED |
| 62 | SkDEBUGCODE(this->validate();) |
| 63 | GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels"); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 64 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 65 | if (!direct && !(direct = fContext->priv().asDirectContext())) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 66 | return false; |
| 67 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 68 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 69 | if (!dst) { |
| 70 | return false; |
| 71 | } |
| 72 | |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 73 | size_t tightRowBytes = origDstInfo.minRowBytes(); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 74 | if (!rowBytes) { |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 75 | rowBytes = tightRowBytes; |
| 76 | } else if (rowBytes < tightRowBytes) { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 77 | return false; |
| 78 | } |
| 79 | |
| 80 | if (!origDstInfo.isValid()) { |
| 81 | return false; |
| 82 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 83 | |
| 84 | GrSurfaceProxy* srcProxy = this->asSurfaceProxy(); |
| 85 | |
| 86 | // MDB TODO: delay this instantiation until later in the method |
| 87 | if (!srcProxy->instantiate(direct->priv().resourceProvider())) { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | GrSurface* srcSurface = srcProxy->peekSurface(); |
| 92 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 93 | auto dstInfo = origDstInfo; |
| 94 | if (!dstInfo.clip(this->width(), this->height(), &pt, &dst, rowBytes)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 95 | return false; |
| 96 | } |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 97 | // Our tight row bytes may have been changed by clipping. |
| 98 | tightRowBytes = dstInfo.minRowBytes(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 99 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 100 | bool premul = this->colorSpaceInfo().alphaType() == kUnpremul_SkAlphaType && |
| 101 | dstInfo.alphaType() == kPremul_SkAlphaType; |
| 102 | bool unpremul = this->colorSpaceInfo().alphaType() == kPremul_SkAlphaType && |
| 103 | dstInfo.alphaType() == kUnpremul_SkAlphaType; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 104 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 105 | bool needColorConversion = SkColorSpaceXformSteps::Required(this->colorSpaceInfo().colorSpace(), |
| 106 | dstInfo.colorSpace()); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 107 | |
| 108 | const GrCaps* caps = direct->priv().caps(); |
| 109 | // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't |
| 110 | // care so much about getImageData performance. However, in order to ensure putImageData/ |
| 111 | // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary |
| 112 | // unpremul step to writeSurfacePixels's premul step (which is determined empirically in |
| 113 | // fContext->vaildaPMUPMConversionExists()). |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 114 | bool canvas2DFastPath = unpremul && !needColorConversion && |
| 115 | (GrColorType::kRGBA_8888 == dstInfo.colorType() || |
| 116 | GrColorType::kBGRA_8888 == dstInfo.colorType()) && |
| 117 | SkToBool(srcProxy->asTextureProxy()) && |
| 118 | (srcProxy->config() == kRGBA_8888_GrPixelConfig || |
| 119 | srcProxy->config() == kBGRA_8888_GrPixelConfig) && |
| 120 | caps->isConfigRenderable(kRGBA_8888_GrPixelConfig) && |
| 121 | direct->priv().validPMUPMConversionExists(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 122 | |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 123 | auto readFlag = caps->surfaceSupportsReadPixels(srcSurface); |
Brian Salomon | dc0710f | 2019-07-01 14:59:32 -0400 | [diff] [blame] | 124 | if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) { |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 125 | return false; |
| 126 | } |
| 127 | |
Brian Salomon | dc0710f | 2019-07-01 14:59:32 -0400 | [diff] [blame] | 128 | if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) { |
Brian Salomon | 27ae52c | 2019-07-03 11:27:44 -0400 | [diff] [blame] | 129 | GrColorType colorType = canvas2DFastPath ? GrColorType::kRGBA_8888 |
| 130 | : this->colorSpaceInfo().colorType(); |
| 131 | sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr |
| 132 | : this->colorSpaceInfo().refColorSpace(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 133 | |
| 134 | sk_sp<GrRenderTargetContext> tempCtx = direct->priv().makeDeferredRenderTargetContext( |
Brian Salomon | 27ae52c | 2019-07-03 11:27:44 -0400 | [diff] [blame] | 135 | SkBackingFit::kApprox, dstInfo.width(), dstInfo.height(), colorType, std::move(cs), |
| 136 | 1, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin, nullptr, SkBudgeted::kYes); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 137 | if (!tempCtx) { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | std::unique_ptr<GrFragmentProcessor> fp; |
| 142 | if (canvas2DFastPath) { |
| 143 | fp = direct->priv().createPMToUPMEffect( |
| 144 | GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()), |
| 145 | SkMatrix::I())); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 146 | if (dstInfo.colorType() == GrColorType::kBGRA_8888) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 147 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA()); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 148 | dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 149 | } |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 150 | // The render target context is incorrectly tagged as kPremul even though we're writing |
| 151 | // unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type so we don't |
| 152 | // double unpremul. |
| 153 | dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 154 | } else { |
| 155 | fp = GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()), SkMatrix::I()); |
| 156 | } |
| 157 | if (!fp) { |
| 158 | return false; |
| 159 | } |
| 160 | GrPaint paint; |
| 161 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 162 | paint.addColorFragmentProcessor(std::move(fp)); |
| 163 | |
| 164 | tempCtx->asRenderTargetContext()->fillRectToRect( |
| 165 | GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 166 | SkRect::MakeWH(dstInfo.width(), dstInfo.height()), |
| 167 | SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height())); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 168 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 169 | return tempCtx->readPixels(dstInfo, dst, rowBytes, {0, 0}, direct); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 170 | } |
| 171 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 172 | bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin; |
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 | auto supportedRead = caps->supportedReadPixelsColorType( |
Greg Daniel | 00fb724 | 2019-07-18 14:28:01 -0400 | [diff] [blame^] | 175 | this->colorSpaceInfo().colorType(), srcProxy->backendFormat(), dstInfo.colorType()); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 176 | |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 177 | bool makeTight = !caps->readPixelsRowBytesSupport() && tightRowBytes != rowBytes; |
| 178 | |
| 179 | bool convert = unpremul || premul || needColorConversion || flip || makeTight || |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 180 | (dstInfo.colorType() != supportedRead.fColorType) || |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 181 | supportedRead.fSwizzle != GrSwizzle::RGBA(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 182 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 183 | std::unique_ptr<char[]> tmpPixels; |
| 184 | GrPixelInfo tmpInfo; |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 185 | void* readDst = dst; |
| 186 | size_t readRB = rowBytes; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 187 | if (convert) { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 188 | tmpInfo = {supportedRead.fColorType, this->colorSpaceInfo().alphaType(), |
| 189 | this->colorSpaceInfo().refColorSpace(), dstInfo.width(), dstInfo.height()}; |
| 190 | size_t tmpRB = tmpInfo.minRowBytes(); |
| 191 | size_t size = tmpRB * tmpInfo.height(); |
| 192 | // Chrome MSAN bots require the data to be initialized (hence the ()). |
| 193 | tmpPixels.reset(new char[size]()); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 194 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 195 | readDst = tmpPixels.get(); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 196 | readRB = tmpRB; |
| 197 | pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | direct->priv().flushSurface(srcProxy); |
| 201 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 202 | if (!direct->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(), |
| 203 | dstInfo.height(), supportedRead.fColorType, readDst, |
| 204 | readRB)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 205 | return false; |
| 206 | } |
| 207 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 208 | if (convert) { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 209 | return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip, |
| 210 | supportedRead.fSwizzle); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 211 | } |
| 212 | return true; |
| 213 | } |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 214 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 215 | bool GrSurfaceContext::writePixels(const GrPixelInfo& origSrcInfo, const void* src, size_t rowBytes, |
| 216 | SkIPoint pt, GrContext* direct) { |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 217 | ASSERT_SINGLE_OWNER |
| 218 | RETURN_FALSE_IF_ABANDONED |
| 219 | SkDEBUGCODE(this->validate();) |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 220 | GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels"); |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 221 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 222 | if (!direct && !(direct = fContext->priv().asDirectContext())) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 223 | return false; |
| 224 | } |
Robert Phillips | 6a6de56 | 2019-02-15 15:19:15 -0500 | [diff] [blame] | 225 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 226 | if (this->asSurfaceProxy()->readOnly()) { |
Robert Phillips | 6a6de56 | 2019-02-15 15:19:15 -0500 | [diff] [blame] | 227 | return false; |
| 228 | } |
| 229 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 230 | if (!src) { |
| 231 | return false; |
| 232 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 233 | |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 234 | size_t tightRowBytes = origSrcInfo.minRowBytes(); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 235 | if (!rowBytes) { |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 236 | rowBytes = tightRowBytes; |
| 237 | } else if (rowBytes < tightRowBytes) { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 238 | return false; |
| 239 | } |
| 240 | |
| 241 | if (!origSrcInfo.isValid()) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 242 | return false; |
| 243 | } |
| 244 | |
| 245 | GrSurfaceProxy* dstProxy = this->asSurfaceProxy(); |
| 246 | if (!dstProxy->instantiate(direct->priv().resourceProvider())) { |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | GrSurface* dstSurface = dstProxy->peekSurface(); |
| 251 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 252 | auto srcInfo = origSrcInfo; |
| 253 | if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 254 | return false; |
| 255 | } |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 256 | // Our tight row bytes may have been changed by clipping. |
| 257 | tightRowBytes = srcInfo.minRowBytes(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 258 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 259 | bool premul = this->colorSpaceInfo().alphaType() == kPremul_SkAlphaType && |
| 260 | srcInfo.alphaType() == kUnpremul_SkAlphaType; |
| 261 | bool unpremul = this->colorSpaceInfo().alphaType() == kUnpremul_SkAlphaType && |
| 262 | srcInfo.alphaType() == kPremul_SkAlphaType; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 263 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 264 | bool needColorConversion = SkColorSpaceXformSteps::Required( |
| 265 | srcInfo.colorSpace(), this->colorSpaceInfo().colorSpace()); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 266 | |
| 267 | const GrCaps* caps = direct->priv().caps(); |
| 268 | // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs |
| 269 | // 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] | 270 | bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion && |
| 271 | (srcInfo.colorType() == GrColorType::kRGBA_8888 || |
| 272 | srcInfo.colorType() == GrColorType::kBGRA_8888) && |
| 273 | SkToBool(this->asRenderTargetContext()) && |
| 274 | (dstProxy->config() == kRGBA_8888_GrPixelConfig || |
| 275 | dstProxy->config() == kBGRA_8888_GrPixelConfig) && |
| 276 | direct->priv().caps()->isConfigTexturable(kRGBA_8888_GrPixelConfig) && |
| 277 | direct->priv().validPMUPMConversionExists(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 278 | |
| 279 | if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) { |
| 280 | GrSurfaceDesc desc; |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 281 | desc.fWidth = srcInfo.width(); |
| 282 | desc.fHeight = srcInfo.height(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 283 | desc.fSampleCnt = 1; |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 284 | GrColorType colorType; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 285 | |
| 286 | GrBackendFormat format; |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 287 | SkAlphaType alphaType; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 288 | if (canvas2DFastPath) { |
| 289 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 290 | colorType = GrColorType::kRGBA_8888; |
Greg Daniel | 627d053 | 2019-07-08 16:48:14 -0400 | [diff] [blame] | 291 | format = caps->getBackendFormatFromColorType(colorType); |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 292 | alphaType = kUnpremul_SkAlphaType; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 293 | } else { |
| 294 | desc.fConfig = dstProxy->config(); |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 295 | colorType = this->colorSpaceInfo().colorType(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 296 | format = dstProxy->backendFormat().makeTexture2D(); |
| 297 | if (!format.isValid()) { |
| 298 | return false; |
| 299 | } |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 300 | alphaType = this->colorSpaceInfo().alphaType(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 301 | } |
| 302 | |
Greg Daniel | 2e52ad1 | 2019-06-13 10:04:16 -0400 | [diff] [blame] | 303 | // It is more efficient for us to write pixels into a top left origin so we prefer that. |
| 304 | // However, if the final proxy isn't a render target then we must use a copy to move the |
| 305 | // data into it which requires the origins to match. If the final proxy is a render target |
| 306 | // we can use a draw instead which doesn't have this origin restriction. Thus for render |
| 307 | // 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] | 308 | GrSurfaceOrigin tempOrigin = |
| 309 | this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : dstProxy->origin(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 310 | auto tempProxy = direct->priv().proxyProvider()->createProxy( |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 311 | format, desc, GrRenderable::kNo, tempOrigin, SkBackingFit::kApprox, |
| 312 | SkBudgeted::kYes); |
Greg Daniel | 2e52ad1 | 2019-06-13 10:04:16 -0400 | [diff] [blame] | 313 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 314 | if (!tempProxy) { |
| 315 | return false; |
| 316 | } |
| 317 | auto tempCtx = direct->priv().drawingManager()->makeTextureContext( |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 318 | tempProxy, colorType, alphaType, this->colorSpaceInfo().refColorSpace()); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 319 | if (!tempCtx) { |
| 320 | return false; |
| 321 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 322 | |
| 323 | // In the fast path we always write the srcData to the temp context as though it were RGBA. |
| 324 | // When the data is really BGRA the write will cause the R and B channels to be swapped in |
| 325 | // the intermediate surface which gets corrected by a swizzle effect when drawing to the |
| 326 | // dst. |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 327 | if (canvas2DFastPath) { |
| 328 | srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888); |
| 329 | } |
| 330 | if (!tempCtx->writePixels(srcInfo, src, rowBytes, {0, 0}, direct)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 331 | return false; |
| 332 | } |
| 333 | |
| 334 | if (this->asRenderTargetContext()) { |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 335 | std::unique_ptr<GrFragmentProcessor> fp; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 336 | if (canvas2DFastPath) { |
| 337 | fp = direct->priv().createUPMToPMEffect( |
| 338 | GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I())); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 339 | // Important: check the original src color type here! |
| 340 | if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 341 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA()); |
| 342 | } |
| 343 | } else { |
| 344 | fp = GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()); |
| 345 | } |
| 346 | if (!fp) { |
| 347 | return false; |
| 348 | } |
| 349 | GrPaint paint; |
| 350 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 351 | paint.addColorFragmentProcessor(std::move(fp)); |
| 352 | this->asRenderTargetContext()->fillRectToRect( |
| 353 | GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 354 | SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()), |
| 355 | SkRect::MakeWH(srcInfo.width(), srcInfo.height())); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 356 | } else { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 357 | SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height()); |
| 358 | SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 359 | if (!this->copy(tempProxy.get(), srcRect, dstPoint)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 360 | return false; |
| 361 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 362 | } |
| 363 | return true; |
| 364 | } |
| 365 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 366 | GrColorType allowedColorType = |
| 367 | caps->supportedWritePixelsColorType(dstProxy->config(), srcInfo.colorType()); |
| 368 | bool flip = dstProxy->origin() == kBottomLeft_GrSurfaceOrigin; |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 369 | bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes; |
| 370 | bool convert = premul || unpremul || needColorConversion || makeTight || |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 371 | (srcInfo.colorType() != allowedColorType) || flip; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 372 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 373 | std::unique_ptr<char[]> tmpPixels; |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 374 | GrColorType srcColorType = srcInfo.colorType(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 375 | if (convert) { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 376 | GrPixelInfo tmpInfo(allowedColorType, this->colorSpaceInfo().alphaType(), |
| 377 | this->colorSpaceInfo().refColorSpace(), srcInfo.width(), |
| 378 | srcInfo.height()); |
| 379 | auto tmpRB = tmpInfo.minRowBytes(); |
| 380 | tmpPixels.reset(new char[tmpRB * tmpInfo.height()]); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 381 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 382 | GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 383 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 384 | srcColorType = tmpInfo.colorType(); |
| 385 | rowBytes = tmpRB; |
| 386 | src = tmpPixels.get(); |
| 387 | pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a |
| 391 | // complete flush here. On platforms that prefer VRAM use over flushes we're better off |
| 392 | // giving the drawing manager the chance of skipping the flush (i.e., by passing in the |
| 393 | // destination proxy) |
| 394 | // TODO: should this policy decision just be moved into the drawing manager? |
| 395 | direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr); |
| 396 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 397 | return direct->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(), |
| 398 | srcInfo.height(), srcColorType, src, rowBytes); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 399 | } |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 400 | |
| 401 | bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) { |
| 402 | ASSERT_SINGLE_OWNER |
| 403 | RETURN_FALSE_IF_ABANDONED |
| 404 | SkDEBUGCODE(this->validate();) |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 405 | GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy"); |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 406 | |
Brian Salomon | 947efe2 | 2019-07-16 15:36:11 -0400 | [diff] [blame] | 407 | const GrCaps* caps = fContext->priv().caps(); |
| 408 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 409 | SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal); |
| 410 | SkASSERT(src->origin() == this->asSurfaceProxy()->origin()); |
Brian Salomon | 947efe2 | 2019-07-16 15:36:11 -0400 | [diff] [blame] | 411 | SkASSERT(caps->makeConfigSpecific(src->config(), src->backendFormat()) == |
| 412 | caps->makeConfigSpecific(this->asSurfaceProxy()->config(), |
| 413 | this->asSurfaceProxy()->backendFormat())); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 414 | |
| 415 | GrSurfaceProxy* dst = this->asSurfaceProxy(); |
| 416 | |
Brian Salomon | 947efe2 | 2019-07-16 15:36:11 -0400 | [diff] [blame] | 417 | if (!caps->canCopySurface(dst, src, srcRect, dstPoint)) { |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 418 | return false; |
| 419 | } |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 420 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 421 | return this->getOpList()->copySurface(fContext, dst, src, srcRect, dstPoint); |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 422 | } |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 423 | |