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 | 1e319f7 | 2014-07-15 08:27:06 -0700 | [diff] [blame] | 7 | DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each task."); |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 8 | |
commit-bot@chromium.org | a65e2fd | 2014-05-30 17:23:31 +0000 | [diff] [blame] | 9 | DECLARE_bool(dryRun); |
| 10 | |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 11 | namespace DM { |
| 12 | |
| 13 | Task::Task(Reporter* reporter, TaskRunner* taskRunner) |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 14 | : fReporter(reporter) |
| 15 | , fTaskRunner(taskRunner) |
| 16 | , fDepth(0) { |
commit-bot@chromium.org | 39e8d93 | 2014-05-29 20:14:48 +0000 | [diff] [blame] | 17 | fReporter->taskCreated(); |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 18 | } |
| 19 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 20 | Task::Task(const Task& parent) |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 21 | : fReporter(parent.fReporter) |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 22 | , fTaskRunner(parent.fTaskRunner) |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 23 | , fDepth(parent.depth() + 1) { |
commit-bot@chromium.org | 39e8d93 | 2014-05-29 20:14:48 +0000 | [diff] [blame] | 24 | fReporter->taskCreated(); |
| 25 | } |
| 26 | |
| 27 | Task::~Task() { |
| 28 | fReporter->taskDestroyed(); |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 29 | } |
| 30 | |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 31 | void 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.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 37 | } |
| 38 | |
commit-bot@chromium.org | a39874b | 2014-03-03 15:44:56 +0000 | [diff] [blame] | 39 | void Task::start() { |
| 40 | fStart = SkTime::GetMSecs(); |
| 41 | } |
| 42 | |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 43 | void Task::finish() { |
commit-bot@chromium.org | 39e8d93 | 2014-05-29 20:14:48 +0000 | [diff] [blame] | 44 | fReporter->printStatus(this->name(), SkTime::GetMSecs() - fStart); |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 45 | } |
| 46 | |
commit-bot@chromium.org | 3f03215 | 2014-05-01 17:41:32 +0000 | [diff] [blame] | 47 | void Task::spawnChildNext(CpuTask* task) { |
| 48 | fTaskRunner->addNext(task); |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | CpuTask::CpuTask(Reporter* reporter, TaskRunner* taskRunner) : Task(reporter, taskRunner) {} |
| 52 | CpuTask::CpuTask(const Task& parent) : Task(parent) {} |
| 53 | |
| 54 | void CpuTask::run() { |
commit-bot@chromium.org | fa8cb1c | 2014-03-31 19:59:44 +0000 | [diff] [blame] | 55 | if (FLAGS_cpu && !this->shouldSkip()) { |
commit-bot@chromium.org | 39e8d93 | 2014-05-29 20:14:48 +0000 | [diff] [blame] | 56 | this->start(); |
commit-bot@chromium.org | a65e2fd | 2014-05-30 17:23:31 +0000 | [diff] [blame] | 57 | if (!FLAGS_dryRun) this->draw(); |
commit-bot@chromium.org | 39e8d93 | 2014-05-29 20:14:48 +0000 | [diff] [blame] | 58 | this->finish(); |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 59 | } |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 60 | SkDELETE(this); |
| 61 | } |
| 62 | |
commit-bot@chromium.org | b0c7156 | 2014-04-30 20:47:30 +0000 | [diff] [blame] | 63 | void 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.org | 3f03215 | 2014-05-01 17:41:32 +0000 | [diff] [blame] | 65 | // 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.org | b0c7156 | 2014-04-30 20:47:30 +0000 | [diff] [blame] | 67 | task->run(); |
| 68 | } |
| 69 | |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 70 | GpuTask::GpuTask(Reporter* reporter, TaskRunner* taskRunner) : Task(reporter, taskRunner) {} |
| 71 | |
| 72 | void GpuTask::run(GrContextFactory& factory) { |
commit-bot@chromium.org | fa8cb1c | 2014-03-31 19:59:44 +0000 | [diff] [blame] | 73 | if (FLAGS_gpu && !this->shouldSkip()) { |
commit-bot@chromium.org | 39e8d93 | 2014-05-29 20:14:48 +0000 | [diff] [blame] | 74 | this->start(); |
commit-bot@chromium.org | a65e2fd | 2014-05-30 17:23:31 +0000 | [diff] [blame] | 75 | if (!FLAGS_dryRun) this->draw(&factory); |
commit-bot@chromium.org | 39e8d93 | 2014-05-29 20:14:48 +0000 | [diff] [blame] | 76 | this->finish(); |
Mike Klein | 18515cf | 2014-07-15 16:10:02 -0400 | [diff] [blame^] | 77 | if (FLAGS_resetGpuContext) { |
| 78 | factory.destroyContexts(); |
| 79 | } |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 80 | } |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 81 | SkDELETE(this); |
| 82 | } |
| 83 | |
commit-bot@chromium.org | b0c7156 | 2014-04-30 20:47:30 +0000 | [diff] [blame] | 84 | void 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.org | 3f03215 | 2014-05-01 17:41:32 +0000 | [diff] [blame] | 86 | // 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.org | b0c7156 | 2014-04-30 20:47:30 +0000 | [diff] [blame] | 88 | } |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 89 | |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 90 | } // namespace DM |