| 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 |
#include "src/gpu/graphite/task/TaskList.h" |
| 9 |
|
| 10 |
#include "src/core/SkTraceEvent.h" |
| 11 |
#include "src/gpu/graphite/ScratchResourceManager.h" |
| 12 |
|
| 13 |
#include <cstdint> |
| 14 |
|
| 15 |
namespace skgpu::graphite { |
| 16 |
|
| 17 |
using Status = Task::Status; |
| 18 |
|
| 19 |
template <typename Fn> |
| 20 |
Status TaskList::visitTasks(Fn fn) { |
| 21 |
int discardCount = 0; |
| 22 |
for (sk_sp<Task>& task: fTasks) { |
| 23 |
if (!task) { |
| 24 |
discardCount++; |
| 25 |
continue; // Skip over discarded tasks |
| 26 |
} |
| 27 |
|
| 28 |
Status status = fn(task.get()); |
| 29 |
if (status == Status::kFail) { |
| 30 |
return Status::kFail; |
| 31 |
} else if (status == Status::kDiscard) { |
| 32 |
task.reset(); |
| 33 |
discardCount++; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
return discardCount == fTasks.size() ? Status::kDiscard : Status::kSuccess; |
| 38 |
} |
| 39 |
|
| 40 |
Status TaskList::prepareResources(ResourceProvider* resourceProvider, |
| 41 |
ScratchResourceManager* scratchManager, |
| 42 |
sk_sp<const RuntimeEffectDictionary> runtimeDict) { |
| 43 |
TRACE_EVENT1("skia.gpu", TRACE_FUNC, "# tasks", fTasks.size()); |
| 44 |
scratchManager->pushScope(); |
| 45 |
Status status = this->visitTasks([&](Task* task) { |
| 46 |
return task->prepareResources(resourceProvider, scratchManager, runtimeDict); |
| 47 |
}); |
| 48 |
scratchManager->popScope(); |
| 49 |
return status; |
| 50 |
} |
| 51 |
|
| 52 |
Status TaskList::addCommands(Context* context, |
| 53 |
CommandBuffer* commandBuffer, |
| 54 |
Task::ReplayTargetData replayData) { |
| 55 |
TRACE_EVENT1("skia.gpu", TRACE_FUNC, "# tasks", fTasks.size()); |
| 56 |
return this->visitTasks([&](Task* task) { |
| 57 |
return task->addCommands(context, commandBuffer, replayData); |
| 58 |
}); |
| 59 |
} |
| 60 |
|
| 61 |
bool TaskList::visitPipelines(const std::function<bool(const GraphicsPipeline*)>& visitor) { |
| 62 |
Status status = this->visitTasks([&](Task* task) { |
| 63 |
return task->visitPipelines(visitor) ? Status::kSuccess : Status::kFail; |
| 64 |
}); |
| 65 |
// Map back to simple bool (treat kDiscard as true too, no pipelines to visit means all |
| 66 |
// pipelines were visited). |
| 67 |
return status != Status::kFail; |
| 68 |
} |
| 69 |
|
| 70 |
bool TaskList::visitProxies(const std::function<bool(const TextureProxy*)>& visitor, |
| 71 |
bool readsOnly) { |
| 72 |
Status status = this->visitTasks([&](Task* task) { |
| 73 |
return task->visitProxies(visitor, readsOnly) ? Status::kSuccess : Status::kFail; |
| 74 |
}); |
| 75 |
// Map back to simple bool (treat kDiscard as true too, no pipelines to visit means all |
| 76 |
// pipelines were visited). |
| 77 |
return status != Status::kFail; |
| 78 |
} |
| 79 |
|
| 80 |
#if defined(SK_DUMP_TASKS) |
| 81 |
void TaskList::visit(const std::function<void(const Task* task, bool isLast)>& visitor) const { |
| 82 |
// Find the last non-null task so we know when to draw the corner branch. |
| 83 |
const Task* lastNonNullTask = nullptr; |
| 84 |
for (int i = fTasks.size() - 1; i >= 0; --i) { |
| 85 |
if (fTasks[i]) { |
| 86 |
lastNonNullTask = fTasks[i].get(); |
| 87 |
break; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
for (const sk_sp<Task>& task : fTasks) { |
| 92 |
if (task) { |
| 93 |
visitor(task.get(), task.get() == lastNonNullTask); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
#endif |
| 98 |
|
| 99 |
} // namespace skgpu::graphite |