mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 1 | #include "DMTask.h" |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 2 | #include "DMTaskRunner.h" |
commit-bot@chromium.org | fa8cb1c | 2014-03-31 19:59:44 +0000 | [diff] [blame] | 3 | #include "SkCommandLineFlags.h" |
| 4 | |
| 5 | DEFINE_bool(cpu, true, "Master switch for running CPU-bound work."); |
| 6 | DEFINE_bool(gpu, true, "Master switch for running GPU-bound work."); |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 7 | |
commit-bot@chromium.org | a65e2fd | 2014-05-30 17:23:31 +0000 | [diff] [blame] | 8 | DECLARE_bool(dryRun); |
| 9 | |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 10 | namespace DM { |
| 11 | |
| 12 | Task::Task(Reporter* reporter, TaskRunner* taskRunner) |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 13 | : fReporter(reporter) |
| 14 | , fTaskRunner(taskRunner) |
| 15 | , fDepth(0) { |
commit-bot@chromium.org | 39e8d93 | 2014-05-29 20:14:48 +0000 | [diff] [blame] | 16 | fReporter->taskCreated(); |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 17 | } |
| 18 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 19 | Task::Task(const Task& parent) |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 20 | : fReporter(parent.fReporter) |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 21 | , fTaskRunner(parent.fTaskRunner) |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 22 | , fDepth(parent.depth() + 1) { |
commit-bot@chromium.org | 39e8d93 | 2014-05-29 20:14:48 +0000 | [diff] [blame] | 23 | fReporter->taskCreated(); |
| 24 | } |
| 25 | |
| 26 | Task::~Task() { |
| 27 | fReporter->taskDestroyed(); |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 28 | } |
| 29 | |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 30 | void 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.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 36 | } |
| 37 | |
commit-bot@chromium.org | a39874b | 2014-03-03 15:44:56 +0000 | [diff] [blame] | 38 | void Task::start() { |
| 39 | fStart = SkTime::GetMSecs(); |
| 40 | } |
| 41 | |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 42 | void Task::finish() { |
commit-bot@chromium.org | 39e8d93 | 2014-05-29 20:14:48 +0000 | [diff] [blame] | 43 | fReporter->printStatus(this->name(), SkTime::GetMSecs() - fStart); |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 44 | } |
| 45 | |
commit-bot@chromium.org | 3f03215 | 2014-05-01 17:41:32 +0000 | [diff] [blame] | 46 | void Task::spawnChildNext(CpuTask* task) { |
| 47 | fTaskRunner->addNext(task); |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | CpuTask::CpuTask(Reporter* reporter, TaskRunner* taskRunner) : Task(reporter, taskRunner) {} |
| 51 | CpuTask::CpuTask(const Task& parent) : Task(parent) {} |
| 52 | |
| 53 | void CpuTask::run() { |
commit-bot@chromium.org | fa8cb1c | 2014-03-31 19:59:44 +0000 | [diff] [blame] | 54 | if (FLAGS_cpu && !this->shouldSkip()) { |
commit-bot@chromium.org | 39e8d93 | 2014-05-29 20:14:48 +0000 | [diff] [blame] | 55 | this->start(); |
commit-bot@chromium.org | a65e2fd | 2014-05-30 17:23:31 +0000 | [diff] [blame] | 56 | if (!FLAGS_dryRun) this->draw(); |
commit-bot@chromium.org | 39e8d93 | 2014-05-29 20:14:48 +0000 | [diff] [blame] | 57 | this->finish(); |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 58 | } |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 59 | SkDELETE(this); |
| 60 | } |
| 61 | |
commit-bot@chromium.org | b0c7156 | 2014-04-30 20:47:30 +0000 | [diff] [blame] | 62 | void 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.org | 3f03215 | 2014-05-01 17:41:32 +0000 | [diff] [blame] | 64 | // 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.org | b0c7156 | 2014-04-30 20:47:30 +0000 | [diff] [blame] | 66 | task->run(); |
| 67 | } |
| 68 | |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 69 | GpuTask::GpuTask(Reporter* reporter, TaskRunner* taskRunner) : Task(reporter, taskRunner) {} |
| 70 | |
| 71 | void GpuTask::run(GrContextFactory& factory) { |
commit-bot@chromium.org | fa8cb1c | 2014-03-31 19:59:44 +0000 | [diff] [blame] | 72 | if (FLAGS_gpu && !this->shouldSkip()) { |
commit-bot@chromium.org | 39e8d93 | 2014-05-29 20:14:48 +0000 | [diff] [blame] | 73 | this->start(); |
commit-bot@chromium.org | a65e2fd | 2014-05-30 17:23:31 +0000 | [diff] [blame] | 74 | if (!FLAGS_dryRun) this->draw(&factory); |
commit-bot@chromium.org | 39e8d93 | 2014-05-29 20:14:48 +0000 | [diff] [blame] | 75 | this->finish(); |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 76 | } |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 77 | SkDELETE(this); |
| 78 | } |
| 79 | |
commit-bot@chromium.org | b0c7156 | 2014-04-30 20:47:30 +0000 | [diff] [blame] | 80 | void 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.org | 3f03215 | 2014-05-01 17:41:32 +0000 | [diff] [blame] | 82 | // 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.org | b0c7156 | 2014-04-30 20:47:30 +0000 | [diff] [blame] | 84 | } |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 85 | |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 86 | } // namespace DM |