blob: ca95bd09949e6978d56f537f47f2cfe99c879cd7 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/core/SkClipStack.h"
12#include "src/core/SkTLList.h"
13#include "src/gpu/GrFragmentProcessor.h"
14#include "src/gpu/GrWindowRectangles.h"
bsalomon@google.com170bd792012-12-05 22:26:11 +000015
Chris Daltona32a3c32017-12-05 10:05:21 -070016class GrCoverageCountingPathRenderer;
Robert Phillips6f0e02f2019-02-13 11:02:28 -050017class GrRecordingContext;
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 */
Brian Salomon57f211b2019-08-21 15:21:09 -040024class GrReducedClip {
tfarinabf54e492014-10-23 17:47:18 -070025public:
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 Nicholaseace9352018-10-15 20:09:54 +000029 GrReducedClip(const SkClipStack&, const SkRect& queryBounds, const GrCaps* caps,
30 int maxWindowRectangles = 0, int maxAnalyticFPs = 0, int maxCCPRClipPaths = 0);
Chris Daltona32a3c32017-12-05 10:05:21 -070031
32 enum class InitialState : bool {
33 kAllIn,
34 kAllOut
35 };
36
37 InitialState initialState() const { return fInitialState; }
csmartdalton77f2fae2016-08-08 09:55:06 -070038
39 /**
Chris Dalton79471932017-10-27 01:50:57 -060040 * If hasScissor() is true, the clip mask is not valid outside this rect and the caller must
41 * enforce this scissor during draw.
csmartdalton77f2fae2016-08-08 09:55:06 -070042 */
Chris Dalton79471932017-10-27 01:50:57 -060043 const SkIRect& scissor() const { SkASSERT(fHasScissor); return fScissor; }
44 int left() const { return this->scissor().left(); }
45 int top() const { return this->scissor().top(); }
46 int width() const { return this->scissor().width(); }
47 int height() const { return this->scissor().height(); }
csmartdaltond211e782016-08-15 11:17:19 -070048
49 /**
Chris Dalton79471932017-10-27 01:50:57 -060050 * Indicates whether scissor() is defined. It will always be defined if the maskElements() are
csmartdaltond211e782016-08-15 11:17:19 -070051 * nonempty.
52 */
Chris Dalton79471932017-10-27 01:50:57 -060053 bool hasScissor() const { return fHasScissor; }
csmartdalton77f2fae2016-08-08 09:55:06 -070054
csmartdaltonbf4a8f92016-09-06 10:01:06 -070055 /**
Chris Dalton79471932017-10-27 01:50:57 -060056 * If nonempty, the clip mask is not valid inside these windows and the caller must clip them
57 * out using the window rectangles GPU extension.
csmartdaltonbf4a8f92016-09-06 10:01:06 -070058 */
59 const GrWindowRectangles& windowRectangles() const { return fWindowRects; }
60
Chris Dalton79471932017-10-27 01:50:57 -060061 /**
62 * An ordered list of clip elements that could not be skipped or implemented by other means. If
63 * nonempty, the caller must create an alpha and/or stencil mask for these elements and apply it
64 * during draw.
65 */
66 const ElementList& maskElements() const { return fMaskElements; }
bsalomon@google.com170bd792012-12-05 22:26:11 +000067
csmartdalton77f2fae2016-08-08 09:55:06 -070068 /**
Brian Salomonc3833b42018-07-09 18:23:58 +000069 * If maskElements() are nonempty, uniquely identifies the region of the clip mask that falls
70 * inside of scissor().
71 *
72 * NOTE: since clip elements might fall outside the query bounds, different regions of the same
73 * clip stack might have more or less restrictive IDs.
74 *
75 * FIXME: this prevents us from reusing a sub-rect of a perfectly good mask when that rect has
76 * been assigned a less restrictive ID.
csmartdalton77f2fae2016-08-08 09:55:06 -070077 */
Brian Salomonc3833b42018-07-09 18:23:58 +000078 uint32_t maskGenID() const { SkASSERT(!fMaskElements.isEmpty()); return fMaskGenID; }
csmartdalton77f2fae2016-08-08 09:55:06 -070079
80 /**
Chris Dalton79471932017-10-27 01:50:57 -060081 * Indicates whether antialiasing is required to process any of the mask elements.
csmartdalton8d3f92a2016-08-17 09:39:38 -070082 */
Chris Dalton79471932017-10-27 01:50:57 -060083 bool maskRequiresAA() const { SkASSERT(!fMaskElements.isEmpty()); return fMaskRequiresAA; }
csmartdalton77f2fae2016-08-08 09:55:06 -070084
Chris Daltonc5348082018-03-30 15:59:38 +000085 bool drawAlphaClipMask(GrRenderTargetContext*) const;
Robert Phillips6f0e02f2019-02-13 11:02:28 -050086 bool drawStencilClipMask(GrRecordingContext*, GrRenderTargetContext*) const;
csmartdaltonbde96c62016-08-31 12:54:46 -070087
Chris Daltona32a3c32017-12-05 10:05:21 -070088 int numAnalyticFPs() const { return fAnalyticFPs.count() + fCCPRClipPaths.count(); }
89
90 /**
Greg Danielf41b2bd2019-08-22 16:19:24 -040091 * Called once the client knows the ID of the opsTask that the clip FPs will operate in. This
92 * method finishes any outstanding work that was waiting for the opsTask ID, then detaches and
Chris Daltona32a3c32017-12-05 10:05:21 -070093 * returns this class's list of FPs that complete the clip.
94 *
95 * NOTE: this must be called AFTER producing the clip mask (if any) because draw calls on
96 * the render target context, surface allocations, and even switching render targets (pre MDB)
Greg Danielf41b2bd2019-08-22 16:19:24 -040097 * may cause flushes or otherwise change which opsTask the actual draw is going into.
Chris Daltona32a3c32017-12-05 10:05:21 -070098 */
Chris Daltonc3318f02019-07-19 14:20:53 -060099 std::unique_ptr<GrFragmentProcessor> finishAndDetachAnalyticFPs(
Greg Danielf41b2bd2019-08-22 16:19:24 -0400100 GrCoverageCountingPathRenderer*, uint32_t opsTaskID);
Chris Daltona32a3c32017-12-05 10:05:21 -0700101
csmartdalton77f2fae2016-08-08 09:55:06 -0700102private:
Chris Dalton584a79a2017-11-15 13:14:01 -0700103 void walkStack(const SkClipStack&, const SkRect& queryBounds);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700104
Chris Dalton79471932017-10-27 01:50:57 -0600105 enum class ClipResult {
106 kNotClipped,
107 kClipped,
108 kMadeEmpty
109 };
110
Chris Daltona32a3c32017-12-05 10:05:21 -0700111 // Intersects the clip with the element's interior, regardless of inverse fill type.
Chris Dalton79471932017-10-27 01:50:57 -0600112 // NOTE: do not call for elements followed by ops that can grow the clip.
Brian Salomonc3833b42018-07-09 18:23:58 +0000113 ClipResult clipInsideElement(const Element*);
Chris Dalton79471932017-10-27 01:50:57 -0600114
Chris Daltona32a3c32017-12-05 10:05:21 -0700115 // Intersects the clip with the element's exterior, regardless of inverse fill type.
Chris Dalton79471932017-10-27 01:50:57 -0600116 // NOTE: do not call for elements followed by ops that can grow the clip.
Brian Salomonc3833b42018-07-09 18:23:58 +0000117 ClipResult clipOutsideElement(const Element*);
Chris Dalton79471932017-10-27 01:50:57 -0600118
119 void addWindowRectangle(const SkRect& elementInteriorRect, bool elementIsAA);
Chris Dalton584a79a2017-11-15 13:14:01 -0700120
121 enum class Invert : bool {
Chris Dalton3b51df12017-11-27 14:33:06 -0700122 kNo = false,
123 kYes = true
Chris Dalton584a79a2017-11-15 13:14:01 -0700124 };
125
Chris Daltona32a3c32017-12-05 10:05:21 -0700126 static GrClipEdgeType GetClipEdgeType(Invert, GrAA);
127 ClipResult addAnalyticFP(const SkRect& deviceSpaceRect, Invert, GrAA);
128 ClipResult addAnalyticFP(const SkRRect& deviceSpaceRRect, Invert, GrAA);
129 ClipResult addAnalyticFP(const SkPath& deviceSpacePath, Invert, GrAA);
Chris Dalton584a79a2017-11-15 13:14:01 -0700130
Chris Dalton79471932017-10-27 01:50:57 -0600131 void makeEmpty();
132
Ethan Nicholaseace9352018-10-15 20:09:54 +0000133 const GrCaps* fCaps;
Chris Dalton584a79a2017-11-15 13:14:01 -0700134 const int fMaxWindowRectangles;
135 const int fMaxAnalyticFPs;
Chris Dalton1dec19a2018-04-27 13:05:19 -0600136 const int fMaxCCPRClipPaths;
Chris Daltona32a3c32017-12-05 10:05:21 -0700137
138 InitialState fInitialState;
Chris Dalton584a79a2017-11-15 13:14:01 -0700139 SkIRect fScissor;
140 bool fHasScissor;
141 SkRect fAAClipRect;
Brian Salomonc3833b42018-07-09 18:23:58 +0000142 uint32_t fAAClipRectGenID; // GenID the mask will have if includes the AA clip rect.
Chris Dalton584a79a2017-11-15 13:14:01 -0700143 GrWindowRectangles fWindowRects;
Chris Dalton584a79a2017-11-15 13:14:01 -0700144 ElementList fMaskElements;
Brian Salomonc3833b42018-07-09 18:23:58 +0000145 uint32_t fMaskGenID;
Chris Dalton584a79a2017-11-15 13:14:01 -0700146 bool fMaskRequiresAA;
Chris Daltona32a3c32017-12-05 10:05:21 -0700147 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> fAnalyticFPs;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400148 SkSTArray<4, SkPath> fCCPRClipPaths; // Will convert to FPs once we have an opsTask ID for CCPR.
bsalomon@google.com170bd792012-12-05 22:26:11 +0000149};
150
george9eb182a2014-06-20 12:01:06 -0700151#endif