blob: 6a8bc9efdfdf9f24f622b97056b23a501793162d [file] [log] [blame]
bsalomon@google.com170bd792012-12-05 22:26:11 +00001/*
csmartdalton77f2fae2016-08-08 09:55:06 -07002 * Copyright 2016 Google Inc.
bsalomon@google.com170bd792012-12-05 22:26:11 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
george9eb182a2014-06-20 12:01:06 -07008#ifndef GrReducedClip_DEFINED
9#define GrReducedClip_DEFINED
10
Chris Dalton584a79a2017-11-15 13:14:01 -070011#include "GrFragmentProcessor.h"
csmartdaltonbf4a8f92016-09-06 10:01:06 -070012#include "GrWindowRectangles.h"
bsalomon@google.com170bd792012-12-05 22:26:11 +000013#include "SkClipStack.h"
14#include "SkTLList.h"
15
csmartdaltonbde96c62016-08-31 12:54:46 -070016class GrContext;
Chris Daltona32a3c32017-12-05 10:05:21 -070017class GrCoverageCountingPathRenderer;
Brian Osman11052242016-10-27 14:47:55 -040018class GrRenderTargetContext;
csmartdaltonbde96c62016-08-31 12:54:46 -070019
csmartdalton77f2fae2016-08-08 09:55:06 -070020/**
21 * This class takes a clip stack and produces a reduced set of elements that are equivalent to
22 * applying that full stack within a specified query rectangle.
23 */
tfarinabf54e492014-10-23 17:47:18 -070024class SK_API GrReducedClip {
25public:
Brian Salomonc3833b42018-07-09 18:23:58 +000026 using Element = SkClipStack::Element;
27 using ElementList = SkTLList<SkClipStack::Element, 16>;
Chris Dalton79471932017-10-27 01:50:57 -060028
Ethan Nicholas222e2752018-10-11 11:21:34 -040029 GrReducedClip(const SkClipStack&, const SkRect& queryBounds, GrContext* context,
30 int maxWindowRectangles = 0, int maxAnalyticFPs = 0,
31 int maxCCPRClipPaths = 0);
Chris Daltona32a3c32017-12-05 10:05:21 -070032
33 enum class InitialState : bool {
34 kAllIn,
35 kAllOut
36 };
37
38 InitialState initialState() const { return fInitialState; }
csmartdalton77f2fae2016-08-08 09:55:06 -070039
40 /**
Chris Dalton79471932017-10-27 01:50:57 -060041 * If hasScissor() is true, the clip mask is not valid outside this rect and the caller must
42 * enforce this scissor during draw.
csmartdalton77f2fae2016-08-08 09:55:06 -070043 */
Chris Dalton79471932017-10-27 01:50:57 -060044 const SkIRect& scissor() const { SkASSERT(fHasScissor); return fScissor; }
45 int left() const { return this->scissor().left(); }
46 int top() const { return this->scissor().top(); }
47 int width() const { return this->scissor().width(); }
48 int height() const { return this->scissor().height(); }
csmartdaltond211e782016-08-15 11:17:19 -070049
50 /**
Chris Dalton79471932017-10-27 01:50:57 -060051 * Indicates whether scissor() is defined. It will always be defined if the maskElements() are
csmartdaltond211e782016-08-15 11:17:19 -070052 * nonempty.
53 */
Chris Dalton79471932017-10-27 01:50:57 -060054 bool hasScissor() const { return fHasScissor; }
csmartdalton77f2fae2016-08-08 09:55:06 -070055
csmartdaltonbf4a8f92016-09-06 10:01:06 -070056 /**
Chris Dalton79471932017-10-27 01:50:57 -060057 * If nonempty, the clip mask is not valid inside these windows and the caller must clip them
58 * out using the window rectangles GPU extension.
csmartdaltonbf4a8f92016-09-06 10:01:06 -070059 */
60 const GrWindowRectangles& windowRectangles() const { return fWindowRects; }
61
Chris Dalton79471932017-10-27 01:50:57 -060062 /**
63 * An ordered list of clip elements that could not be skipped or implemented by other means. If
64 * nonempty, the caller must create an alpha and/or stencil mask for these elements and apply it
65 * during draw.
66 */
67 const ElementList& maskElements() const { return fMaskElements; }
bsalomon@google.com170bd792012-12-05 22:26:11 +000068
csmartdalton77f2fae2016-08-08 09:55:06 -070069 /**
Brian Salomonc3833b42018-07-09 18:23:58 +000070 * If maskElements() are nonempty, uniquely identifies the region of the clip mask that falls
71 * inside of scissor().
72 *
73 * NOTE: since clip elements might fall outside the query bounds, different regions of the same
74 * clip stack might have more or less restrictive IDs.
75 *
76 * FIXME: this prevents us from reusing a sub-rect of a perfectly good mask when that rect has
77 * been assigned a less restrictive ID.
csmartdalton77f2fae2016-08-08 09:55:06 -070078 */
Brian Salomonc3833b42018-07-09 18:23:58 +000079 uint32_t maskGenID() const { SkASSERT(!fMaskElements.isEmpty()); return fMaskGenID; }
csmartdalton77f2fae2016-08-08 09:55:06 -070080
81 /**
Chris Dalton79471932017-10-27 01:50:57 -060082 * Indicates whether antialiasing is required to process any of the mask elements.
csmartdalton8d3f92a2016-08-17 09:39:38 -070083 */
Chris Dalton79471932017-10-27 01:50:57 -060084 bool maskRequiresAA() const { SkASSERT(!fMaskElements.isEmpty()); return fMaskRequiresAA; }
csmartdalton77f2fae2016-08-08 09:55:06 -070085
Chris Daltonc5348082018-03-30 15:59:38 +000086 bool drawAlphaClipMask(GrRenderTargetContext*) const;
Ethan Nicholas222e2752018-10-11 11:21:34 -040087 bool drawStencilClipMask(GrRenderTargetContext*) const;
csmartdaltonbde96c62016-08-31 12:54:46 -070088
Chris Daltona32a3c32017-12-05 10:05:21 -070089 int numAnalyticFPs() const { return fAnalyticFPs.count() + fCCPRClipPaths.count(); }
90
91 /**
92 * Called once the client knows the ID of the opList that the clip FPs will operate in. This
93 * method finishes any outstanding work that was waiting for the opList ID, then detaches and
94 * returns this class's list of FPs that complete the clip.
95 *
96 * NOTE: this must be called AFTER producing the clip mask (if any) because draw calls on
97 * the render target context, surface allocations, and even switching render targets (pre MDB)
98 * may cause flushes or otherwise change which opList the actual draw is going into.
99 */
Chris Dalton1dec19a2018-04-27 13:05:19 -0600100 std::unique_ptr<GrFragmentProcessor> finishAndDetachAnalyticFPs(GrCoverageCountingPathRenderer*,
Chris Dalton4c458b12018-06-16 17:22:59 -0600101 uint32_t opListID, int rtWidth,
102 int rtHeight);
Chris Daltona32a3c32017-12-05 10:05:21 -0700103
csmartdalton77f2fae2016-08-08 09:55:06 -0700104private:
Chris Dalton584a79a2017-11-15 13:14:01 -0700105 void walkStack(const SkClipStack&, const SkRect& queryBounds);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700106
Chris Dalton79471932017-10-27 01:50:57 -0600107 enum class ClipResult {
108 kNotClipped,
109 kClipped,
110 kMadeEmpty
111 };
112
Chris Daltona32a3c32017-12-05 10:05:21 -0700113 // Intersects the clip with the element's interior, regardless of inverse fill type.
Chris Dalton79471932017-10-27 01:50:57 -0600114 // NOTE: do not call for elements followed by ops that can grow the clip.
Brian Salomonc3833b42018-07-09 18:23:58 +0000115 ClipResult clipInsideElement(const Element*);
Chris Dalton79471932017-10-27 01:50:57 -0600116
Chris Daltona32a3c32017-12-05 10:05:21 -0700117 // Intersects the clip with the element's exterior, regardless of inverse fill type.
Chris Dalton79471932017-10-27 01:50:57 -0600118 // NOTE: do not call for elements followed by ops that can grow the clip.
Brian Salomonc3833b42018-07-09 18:23:58 +0000119 ClipResult clipOutsideElement(const Element*);
Chris Dalton79471932017-10-27 01:50:57 -0600120
121 void addWindowRectangle(const SkRect& elementInteriorRect, bool elementIsAA);
Chris Dalton584a79a2017-11-15 13:14:01 -0700122
123 enum class Invert : bool {
Chris Dalton3b51df12017-11-27 14:33:06 -0700124 kNo = false,
125 kYes = true
Chris Dalton584a79a2017-11-15 13:14:01 -0700126 };
127
Chris Daltona32a3c32017-12-05 10:05:21 -0700128 static GrClipEdgeType GetClipEdgeType(Invert, GrAA);
129 ClipResult addAnalyticFP(const SkRect& deviceSpaceRect, Invert, GrAA);
130 ClipResult addAnalyticFP(const SkRRect& deviceSpaceRRect, Invert, GrAA);
131 ClipResult addAnalyticFP(const SkPath& deviceSpacePath, Invert, GrAA);
Chris Dalton584a79a2017-11-15 13:14:01 -0700132
Chris Dalton79471932017-10-27 01:50:57 -0600133 void makeEmpty();
134
Ethan Nicholas222e2752018-10-11 11:21:34 -0400135 GrContext* fContext;
Chris Dalton584a79a2017-11-15 13:14:01 -0700136 const int fMaxWindowRectangles;
137 const int fMaxAnalyticFPs;
Chris Dalton1dec19a2018-04-27 13:05:19 -0600138 const int fMaxCCPRClipPaths;
Chris Daltona32a3c32017-12-05 10:05:21 -0700139
140 InitialState fInitialState;
Chris Dalton584a79a2017-11-15 13:14:01 -0700141 SkIRect fScissor;
142 bool fHasScissor;
143 SkRect fAAClipRect;
Brian Salomonc3833b42018-07-09 18:23:58 +0000144 uint32_t fAAClipRectGenID; // GenID the mask will have if includes the AA clip rect.
Chris Dalton584a79a2017-11-15 13:14:01 -0700145 GrWindowRectangles fWindowRects;
Chris Dalton584a79a2017-11-15 13:14:01 -0700146 ElementList fMaskElements;
Brian Salomonc3833b42018-07-09 18:23:58 +0000147 uint32_t fMaskGenID;
Chris Dalton584a79a2017-11-15 13:14:01 -0700148 bool fMaskRequiresAA;
Chris Daltona32a3c32017-12-05 10:05:21 -0700149 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> fAnalyticFPs;
150 SkSTArray<4, SkPath> fCCPRClipPaths; // Will convert to FPs once we have an opList ID for CCPR.
bsalomon@google.com170bd792012-12-05 22:26:11 +0000151};
152
george9eb182a2014-06-20 12:01:06 -0700153#endif