blob: 00b6164a74cf0ab726bb98cd063f9d879d7820f1 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrGpu.h"
13#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrSamplePatternDictionary.h"
15#include "src/gpu/GrStencilAttachment.h"
16#include "src/gpu/GrStencilSettings.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000017
Brian Salomonf7f54332020-07-28 09:23:35 -040018GrRenderTarget::GrRenderTarget(GrGpu* gpu,
19 const SkISize& dimensions,
20 int sampleCount,
21 GrProtected isProtected,
22 GrStencilAttachment* stencil)
Greg Danield51fa2f2020-01-22 16:53:38 -050023 : INHERITED(gpu, dimensions, isProtected)
Brian Salomonf7f54332020-07-28 09:23:35 -040024 , fStencilAttachment(stencil)
Brian Salomon27b4d8d2019-07-22 14:23:45 -040025 , fSampleCnt(sampleCount)
Brian Salomonf7f54332020-07-28 09:23:35 -040026 , fSamplePatternKey(GrSamplePatternDictionary::kInvalidSamplePatternKey) {}
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
Brian Salomonf7f54332020-07-28 09:23:35 -040042void GrRenderTarget::attachStencilAttachment(sk_sp<GrStencilAttachment> stencil) {
Chris Daltoneffee202019-07-01 22:28:03 -060043#ifdef SK_DEBUG
Brian Salomonf7f54332020-07-28 09:23:35 -040044 if (fSampleCnt == 1) {
Chris Daltoneffee202019-07-01 22:28:03 -060045 // 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.
Brian Salomonf7f54332020-07-28 09:23:35 -040049 SkASSERT(GrSamplePatternDictionary::kInvalidSamplePatternKey == fSamplePatternKey);
Chris Daltoneffee202019-07-01 22:28:03 -060050 } 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.)
Brian Salomonf7f54332020-07-28 09:23:35 -040053 SkASSERT(!stencil || stencil->numSamples() == fSampleCnt);
Chris Daltoneffee202019-07-01 22:28:03 -060054 }
55#endif
56
Brian Salomonf7f54332020-07-28 09:23:35 -040057 if (!stencil && !fStencilAttachment) {
egdaniel79bd2ae2015-09-15 08:46:13 -070058 // No need to do any work since we currently don't have a stencil attachment and
Robert Phillips29e52f12016-11-03 10:19:14 -040059 // we're not actually adding one.
Greg Danielcfa39352018-10-05 12:01:59 -040060 return;
egdaniel79bd2ae2015-09-15 08:46:13 -070061 }
Chris Daltoneffee202019-07-01 22:28:03 -060062
Brian Salomonf7f54332020-07-28 09:23:35 -040063 fStencilAttachment = std::move(stencil);
64 if (!this->completeStencilAttachment()) {
65 fStencilAttachment = nullptr;
halcanary9d524f22016-03-29 09:03:52 -070066 }
bsalomon6bc1b5f2015-02-23 09:06:38 -080067}
cdalton28f45b92016-03-07 13:58:26 -080068
Brian Salomonf7f54332020-07-28 09:23:35 -040069int GrRenderTarget::numStencilBits() const {
Robert Phillips6c2aa7a2019-10-17 19:06:39 +000070 SkASSERT(this->getStencilAttachment());
71 return this->getStencilAttachment()->bits();
72}
73
Brian Salomonf7f54332020-07-28 09:23:35 -040074int GrRenderTarget::getSamplePatternKey() {
Chris Daltoneffee202019-07-01 22:28:03 -060075#ifdef SK_DEBUG
Brian Salomonf7f54332020-07-28 09:23:35 -040076 if (fSampleCnt <= 1) {
Chris Daltoneffee202019-07-01 22:28:03 -060077 // If the color buffer is not multisampled, the sample pattern better come from the stencil
78 // buffer (mixed samples).
Brian Salomonf7f54332020-07-28 09:23:35 -040079 SkASSERT(fStencilAttachment && fStencilAttachment->numSamples() > 1);
Chris Daltoneffee202019-07-01 22:28:03 -060080 } 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.
Brian Salomonf7f54332020-07-28 09:23:35 -040084 SkASSERT(!fStencilAttachment || fStencilAttachment->numSamples() == fSampleCnt);
Chris Daltoneffee202019-07-01 22:28:03 -060085 }
86#endif
Brian Salomonf7f54332020-07-28 09:23:35 -040087 if (GrSamplePatternDictionary::kInvalidSamplePatternKey == fSamplePatternKey) {
88 fSamplePatternKey = this->getGpu()->findOrAssignSamplePatternKey(this);
Chris Daltond7291ba2019-03-07 14:17:03 -070089 }
Brian Salomonf7f54332020-07-28 09:23:35 -040090 SkASSERT(fSamplePatternKey != GrSamplePatternDictionary::kInvalidSamplePatternKey);
91 return fSamplePatternKey;
92}
93
94const SkTArray<SkPoint>& GrRenderTarget::getSampleLocations() {
95 int samplePatternKey = this->getSamplePatternKey();
96 return this->getGpu()->retrieveSampleLocations(samplePatternKey);
Chris Daltond7291ba2019-03-07 14:17:03 -070097}