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" |
| 15 | #include "src/gpu/GrDrawingManager.h" |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrGpu.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 17 | #include "src/gpu/GrOpList.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/gpu/GrRecordingContextPriv.h" |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 19 | #include "src/gpu/GrRenderTargetContext.h" |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrSurfaceContextPriv.h" |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 21 | #include "src/gpu/GrSurfacePriv.h" |
| 22 | #include "src/gpu/GrTextureContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "src/gpu/SkGr.h" |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 24 | |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 25 | #define ASSERT_SINGLE_OWNER \ |
| 26 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());) |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 27 | #define RETURN_FALSE_IF_ABANDONED if (this->fContext->priv().abandoned()) { return false; } |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 28 | |
| 29 | // In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress |
| 30 | // GrOpLists to be picked up and added to by renderTargetContexts lower in the call |
| 31 | // stack. When this occurs with a closed GrOpList, a new one will be allocated |
| 32 | // when the renderTargetContext attempts to use it (via getOpList). |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 33 | GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context, |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 34 | GrPixelConfig config, |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 35 | sk_sp<SkColorSpace> colorSpace) |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 36 | : fContext(context) |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 37 | , fColorSpaceInfo(std::move(colorSpace), config) { |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 38 | } |
| 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; |
| 79 | case GrColorType::kRG_half: 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 | |
| 154 | // TODO: Make GrSurfaceContext know its alpha type and pass dst buffer's alpha type. |
| 155 | bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags); |
| 156 | |
| 157 | if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) { |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | bool needColorConversion = |
| 162 | SkColorSpaceXformSteps::Required(this->colorSpaceInfo().colorSpace(), dstColorSpace); |
| 163 | |
| 164 | const GrCaps* caps = direct->priv().caps(); |
| 165 | // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't |
| 166 | // care so much about getImageData performance. However, in order to ensure putImageData/ |
| 167 | // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary |
| 168 | // unpremul step to writeSurfacePixels's premul step (which is determined empirically in |
| 169 | // fContext->vaildaPMUPMConversionExists()). |
| 170 | bool canvas2DFastPath = |
| 171 | unpremul && |
| 172 | !needColorConversion && |
| 173 | (GrColorType::kRGBA_8888 == dstColorType || GrColorType::kBGRA_8888 == dstColorType) && |
| 174 | SkToBool(srcProxy->asTextureProxy()) && |
| 175 | (srcProxy->config() == kRGBA_8888_GrPixelConfig || |
| 176 | srcProxy->config() == kBGRA_8888_GrPixelConfig) && |
| 177 | caps->isConfigRenderable(kRGBA_8888_GrPixelConfig) && |
| 178 | direct->priv().validPMUPMConversionExists(); |
| 179 | |
| 180 | if (!caps->surfaceSupportsReadPixels(srcSurface) || |
| 181 | canvas2DFastPath) { |
| 182 | GrBackendFormat format; |
| 183 | GrPixelConfig config; |
| 184 | if (canvas2DFastPath) { |
| 185 | config = kRGBA_8888_GrPixelConfig; |
| 186 | format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType); |
| 187 | } else { |
| 188 | config = srcProxy->config(); |
| 189 | format = srcProxy->backendFormat().makeTexture2D(); |
| 190 | if (!format.isValid()) { |
| 191 | return false; |
| 192 | } |
| 193 | } |
| 194 | sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorSpaceInfo().refColorSpace(); |
| 195 | |
| 196 | sk_sp<GrRenderTargetContext> tempCtx = direct->priv().makeDeferredRenderTargetContext( |
| 197 | format, SkBackingFit::kApprox, width, height, config, std::move(cs), 1, |
| 198 | GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin, nullptr, SkBudgeted::kYes); |
| 199 | if (!tempCtx) { |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | std::unique_ptr<GrFragmentProcessor> fp; |
| 204 | if (canvas2DFastPath) { |
| 205 | fp = direct->priv().createPMToUPMEffect( |
| 206 | GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()), |
| 207 | SkMatrix::I())); |
| 208 | if (dstColorType == GrColorType::kBGRA_8888) { |
| 209 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA()); |
| 210 | dstColorType = GrColorType::kRGBA_8888; |
| 211 | } |
| 212 | } else { |
| 213 | fp = GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()), SkMatrix::I()); |
| 214 | } |
| 215 | if (!fp) { |
| 216 | return false; |
| 217 | } |
| 218 | GrPaint paint; |
| 219 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 220 | paint.addColorFragmentProcessor(std::move(fp)); |
| 221 | |
| 222 | tempCtx->asRenderTargetContext()->fillRectToRect( |
| 223 | GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), |
| 224 | SkRect::MakeWH(width, height), SkRect::MakeXYWH(left, top, width, height)); |
| 225 | |
| 226 | uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags; |
| 227 | return tempCtx->readPixelsImpl(direct, 0, 0, width, height, dstColorType, dstColorSpace, |
| 228 | buffer, rowBytes, flags); |
| 229 | } |
| 230 | |
Robert Phillips | c34d993 | 2019-06-18 11:57:12 +0000 | [diff] [blame] | 231 | bool convert = unpremul || needColorConversion; |
| 232 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 233 | bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin; |
Robert Phillips | c34d993 | 2019-06-18 11:57:12 +0000 | [diff] [blame] | 234 | if (flip) { |
| 235 | top = srcSurface->height() - top - height; |
| 236 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 237 | |
Robert Phillips | c34d993 | 2019-06-18 11:57:12 +0000 | [diff] [blame] | 238 | GrColorType allowedColorType = caps->supportedReadPixelsColorType(srcProxy->config(), |
| 239 | dstColorType); |
| 240 | convert = convert || (dstColorType != allowedColorType); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 241 | |
Robert Phillips | c34d993 | 2019-06-18 11:57:12 +0000 | [diff] [blame] | 242 | SkAutoPixmapStorage tempPixmap; |
| 243 | SkPixmap finalPixmap; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 244 | if (convert) { |
Robert Phillips | c34d993 | 2019-06-18 11:57:12 +0000 | [diff] [blame] | 245 | SkColorType srcSkColorType = GrColorTypeToSkColorType(allowedColorType); |
| 246 | SkColorType dstSkColorType = GrColorTypeToSkColorType(dstColorType); |
| 247 | bool srcAlwaysOpaque = SkColorTypeIsAlwaysOpaque(srcSkColorType); |
| 248 | bool dstAlwaysOpaque = SkColorTypeIsAlwaysOpaque(dstSkColorType); |
| 249 | if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) { |
| 250 | return false; |
| 251 | } |
| 252 | auto tempAT = srcAlwaysOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
| 253 | auto tempII = SkImageInfo::Make(width, height, srcSkColorType, tempAT, |
| 254 | this->colorSpaceInfo().refColorSpace()); |
| 255 | SkASSERT(!unpremul || !dstAlwaysOpaque); |
| 256 | auto finalAT = (srcAlwaysOpaque || dstAlwaysOpaque) |
| 257 | ? kOpaque_SkAlphaType |
| 258 | : unpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType; |
| 259 | auto finalII = |
| 260 | SkImageInfo::Make(width, height, dstSkColorType, finalAT, sk_ref_sp(dstColorSpace)); |
| 261 | if (!SkImageInfoValidConversion(finalII, tempII)) { |
| 262 | return false; |
| 263 | } |
| 264 | if (!tempPixmap.tryAlloc(tempII)) { |
| 265 | return false; |
| 266 | } |
| 267 | finalPixmap.reset(finalII, buffer, rowBytes); |
| 268 | buffer = tempPixmap.writable_addr(); |
| 269 | rowBytes = tempPixmap.rowBytes(); |
| 270 | // Chrome msan bots require this. |
| 271 | sk_bzero(buffer, tempPixmap.computeByteSize()); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | direct->priv().flushSurface(srcProxy); |
| 275 | |
Robert Phillips | c34d993 | 2019-06-18 11:57:12 +0000 | [diff] [blame] | 276 | if (!direct->priv().getGpu()->readPixels(srcSurface, left, top, width, height, allowedColorType, |
| 277 | buffer, rowBytes)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 278 | return false; |
| 279 | } |
| 280 | |
Robert Phillips | c34d993 | 2019-06-18 11:57:12 +0000 | [diff] [blame] | 281 | if (flip) { |
| 282 | size_t trimRowBytes = GrColorTypeBytesPerPixel(allowedColorType) * width; |
| 283 | std::unique_ptr<char[]> row(new char[trimRowBytes]); |
| 284 | char* upper = reinterpret_cast<char*>(buffer); |
| 285 | char* lower = reinterpret_cast<char*>(buffer) + (height - 1) * rowBytes; |
| 286 | for (int y = 0; y < height / 2; ++y, upper += rowBytes, lower -= rowBytes) { |
| 287 | memcpy(row.get(), upper, trimRowBytes); |
| 288 | memcpy(upper, lower, trimRowBytes); |
| 289 | memcpy(lower, row.get(), trimRowBytes); |
| 290 | } |
| 291 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 292 | if (convert) { |
Robert Phillips | c34d993 | 2019-06-18 11:57:12 +0000 | [diff] [blame] | 293 | if (!tempPixmap.readPixels(finalPixmap)) { |
| 294 | return false; |
| 295 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 296 | } |
| 297 | return true; |
| 298 | } |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 299 | |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 300 | bool GrSurfaceContext::readPixels(const SkImageInfo& dstInfo, void* dstBuffer, |
| 301 | size_t dstRowBytes, int x, int y, uint32_t flags) { |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 302 | ASSERT_SINGLE_OWNER |
| 303 | RETURN_FALSE_IF_ABANDONED |
| 304 | SkDEBUGCODE(this->validate();) |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 305 | GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels"); |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 306 | |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 307 | // TODO: this seems to duplicate code in SkImage_Gpu::onReadPixels |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 308 | if (kUnpremul_SkAlphaType == dstInfo.alphaType() && |
| 309 | !GrPixelConfigIsOpaque(this->asSurfaceProxy()->config())) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 310 | flags |= kUnpremul_PixelOpsFlag; |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 311 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 312 | auto colorType = SkColorTypeToGrColorType(dstInfo.colorType()); |
| 313 | if (GrColorType::kUnknown == colorType) { |
| 314 | return false; |
| 315 | } |
Robert Phillips | 6a6de56 | 2019-02-15 15:19:15 -0500 | [diff] [blame] | 316 | |
| 317 | auto direct = fContext->priv().asDirectContext(); |
| 318 | if (!direct) { |
| 319 | return false; |
| 320 | } |
| 321 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 322 | return this->readPixelsImpl(direct, x, y, dstInfo.width(), dstInfo.height(), colorType, |
| 323 | dstInfo.colorSpace(), dstBuffer, dstRowBytes, flags); |
| 324 | } |
| 325 | |
| 326 | bool GrSurfaceContext::writePixelsImpl(GrContext* direct, int left, int top, int width, int height, |
| 327 | GrColorType srcColorType, SkColorSpace* srcColorSpace, |
| 328 | const void* srcBuffer, size_t srcRowBytes, |
| 329 | uint32_t pixelOpsFlags) { |
| 330 | if (GrColorType::kUnknown == srcColorType) { |
| 331 | return false; |
| 332 | } |
| 333 | |
| 334 | GrSurfaceProxy* dstProxy = this->asSurfaceProxy(); |
| 335 | if (!dstProxy->instantiate(direct->priv().resourceProvider())) { |
| 336 | return false; |
| 337 | } |
| 338 | |
| 339 | GrSurface* dstSurface = dstProxy->peekSurface(); |
| 340 | |
| 341 | if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(), |
| 342 | GrColorTypeBytesPerPixel(srcColorType), &left, &top, |
| 343 | &width, &height, &srcBuffer, &srcRowBytes)) { |
| 344 | return false; |
| 345 | } |
| 346 | |
| 347 | // TODO: Make GrSurfaceContext know its alpha type and pass src buffer's alpha type. |
| 348 | bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags); |
| 349 | |
| 350 | bool needColorConversion = |
| 351 | SkColorSpaceXformSteps::Required(srcColorSpace, this->colorSpaceInfo().colorSpace()); |
| 352 | |
| 353 | const GrCaps* caps = direct->priv().caps(); |
| 354 | // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs |
| 355 | // that are premultiplied on the GPU. This is kept as narrow as possible for now. |
| 356 | bool canvas2DFastPath = |
| 357 | !caps->avoidWritePixelsFastPath() && |
| 358 | premul && |
| 359 | !needColorConversion && |
| 360 | (srcColorType == GrColorType::kRGBA_8888 || srcColorType == GrColorType::kBGRA_8888) && |
| 361 | SkToBool(this->asRenderTargetContext()) && |
| 362 | (dstProxy->config() == kRGBA_8888_GrPixelConfig || |
| 363 | dstProxy->config() == kBGRA_8888_GrPixelConfig) && |
| 364 | direct->priv().caps()->isConfigTexturable(kRGBA_8888_GrPixelConfig) && |
| 365 | direct->priv().validPMUPMConversionExists(); |
| 366 | |
| 367 | if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) { |
| 368 | GrSurfaceDesc desc; |
| 369 | desc.fWidth = width; |
| 370 | desc.fHeight = height; |
| 371 | desc.fSampleCnt = 1; |
| 372 | |
| 373 | GrBackendFormat format; |
| 374 | if (canvas2DFastPath) { |
| 375 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 376 | format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType); |
| 377 | } else { |
| 378 | desc.fConfig = dstProxy->config(); |
| 379 | format = dstProxy->backendFormat().makeTexture2D(); |
| 380 | if (!format.isValid()) { |
| 381 | return false; |
| 382 | } |
| 383 | } |
| 384 | |
Greg Daniel | 2e52ad1 | 2019-06-13 10:04:16 -0400 | [diff] [blame] | 385 | // It is more efficient for us to write pixels into a top left origin so we prefer that. |
| 386 | // However, if the final proxy isn't a render target then we must use a copy to move the |
| 387 | // data into it which requires the origins to match. If the final proxy is a render target |
| 388 | // we can use a draw instead which doesn't have this origin restriction. Thus for render |
| 389 | // targets we will use top left and otherwise we will make the origins match. |
Robert Phillips | c34d993 | 2019-06-18 11:57:12 +0000 | [diff] [blame] | 390 | GrSurfaceOrigin tempOrigin = this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : |
| 391 | dstProxy->origin(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 392 | auto tempProxy = direct->priv().proxyProvider()->createProxy( |
Greg Daniel | 2e52ad1 | 2019-06-13 10:04:16 -0400 | [diff] [blame] | 393 | format, desc, tempOrigin, SkBackingFit::kApprox, SkBudgeted::kYes); |
| 394 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 395 | if (!tempProxy) { |
| 396 | return false; |
| 397 | } |
| 398 | auto tempCtx = direct->priv().drawingManager()->makeTextureContext( |
| 399 | tempProxy, this->colorSpaceInfo().refColorSpace()); |
| 400 | if (!tempCtx) { |
| 401 | return false; |
| 402 | } |
| 403 | uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags; |
| 404 | |
| 405 | // In the fast path we always write the srcData to the temp context as though it were RGBA. |
| 406 | // When the data is really BGRA the write will cause the R and B channels to be swapped in |
| 407 | // the intermediate surface which gets corrected by a swizzle effect when drawing to the |
| 408 | // dst. |
| 409 | auto tmpColorType = canvas2DFastPath ? GrColorType::kRGBA_8888 : srcColorType; |
| 410 | if (!tempCtx->writePixelsImpl(direct, 0, 0, width, height, tmpColorType, srcColorSpace, |
| 411 | srcBuffer, srcRowBytes, flags)) { |
| 412 | return false; |
| 413 | } |
| 414 | |
| 415 | if (this->asRenderTargetContext()) { |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 416 | std::unique_ptr<GrFragmentProcessor> fp; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 417 | if (canvas2DFastPath) { |
| 418 | fp = direct->priv().createUPMToPMEffect( |
| 419 | GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I())); |
| 420 | if (srcColorType == GrColorType::kBGRA_8888) { |
| 421 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA()); |
| 422 | } |
| 423 | } else { |
| 424 | fp = GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()); |
| 425 | } |
| 426 | if (!fp) { |
| 427 | return false; |
| 428 | } |
| 429 | GrPaint paint; |
| 430 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 431 | paint.addColorFragmentProcessor(std::move(fp)); |
| 432 | this->asRenderTargetContext()->fillRectToRect( |
| 433 | GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), |
| 434 | SkRect::MakeXYWH(left, top, width, height), SkRect::MakeWH(width, height)); |
| 435 | } else { |
| 436 | SkIRect srcRect = SkIRect::MakeWH(width, height); |
| 437 | SkIPoint dstPoint = SkIPoint::Make(left, top); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 438 | if (!this->copy(tempProxy.get(), srcRect, dstPoint)) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 439 | return false; |
| 440 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 441 | } |
| 442 | return true; |
| 443 | } |
| 444 | |
Robert Phillips | c34d993 | 2019-06-18 11:57:12 +0000 | [diff] [blame] | 445 | bool convert = premul || needColorConversion; |
| 446 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 447 | if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) { |
| 448 | return false; |
| 449 | } |
| 450 | |
| 451 | GrColorType allowedColorType = caps->supportedWritePixelsColorType(dstProxy->config(), |
| 452 | srcColorType); |
Robert Phillips | c34d993 | 2019-06-18 11:57:12 +0000 | [diff] [blame] | 453 | convert = convert || (srcColorType != allowedColorType); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 454 | |
Robert Phillips | c34d993 | 2019-06-18 11:57:12 +0000 | [diff] [blame] | 455 | std::unique_ptr<char[]> tempBuffer; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 456 | if (convert) { |
Robert Phillips | c34d993 | 2019-06-18 11:57:12 +0000 | [diff] [blame] | 457 | auto srcSkColorType = GrColorTypeToSkColorType(srcColorType); |
| 458 | auto dstSkColorType = GrColorTypeToSkColorType(allowedColorType); |
| 459 | if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) { |
| 460 | return false; |
| 461 | } |
| 462 | auto srcAlphaType = SkColorTypeIsAlwaysOpaque(srcSkColorType) |
| 463 | ? kOpaque_SkAlphaType |
| 464 | : (premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType); |
| 465 | SkPixmap src(SkImageInfo::Make(width, height, srcSkColorType, srcAlphaType, |
| 466 | sk_ref_sp(srcColorSpace)), |
| 467 | srcBuffer, srcRowBytes); |
| 468 | auto tempSrcII = SkImageInfo::Make(width, height, dstSkColorType, kPremul_SkAlphaType, |
| 469 | this->colorSpaceInfo().refColorSpace()); |
| 470 | auto size = tempSrcII.computeMinByteSize(); |
| 471 | if (!size) { |
| 472 | return false; |
| 473 | } |
| 474 | tempBuffer.reset(new char[size]); |
| 475 | SkPixmap tempSrc(tempSrcII, tempBuffer.get(), tempSrcII.minRowBytes()); |
| 476 | if (!src.readPixels(tempSrc)) { |
| 477 | return false; |
| 478 | } |
| 479 | srcColorType = allowedColorType; |
| 480 | srcBuffer = tempSrc.addr(); |
| 481 | srcRowBytes = tempSrc.rowBytes(); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 482 | if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) { |
Robert Phillips | c34d993 | 2019-06-18 11:57:12 +0000 | [diff] [blame] | 483 | std::unique_ptr<char[]> row(new char[srcRowBytes]); |
| 484 | for (int y = 0; y < height / 2; ++y) { |
| 485 | memcpy(row.get(), tempSrc.addr(0, y), srcRowBytes); |
| 486 | memcpy(tempSrc.writable_addr(0, y), tempSrc.addr(0, height - 1 - y), srcRowBytes); |
| 487 | memcpy(tempSrc.writable_addr(0, height - 1 - y), row.get(), srcRowBytes); |
| 488 | } |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 489 | top = dstSurface->height() - top - height; |
| 490 | } |
Robert Phillips | c34d993 | 2019-06-18 11:57:12 +0000 | [diff] [blame] | 491 | } else if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) { |
| 492 | size_t trimRowBytes = GrColorTypeBytesPerPixel(srcColorType) * width; |
| 493 | tempBuffer.reset(new char[trimRowBytes * height]); |
| 494 | char* dst = reinterpret_cast<char*>(tempBuffer.get()) + trimRowBytes * (height - 1); |
| 495 | const char* src = reinterpret_cast<const char*>(srcBuffer); |
| 496 | for (int i = 0; i < height; ++i, src += srcRowBytes, dst -= trimRowBytes) { |
| 497 | memcpy(dst, src, trimRowBytes); |
| 498 | } |
| 499 | srcBuffer = tempBuffer.get(); |
| 500 | srcRowBytes = trimRowBytes; |
| 501 | top = dstSurface->height() - top - height; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a |
| 505 | // complete flush here. On platforms that prefer VRAM use over flushes we're better off |
| 506 | // giving the drawing manager the chance of skipping the flush (i.e., by passing in the |
| 507 | // destination proxy) |
| 508 | // TODO: should this policy decision just be moved into the drawing manager? |
| 509 | direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr); |
| 510 | |
| 511 | return direct->priv().getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType, |
| 512 | srcBuffer, srcRowBytes); |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | bool GrSurfaceContext::writePixels(const SkImageInfo& srcInfo, const void* srcBuffer, |
| 516 | size_t srcRowBytes, int x, int y, uint32_t flags) { |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 517 | ASSERT_SINGLE_OWNER |
| 518 | RETURN_FALSE_IF_ABANDONED |
| 519 | SkDEBUGCODE(this->validate();) |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 520 | GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels"); |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 521 | |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 522 | if (kUnpremul_SkAlphaType == srcInfo.alphaType()) { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 523 | flags |= kUnpremul_PixelOpsFlag; |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 524 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 525 | auto colorType = SkColorTypeToGrColorType(srcInfo.colorType()); |
| 526 | if (GrColorType::kUnknown == colorType) { |
| 527 | return false; |
| 528 | } |
Robert Phillips | 6a6de56 | 2019-02-15 15:19:15 -0500 | [diff] [blame] | 529 | |
| 530 | auto direct = fContext->priv().asDirectContext(); |
| 531 | if (!direct) { |
| 532 | return false; |
| 533 | } |
| 534 | |
Greg Daniel | b3f82dd | 2019-05-29 14:24:32 -0400 | [diff] [blame] | 535 | if (this->asSurfaceProxy()->readOnly()) { |
| 536 | return false; |
| 537 | } |
| 538 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 539 | return this->writePixelsImpl(direct, x, y, srcInfo.width(), srcInfo.height(), colorType, |
| 540 | srcInfo.colorSpace(), srcBuffer, srcRowBytes, flags); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 541 | } |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 542 | |
| 543 | bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) { |
| 544 | ASSERT_SINGLE_OWNER |
| 545 | RETURN_FALSE_IF_ABANDONED |
| 546 | SkDEBUGCODE(this->validate();) |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 547 | GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy"); |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 548 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 549 | SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal); |
| 550 | SkASSERT(src->origin() == this->asSurfaceProxy()->origin()); |
| 551 | |
| 552 | GrSurfaceProxy* dst = this->asSurfaceProxy(); |
| 553 | |
| 554 | if (!fContext->priv().caps()->canCopySurface(dst, src, srcRect, dstPoint)) { |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 555 | return false; |
| 556 | } |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 557 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 558 | return this->getOpList()->copySurface(fContext, dst, src, srcRect, dstPoint); |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 559 | } |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 560 | |