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