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 "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/GrRenderTargetPriv.h"
17#include "src/gpu/GrSamplePatternDictionary.h"
18#include "src/gpu/GrStencilAttachment.h"
19#include "src/gpu/GrStencilSettings.h"
20
21GrRenderTarget::GrRenderTarget(GrGpu* gpu, const SkISize& dimensions, int sampleCount,
22 GrProtected isProtected, GrStencilAttachment* stencil)
23 : INHERITED(gpu, dimensions, isProtected)
24 , fSampleCnt(sampleCount)
25 , fSamplePatternKey(GrSamplePatternDictionary::kInvalidSamplePatternKey)
26 , fStencilAttachment(stencil) {}
27
28GrRenderTarget::~GrRenderTarget() = default;
29
30void GrRenderTarget::onRelease() {
31 fStencilAttachment = nullptr;
32
33 INHERITED::onRelease();
34}
35
36void GrRenderTarget::onAbandon() {
37 fStencilAttachment = nullptr;
38
39 INHERITED::onAbandon();
40}
41
42///////////////////////////////////////////////////////////////////////////////
43
44void GrRenderTargetPriv::attachStencilAttachment(sk_sp<GrStencilAttachment> stencil) {
45#ifdef SK_DEBUG
46 if (1 == fRenderTarget->fSampleCnt) {
47 // TODO: We don't expect a mixed sampled render target to ever change its stencil buffer
48 // right now. But if it does swap in a stencil buffer with a different number of samples,
49 // and if we have a valid fSamplePatternKey, we will need to invalidate fSamplePatternKey
50 // here and add tests to make sure we it properly.
51 SkASSERT(GrSamplePatternDictionary::kInvalidSamplePatternKey ==
52 fRenderTarget->fSamplePatternKey);
53 } else {
54 // Render targets with >1 color sample should never use mixed samples. (This would lead to
55 // different sample patterns, depending on stencil state.)
56 SkASSERT(!stencil || stencil->numSamples() == fRenderTarget->fSampleCnt);
57 }
58#endif
59
60 if (!stencil && !fRenderTarget->fStencilAttachment) {
61 // No need to do any work since we currently don't have a stencil attachment and
62 // we're not actually adding one.
63 return;
64 }
65
66 fRenderTarget->fStencilAttachment = std::move(stencil);
67 if (!fRenderTarget->completeStencilAttachment()) {
68 fRenderTarget->fStencilAttachment = nullptr;
69 }
70}
71
72int GrRenderTargetPriv::numStencilBits() const {
73 SkASSERT(this->getStencilAttachment());
74 return this->getStencilAttachment()->bits();
75}
76
77int GrRenderTargetPriv::getSamplePatternKey() const {
78#ifdef SK_DEBUG
79 GrStencilAttachment* stencil = fRenderTarget->fStencilAttachment.get();
80 if (fRenderTarget->fSampleCnt <= 1) {
81 // If the color buffer is not multisampled, the sample pattern better come from the stencil
82 // buffer (mixed samples).
83 SkASSERT(stencil && stencil->numSamples() > 1);
84 } else {
85 // The color sample count and stencil count cannot both be unequal and both greater than
86 // one. If this were the case, there would be more than one sample pattern associated with
87 // the render target.
88 SkASSERT(!stencil || stencil->numSamples() == fRenderTarget->fSampleCnt);
89 }
90#endif
91 if (GrSamplePatternDictionary::kInvalidSamplePatternKey == fRenderTarget->fSamplePatternKey) {
92 fRenderTarget->fSamplePatternKey =
93 fRenderTarget->getGpu()->findOrAssignSamplePatternKey(fRenderTarget);
94 }
95 SkASSERT(GrSamplePatternDictionary::kInvalidSamplePatternKey
96 != fRenderTarget->fSamplePatternKey);
97 return fRenderTarget->fSamplePatternKey;
98}
99