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 | |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 10 | #include "include/gpu/GrDirectContext.h" |
| 11 | #include "include/gpu/GrRecordingContext.h" |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 12 | #include "src/core/SkAutoPixmapStorage.h" |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 13 | #include "src/core/SkYUVMath.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrAuditTrail.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/GrContextPriv.h" |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrDataUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrDrawingManager.h" |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrGpu.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 19 | #include "src/gpu/GrImageInfo.h" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrProxyProvider.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/gpu/GrRecordingContextPriv.h" |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 22 | #include "src/gpu/GrRenderTargetContext.h" |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 23 | #include "src/gpu/GrSurfaceContextPriv.h" |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 24 | #include "src/gpu/GrSurfacePriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/gpu/SkGr.h" |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 26 | #include "src/gpu/effects/GrBicubicEffect.h" |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 27 | #include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h" |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 28 | |
Adlai Holler | 33dbd65 | 2020-06-01 12:35:42 -0400 | [diff] [blame] | 29 | #define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner()) |
Robert Phillips | 9eb0002 | 2020-06-30 15:30:12 -0400 | [diff] [blame] | 30 | #define RETURN_FALSE_IF_ABANDONED if (this->fContext->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. |
Robert Phillips | 9eb0002 | 2020-06-30 15:30:12 -0400 | [diff] [blame] | 41 | if (context->abandoned()) { |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 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)) { |
Robert Phillips | 9eb0002 | 2020-06-30 15:30:12 -0400 | [diff] [blame] | 111 | SkASSERT(!context->abandoned()); |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 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, |
Robert Phillips | 44333c5 | 2020-06-30 13:28:00 -0400 | [diff] [blame] | 135 | SkIPoint pt, GrDirectContext* direct) { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 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 | |
Robert Phillips | f8f45d9 | 2020-07-01 11:11:18 -0400 | [diff] [blame] | 141 | if (!direct && !(direct = fContext->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, |
Robert Phillips | 44333c5 | 2020-06-30 13:28:00 -0400 | [diff] [blame] | 293 | SkIPoint pt, GrDirectContext* 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 | |
Robert Phillips | f8f45d9 | 2020-07-01 11:11:18 -0400 | [diff] [blame] | 299 | if (!direct && !(direct = fContext->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 | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 480 | void GrSurfaceContext::asyncRescaleAndReadPixels(const SkImageInfo& info, |
| 481 | const SkIRect& srcRect, |
| 482 | RescaleGamma rescaleGamma, |
| 483 | SkFilterQuality rescaleQuality, |
| 484 | ReadPixelsCallback callback, |
| 485 | ReadPixelsContext context) { |
Robert Phillips | f8f45d9 | 2020-07-01 11:11:18 -0400 | [diff] [blame] | 486 | auto direct = fContext->asDirectContext(); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 487 | |
| 488 | // We implement this by rendering and we don't currently support rendering kUnpremul. |
| 489 | if (info.alphaType() == kUnpremul_SkAlphaType) { |
| 490 | callback(context, nullptr); |
| 491 | return; |
| 492 | } |
| 493 | if (!direct) { |
| 494 | callback(context, nullptr); |
| 495 | return; |
| 496 | } |
| 497 | auto rt = this->asRenderTargetProxy(); |
| 498 | if (rt && rt->wrapsVkSecondaryCB()) { |
| 499 | callback(context, nullptr); |
| 500 | return; |
| 501 | } |
| 502 | if (rt && rt->framebufferOnly()) { |
| 503 | callback(context, nullptr); |
| 504 | return; |
| 505 | } |
| 506 | auto dstCT = SkColorTypeToGrColorType(info.colorType()); |
| 507 | if (dstCT == GrColorType::kUnknown) { |
| 508 | callback(context, nullptr); |
| 509 | return; |
| 510 | } |
| 511 | bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height(); |
| 512 | auto colorTypeOfFinalContext = this->colorInfo().colorType(); |
| 513 | auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat(); |
| 514 | if (needsRescale) { |
| 515 | colorTypeOfFinalContext = dstCT; |
| 516 | backendFormatOfFinalContext = |
| 517 | this->caps()->getDefaultBackendFormat(dstCT, GrRenderable::kYes); |
| 518 | } |
| 519 | auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext, |
| 520 | backendFormatOfFinalContext, dstCT); |
| 521 | // Fail if we can't read from the source surface's color type. |
| 522 | if (readInfo.fColorType == GrColorType::kUnknown) { |
| 523 | callback(context, nullptr); |
| 524 | return; |
| 525 | } |
| 526 | // Fail if read color type does not have all of dstCT's color channels and those missing color |
| 527 | // channels are in the src. |
| 528 | uint32_t dstChannels = GrColorTypeChannelFlags(dstCT); |
| 529 | uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType); |
| 530 | uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType()); |
| 531 | if ((~legalReadChannels & dstChannels) & srcChannels) { |
| 532 | callback(context, nullptr); |
| 533 | return; |
| 534 | } |
| 535 | |
| 536 | std::unique_ptr<GrRenderTargetContext> tempRTC; |
| 537 | int x = srcRect.fLeft; |
| 538 | int y = srcRect.fTop; |
| 539 | if (needsRescale) { |
| 540 | tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma, |
| 541 | rescaleQuality); |
| 542 | if (!tempRTC) { |
| 543 | callback(context, nullptr); |
| 544 | return; |
| 545 | } |
| 546 | SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace())); |
| 547 | SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin); |
| 548 | x = y = 0; |
| 549 | } else { |
| 550 | sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(), |
| 551 | this->colorInfo().alphaType(), |
| 552 | info.colorSpace(), |
| 553 | info.alphaType()); |
| 554 | // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion. |
| 555 | if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) { |
| 556 | GrSurfaceProxyView texProxyView = this->readSurfaceView(); |
| 557 | SkRect srcRectToDraw = SkRect::Make(srcRect); |
| 558 | // If the src is not texturable first try to make a copy to a texture. |
| 559 | if (!texProxyView.asTextureProxy()) { |
| 560 | texProxyView = |
| 561 | GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipMapped::kNo, srcRect, |
| 562 | SkBackingFit::kApprox, SkBudgeted::kNo); |
| 563 | if (!texProxyView) { |
| 564 | callback(context, nullptr); |
| 565 | return; |
| 566 | } |
| 567 | SkASSERT(texProxyView.asTextureProxy()); |
| 568 | srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height()); |
| 569 | } |
| 570 | tempRTC = GrRenderTargetContext::Make(direct, this->colorInfo().colorType(), |
| 571 | info.refColorSpace(), SkBackingFit::kApprox, |
| 572 | srcRect.size(), 1, GrMipMapped::kNo, |
| 573 | GrProtected::kNo, kTopLeft_GrSurfaceOrigin); |
| 574 | if (!tempRTC) { |
| 575 | callback(context, nullptr); |
| 576 | return; |
| 577 | } |
| 578 | tempRTC->drawTexture(nullptr, std::move(texProxyView), this->colorInfo().alphaType(), |
| 579 | GrSamplerState::Filter::kNearest, SkBlendMode::kSrc, |
| 580 | SK_PMColor4fWHITE, srcRectToDraw, |
| 581 | SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo, |
| 582 | GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint, |
| 583 | SkMatrix::I(), std::move(xform)); |
| 584 | x = y = 0; |
| 585 | } |
| 586 | } |
| 587 | auto rtc = tempRTC ? tempRTC.get() : this; |
| 588 | return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()), |
| 589 | info.colorType(), callback, context); |
| 590 | } |
| 591 | |
| 592 | class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult { |
| 593 | public: |
| 594 | AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {} |
| 595 | ~AsyncReadResult() override { |
| 596 | for (int i = 0; i < fPlanes.count(); ++i) { |
| 597 | if (!fPlanes[i].fMappedBuffer) { |
| 598 | delete[] static_cast<const char*>(fPlanes[i].fData); |
| 599 | } else { |
| 600 | GrClientMappedBufferManager::BufferFinishedMessageBus::Post( |
| 601 | {std::move(fPlanes[i].fMappedBuffer), fInboxID}); |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | int count() const override { return fPlanes.count(); } |
| 607 | const void* data(int i) const override { return fPlanes[i].fData; } |
| 608 | size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; } |
| 609 | |
| 610 | bool addTransferResult(const PixelTransferResult& result, |
| 611 | SkISize dimensions, |
| 612 | size_t rowBytes, |
| 613 | GrClientMappedBufferManager* manager) { |
| 614 | SkASSERT(!result.fTransferBuffer->isMapped()); |
| 615 | const void* mappedData = result.fTransferBuffer->map(); |
| 616 | if (!mappedData) { |
| 617 | return false; |
| 618 | } |
| 619 | if (result.fPixelConverter) { |
| 620 | std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]); |
| 621 | result.fPixelConverter(convertedData.get(), mappedData); |
| 622 | this->addCpuPlane(std::move(convertedData), rowBytes); |
| 623 | result.fTransferBuffer->unmap(); |
| 624 | } else { |
| 625 | manager->insert(result.fTransferBuffer); |
| 626 | this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer)); |
| 627 | } |
| 628 | return true; |
| 629 | } |
| 630 | |
| 631 | void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) { |
| 632 | SkASSERT(data); |
| 633 | SkASSERT(rowBytes > 0); |
| 634 | fPlanes.emplace_back(data.release(), rowBytes, nullptr); |
| 635 | } |
| 636 | |
| 637 | private: |
| 638 | void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) { |
| 639 | SkASSERT(data); |
| 640 | SkASSERT(rowBytes > 0); |
| 641 | SkASSERT(mappedBuffer); |
| 642 | SkASSERT(mappedBuffer->isMapped()); |
| 643 | fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer)); |
| 644 | } |
| 645 | |
| 646 | struct Plane { |
| 647 | Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer) |
| 648 | : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {} |
| 649 | const void* fData; |
| 650 | size_t fRowBytes; |
| 651 | // If this is null then fData is heap alloc and must be delete[]ed as const char[]. |
| 652 | sk_sp<GrGpuBuffer> fMappedBuffer; |
| 653 | }; |
| 654 | SkSTArray<3, Plane> fPlanes; |
| 655 | uint32_t fInboxID; |
| 656 | }; |
| 657 | |
| 658 | void GrSurfaceContext::asyncReadPixels(const SkIRect& rect, |
| 659 | SkColorType colorType, |
| 660 | ReadPixelsCallback callback, |
| 661 | ReadPixelsContext context) { |
| 662 | SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width()); |
| 663 | SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height()); |
| 664 | |
| 665 | if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) { |
| 666 | callback(context, nullptr); |
| 667 | return; |
| 668 | } |
| 669 | |
Robert Phillips | f8f45d9 | 2020-07-01 11:11:18 -0400 | [diff] [blame] | 670 | auto directContext = fContext->asDirectContext(); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 671 | SkASSERT(directContext); |
| 672 | auto mappedBufferManager = directContext->priv().clientMappedBufferManager(); |
| 673 | |
| 674 | auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect); |
| 675 | |
| 676 | if (!transferResult.fTransferBuffer) { |
| 677 | auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(), |
| 678 | this->colorInfo().refColorSpace()); |
| 679 | auto result = std::make_unique<AsyncReadResult>(0); |
| 680 | std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]); |
| 681 | SkPixmap pm(ii, data.get(), ii.minRowBytes()); |
| 682 | result->addCpuPlane(std::move(data), pm.rowBytes()); |
| 683 | |
| 684 | if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) { |
| 685 | callback(context, nullptr); |
| 686 | return; |
| 687 | } |
| 688 | callback(context, std::move(result)); |
| 689 | return; |
| 690 | } |
| 691 | |
| 692 | struct FinishContext { |
| 693 | ReadPixelsCallback* fClientCallback; |
| 694 | ReadPixelsContext fClientContext; |
| 695 | SkISize fSize; |
| 696 | SkColorType fColorType; |
| 697 | GrClientMappedBufferManager* fMappedBufferManager; |
| 698 | PixelTransferResult fTransferResult; |
| 699 | }; |
| 700 | // Assumption is that the caller would like to flush. We could take a parameter or require an |
| 701 | // explicit flush from the caller. We'd have to have a way to defer attaching the finish |
| 702 | // callback to GrGpu until after the next flush that flushes our op list, though. |
| 703 | auto* finishContext = new FinishContext{callback, |
| 704 | context, |
| 705 | rect.size(), |
| 706 | colorType, |
| 707 | mappedBufferManager, |
| 708 | std::move(transferResult)}; |
| 709 | auto finishCallback = [](GrGpuFinishedContext c) { |
| 710 | const auto* context = reinterpret_cast<const FinishContext*>(c); |
| 711 | auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID()); |
| 712 | size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType); |
| 713 | if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes, |
| 714 | context->fMappedBufferManager)) { |
| 715 | result.reset(); |
| 716 | } |
| 717 | (*context->fClientCallback)(context->fClientContext, std::move(result)); |
| 718 | delete context; |
| 719 | }; |
| 720 | GrFlushInfo flushInfo; |
| 721 | flushInfo.fFinishedContext = finishContext; |
| 722 | flushInfo.fFinishedProc = finishCallback; |
| 723 | this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo, nullptr); |
| 724 | } |
| 725 | |
| 726 | void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace, |
| 727 | sk_sp<SkColorSpace> dstColorSpace, |
| 728 | const SkIRect& srcRect, |
| 729 | SkISize dstSize, |
| 730 | RescaleGamma rescaleGamma, |
| 731 | SkFilterQuality rescaleQuality, |
| 732 | ReadPixelsCallback callback, |
| 733 | ReadPixelsContext context) { |
| 734 | SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width()); |
| 735 | SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height()); |
| 736 | SkASSERT(!dstSize.isZero()); |
| 737 | SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0)); |
| 738 | |
Robert Phillips | f8f45d9 | 2020-07-01 11:11:18 -0400 | [diff] [blame] | 739 | auto direct = fContext->asDirectContext(); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 740 | if (!direct) { |
| 741 | callback(context, nullptr); |
| 742 | return; |
| 743 | } |
| 744 | auto rt = this->asRenderTargetProxy(); |
| 745 | if (rt && rt->wrapsVkSecondaryCB()) { |
| 746 | callback(context, nullptr); |
| 747 | return; |
| 748 | } |
| 749 | if (rt && rt->framebufferOnly()) { |
| 750 | callback(context, nullptr); |
| 751 | return; |
| 752 | } |
| 753 | if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) { |
| 754 | callback(context, nullptr); |
| 755 | return; |
| 756 | } |
| 757 | int x = srcRect.fLeft; |
| 758 | int y = srcRect.fTop; |
| 759 | bool needsRescale = srcRect.size() != dstSize; |
| 760 | GrSurfaceProxyView srcView; |
| 761 | if (needsRescale) { |
| 762 | // We assume the caller wants kPremul. There is no way to indicate a preference. |
| 763 | auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType, |
| 764 | dstColorSpace); |
| 765 | // TODO: Incorporate the YUV conversion into last pass of rescaling. |
| 766 | auto tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma, |
| 767 | rescaleQuality); |
| 768 | if (!tempRTC) { |
| 769 | callback(context, nullptr); |
| 770 | return; |
| 771 | } |
| 772 | SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace())); |
| 773 | SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin); |
| 774 | x = y = 0; |
| 775 | srcView = tempRTC->readSurfaceView(); |
| 776 | } else { |
| 777 | srcView = this->readSurfaceView(); |
| 778 | if (!srcView.asTextureProxy()) { |
| 779 | srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipMapped::kNo, |
| 780 | srcRect, SkBackingFit::kApprox, SkBudgeted::kYes); |
| 781 | if (!srcView) { |
| 782 | // If we can't get a texture copy of the contents then give up. |
| 783 | callback(context, nullptr); |
| 784 | return; |
| 785 | } |
| 786 | SkASSERT(srcView.asTextureProxy()); |
| 787 | x = y = 0; |
| 788 | } |
| 789 | // We assume the caller wants kPremul. There is no way to indicate a preference. |
| 790 | sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make( |
| 791 | this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(), |
| 792 | kPremul_SkAlphaType); |
| 793 | if (xform) { |
| 794 | SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height()); |
| 795 | auto tempRTC = GrRenderTargetContext::Make( |
| 796 | direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox, |
| 797 | dstSize, 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin); |
| 798 | if (!tempRTC) { |
| 799 | callback(context, nullptr); |
| 800 | return; |
| 801 | } |
| 802 | tempRTC->drawTexture(nullptr, std::move(srcView), this->colorInfo().alphaType(), |
| 803 | GrSamplerState::Filter::kNearest, SkBlendMode::kSrc, |
| 804 | SK_PMColor4fWHITE, srcRectToDraw, SkRect::Make(srcRect.size()), |
| 805 | GrAA::kNo, GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint, |
| 806 | SkMatrix::I(), std::move(xform)); |
| 807 | srcView = tempRTC->readSurfaceView(); |
| 808 | SkASSERT(srcView.asTextureProxy()); |
| 809 | x = y = 0; |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | auto yRTC = GrRenderTargetContext::MakeWithFallback( |
| 814 | direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1, |
| 815 | GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin); |
| 816 | int halfW = dstSize.width() /2; |
| 817 | int halfH = dstSize.height()/2; |
| 818 | auto uRTC = GrRenderTargetContext::MakeWithFallback( |
| 819 | direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1, |
| 820 | GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin); |
| 821 | auto vRTC = GrRenderTargetContext::MakeWithFallback( |
| 822 | direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1, |
| 823 | GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin); |
| 824 | if (!yRTC || !uRTC || !vRTC) { |
| 825 | callback(context, nullptr); |
| 826 | return; |
| 827 | } |
| 828 | |
| 829 | float baseM[20]; |
| 830 | SkColorMatrix_RGB2YUV(yuvColorSpace, baseM); |
| 831 | |
| 832 | // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost? |
| 833 | |
| 834 | auto texMatrix = SkMatrix::Translate(x, y); |
| 835 | |
| 836 | SkRect dstRectY = SkRect::Make(dstSize); |
| 837 | SkRect dstRectUV = SkRect::MakeWH(halfW, halfH); |
| 838 | |
| 839 | bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport(); |
| 840 | PixelTransferResult yTransfer, uTransfer, vTransfer; |
| 841 | |
| 842 | // This matrix generates (r,g,b,a) = (0, 0, 0, y) |
| 843 | float yM[20]; |
| 844 | std::fill_n(yM, 15, 0.f); |
| 845 | std::copy_n(baseM + 0, 5, yM + 15); |
| 846 | GrPaint yPaint; |
| 847 | auto yTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix); |
| 848 | auto yColFP = GrColorMatrixFragmentProcessor::Make(std::move(yTexFP), yM, |
| 849 | /*unpremulInput=*/false, |
| 850 | /*clampRGBOutput=*/true, |
| 851 | /*premulOutput=*/false); |
| 852 | yPaint.addColorFragmentProcessor(std::move(yColFP)); |
| 853 | yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 854 | yRTC->fillRectToRect(nullptr, std::move(yPaint), GrAA::kNo, SkMatrix::I(), dstRectY, dstRectY); |
| 855 | if (!doSynchronousRead) { |
| 856 | yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8, |
| 857 | SkIRect::MakeWH(yRTC->width(), yRTC->height())); |
| 858 | if (!yTransfer.fTransferBuffer) { |
| 859 | callback(context, nullptr); |
| 860 | return; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | texMatrix.preScale(2.f, 2.f); |
| 865 | // This matrix generates (r,g,b,a) = (0, 0, 0, u) |
| 866 | float uM[20]; |
| 867 | std::fill_n(uM, 15, 0.f); |
| 868 | std::copy_n(baseM + 5, 5, uM + 15); |
| 869 | GrPaint uPaint; |
| 870 | auto uTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix, |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 871 | GrSamplerState::Filter::kLinear); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 872 | auto uColFP = GrColorMatrixFragmentProcessor::Make(std::move(uTexFP), uM, |
| 873 | /*unpremulInput=*/false, |
| 874 | /*clampRGBOutput=*/true, |
| 875 | /*premulOutput=*/false); |
| 876 | uPaint.addColorFragmentProcessor(std::move(uColFP)); |
| 877 | uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 878 | uRTC->fillRectToRect(nullptr, std::move(uPaint), GrAA::kNo, SkMatrix::I(), dstRectUV, |
| 879 | dstRectUV); |
| 880 | if (!doSynchronousRead) { |
| 881 | uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8, |
| 882 | SkIRect::MakeWH(uRTC->width(), uRTC->height())); |
| 883 | if (!uTransfer.fTransferBuffer) { |
| 884 | callback(context, nullptr); |
| 885 | return; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | // This matrix generates (r,g,b,a) = (0, 0, 0, v) |
| 890 | float vM[20]; |
| 891 | std::fill_n(vM, 15, 0.f); |
| 892 | std::copy_n(baseM + 10, 5, vM + 15); |
| 893 | GrPaint vPaint; |
| 894 | auto vTexFP = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType(), |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 895 | texMatrix, GrSamplerState::Filter::kLinear); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 896 | auto vColFP = GrColorMatrixFragmentProcessor::Make(std::move(vTexFP), vM, |
| 897 | /*unpremulInput=*/false, |
| 898 | /*clampRGBOutput=*/true, |
| 899 | /*premulOutput=*/false); |
| 900 | vPaint.addColorFragmentProcessor(std::move(vColFP)); |
| 901 | vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 902 | vRTC->fillRectToRect(nullptr, std::move(vPaint), GrAA::kNo, SkMatrix::I(), dstRectUV, |
| 903 | dstRectUV); |
| 904 | if (!doSynchronousRead) { |
| 905 | vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8, |
| 906 | SkIRect::MakeWH(vRTC->width(), vRTC->height())); |
| 907 | if (!vTransfer.fTransferBuffer) { |
| 908 | callback(context, nullptr); |
| 909 | return; |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | if (doSynchronousRead) { |
| 914 | GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize); |
| 915 | GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH); |
| 916 | size_t yRB = yInfo.minRowBytes(); |
| 917 | size_t uvRB = uvInfo.minRowBytes(); |
| 918 | std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]); |
| 919 | std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]); |
| 920 | std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]); |
| 921 | if (!yRTC->readPixels(yInfo, y.get(), yRB, {0, 0}, direct) || |
| 922 | !uRTC->readPixels(uvInfo, u.get(), uvRB, {0, 0}, direct) || |
| 923 | !vRTC->readPixels(uvInfo, v.get(), uvRB, {0, 0}, direct)) { |
| 924 | callback(context, nullptr); |
| 925 | return; |
| 926 | } |
| 927 | auto result = std::make_unique<AsyncReadResult>(direct->priv().contextID()); |
| 928 | result->addCpuPlane(std::move(y), yRB ); |
| 929 | result->addCpuPlane(std::move(u), uvRB); |
| 930 | result->addCpuPlane(std::move(v), uvRB); |
| 931 | callback(context, std::move(result)); |
| 932 | return; |
| 933 | } |
| 934 | |
| 935 | struct FinishContext { |
| 936 | ReadPixelsCallback* fClientCallback; |
| 937 | ReadPixelsContext fClientContext; |
| 938 | GrClientMappedBufferManager* fMappedBufferManager; |
| 939 | SkISize fSize; |
| 940 | PixelTransferResult fYTransfer; |
| 941 | PixelTransferResult fUTransfer; |
| 942 | PixelTransferResult fVTransfer; |
| 943 | }; |
| 944 | // Assumption is that the caller would like to flush. We could take a parameter or require an |
| 945 | // explicit flush from the caller. We'd have to have a way to defer attaching the finish |
| 946 | // callback to GrGpu until after the next flush that flushes our op list, though. |
| 947 | auto* finishContext = new FinishContext{callback, |
| 948 | context, |
| 949 | direct->priv().clientMappedBufferManager(), |
| 950 | dstSize, |
| 951 | std::move(yTransfer), |
| 952 | std::move(uTransfer), |
| 953 | std::move(vTransfer)}; |
| 954 | auto finishCallback = [](GrGpuFinishedContext c) { |
| 955 | const auto* context = reinterpret_cast<const FinishContext*>(c); |
| 956 | auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID()); |
| 957 | auto manager = context->fMappedBufferManager; |
| 958 | size_t rowBytes = SkToSizeT(context->fSize.width()); |
| 959 | if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) { |
| 960 | (*context->fClientCallback)(context->fClientContext, nullptr); |
| 961 | delete context; |
| 962 | return; |
| 963 | } |
| 964 | rowBytes /= 2; |
| 965 | SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2}; |
| 966 | if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) { |
| 967 | (*context->fClientCallback)(context->fClientContext, nullptr); |
| 968 | delete context; |
| 969 | return; |
| 970 | } |
| 971 | if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) { |
| 972 | (*context->fClientCallback)(context->fClientContext, nullptr); |
| 973 | delete context; |
| 974 | return; |
| 975 | } |
| 976 | (*context->fClientCallback)(context->fClientContext, std::move(result)); |
| 977 | delete context; |
| 978 | }; |
| 979 | GrFlushInfo flushInfo; |
| 980 | flushInfo.fFinishedContext = finishContext; |
| 981 | flushInfo.fFinishedProc = finishCallback; |
| 982 | this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo, nullptr); |
| 983 | } |
| 984 | |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 985 | bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) { |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 986 | ASSERT_SINGLE_OWNER |
| 987 | RETURN_FALSE_IF_ABANDONED |
| 988 | SkDEBUGCODE(this->validate();) |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 989 | GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy"); |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 990 | |
Brian Salomon | 947efe2 | 2019-07-16 15:36:11 -0400 | [diff] [blame] | 991 | const GrCaps* caps = fContext->priv().caps(); |
| 992 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 993 | SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal); |
Greg Daniel | c71c796 | 2020-01-14 16:44:18 -0500 | [diff] [blame] | 994 | SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat()); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 995 | |
Stephen White | 3c0a50f | 2020-01-16 18:19:54 -0500 | [diff] [blame] | 996 | if (this->asSurfaceProxy()->framebufferOnly()) { |
| 997 | return false; |
| 998 | } |
| 999 | |
Chris Dalton | f8e5aad | 2019-08-02 12:55:23 -0600 | [diff] [blame] | 1000 | if (!caps->canCopySurface(this->asSurfaceProxy(), src, srcRect, dstPoint)) { |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 1001 | return false; |
| 1002 | } |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 1003 | |
Greg Daniel | 16f5c65 | 2019-10-29 11:26:01 -0400 | [diff] [blame] | 1004 | // The swizzle doesn't matter for copies and it is not used. |
| 1005 | return this->drawingManager()->newCopyRenderTask( |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 1006 | GrSurfaceProxyView(sk_ref_sp(src), this->origin(), GrSwizzle("rgba")), srcRect, |
Greg Daniel | 46e366a | 2019-12-16 14:38:36 -0500 | [diff] [blame] | 1007 | this->readSurfaceView(), dstPoint); |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 1008 | } |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 1009 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 1010 | std::unique_ptr<GrRenderTargetContext> GrSurfaceContext::rescale(const GrImageInfo& info, |
| 1011 | GrSurfaceOrigin origin, |
| 1012 | SkIRect srcRect, |
| 1013 | RescaleGamma rescaleGamma, |
| 1014 | SkFilterQuality rescaleQuality) { |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1015 | auto rtProxy = this->asRenderTargetProxy(); |
| 1016 | if (rtProxy && rtProxy->wrapsVkSecondaryCB()) { |
| 1017 | return nullptr; |
| 1018 | } |
| 1019 | |
Stephen White | 3c0a50f | 2020-01-16 18:19:54 -0500 | [diff] [blame] | 1020 | if (this->asSurfaceProxy()->framebufferOnly()) { |
| 1021 | return nullptr; |
| 1022 | } |
| 1023 | |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1024 | // We rescale by drawing and don't currently support drawing to a kUnpremul destination. |
| 1025 | if (info.alphaType() == kUnpremul_SkAlphaType) { |
| 1026 | return nullptr; |
| 1027 | } |
| 1028 | |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 1029 | GrSurfaceProxyView texView = this->readSurfaceView(); |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 1030 | SkAlphaType srcAlphaType = this->colorInfo().alphaType(); |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 1031 | if (!texView.asTextureProxy()) { |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 1032 | texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipMapped::kNo, srcRect, |
| 1033 | SkBackingFit::kApprox, SkBudgeted::kNo); |
| 1034 | if (!texView) { |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1035 | return nullptr; |
| 1036 | } |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 1037 | SkASSERT(texView.asTextureProxy()); |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 1038 | srcRect = SkIRect::MakeSize(srcRect.size()); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1039 | } |
| 1040 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 1041 | // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the |
| 1042 | // pass B is moved to A. If 'this' is the input on the first pass then tempA is null. |
| 1043 | std::unique_ptr<GrRenderTargetContext> tempA; |
| 1044 | std::unique_ptr<GrRenderTargetContext> tempB; |
| 1045 | |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1046 | // Assume we should ignore the rescale linear request if the surface has no color space since |
| 1047 | // it's unclear how we'd linearize from an unknown color space. |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 1048 | if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() && |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 1049 | !this->colorInfo().colorSpace()->gammaIsLinear()) { |
| 1050 | auto cs = this->colorInfo().colorSpace()->makeLinearGamma(); |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 1051 | auto xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(), srcAlphaType, cs.get(), |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1052 | kPremul_SkAlphaType); |
| 1053 | // We'll fall back to kRGBA_8888 if half float not supported. |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 1054 | auto linearRTC = GrRenderTargetContext::MakeWithFallback( |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 1055 | fContext, GrColorType::kRGBA_F16, cs, SkBackingFit::kApprox, srcRect.size(), 1, |
Brian Salomon | 11ad4cc | 2020-05-15 12:07:59 -0400 | [diff] [blame] | 1056 | GrMipMapped::kNo, GrProtected::kNo, origin); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1057 | if (!linearRTC) { |
| 1058 | return nullptr; |
| 1059 | } |
Brian Salomon | 11ad4cc | 2020-05-15 12:07:59 -0400 | [diff] [blame] | 1060 | // 1-to-1 draw can always be kFast. |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 1061 | linearRTC->drawTexture(nullptr, std::move(texView), srcAlphaType, |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 1062 | GrSamplerState::Filter::kNearest, SkBlendMode::kSrc, |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 1063 | SK_PMColor4fWHITE, SkRect::Make(srcRect), |
| 1064 | SkRect::Make(srcRect.size()), GrAA::kNo, GrQuadAAFlags::kNone, |
| 1065 | SkCanvas::kFast_SrcRectConstraint, SkMatrix::I(), std::move(xform)); |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 1066 | texView = linearRTC->readSurfaceView(); |
| 1067 | SkASSERT(texView.asTextureProxy()); |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 1068 | tempA = std::move(linearRTC); |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 1069 | srcRect = SkIRect::MakeSize(srcRect.size()); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1070 | } |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 1071 | |
Brian Salomon | 9c6f6bf | 2020-05-29 16:37:26 -0400 | [diff] [blame] | 1072 | while (srcRect.size() != info.dimensions()) { |
Brian Salomon | 59f31b1 | 2020-06-04 17:27:15 -0400 | [diff] [blame] | 1073 | SkISize nextDims = info.dimensions(); |
| 1074 | if (rescaleQuality != kNone_SkFilterQuality) { |
| 1075 | if (srcRect.width() > info.width()) { |
| 1076 | nextDims.fWidth = std::max((srcRect.width() + 1)/2, info.width()); |
| 1077 | } else if (srcRect.width() < info.width()) { |
| 1078 | nextDims.fWidth = std::min(srcRect.width()*2, info.width()); |
| 1079 | } |
| 1080 | if (srcRect.height() > info.height()) { |
| 1081 | nextDims.fHeight = std::max((srcRect.height() + 1)/2, info.height()); |
| 1082 | } else if (srcRect.height() < info.height()) { |
| 1083 | nextDims.fHeight = std::min(srcRect.height()*2, info.height()); |
| 1084 | } |
| 1085 | } |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 1086 | auto input = tempA ? tempA.get() : this; |
Brian Salomon | 59f31b1 | 2020-06-04 17:27:15 -0400 | [diff] [blame] | 1087 | GrColorType colorType = input->colorInfo().colorType(); |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 1088 | auto cs = input->colorInfo().refColorSpace(); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1089 | sk_sp<GrColorSpaceXform> xform; |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 1090 | auto prevAlphaType = input->colorInfo().alphaType(); |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 1091 | if (nextDims == info.dimensions()) { |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1092 | // Might as well fold conversion to final info in the last step. |
| 1093 | cs = info.refColorSpace(); |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 1094 | xform = GrColorSpaceXform::Make(input->colorInfo().colorSpace(), |
| 1095 | input->colorInfo().alphaType(), cs.get(), |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1096 | info.alphaType()); |
| 1097 | } |
Brian Salomon | 11ad4cc | 2020-05-15 12:07:59 -0400 | [diff] [blame] | 1098 | tempB = GrRenderTargetContext::MakeWithFallback(fContext, colorType, std::move(cs), |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 1099 | SkBackingFit::kApprox, nextDims, 1, |
Brian Salomon | 11ad4cc | 2020-05-15 12:07:59 -0400 | [diff] [blame] | 1100 | GrMipMapped::kNo, GrProtected::kNo, origin); |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 1101 | if (!tempB) { |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1102 | return nullptr; |
| 1103 | } |
Brian Salomon | 59f31b1 | 2020-06-04 17:27:15 -0400 | [diff] [blame] | 1104 | auto dstRect = SkRect::Make(nextDims); |
| 1105 | if (rescaleQuality == kHigh_SkFilterQuality) { |
| 1106 | SkMatrix matrix; |
| 1107 | matrix.setScaleTranslate((float)srcRect.width()/nextDims.width(), |
| 1108 | (float)srcRect.height()/nextDims.height(), |
| 1109 | srcRect.x(), |
| 1110 | srcRect.y()); |
| 1111 | std::unique_ptr<GrFragmentProcessor> fp; |
| 1112 | auto dir = GrBicubicEffect::Direction::kXY; |
| 1113 | if (nextDims.width() == srcRect.width()) { |
| 1114 | dir = GrBicubicEffect::Direction::kY; |
| 1115 | } else if (nextDims.height() == srcRect.height()) { |
| 1116 | dir = GrBicubicEffect::Direction::kX; |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1117 | } |
Brian Salomon | 1af72d1 | 2020-06-25 10:47:26 -0400 | [diff] [blame] | 1118 | static constexpr auto kWM = GrSamplerState::WrapMode::kClamp; |
| 1119 | static constexpr auto kKernel = GrBicubicEffect::Kernel::kCatmullRom; |
Brian Salomon | 59f31b1 | 2020-06-04 17:27:15 -0400 | [diff] [blame] | 1120 | fp = GrBicubicEffect::MakeSubset(std::move(texView), prevAlphaType, matrix, kWM, kWM, |
Brian Salomon | 1af72d1 | 2020-06-25 10:47:26 -0400 | [diff] [blame] | 1121 | SkRect::Make(srcRect), kKernel, dir, *this->caps()); |
Brian Salomon | 59f31b1 | 2020-06-04 17:27:15 -0400 | [diff] [blame] | 1122 | if (xform) { |
| 1123 | fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform)); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1124 | } |
Brian Salomon | 59f31b1 | 2020-06-04 17:27:15 -0400 | [diff] [blame] | 1125 | GrPaint paint; |
| 1126 | paint.addColorFragmentProcessor(std::move(fp)); |
| 1127 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 1128 | tempB->fillRectToRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect, |
| 1129 | dstRect); |
| 1130 | } else { |
| 1131 | auto filter = rescaleQuality == kNone_SkFilterQuality ? GrSamplerState::Filter::kNearest |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 1132 | : GrSamplerState::Filter::kLinear; |
Brian Salomon | 59f31b1 | 2020-06-04 17:27:15 -0400 | [diff] [blame] | 1133 | // Minimizing draw with integer coord src and dev rects can always be kFast. |
| 1134 | auto constraint = SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint; |
| 1135 | if (nextDims.width() <= srcRect.width() && nextDims.height() <= srcRect.height()) { |
| 1136 | constraint = SkCanvas::SrcRectConstraint::kFast_SrcRectConstraint; |
| 1137 | } |
| 1138 | tempB->drawTexture(nullptr, std::move(texView), srcAlphaType, filter, SkBlendMode::kSrc, |
| 1139 | SK_PMColor4fWHITE, SkRect::Make(srcRect), dstRect, GrAA::kNo, |
| 1140 | GrQuadAAFlags::kNone, constraint, SkMatrix::I(), std::move(xform)); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1141 | } |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 1142 | texView = tempB->readSurfaceView(); |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 1143 | tempA = std::move(tempB); |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 1144 | srcRect = SkIRect::MakeSize(nextDims); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1145 | } |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 1146 | SkASSERT(tempA); |
| 1147 | return tempA; |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1148 | } |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 1149 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 1150 | GrSemaphoresSubmitted GrSurfaceContext::flush(SkSurface::BackendSurfaceAccess access, |
| 1151 | const GrFlushInfo& info, |
| 1152 | const GrBackendSurfaceMutableState* newState) { |
| 1153 | ASSERT_SINGLE_OWNER |
Robert Phillips | 9eb0002 | 2020-06-30 15:30:12 -0400 | [diff] [blame] | 1154 | if (fContext->abandoned()) { |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 1155 | if (info.fSubmittedProc) { |
| 1156 | info.fSubmittedProc(info.fSubmittedContext, false); |
| 1157 | } |
| 1158 | if (info.fFinishedProc) { |
| 1159 | info.fFinishedProc(info.fFinishedContext); |
| 1160 | } |
| 1161 | return GrSemaphoresSubmitted::kNo; |
| 1162 | } |
| 1163 | SkDEBUGCODE(this->validate();) |
| 1164 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext); |
| 1165 | |
| 1166 | return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info, newState); |
| 1167 | } |
| 1168 | |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 1169 | GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT, |
| 1170 | const SkIRect& rect) { |
| 1171 | SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width()); |
| 1172 | SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height()); |
Robert Phillips | f8f45d9 | 2020-07-01 11:11:18 -0400 | [diff] [blame] | 1173 | auto direct = fContext->asDirectContext(); |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 1174 | if (!direct) { |
| 1175 | return {}; |
| 1176 | } |
| 1177 | auto rtProxy = this->asRenderTargetProxy(); |
| 1178 | if (rtProxy && rtProxy->wrapsVkSecondaryCB()) { |
| 1179 | return {}; |
| 1180 | } |
| 1181 | |
| 1182 | auto proxy = this->asSurfaceProxy(); |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 1183 | auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(), |
| 1184 | proxy->backendFormat(), dstCT); |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 1185 | // Fail if read color type does not have all of dstCT's color channels and those missing color |
| 1186 | // channels are in the src. |
Brian Salomon | 2f23ae6 | 2020-03-26 16:17:56 -0400 | [diff] [blame] | 1187 | uint32_t dstChannels = GrColorTypeChannelFlags(dstCT); |
| 1188 | uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType); |
| 1189 | uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType()); |
| 1190 | if ((~legalReadChannels & dstChannels) & srcChannels) { |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 1191 | return {}; |
| 1192 | } |
| 1193 | |
Brian Salomon | fb28c6f | 2020-01-10 13:04:45 -0500 | [diff] [blame] | 1194 | if (!this->caps()->transferFromSurfaceToBufferSupport() || |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 1195 | !supportedRead.fOffsetAlignmentForTransferBuffer) { |
| 1196 | return {}; |
| 1197 | } |
| 1198 | |
| 1199 | size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width(); |
| 1200 | size_t size = rowBytes * rect.height(); |
| 1201 | auto buffer = direct->priv().resourceProvider()->createBuffer( |
| 1202 | size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern); |
| 1203 | if (!buffer) { |
| 1204 | return {}; |
| 1205 | } |
| 1206 | auto srcRect = rect; |
Greg Daniel | b8d84f8 | 2020-02-13 14:25:00 -0500 | [diff] [blame] | 1207 | bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin; |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 1208 | if (flip) { |
| 1209 | srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight, |
| 1210 | this->height() - rect.fTop); |
| 1211 | } |
Greg Daniel | bbfec9d | 2019-08-20 10:56:51 -0400 | [diff] [blame] | 1212 | this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect, |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 1213 | this->colorInfo().colorType(), |
Greg Daniel | bbfec9d | 2019-08-20 10:56:51 -0400 | [diff] [blame] | 1214 | supportedRead.fColorType, buffer, 0); |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 1215 | PixelTransferResult result; |
| 1216 | result.fTransferBuffer = std::move(buffer); |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 1217 | auto at = this->colorInfo().alphaType(); |
Brian Salomon | 8f8354a | 2019-07-31 20:12:02 -0400 | [diff] [blame] | 1218 | if (supportedRead.fColorType != dstCT || flip) { |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 1219 | result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at]( |
| 1220 | void* dst, const void* src) { |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 1221 | GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h); |
| 1222 | GrImageInfo dstInfo(dstCT, at, nullptr, w, h); |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 1223 | GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(), |
| 1224 | srcInfo, src, srcInfo.minRowBytes(), |
Brian Salomon | 8f8354a | 2019-07-31 20:12:02 -0400 | [diff] [blame] | 1225 | /* flipY = */ false); |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 1226 | }; |
| 1227 | } |
| 1228 | return result; |
| 1229 | } |
Greg Daniel | 46e366a | 2019-12-16 14:38:36 -0500 | [diff] [blame] | 1230 | |
| 1231 | #ifdef SK_DEBUG |
| 1232 | void GrSurfaceContext::validate() const { |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 1233 | SkASSERT(fReadView.proxy()); |
| 1234 | fReadView.proxy()->validate(fContext); |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 1235 | if (this->colorInfo().colorType() != GrColorType::kUnknown) { |
| 1236 | SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible( |
| 1237 | this->colorInfo().colorType(), fReadView.proxy()->backendFormat())); |
| 1238 | } |
Greg Daniel | 46e366a | 2019-12-16 14:38:36 -0500 | [diff] [blame] | 1239 | this->onValidate(); |
| 1240 | } |
| 1241 | #endif |