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