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