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