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 12 #include "include/core/SkPaint.h" 13 #include "include/core/SkRect.h" 14 #include "src/gpu/graphite/DrawOrder.h" 15 #include "src/gpu/graphite/geom/Geometry.h" 16 #include "src/gpu/graphite/geom/Rect.h" 17 #include "src/gpu/graphite/geom/Transform_graphite.h" 18 19 #include <optional> 20 21 namespace skgpu::graphite { 22 23 // NOTE: Only represents the stroke or hairline styles; stroke-and-fill must be handled higher up. 24 class StrokeStyle { 25 public: StrokeStyle()26 StrokeStyle() : fHalfWidth(0.f), fJoinLimit(0.f), fCap(SkPaint::kButt_Cap) {} StrokeStyle(float width,float miterLimit,SkPaint::Join join,SkPaint::Cap cap)27 StrokeStyle(float width, 28 float miterLimit, 29 SkPaint::Join join, 30 SkPaint::Cap cap) 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 isMiterJoin()40 bool isMiterJoin() const { return fJoinLimit > 0.f; } isBevelJoin()41 bool isBevelJoin() const { return fJoinLimit == 0.f; } isRoundJoin()42 bool isRoundJoin() const { return fJoinLimit < 0.f; } 43 halfWidth()44 float halfWidth() const { return fHalfWidth; } width()45 float width() const { return 2.f * fHalfWidth; } miterLimit()46 float miterLimit() const { return std::max(0.f, fJoinLimit); } cap()47 SkPaint::Cap cap() const { return fCap; } join()48 SkPaint::Join join() const { 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 joinLimit()54 float joinLimit() const { return fJoinLimit; } 55 56 private: 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 64 class Clip { 65 public: 66 Clip() = default; Clip(const Rect & drawBounds,const Rect & shapeBounds,const SkIRect & scissor,const SkShader * shader)67 Clip(const Rect& drawBounds, 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. drawBounds()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. scissor()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(). transformedShapeBounds()88 const Rect& transformedShapeBounds() const { return fTransformedShapeBounds; } 89 90 // If set, the clip shader's output alpha is further used to clip the draw. shader()91 const SkShader* shader() const { return fShader; } 92 isClippedOut()93 bool isClippedOut() const { return fDrawBounds.isEmptyNegativeOrNaN(); } 94 95 private: 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 109 class DrawParams { 110 public: DrawParams(const Transform & transform,const Geometry & geometry,const Clip & clip,DrawOrder drawOrder,const StrokeStyle * stroke)111 DrawParams(const Transform& transform, 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 transform()122 const Transform& transform() const { return fTransform; } geometry()123 const Geometry& geometry() const { return fGeometry; } clip()124 const Clip& clip() const { return fClip; } order()125 DrawOrder order() const { return fOrder; } 126 127 // Optional stroke parameters if the geometry is stroked instead of filled isStroke()128 bool isStroke() const { return fStroke.has_value(); } strokeStyle()129 const StrokeStyle& strokeStyle() const { 130 SkASSERT(this->isStroke()); 131 return *fStroke; 132 } 133 134 private: 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 147