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