blob: b7e10686ac3cb64bf07123e8a9d5b5308f1ae30d [file] [log] [blame]
Chris Daltonbbfd5162017-11-07 13:35:22 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrAppliedClip.h"
12#include "src/gpu/GrFixedClip.h"
Chris Daltonbbfd5162017-11-07 13:35:22 -070013
14/**
15 * Implements GrHardClip with the currently-existing stencil buffer contents and GrFixedClip.
16 */
17class GrStencilClip final : public GrHardClip {
18public:
Michael Ludwigd1d997e2020-06-04 15:52:44 -040019 explicit GrStencilClip(const SkISize& rtDims, uint32_t stencilStackID = SK_InvalidGenID)
20 : fFixedClip(rtDims)
21 , fStencilStackID(stencilStackID) {}
Chris Daltonbbfd5162017-11-07 13:35:22 -070022
Michael Ludwigd1d997e2020-06-04 15:52:44 -040023 GrStencilClip(const SkISize& rtDims, const SkIRect& scissorRect,
24 uint32_t stencilStackID = SK_InvalidGenID)
25 : fFixedClip(rtDims, scissorRect)
26 , fStencilStackID(stencilStackID) {}
Chris Daltonbbfd5162017-11-07 13:35:22 -070027
28 const GrFixedClip& fixedClip() const { return fFixedClip; }
29 GrFixedClip& fixedClip() { return fFixedClip; }
30
Michael Ludwig828d3412020-05-12 13:15:35 -040031 uint32_t stencilStackID() const { return fStencilStackID; }
Chris Daltonbbfd5162017-11-07 13:35:22 -070032 bool hasStencilClip() const { return SK_InvalidGenID != fStencilStackID; }
33 void setStencilClip(uint32_t stencilStackID) { fStencilStackID = stencilStackID; }
34
Michael Ludwig4e3cab72020-06-30 11:12:46 -040035 SkIRect getConservativeBounds() const final {
Michael Ludwige06a8972020-06-11 10:29:00 -040036 return fFixedClip.getConservativeBounds();
Chris Daltonbbfd5162017-11-07 13:35:22 -070037 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -040038
Michael Ludwig6397e802020-08-05 15:50:01 -040039 Effect apply(GrAppliedHardClip* out, SkIRect* bounds) const final {
Michael Ludwig4e3cab72020-06-30 11:12:46 -040040 Effect effect = fFixedClip.apply(out, bounds);
41 if (effect == Effect::kClippedOut) {
42 // Stencil won't bring back coverage
43 return Effect::kClippedOut;
Chris Daltonbbfd5162017-11-07 13:35:22 -070044 }
45 if (this->hasStencilClip()) {
Brian Salomonc3833b42018-07-09 18:23:58 +000046 out->addStencilClip(fStencilStackID);
Michael Ludwig4e3cab72020-06-30 11:12:46 -040047 effect = Effect::kClipped;
Chris Daltonbbfd5162017-11-07 13:35:22 -070048 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -040049 return effect;
50 }
51
Michael Ludwig6397e802020-08-05 15:50:01 -040052 PreClipResult preApply(const SkRect& drawBounds, GrAA aa) const final {
Michael Ludwig4e3cab72020-06-30 11:12:46 -040053 if (this->hasStencilClip()) {
Michael Ludwig6397e802020-08-05 15:50:01 -040054 return this->INHERITED::preApply(drawBounds, aa);
Michael Ludwig4e3cab72020-06-30 11:12:46 -040055 } else {
Michael Ludwig6397e802020-08-05 15:50:01 -040056 return fFixedClip.preApply(drawBounds, aa);
Michael Ludwig4e3cab72020-06-30 11:12:46 -040057 }
Chris Daltonbbfd5162017-11-07 13:35:22 -070058 }
59
60private:
61 GrFixedClip fFixedClip;
62 uint32_t fStencilStackID;
63
John Stiles7571f9e2020-09-02 22:42:33 -040064 using INHERITED = GrClip;
Chris Daltonbbfd5162017-11-07 13:35:22 -070065};
66
67#endif