Flutter Engine
The Flutter Engine
DrawContext.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_DrawContext_DEFINED
9#define skgpu_graphite_DrawContext_DEFINED
10
15
22
23#include <vector>
24
25class SkPixmap;
26
27namespace skgpu::graphite {
28
29class Geometry;
30class Recorder;
31class Transform;
32
33class Caps;
34class ComputePathAtlas;
35class DrawTask;
36class PathAtlas;
37class Task;
38class TextureProxy;
39
40/**
41 * DrawContext records draw commands into a specific Surface, via a general task graph
42 * representing GPU work and their inter-dependencies.
43 */
44class DrawContext final : public SkRefCnt {
45public:
46 static sk_sp<DrawContext> Make(const Caps* caps,
48 SkISize deviceSize,
49 const SkColorInfo&,
50 const SkSurfaceProps&);
51
52 ~DrawContext() override;
53
54 const SkImageInfo& imageInfo() const { return fImageInfo; }
55 const SkColorInfo& colorInfo() const { return fImageInfo.colorInfo(); }
56 TextureProxy* target() { return fTarget.get(); }
57 const TextureProxy* target() const { return fTarget.get(); }
58 sk_sp<TextureProxy> refTarget() const { return fTarget; }
59
60 // May be null if the target is not texturable.
61 const TextureProxyView& readSurfaceView() const { return fReadView; }
62
63 const SkSurfaceProps& surfaceProps() const { return fSurfaceProps; }
64
65 int pendingRenderSteps() const { return fPendingDraws->renderStepCount(); }
66
67 void clear(const SkColor4f& clearColor);
68 void discard();
69
70 void recordDraw(const Renderer* renderer,
71 const Transform& localToDevice,
72 const Geometry& geometry,
73 const Clip& clip,
74 DrawOrder ordering,
75 const PaintParams* paint,
76 const StrokeStyle* stroke);
77
78 bool recordUpload(Recorder* recorder,
79 sk_sp<TextureProxy> targetProxy,
80 const SkColorInfo& srcColorInfo,
81 const SkColorInfo& dstColorInfo,
82 const std::vector<MipLevel>& levels,
83 const SkIRect& dstRect,
84 std::unique_ptr<ConditionalUploadContext>);
85
86 // Add a Task that will be executed *before* any of the pending draws and uploads are
87 // executed as part of the next flush(). Dependency
89
90 // Returns the transient path atlas that uses compute to accumulate coverage masks for atlas
91 // draws recorded to this SDC. The atlas gets created lazily upon request. Returns nullptr
92 // if compute path generation is not supported.
94
95 // Moves all accumulated pending recorded operations (draws and uploads), and any other
96 // dependent tasks into the DrawTask currently being built.
97 void flush(Recorder*);
98
99 // Flushes (if needed) and completes the current DrawTask, returning it to the caller.
100 // Subsequent recorded operations will be added to a new DrawTask.
102
103private:
105
106 sk_sp<TextureProxy> fTarget;
107 TextureProxyView fReadView;
108 SkImageInfo fImageInfo;
109 const SkSurfaceProps fSurfaceProps;
110
111 // The in-progress DrawTask that will be snapped and returned when some external requirement
112 // must depend on the contents of this DrawContext's target. As higher-level Skia operations
113 // are recorded, it can be necessary to flush pending draws and uploads into the task list.
114 // This provides a place to reset scratch textures or buffers as their previous state will have
115 // been consumed by the flushed tasks rendering to this DrawContext's target.
116 sk_sp<DrawTask> fCurrentDrawTask;
117
118 // Stores the most immediately recorded draws and uploads into the DrawContext's target. These
119 // are collected outside of the DrawTask so that encoder switches can be minimized when
120 // flushing.
121 std::unique_ptr<DrawList> fPendingDraws;
122 std::unique_ptr<UploadList> fPendingUploads;
123 // Load and store information for the current pending draws.
124 LoadOp fPendingLoadOp = LoadOp::kLoad;
125 StoreOp fPendingStoreOp = StoreOp::kStore;
126 std::array<float, 4> fPendingClearColor = { 0, 0, 0, 0 };
127
128 // Accumulates atlas coverage masks generated by compute dispatches that are required by one or
129 // more entries in `fPendingDraws`. When pending draws are snapped into a new DrawPass, a
130 // compute dispatch group gets recorded which schedules the accumulated masks to get drawn into
131 // an atlas texture. The accumulated masks are then cleared which frees up the atlas for
132 // future draws.
133 //
134 // TODO: Currently every PathAtlas contains a single texture. If multiple snapped draw
135 // passes resulted in multiple ComputePathAtlas dispatch groups, the later dispatches would
136 // overwrite the atlas texture since all compute tasks are scheduled before render tasks. This
137 // is currently not an issue since there is only one DrawPass per flush but we may want to
138 // either support one atlas texture per DrawPass or record the dispatches once per
139 // RenderPassTask rather than DrawPass.
140 std::unique_ptr<ComputePathAtlas> fComputePathAtlas;
141};
142
143} // namespace skgpu::graphite
144
145#endif // skgpu_graphite_DrawContext_DEFINED
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition: SkPath.cpp:3892
const SkSurfaceProps & surfaceProps() const
Definition: DrawContext.h:63
TextureProxy * target()
Definition: DrawContext.h:56
void recordDependency(sk_sp< Task >)
const SkImageInfo & imageInfo() const
Definition: DrawContext.h:54
void recordDraw(const Renderer *renderer, const Transform &localToDevice, const Geometry &geometry, const Clip &clip, DrawOrder ordering, const PaintParams *paint, const StrokeStyle *stroke)
const SkColorInfo & colorInfo() const
Definition: DrawContext.h:55
const TextureProxy * target() const
Definition: DrawContext.h:57
static sk_sp< DrawContext > Make(const Caps *caps, sk_sp< TextureProxy > target, SkISize deviceSize, const SkColorInfo &, const SkSurfaceProps &)
Definition: DrawContext.cpp:65
sk_sp< TextureProxy > refTarget() const
Definition: DrawContext.h:58
PathAtlas * getComputePathAtlas(Recorder *)
void clear(const SkColor4f &clearColor)
sk_sp< Task > snapDrawTask(Recorder *)
bool recordUpload(Recorder *recorder, sk_sp< TextureProxy > targetProxy, const SkColorInfo &srcColorInfo, const SkColorInfo &dstColorInfo, const std::vector< MipLevel > &levels, const SkIRect &dstRect, std::unique_ptr< ConditionalUploadContext >)
const TextureProxyView & readSurfaceView() const
Definition: DrawContext.h:61
const Paint & paint
Definition: color_source.cc:38
skgpu::graphite::Transform Transform
Definition: SkRect.h:32
Definition: SkSize.h:16
const SkColorInfo & colorInfo() const
Definition: SkImageInfo.h:404
Definition: DM.cpp:1161