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