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