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 | |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame^] | 10 | #include <memory> |
| 11 | |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 12 | #include "include/gpu/GrDirectContext.h" |
| 13 | #include "include/gpu/GrRecordingContext.h" |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 14 | #include "src/core/SkAutoPixmapStorage.h" |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 15 | #include "src/core/SkYUVMath.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrAuditTrail.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrContextPriv.h" |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrDataUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/gpu/GrDrawingManager.h" |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrGpu.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 21 | #include "src/gpu/GrImageInfo.h" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 22 | #include "src/gpu/GrProxyProvider.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "src/gpu/GrRecordingContextPriv.h" |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 24 | #include "src/gpu/GrRenderTargetContext.h" |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 25 | #include "src/gpu/GrSurfaceContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 26 | #include "src/gpu/SkGr.h" |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 27 | #include "src/gpu/effects/GrBicubicEffect.h" |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 28 | #include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h" |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 29 | |
Adlai Holler | 33dbd65 | 2020-06-01 12:35:42 -0400 | [diff] [blame] | 30 | #define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner()) |
Robert Phillips | 9eb0002 | 2020-06-30 15:30:12 -0400 | [diff] [blame] | 31 | #define RETURN_FALSE_IF_ABANDONED if (this->fContext->abandoned()) { return false; } |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 32 | |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 33 | std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context, |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 34 | GrSurfaceProxyView readView, |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 35 | GrColorType colorType, |
| 36 | SkAlphaType alphaType, |
| 37 | sk_sp<SkColorSpace> colorSpace) { |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 38 | // It is probably not necessary to check if the context is abandoned here since uses of the |
| 39 | // GrSurfaceContext which need the context will mostly likely fail later on without an issue. |
| 40 | // However having this hear adds some reassurance in case there is a path doesn't handle an |
| 41 | // 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] | 42 | if (context->abandoned()) { |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 43 | return nullptr; |
| 44 | } |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 45 | GrSurfaceProxy* proxy = readView.proxy(); |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 46 | SkASSERT(proxy && proxy->asTextureProxy()); |
| 47 | |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 48 | std::unique_ptr<GrSurfaceContext> surfaceContext; |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 49 | if (proxy->asRenderTargetProxy()) { |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 50 | SkASSERT(kPremul_SkAlphaType == alphaType || kOpaque_SkAlphaType == alphaType); |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 51 | // 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] | 52 | // colorType here? If so we will need to manually pass that in. |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 53 | GrSwizzle writeSwizzle; |
| 54 | if (colorType != GrColorType::kUnknown) { |
| 55 | writeSwizzle = |
| 56 | context->priv().caps()->getWriteSwizzle(proxy->backendFormat(), colorType); |
| 57 | } |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 58 | GrSurfaceProxyView writeView(readView.refProxy(), readView.origin(), writeSwizzle); |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame^] | 59 | surfaceContext = std::make_unique<GrRenderTargetContext>(context, std::move(readView), |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 60 | std::move(writeView), colorType, |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame^] | 61 | std::move(colorSpace), nullptr); |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 62 | } else { |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame^] | 63 | surfaceContext = std::make_unique<GrSurfaceContext>(context, std::move(readView), colorType, |
| 64 | alphaType, std::move(colorSpace)); |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 65 | } |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 66 | SkDEBUGCODE(surfaceContext->validate();) |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 67 | return surfaceContext; |
| 68 | } |
| 69 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 70 | std::unique_ptr<GrSurfaceContext> GrSurfaceContext::Make(GrRecordingContext* context, |
| 71 | SkISize dimensions, |
| 72 | const GrBackendFormat& format, |
| 73 | GrRenderable renderable, |
| 74 | int renderTargetSampleCnt, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 75 | GrMipmapped mipMapped, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 76 | GrProtected isProtected, |
| 77 | GrSurfaceOrigin origin, |
| 78 | GrColorType colorType, |
| 79 | SkAlphaType alphaType, |
| 80 | sk_sp<SkColorSpace> colorSpace, |
| 81 | SkBackingFit fit, |
| 82 | SkBudgeted budgeted) { |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 83 | GrSwizzle swizzle; |
| 84 | if (colorType != GrColorType::kUnknown && !context->priv().caps()->isFormatCompressed(format)) { |
Brian Salomon | d005b69 | 2020-04-01 15:47:05 -0400 | [diff] [blame] | 85 | swizzle = context->priv().caps()->getReadSwizzle(format, colorType); |
| 86 | } |
Greg Daniel | 47c20e8 | 2020-01-21 14:29:57 -0500 | [diff] [blame] | 87 | |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 88 | sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy( |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 89 | format, dimensions, renderable, renderTargetSampleCnt, mipMapped, fit, budgeted, |
| 90 | isProtected); |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 91 | if (!proxy) { |
| 92 | return nullptr; |
| 93 | } |
| 94 | |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 95 | GrSurfaceProxyView view(std::move(proxy), origin, swizzle); |
| 96 | return GrSurfaceContext::Make(context, std::move(view), colorType, alphaType, |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 97 | std::move(colorSpace)); |
| 98 | } |
| 99 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 100 | // In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress |
| 101 | // GrOpsTasks to be picked up and added to by renderTargetContexts lower in the call |
| 102 | // stack. When this occurs with a closed GrOpsTask, a new one will be allocated |
| 103 | // when the renderTargetContext attempts to use it (via getOpsTask). |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 104 | GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context, |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 105 | GrSurfaceProxyView readView, |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 106 | GrColorType colorType, |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 107 | SkAlphaType alphaType, |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 108 | sk_sp<SkColorSpace> colorSpace) |
Greg Daniel | 901b98e | 2019-10-22 09:54:02 -0400 | [diff] [blame] | 109 | : fContext(context) |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 110 | , fReadView(std::move(readView)) |
| 111 | , fColorInfo(colorType, alphaType, std::move(colorSpace)) { |
Robert Phillips | 9eb0002 | 2020-06-30 15:30:12 -0400 | [diff] [blame] | 112 | SkASSERT(!context->abandoned()); |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 113 | } |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 114 | |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 115 | const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); } |
| 116 | |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 117 | GrAuditTrail* GrSurfaceContext::auditTrail() { |
| 118 | return fContext->priv().auditTrail(); |
| 119 | } |
| 120 | |
| 121 | GrDrawingManager* GrSurfaceContext::drawingManager() { |
| 122 | return fContext->priv().drawingManager(); |
| 123 | } |
| 124 | |
| 125 | const GrDrawingManager* GrSurfaceContext::drawingManager() const { |
| 126 | return fContext->priv().drawingManager(); |
| 127 | } |
| 128 | |
| 129 | #ifdef SK_DEBUG |
| 130 | GrSingleOwner* GrSurfaceContext::singleOwner() { |
| 131 | return fContext->priv().singleOwner(); |
| 132 | } |
| 133 | #endif |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 134 | |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 135 | bool GrSurfaceContext::readPixels(const GrImageInfo& origDstInfo, void* dst, size_t rowBytes, |
Robert Phillips | 44333c5 | 2020-06-30 13:28:00 -0400 | [diff] [blame] | 136 | SkIPoint pt, GrDirectContext* direct) { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 137 | ASSERT_SINGLE_OWNER |
| 138 | RETURN_FALSE_IF_ABANDONED |
| 139 | SkDEBUGCODE(this->validate();) |
| 140 | GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels"); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 141 | |
Robert Phillips | f8f45d9 | 2020-07-01 11:11:18 -0400 | [diff] [blame] | 142 | if (!direct && !(direct = fContext->asDirectContext())) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 143 | return false; |
| 144 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 145 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 146 | if (!dst) { |
| 147 | return false; |
| 148 | } |
| 149 | |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 150 | size_t tightRowBytes = origDstInfo.minRowBytes(); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 151 | if (!rowBytes) { |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 152 | rowBytes = tightRowBytes; |
| 153 | } else if (rowBytes < tightRowBytes) { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 154 | return false; |
| 155 | } |
| 156 | |
| 157 | if (!origDstInfo.isValid()) { |
| 158 | return false; |
| 159 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 160 | |
| 161 | GrSurfaceProxy* srcProxy = this->asSurfaceProxy(); |
| 162 | |
Stephen White | 3c0a50f | 2020-01-16 18:19:54 -0500 | [diff] [blame] | 163 | if (srcProxy->framebufferOnly()) { |
| 164 | return false; |
| 165 | } |
| 166 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 167 | // MDB TODO: delay this instantiation until later in the method |
| 168 | if (!srcProxy->instantiate(direct->priv().resourceProvider())) { |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | GrSurface* srcSurface = srcProxy->peekSurface(); |
| 173 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 174 | auto dstInfo = origDstInfo; |
| 175 | if (!dstInfo.clip(this->width(), this->height(), &pt, &dst, rowBytes)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 176 | return false; |
| 177 | } |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 178 | // Our tight row bytes may have been changed by clipping. |
| 179 | tightRowBytes = dstInfo.minRowBytes(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 180 | |
Mike Klein | 7321e6a | 2019-12-03 11:08:40 -0600 | [diff] [blame] | 181 | SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{this->colorInfo(), dstInfo}.flags; |
| 182 | bool unpremul = flags.unpremul, |
| 183 | needColorConversion = flags.linearize || flags.gamut_transform || flags.encode, |
| 184 | premul = flags.premul; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 185 | |
| 186 | const GrCaps* caps = direct->priv().caps(); |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 187 | bool srcIsCompressed = caps->isFormatCompressed(srcSurface->backendFormat()); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 188 | // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't |
| 189 | // care so much about getImageData performance. However, in order to ensure putImageData/ |
| 190 | // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary |
| 191 | // unpremul step to writeSurfacePixels's premul step (which is determined empirically in |
| 192 | // fContext->vaildaPMUPMConversionExists()). |
Greg Daniel | 0258c90 | 2019-08-01 13:08:33 -0400 | [diff] [blame] | 193 | GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888, |
| 194 | GrRenderable::kYes); |
Greg Daniel | c71c796 | 2020-01-14 16:44:18 -0500 | [diff] [blame] | 195 | GrColorType srcColorType = this->colorInfo().colorType(); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 196 | bool canvas2DFastPath = unpremul && !needColorConversion && |
| 197 | (GrColorType::kRGBA_8888 == dstInfo.colorType() || |
| 198 | GrColorType::kBGRA_8888 == dstInfo.colorType()) && |
| 199 | SkToBool(srcProxy->asTextureProxy()) && |
Greg Daniel | c71c796 | 2020-01-14 16:44:18 -0500 | [diff] [blame] | 200 | (srcColorType == GrColorType::kRGBA_8888 || |
| 201 | srcColorType == GrColorType::kBGRA_8888) && |
Greg Daniel | 0258c90 | 2019-08-01 13:08:33 -0400 | [diff] [blame] | 202 | defaultRGBAFormat.isValid() && |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 203 | direct->priv().validPMUPMConversionExists(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 204 | |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 205 | auto readFlag = caps->surfaceSupportsReadPixels(srcSurface); |
Brian Salomon | dc0710f | 2019-07-01 14:59:32 -0400 | [diff] [blame] | 206 | if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) { |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 207 | return false; |
| 208 | } |
| 209 | |
Brian Salomon | dc0710f | 2019-07-01 14:59:32 -0400 | [diff] [blame] | 210 | if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) { |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 211 | GrColorType colorType = (canvas2DFastPath || srcIsCompressed) |
| 212 | ? GrColorType::kRGBA_8888 : this->colorInfo().colorType(); |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 213 | sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorInfo().refColorSpace(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 214 | |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 215 | auto tempCtx = GrRenderTargetContext::Make( |
| 216 | direct, colorType, std::move(cs), SkBackingFit::kApprox, dstInfo.dimensions(), |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 217 | 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 218 | if (!tempCtx) { |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | std::unique_ptr<GrFragmentProcessor> fp; |
| 223 | if (canvas2DFastPath) { |
Greg Daniel | d2ccbb5 | 2020-02-05 10:45:39 -0500 | [diff] [blame] | 224 | fp = direct->priv().createPMToUPMEffect( |
| 225 | GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType())); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 226 | if (dstInfo.colorType() == GrColorType::kBGRA_8888) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 227 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA()); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 228 | dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 229 | } |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 230 | // The render target context is incorrectly tagged as kPremul even though we're writing |
| 231 | // unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type so we don't |
| 232 | // double unpremul. |
| 233 | dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 234 | } else { |
Greg Daniel | d2ccbb5 | 2020-02-05 10:45:39 -0500 | [diff] [blame] | 235 | fp = GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType()); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 236 | } |
| 237 | if (!fp) { |
| 238 | return false; |
| 239 | } |
| 240 | GrPaint paint; |
| 241 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
John Stiles | 5933d7d | 2020-07-21 12:28:35 -0400 | [diff] [blame] | 242 | paint.setColorFragmentProcessor(std::move(fp)); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 243 | |
| 244 | tempCtx->asRenderTargetContext()->fillRectToRect( |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 245 | nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 246 | SkRect::MakeWH(dstInfo.width(), dstInfo.height()), |
| 247 | SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height())); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 248 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 249 | return tempCtx->readPixels(dstInfo, dst, rowBytes, {0, 0}, direct); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 250 | } |
| 251 | |
Greg Daniel | b8d84f8 | 2020-02-13 14:25:00 -0500 | [diff] [blame] | 252 | bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 253 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 254 | auto supportedRead = caps->supportedReadPixelsColorType( |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 255 | this->colorInfo().colorType(), srcProxy->backendFormat(), dstInfo.colorType()); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 256 | |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 257 | bool makeTight = !caps->readPixelsRowBytesSupport() && tightRowBytes != rowBytes; |
| 258 | |
| 259 | bool convert = unpremul || premul || needColorConversion || flip || makeTight || |
Brian Salomon | 8f8354a | 2019-07-31 20:12:02 -0400 | [diff] [blame] | 260 | (dstInfo.colorType() != supportedRead.fColorType); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 261 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 262 | std::unique_ptr<char[]> tmpPixels; |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 263 | GrImageInfo tmpInfo; |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 264 | void* readDst = dst; |
| 265 | size_t readRB = rowBytes; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 266 | if (convert) { |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 267 | tmpInfo = {supportedRead.fColorType, this->colorInfo().alphaType(), |
| 268 | this->colorInfo().refColorSpace(), dstInfo.width(), dstInfo.height()}; |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 269 | size_t tmpRB = tmpInfo.minRowBytes(); |
| 270 | size_t size = tmpRB * tmpInfo.height(); |
| 271 | // Chrome MSAN bots require the data to be initialized (hence the ()). |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame^] | 272 | tmpPixels = std::make_unique<char[]>(size); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 273 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 274 | readDst = tmpPixels.get(); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 275 | readRB = tmpRB; |
| 276 | pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 277 | } |
| 278 | |
Greg Daniel | 55f040b | 2020-02-13 15:38:32 +0000 | [diff] [blame] | 279 | direct->priv().flushSurface(srcProxy); |
Greg Daniel | 04283f3 | 2020-05-20 13:16:00 -0400 | [diff] [blame] | 280 | direct->submit(); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 281 | if (!direct->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(), |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 282 | dstInfo.height(), this->colorInfo().colorType(), |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 283 | supportedRead.fColorType, readDst, readRB)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 284 | return false; |
| 285 | } |
| 286 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 287 | if (convert) { |
Brian Salomon | 8f8354a | 2019-07-31 20:12:02 -0400 | [diff] [blame] | 288 | return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 289 | } |
| 290 | return true; |
| 291 | } |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 292 | |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 293 | bool GrSurfaceContext::writePixels(const GrImageInfo& origSrcInfo, const void* src, size_t rowBytes, |
Robert Phillips | 44333c5 | 2020-06-30 13:28:00 -0400 | [diff] [blame] | 294 | SkIPoint pt, GrDirectContext* direct) { |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 295 | ASSERT_SINGLE_OWNER |
| 296 | RETURN_FALSE_IF_ABANDONED |
| 297 | SkDEBUGCODE(this->validate();) |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 298 | GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels"); |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 299 | |
Robert Phillips | f8f45d9 | 2020-07-01 11:11:18 -0400 | [diff] [blame] | 300 | if (!direct && !(direct = fContext->asDirectContext())) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 301 | return false; |
| 302 | } |
Robert Phillips | 6a6de56 | 2019-02-15 15:19:15 -0500 | [diff] [blame] | 303 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 304 | if (this->asSurfaceProxy()->readOnly()) { |
Robert Phillips | 6a6de56 | 2019-02-15 15:19:15 -0500 | [diff] [blame] | 305 | return false; |
| 306 | } |
| 307 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 308 | if (!src) { |
| 309 | return false; |
| 310 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 311 | |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 312 | size_t tightRowBytes = origSrcInfo.minRowBytes(); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 313 | if (!rowBytes) { |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 314 | rowBytes = tightRowBytes; |
| 315 | } else if (rowBytes < tightRowBytes) { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 316 | return false; |
| 317 | } |
| 318 | |
| 319 | if (!origSrcInfo.isValid()) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 320 | return false; |
| 321 | } |
| 322 | |
| 323 | GrSurfaceProxy* dstProxy = this->asSurfaceProxy(); |
Stephen White | 3c0a50f | 2020-01-16 18:19:54 -0500 | [diff] [blame] | 324 | |
| 325 | if (dstProxy->framebufferOnly()) { |
| 326 | return false; |
| 327 | } |
| 328 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 329 | if (!dstProxy->instantiate(direct->priv().resourceProvider())) { |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | GrSurface* dstSurface = dstProxy->peekSurface(); |
| 334 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 335 | auto srcInfo = origSrcInfo; |
| 336 | if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 337 | return false; |
| 338 | } |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 339 | // Our tight row bytes may have been changed by clipping. |
| 340 | tightRowBytes = srcInfo.minRowBytes(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 341 | |
Mike Klein | 7321e6a | 2019-12-03 11:08:40 -0600 | [diff] [blame] | 342 | SkColorSpaceXformSteps::Flags flags = SkColorSpaceXformSteps{srcInfo, this->colorInfo()}.flags; |
| 343 | bool unpremul = flags.unpremul, |
| 344 | needColorConversion = flags.linearize || flags.gamut_transform || flags.encode, |
| 345 | premul = flags.premul; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 346 | |
| 347 | const GrCaps* caps = direct->priv().caps(); |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 348 | |
| 349 | auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888, |
| 350 | GrRenderable::kNo); |
| 351 | |
Greg Daniel | c71c796 | 2020-01-14 16:44:18 -0500 | [diff] [blame] | 352 | GrColorType dstColorType = this->colorInfo().colorType(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 353 | // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs |
| 354 | // 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] | 355 | bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion && |
| 356 | (srcInfo.colorType() == GrColorType::kRGBA_8888 || |
| 357 | srcInfo.colorType() == GrColorType::kBGRA_8888) && |
| 358 | SkToBool(this->asRenderTargetContext()) && |
Greg Daniel | c71c796 | 2020-01-14 16:44:18 -0500 | [diff] [blame] | 359 | (dstColorType == GrColorType::kRGBA_8888 || |
| 360 | dstColorType == GrColorType::kBGRA_8888) && |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 361 | rgbaDefaultFormat.isValid() && |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 362 | direct->priv().validPMUPMConversionExists(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 363 | |
| 364 | if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) { |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 365 | GrColorType colorType; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 366 | |
| 367 | GrBackendFormat format; |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 368 | SkAlphaType alphaType; |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 369 | GrSwizzle tempReadSwizzle; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 370 | if (canvas2DFastPath) { |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 371 | colorType = GrColorType::kRGBA_8888; |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 372 | format = rgbaDefaultFormat; |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 373 | alphaType = kUnpremul_SkAlphaType; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 374 | } else { |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 375 | colorType = this->colorInfo().colorType(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 376 | format = dstProxy->backendFormat().makeTexture2D(); |
| 377 | if (!format.isValid()) { |
| 378 | return false; |
| 379 | } |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 380 | alphaType = this->colorInfo().alphaType(); |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 381 | tempReadSwizzle = this->readSwizzle(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 382 | } |
| 383 | |
Greg Daniel | 2e52ad1 | 2019-06-13 10:04:16 -0400 | [diff] [blame] | 384 | // It is more efficient for us to write pixels into a top left origin so we prefer that. |
| 385 | // However, if the final proxy isn't a render target then we must use a copy to move the |
| 386 | // data into it which requires the origins to match. If the final proxy is a render target |
| 387 | // we can use a draw instead which doesn't have this origin restriction. Thus for render |
| 388 | // 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] | 389 | GrSurfaceOrigin tempOrigin = |
Greg Daniel | b8d84f8 | 2020-02-13 14:25:00 -0500 | [diff] [blame] | 390 | this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : this->origin(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 391 | auto tempProxy = direct->priv().proxyProvider()->createProxy( |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 392 | format, srcInfo.dimensions(), GrRenderable::kNo, 1, GrMipmapped::kNo, |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 393 | SkBackingFit::kApprox, SkBudgeted::kYes, GrProtected::kNo); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 394 | if (!tempProxy) { |
| 395 | return false; |
| 396 | } |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 397 | GrSurfaceProxyView tempView(tempProxy, tempOrigin, tempReadSwizzle); |
Greg Daniel | d2ccbb5 | 2020-02-05 10:45:39 -0500 | [diff] [blame] | 398 | GrSurfaceContext tempCtx(direct, tempView, colorType, alphaType, |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 399 | this->colorInfo().refColorSpace()); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 400 | |
| 401 | // In the fast path we always write the srcData to the temp context as though it were RGBA. |
| 402 | // When the data is really BGRA the write will cause the R and B channels to be swapped in |
| 403 | // the intermediate surface which gets corrected by a swizzle effect when drawing to the |
| 404 | // dst. |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 405 | if (canvas2DFastPath) { |
| 406 | srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888); |
| 407 | } |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 408 | if (!tempCtx.writePixels(srcInfo, src, rowBytes, {0, 0}, direct)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 409 | return false; |
| 410 | } |
| 411 | |
| 412 | if (this->asRenderTargetContext()) { |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 413 | std::unique_ptr<GrFragmentProcessor> fp; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 414 | if (canvas2DFastPath) { |
Brian Salomon | b8f098d | 2020-01-07 11:15:44 -0500 | [diff] [blame] | 415 | fp = direct->priv().createUPMToPMEffect( |
Greg Daniel | d2ccbb5 | 2020-02-05 10:45:39 -0500 | [diff] [blame] | 416 | GrTextureEffect::Make(std::move(tempView), alphaType)); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 417 | // Important: check the original src color type here! |
| 418 | if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 419 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA()); |
| 420 | } |
| 421 | } else { |
Greg Daniel | d2ccbb5 | 2020-02-05 10:45:39 -0500 | [diff] [blame] | 422 | fp = GrTextureEffect::Make(std::move(tempView), alphaType); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 423 | } |
| 424 | if (!fp) { |
| 425 | return false; |
| 426 | } |
| 427 | GrPaint paint; |
| 428 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
John Stiles | 5933d7d | 2020-07-21 12:28:35 -0400 | [diff] [blame] | 429 | paint.setColorFragmentProcessor(std::move(fp)); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 430 | this->asRenderTargetContext()->fillRectToRect( |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 431 | nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 432 | SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()), |
| 433 | SkRect::MakeWH(srcInfo.width(), srcInfo.height())); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 434 | } else { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 435 | SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height()); |
| 436 | SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY); |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 437 | if (!this->copy(tempProxy.get(), srcRect, dstPoint)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 438 | return false; |
| 439 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 440 | } |
| 441 | return true; |
| 442 | } |
| 443 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 444 | GrColorType allowedColorType = |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 445 | caps->supportedWritePixelsColorType(this->colorInfo().colorType(), |
Brian Salomon | 01915c0 | 2019-08-02 09:57:21 -0400 | [diff] [blame] | 446 | dstProxy->backendFormat(), |
| 447 | srcInfo.colorType()).fColorType; |
Greg Daniel | b8d84f8 | 2020-02-13 14:25:00 -0500 | [diff] [blame] | 448 | bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin; |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 449 | bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes; |
| 450 | bool convert = premul || unpremul || needColorConversion || makeTight || |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 451 | (srcInfo.colorType() != allowedColorType) || flip; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 452 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 453 | std::unique_ptr<char[]> tmpPixels; |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 454 | GrColorType srcColorType = srcInfo.colorType(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 455 | if (convert) { |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 456 | GrImageInfo tmpInfo(allowedColorType, this->colorInfo().alphaType(), |
| 457 | this->colorInfo().refColorSpace(), srcInfo.width(), srcInfo.height()); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 458 | auto tmpRB = tmpInfo.minRowBytes(); |
| 459 | tmpPixels.reset(new char[tmpRB * tmpInfo.height()]); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 460 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 461 | GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 462 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 463 | srcColorType = tmpInfo.colorType(); |
| 464 | rowBytes = tmpRB; |
| 465 | src = tmpPixels.get(); |
| 466 | pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a |
| 470 | // complete flush here. On platforms that prefer VRAM use over flushes we're better off |
| 471 | // giving the drawing manager the chance of skipping the flush (i.e., by passing in the |
| 472 | // destination proxy) |
| 473 | // TODO: should this policy decision just be moved into the drawing manager? |
Greg Daniel | 55f040b | 2020-02-13 15:38:32 +0000 | [diff] [blame] | 474 | direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 475 | |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 476 | return direct->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(), |
| 477 | srcInfo.height(), this->colorInfo().colorType(), |
| 478 | srcColorType, src, rowBytes); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 479 | } |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 480 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 481 | void GrSurfaceContext::asyncRescaleAndReadPixels(const SkImageInfo& info, |
| 482 | const SkIRect& srcRect, |
| 483 | RescaleGamma rescaleGamma, |
| 484 | SkFilterQuality rescaleQuality, |
| 485 | ReadPixelsCallback callback, |
| 486 | ReadPixelsContext context) { |
Robert Phillips | f8f45d9 | 2020-07-01 11:11:18 -0400 | [diff] [blame] | 487 | auto direct = fContext->asDirectContext(); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 488 | |
| 489 | // We implement this by rendering and we don't currently support rendering kUnpremul. |
| 490 | if (info.alphaType() == kUnpremul_SkAlphaType) { |
| 491 | callback(context, nullptr); |
| 492 | return; |
| 493 | } |
| 494 | if (!direct) { |
| 495 | callback(context, nullptr); |
| 496 | return; |
| 497 | } |
| 498 | auto rt = this->asRenderTargetProxy(); |
| 499 | if (rt && rt->wrapsVkSecondaryCB()) { |
| 500 | callback(context, nullptr); |
| 501 | return; |
| 502 | } |
| 503 | if (rt && rt->framebufferOnly()) { |
| 504 | callback(context, nullptr); |
| 505 | return; |
| 506 | } |
| 507 | auto dstCT = SkColorTypeToGrColorType(info.colorType()); |
| 508 | if (dstCT == GrColorType::kUnknown) { |
| 509 | callback(context, nullptr); |
| 510 | return; |
| 511 | } |
| 512 | bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height(); |
| 513 | auto colorTypeOfFinalContext = this->colorInfo().colorType(); |
| 514 | auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat(); |
| 515 | if (needsRescale) { |
| 516 | colorTypeOfFinalContext = dstCT; |
| 517 | backendFormatOfFinalContext = |
| 518 | this->caps()->getDefaultBackendFormat(dstCT, GrRenderable::kYes); |
| 519 | } |
| 520 | auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext, |
| 521 | backendFormatOfFinalContext, dstCT); |
| 522 | // Fail if we can't read from the source surface's color type. |
| 523 | if (readInfo.fColorType == GrColorType::kUnknown) { |
| 524 | callback(context, nullptr); |
| 525 | return; |
| 526 | } |
| 527 | // Fail if read color type does not have all of dstCT's color channels and those missing color |
| 528 | // channels are in the src. |
| 529 | uint32_t dstChannels = GrColorTypeChannelFlags(dstCT); |
| 530 | uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType); |
| 531 | uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType()); |
| 532 | if ((~legalReadChannels & dstChannels) & srcChannels) { |
| 533 | callback(context, nullptr); |
| 534 | return; |
| 535 | } |
| 536 | |
| 537 | std::unique_ptr<GrRenderTargetContext> tempRTC; |
| 538 | int x = srcRect.fLeft; |
| 539 | int y = srcRect.fTop; |
| 540 | if (needsRescale) { |
| 541 | tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma, |
| 542 | rescaleQuality); |
| 543 | if (!tempRTC) { |
| 544 | callback(context, nullptr); |
| 545 | return; |
| 546 | } |
| 547 | SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace())); |
| 548 | SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin); |
| 549 | x = y = 0; |
| 550 | } else { |
| 551 | sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(), |
| 552 | this->colorInfo().alphaType(), |
| 553 | info.colorSpace(), |
| 554 | info.alphaType()); |
| 555 | // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion. |
| 556 | if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) { |
| 557 | GrSurfaceProxyView texProxyView = this->readSurfaceView(); |
| 558 | SkRect srcRectToDraw = SkRect::Make(srcRect); |
| 559 | // If the src is not texturable first try to make a copy to a texture. |
| 560 | if (!texProxyView.asTextureProxy()) { |
| 561 | texProxyView = |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 562 | GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipmapped::kNo, srcRect, |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 563 | SkBackingFit::kApprox, SkBudgeted::kNo); |
| 564 | if (!texProxyView) { |
| 565 | callback(context, nullptr); |
| 566 | return; |
| 567 | } |
| 568 | SkASSERT(texProxyView.asTextureProxy()); |
| 569 | srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height()); |
| 570 | } |
| 571 | tempRTC = GrRenderTargetContext::Make(direct, this->colorInfo().colorType(), |
| 572 | info.refColorSpace(), SkBackingFit::kApprox, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 573 | srcRect.size(), 1, GrMipmapped::kNo, |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 574 | GrProtected::kNo, kTopLeft_GrSurfaceOrigin); |
| 575 | if (!tempRTC) { |
| 576 | callback(context, nullptr); |
| 577 | return; |
| 578 | } |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 579 | tempRTC->drawTexture(nullptr, |
| 580 | std::move(texProxyView), |
| 581 | this->colorInfo().alphaType(), |
| 582 | GrSamplerState::Filter::kNearest, |
| 583 | GrSamplerState::MipmapMode::kNone, |
| 584 | SkBlendMode::kSrc, |
| 585 | SK_PMColor4fWHITE, |
| 586 | srcRectToDraw, |
| 587 | SkRect::MakeWH(srcRect.width(), srcRect.height()), |
| 588 | GrAA::kNo, |
| 589 | GrQuadAAFlags::kNone, |
| 590 | SkCanvas::kFast_SrcRectConstraint, |
| 591 | SkMatrix::I(), |
| 592 | std::move(xform)); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 593 | x = y = 0; |
| 594 | } |
| 595 | } |
| 596 | auto rtc = tempRTC ? tempRTC.get() : this; |
| 597 | return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()), |
| 598 | info.colorType(), callback, context); |
| 599 | } |
| 600 | |
| 601 | class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult { |
| 602 | public: |
| 603 | AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {} |
| 604 | ~AsyncReadResult() override { |
| 605 | for (int i = 0; i < fPlanes.count(); ++i) { |
| 606 | if (!fPlanes[i].fMappedBuffer) { |
| 607 | delete[] static_cast<const char*>(fPlanes[i].fData); |
| 608 | } else { |
| 609 | GrClientMappedBufferManager::BufferFinishedMessageBus::Post( |
| 610 | {std::move(fPlanes[i].fMappedBuffer), fInboxID}); |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | int count() const override { return fPlanes.count(); } |
| 616 | const void* data(int i) const override { return fPlanes[i].fData; } |
| 617 | size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; } |
| 618 | |
| 619 | bool addTransferResult(const PixelTransferResult& result, |
| 620 | SkISize dimensions, |
| 621 | size_t rowBytes, |
| 622 | GrClientMappedBufferManager* manager) { |
| 623 | SkASSERT(!result.fTransferBuffer->isMapped()); |
| 624 | const void* mappedData = result.fTransferBuffer->map(); |
| 625 | if (!mappedData) { |
| 626 | return false; |
| 627 | } |
| 628 | if (result.fPixelConverter) { |
| 629 | std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]); |
| 630 | result.fPixelConverter(convertedData.get(), mappedData); |
| 631 | this->addCpuPlane(std::move(convertedData), rowBytes); |
| 632 | result.fTransferBuffer->unmap(); |
| 633 | } else { |
| 634 | manager->insert(result.fTransferBuffer); |
| 635 | this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer)); |
| 636 | } |
| 637 | return true; |
| 638 | } |
| 639 | |
| 640 | void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) { |
| 641 | SkASSERT(data); |
| 642 | SkASSERT(rowBytes > 0); |
| 643 | fPlanes.emplace_back(data.release(), rowBytes, nullptr); |
| 644 | } |
| 645 | |
| 646 | private: |
| 647 | void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) { |
| 648 | SkASSERT(data); |
| 649 | SkASSERT(rowBytes > 0); |
| 650 | SkASSERT(mappedBuffer); |
| 651 | SkASSERT(mappedBuffer->isMapped()); |
| 652 | fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer)); |
| 653 | } |
| 654 | |
| 655 | struct Plane { |
| 656 | Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer) |
| 657 | : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {} |
| 658 | const void* fData; |
| 659 | size_t fRowBytes; |
| 660 | // If this is null then fData is heap alloc and must be delete[]ed as const char[]. |
| 661 | sk_sp<GrGpuBuffer> fMappedBuffer; |
| 662 | }; |
| 663 | SkSTArray<3, Plane> fPlanes; |
| 664 | uint32_t fInboxID; |
| 665 | }; |
| 666 | |
| 667 | void GrSurfaceContext::asyncReadPixels(const SkIRect& rect, |
| 668 | SkColorType colorType, |
| 669 | ReadPixelsCallback callback, |
| 670 | ReadPixelsContext context) { |
| 671 | SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width()); |
| 672 | SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height()); |
| 673 | |
| 674 | if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) { |
| 675 | callback(context, nullptr); |
| 676 | return; |
| 677 | } |
| 678 | |
Robert Phillips | f8f45d9 | 2020-07-01 11:11:18 -0400 | [diff] [blame] | 679 | auto directContext = fContext->asDirectContext(); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 680 | SkASSERT(directContext); |
| 681 | auto mappedBufferManager = directContext->priv().clientMappedBufferManager(); |
| 682 | |
| 683 | auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect); |
| 684 | |
| 685 | if (!transferResult.fTransferBuffer) { |
| 686 | auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(), |
| 687 | this->colorInfo().refColorSpace()); |
| 688 | auto result = std::make_unique<AsyncReadResult>(0); |
| 689 | std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]); |
| 690 | SkPixmap pm(ii, data.get(), ii.minRowBytes()); |
| 691 | result->addCpuPlane(std::move(data), pm.rowBytes()); |
| 692 | |
| 693 | if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) { |
| 694 | callback(context, nullptr); |
| 695 | return; |
| 696 | } |
| 697 | callback(context, std::move(result)); |
| 698 | return; |
| 699 | } |
| 700 | |
| 701 | struct FinishContext { |
| 702 | ReadPixelsCallback* fClientCallback; |
| 703 | ReadPixelsContext fClientContext; |
| 704 | SkISize fSize; |
| 705 | SkColorType fColorType; |
| 706 | GrClientMappedBufferManager* fMappedBufferManager; |
| 707 | PixelTransferResult fTransferResult; |
| 708 | }; |
| 709 | // Assumption is that the caller would like to flush. We could take a parameter or require an |
| 710 | // explicit flush from the caller. We'd have to have a way to defer attaching the finish |
| 711 | // callback to GrGpu until after the next flush that flushes our op list, though. |
| 712 | auto* finishContext = new FinishContext{callback, |
| 713 | context, |
| 714 | rect.size(), |
| 715 | colorType, |
| 716 | mappedBufferManager, |
| 717 | std::move(transferResult)}; |
| 718 | auto finishCallback = [](GrGpuFinishedContext c) { |
| 719 | const auto* context = reinterpret_cast<const FinishContext*>(c); |
| 720 | auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID()); |
| 721 | size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType); |
| 722 | if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes, |
| 723 | context->fMappedBufferManager)) { |
| 724 | result.reset(); |
| 725 | } |
| 726 | (*context->fClientCallback)(context->fClientContext, std::move(result)); |
| 727 | delete context; |
| 728 | }; |
| 729 | GrFlushInfo flushInfo; |
| 730 | flushInfo.fFinishedContext = finishContext; |
| 731 | flushInfo.fFinishedProc = finishCallback; |
| 732 | this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo, nullptr); |
| 733 | } |
| 734 | |
| 735 | void GrSurfaceContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace, |
| 736 | sk_sp<SkColorSpace> dstColorSpace, |
| 737 | const SkIRect& srcRect, |
| 738 | SkISize dstSize, |
| 739 | RescaleGamma rescaleGamma, |
| 740 | SkFilterQuality rescaleQuality, |
| 741 | ReadPixelsCallback callback, |
| 742 | ReadPixelsContext context) { |
| 743 | SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width()); |
| 744 | SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height()); |
| 745 | SkASSERT(!dstSize.isZero()); |
| 746 | SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0)); |
| 747 | |
Robert Phillips | f8f45d9 | 2020-07-01 11:11:18 -0400 | [diff] [blame] | 748 | auto direct = fContext->asDirectContext(); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 749 | if (!direct) { |
| 750 | callback(context, nullptr); |
| 751 | return; |
| 752 | } |
| 753 | auto rt = this->asRenderTargetProxy(); |
| 754 | if (rt && rt->wrapsVkSecondaryCB()) { |
| 755 | callback(context, nullptr); |
| 756 | return; |
| 757 | } |
| 758 | if (rt && rt->framebufferOnly()) { |
| 759 | callback(context, nullptr); |
| 760 | return; |
| 761 | } |
| 762 | if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) { |
| 763 | callback(context, nullptr); |
| 764 | return; |
| 765 | } |
| 766 | int x = srcRect.fLeft; |
| 767 | int y = srcRect.fTop; |
| 768 | bool needsRescale = srcRect.size() != dstSize; |
| 769 | GrSurfaceProxyView srcView; |
| 770 | if (needsRescale) { |
| 771 | // We assume the caller wants kPremul. There is no way to indicate a preference. |
| 772 | auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType, |
| 773 | dstColorSpace); |
| 774 | // TODO: Incorporate the YUV conversion into last pass of rescaling. |
| 775 | auto tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma, |
| 776 | rescaleQuality); |
| 777 | if (!tempRTC) { |
| 778 | callback(context, nullptr); |
| 779 | return; |
| 780 | } |
| 781 | SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace())); |
| 782 | SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin); |
| 783 | x = y = 0; |
| 784 | srcView = tempRTC->readSurfaceView(); |
| 785 | } else { |
| 786 | srcView = this->readSurfaceView(); |
| 787 | if (!srcView.asTextureProxy()) { |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 788 | srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipmapped::kNo, |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 789 | srcRect, SkBackingFit::kApprox, SkBudgeted::kYes); |
| 790 | if (!srcView) { |
| 791 | // If we can't get a texture copy of the contents then give up. |
| 792 | callback(context, nullptr); |
| 793 | return; |
| 794 | } |
| 795 | SkASSERT(srcView.asTextureProxy()); |
| 796 | x = y = 0; |
| 797 | } |
| 798 | // We assume the caller wants kPremul. There is no way to indicate a preference. |
| 799 | sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make( |
| 800 | this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(), |
| 801 | kPremul_SkAlphaType); |
| 802 | if (xform) { |
| 803 | SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height()); |
| 804 | auto tempRTC = GrRenderTargetContext::Make( |
| 805 | direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 806 | dstSize, 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 807 | if (!tempRTC) { |
| 808 | callback(context, nullptr); |
| 809 | return; |
| 810 | } |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 811 | tempRTC->drawTexture(nullptr, |
| 812 | std::move(srcView), |
| 813 | this->colorInfo().alphaType(), |
| 814 | GrSamplerState::Filter::kNearest, |
| 815 | GrSamplerState::MipmapMode::kNone, |
| 816 | SkBlendMode::kSrc, |
| 817 | SK_PMColor4fWHITE, |
| 818 | srcRectToDraw, |
| 819 | SkRect::Make(srcRect.size()), |
| 820 | GrAA::kNo, |
| 821 | GrQuadAAFlags::kNone, |
| 822 | SkCanvas::kFast_SrcRectConstraint, |
| 823 | SkMatrix::I(), |
| 824 | std::move(xform)); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 825 | srcView = tempRTC->readSurfaceView(); |
| 826 | SkASSERT(srcView.asTextureProxy()); |
| 827 | x = y = 0; |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | auto yRTC = GrRenderTargetContext::MakeWithFallback( |
| 832 | direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 833 | GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 834 | int halfW = dstSize.width() /2; |
| 835 | int halfH = dstSize.height()/2; |
| 836 | auto uRTC = GrRenderTargetContext::MakeWithFallback( |
| 837 | direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 838 | GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 839 | auto vRTC = GrRenderTargetContext::MakeWithFallback( |
| 840 | direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 841 | GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 842 | if (!yRTC || !uRTC || !vRTC) { |
| 843 | callback(context, nullptr); |
| 844 | return; |
| 845 | } |
| 846 | |
| 847 | float baseM[20]; |
| 848 | SkColorMatrix_RGB2YUV(yuvColorSpace, baseM); |
| 849 | |
| 850 | // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost? |
| 851 | |
| 852 | auto texMatrix = SkMatrix::Translate(x, y); |
| 853 | |
| 854 | SkRect dstRectY = SkRect::Make(dstSize); |
| 855 | SkRect dstRectUV = SkRect::MakeWH(halfW, halfH); |
| 856 | |
| 857 | bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport(); |
| 858 | PixelTransferResult yTransfer, uTransfer, vTransfer; |
| 859 | |
| 860 | // This matrix generates (r,g,b,a) = (0, 0, 0, y) |
| 861 | float yM[20]; |
| 862 | std::fill_n(yM, 15, 0.f); |
| 863 | std::copy_n(baseM + 0, 5, yM + 15); |
| 864 | GrPaint yPaint; |
| 865 | auto yTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix); |
| 866 | auto yColFP = GrColorMatrixFragmentProcessor::Make(std::move(yTexFP), yM, |
| 867 | /*unpremulInput=*/false, |
| 868 | /*clampRGBOutput=*/true, |
| 869 | /*premulOutput=*/false); |
John Stiles | 5933d7d | 2020-07-21 12:28:35 -0400 | [diff] [blame] | 870 | yPaint.setColorFragmentProcessor(std::move(yColFP)); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 871 | yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 872 | yRTC->fillRectToRect(nullptr, std::move(yPaint), GrAA::kNo, SkMatrix::I(), dstRectY, dstRectY); |
| 873 | if (!doSynchronousRead) { |
| 874 | yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8, |
| 875 | SkIRect::MakeWH(yRTC->width(), yRTC->height())); |
| 876 | if (!yTransfer.fTransferBuffer) { |
| 877 | callback(context, nullptr); |
| 878 | return; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | texMatrix.preScale(2.f, 2.f); |
| 883 | // This matrix generates (r,g,b,a) = (0, 0, 0, u) |
| 884 | float uM[20]; |
| 885 | std::fill_n(uM, 15, 0.f); |
| 886 | std::copy_n(baseM + 5, 5, uM + 15); |
| 887 | GrPaint uPaint; |
| 888 | auto uTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix, |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 889 | GrSamplerState::Filter::kLinear); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 890 | auto uColFP = GrColorMatrixFragmentProcessor::Make(std::move(uTexFP), uM, |
| 891 | /*unpremulInput=*/false, |
| 892 | /*clampRGBOutput=*/true, |
| 893 | /*premulOutput=*/false); |
John Stiles | 5933d7d | 2020-07-21 12:28:35 -0400 | [diff] [blame] | 894 | uPaint.setColorFragmentProcessor(std::move(uColFP)); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 895 | uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 896 | uRTC->fillRectToRect(nullptr, std::move(uPaint), GrAA::kNo, SkMatrix::I(), dstRectUV, |
| 897 | dstRectUV); |
| 898 | if (!doSynchronousRead) { |
| 899 | uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8, |
| 900 | SkIRect::MakeWH(uRTC->width(), uRTC->height())); |
| 901 | if (!uTransfer.fTransferBuffer) { |
| 902 | callback(context, nullptr); |
| 903 | return; |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | // This matrix generates (r,g,b,a) = (0, 0, 0, v) |
| 908 | float vM[20]; |
| 909 | std::fill_n(vM, 15, 0.f); |
| 910 | std::copy_n(baseM + 10, 5, vM + 15); |
| 911 | GrPaint vPaint; |
| 912 | auto vTexFP = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType(), |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 913 | texMatrix, GrSamplerState::Filter::kLinear); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 914 | auto vColFP = GrColorMatrixFragmentProcessor::Make(std::move(vTexFP), vM, |
| 915 | /*unpremulInput=*/false, |
| 916 | /*clampRGBOutput=*/true, |
| 917 | /*premulOutput=*/false); |
John Stiles | 5933d7d | 2020-07-21 12:28:35 -0400 | [diff] [blame] | 918 | vPaint.setColorFragmentProcessor(std::move(vColFP)); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 919 | vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 920 | vRTC->fillRectToRect(nullptr, std::move(vPaint), GrAA::kNo, SkMatrix::I(), dstRectUV, |
| 921 | dstRectUV); |
| 922 | if (!doSynchronousRead) { |
| 923 | vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8, |
| 924 | SkIRect::MakeWH(vRTC->width(), vRTC->height())); |
| 925 | if (!vTransfer.fTransferBuffer) { |
| 926 | callback(context, nullptr); |
| 927 | return; |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | if (doSynchronousRead) { |
| 932 | GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize); |
| 933 | GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH); |
| 934 | size_t yRB = yInfo.minRowBytes(); |
| 935 | size_t uvRB = uvInfo.minRowBytes(); |
| 936 | std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]); |
| 937 | std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]); |
| 938 | std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]); |
| 939 | if (!yRTC->readPixels(yInfo, y.get(), yRB, {0, 0}, direct) || |
| 940 | !uRTC->readPixels(uvInfo, u.get(), uvRB, {0, 0}, direct) || |
| 941 | !vRTC->readPixels(uvInfo, v.get(), uvRB, {0, 0}, direct)) { |
| 942 | callback(context, nullptr); |
| 943 | return; |
| 944 | } |
| 945 | auto result = std::make_unique<AsyncReadResult>(direct->priv().contextID()); |
| 946 | result->addCpuPlane(std::move(y), yRB ); |
| 947 | result->addCpuPlane(std::move(u), uvRB); |
| 948 | result->addCpuPlane(std::move(v), uvRB); |
| 949 | callback(context, std::move(result)); |
| 950 | return; |
| 951 | } |
| 952 | |
| 953 | struct FinishContext { |
| 954 | ReadPixelsCallback* fClientCallback; |
| 955 | ReadPixelsContext fClientContext; |
| 956 | GrClientMappedBufferManager* fMappedBufferManager; |
| 957 | SkISize fSize; |
| 958 | PixelTransferResult fYTransfer; |
| 959 | PixelTransferResult fUTransfer; |
| 960 | PixelTransferResult fVTransfer; |
| 961 | }; |
| 962 | // Assumption is that the caller would like to flush. We could take a parameter or require an |
| 963 | // explicit flush from the caller. We'd have to have a way to defer attaching the finish |
| 964 | // callback to GrGpu until after the next flush that flushes our op list, though. |
| 965 | auto* finishContext = new FinishContext{callback, |
| 966 | context, |
| 967 | direct->priv().clientMappedBufferManager(), |
| 968 | dstSize, |
| 969 | std::move(yTransfer), |
| 970 | std::move(uTransfer), |
| 971 | std::move(vTransfer)}; |
| 972 | auto finishCallback = [](GrGpuFinishedContext c) { |
| 973 | const auto* context = reinterpret_cast<const FinishContext*>(c); |
| 974 | auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID()); |
| 975 | auto manager = context->fMappedBufferManager; |
| 976 | size_t rowBytes = SkToSizeT(context->fSize.width()); |
| 977 | if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) { |
| 978 | (*context->fClientCallback)(context->fClientContext, nullptr); |
| 979 | delete context; |
| 980 | return; |
| 981 | } |
| 982 | rowBytes /= 2; |
| 983 | SkISize uvSize = {context->fSize.width() / 2, context->fSize.height() / 2}; |
| 984 | if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) { |
| 985 | (*context->fClientCallback)(context->fClientContext, nullptr); |
| 986 | delete context; |
| 987 | return; |
| 988 | } |
| 989 | if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) { |
| 990 | (*context->fClientCallback)(context->fClientContext, nullptr); |
| 991 | delete context; |
| 992 | return; |
| 993 | } |
| 994 | (*context->fClientCallback)(context->fClientContext, std::move(result)); |
| 995 | delete context; |
| 996 | }; |
| 997 | GrFlushInfo flushInfo; |
| 998 | flushInfo.fFinishedContext = finishContext; |
| 999 | flushInfo.fFinishedProc = finishCallback; |
| 1000 | this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo, nullptr); |
| 1001 | } |
| 1002 | |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 1003 | bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) { |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 1004 | ASSERT_SINGLE_OWNER |
| 1005 | RETURN_FALSE_IF_ABANDONED |
| 1006 | SkDEBUGCODE(this->validate();) |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 1007 | GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy"); |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 1008 | |
Brian Salomon | 947efe2 | 2019-07-16 15:36:11 -0400 | [diff] [blame] | 1009 | const GrCaps* caps = fContext->priv().caps(); |
| 1010 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 1011 | SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal); |
Greg Daniel | c71c796 | 2020-01-14 16:44:18 -0500 | [diff] [blame] | 1012 | SkASSERT(src->backendFormat() == this->asSurfaceProxy()->backendFormat()); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 1013 | |
Stephen White | 3c0a50f | 2020-01-16 18:19:54 -0500 | [diff] [blame] | 1014 | if (this->asSurfaceProxy()->framebufferOnly()) { |
| 1015 | return false; |
| 1016 | } |
| 1017 | |
Chris Dalton | f8e5aad | 2019-08-02 12:55:23 -0600 | [diff] [blame] | 1018 | if (!caps->canCopySurface(this->asSurfaceProxy(), src, srcRect, dstPoint)) { |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 1019 | return false; |
| 1020 | } |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 1021 | |
Greg Daniel | 16f5c65 | 2019-10-29 11:26:01 -0400 | [diff] [blame] | 1022 | // The swizzle doesn't matter for copies and it is not used. |
| 1023 | return this->drawingManager()->newCopyRenderTask( |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 1024 | GrSurfaceProxyView(sk_ref_sp(src), this->origin(), GrSwizzle("rgba")), srcRect, |
Greg Daniel | 46e366a | 2019-12-16 14:38:36 -0500 | [diff] [blame] | 1025 | this->readSurfaceView(), dstPoint); |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 1026 | } |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 1027 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 1028 | std::unique_ptr<GrRenderTargetContext> GrSurfaceContext::rescale(const GrImageInfo& info, |
| 1029 | GrSurfaceOrigin origin, |
| 1030 | SkIRect srcRect, |
| 1031 | RescaleGamma rescaleGamma, |
| 1032 | SkFilterQuality rescaleQuality) { |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1033 | auto rtProxy = this->asRenderTargetProxy(); |
| 1034 | if (rtProxy && rtProxy->wrapsVkSecondaryCB()) { |
| 1035 | return nullptr; |
| 1036 | } |
| 1037 | |
Stephen White | 3c0a50f | 2020-01-16 18:19:54 -0500 | [diff] [blame] | 1038 | if (this->asSurfaceProxy()->framebufferOnly()) { |
| 1039 | return nullptr; |
| 1040 | } |
| 1041 | |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1042 | // We rescale by drawing and don't currently support drawing to a kUnpremul destination. |
| 1043 | if (info.alphaType() == kUnpremul_SkAlphaType) { |
| 1044 | return nullptr; |
| 1045 | } |
| 1046 | |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 1047 | GrSurfaceProxyView texView = this->readSurfaceView(); |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 1048 | SkAlphaType srcAlphaType = this->colorInfo().alphaType(); |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 1049 | if (!texView.asTextureProxy()) { |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 1050 | texView = GrSurfaceProxyView::Copy(fContext, std::move(texView), GrMipmapped::kNo, srcRect, |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 1051 | SkBackingFit::kApprox, SkBudgeted::kNo); |
| 1052 | if (!texView) { |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1053 | return nullptr; |
| 1054 | } |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 1055 | SkASSERT(texView.asTextureProxy()); |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 1056 | srcRect = SkIRect::MakeSize(srcRect.size()); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1057 | } |
| 1058 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 1059 | // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the |
| 1060 | // pass B is moved to A. If 'this' is the input on the first pass then tempA is null. |
| 1061 | std::unique_ptr<GrRenderTargetContext> tempA; |
| 1062 | std::unique_ptr<GrRenderTargetContext> tempB; |
| 1063 | |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1064 | // Assume we should ignore the rescale linear request if the surface has no color space since |
| 1065 | // it's unclear how we'd linearize from an unknown color space. |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 1066 | if (rescaleGamma == RescaleGamma::kLinear && this->colorInfo().colorSpace() && |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 1067 | !this->colorInfo().colorSpace()->gammaIsLinear()) { |
| 1068 | auto cs = this->colorInfo().colorSpace()->makeLinearGamma(); |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 1069 | auto xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(), srcAlphaType, cs.get(), |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1070 | kPremul_SkAlphaType); |
| 1071 | // We'll fall back to kRGBA_8888 if half float not supported. |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 1072 | auto linearRTC = GrRenderTargetContext::MakeWithFallback( |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 1073 | fContext, GrColorType::kRGBA_F16, cs, SkBackingFit::kApprox, srcRect.size(), 1, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 1074 | GrMipmapped::kNo, GrProtected::kNo, origin); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1075 | if (!linearRTC) { |
| 1076 | return nullptr; |
| 1077 | } |
Brian Salomon | 11ad4cc | 2020-05-15 12:07:59 -0400 | [diff] [blame] | 1078 | // 1-to-1 draw can always be kFast. |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 1079 | linearRTC->drawTexture(nullptr, |
| 1080 | std::move(texView), |
| 1081 | srcAlphaType, |
| 1082 | GrSamplerState::Filter::kNearest, |
| 1083 | GrSamplerState::MipmapMode::kNone, |
| 1084 | SkBlendMode::kSrc, |
| 1085 | SK_PMColor4fWHITE, |
| 1086 | SkRect::Make(srcRect), |
| 1087 | SkRect::Make(srcRect.size()), |
| 1088 | GrAA::kNo, |
| 1089 | GrQuadAAFlags::kNone, |
| 1090 | SkCanvas::kFast_SrcRectConstraint, |
| 1091 | SkMatrix::I(), |
| 1092 | std::move(xform)); |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 1093 | texView = linearRTC->readSurfaceView(); |
| 1094 | SkASSERT(texView.asTextureProxy()); |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 1095 | tempA = std::move(linearRTC); |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 1096 | srcRect = SkIRect::MakeSize(srcRect.size()); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1097 | } |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 1098 | |
Brian Salomon | 9c6f6bf | 2020-05-29 16:37:26 -0400 | [diff] [blame] | 1099 | while (srcRect.size() != info.dimensions()) { |
Brian Salomon | 59f31b1 | 2020-06-04 17:27:15 -0400 | [diff] [blame] | 1100 | SkISize nextDims = info.dimensions(); |
| 1101 | if (rescaleQuality != kNone_SkFilterQuality) { |
| 1102 | if (srcRect.width() > info.width()) { |
| 1103 | nextDims.fWidth = std::max((srcRect.width() + 1)/2, info.width()); |
| 1104 | } else if (srcRect.width() < info.width()) { |
| 1105 | nextDims.fWidth = std::min(srcRect.width()*2, info.width()); |
| 1106 | } |
| 1107 | if (srcRect.height() > info.height()) { |
| 1108 | nextDims.fHeight = std::max((srcRect.height() + 1)/2, info.height()); |
| 1109 | } else if (srcRect.height() < info.height()) { |
| 1110 | nextDims.fHeight = std::min(srcRect.height()*2, info.height()); |
| 1111 | } |
| 1112 | } |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 1113 | auto input = tempA ? tempA.get() : this; |
Brian Salomon | 59f31b1 | 2020-06-04 17:27:15 -0400 | [diff] [blame] | 1114 | GrColorType colorType = input->colorInfo().colorType(); |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 1115 | auto cs = input->colorInfo().refColorSpace(); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1116 | sk_sp<GrColorSpaceXform> xform; |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 1117 | auto prevAlphaType = input->colorInfo().alphaType(); |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 1118 | if (nextDims == info.dimensions()) { |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1119 | // Might as well fold conversion to final info in the last step. |
| 1120 | cs = info.refColorSpace(); |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 1121 | xform = GrColorSpaceXform::Make(input->colorInfo().colorSpace(), |
| 1122 | input->colorInfo().alphaType(), cs.get(), |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1123 | info.alphaType()); |
| 1124 | } |
Brian Salomon | 11ad4cc | 2020-05-15 12:07:59 -0400 | [diff] [blame] | 1125 | tempB = GrRenderTargetContext::MakeWithFallback(fContext, colorType, std::move(cs), |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 1126 | SkBackingFit::kApprox, nextDims, 1, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 1127 | GrMipmapped::kNo, GrProtected::kNo, origin); |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 1128 | if (!tempB) { |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1129 | return nullptr; |
| 1130 | } |
Brian Salomon | 59f31b1 | 2020-06-04 17:27:15 -0400 | [diff] [blame] | 1131 | auto dstRect = SkRect::Make(nextDims); |
| 1132 | if (rescaleQuality == kHigh_SkFilterQuality) { |
| 1133 | SkMatrix matrix; |
| 1134 | matrix.setScaleTranslate((float)srcRect.width()/nextDims.width(), |
| 1135 | (float)srcRect.height()/nextDims.height(), |
| 1136 | srcRect.x(), |
| 1137 | srcRect.y()); |
| 1138 | std::unique_ptr<GrFragmentProcessor> fp; |
| 1139 | auto dir = GrBicubicEffect::Direction::kXY; |
| 1140 | if (nextDims.width() == srcRect.width()) { |
| 1141 | dir = GrBicubicEffect::Direction::kY; |
| 1142 | } else if (nextDims.height() == srcRect.height()) { |
| 1143 | dir = GrBicubicEffect::Direction::kX; |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1144 | } |
Brian Salomon | 1af72d1 | 2020-06-25 10:47:26 -0400 | [diff] [blame] | 1145 | static constexpr auto kWM = GrSamplerState::WrapMode::kClamp; |
| 1146 | static constexpr auto kKernel = GrBicubicEffect::Kernel::kCatmullRom; |
Brian Salomon | 59f31b1 | 2020-06-04 17:27:15 -0400 | [diff] [blame] | 1147 | fp = GrBicubicEffect::MakeSubset(std::move(texView), prevAlphaType, matrix, kWM, kWM, |
Brian Salomon | 1af72d1 | 2020-06-25 10:47:26 -0400 | [diff] [blame] | 1148 | SkRect::Make(srcRect), kKernel, dir, *this->caps()); |
Brian Salomon | 59f31b1 | 2020-06-04 17:27:15 -0400 | [diff] [blame] | 1149 | if (xform) { |
| 1150 | fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform)); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1151 | } |
Brian Salomon | 59f31b1 | 2020-06-04 17:27:15 -0400 | [diff] [blame] | 1152 | GrPaint paint; |
John Stiles | 5933d7d | 2020-07-21 12:28:35 -0400 | [diff] [blame] | 1153 | paint.setColorFragmentProcessor(std::move(fp)); |
Brian Salomon | 59f31b1 | 2020-06-04 17:27:15 -0400 | [diff] [blame] | 1154 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 1155 | tempB->fillRectToRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect, |
| 1156 | dstRect); |
| 1157 | } else { |
| 1158 | auto filter = rescaleQuality == kNone_SkFilterQuality ? GrSamplerState::Filter::kNearest |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 1159 | : GrSamplerState::Filter::kLinear; |
Brian Salomon | 59f31b1 | 2020-06-04 17:27:15 -0400 | [diff] [blame] | 1160 | // Minimizing draw with integer coord src and dev rects can always be kFast. |
| 1161 | auto constraint = SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint; |
| 1162 | if (nextDims.width() <= srcRect.width() && nextDims.height() <= srcRect.height()) { |
| 1163 | constraint = SkCanvas::SrcRectConstraint::kFast_SrcRectConstraint; |
| 1164 | } |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 1165 | tempB->drawTexture(nullptr, |
| 1166 | std::move(texView), |
| 1167 | srcAlphaType, |
| 1168 | filter, |
| 1169 | GrSamplerState::MipmapMode::kNone, |
| 1170 | SkBlendMode::kSrc, |
| 1171 | SK_PMColor4fWHITE, |
| 1172 | SkRect::Make(srcRect), |
| 1173 | dstRect, |
| 1174 | GrAA::kNo, |
| 1175 | GrQuadAAFlags::kNone, |
| 1176 | constraint, |
| 1177 | SkMatrix::I(), |
| 1178 | std::move(xform)); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1179 | } |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 1180 | texView = tempB->readSurfaceView(); |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 1181 | tempA = std::move(tempB); |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 1182 | srcRect = SkIRect::MakeSize(nextDims); |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1183 | } |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 1184 | SkASSERT(tempA); |
| 1185 | return tempA; |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 1186 | } |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 1187 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 1188 | GrSemaphoresSubmitted GrSurfaceContext::flush(SkSurface::BackendSurfaceAccess access, |
| 1189 | const GrFlushInfo& info, |
| 1190 | const GrBackendSurfaceMutableState* newState) { |
| 1191 | ASSERT_SINGLE_OWNER |
Robert Phillips | 9eb0002 | 2020-06-30 15:30:12 -0400 | [diff] [blame] | 1192 | if (fContext->abandoned()) { |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 1193 | if (info.fSubmittedProc) { |
| 1194 | info.fSubmittedProc(info.fSubmittedContext, false); |
| 1195 | } |
| 1196 | if (info.fFinishedProc) { |
| 1197 | info.fFinishedProc(info.fFinishedContext); |
| 1198 | } |
| 1199 | return GrSemaphoresSubmitted::kNo; |
| 1200 | } |
| 1201 | SkDEBUGCODE(this->validate();) |
| 1202 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext); |
| 1203 | |
| 1204 | return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info, newState); |
| 1205 | } |
| 1206 | |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 1207 | GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT, |
| 1208 | const SkIRect& rect) { |
| 1209 | SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width()); |
| 1210 | SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height()); |
Robert Phillips | f8f45d9 | 2020-07-01 11:11:18 -0400 | [diff] [blame] | 1211 | auto direct = fContext->asDirectContext(); |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 1212 | if (!direct) { |
| 1213 | return {}; |
| 1214 | } |
| 1215 | auto rtProxy = this->asRenderTargetProxy(); |
| 1216 | if (rtProxy && rtProxy->wrapsVkSecondaryCB()) { |
| 1217 | return {}; |
| 1218 | } |
| 1219 | |
| 1220 | auto proxy = this->asSurfaceProxy(); |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 1221 | auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(), |
| 1222 | proxy->backendFormat(), dstCT); |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 1223 | // Fail if read color type does not have all of dstCT's color channels and those missing color |
| 1224 | // channels are in the src. |
Brian Salomon | 2f23ae6 | 2020-03-26 16:17:56 -0400 | [diff] [blame] | 1225 | uint32_t dstChannels = GrColorTypeChannelFlags(dstCT); |
| 1226 | uint32_t legalReadChannels = GrColorTypeChannelFlags(supportedRead.fColorType); |
| 1227 | uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType()); |
| 1228 | if ((~legalReadChannels & dstChannels) & srcChannels) { |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 1229 | return {}; |
| 1230 | } |
| 1231 | |
Brian Salomon | fb28c6f | 2020-01-10 13:04:45 -0500 | [diff] [blame] | 1232 | if (!this->caps()->transferFromSurfaceToBufferSupport() || |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 1233 | !supportedRead.fOffsetAlignmentForTransferBuffer) { |
| 1234 | return {}; |
| 1235 | } |
| 1236 | |
| 1237 | size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width(); |
| 1238 | size_t size = rowBytes * rect.height(); |
| 1239 | auto buffer = direct->priv().resourceProvider()->createBuffer( |
| 1240 | size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern); |
| 1241 | if (!buffer) { |
| 1242 | return {}; |
| 1243 | } |
| 1244 | auto srcRect = rect; |
Greg Daniel | b8d84f8 | 2020-02-13 14:25:00 -0500 | [diff] [blame] | 1245 | bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin; |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 1246 | if (flip) { |
| 1247 | srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight, |
| 1248 | this->height() - rect.fTop); |
| 1249 | } |
Greg Daniel | bbfec9d | 2019-08-20 10:56:51 -0400 | [diff] [blame] | 1250 | this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect, |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 1251 | this->colorInfo().colorType(), |
Greg Daniel | bbfec9d | 2019-08-20 10:56:51 -0400 | [diff] [blame] | 1252 | supportedRead.fColorType, buffer, 0); |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 1253 | PixelTransferResult result; |
| 1254 | result.fTransferBuffer = std::move(buffer); |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 1255 | auto at = this->colorInfo().alphaType(); |
Brian Salomon | 8f8354a | 2019-07-31 20:12:02 -0400 | [diff] [blame] | 1256 | if (supportedRead.fColorType != dstCT || flip) { |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 1257 | result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at]( |
| 1258 | void* dst, const void* src) { |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 1259 | GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h); |
| 1260 | GrImageInfo dstInfo(dstCT, at, nullptr, w, h); |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 1261 | GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(), |
| 1262 | srcInfo, src, srcInfo.minRowBytes(), |
Brian Salomon | 8f8354a | 2019-07-31 20:12:02 -0400 | [diff] [blame] | 1263 | /* flipY = */ false); |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 1264 | }; |
| 1265 | } |
| 1266 | return result; |
| 1267 | } |
Greg Daniel | 46e366a | 2019-12-16 14:38:36 -0500 | [diff] [blame] | 1268 | |
| 1269 | #ifdef SK_DEBUG |
| 1270 | void GrSurfaceContext::validate() const { |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 1271 | SkASSERT(fReadView.proxy()); |
| 1272 | fReadView.proxy()->validate(fContext); |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 1273 | if (this->colorInfo().colorType() != GrColorType::kUnknown) { |
| 1274 | SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible( |
| 1275 | this->colorInfo().colorType(), fReadView.proxy()->backendFormat())); |
| 1276 | } |
Greg Daniel | 46e366a | 2019-12-16 14:38:36 -0500 | [diff] [blame] | 1277 | this->onValidate(); |
| 1278 | } |
| 1279 | #endif |