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