bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/gpu/GrRenderTarget.h" |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/gpu/GrContext.h" |
| 12 | #include "src/core/SkRectPriv.h" |
| 13 | #include "src/gpu/GrContextPriv.h" |
| 14 | #include "src/gpu/GrGpu.h" |
| 15 | #include "src/gpu/GrRenderTargetContext.h" |
| 16 | #include "src/gpu/GrRenderTargetOpList.h" |
| 17 | #include "src/gpu/GrRenderTargetPriv.h" |
| 18 | #include "src/gpu/GrSamplePatternDictionary.h" |
| 19 | #include "src/gpu/GrStencilAttachment.h" |
| 20 | #include "src/gpu/GrStencilSettings.h" |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 21 | |
Robert Phillips | c4f0a82 | 2017-06-13 08:11:36 -0400 | [diff] [blame] | 22 | GrRenderTarget::GrRenderTarget(GrGpu* gpu, const GrSurfaceDesc& desc, |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 23 | GrStencilAttachment* stencil) |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 24 | : INHERITED(gpu, desc) |
| 25 | , fSampleCnt(desc.fSampleCnt) |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 26 | , fSamplePatternKey(GrSamplePatternDictionary::kInvalidSamplePatternKey) |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 27 | , fStencilAttachment(stencil) { |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 28 | SkASSERT(desc.fFlags & kRenderTarget_GrSurfaceFlag); |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 29 | fResolveRect = SkRectPriv::MakeILargestInverted(); |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 32 | GrRenderTarget::~GrRenderTarget() = default; |
| 33 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 34 | void GrRenderTarget::flagAsNeedingResolve(const SkIRect* rect) { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 35 | if (kCanResolve_ResolveType == getResolveType()) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 36 | if (rect) { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 37 | fResolveRect.join(*rect); |
| 38 | if (!fResolveRect.intersect(0, 0, this->width(), this->height())) { |
| 39 | fResolveRect.setEmpty(); |
| 40 | } |
| 41 | } else { |
| 42 | fResolveRect.setLTRB(0, 0, this->width(), this->height()); |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 47 | void GrRenderTarget::overrideResolveRect(const SkIRect rect) { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 48 | fResolveRect = rect; |
| 49 | if (fResolveRect.isEmpty()) { |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 50 | fResolveRect = SkRectPriv::MakeILargestInverted(); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 51 | } else { |
| 52 | if (!fResolveRect.intersect(0, 0, this->width(), this->height())) { |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 53 | fResolveRect = SkRectPriv::MakeILargestInverted(); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 58 | void GrRenderTarget::flagAsResolved() { |
| 59 | fResolveRect = SkRectPriv::MakeILargestInverted(); |
| 60 | } |
| 61 | |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 62 | void GrRenderTarget::onRelease() { |
Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 63 | fStencilAttachment = nullptr; |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 64 | |
| 65 | INHERITED::onRelease(); |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void GrRenderTarget::onAbandon() { |
Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 69 | fStencilAttachment = nullptr; |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 70 | |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 71 | INHERITED::onAbandon(); |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 72 | } |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 73 | |
| 74 | /////////////////////////////////////////////////////////////////////////////// |
| 75 | |
Greg Daniel | cfa3935 | 2018-10-05 12:01:59 -0400 | [diff] [blame] | 76 | void GrRenderTargetPriv::attachStencilAttachment(sk_sp<GrStencilAttachment> stencil) { |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame^] | 77 | #ifdef SK_DEBUG |
| 78 | if (1 == fRenderTarget->fSampleCnt) { |
| 79 | // TODO: We don't expect a mixed sampled render target to ever change its stencil buffer |
| 80 | // right now. But if it does swap in a stencil buffer with a different number of samples, |
| 81 | // and if we have a valid fSamplePatternKey, we will need to invalidate fSamplePatternKey |
| 82 | // here and add tests to make sure we it properly. |
| 83 | SkASSERT(GrSamplePatternDictionary::kInvalidSamplePatternKey == |
| 84 | fRenderTarget->fSamplePatternKey); |
| 85 | } else { |
| 86 | // Render targets with >1 color sample should never use mixed samples. (This would lead to |
| 87 | // different sample patterns, depending on stencil state.) |
| 88 | SkASSERT(!stencil || stencil->numSamples() == fRenderTarget->fSampleCnt); |
| 89 | } |
| 90 | #endif |
| 91 | |
egdaniel | 79bd2ae | 2015-09-15 08:46:13 -0700 | [diff] [blame] | 92 | if (!stencil && !fRenderTarget->fStencilAttachment) { |
| 93 | // No need to do any work since we currently don't have a stencil attachment and |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 94 | // we're not actually adding one. |
Greg Daniel | cfa3935 | 2018-10-05 12:01:59 -0400 | [diff] [blame] | 95 | return; |
egdaniel | 79bd2ae | 2015-09-15 08:46:13 -0700 | [diff] [blame] | 96 | } |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame^] | 97 | |
Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 98 | fRenderTarget->fStencilAttachment = std::move(stencil); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 99 | if (!fRenderTarget->completeStencilAttachment()) { |
Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 100 | fRenderTarget->fStencilAttachment = nullptr; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 101 | } |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 102 | } |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 103 | |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 104 | int GrRenderTargetPriv::numStencilBits() const { |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 105 | SkASSERT(this->getStencilAttachment()); |
| 106 | return this->getStencilAttachment()->bits(); |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 107 | } |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 108 | |
Chris Dalton | 8c4cafd | 2019-04-15 19:14:36 -0600 | [diff] [blame] | 109 | int GrRenderTargetPriv::getSamplePatternKey() const { |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame^] | 110 | #ifdef SK_DEBUG |
| 111 | GrStencilAttachment* stencil = fRenderTarget->fStencilAttachment.get(); |
| 112 | if (fRenderTarget->fSampleCnt <= 1) { |
| 113 | // If the color buffer is not multisampled, the sample pattern better come from the stencil |
| 114 | // buffer (mixed samples). |
| 115 | SkASSERT(stencil && stencil->numSamples() > 1); |
| 116 | } else { |
| 117 | // The color sample count and stencil count cannot both be unequal and both greater than |
| 118 | // one. If this were the case, there would be more than one sample pattern associated with |
| 119 | // the render target. |
| 120 | SkASSERT(!stencil || stencil->numSamples() == fRenderTarget->fSampleCnt); |
| 121 | } |
| 122 | #endif |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 123 | if (GrSamplePatternDictionary::kInvalidSamplePatternKey == fRenderTarget->fSamplePatternKey) { |
| 124 | fRenderTarget->fSamplePatternKey = |
Chris Dalton | 8c4cafd | 2019-04-15 19:14:36 -0600 | [diff] [blame] | 125 | fRenderTarget->getGpu()->findOrAssignSamplePatternKey(fRenderTarget); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 126 | } |
| 127 | SkASSERT(GrSamplePatternDictionary::kInvalidSamplePatternKey |
| 128 | != fRenderTarget->fSamplePatternKey); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 129 | return fRenderTarget->fSamplePatternKey; |
| 130 | } |