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 | |
| 8 | namespace DM { |
| 9 | |
| 10 | Task::Task(Reporter* reporter, TaskRunner* taskRunner) |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 11 | : fReporter(reporter) |
| 12 | , fTaskRunner(taskRunner) |
| 13 | , fDepth(0) { |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 14 | fReporter->start(); |
| 15 | } |
| 16 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 17 | Task::Task(const Task& parent) |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 18 | : fReporter(parent.fReporter) |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 19 | , fTaskRunner(parent.fTaskRunner) |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 20 | , fDepth(parent.depth() + 1) { |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 21 | fReporter->start(); |
| 22 | } |
| 23 | |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 24 | void 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.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 30 | } |
| 31 | |
commit-bot@chromium.org | a39874b | 2014-03-03 15:44:56 +0000 | [diff] [blame] | 32 | void Task::start() { |
| 33 | fStart = SkTime::GetMSecs(); |
| 34 | } |
| 35 | |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 36 | void Task::finish() { |
commit-bot@chromium.org | a39874b | 2014-03-03 15:44:56 +0000 | [diff] [blame] | 37 | fReporter->finish(this->name(), SkTime::GetMSecs() - fStart); |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 38 | } |
| 39 | |
commit-bot@chromium.org | 3f03215 | 2014-05-01 17:41:32 +0000 | [diff] [blame] | 40 | void Task::spawnChildNext(CpuTask* task) { |
| 41 | fTaskRunner->addNext(task); |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | CpuTask::CpuTask(Reporter* reporter, TaskRunner* taskRunner) : Task(reporter, taskRunner) {} |
| 45 | CpuTask::CpuTask(const Task& parent) : Task(parent) {} |
| 46 | |
| 47 | void CpuTask::run() { |
commit-bot@chromium.org | a39874b | 2014-03-03 15:44:56 +0000 | [diff] [blame] | 48 | this->start(); |
commit-bot@chromium.org | fa8cb1c | 2014-03-31 19:59:44 +0000 | [diff] [blame] | 49 | if (FLAGS_cpu && !this->shouldSkip()) { |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 50 | this->draw(); |
| 51 | } |
| 52 | this->finish(); |
| 53 | SkDELETE(this); |
| 54 | } |
| 55 | |
commit-bot@chromium.org | b0c7156 | 2014-04-30 20:47:30 +0000 | [diff] [blame] | 56 | void 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.org | 3f03215 | 2014-05-01 17:41:32 +0000 | [diff] [blame] | 58 | // 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.org | b0c7156 | 2014-04-30 20:47:30 +0000 | [diff] [blame] | 60 | task->run(); |
| 61 | } |
| 62 | |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 63 | GpuTask::GpuTask(Reporter* reporter, TaskRunner* taskRunner) : Task(reporter, taskRunner) {} |
| 64 | |
| 65 | void GpuTask::run(GrContextFactory& factory) { |
commit-bot@chromium.org | a39874b | 2014-03-03 15:44:56 +0000 | [diff] [blame] | 66 | this->start(); |
commit-bot@chromium.org | fa8cb1c | 2014-03-31 19:59:44 +0000 | [diff] [blame] | 67 | if (FLAGS_gpu && !this->shouldSkip()) { |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 68 | this->draw(&factory); |
| 69 | } |
| 70 | this->finish(); |
| 71 | SkDELETE(this); |
| 72 | } |
| 73 | |
commit-bot@chromium.org | b0c7156 | 2014-04-30 20:47:30 +0000 | [diff] [blame] | 74 | void 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.org | 3f03215 | 2014-05-01 17:41:32 +0000 | [diff] [blame] | 76 | // 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.org | b0c7156 | 2014-04-30 20:47:30 +0000 | [diff] [blame] | 78 | } |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 79 | |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 80 | } // namespace DM |