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 | |
Brian Salomon | a9c2257 | 2019-08-05 12:57:09 -0400 | [diff] [blame^] | 22 | GrRenderTarget::GrRenderTarget(GrGpu* gpu, const SkISize& size, GrPixelConfig config, |
| 23 | int sampleCount, GrProtected isProtected, |
| 24 | GrStencilAttachment* stencil) |
| 25 | : INHERITED(gpu, size, config, isProtected) |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 26 | , fSampleCnt(sampleCount) |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 27 | , fSamplePatternKey(GrSamplePatternDictionary::kInvalidSamplePatternKey) |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 28 | , fStencilAttachment(stencil) { |
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 | |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 47 | void GrRenderTarget::flagAsResolved() { |
| 48 | fResolveRect = SkRectPriv::MakeILargestInverted(); |
| 49 | } |
| 50 | |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 51 | void GrRenderTarget::onRelease() { |
Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 52 | fStencilAttachment = nullptr; |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 53 | |
| 54 | INHERITED::onRelease(); |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void GrRenderTarget::onAbandon() { |
Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 58 | fStencilAttachment = nullptr; |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 59 | |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 60 | INHERITED::onAbandon(); |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 61 | } |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 62 | |
| 63 | /////////////////////////////////////////////////////////////////////////////// |
| 64 | |
Greg Daniel | cfa3935 | 2018-10-05 12:01:59 -0400 | [diff] [blame] | 65 | void GrRenderTargetPriv::attachStencilAttachment(sk_sp<GrStencilAttachment> stencil) { |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 66 | #ifdef SK_DEBUG |
| 67 | if (1 == fRenderTarget->fSampleCnt) { |
| 68 | // TODO: We don't expect a mixed sampled render target to ever change its stencil buffer |
| 69 | // right now. But if it does swap in a stencil buffer with a different number of samples, |
| 70 | // and if we have a valid fSamplePatternKey, we will need to invalidate fSamplePatternKey |
| 71 | // here and add tests to make sure we it properly. |
| 72 | SkASSERT(GrSamplePatternDictionary::kInvalidSamplePatternKey == |
| 73 | fRenderTarget->fSamplePatternKey); |
| 74 | } else { |
| 75 | // Render targets with >1 color sample should never use mixed samples. (This would lead to |
| 76 | // different sample patterns, depending on stencil state.) |
| 77 | SkASSERT(!stencil || stencil->numSamples() == fRenderTarget->fSampleCnt); |
| 78 | } |
| 79 | #endif |
| 80 | |
egdaniel | 79bd2ae | 2015-09-15 08:46:13 -0700 | [diff] [blame] | 81 | if (!stencil && !fRenderTarget->fStencilAttachment) { |
| 82 | // 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] | 83 | // we're not actually adding one. |
Greg Daniel | cfa3935 | 2018-10-05 12:01:59 -0400 | [diff] [blame] | 84 | return; |
egdaniel | 79bd2ae | 2015-09-15 08:46:13 -0700 | [diff] [blame] | 85 | } |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 86 | |
Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 87 | fRenderTarget->fStencilAttachment = std::move(stencil); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 88 | if (!fRenderTarget->completeStencilAttachment()) { |
Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 89 | fRenderTarget->fStencilAttachment = nullptr; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 90 | } |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 91 | } |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 92 | |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 93 | int GrRenderTargetPriv::numStencilBits() const { |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 94 | SkASSERT(this->getStencilAttachment()); |
| 95 | return this->getStencilAttachment()->bits(); |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 96 | } |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 97 | |
Chris Dalton | 8c4cafd | 2019-04-15 19:14:36 -0600 | [diff] [blame] | 98 | int GrRenderTargetPriv::getSamplePatternKey() const { |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 99 | #ifdef SK_DEBUG |
| 100 | GrStencilAttachment* stencil = fRenderTarget->fStencilAttachment.get(); |
| 101 | if (fRenderTarget->fSampleCnt <= 1) { |
| 102 | // If the color buffer is not multisampled, the sample pattern better come from the stencil |
| 103 | // buffer (mixed samples). |
| 104 | SkASSERT(stencil && stencil->numSamples() > 1); |
| 105 | } else { |
| 106 | // The color sample count and stencil count cannot both be unequal and both greater than |
| 107 | // one. If this were the case, there would be more than one sample pattern associated with |
| 108 | // the render target. |
| 109 | SkASSERT(!stencil || stencil->numSamples() == fRenderTarget->fSampleCnt); |
| 110 | } |
| 111 | #endif |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 112 | if (GrSamplePatternDictionary::kInvalidSamplePatternKey == fRenderTarget->fSamplePatternKey) { |
| 113 | fRenderTarget->fSamplePatternKey = |
Chris Dalton | 8c4cafd | 2019-04-15 19:14:36 -0600 | [diff] [blame] | 114 | fRenderTarget->getGpu()->findOrAssignSamplePatternKey(fRenderTarget); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 115 | } |
| 116 | SkASSERT(GrSamplePatternDictionary::kInvalidSamplePatternKey |
| 117 | != fRenderTarget->fSamplePatternKey); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 118 | return fRenderTarget->fSamplePatternKey; |
| 119 | } |