blob: 5fe7a427ff2a9a79fd724899967216d5d3b30635 [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 "src/core/SkRectPriv.h"
Greg Daniel8ade5e82020-10-07 13:09:48 -040012#include "src/gpu/GrBackendUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrGpu.h"
14#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrSamplePatternDictionary.h"
16#include "src/gpu/GrStencilAttachment.h"
17#include "src/gpu/GrStencilSettings.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000018
Brian Salomonf7f54332020-07-28 09:23:35 -040019GrRenderTarget::GrRenderTarget(GrGpu* gpu,
20 const SkISize& dimensions,
21 int sampleCount,
22 GrProtected isProtected,
23 GrStencilAttachment* stencil)
Greg Danield51fa2f2020-01-22 16:53:38 -050024 : INHERITED(gpu, dimensions, isProtected)
Brian Salomonf7f54332020-07-28 09:23:35 -040025 , fStencilAttachment(stencil)
Brian Salomon27b4d8d2019-07-22 14:23:45 -040026 , fSampleCnt(sampleCount)
Brian Salomonf7f54332020-07-28 09:23:35 -040027 , fSamplePatternKey(GrSamplePatternDictionary::kInvalidSamplePatternKey) {}
csmartdaltonf9635992016-08-10 11:09:07 -070028
Ben Wagner9ec70c62018-07-12 13:30:47 -040029GrRenderTarget::~GrRenderTarget() = default;
30
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +000031void GrRenderTarget::onRelease() {
Ben Wagner9ec70c62018-07-12 13:30:47 -040032 fStencilAttachment = nullptr;
robertphillips@google.comd3645542012-09-05 18:37:39 +000033
34 INHERITED::onRelease();
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +000035}
36
37void GrRenderTarget::onAbandon() {
Ben Wagner9ec70c62018-07-12 13:30:47 -040038 fStencilAttachment = nullptr;
robertphillips498d7ac2015-10-30 10:11:30 -070039
robertphillips@google.comd3645542012-09-05 18:37:39 +000040 INHERITED::onAbandon();
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +000041}
bsalomon6bc1b5f2015-02-23 09:06:38 -080042
Brian Salomonf7f54332020-07-28 09:23:35 -040043void GrRenderTarget::attachStencilAttachment(sk_sp<GrStencilAttachment> stencil) {
Chris Daltoneffee202019-07-01 22:28:03 -060044#ifdef SK_DEBUG
Brian Salomonf7f54332020-07-28 09:23:35 -040045 if (fSampleCnt == 1) {
Chris Daltoneffee202019-07-01 22:28:03 -060046 // 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 Salomonf7f54332020-07-28 09:23:35 -040050 SkASSERT(GrSamplePatternDictionary::kInvalidSamplePatternKey == fSamplePatternKey);
Chris Daltoneffee202019-07-01 22:28:03 -060051 } 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 Salomonf7f54332020-07-28 09:23:35 -040054 SkASSERT(!stencil || stencil->numSamples() == fSampleCnt);
Chris Daltoneffee202019-07-01 22:28:03 -060055 }
56#endif
57
Brian Salomonf7f54332020-07-28 09:23:35 -040058 if (!stencil && !fStencilAttachment) {
egdaniel79bd2ae2015-09-15 08:46:13 -070059 // No need to do any work since we currently don't have a stencil attachment and
Robert Phillips29e52f12016-11-03 10:19:14 -040060 // we're not actually adding one.
Greg Danielcfa39352018-10-05 12:01:59 -040061 return;
egdaniel79bd2ae2015-09-15 08:46:13 -070062 }
Chris Daltoneffee202019-07-01 22:28:03 -060063
Brian Salomonf7f54332020-07-28 09:23:35 -040064 fStencilAttachment = std::move(stencil);
65 if (!this->completeStencilAttachment()) {
66 fStencilAttachment = nullptr;
halcanary9d524f22016-03-29 09:03:52 -070067 }
bsalomon6bc1b5f2015-02-23 09:06:38 -080068}
cdalton28f45b92016-03-07 13:58:26 -080069
Brian Salomonf7f54332020-07-28 09:23:35 -040070int GrRenderTarget::numStencilBits() const {
Robert Phillips6c2aa7a2019-10-17 19:06:39 +000071 SkASSERT(this->getStencilAttachment());
Greg Daniel8ade5e82020-10-07 13:09:48 -040072 return GrBackendFormatStencilBits(this->getStencilAttachment()->backendFormat());
Robert Phillips6c2aa7a2019-10-17 19:06:39 +000073}
74
Brian Salomonf7f54332020-07-28 09:23:35 -040075int GrRenderTarget::getSamplePatternKey() {
Chris Daltoneffee202019-07-01 22:28:03 -060076#ifdef SK_DEBUG
Brian Salomonf7f54332020-07-28 09:23:35 -040077 if (fSampleCnt <= 1) {
Chris Daltoneffee202019-07-01 22:28:03 -060078 // If the color buffer is not multisampled, the sample pattern better come from the stencil
79 // buffer (mixed samples).
Brian Salomonf7f54332020-07-28 09:23:35 -040080 SkASSERT(fStencilAttachment && fStencilAttachment->numSamples() > 1);
Chris Daltoneffee202019-07-01 22:28:03 -060081 } 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 Salomonf7f54332020-07-28 09:23:35 -040085 SkASSERT(!fStencilAttachment || fStencilAttachment->numSamples() == fSampleCnt);
Chris Daltoneffee202019-07-01 22:28:03 -060086 }
87#endif
Brian Salomonf7f54332020-07-28 09:23:35 -040088 if (GrSamplePatternDictionary::kInvalidSamplePatternKey == fSamplePatternKey) {
89 fSamplePatternKey = this->getGpu()->findOrAssignSamplePatternKey(this);
Chris Daltond7291ba2019-03-07 14:17:03 -070090 }
Brian Salomonf7f54332020-07-28 09:23:35 -040091 SkASSERT(fSamplePatternKey != GrSamplePatternDictionary::kInvalidSamplePatternKey);
92 return fSamplePatternKey;
93}
94
95const SkTArray<SkPoint>& GrRenderTarget::getSampleLocations() {
96 int samplePatternKey = this->getSamplePatternKey();
97 return this->getGpu()->retrieveSampleLocations(samplePatternKey);
Chris Daltond7291ba2019-03-07 14:17:03 -070098}