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 | |
Brian Salomon | 9c6f6bf | 2020-05-29 16:37:26 -0400 | [diff] [blame] | 10 | #include "include/effects/SkRuntimeEffect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/private/GrRecordingContext.h" |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 12 | #include "src/core/SkAutoPixmapStorage.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrAuditTrail.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" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 19 | #include "src/gpu/GrProxyProvider.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/gpu/GrRecordingContextPriv.h" |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 21 | #include "src/gpu/GrRenderTargetContext.h" |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 22 | #include "src/gpu/GrSurfaceContextPriv.h" |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 23 | #include "src/gpu/GrSurfacePriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/gpu/SkGr.h" |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 25 | #include "src/gpu/effects/GrBicubicEffect.h" |
Brian Salomon | 9c6f6bf | 2020-05-29 16:37:26 -0400 | [diff] [blame] | 26 | #include "src/gpu/effects/GrSkSLFP.h" |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 27 | |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 28 | #define ASSERT_SINGLE_OWNER \ |
| 29 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());) |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 30 | #define RETURN_FALSE_IF_ABANDONED if (this->fContext->priv().abandoned()) { return false; } |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 31 | |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 32 | std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context, |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 33 | GrSurfaceProxyView readView, |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 34 | GrColorType colorType, |
| 35 | SkAlphaType alphaType, |
| 36 | sk_sp<SkColorSpace> colorSpace) { |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 37 | // It is probably not necessary to check if the context is abandoned here since uses of the |
| 38 | // GrSurfaceContext which need the context will mostly likely fail later on without an issue. |
| 39 | // However having this hear adds some reassurance in case there is a path doesn't handle an |
| 40 | // abandoned context correctly. It also lets us early out of some extra work. |
| 41 | if (context->priv().abandoned()) { |
| 42 | return nullptr; |
| 43 | } |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 44 | GrSurfaceProxy* proxy = readView.proxy(); |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 45 | SkASSERT(proxy && proxy->asTextureProxy()); |
| 46 | |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 47 | std::unique_ptr<GrSurfaceContext> surfaceContext; |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 48 | if (proxy->asRenderTargetProxy()) { |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 49 | SkASSERT(kPremul_SkAlphaType == alphaType || kOpaque_SkAlphaType == alphaType); |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 50 | // Will we ever want a swizzle that is not the default write swizzle for the format and |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 51 | // colorType here? If so we will need to manually pass that in. |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 52 | GrSwizzle writeSwizzle; |
| 53 | if (colorType != GrColorType::kUnknown) { |
| 54 | writeSwizzle = |
| 55 | context->priv().caps()->getWriteSwizzle(proxy->backendFormat(), colorType); |
| 56 | } |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 57 | GrSurfaceProxyView writeView(readView.refProxy(), readView.origin(), writeSwizzle); |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 58 | surfaceContext.reset(new GrRenderTargetContext(context, std::move(readView), |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 59 | std::move(writeView), colorType, |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 60 | std::move(colorSpace), nullptr)); |
| 61 | } else { |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 62 | surfaceContext.reset(new GrSurfaceContext(context, std::move(readView), colorType, |
| 63 | alphaType, std::move(colorSpace))); |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 64 | } |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 65 | SkDEBUGCODE(surfaceContext->validate();) |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 66 | return surfaceContext; |
| 67 | } |
| 68 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 69 | std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context, |
| 70 | SkISize dimensions, |
| 71 | const GrBackendFormat& format, |
| 72 | GrRenderable renderable, |
| 73 | int renderTargetSampleCnt, |
| 74 | GrMipMapped mipMapped, |
| 75 | GrProtected isProtected, |
| 76 | GrSurfaceOrigin origin, |
| 77 | GrColorType colorType, |
| 78 | SkAlphaType alphaType, |
| 79 | sk_sp<SkColorSpace> colorSpace, |
| 80 | SkBackingFit fit, |
| 81 | SkBudgeted budgeted) { |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 82 | GrSwizzle swizzle; |
| 83 | if (colorType != GrColorType::kUnknown && !context->priv().caps()->isFormatCompressed(format)) { |
Brian Salomon | d005b69 | 2020-04-01 15:47:05 -0400 | [diff] [blame] | 84 | swizzle = context->priv().caps()->getReadSwizzle(format, colorType); |
| 85 | } |
Greg Daniel | 47c20e8 | 2020-01-21 14:29:57 -0500 | [diff] [blame] | 86 | |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 87 | sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy( |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 88 | format, dimensions, renderable, renderTargetSampleCnt, mipMapped, fit, budgeted, |
| 89 | isProtected); |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 90 | if (!proxy) { |
| 91 | return nullptr; |
| 92 | } |
| 93 | |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 94 | GrSurfaceProxyView view(std::move(proxy), origin, swizzle); |
| 95 | return GrSurfaceContext::Make(context, std::move(view), colorType, alphaType, |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 96 | std::move(colorSpace)); |
| 97 | } |
| 98 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 99 | // In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress |
| 100 | // GrOpsTasks to be picked up and added to by renderTargetContexts lower in the call |
| 101 | // stack. When this occurs with a closed GrOpsTask, a new one will be allocated |
| 102 | // when the renderTargetContext attempts to use it (via getOpsTask). |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 103 | GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context, |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 104 | GrSurfaceProxyView readView, |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 105 | GrColorType colorType, |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 106 | SkAlphaType alphaType, |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 107 | sk_sp<SkColorSpace> colorSpace) |
Greg Daniel | 901b98e | 2019-10-22 09:54:02 -0400 | [diff] [blame] | 108 | : fContext(context) |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 109 | , fReadView(std::move(readView)) |
| 110 | , fColorInfo(colorType, alphaType, std::move(colorSpace)) { |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 111 | SkASSERT(!context->priv().abandoned()); |
| 112 | } |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 113 | |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 114 | const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); } |
| 115 | |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 116 | GrAuditTrail* GrSurfaceContext::auditTrail() { |
| 117 | return fContext->priv().auditTrail(); |
| 118 | } |
| 119 | |
| 120 | GrDrawingManager* GrSurfaceContext::drawingManager() { |
| 121 | return fContext->priv().drawingManager(); |
| 122 | } |
| 123 | |
| 124 | const GrDrawingManager* GrSurfaceContext::drawingManager() const { |
| 125 | return fContext->priv().drawingManager(); |
| 126 | } |
| 127 | |
| 128 | #ifdef SK_DEBUG |
| 129 | GrSingleOwner* GrSurfaceContext::singleOwner() { |
| 130 | return fContext->priv().singleOwner(); |
| 131 | } |
| 132 | #endif |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 133 | |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 134 | bool GrSurfaceContext::readPixels(const GrImageInfo& origDstInfo, void* dst, size_t rowBytes, |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 135 | SkIPoint pt, GrContext* direct) { |
| 136 | ASSERT_SINGLE_OWNER |
| 137 | RETURN_FALSE_IF_ABANDONED |
| 138 | SkDEBUGCODE(this->validate();) |
| 139 | GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels"); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 140 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 141 | if (!direct && !(direct = fContext->priv().asDirectContext())) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 142 | return false; |
| 143 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 144 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 145 | if (!dst) { |
| 146 | return false; |
| 147 | } |
| 148 | |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 149 | size_t tightRowBytes = origDstInfo.minRowBytes(); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 150 | if (!rowBytes) { |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 151 | rowBytes = tightRowBytes; |
| 152 | } else if (rowBytes < tightRowBytes) { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 153 | return false; |
| 154 | } |
| 155 | |
| 156 | if (!origDstInfo.isValid()) { |
| 157 | return false; |
| 158 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 159 | |
| 160 | GrSurfaceProxy* srcProxy = this->asSurfaceProxy(); |
| 161 | |
Stephen White | 3c0a50f | 2020-01-16 18:19:54 -0500 | [diff] [blame] | 162 | if (srcProxy->framebufferOnly()) { |
| 163 | return false; |
| 164 | } |
| 165 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 166 | // MDB TODO: delay this instantiation until later in the method |
| 167 | if (!srcProxy->instantiate(direct->priv().resourceProvider())) { |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | GrSurface* srcSurface = srcProxy->peekSurface(); |
| 172 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 173 | auto dstInfo = origDstInfo; |
| 174 | if (!dstInfo.clip(this->width(), this->height(), &pt, &dst, rowBytes)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 175 | return false; |
| 176 | } |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 177 | // Our tight row bytes may have been changed by clipping. |
| 178 | tightRowBytes = dstInfo.minRowBytes(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 179 | |
Mike Klein | 7321e6a | 2019-12-03 11:08:40 -0600 | [diff] [blame] | 180 | SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{this->colorInfo(), dstInfo}.flags; |
| 181 | bool unpremul = flags.unpremul, |
| 182 | needColorConversion = flags.linearize || flags.gamut_transform || flags.encode, |
| 183 | premul = flags.premul; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 184 | |
| 185 | const GrCaps* caps = direct->priv().caps(); |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 186 | bool srcIsCompressed = caps->isFormatCompressed(srcSurface->backendFormat()); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 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); |
Greg Daniel | c71c796 | 2020-01-14 16:44:18 -0500 | [diff] [blame] | 194 | GrColorType srcColorType = this->colorInfo().colorType(); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 195 | bool canvas2DFastPath = unpremul && !needColorConversion && |
| 196 | (GrColorType::kRGBA_8888 == dstInfo.colorType() || |
| 197 | GrColorType::kBGRA_8888 == dstInfo.colorType()) && |
| 198 | SkToBool(srcProxy->asTextureProxy()) && |
Greg Daniel | c71c796 | 2020-01-14 16:44:18 -0500 | [diff] [blame] | 199 | (srcColorType == GrColorType::kRGBA_8888 || |
| 200 | srcColorType == GrColorType::kBGRA_8888) && |
Greg Daniel | 0258c90 | 2019-08-01 13:08:33 -0400 | [diff] [blame] | 201 | defaultRGBAFormat.isValid() && |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 202 | direct->priv().validPMUPMConversionExists(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 203 | |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 204 | auto readFlag = caps->surfaceSupportsReadPixels(srcSurface); |
Brian Salomon | dc0710f | 2019-07-01 14:59:32 -0400 | [diff] [blame] | 205 | if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) { |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 206 | return false; |
| 207 | } |
| 208 | |
Brian Salomon | dc0710f | 2019-07-01 14:59:32 -0400 | [diff] [blame] | 209 | if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) { |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 210 | GrColorType colorType = (canvas2DFastPath || srcIsCompressed) |
| 211 | ? GrColorType::kRGBA_8888 : this->colorInfo().colorType(); |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 212 | sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorInfo().refColorSpace(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 213 | |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 214 | auto tempCtx = GrRenderTargetContext::Make( |
| 215 | direct, colorType, std::move(cs), SkBackingFit::kApprox, dstInfo.dimensions(), |
| 216 | 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 217 | if (!tempCtx) { |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | std::unique_ptr<GrFragmentProcessor> fp; |
| 222 | if (canvas2DFastPath) { |
Greg Daniel | d2ccbb5 | 2020-02-05 10:45:39 -0500 | [diff] [blame] | 223 | fp = direct->priv().createPMToUPMEffect( |
| 224 | GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType())); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 225 | if (dstInfo.colorType() == GrColorType::kBGRA_8888) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 226 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA()); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 227 | dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 228 | } |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 229 | // The render target context is incorrectly tagged as kPremul even though we're writing |
| 230 | // unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type so we don't |
| 231 | // double unpremul. |
| 232 | dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 233 | } else { |
Greg Daniel | d2ccbb5 | 2020-02-05 10:45:39 -0500 | [diff] [blame] | 234 | fp = GrTextureEffect::Make(this->readSurfaceView(), 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( |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 244 | nullptr, 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 | b8d84f8 | 2020-02-13 14:25:00 -0500 | [diff] [blame] | 251 | bool flip = this->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 | |
Greg Daniel | 55f040b | 2020-02-13 15:38:32 +0000 | [diff] [blame] | 278 | direct->priv().flushSurface(srcProxy); |
Greg Daniel | 04283f3 | 2020-05-20 13:16:00 -0400 | [diff] [blame] | 279 | direct->submit(); |
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(); |
Stephen White | 3c0a50f | 2020-01-16 18:19:54 -0500 | [diff] [blame] | 323 | |
| 324 | if (dstProxy->framebufferOnly()) { |
| 325 | return false; |
| 326 | } |
| 327 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 328 | if (!dstProxy->instantiate(direct->priv().resourceProvider())) { |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | GrSurface* dstSurface = dstProxy->peekSurface(); |
| 333 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 334 | auto srcInfo = origSrcInfo; |
| 335 | if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 336 | return false; |
| 337 | } |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 338 | // Our tight row bytes may have been changed by clipping. |
| 339 | tightRowBytes = srcInfo.minRowBytes(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 340 | |
Mike Klein | 7321e6a | 2019-12-03 11:08:40 -0600 | [diff] [blame] | 341 | SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{srcInfo, this->colorInfo()}.flags; |
| 342 | bool unpremul = flags.unpremul, |
| 343 | needColorConversion = flags.linearize || flags.gamut_transform || flags.encode, |
| 344 | premul = flags.premul; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 345 | |
| 346 | const GrCaps* caps = direct->priv().caps(); |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 347 | |
| 348 | auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888, |
| 349 | GrRenderable::kNo); |
| 350 | |
Greg Daniel | c71c796 | 2020-01-14 16:44:18 -0500 | [diff] [blame] | 351 | GrColorType dstColorType = this->colorInfo().colorType(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 352 | // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs |
| 353 | // 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] | 354 | bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion && |
| 355 | (srcInfo.colorType() == GrColorType::kRGBA_8888 || |
| 356 | srcInfo.colorType() == GrColorType::kBGRA_8888) && |
| 357 | SkToBool(this->asRenderTargetContext()) && |
Greg Daniel | c71c796 | 2020-01-14 16:44:18 -0500 | [diff] [blame] | 358 | (dstColorType == GrColorType::kRGBA_8888 || |
| 359 | dstColorType == GrColorType::kBGRA_8888) && |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 360 | rgbaDefaultFormat.isValid() && |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 361 | direct->priv().validPMUPMConversionExists(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 362 | |
| 363 | if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) { |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 364 | GrColorType colorType; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 365 | |
| 366 | GrBackendFormat format; |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 367 | SkAlphaType alphaType; |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 368 | GrSwizzle tempReadSwizzle; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 369 | if (canvas2DFastPath) { |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 370 | colorType = GrColorType::kRGBA_8888; |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 371 | format = rgbaDefaultFormat; |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 372 | alphaType = kUnpremul_SkAlphaType; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 373 | } else { |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 374 | colorType = this->colorInfo().colorType(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 375 | format = dstProxy->backendFormat().makeTexture2D(); |
| 376 | if (!format.isValid()) { |
| 377 | return false; |
| 378 | } |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 379 | alphaType = this->colorInfo().alphaType(); |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 380 | tempReadSwizzle = this->readSwizzle(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 381 | } |
| 382 | |
Greg Daniel | 2e52ad1 | 2019-06-13 10:04:16 -0400 | [diff] [blame] | 383 | // It is more efficient for us to write pixels into a top left origin so we prefer that. |
| 384 | // However, if the final proxy isn't a render target then we must use a copy to move the |
| 385 | // data into it which requires the origins to match. If the final proxy is a render target |
| 386 | // we can use a draw instead which doesn't have this origin restriction. Thus for render |
| 387 | // 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] | 388 | GrSurfaceOrigin tempOrigin = |
Greg Daniel | b8d84f8 | 2020-02-13 14:25:00 -0500 | [diff] [blame] | 389 | this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : this->origin(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 390 | auto tempProxy = direct->priv().proxyProvider()->createProxy( |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 391 | format, srcInfo.dimensions(), GrRenderable::kNo, 1, GrMipMapped::kNo, |
| 392 | SkBackingFit::kApprox, SkBudgeted::kYes, GrProtected::kNo); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 393 | if (!tempProxy) { |
| 394 | return false; |
| 395 | } |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 396 | GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle); |
Greg Daniel | d2ccbb5 | 2020-02-05 10:45:39 -0500 | [diff] [blame] | 397 | GrSurfaceContext tempCtx(direct, tempView, colorType, alphaType, |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 398 | this->colorInfo().refColorSpace()); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 399 | |
| 400 | // In the fast path we always write the srcData to the temp context as though it were RGBA. |
| 401 | // When the data is really BGRA the write will cause the R and B channels to be swapped in |
| 402 | // the intermediate surface which gets corrected by a swizzle effect when drawing to the |
| 403 | // dst. |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 404 | if (canvas2DFastPath) { |
| 405 | srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888); |
| 406 | } |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 407 | if (!tempCtx.writePixels(srcInfo, src, rowBytes, {0, 0}, direct)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 408 | return false; |
| 409 | } |
| 410 | |
| 411 | if (this->asRenderTargetContext()) { |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 412 | std::unique_ptr<GrFragmentProcessor> fp; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 413 | if (canvas2DFastPath) { |
Brian Salomon | b8f098d | 2020-01-07 11:15:44 -0500 | [diff] [blame] | 414 | fp = direct->priv().createUPMToPMEffect( |
Greg Daniel | d2ccbb5 | 2020-02-05 10:45:39 -0500 | [diff] [blame] | 415 | GrTextureEffect::Make(std::move(tempView), alphaType)); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 416 | // Important: check the original src color type here! |
| 417 | if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 418 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA()); |
| 419 | } |
| 420 | } else { |
Greg Daniel | d2ccbb5 | 2020-02-05 10:45:39 -0500 | [diff] [blame] | 421 | fp = GrTextureEffect::Make(std::move(tempView), alphaType); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 422 | } |
| 423 | if (!fp) { |
| 424 | return false; |
| 425 | } |
| 426 | GrPaint paint; |
| 427 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 428 | paint.addColorFragmentProcessor(std::move(fp)); |
| 429 | this->asRenderTargetContext()->fillRectToRect( |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 430 | nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 431 | SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()), |
| 432 | SkRect::MakeWH(srcInfo.width(), srcInfo.height())); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 433 | } else { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 434 | SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height()); |
| 435 | SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY); |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 436 | if (!this->copy(tempProxy.get(), srcRect, dstPoint)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 437 | return false; |
| 438 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 439 | } |
| 440 | return true; |
| 441 | } |
| 442 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 443 | GrColorType allowedColorType = |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 444 | caps->supportedWritePixelsColorType(this->colorInfo().colorType(), |
Brian Salomon | 01915c0 | 2019-08-02 09:57:21 -0400 | [diff] [blame] | 445 | dstProxy->backendFormat(), |
| 446 | srcInfo.colorType()).fColorType; |
Greg Daniel | b8d84f8 | 2020-02-13 14:25:00 -0500 | [diff] [blame] | 447 | bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin; |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 448 | bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes; |
| 449 | bool convert = premul || unpremul || needColorConversion || makeTight || |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 450 | (srcInfo.colorType() != allowedColorType) || flip; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 451 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 452 | std::unique_ptr<char[]> tmpPixels; |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 453 | GrColorType srcColorType = srcInfo.colorType(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 454 | if (convert) { |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 455 | GrImageInfo tmpInfo(allowedColorType, this->colorInfo().alphaType(), |
| 456 | this->colorInfo().refColorSpace(), srcInfo.width(), srcInfo.height()); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 457 | auto tmpRB = tmpInfo.minRowBytes(); |
| 458 | tmpPixels.reset(new char[tmpRB * tmpInfo.height()]); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 459 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 460 | GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 461 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 462 | srcColorType = tmpInfo.colorType(); |
| 463 | rowBytes = tmpRB; |
| 464 | src = tmpPixels.get(); |
| 465 | pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a |
| 469 | // complete flush here. On platforms that prefer VRAM use over flushes we're better off |
| 470 | // giving the drawing manager the chance of skipping the flush (i.e., by passing in the |
| 471 | // destination proxy) |
| 472 | // TODO: should this policy decision just be moved into the drawing manager? |
Greg Daniel | 55f040b | 2020-02-13 15:38:32 +0000 | [diff] [blame] | 473 | direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 474 | |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 475 | return direct->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(), |
| 476 | srcInfo.height(), this->colorInfo().colorType(), |
| 477 | srcColorType, src, rowBytes); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 478 | } |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 479 | |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 480 | bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) { |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 481 | ASSERT_SINGLE_OWNER |
| 482 | RETURN_FALSE_IF_ABANDONED |
| 483 | SkDEBUGCODE(this->validate();) |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 484 | GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy"); |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 485 | |
Brian Salomon | 947efe2 | 2019-07-16 15:36:11 -0400 | [diff] [blame] | 486 | const GrCaps* caps = fContext->priv().caps(); |
| 487 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 488 | SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal); |
Greg Daniel | c71c796 | 2020-01-14 16:44:18 -0500 | [diff] [blame] | 489 | SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat()); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 490 | |
Stephen White | 3c0a50f | 2020-01-16 18:19:54 -0500 | [diff] [blame] | 491 | if (this->asSurfaceProxy()->framebufferOnly()) { |
| 492 | return false; |
| 493 | } |
| 494 | |
Chris Dalton | f8e5aad | 2019-08-02 12:55:23 -0600 | [diff] [blame] | 495 | if (!caps->canCopySurface(this->asSurfaceProxy(), src, srcRect, dstPoint)) { |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 496 | return false; |
| 497 | } |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 498 | |
Greg Daniel | 16f5c65 | 2019-10-29 11:26:01 -0400 | [diff] [blame] | 499 | // The swizzle doesn't matter for copies and it is not used. |
| 500 | return this->drawingManager()->newCopyRenderTask( |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 501 | GrSurfaceProxyView(sk_ref_sp(src), this->origin(), GrSwizzle("rgba")), srcRect, |
Greg Daniel | 46e366a | 2019-12-16 14:38:36 -0500 | [diff] [blame] | 502 | this->readSurfaceView(), dstPoint); |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 503 | } |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 504 | |
Brian Salomon | 9c6f6bf | 2020-05-29 16:37:26 -0400 | [diff] [blame] | 505 | template <int N> static std::unique_ptr<GrSkSLFP> make_avg_effect(GrRecordingContext* context) { |
| 506 | static sk_sp<SkRuntimeEffect> gEffect; |
| 507 | static SkString gName; |
| 508 | if (!gEffect) { |
| 509 | SkString string; |
| 510 | for (int i = 0; i < N; ++i) { |
| 511 | string.appendf("in fragmentProcessor child%d;\n", i); |
| 512 | } |
| 513 | string.append("void main(float2 p, inout half4 color) {\n"); |
| 514 | for (int i = 0; i < N; ++i) { |
| 515 | string.appendf(" color %c= sample(child%d, p);\n", i ? '+' : ' ', i); |
| 516 | } |
| 517 | string.appendf(" color /= half(%d);\n" |
| 518 | "}\n", N); |
| 519 | gEffect = std::get<0>(SkRuntimeEffect::Make(std::move(string))); |
| 520 | SkASSERT(gEffect); |
| 521 | gName.printf("Avg%d", N); |
| 522 | } |
| 523 | return GrSkSLFP::Make(context, gEffect, gName.c_str(), nullptr); |
| 524 | } |
| 525 | |
| 526 | template <int NX, int NY> |
| 527 | static std::unique_ptr<GrFragmentProcessor> make_multibilerp_effect(GrRecordingContext* context, |
| 528 | GrSurfaceProxyView srcView, |
| 529 | SkAlphaType alphaType, |
| 530 | const SkIRect& srcRect, |
| 531 | const SkISize& dstSize) { |
| 532 | auto effect = make_avg_effect<NX*NY>(context); |
| 533 | |
| 534 | // scale factors. |
| 535 | float sx = static_cast<float>(srcRect.width()) /dstSize.width(), |
| 536 | sy = static_cast<float>(srcRect.height())/dstSize.height(); |
| 537 | // spacing between bilerp samples. |
| 538 | float dx = sx/NX, |
| 539 | dy = sy/NY; |
| 540 | // offset in src from back projection of dst pixel center to left/upper-most bilerp sample. |
| 541 | float x0 = (dx - sx)/2, |
| 542 | y0 = (dy - sy)/2; |
| 543 | |
| 544 | const auto& caps = *context->priv().caps(); |
| 545 | for (int j = 0; j < NY; ++j) { |
| 546 | for (int i = 0; i < NX; ++i) { |
| 547 | float tx = x0 + i*dx, |
| 548 | ty = y0 + j*dy; |
| 549 | SkMatrix m = SkMatrix::Scale(sx, sy); |
| 550 | m.postTranslate(tx + srcRect.x(), ty + srcRect.y()); |
| 551 | SkRect domain = SkRect::Make(srcRect).makeInset(sx/2, sy/2).makeOffset(tx, ty); |
| 552 | effect->addChild(GrTextureEffect::MakeSubset(srcView, alphaType, m, |
| 553 | GrSamplerState::Filter::kBilerp, |
| 554 | SkRect::Make(srcRect), domain, caps)); |
| 555 | } |
| 556 | } |
| 557 | return std::move(effect); |
| 558 | } |
| 559 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 560 | std::unique_ptr<GrRenderTargetContext> GrSurfaceContext::rescale( |
Brian Salomon | 11ad4cc | 2020-05-15 12:07:59 -0400 | [diff] [blame] | 561 | const GrImageInfo& info, |
| 562 | GrSurfaceOrigin origin, |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 563 | SkIRect srcRect, |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 564 | SkSurface::RescaleGamma rescaleGamma, |
| 565 | SkFilterQuality rescaleQuality) { |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 566 | auto rtProxy = this->asRenderTargetProxy(); |
| 567 | if (rtProxy && rtProxy->wrapsVkSecondaryCB()) { |
| 568 | return nullptr; |
| 569 | } |
| 570 | |
Stephen White | 3c0a50f | 2020-01-16 18:19:54 -0500 | [diff] [blame] | 571 | if (this->asSurfaceProxy()->framebufferOnly()) { |
| 572 | return nullptr; |
| 573 | } |
| 574 | |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 575 | // We rescale by drawing and don't currently support drawing to a kUnpremul destination. |
| 576 | if (info.alphaType() == kUnpremul_SkAlphaType) { |
| 577 | return nullptr; |
| 578 | } |
| 579 | |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 580 | GrSurfaceProxyView texView = this->readSurfaceView(); |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 581 | SkAlphaType srcAlphaType = this->colorInfo().alphaType(); |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 582 | if (!texView.asTextureProxy()) { |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 583 | texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipMapped::kNo, srcRect, |
| 584 | SkBackingFit::kApprox, SkBudgeted::kNo); |
| 585 | if (!texView) { |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 586 | return nullptr; |
| 587 | } |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 588 | SkASSERT(texView.asTextureProxy()); |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 589 | srcRect = SkIRect::MakeSize(srcRect.size()); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 590 | } |
| 591 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 592 | // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the |
| 593 | // pass B is moved to A. If 'this' is the input on the first pass then tempA is null. |
| 594 | std::unique_ptr<GrRenderTargetContext> tempA; |
| 595 | std::unique_ptr<GrRenderTargetContext> tempB; |
| 596 | |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 597 | // Assume we should ignore the rescale linear request if the surface has no color space since |
| 598 | // it's unclear how we'd linearize from an unknown color space. |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 599 | if (rescaleGamma == SkSurface::kLinear && this->colorInfo().colorSpace() && |
| 600 | !this->colorInfo().colorSpace()->gammaIsLinear()) { |
| 601 | auto cs = this->colorInfo().colorSpace()->makeLinearGamma(); |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 602 | auto xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(), srcAlphaType, cs.get(), |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 603 | kPremul_SkAlphaType); |
| 604 | // We'll fall back to kRGBA_8888 if half float not supported. |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 605 | auto linearRTC = GrRenderTargetContext::MakeWithFallback( |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 606 | fContext, GrColorType::kRGBA_F16, cs, SkBackingFit::kApprox, srcRect.size(), 1, |
Brian Salomon | 11ad4cc | 2020-05-15 12:07:59 -0400 | [diff] [blame] | 607 | GrMipMapped::kNo, GrProtected::kNo, origin); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 608 | if (!linearRTC) { |
| 609 | return nullptr; |
| 610 | } |
Brian Salomon | 11ad4cc | 2020-05-15 12:07:59 -0400 | [diff] [blame] | 611 | // 1-to-1 draw can always be kFast. |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 612 | linearRTC->drawTexture(nullptr, std::move(texView), srcAlphaType, |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 613 | GrSamplerState::Filter::kNearest, SkBlendMode::kSrc, |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 614 | SK_PMColor4fWHITE, SkRect::Make(srcRect), |
| 615 | SkRect::Make(srcRect.size()), GrAA::kNo, GrQuadAAFlags::kNone, |
| 616 | SkCanvas::kFast_SrcRectConstraint, SkMatrix::I(), std::move(xform)); |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 617 | texView = linearRTC->readSurfaceView(); |
| 618 | SkASSERT(texView.asTextureProxy()); |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 619 | tempA = std::move(linearRTC); |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 620 | srcRect = SkIRect::MakeSize(srcRect.size()); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 621 | } |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 622 | |
Brian Salomon | 9c6f6bf | 2020-05-29 16:37:26 -0400 | [diff] [blame] | 623 | enum StepType { |
| 624 | kNearest, |
| 625 | kBilinear, |
| 626 | kFourTapBilinear, |
| 627 | kBicubic, |
| 628 | }; |
| 629 | auto determineNextStep = [rescaleQuality, finalSize = info.dimensions()](SkISize srcSize) { |
| 630 | if (rescaleQuality == kNone_SkFilterQuality) { |
Brian Salomon | 7a34aff | 2020-05-29 22:47:54 -0400 | [diff] [blame] | 631 | return std::make_tuple(StepType::kNearest, finalSize); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 632 | } |
Brian Salomon | 9c6f6bf | 2020-05-29 16:37:26 -0400 | [diff] [blame] | 633 | SkISize nextSize; |
| 634 | if (srcSize.width() > finalSize.width()) { |
| 635 | nextSize.fWidth = std::max((srcSize.width() + 1)/2, finalSize.width()); |
| 636 | } else { |
| 637 | nextSize.fWidth = std::min(srcSize.width()*2, finalSize.width()); |
| 638 | } |
| 639 | if (srcSize.height() > finalSize.height()) { |
| 640 | nextSize.fHeight = std::max((srcSize.height() + 1)/2, finalSize.height()); |
| 641 | } else { |
| 642 | nextSize.fHeight = std::min(srcSize.height()*2, finalSize.height()); |
| 643 | } |
| 644 | if (rescaleQuality == kHigh_SkFilterQuality) { |
Brian Salomon | 7a34aff | 2020-05-29 22:47:54 -0400 | [diff] [blame] | 645 | return std::make_tuple(StepType::kBicubic, nextSize); |
Brian Salomon | 9c6f6bf | 2020-05-29 16:37:26 -0400 | [diff] [blame] | 646 | } |
| 647 | // See if we can do multiple bilinear steps in one. |
| 648 | if (nextSize.width() > finalSize.width() && nextSize.height() > finalSize.height()) { |
| 649 | nextSize = {std::max((nextSize.width() + 1)/2, finalSize.width()), |
| 650 | std::max((nextSize.height() + 1)/2, finalSize.height())}; |
Brian Salomon | 7a34aff | 2020-05-29 22:47:54 -0400 | [diff] [blame] | 651 | return std::make_tuple(StepType::kFourTapBilinear, nextSize); |
Brian Salomon | 9c6f6bf | 2020-05-29 16:37:26 -0400 | [diff] [blame] | 652 | } |
Brian Salomon | 7a34aff | 2020-05-29 22:47:54 -0400 | [diff] [blame] | 653 | return std::make_tuple(StepType::kBilinear, nextSize); |
Brian Salomon | 9c6f6bf | 2020-05-29 16:37:26 -0400 | [diff] [blame] | 654 | }; |
| 655 | while (srcRect.size() != info.dimensions()) { |
| 656 | auto [stepType, nextDims] = determineNextStep(srcRect.size()); |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 657 | auto input = tempA ? tempA.get() : this; |
Brian Salomon | 9c6f6bf | 2020-05-29 16:37:26 -0400 | [diff] [blame] | 658 | auto colorType = input->colorInfo().colorType(); |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 659 | auto cs = input->colorInfo().refColorSpace(); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 660 | sk_sp<GrColorSpaceXform> xform; |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 661 | auto prevAlphaType = input->colorInfo().alphaType(); |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 662 | if (nextDims == info.dimensions()) { |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 663 | // Might as well fold conversion to final info in the last step. |
| 664 | cs = info.refColorSpace(); |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 665 | xform = GrColorSpaceXform::Make(input->colorInfo().colorSpace(), |
| 666 | input->colorInfo().alphaType(), cs.get(), |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 667 | info.alphaType()); |
| 668 | } |
Brian Salomon | 11ad4cc | 2020-05-15 12:07:59 -0400 | [diff] [blame] | 669 | tempB = GrRenderTargetContext::MakeWithFallback(fContext, colorType, std::move(cs), |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 670 | SkBackingFit::kApprox, nextDims, 1, |
Brian Salomon | 11ad4cc | 2020-05-15 12:07:59 -0400 | [diff] [blame] | 671 | GrMipMapped::kNo, GrProtected::kNo, origin); |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 672 | if (!tempB) { |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 673 | return nullptr; |
| 674 | } |
Brian Salomon | 9c6f6bf | 2020-05-29 16:37:26 -0400 | [diff] [blame] | 675 | std::unique_ptr<GrFragmentProcessor> srcFP; |
| 676 | switch (stepType) { |
| 677 | case StepType::kBicubic: { |
| 678 | SkMatrix matrix; |
| 679 | matrix.setScaleTranslate((float)srcRect.width() /nextDims.width(), |
| 680 | (float)srcRect.height()/nextDims.height(), |
| 681 | srcRect.x(), |
| 682 | srcRect.y()); |
| 683 | auto dir = GrBicubicEffect::Direction::kXY; |
| 684 | if (nextDims.width() == srcRect.width()) { |
| 685 | dir = GrBicubicEffect::Direction::kY; |
| 686 | } else if (nextDims.height() == srcRect.height()) { |
| 687 | dir = GrBicubicEffect::Direction::kX; |
| 688 | } |
| 689 | static constexpr GrSamplerState::WrapMode kWM = GrSamplerState::WrapMode::kClamp; |
| 690 | srcFP = GrBicubicEffect::MakeSubset(std::move(texView), prevAlphaType, matrix, kWM, |
| 691 | kWM, SkRect::Make(srcRect), dir, *this->caps()); |
| 692 | break; |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 693 | } |
Brian Salomon | 9c6f6bf | 2020-05-29 16:37:26 -0400 | [diff] [blame] | 694 | case StepType::kFourTapBilinear: |
| 695 | srcFP = make_multibilerp_effect<2, 2>(fContext, texView, srcAlphaType, srcRect, |
| 696 | nextDims); |
| 697 | break; |
| 698 | default: { |
| 699 | auto filter = stepType == StepType::kNearest ? GrSamplerState::Filter::kNearest |
| 700 | : GrSamplerState::Filter::kBilerp; |
| 701 | float sx = static_cast<float>(srcRect.width()) /nextDims.width(), |
| 702 | sy = static_cast<float>(srcRect.height())/nextDims.height(); |
| 703 | SkMatrix matrix; |
| 704 | matrix.setScaleTranslate(sx, sy, srcRect.x(), srcRect.y()); |
| 705 | SkRect domain = SkRect::Make(srcRect).makeInset(sx/2, sy/2); |
| 706 | srcFP = GrTextureEffect::MakeSubset(std::move(texView), srcAlphaType, matrix, |
| 707 | filter, SkRect::Make(srcRect), domain, |
| 708 | *this->caps()); |
| 709 | break; |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 710 | } |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 711 | } |
Brian Salomon | 9c6f6bf | 2020-05-29 16:37:26 -0400 | [diff] [blame] | 712 | GrPaint paint; |
| 713 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 714 | srcFP = GrColorSpaceXformEffect::Make(std::move(srcFP), std::move(xform)); |
| 715 | paint.addColorFragmentProcessor(std::move(srcFP)); |
| 716 | tempB->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), |
| 717 | SkRect::Make(nextDims)); |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 718 | texView = tempB->readSurfaceView(); |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 719 | tempA = std::move(tempB); |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 720 | srcRect = SkIRect::MakeSize(nextDims); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 721 | } |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 722 | SkASSERT(tempA); |
| 723 | return tempA; |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 724 | } |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 725 | |
| 726 | GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT, |
| 727 | const SkIRect& rect) { |
| 728 | SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width()); |
| 729 | SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height()); |
| 730 | auto direct = fContext->priv().asDirectContext(); |
| 731 | if (!direct) { |
| 732 | return {}; |
| 733 | } |
| 734 | auto rtProxy = this->asRenderTargetProxy(); |
| 735 | if (rtProxy && rtProxy->wrapsVkSecondaryCB()) { |
| 736 | return {}; |
| 737 | } |
| 738 | |
| 739 | auto proxy = this->asSurfaceProxy(); |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 740 | auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(), |
| 741 | proxy->backendFormat(), dstCT); |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 742 | // Fail if read color type does not have all of dstCT's color channels and those missing color |
| 743 | // channels are in the src. |
Brian Salomon | 2f23ae6 | 2020-03-26 16:17:56 -0400 | [diff] [blame] | 744 | uint32_t dstChannels = GrColorTypeChannelFlags(dstCT); |
| 745 | uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType); |
| 746 | uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType()); |
| 747 | if ((~legalReadChannels & dstChannels) & srcChannels) { |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 748 | return {}; |
| 749 | } |
| 750 | |
Brian Salomon | fb28c6f | 2020-01-10 13:04:45 -0500 | [diff] [blame] | 751 | if (!this->caps()->transferFromSurfaceToBufferSupport() || |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 752 | !supportedRead.fOffsetAlignmentForTransferBuffer) { |
| 753 | return {}; |
| 754 | } |
| 755 | |
| 756 | size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width(); |
| 757 | size_t size = rowBytes * rect.height(); |
| 758 | auto buffer = direct->priv().resourceProvider()->createBuffer( |
| 759 | size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern); |
| 760 | if (!buffer) { |
| 761 | return {}; |
| 762 | } |
| 763 | auto srcRect = rect; |
Greg Daniel | b8d84f8 | 2020-02-13 14:25:00 -0500 | [diff] [blame] | 764 | bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin; |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 765 | if (flip) { |
| 766 | srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight, |
| 767 | this->height() - rect.fTop); |
| 768 | } |
Greg Daniel | bbfec9d | 2019-08-20 10:56:51 -0400 | [diff] [blame] | 769 | this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect, |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 770 | this->colorInfo().colorType(), |
Greg Daniel | bbfec9d | 2019-08-20 10:56:51 -0400 | [diff] [blame] | 771 | supportedRead.fColorType, buffer, 0); |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 772 | PixelTransferResult result; |
| 773 | result.fTransferBuffer = std::move(buffer); |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 774 | auto at = this->colorInfo().alphaType(); |
Brian Salomon | 8f8354a | 2019-07-31 20:12:02 -0400 | [diff] [blame] | 775 | if (supportedRead.fColorType != dstCT || flip) { |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 776 | result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at]( |
| 777 | void* dst, const void* src) { |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 778 | GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h); |
| 779 | GrImageInfo dstInfo(dstCT, at, nullptr, w, h); |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 780 | GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(), |
| 781 | srcInfo, src, srcInfo.minRowBytes(), |
Brian Salomon | 8f8354a | 2019-07-31 20:12:02 -0400 | [diff] [blame] | 782 | /* flipY = */ false); |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 783 | }; |
| 784 | } |
| 785 | return result; |
| 786 | } |
Greg Daniel | 46e366a | 2019-12-16 14:38:36 -0500 | [diff] [blame] | 787 | |
| 788 | #ifdef SK_DEBUG |
| 789 | void GrSurfaceContext::validate() const { |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 790 | SkASSERT(fReadView.proxy()); |
| 791 | fReadView.proxy()->validate(fContext); |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 792 | if (this->colorInfo().colorType() != GrColorType::kUnknown) { |
| 793 | SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible( |
| 794 | this->colorInfo().colorType(), fReadView.proxy()->backendFormat())); |
| 795 | } |
Greg Daniel | 46e366a | 2019-12-16 14:38:36 -0500 | [diff] [blame] | 796 | this->onValidate(); |
| 797 | } |
| 798 | #endif |