Flutter Engine
The Flutter Engine
DrawParams.h
Go to the documentation of this file.
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
13#include "include/core/SkRect.h"
18
19#include <optional>
20
21namespace skgpu::graphite {
22
23// NOTE: Only represents the stroke or hairline styles; stroke-and-fill must be handled higher up.
25public:
26 StrokeStyle() : fHalfWidth(0.f), fJoinLimit(0.f), fCap(SkPaint::kButt_Cap) {}
28 float miterLimit,
31 : fHalfWidth(std::max(0.f, 0.5f * width))
32 , fJoinLimit(join == SkPaint::kMiter_Join ? std::max(0.f, miterLimit) :
33 (join == SkPaint::kBevel_Join ? 0.f : -1.f))
34 , fCap(cap) {}
35
36 StrokeStyle(const StrokeStyle&) = default;
37
38 StrokeStyle& operator=(const StrokeStyle&) = default;
39
40 bool isMiterJoin() const { return fJoinLimit > 0.f; }
41 bool isBevelJoin() const { return fJoinLimit == 0.f; }
42 bool isRoundJoin() const { return fJoinLimit < 0.f; }
43
44 float halfWidth() const { return fHalfWidth; }
45 float width() const { return 2.f * fHalfWidth; }
46 float miterLimit() const { return std::max(0.f, fJoinLimit); }
47 SkPaint::Cap cap() const { return fCap; }
49 return fJoinLimit > 0.f ? SkPaint::kMiter_Join :
50 (fJoinLimit == 0.f ? SkPaint::kBevel_Join : SkPaint::kRound_Join);
51 }
52
53 // Raw join limit, compatible with tess::StrokeParams
54 float joinLimit() const { return fJoinLimit; }
55
56private:
57 float fHalfWidth; // >0: relative to transform; ==0: hairline, 1px in device space
58 float fJoinLimit; // >0: miter join; ==0: bevel join; <0: round join
59 SkPaint::Cap fCap;
60};
61
62// TBD: Separate DashParams extracted from an SkDashPathEffect? Or folded into StrokeStyle?
63
64class Clip {
65public:
66 Clip() = default;
68 const Rect& shapeBounds,
69 const SkIRect& scissor,
70 const SkShader* shader)
71 : fDrawBounds(drawBounds)
72 , fTransformedShapeBounds(shapeBounds)
73 , fScissor(scissor)
74 , fShader(shader) {}
75
76 // Tight bounds of the draw, including any padding/outset for stroking and expansion due to
77 // inverse fill and intersected with the scissor.
78 const Rect& drawBounds() const { return fDrawBounds; }
79
80 // The scissor rectangle obtained by restricting the bounds of the clip stack that affects the
81 // draw to the device bounds. The scissor must contain drawBounds() and must already be
82 // intersected with the device bounds.
83 const SkIRect& scissor() const { return fScissor; }
84
85 // Clipped bounds of the shape in device space, including any padding/outset for stroking,
86 // intersected with the scissor and ignoring the fill rule. For a regular fill this is identical
87 // to drawBounds(). For an inverse fill, this is a subset of drawBounds().
88 const Rect& transformedShapeBounds() const { return fTransformedShapeBounds; }
89
90 // If set, the clip shader's output alpha is further used to clip the draw.
91 const SkShader* shader() const { return fShader; }
92
93 bool isClippedOut() const { return fDrawBounds.isEmptyNegativeOrNaN(); }
94
95private:
96 // DrawList assumes the DrawBounds are correct for a given shape, transform, and style. They
97 // are provided to the DrawList to avoid re-calculating the same bounds.
98 Rect fDrawBounds;
99 Rect fTransformedShapeBounds;
100 SkIRect fScissor;
101 const SkShader* fShader;
102
103 // TODO: If we add more complex analytic shapes for clipping, e.g. coverage rrect, it should
104 // go here.
105};
106
107// Encapsulates all geometric state for a single high-level draw call. RenderSteps are responsible
108// for transforming this state into actual rendering; shading from PaintParams is handled separately
110public:
112 const Geometry& geometry,
113 const Clip& clip,
114 DrawOrder drawOrder,
115 const StrokeStyle* stroke)
116 : fTransform(transform)
117 , fGeometry(geometry)
118 , fClip(clip)
119 , fOrder(drawOrder)
120 , fStroke(stroke ? std::optional<StrokeStyle>(*stroke) : std::nullopt) {}
121
122 const Transform& transform() const { return fTransform; }
123 const Geometry& geometry() const { return fGeometry; }
124 const Clip& clip() const { return fClip; }
125 DrawOrder order() const { return fOrder; }
126
127 // Optional stroke parameters if the geometry is stroked instead of filled
128 bool isStroke() const { return fStroke.has_value(); }
129 const StrokeStyle& strokeStyle() const {
130 SkASSERT(this->isStroke());
131 return *fStroke;
132 }
133
134private:
135 const Transform& fTransform; // Lifetime of the transform must be held longer than the geometry
136
137 Geometry fGeometry;
138 Clip fClip;
139 DrawOrder fOrder;
140
141 std::optional<StrokeStyle> fStroke; // Not present implies fill
142};
143
144} // namespace skgpu::graphite
145
146#endif // skgpu_graphite_DrawParams_DEFINED
#define SkASSERT(cond)
Definition: SkAssert.h:116
@ kRound_Join
adds circle
Definition: SkPaint.h:360
@ kMiter_Join
extends to miter limit
Definition: SkPaint.h:359
@ kBevel_Join
connects outside edges
Definition: SkPaint.h:361
const Rect & drawBounds() const
Definition: DrawParams.h:78
const SkShader * shader() const
Definition: DrawParams.h:91
const SkIRect & scissor() const
Definition: DrawParams.h:83
bool isClippedOut() const
Definition: DrawParams.h:93
const Rect & transformedShapeBounds() const
Definition: DrawParams.h:88
Clip(const Rect &drawBounds, const Rect &shapeBounds, const SkIRect &scissor, const SkShader *shader)
Definition: DrawParams.h:67
const Clip & clip() const
Definition: DrawParams.h:124
const Transform & transform() const
Definition: DrawParams.h:122
DrawParams(const Transform &transform, const Geometry &geometry, const Clip &clip, DrawOrder drawOrder, const StrokeStyle *stroke)
Definition: DrawParams.h:111
DrawOrder order() const
Definition: DrawParams.h:125
const Geometry & geometry() const
Definition: DrawParams.h:123
const StrokeStyle & strokeStyle() const
Definition: DrawParams.h:129
AI bool isEmptyNegativeOrNaN() const
Definition: Rect.h:102
StrokeStyle(float width, float miterLimit, SkPaint::Join join, SkPaint::Cap cap)
Definition: DrawParams.h:27
SkPaint::Join join() const
Definition: DrawParams.h:48
SkPaint::Cap cap() const
Definition: DrawParams.h:47
StrokeStyle & operator=(const StrokeStyle &)=default
StrokeStyle(const StrokeStyle &)=default
static float max(float r, float g, float b)
Definition: hsl.cpp:49
Definition: ref_ptr.h:256
Definition: SkRect.h:32