blob: 1b5796cb1a1c2b9e734063eb2dabd035138ecdb4 [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
Brian Salomona9c22572019-08-05 12:57:09 -040021GrRenderTarget::GrRenderTarget(GrGpu* gpu, const SkISize& size, GrPixelConfig config,
22 int sampleCount, GrProtected isProtected,
23 GrStencilAttachment* stencil)
24 : INHERITED(gpu, size, config, isProtected)
Brian Salomon27b4d8d2019-07-22 14:23:45 -040025 , fSampleCnt(sampleCount)
Chris Daltond7291ba2019-03-07 14:17:03 -070026 , fSamplePatternKey(GrSamplePatternDictionary::kInvalidSamplePatternKey)
Robert Phillipsfe0253f2018-03-16 16:47:25 -040027 , fStencilAttachment(stencil) {
Mike Reed274218e2018-01-08 15:05:02 -050028 fResolveRect = SkRectPriv::MakeILargestInverted();
csmartdaltonf9635992016-08-10 11:09:07 -070029}
30
Ben Wagner9ec70c62018-07-12 13:30:47 -040031GrRenderTarget::~GrRenderTarget() = default;
32
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000033void GrRenderTarget::flagAsNeedingResolve(const SkIRect* rect) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000034 if (kCanResolve_ResolveType == getResolveType()) {
bsalomon49f085d2014-09-05 13:34:00 -070035 if (rect) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000036 fResolveRect.join(*rect);
Mike Reed92b33352019-08-24 19:39:13 -040037 if (!fResolveRect.intersect({0, 0, this->width(), this->height()})) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000038 fResolveRect.setEmpty();
39 }
40 } else {
41 fResolveRect.setLTRB(0, 0, this->width(), this->height());
42 }
43 }
44}
45
Mike Reed274218e2018-01-08 15:05:02 -050046void GrRenderTarget::flagAsResolved() {
47 fResolveRect = SkRectPriv::MakeILargestInverted();
48}
49
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +000050void GrRenderTarget::onRelease() {
Ben Wagner9ec70c62018-07-12 13:30:47 -040051 fStencilAttachment = nullptr;
robertphillips@google.comd3645542012-09-05 18:37:39 +000052
53 INHERITED::onRelease();
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +000054}
55
56void GrRenderTarget::onAbandon() {
Ben Wagner9ec70c62018-07-12 13:30:47 -040057 fStencilAttachment = nullptr;
robertphillips498d7ac2015-10-30 10:11:30 -070058
robertphillips@google.comd3645542012-09-05 18:37:39 +000059 INHERITED::onAbandon();
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +000060}
bsalomon6bc1b5f2015-02-23 09:06:38 -080061
62///////////////////////////////////////////////////////////////////////////////
63
Greg Danielcfa39352018-10-05 12:01:59 -040064void GrRenderTargetPriv::attachStencilAttachment(sk_sp<GrStencilAttachment> stencil) {
Chris Daltoneffee202019-07-01 22:28:03 -060065#ifdef SK_DEBUG
66 if (1 == fRenderTarget->fSampleCnt) {
67 // TODO: We don't expect a mixed sampled render target to ever change its stencil buffer
68 // right now. But if it does swap in a stencil buffer with a different number of samples,
69 // and if we have a valid fSamplePatternKey, we will need to invalidate fSamplePatternKey
70 // here and add tests to make sure we it properly.
71 SkASSERT(GrSamplePatternDictionary::kInvalidSamplePatternKey ==
72 fRenderTarget->fSamplePatternKey);
73 } else {
74 // Render targets with >1 color sample should never use mixed samples. (This would lead to
75 // different sample patterns, depending on stencil state.)
76 SkASSERT(!stencil || stencil->numSamples() == fRenderTarget->fSampleCnt);
77 }
78#endif
79
egdaniel79bd2ae2015-09-15 08:46:13 -070080 if (!stencil && !fRenderTarget->fStencilAttachment) {
81 // No need to do any work since we currently don't have a stencil attachment and
Robert Phillips29e52f12016-11-03 10:19:14 -040082 // we're not actually adding one.
Greg Danielcfa39352018-10-05 12:01:59 -040083 return;
egdaniel79bd2ae2015-09-15 08:46:13 -070084 }
Chris Daltoneffee202019-07-01 22:28:03 -060085
Ben Wagner9ec70c62018-07-12 13:30:47 -040086 fRenderTarget->fStencilAttachment = std::move(stencil);
egdanielec00d942015-09-14 12:56:10 -070087 if (!fRenderTarget->completeStencilAttachment()) {
Ben Wagner9ec70c62018-07-12 13:30:47 -040088 fRenderTarget->fStencilAttachment = nullptr;
halcanary9d524f22016-03-29 09:03:52 -070089 }
bsalomon6bc1b5f2015-02-23 09:06:38 -080090}
cdalton28f45b92016-03-07 13:58:26 -080091
cdalton193d9cf2016-05-12 11:52:02 -070092int GrRenderTargetPriv::numStencilBits() const {
csmartdaltonc633abb2016-11-01 08:55:55 -070093 SkASSERT(this->getStencilAttachment());
94 return this->getStencilAttachment()->bits();
cdalton193d9cf2016-05-12 11:52:02 -070095}
Chris Daltond7291ba2019-03-07 14:17:03 -070096
Chris Dalton8c4cafd2019-04-15 19:14:36 -060097int GrRenderTargetPriv::getSamplePatternKey() const {
Chris Daltoneffee202019-07-01 22:28:03 -060098#ifdef SK_DEBUG
99 GrStencilAttachment* stencil = fRenderTarget->fStencilAttachment.get();
100 if (fRenderTarget->fSampleCnt <= 1) {
101 // If the color buffer is not multisampled, the sample pattern better come from the stencil
102 // buffer (mixed samples).
103 SkASSERT(stencil && stencil->numSamples() > 1);
104 } else {
105 // The color sample count and stencil count cannot both be unequal and both greater than
106 // one. If this were the case, there would be more than one sample pattern associated with
107 // the render target.
108 SkASSERT(!stencil || stencil->numSamples() == fRenderTarget->fSampleCnt);
109 }
110#endif
Chris Daltond7291ba2019-03-07 14:17:03 -0700111 if (GrSamplePatternDictionary::kInvalidSamplePatternKey == fRenderTarget->fSamplePatternKey) {
112 fRenderTarget->fSamplePatternKey =
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600113 fRenderTarget->getGpu()->findOrAssignSamplePatternKey(fRenderTarget);
Chris Daltond7291ba2019-03-07 14:17:03 -0700114 }
115 SkASSERT(GrSamplePatternDictionary::kInvalidSamplePatternKey
116 != fRenderTarget->fSamplePatternKey);
Chris Daltond7291ba2019-03-07 14:17:03 -0700117 return fRenderTarget->fSamplePatternKey;
118}