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 | |
| 8 | #include "GrSurfaceContext.h" |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 9 | |
| 10 | #include "GrContextPriv.h" |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 11 | #include "GrDrawingManager.h" |
| 12 | #include "GrOpList.h" |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 13 | #include "SkGr.h" |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 14 | |
| 15 | #include "../private/GrAuditTrail.h" |
| 16 | |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 17 | #define ASSERT_SINGLE_OWNER \ |
| 18 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());) |
| 19 | #define RETURN_FALSE_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return false; } |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 20 | |
| 21 | // In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress |
| 22 | // GrOpLists to be picked up and added to by renderTargetContexts lower in the call |
| 23 | // stack. When this occurs with a closed GrOpList, a new one will be allocated |
| 24 | // when the renderTargetContext attempts to use it (via getOpList). |
| 25 | GrSurfaceContext::GrSurfaceContext(GrContext* context, |
Robert Phillips | 7215283 | 2017-01-25 17:31:35 -0500 | [diff] [blame] | 26 | GrDrawingManager* drawingMgr, |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 27 | GrPixelConfig config, |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 28 | sk_sp<SkColorSpace> colorSpace, |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 29 | GrAuditTrail* auditTrail, |
| 30 | GrSingleOwner* singleOwner) |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 31 | : fContext(context) |
| 32 | , fAuditTrail(auditTrail) |
| 33 | , fColorSpaceInfo(std::move(colorSpace), config) |
| 34 | , fDrawingManager(drawingMgr) |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 35 | #ifdef SK_DEBUG |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 36 | , fSingleOwner(singleOwner) |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 37 | #endif |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 38 | { |
Brian Salomon | 366093f | 2018-02-13 09:25:22 -0500 | [diff] [blame] | 39 | // We never should have a sRGB pixel config with a non-SRGB gamma color space. |
| 40 | SkASSERT(!GrPixelConfigIsSRGB(config) || |
| 41 | (fColorSpaceInfo.colorSpace() && fColorSpaceInfo.colorSpace()->gammaCloseToSRGB())); |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | bool GrSurfaceContext::readPixels(const SkImageInfo& dstInfo, void* dstBuffer, |
| 45 | size_t dstRowBytes, int x, int y, uint32_t flags) { |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 46 | ASSERT_SINGLE_OWNER |
| 47 | RETURN_FALSE_IF_ABANDONED |
| 48 | SkDEBUGCODE(this->validate();) |
| 49 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrSurfaceContext::readPixels"); |
| 50 | |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 51 | // TODO: this seems to duplicate code in SkImage_Gpu::onReadPixels |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 52 | if (kUnpremul_SkAlphaType == dstInfo.alphaType() && |
| 53 | !GrPixelConfigIsOpaque(this->asSurfaceProxy()->config())) { |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 54 | flags |= GrContextPriv::kUnpremul_PixelOpsFlag; |
| 55 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 56 | auto colorType = SkColorTypeToGrColorType(dstInfo.colorType()); |
| 57 | if (GrColorType::kUnknown == colorType) { |
| 58 | return false; |
| 59 | } |
| 60 | return fContext->contextPriv().readSurfacePixels(this, x, y, dstInfo.width(), dstInfo.height(), |
| 61 | colorType, dstInfo.colorSpace(), dstBuffer, |
| 62 | dstRowBytes, flags); |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | bool GrSurfaceContext::writePixels(const SkImageInfo& srcInfo, const void* srcBuffer, |
| 66 | size_t srcRowBytes, int x, int y, uint32_t flags) { |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 67 | ASSERT_SINGLE_OWNER |
| 68 | RETURN_FALSE_IF_ABANDONED |
| 69 | SkDEBUGCODE(this->validate();) |
| 70 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrSurfaceContext::writePixels"); |
| 71 | |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 72 | if (kUnpremul_SkAlphaType == srcInfo.alphaType()) { |
| 73 | flags |= GrContextPriv::kUnpremul_PixelOpsFlag; |
| 74 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 75 | auto colorType = SkColorTypeToGrColorType(srcInfo.colorType()); |
| 76 | if (GrColorType::kUnknown == colorType) { |
| 77 | return false; |
| 78 | } |
| 79 | return fContext->contextPriv().writeSurfacePixels(this, x, y, srcInfo.width(), srcInfo.height(), |
| 80 | colorType, srcInfo.colorSpace(), srcBuffer, |
| 81 | srcRowBytes, flags); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 82 | } |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 83 | |
| 84 | bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) { |
| 85 | ASSERT_SINGLE_OWNER |
| 86 | RETURN_FALSE_IF_ABANDONED |
| 87 | SkDEBUGCODE(this->validate();) |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame^] | 88 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrSurfaceContext::copy"); |
| 89 | |
| 90 | if (!fContext->caps()->canCopySurface(this->asSurfaceProxy(), src, srcRect, dstPoint)) { |
| 91 | return false; |
| 92 | } |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 93 | |
| 94 | return this->getOpList()->copySurface(*fContext->caps(), |
| 95 | this->asSurfaceProxy(), src, srcRect, dstPoint); |
| 96 | } |