blob: a10ec81156c7373786d04fea14c6734617e23047 [file] [log] [blame]
bsalomon@google.comaa5b6732011-07-29 15:13:20 +00001/*
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 Salomon201cdbb2019-08-14 17:00:30 -04009#include "src/gpu/GrRenderTarget.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrRenderTargetPriv.h"
17#include "src/gpu/GrSamplePatternDictionary.h"
18#include "src/gpu/GrStencilAttachment.h"
19#include "src/gpu/GrStencilSettings.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000020
Greg Danield51fa2f2020-01-22 16:53:38 -050021GrRenderTarget::GrRenderTarget(GrGpu* gpu, const SkISize& dimensions, int sampleCount,
22 GrProtected isProtected, GrStencilAttachment* stencil)
23 : INHERITED(gpu, dimensions, isProtected)
Brian Salomon27b4d8d2019-07-22 14:23:45 -040024 , fSampleCnt(sampleCount)
Chris Daltond7291ba2019-03-07 14:17:03 -070025 , fSamplePatternKey(GrSamplePatternDictionary::kInvalidSamplePatternKey)
Brian Salomon9f2b86c2019-10-22 10:37:46 -040026 , fStencilAttachment(stencil) {}
csmartdaltonf9635992016-08-10 11:09:07 -070027
Ben Wagner9ec70c62018-07-12 13:30:47 -040028GrRenderTarget::~GrRenderTarget() = default;
29
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +000030void GrRenderTarget::onRelease() {
Ben Wagner9ec70c62018-07-12 13:30:47 -040031 fStencilAttachment = nullptr;
robertphillips@google.comd3645542012-09-05 18:37:39 +000032
33 INHERITED::onRelease();
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +000034}
35
36void GrRenderTarget::onAbandon() {
Ben Wagner9ec70c62018-07-12 13:30:47 -040037 fStencilAttachment = nullptr;
robertphillips498d7ac2015-10-30 10:11:30 -070038
robertphillips@google.comd3645542012-09-05 18:37:39 +000039 INHERITED::onAbandon();
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +000040}
bsalomon6bc1b5f2015-02-23 09:06:38 -080041
42///////////////////////////////////////////////////////////////////////////////
43
Greg Danielcfa39352018-10-05 12:01:59 -040044void GrRenderTargetPriv::attachStencilAttachment(sk_sp<GrStencilAttachment> stencil) {
Chris Daltoneffee202019-07-01 22:28:03 -060045#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
egdaniel79bd2ae2015-09-15 08:46:13 -070060 if (!stencil && !fRenderTarget->fStencilAttachment) {
61 // No need to do any work since we currently don't have a stencil attachment and
Robert Phillips29e52f12016-11-03 10:19:14 -040062 // we're not actually adding one.
Greg Danielcfa39352018-10-05 12:01:59 -040063 return;
egdaniel79bd2ae2015-09-15 08:46:13 -070064 }
Chris Daltoneffee202019-07-01 22:28:03 -060065
Ben Wagner9ec70c62018-07-12 13:30:47 -040066 fRenderTarget->fStencilAttachment = std::move(stencil);
egdanielec00d942015-09-14 12:56:10 -070067 if (!fRenderTarget->completeStencilAttachment()) {
Ben Wagner9ec70c62018-07-12 13:30:47 -040068 fRenderTarget->fStencilAttachment = nullptr;
halcanary9d524f22016-03-29 09:03:52 -070069 }
bsalomon6bc1b5f2015-02-23 09:06:38 -080070}
cdalton28f45b92016-03-07 13:58:26 -080071
Robert Phillips6c2aa7a2019-10-17 19:06:39 +000072int GrRenderTargetPriv::numStencilBits() const {
73 SkASSERT(this->getStencilAttachment());
74 return this->getStencilAttachment()->bits();
75}
76
Chris Dalton8c4cafd2019-04-15 19:14:36 -060077int GrRenderTargetPriv::getSamplePatternKey() const {
Chris Daltoneffee202019-07-01 22:28:03 -060078#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
Chris Daltond7291ba2019-03-07 14:17:03 -070091 if (GrSamplePatternDictionary::kInvalidSamplePatternKey == fRenderTarget->fSamplePatternKey) {
92 fRenderTarget->fSamplePatternKey =
Chris Dalton8c4cafd2019-04-15 19:14:36 -060093 fRenderTarget->getGpu()->findOrAssignSamplePatternKey(fRenderTarget);
Chris Daltond7291ba2019-03-07 14:17:03 -070094 }
95 SkASSERT(GrSamplePatternDictionary::kInvalidSamplePatternKey
96 != fRenderTarget->fSamplePatternKey);
Chris Daltond7291ba2019-03-07 14:17:03 -070097 return fRenderTarget->fSamplePatternKey;
98}