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