| 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 | |
| 9 | #include "src/gpu/GrRenderTarget.h" |
| 10 | |
| 11 | #include "src/core/SkRectPriv.h" |
| 12 | #include "src/gpu/GrGpu.h" |
| 13 | #include "src/gpu/GrRenderTargetContext.h" |
| 14 | #include "src/gpu/GrSamplePatternDictionary.h" |
| 15 | #include "src/gpu/GrStencilAttachment.h" |
| 16 | #include "src/gpu/GrStencilSettings.h" |
| 17 | |
| 18 | GrRenderTarget::GrRenderTarget(GrGpu* gpu, |
| 19 | const SkISize& dimensions, |
| 20 | int sampleCount, |
| 21 | GrProtected isProtected, |
| 22 | GrStencilAttachment* stencil) |
| 23 | : INHERITED(gpu, dimensions, isProtected) |
| 24 | , fStencilAttachment(stencil) |
| 25 | , fSampleCnt(sampleCount) |
| 26 | , fSamplePatternKey(GrSamplePatternDictionary::kInvalidSamplePatternKey) {} |
| 27 | |
| 28 | GrRenderTarget::~GrRenderTarget() = default; |
| 29 | |
| 30 | void GrRenderTarget::onRelease() { |
| 31 | fStencilAttachment = nullptr; |
| 32 | |
| 33 | INHERITED::onRelease(); |
| 34 | } |
| 35 | |
| 36 | void GrRenderTarget::onAbandon() { |
| 37 | fStencilAttachment = nullptr; |
| 38 | |
| 39 | INHERITED::onAbandon(); |
| 40 | } |
| 41 | |
| 42 | void GrRenderTarget::attachStencilAttachment(sk_sp<GrStencilAttachment> stencil) { |
| 43 | #ifdef SK_DEBUG |
| 44 | if (fSampleCnt == 1) { |
| 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. |
| 49 | SkASSERT(GrSamplePatternDictionary::kInvalidSamplePatternKey == fSamplePatternKey); |
| 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.) |
| 53 | SkASSERT(!stencil || stencil->numSamples() == fSampleCnt); |
| 54 | } |
| 55 | #endif |
| 56 | |
| 57 | if (!stencil && !fStencilAttachment) { |
| 58 | // No need to do any work since we currently don't have a stencil attachment and |
| 59 | // we're not actually adding one. |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | fStencilAttachment = std::move(stencil); |
| 64 | if (!this->completeStencilAttachment()) { |
| 65 | fStencilAttachment = nullptr; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | int GrRenderTarget::numStencilBits() const { |
| 70 | SkASSERT(this->getStencilAttachment()); |
| 71 | return this->getStencilAttachment()->bits(); |
| 72 | } |
| 73 | |
| 74 | int GrRenderTarget::getSamplePatternKey() { |
| 75 | #ifdef SK_DEBUG |
| 76 | if (fSampleCnt <= 1) { |
| 77 | // If the color buffer is not multisampled, the sample pattern better come from the stencil |
| 78 | // buffer (mixed samples). |
| 79 | SkASSERT(fStencilAttachment && fStencilAttachment->numSamples() > 1); |
| 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. |
| 84 | SkASSERT(!fStencilAttachment || fStencilAttachment->numSamples() == fSampleCnt); |
| 85 | } |
| 86 | #endif |
| 87 | if (GrSamplePatternDictionary::kInvalidSamplePatternKey == fSamplePatternKey) { |
| 88 | fSamplePatternKey = this->getGpu()->findOrAssignSamplePatternKey(this); |
| 89 | } |
| 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); |
| 97 | } |
| 98 | |