Debian Codegraph @

src/gpu/graphite/DrawParams.h

raw
1 /*
2 * Copyright 2022 Google LLC
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 skgpu_graphite_DrawParams_DEFINED
9 #define skgpu_graphite_DrawParams_DEFINED
10
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkRect.h"
13 #include "include/private/base/SkAssert.h"
14 #include "include/private/base/SkTo.h"
15 #include "src/gpu/graphite/DrawOrder.h"
16 #include "src/gpu/graphite/geom/Geometry.h"
17 #include "src/gpu/graphite/geom/NonMSAAClip.h"
18 #include "src/gpu/graphite/geom/Rect.h"
19
20 #include <algorithm>
21 #include <optional>
22
23 class SkShader;
24
25 namespace skgpu::graphite {
26 class Transform;
27
28 // NOTE: Only represents the stroke or hairline styles; stroke-and-fill must be handled higher up.
29 class StrokeStyle {
30 public:
31 StrokeStyle() : fHalfWidth(0.f), fJoinLimit(0.f), fCap(SkPaint::kButt_Cap) {}
32 StrokeStyle(float width,
33 float miterLimit,
34 SkPaint::Join join,
35 SkPaint::Cap cap)
36 : fHalfWidth(std::max(0.f, 0.5f * width))
37 , fJoinLimit(join == SkPaint::kMiter_Join ? std::max(0.f, miterLimit) :
38 (join == SkPaint::kBevel_Join ? 0.f : -1.f))
39 , fCap(cap) {}
40
41 StrokeStyle(const StrokeStyle&) = default;
42
43 StrokeStyle& operator=(const StrokeStyle&) = default;
44
45 bool isMiterJoin() const { return fJoinLimit > 0.f; }
46 bool isBevelJoin() const { return fJoinLimit == 0.f; }
47 bool isRoundJoin() const { return fJoinLimit < 0.f; }
48
49 float halfWidth() const { return fHalfWidth; }
50 float width() const { return 2.f * fHalfWidth; }
51 float miterLimit() const { return std::max(0.f, fJoinLimit); }
52 SkPaint::Cap cap() const { return fCap; }
53 SkPaint::Join join() const {
54 return fJoinLimit > 0.f ? SkPaint::kMiter_Join :
55 (fJoinLimit == 0.f ? SkPaint::kBevel_Join : SkPaint::kRound_Join);
56 }
57
58 // Raw join limit, compatible with tess::StrokeParams
59 float joinLimit() const { return fJoinLimit; }
60
61 private:
62 float fHalfWidth; // >0: relative to transform; ==0: hairline, 1px in device space
63 float fJoinLimit; // >0: miter join; ==0: bevel join; <0: round join
64 SkPaint::Cap fCap;
65 };
66
67 // TBD: Separate DashParams extracted from an SkDashPathEffect? Or folded into StrokeStyle?
68
69 class Clip {
70 public:
71 Clip() = default;
72 Clip(const Rect& drawBounds,
73 const Rect& shapeBounds,
74 const SkIRect& scissor,
75 const NonMSAAClip& nonMSAAClip,
76 const SkShader* shader)
77 : fDrawBounds(drawBounds)
78 , fTransformedShapeBounds(shapeBounds)
79 , fScissor(scissor)
80 , fNonMSAAClip(nonMSAAClip)
81 , fShader(shader) {}
82
83 // Tight bounds of the draw, including any padding/outset for stroking and expansion due to
84 // inverse fill and intersected with the scissor.
85 const Rect& drawBounds() const { return fDrawBounds; }
86
87 // The scissor rectangle obtained by restricting the bounds of the clip stack that affects the
88 // draw to the device bounds. The scissor must contain drawBounds() and must already be
89 // intersected with the device bounds.
90 const SkIRect& scissor() const { return fScissor; }
91
92 // Unclipped bounds of the shape in device space, including any padding/outset for stroking but
93 // ignoring the fill rule. This is not restricted by the scissor (or the target device's
94 // physical bounds).
95 //
96 // For a regular fill, drawBounds() is the intersection of this rectangle and scissor().
97 //
98 // For an inverse fill, this is the bounding box of the interesting portion of any coverage
99 // mask. If it doesn't intersect the scissor, the draw fully covers the scissor; regardless the
100 // drawBounds() are equal to the scissor.
101 const Rect& transformedShapeBounds() const { return fTransformedShapeBounds; }
102
103 // If set, the shape's bounds and/or an atlas mask are further used to clip the draw.
104 // NOTE: This cannot impact `drawBounds()` as pixels outside of the non-msaa clip may still be
105 // shaded and blended with a coverage value of 0, which could lead to undefined behavior on the
106 // GPU if operations were ordered assuming tighter bounds.
107 const NonMSAAClip& nonMSAAClip() const { return fNonMSAAClip; }
108
109 // If set, the clip shader's output alpha is further used to clip the draw.
110 const SkShader* shader() const { return fShader; }
111
112 bool isClippedOut() const { return fDrawBounds.isEmptyNegativeOrNaN(); }
113
114 bool needsCoverage() const { return SkToBool(fShader) || !fNonMSAAClip.isEmpty(); }
115
116 void outsetBoundsForAA() {
117 // We use 1px to handle both subpixel/hairline approaches and the standard 1/2px outset
118 // for shapes that cover multiple pixels.
119 fTransformedShapeBounds.outset(1.f);
120 // This is a no-op for inverse fills (where fDrawBounds was already equal to fScissor),
121 // and equivalent to fDrawBounds = fTransformedShapeBounds.makeIntersect(fScissor) with
122 // the outset shape bounds.
123 fDrawBounds.outset(1.f).intersect(fScissor);
124 }
125
126 private:
127 // DrawList assumes the DrawBounds are correct for a given shape, transform, and style. They
128 // are provided to the DrawList to avoid re-calculating the same bounds.
129 Rect fDrawBounds;
130 Rect fTransformedShapeBounds;
131 SkIRect fScissor;
132 NonMSAAClip fNonMSAAClip;
133 const SkShader* fShader;
134 };
135
136 // Encapsulates all geometric state for a single high-level draw call. RenderSteps are responsible
137 // for transforming this state into actual rendering; shading from PaintParams is handled separately
138 class DrawParams {
139 public:
140 DrawParams(const Transform& transform,
141 const Geometry& geometry,
142 const Clip& clip,
143 DrawOrder drawOrder,
144 const StrokeStyle* stroke)
145 : fTransform(transform)
146 , fGeometry(geometry)
147 , fDrawBounds(clip.drawBounds())
148 , fTransformedShapeBounds(clip.transformedShapeBounds())
149 , fScissor(clip.scissor())
150 , fOrder(drawOrder)
151 , fStroke(stroke ? std::optional<StrokeStyle>(*stroke) : std::nullopt) {}
152
153 const Transform& transform() const { return fTransform; }
154 const Geometry& geometry() const { return fGeometry; }
155 DrawOrder order() const { return fOrder; }
156
157 // The subset of a Clip's state that is preserved in a DrawList, whereas the other properties
158 // of a clip get consumed into the paint's key and uniform data.
159 Rect drawBounds() const { return fDrawBounds; }
160 Rect transformedShapeBounds() const { return fTransformedShapeBounds; }
161 const SkIRect& scissor() const { return fScissor; }
162
163 // Optional stroke parameters if the geometry is stroked instead of filled
164 bool isStroke() const { return fStroke.has_value(); }
165 const StrokeStyle& strokeStyle() const {
166 SkASSERT(this->isStroke());
167 return *fStroke;
168 }
169
170 private:
171 const Transform& fTransform; // Lifetime of the transform must be held longer than the geometry
172
173 Geometry fGeometry;
174 Rect fDrawBounds;
175 Rect fTransformedShapeBounds;
176 SkIRect fScissor;
177 DrawOrder fOrder;
178
179 std::optional<StrokeStyle> fStroke; // Not present implies fill
180 };
181
182 } // namespace skgpu::graphite
183
184 #endif // skgpu_graphite_DrawParams_DEFINED