blob: 6c7a893ef8ee1262493a181cb86d6998b756685c [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/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"
16#include "src/gpu/GrRenderTargetOpList.h"
17#include "src/gpu/GrRenderTargetPriv.h"
18#include "src/gpu/GrSamplePatternDictionary.h"
19#include "src/gpu/GrStencilAttachment.h"
20#include "src/gpu/GrStencilSettings.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000021
Robert Phillipsc4f0a822017-06-13 08:11:36 -040022GrRenderTarget::GrRenderTarget(GrGpu* gpu, const GrSurfaceDesc& desc,
csmartdaltonf9635992016-08-10 11:09:07 -070023 GrStencilAttachment* stencil)
Brian Salomond34edf32017-05-19 15:45:48 -040024 : INHERITED(gpu, desc)
25 , fSampleCnt(desc.fSampleCnt)
Chris Daltond7291ba2019-03-07 14:17:03 -070026 , fSamplePatternKey(GrSamplePatternDictionary::kInvalidSamplePatternKey)
Robert Phillipsfe0253f2018-03-16 16:47:25 -040027 , fStencilAttachment(stencil) {
Brian Salomond34edf32017-05-19 15:45:48 -040028 SkASSERT(desc.fFlags & kRenderTarget_GrSurfaceFlag);
Mike Reed274218e2018-01-08 15:05:02 -050029 fResolveRect = SkRectPriv::MakeILargestInverted();
csmartdaltonf9635992016-08-10 11:09:07 -070030}
31
Ben Wagner9ec70c62018-07-12 13:30:47 -040032GrRenderTarget::~GrRenderTarget() = default;
33
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000034void GrRenderTarget::flagAsNeedingResolve(const SkIRect* rect) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000035 if (kCanResolve_ResolveType == getResolveType()) {
bsalomon49f085d2014-09-05 13:34:00 -070036 if (rect) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000037 fResolveRect.join(*rect);
38 if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
39 fResolveRect.setEmpty();
40 }
41 } else {
42 fResolveRect.setLTRB(0, 0, this->width(), this->height());
43 }
44 }
45}
46
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000047void GrRenderTarget::overrideResolveRect(const SkIRect rect) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000048 fResolveRect = rect;
49 if (fResolveRect.isEmpty()) {
Mike Reed274218e2018-01-08 15:05:02 -050050 fResolveRect = SkRectPriv::MakeILargestInverted();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000051 } else {
52 if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
Mike Reed274218e2018-01-08 15:05:02 -050053 fResolveRect = SkRectPriv::MakeILargestInverted();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000054 }
55 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000056}
57
Mike Reed274218e2018-01-08 15:05:02 -050058void GrRenderTarget::flagAsResolved() {
59 fResolveRect = SkRectPriv::MakeILargestInverted();
60}
61
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +000062void GrRenderTarget::onRelease() {
Ben Wagner9ec70c62018-07-12 13:30:47 -040063 fStencilAttachment = nullptr;
robertphillips@google.comd3645542012-09-05 18:37:39 +000064
65 INHERITED::onRelease();
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +000066}
67
68void GrRenderTarget::onAbandon() {
Ben Wagner9ec70c62018-07-12 13:30:47 -040069 fStencilAttachment = nullptr;
robertphillips498d7ac2015-10-30 10:11:30 -070070
robertphillips@google.comd3645542012-09-05 18:37:39 +000071 INHERITED::onAbandon();
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +000072}
bsalomon6bc1b5f2015-02-23 09:06:38 -080073
74///////////////////////////////////////////////////////////////////////////////
75
Greg Danielcfa39352018-10-05 12:01:59 -040076void GrRenderTargetPriv::attachStencilAttachment(sk_sp<GrStencilAttachment> stencil) {
Chris Daltoneffee202019-07-01 22:28:03 -060077#ifdef SK_DEBUG
78 if (1 == fRenderTarget->fSampleCnt) {
79 // TODO: We don't expect a mixed sampled render target to ever change its stencil buffer
80 // right now. But if it does swap in a stencil buffer with a different number of samples,
81 // and if we have a valid fSamplePatternKey, we will need to invalidate fSamplePatternKey
82 // here and add tests to make sure we it properly.
83 SkASSERT(GrSamplePatternDictionary::kInvalidSamplePatternKey ==
84 fRenderTarget->fSamplePatternKey);
85 } else {
86 // Render targets with >1 color sample should never use mixed samples. (This would lead to
87 // different sample patterns, depending on stencil state.)
88 SkASSERT(!stencil || stencil->numSamples() == fRenderTarget->fSampleCnt);
89 }
90#endif
91
egdaniel79bd2ae2015-09-15 08:46:13 -070092 if (!stencil && !fRenderTarget->fStencilAttachment) {
93 // No need to do any work since we currently don't have a stencil attachment and
Robert Phillips29e52f12016-11-03 10:19:14 -040094 // we're not actually adding one.
Greg Danielcfa39352018-10-05 12:01:59 -040095 return;
egdaniel79bd2ae2015-09-15 08:46:13 -070096 }
Chris Daltoneffee202019-07-01 22:28:03 -060097
Ben Wagner9ec70c62018-07-12 13:30:47 -040098 fRenderTarget->fStencilAttachment = std::move(stencil);
egdanielec00d942015-09-14 12:56:10 -070099 if (!fRenderTarget->completeStencilAttachment()) {
Ben Wagner9ec70c62018-07-12 13:30:47 -0400100 fRenderTarget->fStencilAttachment = nullptr;
halcanary9d524f22016-03-29 09:03:52 -0700101 }
bsalomon6bc1b5f2015-02-23 09:06:38 -0800102}
cdalton28f45b92016-03-07 13:58:26 -0800103
cdalton193d9cf2016-05-12 11:52:02 -0700104int GrRenderTargetPriv::numStencilBits() const {
csmartdaltonc633abb2016-11-01 08:55:55 -0700105 SkASSERT(this->getStencilAttachment());
106 return this->getStencilAttachment()->bits();
cdalton193d9cf2016-05-12 11:52:02 -0700107}
Chris Daltond7291ba2019-03-07 14:17:03 -0700108
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600109int GrRenderTargetPriv::getSamplePatternKey() const {
Chris Daltoneffee202019-07-01 22:28:03 -0600110#ifdef SK_DEBUG
111 GrStencilAttachment* stencil = fRenderTarget->fStencilAttachment.get();
112 if (fRenderTarget->fSampleCnt <= 1) {
113 // If the color buffer is not multisampled, the sample pattern better come from the stencil
114 // buffer (mixed samples).
115 SkASSERT(stencil && stencil->numSamples() > 1);
116 } else {
117 // The color sample count and stencil count cannot both be unequal and both greater than
118 // one. If this were the case, there would be more than one sample pattern associated with
119 // the render target.
120 SkASSERT(!stencil || stencil->numSamples() == fRenderTarget->fSampleCnt);
121 }
122#endif
Chris Daltond7291ba2019-03-07 14:17:03 -0700123 if (GrSamplePatternDictionary::kInvalidSamplePatternKey == fRenderTarget->fSamplePatternKey) {
124 fRenderTarget->fSamplePatternKey =
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600125 fRenderTarget->getGpu()->findOrAssignSamplePatternKey(fRenderTarget);
Chris Daltond7291ba2019-03-07 14:17:03 -0700126 }
127 SkASSERT(GrSamplePatternDictionary::kInvalidSamplePatternKey
128 != fRenderTarget->fSamplePatternKey);
Chris Daltond7291ba2019-03-07 14:17:03 -0700129 return fRenderTarget->fSamplePatternKey;
130}