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