blob: e32249d410c425e3912e94dbdd2c8f4b96bda810 [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
8namespace DM {
9
10Task::Task(Reporter* reporter, TaskRunner* taskRunner)
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000011 : fReporter(reporter)
12 , fTaskRunner(taskRunner)
13 , fDepth(0) {
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000014 fReporter->taskCreated();
mtklein@google.comd36522d2013-10-16 13:02:15 +000015}
16
rmistry@google.comd6bab022013-12-02 13:50:38 +000017Task::Task(const Task& parent)
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000018 : fReporter(parent.fReporter)
rmistry@google.comd6bab022013-12-02 13:50:38 +000019 , fTaskRunner(parent.fTaskRunner)
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000020 , fDepth(parent.depth() + 1) {
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000021 fReporter->taskCreated();
22}
23
24Task::~Task() {
25 fReporter->taskDestroyed();
mtklein@google.comd36522d2013-10-16 13:02:15 +000026}
27
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000028void Task::fail(const char* msg) {
29 SkString failure(this->name());
30 if (msg) {
31 failure.appendf(": %s", msg);
32 }
33 fReporter->fail(failure);
mtklein@google.comd36522d2013-10-16 13:02:15 +000034}
35
commit-bot@chromium.orga39874b2014-03-03 15:44:56 +000036void Task::start() {
37 fStart = SkTime::GetMSecs();
38}
39
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000040void Task::finish() {
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000041 fReporter->printStatus(this->name(), SkTime::GetMSecs() - fStart);
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000042}
43
commit-bot@chromium.org3f032152014-05-01 17:41:32 +000044void Task::spawnChildNext(CpuTask* task) {
45 fTaskRunner->addNext(task);
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000046}
47
48CpuTask::CpuTask(Reporter* reporter, TaskRunner* taskRunner) : Task(reporter, taskRunner) {}
49CpuTask::CpuTask(const Task& parent) : Task(parent) {}
50
51void CpuTask::run() {
commit-bot@chromium.orgfa8cb1c2014-03-31 19:59:44 +000052 if (FLAGS_cpu && !this->shouldSkip()) {
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000053 this->start();
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000054 this->draw();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000055 this->finish();
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000056 }
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000057 SkDELETE(this);
58}
59
commit-bot@chromium.orgb0c71562014-04-30 20:47:30 +000060void CpuTask::spawnChild(CpuTask* task) {
61 // 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 +000062 // Calling spawnChildNext() is nearly equivalent, but it'd pointlessly contend on the
63 // threadpool; spawnChildNext() is most useful when you want to change threadpools.
commit-bot@chromium.orgb0c71562014-04-30 20:47:30 +000064 task->run();
65}
66
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000067GpuTask::GpuTask(Reporter* reporter, TaskRunner* taskRunner) : Task(reporter, taskRunner) {}
68
69void GpuTask::run(GrContextFactory& factory) {
commit-bot@chromium.orgfa8cb1c2014-03-31 19:59:44 +000070 if (FLAGS_gpu && !this->shouldSkip()) {
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000071 this->start();
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000072 this->draw(&factory);
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000073 this->finish();
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000074 }
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000075 SkDELETE(this);
76}
77
commit-bot@chromium.orgb0c71562014-04-30 20:47:30 +000078void GpuTask::spawnChild(CpuTask* task) {
79 // 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 +000080 // It goes on the front of the queue to minimize the time we must hold reference bitmaps in RAM.
81 this->spawnChildNext(task);
commit-bot@chromium.orgb0c71562014-04-30 20:47:30 +000082}
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000083
mtklein@google.comd36522d2013-10-16 13:02:15 +000084} // namespace DM