blob: 54e7df3803a763324992b65b0c808ca2447d56f5 [file] [log] [blame]
mtklein@google.comd36522d2013-10-16 13:02:15 +00001#include "DMTask.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +00002#include "DMTaskRunner.h"
commit-bot@chromium.orgfa8cb1c2014-03-31 19:59:44 +00003#include "SkCommandLineFlags.h"
4
5DEFINE_bool(cpu, true, "Master switch for running CPU-bound work.");
6DEFINE_bool(gpu, true, "Master switch for running GPU-bound work.");
mtklein@google.comd36522d2013-10-16 13:02:15 +00007
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00008DECLARE_bool(dryRun);
9
mtklein@google.comd36522d2013-10-16 13:02:15 +000010namespace DM {
11
12Task::Task(Reporter* reporter, TaskRunner* taskRunner)
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000013 : fReporter(reporter)
14 , fTaskRunner(taskRunner)
15 , fDepth(0) {
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000016 fReporter->taskCreated();
mtklein@google.comd36522d2013-10-16 13:02:15 +000017}
18
rmistry@google.comd6bab022013-12-02 13:50:38 +000019Task::Task(const Task& parent)
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000020 : fReporter(parent.fReporter)
rmistry@google.comd6bab022013-12-02 13:50:38 +000021 , fTaskRunner(parent.fTaskRunner)
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000022 , fDepth(parent.depth() + 1) {
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000023 fReporter->taskCreated();
24}
25
26Task::~Task() {
27 fReporter->taskDestroyed();
mtklein@google.comd36522d2013-10-16 13:02:15 +000028}
29
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000030void Task::fail(const char* msg) {
31 SkString failure(this->name());
32 if (msg) {
33 failure.appendf(": %s", msg);
34 }
35 fReporter->fail(failure);
mtklein@google.comd36522d2013-10-16 13:02:15 +000036}
37
commit-bot@chromium.orga39874b2014-03-03 15:44:56 +000038void Task::start() {
39 fStart = SkTime::GetMSecs();
40}
41
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000042void Task::finish() {
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000043 fReporter->printStatus(this->name(), SkTime::GetMSecs() - fStart);
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000044}
45
commit-bot@chromium.org3f032152014-05-01 17:41:32 +000046void Task::spawnChildNext(CpuTask* task) {
47 fTaskRunner->addNext(task);
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000048}
49
50CpuTask::CpuTask(Reporter* reporter, TaskRunner* taskRunner) : Task(reporter, taskRunner) {}
51CpuTask::CpuTask(const Task& parent) : Task(parent) {}
52
53void CpuTask::run() {
commit-bot@chromium.orgfa8cb1c2014-03-31 19:59:44 +000054 if (FLAGS_cpu && !this->shouldSkip()) {
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000055 this->start();
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +000056 if (!FLAGS_dryRun) this->draw();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000057 this->finish();
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000058 }
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000059 SkDELETE(this);
60}
61
commit-bot@chromium.orgb0c71562014-04-30 20:47:30 +000062void CpuTask::spawnChild(CpuTask* task) {
63 // Run children serially on this (CPU) thread. This tends to save RAM and is usually no slower.
commit-bot@chromium.org3f032152014-05-01 17:41:32 +000064 // Calling spawnChildNext() is nearly equivalent, but it'd pointlessly contend on the
65 // threadpool; spawnChildNext() is most useful when you want to change threadpools.
commit-bot@chromium.orgb0c71562014-04-30 20:47:30 +000066 task->run();
67}
68
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000069GpuTask::GpuTask(Reporter* reporter, TaskRunner* taskRunner) : Task(reporter, taskRunner) {}
70
71void GpuTask::run(GrContextFactory& factory) {
commit-bot@chromium.orgfa8cb1c2014-03-31 19:59:44 +000072 if (FLAGS_gpu && !this->shouldSkip()) {
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000073 this->start();
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +000074 if (!FLAGS_dryRun) this->draw(&factory);
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000075 this->finish();
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000076 }
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000077 SkDELETE(this);
78}
79
commit-bot@chromium.orgb0c71562014-04-30 20:47:30 +000080void GpuTask::spawnChild(CpuTask* task) {
81 // Really spawn a new task so it runs on the CPU threadpool instead of the GPU one we're on now.
commit-bot@chromium.org3f032152014-05-01 17:41:32 +000082 // It goes on the front of the queue to minimize the time we must hold reference bitmaps in RAM.
83 this->spawnChildNext(task);
commit-bot@chromium.orgb0c71562014-04-30 20:47:30 +000084}
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000085
mtklein@google.comd36522d2013-10-16 13:02:15 +000086} // namespace DM