blob: 7725780d4ced9ec8377ddc04e6c0cfeaaef6aff1 [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) {
mtklein@google.comd36522d2013-10-16 13:02:15 +000014 fReporter->start();
15}
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) {
mtklein@google.comd36522d2013-10-16 13:02:15 +000021 fReporter->start();
22}
23
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000024void Task::fail(const char* msg) {
25 SkString failure(this->name());
26 if (msg) {
27 failure.appendf(": %s", msg);
28 }
29 fReporter->fail(failure);
mtklein@google.comd36522d2013-10-16 13:02:15 +000030}
31
commit-bot@chromium.orga39874b2014-03-03 15:44:56 +000032void Task::start() {
33 fStart = SkTime::GetMSecs();
34}
35
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000036void Task::finish() {
commit-bot@chromium.orga39874b2014-03-03 15:44:56 +000037 fReporter->finish(this->name(), SkTime::GetMSecs() - fStart);
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000038}
39
commit-bot@chromium.org3f032152014-05-01 17:41:32 +000040void Task::spawnChildNext(CpuTask* task) {
41 fTaskRunner->addNext(task);
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000042}
43
44CpuTask::CpuTask(Reporter* reporter, TaskRunner* taskRunner) : Task(reporter, taskRunner) {}
45CpuTask::CpuTask(const Task& parent) : Task(parent) {}
46
47void CpuTask::run() {
commit-bot@chromium.orga39874b2014-03-03 15:44:56 +000048 this->start();
commit-bot@chromium.orgfa8cb1c2014-03-31 19:59:44 +000049 if (FLAGS_cpu && !this->shouldSkip()) {
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000050 this->draw();
51 }
52 this->finish();
53 SkDELETE(this);
54}
55
commit-bot@chromium.orgb0c71562014-04-30 20:47:30 +000056void CpuTask::spawnChild(CpuTask* task) {
57 // 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 +000058 // Calling spawnChildNext() is nearly equivalent, but it'd pointlessly contend on the
59 // threadpool; spawnChildNext() is most useful when you want to change threadpools.
commit-bot@chromium.orgb0c71562014-04-30 20:47:30 +000060 task->run();
61}
62
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000063GpuTask::GpuTask(Reporter* reporter, TaskRunner* taskRunner) : Task(reporter, taskRunner) {}
64
65void GpuTask::run(GrContextFactory& factory) {
commit-bot@chromium.orga39874b2014-03-03 15:44:56 +000066 this->start();
commit-bot@chromium.orgfa8cb1c2014-03-31 19:59:44 +000067 if (FLAGS_gpu && !this->shouldSkip()) {
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000068 this->draw(&factory);
69 }
70 this->finish();
71 SkDELETE(this);
72}
73
commit-bot@chromium.orgb0c71562014-04-30 20:47:30 +000074void GpuTask::spawnChild(CpuTask* task) {
75 // 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 +000076 // It goes on the front of the queue to minimize the time we must hold reference bitmaps in RAM.
77 this->spawnChildNext(task);
commit-bot@chromium.orgb0c71562014-04-30 20:47:30 +000078}
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000079
mtklein@google.comd36522d2013-10-16 13:02:15 +000080} // namespace DM