Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | #ifndef GrStencilClip_DEFINED |
| 9 | #define GrStencilClip_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrAppliedClip.h" |
| 12 | #include "src/gpu/GrFixedClip.h" |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 13 | |
| 14 | /** |
| 15 | * Implements GrHardClip with the currently-existing stencil buffer contents and GrFixedClip. |
| 16 | */ |
| 17 | class GrStencilClip final : public GrHardClip { |
| 18 | public: |
Michael Ludwig | d1d997e | 2020-06-04 15:52:44 -0400 | [diff] [blame] | 19 | explicit GrStencilClip(const SkISize& rtDims, uint32_t stencilStackID = SK_InvalidGenID) |
| 20 | : fFixedClip(rtDims) |
| 21 | , fStencilStackID(stencilStackID) {} |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 22 | |
Michael Ludwig | d1d997e | 2020-06-04 15:52:44 -0400 | [diff] [blame] | 23 | GrStencilClip(const SkISize& rtDims, const SkIRect& scissorRect, |
| 24 | uint32_t stencilStackID = SK_InvalidGenID) |
| 25 | : fFixedClip(rtDims, scissorRect) |
| 26 | , fStencilStackID(stencilStackID) {} |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 27 | |
| 28 | const GrFixedClip& fixedClip() const { return fFixedClip; } |
| 29 | GrFixedClip& fixedClip() { return fFixedClip; } |
| 30 | |
Michael Ludwig | 828d341 | 2020-05-12 13:15:35 -0400 | [diff] [blame] | 31 | uint32_t stencilStackID() const { return fStencilStackID; } |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 32 | bool hasStencilClip() const { return SK_InvalidGenID != fStencilStackID; } |
| 33 | void setStencilClip(uint32_t stencilStackID) { fStencilStackID = stencilStackID; } |
| 34 | |
Michael Ludwig | 4e3cab7 | 2020-06-30 11:12:46 -0400 | [diff] [blame] | 35 | SkIRect getConservativeBounds() const final { |
Michael Ludwig | e06a897 | 2020-06-11 10:29:00 -0400 | [diff] [blame] | 36 | return fFixedClip.getConservativeBounds(); |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 37 | } |
Michael Ludwig | 4e3cab7 | 2020-06-30 11:12:46 -0400 | [diff] [blame] | 38 | |
Michael Ludwig | 6397e80 | 2020-08-05 15:50:01 -0400 | [diff] [blame] | 39 | Effect apply(GrAppliedHardClip* out, SkIRect* bounds) const final { |
Michael Ludwig | 4e3cab7 | 2020-06-30 11:12:46 -0400 | [diff] [blame] | 40 | Effect effect = fFixedClip.apply(out, bounds); |
| 41 | if (effect == Effect::kClippedOut) { |
| 42 | // Stencil won't bring back coverage |
| 43 | return Effect::kClippedOut; |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 44 | } |
| 45 | if (this->hasStencilClip()) { |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 46 | out->addStencilClip(fStencilStackID); |
Michael Ludwig | 4e3cab7 | 2020-06-30 11:12:46 -0400 | [diff] [blame] | 47 | effect = Effect::kClipped; |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 48 | } |
Michael Ludwig | 4e3cab7 | 2020-06-30 11:12:46 -0400 | [diff] [blame] | 49 | return effect; |
| 50 | } |
| 51 | |
Michael Ludwig | 6397e80 | 2020-08-05 15:50:01 -0400 | [diff] [blame] | 52 | PreClipResult preApply(const SkRect& drawBounds, GrAA aa) const final { |
Michael Ludwig | 4e3cab7 | 2020-06-30 11:12:46 -0400 | [diff] [blame] | 53 | if (this->hasStencilClip()) { |
Michael Ludwig | 6397e80 | 2020-08-05 15:50:01 -0400 | [diff] [blame] | 54 | return this->INHERITED::preApply(drawBounds, aa); |
Michael Ludwig | 4e3cab7 | 2020-06-30 11:12:46 -0400 | [diff] [blame] | 55 | } else { |
Michael Ludwig | 6397e80 | 2020-08-05 15:50:01 -0400 | [diff] [blame] | 56 | return fFixedClip.preApply(drawBounds, aa); |
Michael Ludwig | 4e3cab7 | 2020-06-30 11:12:46 -0400 | [diff] [blame] | 57 | } |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | private: |
| 61 | GrFixedClip fFixedClip; |
| 62 | uint32_t fStencilStackID; |
| 63 | |
| 64 | typedef GrClip INHERITED; |
| 65 | }; |
| 66 | |
| 67 | #endif |