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