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 | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 12 | #include "GrRecordingContext.h" |
| 13 | #include "GrRecordingContextPriv.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 | #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());) |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 19 | #define RETURN_FALSE_IF_ABANDONED if (this->fContext->priv().abandoned()) { 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). |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 25 | GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context, |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 26 | GrPixelConfig config, |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 27 | sk_sp<SkColorSpace> colorSpace) |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 28 | : fContext(context) |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 29 | , fColorSpaceInfo(std::move(colorSpace), config) { |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 30 | } |
| 31 | |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 32 | GrAuditTrail* GrSurfaceContext::auditTrail() { |
| 33 | return fContext->priv().auditTrail(); |
| 34 | } |
| 35 | |
| 36 | GrDrawingManager* GrSurfaceContext::drawingManager() { |
| 37 | return fContext->priv().drawingManager(); |
| 38 | } |
| 39 | |
| 40 | const GrDrawingManager* GrSurfaceContext::drawingManager() const { |
| 41 | return fContext->priv().drawingManager(); |
| 42 | } |
| 43 | |
| 44 | #ifdef SK_DEBUG |
| 45 | GrSingleOwner* GrSurfaceContext::singleOwner() { |
| 46 | return fContext->priv().singleOwner(); |
| 47 | } |
| 48 | #endif |
| 49 | |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 50 | bool GrSurfaceContext::readPixels(const SkImageInfo& dstInfo, void* dstBuffer, |
| 51 | size_t dstRowBytes, int x, int y, uint32_t flags) { |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 52 | ASSERT_SINGLE_OWNER |
| 53 | RETURN_FALSE_IF_ABANDONED |
| 54 | SkDEBUGCODE(this->validate();) |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 55 | GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels"); |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 56 | |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 57 | // TODO: this seems to duplicate code in SkImage_Gpu::onReadPixels |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 58 | if (kUnpremul_SkAlphaType == dstInfo.alphaType() && |
| 59 | !GrPixelConfigIsOpaque(this->asSurfaceProxy()->config())) { |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 60 | flags |= GrContextPriv::kUnpremul_PixelOpsFlag; |
| 61 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 62 | auto colorType = SkColorTypeToGrColorType(dstInfo.colorType()); |
| 63 | if (GrColorType::kUnknown == colorType) { |
| 64 | return false; |
| 65 | } |
Robert Phillips | 6a6de56 | 2019-02-15 15:19:15 -0500 | [diff] [blame] | 66 | |
| 67 | auto direct = fContext->priv().asDirectContext(); |
| 68 | if (!direct) { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | return direct->priv().readSurfacePixels(this, x, y, dstInfo.width(), dstInfo.height(), |
| 73 | colorType, dstInfo.colorSpace(), dstBuffer, |
| 74 | dstRowBytes, flags); |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | bool GrSurfaceContext::writePixels(const SkImageInfo& srcInfo, const void* srcBuffer, |
| 78 | size_t srcRowBytes, int x, int y, uint32_t flags) { |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 79 | ASSERT_SINGLE_OWNER |
| 80 | RETURN_FALSE_IF_ABANDONED |
| 81 | SkDEBUGCODE(this->validate();) |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 82 | GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels"); |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 83 | |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 84 | if (kUnpremul_SkAlphaType == srcInfo.alphaType()) { |
| 85 | flags |= GrContextPriv::kUnpremul_PixelOpsFlag; |
| 86 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 87 | auto colorType = SkColorTypeToGrColorType(srcInfo.colorType()); |
| 88 | if (GrColorType::kUnknown == colorType) { |
| 89 | return false; |
| 90 | } |
Robert Phillips | 6a6de56 | 2019-02-15 15:19:15 -0500 | [diff] [blame] | 91 | |
| 92 | auto direct = fContext->priv().asDirectContext(); |
| 93 | if (!direct) { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | return direct->priv().writeSurfacePixels(this, x, y, srcInfo.width(), srcInfo.height(), |
| 98 | colorType, srcInfo.colorSpace(), srcBuffer, |
| 99 | srcRowBytes, flags); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 100 | } |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 101 | |
| 102 | bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) { |
| 103 | ASSERT_SINGLE_OWNER |
| 104 | RETURN_FALSE_IF_ABANDONED |
| 105 | SkDEBUGCODE(this->validate();) |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 106 | GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::copy"); |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 107 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 108 | if (!fContext->priv().caps()->canCopySurface(this->asSurfaceProxy(), src, srcRect, |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 109 | dstPoint)) { |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 110 | return false; |
| 111 | } |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 112 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 113 | return this->getOpList()->copySurface(fContext, this->asSurfaceProxy(), |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 114 | src, srcRect, dstPoint); |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 115 | } |