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