blob: 5ceb53b0625088a3077fab3237a16da4a122c5a6 [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"
caryclark17f0b6d2014-07-22 10:15:34 -07003#include "SkCommonFlags.h"
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00004
mtklein@google.comd36522d2013-10-16 13:02:15 +00005namespace DM {
6
7Task::Task(Reporter* reporter, TaskRunner* taskRunner)
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +00008 : fReporter(reporter)
9 , fTaskRunner(taskRunner)
10 , fDepth(0) {
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000011 fReporter->taskCreated();
mtklein@google.comd36522d2013-10-16 13:02:15 +000012}
13
rmistry@google.comd6bab022013-12-02 13:50:38 +000014Task::Task(const Task& parent)
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000015 : fReporter(parent.fReporter)
rmistry@google.comd6bab022013-12-02 13:50:38 +000016 , fTaskRunner(parent.fTaskRunner)
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000017 , fDepth(parent.depth() + 1) {
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000018 fReporter->taskCreated();
19}
20
21Task::~Task() {
22 fReporter->taskDestroyed();
mtklein@google.comd36522d2013-10-16 13:02:15 +000023}
24
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000025void Task::fail(const char* msg) {
26 SkString failure(this->name());
27 if (msg) {
28 failure.appendf(": %s", msg);
29 }
30 fReporter->fail(failure);
mtklein@google.comd36522d2013-10-16 13:02:15 +000031}
32
commit-bot@chromium.orga39874b2014-03-03 15:44:56 +000033void Task::start() {
34 fStart = SkTime::GetMSecs();
35}
36
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000037void Task::finish() {
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000038 fReporter->printStatus(this->name(), SkTime::GetMSecs() - fStart);
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000039}
40
mtklein406654b2014-09-03 15:34:37 -070041void Task::reallySpawnChild(CpuTask* task) {
42 fTaskRunner->add(task);
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000043}
44
45CpuTask::CpuTask(Reporter* reporter, TaskRunner* taskRunner) : Task(reporter, taskRunner) {}
46CpuTask::CpuTask(const Task& parent) : Task(parent) {}
47
48void CpuTask::run() {
commit-bot@chromium.orgfa8cb1c2014-03-31 19:59:44 +000049 if (FLAGS_cpu && !this->shouldSkip()) {
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000050 this->start();
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +000051 if (!FLAGS_dryRun) this->draw();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000052 this->finish();
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000053 }
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000054 SkDELETE(this);
55}
56
commit-bot@chromium.orgb0c71562014-04-30 20:47:30 +000057void CpuTask::spawnChild(CpuTask* task) {
58 // Run children serially on this (CPU) thread. This tends to save RAM and is usually no slower.
mtklein406654b2014-09-03 15:34:37 -070059 // Calling reallySpawnChild() is nearly equivalent, but it'd pointlessly contend on the
60 // threadpool; reallySpawnChild() is most useful when you want to change threadpools.
commit-bot@chromium.orgb0c71562014-04-30 20:47:30 +000061 task->run();
62}
63
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000064GpuTask::GpuTask(Reporter* reporter, TaskRunner* taskRunner) : Task(reporter, taskRunner) {}
65
mtklein406654b2014-09-03 15:34:37 -070066void GpuTask::run(GrContextFactory* factory) {
commit-bot@chromium.orgfa8cb1c2014-03-31 19:59:44 +000067 if (FLAGS_gpu && !this->shouldSkip()) {
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000068 this->start();
mtklein406654b2014-09-03 15:34:37 -070069 if (!FLAGS_dryRun) this->draw(factory);
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +000070 this->finish();
bsalomon2354f842014-07-28 13:48:36 -070071 if (FLAGS_abandonGpuContext) {
mtklein406654b2014-09-03 15:34:37 -070072 factory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -070073 }
74 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
mtklein406654b2014-09-03 15:34:37 -070075 factory->destroyContexts();
Mike Klein18515cf2014-07-15 16:10:02 -040076 }
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000077 }
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000078 SkDELETE(this);
79}
80
commit-bot@chromium.orgb0c71562014-04-30 20:47:30 +000081void GpuTask::spawnChild(CpuTask* task) {
mtklein406654b2014-09-03 15:34:37 -070082 // 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 +000083 // It goes on the front of the queue to minimize the time we must hold reference bitmaps in RAM.
mtklein406654b2014-09-03 15:34:37 -070084 this->reallySpawnChild(task);
commit-bot@chromium.orgb0c71562014-04-30 20:47:30 +000085}
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000086
mtklein@google.comd36522d2013-10-16 13:02:15 +000087} // namespace DM