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) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 129 | GrBackendFormat format; |
| 130 | GrPixelConfig config; |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 131 | GrColorType colorType; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 132 | if (canvas2DFastPath) { |
| 133 | config = kRGBA_8888_GrPixelConfig; |
| 134 | format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType); |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 135 | colorType = GrColorType::kRGBA_8888; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 136 | } else { |
| 137 | config = srcProxy->config(); |
| 138 | format = srcProxy->backendFormat().makeTexture2D(); |
| 139 | if (!format.isValid()) { |
| 140 | return false; |
| 141 | } |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 142 | colorType = this->colorSpaceInfo().colorType(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 143 | } |
| 144 | sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorSpaceInfo().refColorSpace(); |
| 145 | |
| 146 | sk_sp<GrRenderTargetContext> tempCtx = direct->priv().makeDeferredRenderTargetContext( |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 147 | format, SkBackingFit::kApprox, dstInfo.width(), dstInfo.height(), config, colorType, |
| 148 | std::move(cs), 1, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin, nullptr, |
| 149 | SkBudgeted::kYes); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 150 | if (!tempCtx) { |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | std::unique_ptr<GrFragmentProcessor> fp; |
| 155 | if (canvas2DFastPath) { |
| 156 | fp = direct->priv().createPMToUPMEffect( |
| 157 | GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()), |
| 158 | SkMatrix::I())); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 159 | if (dstInfo.colorType() == GrColorType::kBGRA_8888) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 160 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA()); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 161 | dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 162 | } |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 163 | // The render target context is incorrectly tagged as kPremul even though we're writing |
| 164 | // unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type so we don't |
| 165 | // double unpremul. |
| 166 | dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 167 | } else { |
| 168 | fp = GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()), SkMatrix::I()); |
| 169 | } |
| 170 | if (!fp) { |
| 171 | return false; |
| 172 | } |
| 173 | GrPaint paint; |
| 174 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 175 | paint.addColorFragmentProcessor(std::move(fp)); |
| 176 | |
| 177 | tempCtx->asRenderTargetContext()->fillRectToRect( |
| 178 | GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 179 | SkRect::MakeWH(dstInfo.width(), dstInfo.height()), |
| 180 | SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height())); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 181 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 182 | return tempCtx->readPixels(dstInfo, dst, rowBytes, {0, 0}, direct); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 183 | } |
| 184 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 185 | bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 186 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 187 | auto supportedRead = caps->supportedReadPixelsColorType( |
| 188 | srcProxy->config(), srcProxy->backendFormat(), dstInfo.colorType()); |
| 189 | |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 190 | bool makeTight = !caps->readPixelsRowBytesSupport() && tightRowBytes != rowBytes; |
| 191 | |
| 192 | bool convert = unpremul || premul || needColorConversion || flip || makeTight || |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 193 | (dstInfo.colorType() != supportedRead.fColorType) || |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 194 | supportedRead.fSwizzle != GrSwizzle::RGBA(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 195 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 196 | std::unique_ptr<char[]> tmpPixels; |
| 197 | GrPixelInfo tmpInfo; |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 198 | void* readDst = dst; |
| 199 | size_t readRB = rowBytes; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 200 | if (convert) { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 201 | tmpInfo = {supportedRead.fColorType, this->colorSpaceInfo().alphaType(), |
| 202 | this->colorSpaceInfo().refColorSpace(), dstInfo.width(), dstInfo.height()}; |
| 203 | size_t tmpRB = tmpInfo.minRowBytes(); |
| 204 | size_t size = tmpRB * tmpInfo.height(); |
| 205 | // Chrome MSAN bots require the data to be initialized (hence the ()). |
| 206 | tmpPixels.reset(new char[size]()); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 207 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 208 | readDst = tmpPixels.get(); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 209 | readRB = tmpRB; |
| 210 | pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | direct->priv().flushSurface(srcProxy); |
| 214 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 215 | if (!direct->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(), |
| 216 | dstInfo.height(), supportedRead.fColorType, readDst, |
| 217 | readRB)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 218 | return false; |
| 219 | } |
| 220 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 221 | if (convert) { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 222 | return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip, |
| 223 | supportedRead.fSwizzle); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 224 | } |
| 225 | return true; |
| 226 | } |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 227 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 228 | bool GrSurfaceContext::writePixels(const GrPixelInfo& origSrcInfo, const void* src, size_t rowBytes, |
| 229 | SkIPoint pt, GrContext* direct) { |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 230 | ASSERT_SINGLE_OWNER |
| 231 | RETURN_FALSE_IF_ABANDONED |
| 232 | SkDEBUGCODE(this->validate();) |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 233 | GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels"); |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 234 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 235 | if (!direct && !(direct = fContext->priv().asDirectContext())) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 236 | return false; |
| 237 | } |
Robert Phillips | 6a6de56 | 2019-02-15 15:19:15 -0500 | [diff] [blame] | 238 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 239 | if (this->asSurfaceProxy()->readOnly()) { |
Robert Phillips | 6a6de56 | 2019-02-15 15:19:15 -0500 | [diff] [blame] | 240 | return false; |
| 241 | } |
| 242 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 243 | if (!src) { |
| 244 | return false; |
| 245 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 246 | |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 247 | size_t tightRowBytes = origSrcInfo.minRowBytes(); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 248 | if (!rowBytes) { |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 249 | rowBytes = tightRowBytes; |
| 250 | } else if (rowBytes < tightRowBytes) { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 251 | return false; |
| 252 | } |
| 253 | |
| 254 | if (!origSrcInfo.isValid()) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 255 | return false; |
| 256 | } |
| 257 | |
| 258 | GrSurfaceProxy* dstProxy = this->asSurfaceProxy(); |
| 259 | if (!dstProxy->instantiate(direct->priv().resourceProvider())) { |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | GrSurface* dstSurface = dstProxy->peekSurface(); |
| 264 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 265 | auto srcInfo = origSrcInfo; |
| 266 | if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 267 | return false; |
| 268 | } |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 269 | // Our tight row bytes may have been changed by clipping. |
| 270 | tightRowBytes = srcInfo.minRowBytes(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 271 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 272 | bool premul = this->colorSpaceInfo().alphaType() == kPremul_SkAlphaType && |
| 273 | srcInfo.alphaType() == kUnpremul_SkAlphaType; |
| 274 | bool unpremul = this->colorSpaceInfo().alphaType() == kUnpremul_SkAlphaType && |
| 275 | srcInfo.alphaType() == kPremul_SkAlphaType; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 276 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 277 | bool needColorConversion = SkColorSpaceXformSteps::Required( |
| 278 | srcInfo.colorSpace(), this->colorSpaceInfo().colorSpace()); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 279 | |
| 280 | const GrCaps* caps = direct->priv().caps(); |
| 281 | // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs |
| 282 | // 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] | 283 | bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion && |
| 284 | (srcInfo.colorType() == GrColorType::kRGBA_8888 || |
| 285 | srcInfo.colorType() == GrColorType::kBGRA_8888) && |
| 286 | SkToBool(this->asRenderTargetContext()) && |
| 287 | (dstProxy->config() == kRGBA_8888_GrPixelConfig || |
| 288 | dstProxy->config() == kBGRA_8888_GrPixelConfig) && |
| 289 | direct->priv().caps()->isConfigTexturable(kRGBA_8888_GrPixelConfig) && |
| 290 | direct->priv().validPMUPMConversionExists(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 291 | |
| 292 | if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) { |
| 293 | GrSurfaceDesc desc; |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 294 | desc.fWidth = srcInfo.width(); |
| 295 | desc.fHeight = srcInfo.height(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 296 | desc.fSampleCnt = 1; |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 297 | GrColorType colorType; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 298 | |
| 299 | GrBackendFormat format; |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 300 | SkAlphaType alphaType; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 301 | if (canvas2DFastPath) { |
| 302 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 303 | colorType = GrColorType::kRGBA_8888; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 304 | format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType); |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 305 | alphaType = kUnpremul_SkAlphaType; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 306 | } else { |
| 307 | desc.fConfig = dstProxy->config(); |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 308 | colorType = this->colorSpaceInfo().colorType(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 309 | format = dstProxy->backendFormat().makeTexture2D(); |
| 310 | if (!format.isValid()) { |
| 311 | return false; |
| 312 | } |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 313 | alphaType = this->colorSpaceInfo().alphaType(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 314 | } |
| 315 | |
Greg Daniel | 2e52ad1 | 2019-06-13 10:04:16 -0400 | [diff] [blame] | 316 | // It is more efficient for us to write pixels into a top left origin so we prefer that. |
| 317 | // However, if the final proxy isn't a render target then we must use a copy to move the |
| 318 | // data into it which requires the origins to match. If the final proxy is a render target |
| 319 | // we can use a draw instead which doesn't have this origin restriction. Thus for render |
| 320 | // 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] | 321 | GrSurfaceOrigin tempOrigin = |
| 322 | this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : dstProxy->origin(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 323 | auto tempProxy = direct->priv().proxyProvider()->createProxy( |
Greg Daniel | 2e52ad1 | 2019-06-13 10:04:16 -0400 | [diff] [blame] | 324 | format, desc, tempOrigin, SkBackingFit::kApprox, SkBudgeted::kYes); |
| 325 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 326 | if (!tempProxy) { |
| 327 | return false; |
| 328 | } |
| 329 | auto tempCtx = direct->priv().drawingManager()->makeTextureContext( |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 330 | tempProxy, colorType, alphaType, this->colorSpaceInfo().refColorSpace()); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 331 | if (!tempCtx) { |
| 332 | return false; |
| 333 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 334 | |
| 335 | // In the fast path we always write the srcData to the temp context as though it were RGBA. |
| 336 | // When the data is really BGRA the write will cause the R and B channels to be swapped in |
| 337 | // the intermediate surface which gets corrected by a swizzle effect when drawing to the |
| 338 | // dst. |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 339 | if (canvas2DFastPath) { |
| 340 | srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888); |
| 341 | } |
| 342 | if (!tempCtx->writePixels(srcInfo, src, rowBytes, {0, 0}, direct)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 343 | return false; |
| 344 | } |
| 345 | |
| 346 | if (this->asRenderTargetContext()) { |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 347 | std::unique_ptr<GrFragmentProcessor> fp; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 348 | if (canvas2DFastPath) { |
| 349 | fp = direct->priv().createUPMToPMEffect( |
| 350 | GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I())); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 351 | // Important: check the original src color type here! |
| 352 | if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 353 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA()); |
| 354 | } |
| 355 | } else { |
| 356 | fp = GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()); |
| 357 | } |
| 358 | if (!fp) { |
| 359 | return false; |
| 360 | } |
| 361 | GrPaint paint; |
| 362 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 363 | paint.addColorFragmentProcessor(std::move(fp)); |
| 364 | this->asRenderTargetContext()->fillRectToRect( |
| 365 | GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 366 | SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()), |
| 367 | SkRect::MakeWH(srcInfo.width(), srcInfo.height())); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 368 | } else { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 369 | SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height()); |
| 370 | SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 371 | if (!this->copy(tempProxy.get(), srcRect, dstPoint)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 372 | return false; |
| 373 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 374 | } |
| 375 | return true; |
| 376 | } |
| 377 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 378 | GrColorType allowedColorType = |
| 379 | caps->supportedWritePixelsColorType(dstProxy->config(), srcInfo.colorType()); |
| 380 | bool flip = dstProxy->origin() == kBottomLeft_GrSurfaceOrigin; |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 381 | bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes; |
| 382 | bool convert = premul || unpremul || needColorConversion || makeTight || |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 383 | (srcInfo.colorType() != allowedColorType) || flip; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 384 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 385 | std::unique_ptr<char[]> tmpPixels; |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 386 | GrColorType srcColorType = srcInfo.colorType(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 387 | if (convert) { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 388 | GrPixelInfo tmpInfo(allowedColorType, this->colorSpaceInfo().alphaType(), |
| 389 | this->colorSpaceInfo().refColorSpace(), srcInfo.width(), |
| 390 | srcInfo.height()); |
| 391 | auto tmpRB = tmpInfo.minRowBytes(); |
| 392 | tmpPixels.reset(new char[tmpRB * tmpInfo.height()]); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 393 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 394 | GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 395 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 396 | srcColorType = tmpInfo.colorType(); |
| 397 | rowBytes = tmpRB; |
| 398 | src = tmpPixels.get(); |
| 399 | pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a |
| 403 | // complete flush here. On platforms that prefer VRAM use over flushes we're better off |
| 404 | // giving the drawing manager the chance of skipping the flush (i.e., by passing in the |
| 405 | // destination proxy) |
| 406 | // TODO: should this policy decision just be moved into the drawing manager? |
| 407 | direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr); |
| 408 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 409 | return direct->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(), |
| 410 | srcInfo.height(), srcColorType, src, rowBytes); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 411 | } |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 412 | |
| 413 | bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) { |
| 414 | ASSERT_SINGLE_OWNER |
| 415 | RETURN_FALSE_IF_ABANDONED |
| 416 | SkDEBUGCODE(this->validate();) |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 417 | GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy"); |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 418 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 419 | SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal); |
| 420 | SkASSERT(src->origin() == this->asSurfaceProxy()->origin()); |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 421 | SkASSERT(src->config() == this->asSurfaceProxy()->config()); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 422 | |
| 423 | GrSurfaceProxy* dst = this->asSurfaceProxy(); |
| 424 | |
| 425 | if (!fContext->priv().caps()->canCopySurface(dst, src, srcRect, dstPoint)) { |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 426 | return false; |
| 427 | } |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 428 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 429 | return this->getOpList()->copySurface(fContext, dst, src, srcRect, 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 | |