blob: 83f585f00dac9e32845aa3191974e8c04844c5d5 [file] [log] [blame]
Adlai Holler08f53112021-01-20 17:44:15 -05001/*
2 * Copyright 2021 Google Inc.
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#include "src/gpu/GrRenderTaskCluster.h"
9
10#include "include/private/SkTHash.h"
11#include "src/gpu/GrRenderTask.h"
12
13// Uncomment to get lots of logging.
14#define CLUSTER_DEBUGF(...) //SkDebugf(__VA_ARGS__)
15
Brian Salomon982127b2021-01-21 10:43:35 -050016static GrSurfaceProxy* first_target(GrRenderTask* task) { return task->target(0); }
Adlai Holler08f53112021-01-20 17:44:15 -050017
18#ifdef SK_DEBUG
19[[maybe_unused]] static SkString describe_task(GrRenderTask* t) {
20 if (GrSurfaceProxy* target = first_target(t)) {
21 return SkStringPrintf("%s(%d)", target->getDebugName().c_str(), t->uniqueID());
22 } else {
23 return SkStringPrintf("%d", t->uniqueID());
24 }
25}
26
27[[maybe_unused]] static SkString describe_tasks(SkSpan<const sk_sp<GrRenderTask>> collection) {
28 SkString s;
29 for (const sk_sp<GrRenderTask>& t : collection) {
30 s.appendf("%s ", describe_task(t.get()).c_str());
31 }
32 return s;
33}
34
35[[maybe_unused]] static SkString describe_tasks(const SkTInternalLList<GrRenderTask>& collection) {
36 SkString s;
37 for (GrRenderTask* t : collection) {
38 s.appendf("%s ", describe_task(t).c_str());
39 }
40 return s;
41}
42
43static void validate(SkSpan<const sk_sp<GrRenderTask>> input,
44 const SkTInternalLList<GrRenderTask>& llist) {
45 // Check that we didn't break dependencies.
46 SkTHashSet<GrRenderTask*> seen;
47 for (GrRenderTask* t : llist) {
48 seen.add(t);
49 for (GrRenderTask* dep : t->dependencies()) {
50 SkASSERTF(seen.contains(dep),
51 "%s came before dependency %s",
52 describe_task(t).c_str(),
53 describe_task(dep).c_str());
54 }
55 }
56 // Check that llist has the same entries as the input.
57 for (const auto& orig : input) {
58 seen.remove(orig.get());
59 }
60 SkASSERT(seen.empty());
61}
62
63#endif // SK_DEBUG
64
65// Returns whether reordering occurred.
66static bool task_cluster_visit(GrRenderTask* task, SkTInternalLList<GrRenderTask>* llist,
67 SkTHashMap<GrSurfaceProxy*, GrRenderTask*>* lastTaskMap) {
68 CLUSTER_DEBUGF("Cluster: ***Step***\nLooking at %s\n",
69 describe_task(task).c_str());
70 if (task->numTargets() != 1) {
71 CLUSTER_DEBUGF("Cluster: %d targets. Emitting barriers.\n", task->numTargets());
72 // Tasks with 0 or multiple targets are treated as full barriers
73 // for all their targets.
74 for (int j = 0; j < task->numTargets(); j++) {
Adlai Holler9202ebc2021-01-27 11:06:52 -050075 if (lastTaskMap->find(task->target(0))) {
76 lastTaskMap->remove(task->target(0));
77 }
Adlai Holler08f53112021-01-20 17:44:15 -050078 }
79 return false;
80 }
81
82 GrSurfaceProxy* target = first_target(task);
83 GrRenderTask* clusterTail = (lastTaskMap->find(target) ? *lastTaskMap->find(target) : nullptr);
84 lastTaskMap->set(target, task);
85
86 if (!clusterTail) {
87 CLUSTER_DEBUGF("Cluster: Bail, no cluster to extend.\n");
88 return false;
89 }
90
91 CLUSTER_DEBUGF("Cluster: clusterTail is %s.\n", describe_task(clusterTail).c_str());
92
93 if (clusterTail == llist->tail()) {
94 CLUSTER_DEBUGF("Cluster: Bail, cluster is already tail.\n");
95 return false;
96 }
97 GrRenderTask* movedHead = clusterTail->fNext;
98
99 // Now, let's refer to the "cluster" as the chain of tasks with the same target, that we're
100 // hoping to extend by reordering. Let "moved tasks" be the ones we want to move to extend the
101 // cluster.
102 GrRenderTask* clusterHead = clusterTail;
103 while (clusterHead->fPrev
104 && 1 == clusterHead->fPrev->numTargets()
105 && target == first_target(clusterHead->fPrev)) {
106 clusterHead = clusterHead->fPrev;
107 }
108
109 // We can't reorder if any moved task depends on anything in the cluster.
110 // Time complexity here is high, but making a hash set is a lot worse.
111 for (GrRenderTask* moved = movedHead; moved; moved = moved->fNext) {
112 for (GrRenderTask* dep : moved->dependencies()) {
113 for (GrRenderTask* t = clusterHead; t != movedHead; t = t->fNext) {
114 if (t == dep) {
115 CLUSTER_DEBUGF("Cluster: Bail, %s depends on %s.\n",
116 describe_task(moved).c_str(),
117 describe_task(dep).c_str());
118 return false;
119 }
120 }
121 }
122 }
123
124 // Grab the moved tasks and pull them before clusterHead.
125 for (GrRenderTask* moved = movedHead; moved;) {
126 CLUSTER_DEBUGF("Cluster: Reorder %s behind %s.\n",
127 describe_task(moved).c_str(),
128 describe_task(clusterHead).c_str());
129 // Be careful to save fNext before each move.
130 GrRenderTask* nextMoved = moved->fNext;
131 llist->remove(moved);
132 llist->addBefore(moved, clusterHead);
133 moved = nextMoved;
134 }
135 return true;
136}
137
138bool GrClusterRenderTasks(SkSpan<const sk_sp<GrRenderTask>> input,
139 SkTInternalLList<GrRenderTask>* llist) {
140 SkASSERT(llist->isEmpty());
141
142 if (input.count() < 3) {
143 return false;
144 }
145
146 CLUSTER_DEBUGF("Cluster: Original order is %s\n", describe_tasks(input).c_str());
147
148 SkTHashMap<GrSurfaceProxy*, GrRenderTask*> lastTaskMap;
149 bool didReorder = false;
150 for (const auto& t : input) {
151 didReorder |= task_cluster_visit(t.get(), llist, &lastTaskMap);
152 llist->addToTail(t.get());
153 CLUSTER_DEBUGF("Cluster: Output order is now: %s\n", describe_tasks(*llist).c_str());
154 }
155
156#ifdef SK_DEBUG
157 if (didReorder) {
158 validate(input, *llist);
159 }
160#endif
161
162 return didReorder;
163}
164
165#undef CLUSTER_DEBUGF