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 | |
Brian Salomon | 201cdbb | 2019-08-14 17:00:30 -0400 | [diff] [blame] | 9 | #include "src/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 "src/core/SkRectPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrGpu.h" |
| 13 | #include "src/gpu/GrRenderTargetContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrSamplePatternDictionary.h" |
| 15 | #include "src/gpu/GrStencilAttachment.h" |
| 16 | #include "src/gpu/GrStencilSettings.h" |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 17 | |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame^] | 18 | GrRenderTarget::GrRenderTarget(GrGpu* gpu, |
| 19 | const SkISize& dimensions, |
| 20 | int sampleCount, |
| 21 | GrProtected isProtected, |
| 22 | GrStencilAttachment* stencil) |
Greg Daniel | d51fa2f | 2020-01-22 16:53:38 -0500 | [diff] [blame] | 23 | : INHERITED(gpu, dimensions, isProtected) |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame^] | 24 | , fStencilAttachment(stencil) |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 25 | , fSampleCnt(sampleCount) |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame^] | 26 | , fSamplePatternKey(GrSamplePatternDictionary::kInvalidSamplePatternKey) {} |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 27 | |
Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 28 | GrRenderTarget::~GrRenderTarget() = default; |
| 29 | |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 30 | void GrRenderTarget::onRelease() { |
Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 31 | fStencilAttachment = nullptr; |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 32 | |
| 33 | INHERITED::onRelease(); |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void GrRenderTarget::onAbandon() { |
Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 37 | fStencilAttachment = nullptr; |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 38 | |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 39 | INHERITED::onAbandon(); |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 40 | } |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 41 | |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame^] | 42 | void GrRenderTarget::attachStencilAttachment(sk_sp<GrStencilAttachment> stencil) { |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 43 | #ifdef SK_DEBUG |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame^] | 44 | if (fSampleCnt == 1) { |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 45 | // TODO: We don't expect a mixed sampled render target to ever change its stencil buffer |
| 46 | // right now. But if it does swap in a stencil buffer with a different number of samples, |
| 47 | // and if we have a valid fSamplePatternKey, we will need to invalidate fSamplePatternKey |
| 48 | // here and add tests to make sure we it properly. |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame^] | 49 | SkASSERT(GrSamplePatternDictionary::kInvalidSamplePatternKey == fSamplePatternKey); |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 50 | } else { |
| 51 | // Render targets with >1 color sample should never use mixed samples. (This would lead to |
| 52 | // different sample patterns, depending on stencil state.) |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame^] | 53 | SkASSERT(!stencil || stencil->numSamples() == fSampleCnt); |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 54 | } |
| 55 | #endif |
| 56 | |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame^] | 57 | if (!stencil && !fStencilAttachment) { |
egdaniel | 79bd2ae | 2015-09-15 08:46:13 -0700 | [diff] [blame] | 58 | // 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] | 59 | // we're not actually adding one. |
Greg Daniel | cfa3935 | 2018-10-05 12:01:59 -0400 | [diff] [blame] | 60 | return; |
egdaniel | 79bd2ae | 2015-09-15 08:46:13 -0700 | [diff] [blame] | 61 | } |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 62 | |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame^] | 63 | fStencilAttachment = std::move(stencil); |
| 64 | if (!this->completeStencilAttachment()) { |
| 65 | fStencilAttachment = nullptr; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 66 | } |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 67 | } |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 68 | |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame^] | 69 | int GrRenderTarget::numStencilBits() const { |
Robert Phillips | 6c2aa7a | 2019-10-17 19:06:39 +0000 | [diff] [blame] | 70 | SkASSERT(this->getStencilAttachment()); |
| 71 | return this->getStencilAttachment()->bits(); |
| 72 | } |
| 73 | |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame^] | 74 | int GrRenderTarget::getSamplePatternKey() { |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 75 | #ifdef SK_DEBUG |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame^] | 76 | if (fSampleCnt <= 1) { |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 77 | // If the color buffer is not multisampled, the sample pattern better come from the stencil |
| 78 | // buffer (mixed samples). |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame^] | 79 | SkASSERT(fStencilAttachment && fStencilAttachment->numSamples() > 1); |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 80 | } else { |
| 81 | // The color sample count and stencil count cannot both be unequal and both greater than |
| 82 | // one. If this were the case, there would be more than one sample pattern associated with |
| 83 | // the render target. |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame^] | 84 | SkASSERT(!fStencilAttachment || fStencilAttachment->numSamples() == fSampleCnt); |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 85 | } |
| 86 | #endif |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame^] | 87 | if (GrSamplePatternDictionary::kInvalidSamplePatternKey == fSamplePatternKey) { |
| 88 | fSamplePatternKey = this->getGpu()->findOrAssignSamplePatternKey(this); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 89 | } |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame^] | 90 | SkASSERT(fSamplePatternKey != GrSamplePatternDictionary::kInvalidSamplePatternKey); |
| 91 | return fSamplePatternKey; |
| 92 | } |
| 93 | |
| 94 | const SkTArray<SkPoint>& GrRenderTarget::getSampleLocations() { |
| 95 | int samplePatternKey = this->getSamplePatternKey(); |
| 96 | return this->getGpu()->retrieveSampleLocations(samplePatternKey); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 97 | } |