1 /*
2  * Copyright 2016 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 GrAppliedClip_DEFINED
9 #define GrAppliedClip_DEFINED
10 
11 #include "GrScissorState.h"
12 #include "GrWindowRectsState.h"
13 
14 class GrFragmentProcessor;
15 
16 /**
17  * Produced by GrClip. It provides a set of modifications to the drawing state that are used to
18  * create the final GrPipeline for a GrBatch.
19  */
20 class GrAppliedClip : public SkNoncopyable {
21 public:
GrAppliedClip(const SkRect & drawBounds)22     GrAppliedClip(const SkRect& drawBounds)
23         : fHasStencilClip(false)
24         , fClippedDrawBounds(drawBounds) {
25     }
26 
scissorState()27     const GrScissorState& scissorState() const { return fScissorState; }
windowRectsState()28     const GrWindowRectsState& windowRectsState() const { return fWindowRectsState; }
clipCoverageFragmentProcessor()29     GrFragmentProcessor* clipCoverageFragmentProcessor() const { return fClipCoverageFP.get(); }
hasStencilClip()30     bool hasStencilClip() const { return fHasStencilClip; }
31 
32     /**
33      * Intersects the applied clip with the provided rect. Returns false if the draw became empty.
34      */
addScissor(const SkIRect & irect)35     bool addScissor(const SkIRect& irect) {
36         return fScissorState.intersect(irect) && fClippedDrawBounds.intersect(SkRect::Make(irect));
37     }
38 
addWindowRectangles(const GrWindowRectsState & windowState)39     void addWindowRectangles(const GrWindowRectsState& windowState) {
40         SkASSERT(!fWindowRectsState.enabled());
41         fWindowRectsState = windowState;
42     }
43 
addWindowRectangles(const GrWindowRectangles & windows,const SkIPoint & origin,GrWindowRectsState::Mode mode)44     void addWindowRectangles(const GrWindowRectangles& windows, const SkIPoint& origin,
45                              GrWindowRectsState::Mode mode) {
46         SkASSERT(!fWindowRectsState.enabled());
47         fWindowRectsState.set(windows, origin, mode);
48     }
49 
addCoverageFP(sk_sp<GrFragmentProcessor> fp)50     void addCoverageFP(sk_sp<GrFragmentProcessor> fp) {
51         SkASSERT(!fClipCoverageFP);
52         fClipCoverageFP = fp;
53     }
54 
addStencilClip()55     void addStencilClip() {
56         SkASSERT(!fHasStencilClip);
57         fHasStencilClip = true;
58     }
59 
60     /**
61      * Returns the device bounds of the draw after clip has been applied. TODO: Ideally this would
62      * consider the combined effect of all clipping techniques in play (scissor, stencil, fp, etc.).
63      */
clippedDrawBounds()64     const SkRect& clippedDrawBounds() const { return fClippedDrawBounds; }
65 
66 private:
67     GrScissorState             fScissorState;
68     GrWindowRectsState         fWindowRectsState;
69     sk_sp<GrFragmentProcessor> fClipCoverageFP;
70     bool                       fHasStencilClip;
71     SkRect                     fClippedDrawBounds;
72     typedef SkNoncopyable INHERITED;
73 };
74 
75 #endif
76