Flutter Engine
The Flutter Engine
DrawList.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 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_DrawList_DEFINED
9#define skgpu_graphite_DrawList_DEFINED
10
13
20
21#include <limits>
22#include <optional>
23
24namespace skgpu::graphite {
25
26class Renderer;
27
28/**
29 * A DrawList represents a collection of drawing commands (and related clip/shading state) in
30 * a form that closely mirrors what can be rendered efficiently and directly by the GPU backend
31 * (while balancing how much pre-processing to do for draws that might get eliminated later due to
32 * occlusion culling).
33 *
34 * A draw command combines:
35 * - a shape
36 * - a transform
37 * - a primitive clip (not affected by the transform)
38 * - optional shading description (shader, color filter, blend mode, etc)
39 * - a draw ordering (compressed painters index, stencil set, and write/test depth)
40 *
41 * Commands are accumulated in an arbitrary order and then sorted by increasing sort z when the list
42 * is prepared into an actual command buffer. The result of a draw command is the rasterization of
43 * the transformed shape, restricted by its primitive clip (e.g. a scissor rect) and a depth test
44 * of "GREATER" vs. its write/test z. (A test of GREATER, as opposed to GEQUAL, avoids double hits
45 * for draws that may have overlapping geometry, e.g. stroking.) If the command has a shading
46 * description, the color buffer will be modified; if not, it will be a depth-only draw.
47 *
48 * In addition to sorting the collected commands, the command list can be optimized during
49 * preparation. Commands that are fully occluded by later operations can be skipped entirely without
50 * affecting the final results. Adjacent commands (post sort) that would use equivalent GPU
51 * pipelines are merged to produce fewer (but larger) operations on the GPU.
52 *
53 * Other than flush-time optimizations (sort, cull, and merge), the command list does what you tell
54 * it to. Draw-specific simplification, style application, and advanced clipping should be handled
55 * at a higher layer.
56 */
57class DrawList {
58public:
59 // The maximum number of render steps that can be recorded into a DrawList before it must be
60 // converted to a DrawPass. The true fundamental limit is imposed by the limits of the depth
61 // attachment and precision of CompressedPaintersOrder and PaintDepth. These values can be
62 // shared by multiple draw calls so it's more difficult to reason about how much room is left
63 // in a DrawList. Limiting it to this keeps tracking simple and ensures that the sequences in
64 // DrawOrder cannot overflow since they are always less than or equal to the number of draws.
65 // TODO(b/322840221): The theoretic max for this value is 16-bit, but we see markedly better
66 // performance with smaller values. This should be understood and fixed directly rather than as
67 // a magic side-effect, but for now, let it go fast.
68 static constexpr int kMaxRenderSteps = 4096;
70
71 // DrawList requires that all Transforms be valid and asserts as much; invalid transforms should
72 // be detected at the Device level or similar. The provided Renderer must be compatible with the
73 // 'shape' and 'stroke' parameters. If the renderer uses coverage AA, 'ordering' must have a
74 // compressed painters order that reflects that. If the renderer uses stencil, the 'ordering'
75 // must have a valid stencil index as well.
76 void recordDraw(const Renderer* renderer,
77 const Transform& localToDevice,
78 const Geometry& geometry,
79 const Clip& clip,
80 DrawOrder ordering,
81 const PaintParams* paint,
82 const StrokeStyle* stroke);
83
84 int renderStepCount() const { return fRenderStepCount; }
85
86 // Bounds for a dst copy required by this DrawList.
87 const Rect& dstCopyBounds() const { return fDstCopyBounds; }
88
89 SkDEBUGCODE(bool hasCoverageMaskDraws() const { return fCoverageMaskShapeDrawCount > 0; })
90
91private:
92 friend class DrawPass;
93
94 struct Draw {
95 const Renderer* fRenderer; // Owned by SharedContext of Recorder that recorded the draw
96 DrawParams fDrawParams; // The DrawParam's transform is owned by fTransforms of the DrawList
97 std::optional<PaintParams> fPaintParams; // Not present implies depth-only draw
98
99 Draw(const Renderer* renderer, const Transform& transform, const Geometry& geometry,
100 const Clip& clip, DrawOrder order, const PaintParams* paint,
101 const StrokeStyle* stroke)
103 , fDrawParams(transform, geometry, clip, order, stroke)
104 , fPaintParams(paint ? std::optional<PaintParams>(*paint) : std::nullopt) {}
105 };
106
107 // The returned Transform reference remains valid for the lifetime of the DrawList.
109
112
113 // Running total of RenderSteps for all draws, assuming nothing is culled
115
116#if defined(SK_DEBUG)
117 // The number of CoverageMask draws that have been recorded. Used in debugging.
118 int fCoverageMaskShapeDrawCount = 0;
119#endif
120
122};
123
124} // namespace skgpu::graphite
125
126#endif // skgpu_graphite_DrawList_DEFINED
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition: SkPath.cpp:3892
SkDEBUGCODE(SK_SPI) SkThreadID SkGetThreadID()
const Rect & dstCopyBounds() const
Definition: DrawList.h:87
const Transform & deduplicateTransform(const Transform &)
Definition: DrawList.cpp:16
SkTBlockList< Transform, 16 > fTransforms
Definition: DrawList.h:110
SkTBlockList< Draw, 16 > fDraws
Definition: DrawList.h:111
int renderStepCount() const
Definition: DrawList.h:84
static constexpr int kMaxRenderSteps
Definition: DrawList.h:68
void recordDraw(const Renderer *renderer, const Transform &localToDevice, const Geometry &geometry, const Clip &clip, DrawOrder ordering, const PaintParams *paint, const StrokeStyle *stroke)
Definition: DrawList.cpp:25
static AI Rect InfiniteInverted()
Definition: Rect.h:64
const Paint & paint
Definition: color_source.cc:38
static float max(float r, float g, float b)
Definition: hsl.cpp:49
Definition: ref_ptr.h:256
static SkColor4f transform(SkColor4f c, SkColorSpace *src, SkColorSpace *dst)
Definition: p3.cpp:47
const Renderer * fRenderer
Definition: DrawList.h:95
std::optional< PaintParams > fPaintParams
Definition: DrawList.h:97
Draw(const Renderer *renderer, const Transform &transform, const Geometry &geometry, const Clip &clip, DrawOrder order, const PaintParams *paint, const StrokeStyle *stroke)
Definition: DrawList.h:99