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