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