joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010 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 GrClip_DEFINED |
| 9 | #define GrClip_DEFINED |
| 10 | |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 11 | #include "GrFragmentProcessor.h" |
| 12 | #include "GrTypesPriv.h" |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 13 | #include "SkClipStack.h" |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 14 | |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 15 | class GrClipMaskManager; |
| 16 | class GrPipelineBuilder; |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 17 | |
| 18 | /** |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 19 | * Produced by GrClip. It provides a set of modifications to the drawing state that are used to |
| 20 | * create the final GrPipeline for a GrBatch. |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 21 | */ |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 22 | class GrAppliedClip { |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 23 | public: |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 24 | GrAppliedClip() : fHasStencilClip(false) {} |
| 25 | const GrFragmentProcessor* clipCoverageFragmentProcessor() const { return fClipCoverageFP; } |
| 26 | const GrScissorState& scissorState() const { return fScissorState; } |
| 27 | bool hasStencilClip() const { return fHasStencilClip; } |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 28 | |
| 29 | private: |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 30 | SkAutoTUnref<const GrFragmentProcessor> fClipCoverageFP; |
| 31 | GrScissorState fScissorState; |
| 32 | bool fHasStencilClip; |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 33 | |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 34 | friend class GrFixedClip; |
| 35 | friend class GrClipMaskManager; |
| 36 | |
| 37 | typedef SkNoncopyable INHERITED; |
| 38 | }; |
| 39 | |
| 40 | /** |
| 41 | * GrClip is an abstract base class for applying a clip. It constructs a clip mask if necessary, and |
| 42 | * fills out a GrAppliedClip instructing the caller on how to set up the draw state. |
| 43 | */ |
| 44 | class GrClip { |
| 45 | public: |
| 46 | virtual bool quickContains(const SkRect&) const = 0; |
| 47 | virtual void getConservativeBounds(int width, int height, SkIRect* devResult, |
| 48 | bool* isIntersectionOfRects = nullptr) const = 0; |
| 49 | virtual bool apply(GrClipMaskManager*, const GrPipelineBuilder&, const SkRect* devBounds, |
| 50 | GrAppliedClip*) const = 0; |
| 51 | |
| 52 | virtual ~GrClip() {} |
| 53 | }; |
| 54 | |
| 55 | /** |
| 56 | * Specialized implementation for no clip. |
| 57 | */ |
| 58 | class GrNoClip final : public GrClip { |
| 59 | private: |
| 60 | bool quickContains(const SkRect&) const final { return true; } |
| 61 | void getConservativeBounds(int width, int height, SkIRect* devResult, |
| 62 | bool* isIntersectionOfRects) const final; |
| 63 | bool apply(GrClipMaskManager*, const GrPipelineBuilder&, |
| 64 | const SkRect*, GrAppliedClip*) const final { return true; } |
| 65 | }; |
| 66 | |
| 67 | /** |
| 68 | * GrFixedClip is a clip that can be represented by fixed-function hardware. It never modifies the |
| 69 | * stencil buffer itself, but can be configured to use whatever clip is already there. |
| 70 | */ |
| 71 | class GrFixedClip final : public GrClip { |
| 72 | public: |
| 73 | GrFixedClip() : fHasStencilClip(false) {} |
| 74 | GrFixedClip(const SkIRect& scissorRect) : fScissorState(scissorRect), fHasStencilClip(false) {} |
| 75 | |
| 76 | void reset() { |
| 77 | fScissorState.setDisabled(); |
| 78 | fHasStencilClip = false; |
| 79 | } |
| 80 | |
| 81 | void reset(const SkIRect& scissorRect) { |
| 82 | fScissorState.set(scissorRect); |
| 83 | fHasStencilClip = false; |
| 84 | } |
| 85 | |
| 86 | void enableStencilClip(bool enable) { fHasStencilClip = enable; } |
| 87 | |
| 88 | const GrScissorState& scissorState() const { return fScissorState; } |
| 89 | bool hasStencilClip() const { return fHasStencilClip; } |
| 90 | |
| 91 | bool quickContains(const SkRect&) const final; |
| 92 | void getConservativeBounds(int width, int height, SkIRect* devResult, |
| 93 | bool* isIntersectionOfRects) const final; |
| 94 | |
| 95 | private: |
| 96 | bool apply(GrClipMaskManager*, const GrPipelineBuilder&, |
| 97 | const SkRect* devBounds, GrAppliedClip* out) const final; |
| 98 | |
| 99 | GrScissorState fScissorState; |
| 100 | bool fHasStencilClip; |
| 101 | }; |
| 102 | |
| 103 | /** |
| 104 | * GrClipStackClip can apply a generic SkClipStack to the draw state. It may generate clip masks or |
| 105 | * write to the stencil buffer during apply(). |
| 106 | */ |
| 107 | class GrClipStackClip final : public GrClip { |
| 108 | public: |
| 109 | GrClipStackClip(const SkClipStack* stack = nullptr, const SkIPoint* origin = nullptr) { |
| 110 | this->reset(stack, origin); |
| 111 | } |
| 112 | |
| 113 | void reset(const SkClipStack* stack = nullptr, const SkIPoint* origin = nullptr) { |
| 114 | fOrigin = origin ? *origin : SkIPoint::Make(0, 0); |
| 115 | fStack.reset(SkSafeRef(stack)); |
| 116 | } |
| 117 | |
| 118 | const SkIPoint& origin() const { return fOrigin; } |
| 119 | const SkClipStack* clipStack() const { return fStack; } |
| 120 | |
| 121 | bool quickContains(const SkRect&) const final; |
| 122 | void getConservativeBounds(int width, int height, SkIRect* devResult, |
| 123 | bool* isIntersectionOfRects) const final; |
| 124 | bool apply(GrClipMaskManager*, const GrPipelineBuilder&, |
| 125 | const SkRect* devBounds, GrAppliedClip*) const final; |
| 126 | |
| 127 | private: |
| 128 | SkIPoint fOrigin; |
| 129 | SkAutoTUnref<const SkClipStack> fStack; |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | #endif |