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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/private/GrRecordingContext.h" |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 11 | #include "src/core/SkAutoPixmapStorage.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrAuditTrail.h" |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrClip.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrContextPriv.h" |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrDataUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/GrDrawingManager.h" |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 17 | #include "src/gpu/GrGpu.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrOpList.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/gpu/GrRecordingContextPriv.h" |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrRenderTargetContext.h" |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 21 | #include "src/gpu/GrSurfaceContextPriv.h" |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 22 | #include "src/gpu/GrSurfacePriv.h" |
| 23 | #include "src/gpu/GrTextureContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/gpu/SkGr.h" |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 25 | |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 26 | #define ASSERT_SINGLE_OWNER \ |
| 27 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());) |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 28 | #define RETURN_FALSE_IF_ABANDONED if (this->fContext->priv().abandoned()) { return false; } |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 29 | |
| 30 | // In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress |
| 31 | // GrOpLists to be picked up and added to by renderTargetContexts lower in the call |
| 32 | // stack. When this occurs with a closed GrOpList, a new one will be allocated |
| 33 | // when the renderTargetContext attempts to use it (via getOpList). |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 34 | GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context, |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 35 | SkAlphaType alphaType, |
| 36 | sk_sp<SkColorSpace> colorSpace, |
| 37 | GrPixelConfig config) |
| 38 | : fContext(context), fColorSpaceInfo(alphaType, std::move(colorSpace), config) {} |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 39 | |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 40 | GrAuditTrail* GrSurfaceContext::auditTrail() { |
| 41 | return fContext->priv().auditTrail(); |
| 42 | } |
| 43 | |
| 44 | GrDrawingManager* GrSurfaceContext::drawingManager() { |
| 45 | return fContext->priv().drawingManager(); |
| 46 | } |
| 47 | |
| 48 | const GrDrawingManager* GrSurfaceContext::drawingManager() const { |
| 49 | return fContext->priv().drawingManager(); |
| 50 | } |
| 51 | |
| 52 | #ifdef SK_DEBUG |
| 53 | GrSingleOwner* GrSurfaceContext::singleOwner() { |
| 54 | return fContext->priv().singleOwner(); |
| 55 | } |
| 56 | #endif |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 57 | static bool valid_premul_color_type(GrColorType ct) { |
| 58 | switch (ct) { |
| 59 | case GrColorType::kUnknown: return false; |
| 60 | case GrColorType::kAlpha_8: return false; |
Greg Daniel | 48fec76 | 2019-06-18 17:06:43 -0400 | [diff] [blame] | 61 | case GrColorType::kBGR_565: return false; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 62 | case GrColorType::kABGR_4444: return true; |
| 63 | case GrColorType::kRGBA_8888: return true; |
| 64 | case GrColorType::kRGB_888x: return false; |
| 65 | case GrColorType::kRG_88: return false; |
| 66 | case GrColorType::kBGRA_8888: return true; |
| 67 | case GrColorType::kRGBA_1010102: return true; |
| 68 | case GrColorType::kGray_8: return false; |
| 69 | case GrColorType::kAlpha_F16: return false; |
| 70 | case GrColorType::kRGBA_F16: return true; |
| 71 | case GrColorType::kRGBA_F16_Clamped: return true; |
| 72 | case GrColorType::kRG_F32: return false; |
| 73 | case GrColorType::kRGBA_F32: return true; |
| 74 | case GrColorType::kRGB_ETC1: return false; |
Robert Phillips | fe18de5 | 2019-06-06 17:21:50 -0400 | [diff] [blame] | 75 | case GrColorType::kR_16: return false; |
| 76 | case GrColorType::kRG_1616: return false; |
Robert Phillips | 66a4603 | 2019-06-18 08:00:42 -0400 | [diff] [blame] | 77 | // Experimental (for Y416 and mutant P016/P010) |
| 78 | case GrColorType::kRGBA_16161616: return false; |
Brian Salomon | e14cfbe | 2019-06-24 15:00:58 -0400 | [diff] [blame^] | 79 | case GrColorType::kRG_F16: return false; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 80 | } |
| 81 | SK_ABORT("Invalid GrColorType"); |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | // TODO: This will be removed when GrSurfaceContexts are aware of their color types. |
| 86 | // (skbug.com/6718) |
| 87 | static bool valid_premul_config(GrPixelConfig config) { |
| 88 | switch (config) { |
| 89 | case kUnknown_GrPixelConfig: return false; |
| 90 | case kAlpha_8_GrPixelConfig: return false; |
| 91 | case kGray_8_GrPixelConfig: return false; |
| 92 | case kRGB_565_GrPixelConfig: return false; |
| 93 | case kRGBA_4444_GrPixelConfig: return true; |
| 94 | case kRGBA_8888_GrPixelConfig: return true; |
| 95 | case kRGB_888_GrPixelConfig: return false; |
| 96 | case kRGB_888X_GrPixelConfig: return false; |
| 97 | case kRG_88_GrPixelConfig: return false; |
| 98 | case kBGRA_8888_GrPixelConfig: return true; |
| 99 | case kSRGBA_8888_GrPixelConfig: return true; |
| 100 | case kSBGRA_8888_GrPixelConfig: return true; |
| 101 | case kRGBA_1010102_GrPixelConfig: return true; |
| 102 | case kRGBA_float_GrPixelConfig: return true; |
| 103 | case kRG_float_GrPixelConfig: return false; |
| 104 | case kAlpha_half_GrPixelConfig: return false; |
| 105 | case kRGBA_half_GrPixelConfig: return true; |
| 106 | case kRGBA_half_Clamped_GrPixelConfig: return true; |
| 107 | case kRGB_ETC1_GrPixelConfig: return false; |
| 108 | case kAlpha_8_as_Alpha_GrPixelConfig: return false; |
| 109 | case kAlpha_8_as_Red_GrPixelConfig: return false; |
| 110 | case kAlpha_half_as_Red_GrPixelConfig: return false; |
| 111 | case kGray_8_as_Lum_GrPixelConfig: return false; |
| 112 | case kGray_8_as_Red_GrPixelConfig: return false; |
Robert Phillips | fe18de5 | 2019-06-06 17:21:50 -0400 | [diff] [blame] | 113 | case kR_16_GrPixelConfig: return false; |
| 114 | case kRG_1616_GrPixelConfig: return false; |
Robert Phillips | 66a4603 | 2019-06-18 08:00:42 -0400 | [diff] [blame] | 115 | // Experimental (for Y416 and mutant P016/P010) |
| 116 | case kRGBA_16161616_GrPixelConfig: return false; |
| 117 | case kRG_half_GrPixelConfig: return false; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 118 | } |
| 119 | SK_ABORT("Invalid GrPixelConfig"); |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig, |
| 124 | bool premulConversion) { |
| 125 | // We only allow premul <-> unpremul conversions for some formats |
| 126 | if (premulConversion && |
| 127 | (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) { |
| 128 | return false; |
| 129 | } |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | bool GrSurfaceContext::readPixelsImpl(GrContext* direct, int left, int top, int width, |
| 134 | int height, GrColorType dstColorType, |
| 135 | SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes, |
| 136 | uint32_t pixelOpsFlags) { |
| 137 | SkASSERT(buffer); |
| 138 | |
| 139 | GrSurfaceProxy* srcProxy = this->asSurfaceProxy(); |
| 140 | |
| 141 | // MDB TODO: delay this instantiation until later in the method |
| 142 | if (!srcProxy->instantiate(direct->priv().resourceProvider())) { |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | GrSurface* srcSurface = srcProxy->peekSurface(); |
| 147 | |
| 148 | if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(), |
| 149 | GrColorTypeBytesPerPixel(dstColorType), &left, &top, |
| 150 | &width, &height, &buffer, &rowBytes)) { |
| 151 | return false; |
| 152 | } |
| 153 | |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 154 | // TODO: Pass dst buffer's alpha type. |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 155 | bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags); |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 156 | SkASSERT(!unpremul || this->colorSpaceInfo().alphaType() != kUnpremul_SkAlphaType); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 157 | |
| 158 | if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | bool needColorConversion = |
| 163 | SkColorSpaceXformSteps::Required(this->colorSpaceInfo().colorSpace(), dstColorSpace); |
| 164 | |
| 165 | const GrCaps* caps = direct->priv().caps(); |
| 166 | // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't |
| 167 | // care so much about getImageData performance. However, in order to ensure putImageData/ |
| 168 | // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary |
| 169 | // unpremul step to writeSurfacePixels's premul step (which is determined empirically in |
| 170 | // fContext->vaildaPMUPMConversionExists()). |
| 171 | bool canvas2DFastPath = |
| 172 | unpremul && |
| 173 | !needColorConversion && |
| 174 | (GrColorType::kRGBA_8888 == dstColorType || GrColorType::kBGRA_8888 == dstColorType) && |
| 175 | SkToBool(srcProxy->asTextureProxy()) && |
| 176 | (srcProxy->config() == kRGBA_8888_GrPixelConfig || |
| 177 | srcProxy->config() == kBGRA_8888_GrPixelConfig) && |
| 178 | caps->isConfigRenderable(kRGBA_8888_GrPixelConfig) && |
| 179 | direct->priv().validPMUPMConversionExists(); |
| 180 | |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 181 | auto readFlag = caps->surfaceSupportsReadPixels(srcSurface); |
| 182 | if (readFlag == GrCaps::kProtected_ReadFlag) { |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | if (readFlag == GrCaps::kRequiresCopy_ReadFlag || canvas2DFastPath) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 187 | GrBackendFormat format; |
| 188 | GrPixelConfig config; |
| 189 | if (canvas2DFastPath) { |
| 190 | config = kRGBA_8888_GrPixelConfig; |
| 191 | format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType); |
| 192 | } else { |
| 193 | config = srcProxy->config(); |
| 194 | format = srcProxy->backendFormat().makeTexture2D(); |
| 195 | if (!format.isValid()) { |
| 196 | return false; |
| 197 | } |
| 198 | } |
| 199 | sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorSpaceInfo().refColorSpace(); |
| 200 | |
| 201 | sk_sp<GrRenderTargetContext> tempCtx = direct->priv().makeDeferredRenderTargetContext( |
| 202 | format, SkBackingFit::kApprox, width, height, config, std::move(cs), 1, |
| 203 | GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin, nullptr, SkBudgeted::kYes); |
| 204 | if (!tempCtx) { |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | std::unique_ptr<GrFragmentProcessor> fp; |
| 209 | if (canvas2DFastPath) { |
| 210 | fp = direct->priv().createPMToUPMEffect( |
| 211 | GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()), |
| 212 | SkMatrix::I())); |
| 213 | if (dstColorType == GrColorType::kBGRA_8888) { |
| 214 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA()); |
| 215 | dstColorType = GrColorType::kRGBA_8888; |
| 216 | } |
| 217 | } else { |
| 218 | fp = GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()), SkMatrix::I()); |
| 219 | } |
| 220 | if (!fp) { |
| 221 | return false; |
| 222 | } |
| 223 | GrPaint paint; |
| 224 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 225 | paint.addColorFragmentProcessor(std::move(fp)); |
| 226 | |
| 227 | tempCtx->asRenderTargetContext()->fillRectToRect( |
| 228 | GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), |
| 229 | SkRect::MakeWH(width, height), SkRect::MakeXYWH(left, top, width, height)); |
| 230 | |
| 231 | uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags; |
| 232 | return tempCtx->readPixelsImpl(direct, 0, 0, width, height, dstColorType, dstColorSpace, |
| 233 | buffer, rowBytes, flags); |
| 234 | } |
| 235 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 236 | bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 237 | auto supportedRead = caps->supportedReadPixelsColorType( |
| 238 | srcProxy->config(), srcProxy->backendFormat(), dstColorType); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 239 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 240 | bool convert = unpremul || needColorConversion || flip || |
| 241 | (dstColorType != supportedRead.fColorType) || |
| 242 | supportedRead.fSwizzle != GrSwizzle::RGBA(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 243 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 244 | std::unique_ptr<char[]> tmpPixels; |
| 245 | GrPixelInfo tmpInfo; |
| 246 | GrPixelInfo dstInfo; |
| 247 | void* readDst = buffer; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 248 | if (convert) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 249 | tmpInfo.fColorInfo.fColorType = supportedRead.fColorType; |
| 250 | tmpInfo.fColorInfo.fAlphaType = kPremul_SkAlphaType; |
| 251 | tmpInfo.fColorInfo.fColorSpace = this->colorSpaceInfo().colorSpace(); |
| 252 | tmpInfo.fOrigin = srcProxy->origin(); |
| 253 | tmpInfo.fRowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * width; |
| 254 | |
| 255 | dstInfo.fColorInfo.fColorType = dstColorType; |
| 256 | dstInfo.fColorInfo.fAlphaType = unpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType; |
| 257 | dstInfo.fColorInfo.fColorSpace = dstColorSpace; |
| 258 | dstInfo.fOrigin = kTopLeft_GrSurfaceOrigin; |
| 259 | dstInfo.fRowBytes = rowBytes; |
| 260 | |
| 261 | dstInfo.fWidth = tmpInfo.fWidth = width; |
| 262 | dstInfo.fHeight = tmpInfo.fHeight = height; |
| 263 | |
| 264 | size_t size = tmpInfo.fRowBytes * height; |
| 265 | tmpPixels.reset(new char[size]); |
| 266 | // Chrome MSAN bots require this. |
| 267 | sk_bzero(tmpPixels.get(), size); |
| 268 | readDst = tmpPixels.get(); |
| 269 | rowBytes = tmpInfo.fRowBytes; |
| 270 | top = flip ? srcSurface->height() - top - height : top; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | direct->priv().flushSurface(srcProxy); |
| 274 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 275 | if (!direct->priv().getGpu()->readPixels(srcSurface, left, top, width, height, |
| 276 | supportedRead.fColorType, readDst, rowBytes)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 277 | return false; |
| 278 | } |
| 279 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 280 | if (convert) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 281 | return GrConvertPixels(dstInfo, buffer, tmpInfo, tmpPixels.get(), supportedRead.fSwizzle); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 282 | } |
| 283 | return true; |
| 284 | } |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 285 | |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 286 | bool GrSurfaceContext::readPixels(const SkImageInfo& dstInfo, void* dstBuffer, |
| 287 | size_t dstRowBytes, int x, int y, uint32_t flags) { |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 288 | ASSERT_SINGLE_OWNER |
| 289 | RETURN_FALSE_IF_ABANDONED |
| 290 | SkDEBUGCODE(this->validate();) |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 291 | GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels"); |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 292 | |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 293 | // TODO: this seems to duplicate code in SkImage_Gpu::onReadPixels |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 294 | if (kUnpremul_SkAlphaType == dstInfo.alphaType() && |
| 295 | !GrPixelConfigIsOpaque(this->asSurfaceProxy()->config())) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 296 | flags |= kUnpremul_PixelOpsFlag; |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 297 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 298 | auto colorType = SkColorTypeToGrColorType(dstInfo.colorType()); |
| 299 | if (GrColorType::kUnknown == colorType) { |
| 300 | return false; |
| 301 | } |
Robert Phillips | 6a6de56 | 2019-02-15 15:19:15 -0500 | [diff] [blame] | 302 | |
| 303 | auto direct = fContext->priv().asDirectContext(); |
| 304 | if (!direct) { |
| 305 | return false; |
| 306 | } |
| 307 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 308 | return this->readPixelsImpl(direct, x, y, dstInfo.width(), dstInfo.height(), colorType, |
| 309 | dstInfo.colorSpace(), dstBuffer, dstRowBytes, flags); |
| 310 | } |
| 311 | |
| 312 | bool GrSurfaceContext::writePixelsImpl(GrContext* direct, int left, int top, int width, int height, |
| 313 | GrColorType srcColorType, SkColorSpace* srcColorSpace, |
| 314 | const void* srcBuffer, size_t srcRowBytes, |
| 315 | uint32_t pixelOpsFlags) { |
| 316 | if (GrColorType::kUnknown == srcColorType) { |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | GrSurfaceProxy* dstProxy = this->asSurfaceProxy(); |
| 321 | if (!dstProxy->instantiate(direct->priv().resourceProvider())) { |
| 322 | return false; |
| 323 | } |
| 324 | |
| 325 | GrSurface* dstSurface = dstProxy->peekSurface(); |
| 326 | |
| 327 | if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(), |
| 328 | GrColorTypeBytesPerPixel(srcColorType), &left, &top, |
| 329 | &width, &height, &srcBuffer, &srcRowBytes)) { |
| 330 | return false; |
| 331 | } |
| 332 | |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 333 | // TODO: Pass src buffer's alpha type. |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 334 | bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags); |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 335 | SkASSERT(!premul || this->colorSpaceInfo().alphaType() != kUnpremul_SkAlphaType); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 336 | |
| 337 | bool needColorConversion = |
| 338 | SkColorSpaceXformSteps::Required(srcColorSpace, this->colorSpaceInfo().colorSpace()); |
| 339 | |
| 340 | const GrCaps* caps = direct->priv().caps(); |
| 341 | // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs |
| 342 | // that are premultiplied on the GPU. This is kept as narrow as possible for now. |
| 343 | bool canvas2DFastPath = |
| 344 | !caps->avoidWritePixelsFastPath() && |
| 345 | premul && |
| 346 | !needColorConversion && |
| 347 | (srcColorType == GrColorType::kRGBA_8888 || srcColorType == GrColorType::kBGRA_8888) && |
| 348 | SkToBool(this->asRenderTargetContext()) && |
| 349 | (dstProxy->config() == kRGBA_8888_GrPixelConfig || |
| 350 | dstProxy->config() == kBGRA_8888_GrPixelConfig) && |
| 351 | direct->priv().caps()->isConfigTexturable(kRGBA_8888_GrPixelConfig) && |
| 352 | direct->priv().validPMUPMConversionExists(); |
| 353 | |
| 354 | if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) { |
| 355 | GrSurfaceDesc desc; |
| 356 | desc.fWidth = width; |
| 357 | desc.fHeight = height; |
| 358 | desc.fSampleCnt = 1; |
| 359 | |
| 360 | GrBackendFormat format; |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 361 | SkAlphaType alphaType; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 362 | if (canvas2DFastPath) { |
| 363 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 364 | format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType); |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 365 | alphaType = kUnpremul_SkAlphaType; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 366 | } else { |
| 367 | desc.fConfig = dstProxy->config(); |
| 368 | format = dstProxy->backendFormat().makeTexture2D(); |
| 369 | if (!format.isValid()) { |
| 370 | return false; |
| 371 | } |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 372 | alphaType = this->colorSpaceInfo().alphaType(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 373 | } |
| 374 | |
Greg Daniel | 2e52ad1 | 2019-06-13 10:04:16 -0400 | [diff] [blame] | 375 | // It is more efficient for us to write pixels into a top left origin so we prefer that. |
| 376 | // However, if the final proxy isn't a render target then we must use a copy to move the |
| 377 | // data into it which requires the origins to match. If the final proxy is a render target |
| 378 | // we can use a draw instead which doesn't have this origin restriction. Thus for render |
| 379 | // 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] | 380 | GrSurfaceOrigin tempOrigin = |
| 381 | this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : dstProxy->origin(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 382 | auto tempProxy = direct->priv().proxyProvider()->createProxy( |
Greg Daniel | 2e52ad1 | 2019-06-13 10:04:16 -0400 | [diff] [blame] | 383 | format, desc, tempOrigin, SkBackingFit::kApprox, SkBudgeted::kYes); |
| 384 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 385 | if (!tempProxy) { |
| 386 | return false; |
| 387 | } |
| 388 | auto tempCtx = direct->priv().drawingManager()->makeTextureContext( |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 389 | tempProxy, alphaType, this->colorSpaceInfo().refColorSpace()); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 390 | if (!tempCtx) { |
| 391 | return false; |
| 392 | } |
| 393 | uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags; |
| 394 | |
| 395 | // In the fast path we always write the srcData to the temp context as though it were RGBA. |
| 396 | // When the data is really BGRA the write will cause the R and B channels to be swapped in |
| 397 | // the intermediate surface which gets corrected by a swizzle effect when drawing to the |
| 398 | // dst. |
| 399 | auto tmpColorType = canvas2DFastPath ? GrColorType::kRGBA_8888 : srcColorType; |
| 400 | if (!tempCtx->writePixelsImpl(direct, 0, 0, width, height, tmpColorType, srcColorSpace, |
| 401 | srcBuffer, srcRowBytes, flags)) { |
| 402 | return false; |
| 403 | } |
| 404 | |
| 405 | if (this->asRenderTargetContext()) { |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 406 | std::unique_ptr<GrFragmentProcessor> fp; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 407 | if (canvas2DFastPath) { |
| 408 | fp = direct->priv().createUPMToPMEffect( |
| 409 | GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I())); |
| 410 | if (srcColorType == GrColorType::kBGRA_8888) { |
| 411 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA()); |
| 412 | } |
| 413 | } else { |
| 414 | fp = GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()); |
| 415 | } |
| 416 | if (!fp) { |
| 417 | return false; |
| 418 | } |
| 419 | GrPaint paint; |
| 420 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 421 | paint.addColorFragmentProcessor(std::move(fp)); |
| 422 | this->asRenderTargetContext()->fillRectToRect( |
| 423 | GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), |
| 424 | SkRect::MakeXYWH(left, top, width, height), SkRect::MakeWH(width, height)); |
| 425 | } else { |
| 426 | SkIRect srcRect = SkIRect::MakeWH(width, height); |
| 427 | SkIPoint dstPoint = SkIPoint::Make(left, top); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 428 | if (!this->copy(tempProxy.get(), srcRect, dstPoint)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 429 | return false; |
| 430 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 431 | } |
| 432 | return true; |
| 433 | } |
| 434 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 435 | if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) { |
| 436 | return false; |
| 437 | } |
| 438 | |
| 439 | GrColorType allowedColorType = caps->supportedWritePixelsColorType(dstProxy->config(), |
| 440 | srcColorType); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 441 | bool convert = premul || needColorConversion || (srcColorType != allowedColorType) || |
| 442 | dstProxy->origin() == kBottomLeft_GrSurfaceOrigin; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 443 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 444 | std::unique_ptr<char[]> tmpPixels; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 445 | if (convert) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 446 | GrPixelInfo srcInfo; |
| 447 | srcInfo.fColorInfo.fAlphaType = (premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType); |
| 448 | srcInfo.fColorInfo.fColorType = srcColorType; |
| 449 | srcInfo.fColorInfo.fColorSpace = srcColorSpace; |
| 450 | srcInfo.fRowBytes = srcRowBytes; |
| 451 | srcInfo.fOrigin = kTopLeft_GrSurfaceOrigin; |
| 452 | |
| 453 | GrPixelInfo tmpInfo; |
| 454 | tmpInfo.fColorInfo.fAlphaType = kPremul_SkAlphaType; |
| 455 | tmpInfo.fColorInfo.fColorType = allowedColorType; |
| 456 | tmpInfo.fColorInfo.fColorSpace = this->colorSpaceInfo().colorSpace(); |
| 457 | tmpInfo.fRowBytes = GrColorTypeBytesPerPixel(allowedColorType) * width; |
| 458 | tmpInfo.fOrigin = dstProxy->origin(); |
| 459 | |
| 460 | srcInfo.fWidth = tmpInfo.fWidth = width; |
| 461 | srcInfo.fHeight = tmpInfo.fHeight = height; |
| 462 | |
| 463 | tmpPixels.reset(new char[tmpInfo.fRowBytes * height]); |
| 464 | |
| 465 | GrConvertPixels(tmpInfo, tmpPixels.get(), srcInfo, srcBuffer); |
| 466 | |
| 467 | srcColorType = tmpInfo.fColorInfo.fColorType; |
| 468 | srcBuffer = tmpPixels.get(); |
| 469 | srcRowBytes = tmpInfo.fRowBytes; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 470 | if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 471 | top = dstSurface->height() - top - height; |
| 472 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a |
| 476 | // complete flush here. On platforms that prefer VRAM use over flushes we're better off |
| 477 | // giving the drawing manager the chance of skipping the flush (i.e., by passing in the |
| 478 | // destination proxy) |
| 479 | // TODO: should this policy decision just be moved into the drawing manager? |
| 480 | direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr); |
| 481 | |
| 482 | return direct->priv().getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType, |
| 483 | srcBuffer, srcRowBytes); |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | bool GrSurfaceContext::writePixels(const SkImageInfo& srcInfo, const void* srcBuffer, |
| 487 | size_t srcRowBytes, int x, int y, uint32_t flags) { |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 488 | ASSERT_SINGLE_OWNER |
| 489 | RETURN_FALSE_IF_ABANDONED |
| 490 | SkDEBUGCODE(this->validate();) |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 491 | GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels"); |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 492 | |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 493 | if (kUnpremul_SkAlphaType == srcInfo.alphaType()) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 494 | flags |= kUnpremul_PixelOpsFlag; |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 495 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 496 | auto colorType = SkColorTypeToGrColorType(srcInfo.colorType()); |
| 497 | if (GrColorType::kUnknown == colorType) { |
| 498 | return false; |
| 499 | } |
Robert Phillips | 6a6de56 | 2019-02-15 15:19:15 -0500 | [diff] [blame] | 500 | |
| 501 | auto direct = fContext->priv().asDirectContext(); |
| 502 | if (!direct) { |
| 503 | return false; |
| 504 | } |
| 505 | |
Greg Daniel | b3f82dd | 2019-05-29 14:24:32 -0400 | [diff] [blame] | 506 | if (this->asSurfaceProxy()->readOnly()) { |
| 507 | return false; |
| 508 | } |
| 509 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 510 | return this->writePixelsImpl(direct, x, y, srcInfo.width(), srcInfo.height(), colorType, |
| 511 | srcInfo.colorSpace(), srcBuffer, srcRowBytes, flags); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 512 | } |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 513 | |
| 514 | bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) { |
| 515 | ASSERT_SINGLE_OWNER |
| 516 | RETURN_FALSE_IF_ABANDONED |
| 517 | SkDEBUGCODE(this->validate();) |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 518 | GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy"); |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 519 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 520 | SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal); |
| 521 | SkASSERT(src->origin() == this->asSurfaceProxy()->origin()); |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 522 | SkASSERT(src->config() == this->asSurfaceProxy()->config()); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 523 | |
| 524 | GrSurfaceProxy* dst = this->asSurfaceProxy(); |
| 525 | |
| 526 | if (!fContext->priv().caps()->canCopySurface(dst, src, srcRect, dstPoint)) { |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 527 | return false; |
| 528 | } |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 529 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 530 | return this->getOpList()->copySurface(fContext, dst, src, srcRect, dstPoint); |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 531 | } |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 532 | |