blob: 2c7cdd79a41ae4f04ca9889ed4510821d63792c3 [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.");
mtklein1e319f72014-07-15 08:27:06 -07007DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each task.");
mtklein@google.comd36522d2013-10-16 13:02:15 +00008
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00009DECLARE_bool(dryRun);
10
mtklein@google.comd36522d2013-10-16 13:02:15 +000011namespace DM {
12
13Task::Task(Reporter* reporter, TaskRunner* taskRunner)
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000014 : fReporter(reporter)
15 , fTaskRunner(taskRunner)
16 , fDepth(0) {
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000017 fReporter->taskCreated();
mtklein@google.comd36522d2013-10-16 13:02:15 +000018}
19
rmistry@google.comd6bab022013-12-02 13:50:38 +000020Task::Task(const Task& parent)
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000021 : fReporter(parent.fReporter)
rmistry@google.comd6bab022013-12-02 13:50:38 +000022 , fTaskRunner(parent.fTaskRunner)
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000023 , fDepth(parent.depth() + 1) {
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000024 fReporter->taskCreated();
25}
26
27Task::~Task() {
28 fReporter->taskDestroyed();
mtklein@google.comd36522d2013-10-16 13:02:15 +000029}
30
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000031void Task::fail(const char* msg) {
32 SkString failure(this->name());
33 if (msg) {
34 failure.appendf(": %s", msg);
35 }
36 fReporter->fail(failure);
mtklein@google.comd36522d2013-10-16 13:02:15 +000037}
38
commit-bot@chromium.orga39874b2014-03-03 15:44:56 +000039void Task::start() {
40 fStart = SkTime::GetMSecs();
41}
42
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000043void Task::finish() {
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000044 fReporter->printStatus(this->name(), SkTime::GetMSecs() - fStart);
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000045}
46
commit-bot@chromium.org3f032152014-05-01 17:41:32 +000047void Task::spawnChildNext(CpuTask* task) {
48 fTaskRunner->addNext(task);
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000049}
50
51CpuTask::CpuTask(Reporter* reporter, TaskRunner* taskRunner) : Task(reporter, taskRunner) {}
52CpuTask::CpuTask(const Task& parent) : Task(parent) {}
53
54void CpuTask::run() {
commit-bot@chromium.orgfa8cb1c2014-03-31 19:59:44 +000055 if (FLAGS_cpu && !this->shouldSkip()) {
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000056 this->start();
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +000057 if (!FLAGS_dryRun) this->draw();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000058 this->finish();
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000059 }
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000060 SkDELETE(this);
61}
62
commit-bot@chromium.orgb0c71562014-04-30 20:47:30 +000063void CpuTask::spawnChild(CpuTask* task) {
64 // 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 +000065 // Calling spawnChildNext() is nearly equivalent, but it'd pointlessly contend on the
66 // threadpool; spawnChildNext() is most useful when you want to change threadpools.
commit-bot@chromium.orgb0c71562014-04-30 20:47:30 +000067 task->run();
68}
69
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000070GpuTask::GpuTask(Reporter* reporter, TaskRunner* taskRunner) : Task(reporter, taskRunner) {}
71
72void GpuTask::run(GrContextFactory& factory) {
commit-bot@chromium.orgfa8cb1c2014-03-31 19:59:44 +000073 if (FLAGS_gpu && !this->shouldSkip()) {
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000074 this->start();
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +000075 if (!FLAGS_dryRun) this->draw(&factory);
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000076 this->finish();
Mike Klein18515cf2014-07-15 16:10:02 -040077 if (FLAGS_resetGpuContext) {
78 factory.destroyContexts();
79 }
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000080 }
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000081 SkDELETE(this);
82}
83
commit-bot@chromium.orgb0c71562014-04-30 20:47:30 +000084void GpuTask::spawnChild(CpuTask* task) {
85 // 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 +000086 // It goes on the front of the queue to minimize the time we must hold reference bitmaps in RAM.
87 this->spawnChildNext(task);
commit-bot@chromium.orgb0c71562014-04-30 20:47:30 +000088}
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000089
mtklein@google.comd36522d2013-10-16 13:02:15 +000090} // namespace DM