Debian Codegraph @

src/gpu/graphite/task/DrawTask.h

raw
1 /*
2 * Copyright 2024 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_task_DrawTask_DEFINED
9 #define skgpu_graphite_task_DrawTask_DEFINED
10
11 #include "include/core/SkRefCnt.h"
12 #include "src/gpu/graphite/ScratchResourceManager.h"
13 #include "src/gpu/graphite/TextureProxy.h"
14 #include "src/gpu/graphite/task/Task.h"
15 #include "src/gpu/graphite/task/TaskList.h"
16
17 #include <functional>
18 #include <utility>
19
20 namespace skgpu::graphite {
21
22 class CommandBuffer;
23 class Context;
24 class GraphicsPipeline;
25 class ResourceProvider;
26 class RuntimeEffectDictionary;
27
28 /**
29 * DrawTask is a collection of subtasks that are executed in order to produce some intended
30 * image in the DrawTask's target. As such, at least one of its subtasks will either be a
31 * RenderPassTask, ComputeTask or CopyXToTextureTask that directly modify the target.
32 */
33 class DrawTask final : public Task, private ScratchResourceManager::PendingUseListener {
34 public:
35 explicit DrawTask(sk_sp<TextureProxy> target);
36
37 ~DrawTask() override;
38
39 Status prepareResources(ResourceProvider*,
40 ScratchResourceManager*,
41 sk_sp<const RuntimeEffectDictionary>) override;
42
43 Status addCommands(Context*, CommandBuffer*, ReplayTargetData) override;
44
45 bool visitPipelines(const std::function<bool(const GraphicsPipeline*)>& visitor) override {
46 return fChildTasks.visitPipelines(visitor);
47 }
48
49 bool visitProxies(const std::function<bool(const TextureProxy*)>& visitor,
50 bool readsOnly) override {
51 return fChildTasks.visitProxies(visitor, readsOnly);
52 }
53
54 private:
55 friend class DrawContext; // for "addTask"
56
57 // DrawTask is modified directly by DrawContext for efficiency, but its task list will be
58 // fixed once DrawContext snaps the task.
59 void addTask(sk_sp<Task> task) { fChildTasks.add(std::move(task)); }
60 bool hasTasks() const { return fChildTasks.hasTasks(); }
61
62 void onUseCompleted(ScratchResourceManager*) override;
63
64 sk_sp<TextureProxy> fTarget;
65 TaskList fChildTasks;
66
67 // Once there is one DrawTask for a scratch device, whether or not the target is instantaited
68 // will be equivalent to whether or not prepareResources() has been called already if the task
69 // is referenced multiple times in a Recording. Right now, however, a scratch device can still
70 // produce several DrawTasks (in which case they will see an instantiated proxy so should still
71 // prepare their own resources instead of discarding themselves).
72 bool fPrepared = false;
73
74 #if defined(SK_DUMP_TASKS)
75 void dump(int index, const char* prefix) const override {
76 if (fTarget) {
77 if (index >= 0) {
78 SkDebugf("%s%d: Draw Task=%p (Target=%p) (Label=%s)\n", prefix, index, this,
79 fTarget.get(), fTarget->label());
80 } else {
81 SkDebugf("%sDraw Task=%p (Target=%p) (Label=%s)\n", prefix, this, fTarget.get(),
82 fTarget->label());
83 }
84 } else {
85 if (index >= 0) {
86 SkDebugf("%s%d: Draw Task=%p (Target=%p)\n", prefix, index, this,
87 fTarget.get());
88 } else {
89 SkDebugf("%sDraw Task=%p (Target=%p)\n", prefix, this, fTarget.get());
90 }
91 }
92
93 std::string childPrefix = prefix;
94 static constexpr uint32_t kPrefixIncrement = 4;
95 if (strlen(prefix) >= kPrefixIncrement) {
96 const char* lastBranch = prefix + strlen(prefix) - kPrefixIncrement;
97 if (strcmp(lastBranch, "│ ") == 0) {
98 childPrefix.replace(strlen(prefix) - kPrefixIncrement, kPrefixIncrement, "│ ");
99 } else if (strcmp(lastBranch, "└── ") == 0) {
100 childPrefix.replace(strlen(prefix) - kPrefixIncrement, kPrefixIncrement, " ");
101 }
102 }
103
104 fChildTasks.visit([&](const Task* task, bool isLast) {
105 task->dump(-1, (childPrefix + (isLast ? "└── " : "│ ")).c_str());
106 });
107 }
108 #endif
109 };
110
111 } // namespace skgpu::graphite
112
113 #endif // skgpu_graphite_task_DrawTask_DEFINED