blob: 68a4eb92241df7286f212c507f72ea38fbe0d2ce [file] [log] [blame]
joshualitt44701df2015-02-23 14:44:57 -08001/*
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
cdalton846c0512016-05-13 10:25:00 -070011#include "GrFragmentProcessor.h"
12#include "GrTypesPriv.h"
joshualitt44701df2015-02-23 14:44:57 -080013#include "SkClipStack.h"
joshualitt44701df2015-02-23 14:44:57 -080014
cdalton846c0512016-05-13 10:25:00 -070015class GrClipMaskManager;
16class GrPipelineBuilder;
joshualitt44701df2015-02-23 14:44:57 -080017
18/**
cdalton846c0512016-05-13 10:25:00 -070019 * 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.
joshualitt44701df2015-02-23 14:44:57 -080021 */
cdalton846c0512016-05-13 10:25:00 -070022class GrAppliedClip {
joshualitt44701df2015-02-23 14:44:57 -080023public:
cdalton846c0512016-05-13 10:25:00 -070024 GrAppliedClip() : fHasStencilClip(false) {}
25 const GrFragmentProcessor* clipCoverageFragmentProcessor() const { return fClipCoverageFP; }
26 const GrScissorState& scissorState() const { return fScissorState; }
27 bool hasStencilClip() const { return fHasStencilClip; }
joshualitt44701df2015-02-23 14:44:57 -080028
29private:
cdalton846c0512016-05-13 10:25:00 -070030 SkAutoTUnref<const GrFragmentProcessor> fClipCoverageFP;
31 GrScissorState fScissorState;
32 bool fHasStencilClip;
joshualitt44701df2015-02-23 14:44:57 -080033
cdalton846c0512016-05-13 10:25:00 -070034 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 */
44class GrClip {
45public:
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 */
58class GrNoClip final : public GrClip {
59private:
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 */
71class GrFixedClip final : public GrClip {
72public:
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
95private:
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 */
107class GrClipStackClip final : public GrClip {
108public:
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
127private:
128 SkIPoint fOrigin;
129 SkAutoTUnref<const SkClipStack> fStack;
joshualitt44701df2015-02-23 14:44:57 -0800130};
131
132#endif