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