Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: include/gpu/GrClip.h

Issue 1971343002: Convert GrClip to an abstract base class (Closed) Base URL: https://skia.googlesource.com/skia.git@upload2_clipout
Patch Set: fix crash Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gm/texdata.cpp ('k') | include/gpu/GrTypesPriv.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2010 Google Inc. 2 * Copyright 2010 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef GrClip_DEFINED 8 #ifndef GrClip_DEFINED
9 #define GrClip_DEFINED 9 #define GrClip_DEFINED
10 10
11 #include "GrFragmentProcessor.h"
12 #include "GrTypesPriv.h"
11 #include "SkClipStack.h" 13 #include "SkClipStack.h"
12 14
13 struct SkIRect; 15 class GrClipMaskManager;
16 class GrPipelineBuilder;
14 17
15 /** 18 /**
16 * GrClip encapsulates the information required to construct the clip 19 * Produced by GrClip. It provides a set of modifications to the drawing state t hat are used to
17 * masks. 'A GrClip is either wide open, just an IRect, just a Rect, or a full c lipstack. 20 * create the final GrPipeline for a GrBatch.
18 * If the clip is a clipstack than the origin is used to translate the stack wit h
19 * respect to device coordinates. This allows us to use a clip stack that is
20 * specified for a root device with a layer device that is restricted to a subse t
21 * of the original canvas. For other clip types the origin will always be (0,0).
22 *
23 * NOTE: GrClip *must* point to a const clipstack
24 */ 21 */
25 class GrClip : SkNoncopyable { 22 class GrAppliedClip {
26 public: 23 public:
27 GrClip() : fClipType(kWideOpen_ClipType) { 24 GrAppliedClip() : fHasStencilClip(false) {}
28 fOrigin.setZero(); 25 const GrFragmentProcessor* clipCoverageFragmentProcessor() const { return fC lipCoverageFP; }
26 const GrScissorState& scissorState() const { return fScissorState; }
27 bool hasStencilClip() const { return fHasStencilClip; }
28
29 private:
30 SkAutoTUnref<const GrFragmentProcessor> fClipCoverageFP;
31 GrScissorState fScissorState;
32 bool fHasStencilClip;
33
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 ma sk if necessary, and
42 * fills out a GrAppliedClip instructing the caller on how to set up the draw st ate.
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) co nst = 0;
49 virtual bool apply(GrClipMaskManager*, const GrPipelineBuilder&, const SkRec t* 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), fHasSt encilClip(false) {}
75
76 void reset() {
77 fScissorState.setDisabled();
78 fHasStencilClip = false;
29 } 79 }
30 80
31 GrClip(const SkIRect& rect) : fClipType(kIRect_ClipType) { 81 void reset(const SkIRect& scissorRect) {
32 fOrigin.setZero(); 82 fScissorState.set(scissorRect);
33 fClip.fIRect = rect; 83 fHasStencilClip = false;
34 } 84 }
35 85
36 GrClip(const SkRect& rect) : fClipType(kIRect_ClipType) { 86 void enableStencilClip(bool enable) { fHasStencilClip = enable; }
37 fOrigin.setZero(); 87
38 fClip.fIRect.fLeft = SkScalarRoundToInt(rect.fLeft); 88 const GrScissorState& scissorState() const { return fScissorState; }
39 fClip.fIRect.fTop = SkScalarRoundToInt(rect.fTop); 89 bool hasStencilClip() const { return fHasStencilClip; }
40 fClip.fIRect.fRight = SkScalarRoundToInt(rect.fRight); 90
41 fClip.fIRect.fBottom = SkScalarRoundToInt(rect.fBottom); 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 gen erate 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);
42 } 111 }
43 112
44 ~GrClip() { this->reset(); } 113 void reset(const SkClipStack* stack = nullptr, const SkIPoint* origin = null ptr) {
45 114 fOrigin = origin ? *origin : SkIPoint::Make(0, 0);
46 const GrClip& operator=(const GrClip& other) { 115 fStack.reset(SkSafeRef(stack));
47 this->reset();
48 fClipType = other.fClipType;
49 switch (other.fClipType) {
50 case kWideOpen_ClipType:
51 fOrigin.setZero();
52 break;
53 case kClipStack_ClipType:
54 fClip.fStack = SkRef(other.clipStack());
55 fOrigin = other.origin();
56 break;
57 case kIRect_ClipType:
58 fClip.fIRect = other.irect();
59 fOrigin.setZero();
60 break;
61 }
62 return *this;
63 } 116 }
64 117
65 bool operator==(const GrClip& other) const { 118 const SkIPoint& origin() const { return fOrigin; }
66 if (this->clipType() != other.clipType()) { 119 const SkClipStack* clipStack() const { return fStack; }
67 return false;
68 }
69 120
70 switch (fClipType) { 121 bool quickContains(const SkRect&) const final;
71 case kWideOpen_ClipType: 122 void getConservativeBounds(int width, int height, SkIRect* devResult,
72 return true; 123 bool* isIntersectionOfRects) const final;
73 case kClipStack_ClipType: 124 bool apply(GrClipMaskManager*, const GrPipelineBuilder&,
74 if (this->origin() != other.origin()) { 125 const SkRect* devBounds, GrAppliedClip*) const final;
75 return false;
76 }
77
78 if (this->clipStack() && other.clipStack()) {
79 return *this->clipStack() == *other.clipStack();
80 } else {
81 return this->clipStack() == other.clipStack();
82 }
83 break;
84 case kIRect_ClipType:
85 return this->irect() == other.irect();
86 break;
87 }
88 SkFAIL("This should not occur\n");
89 return false;
90 }
91
92 bool operator!=(const GrClip& other) const {
93 return !(*this == other);
94 }
95
96 const SkClipStack* clipStack() const {
97 SkASSERT(kClipStack_ClipType == fClipType);
98 return fClip.fStack;
99 }
100
101 void setClipStack(const SkClipStack* clipStack, const SkIPoint* origin = NUL L) {
102 this->reset();
103 if (clipStack->isWideOpen()) {
104 fClipType = kWideOpen_ClipType;
105 fOrigin.setZero();
106 } else {
107 fClipType = kClipStack_ClipType;
108 fClip.fStack = SkRef(clipStack);
109 if (origin) {
110 fOrigin = *origin;
111 } else {
112 fOrigin.setZero();
113 }
114 }
115 }
116
117 void setIRect(const SkIRect& irect) {
118 this->reset();
119 fClipType = kIRect_ClipType;
120 fOrigin.setZero();
121 fClip.fIRect = irect;
122 }
123
124 const SkIRect& irect() const {
125 SkASSERT(kIRect_ClipType == fClipType);
126 return fClip.fIRect;
127 }
128
129 void reset() {
130 if (kClipStack_ClipType == fClipType) {
131 fClip.fStack->unref();
132 fClip.fStack = NULL;
133 }
134 fClipType = kWideOpen_ClipType;
135 fOrigin.setZero();
136 }
137
138 // We support this for all cliptypes to simplify the logic a bit in clip mas k manager.
139 // non clipstack clip types MUST have a (0,0) origin
140 const SkIPoint& origin() const {
141 SkASSERT(fClipType == kClipStack_ClipType || (fOrigin.fX == 0 && fOrigin .fY == 0));
142 return fOrigin;
143 }
144
145 bool isWideOpen(const SkRect& rect) const {
146 return (kWideOpen_ClipType == fClipType) ||
147 (kClipStack_ClipType == fClipType && this->clipStack()->isWideOpe n()) ||
148 (kIRect_ClipType == fClipType && this->irect().contains(rect));
149 }
150
151 bool isWideOpen(const SkIRect& rect) const {
152 return (kWideOpen_ClipType == fClipType) ||
153 (kClipStack_ClipType == fClipType && this->clipStack()->isWideOpe n()) ||
154 (kIRect_ClipType == fClipType && this->irect().contains(rect));
155 }
156
157 bool isWideOpen() const {
158 return (kWideOpen_ClipType == fClipType) ||
159 (kClipStack_ClipType == fClipType && this->clipStack()->isWideOpe n());
160 }
161
162 bool quickContains(const SkRect& rect) const {
163 return (kWideOpen_ClipType == fClipType) ||
164 (kClipStack_ClipType == fClipType && this->clipStack()->quickCont ains(rect)) ||
165 (kIRect_ClipType == fClipType && this->irect().contains(rect));
166 }
167
168 void getConservativeBounds(int width, int height,
169 SkIRect* devResult,
170 bool* isIntersectionOfRects = NULL) const;
171
172 static const GrClip& WideOpen();
173
174 enum ClipType {
175 kClipStack_ClipType,
176 kWideOpen_ClipType,
177 kIRect_ClipType,
178 };
179
180 ClipType clipType() const { return fClipType; }
181 126
182 private: 127 private:
183 union Clip { 128 SkIPoint fOrigin;
184 const SkClipStack* fStack; 129 SkAutoTUnref<const SkClipStack> fStack;
185 SkIRect fIRect;
186 } fClip;
187
188 SkIPoint fOrigin;
189 ClipType fClipType;
190 }; 130 };
191 131
192 #endif 132 #endif
OLDNEW
« no previous file with comments | « gm/texdata.cpp ('k') | include/gpu/GrTypesPriv.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698