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" |
caryclark | 17f0b6d | 2014-07-22 10:15:34 -0700 | [diff] [blame] | 3 | #include "SkCommonFlags.h" |
commit-bot@chromium.org | a65e2fd | 2014-05-30 17:23:31 +0000 | [diff] [blame] | 4 | |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 5 | namespace DM { |
| 6 | |
| 7 | Task::Task(Reporter* reporter, TaskRunner* taskRunner) |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 8 | : fReporter(reporter) |
| 9 | , fTaskRunner(taskRunner) |
| 10 | , fDepth(0) { |
commit-bot@chromium.org | 39e8d93 | 2014-05-29 20:14:48 +0000 | [diff] [blame] | 11 | fReporter->taskCreated(); |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 12 | } |
| 13 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 14 | Task::Task(const Task& parent) |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 15 | : fReporter(parent.fReporter) |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 16 | , fTaskRunner(parent.fTaskRunner) |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 17 | , fDepth(parent.depth() + 1) { |
commit-bot@chromium.org | 39e8d93 | 2014-05-29 20:14:48 +0000 | [diff] [blame] | 18 | fReporter->taskCreated(); |
| 19 | } |
| 20 | |
| 21 | Task::~Task() { |
| 22 | fReporter->taskDestroyed(); |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 23 | } |
| 24 | |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 25 | void 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.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 31 | } |
| 32 | |
commit-bot@chromium.org | a39874b | 2014-03-03 15:44:56 +0000 | [diff] [blame] | 33 | void Task::start() { |
| 34 | fStart = SkTime::GetMSecs(); |
| 35 | } |
| 36 | |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 37 | void Task::finish() { |
commit-bot@chromium.org | 39e8d93 | 2014-05-29 20:14:48 +0000 | [diff] [blame] | 38 | fReporter->printStatus(this->name(), SkTime::GetMSecs() - fStart); |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 39 | } |
| 40 | |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 41 | void Task::reallySpawnChild(CpuTask* task) { |
| 42 | fTaskRunner->add(task); |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | CpuTask::CpuTask(Reporter* reporter, TaskRunner* taskRunner) : Task(reporter, taskRunner) {} |
| 46 | CpuTask::CpuTask(const Task& parent) : Task(parent) {} |
| 47 | |
| 48 | void CpuTask::run() { |
mtklein | 53e0be6 | 2014-09-17 12:26:18 -0700 | [diff] [blame] | 49 | // If the task says skip, or if we're starting a top-level CPU task and we don't want to, skip. |
| 50 | const bool skip = this->shouldSkip() || (this->depth() == 0 && !FLAGS_cpu); |
| 51 | if (!skip) { |
commit-bot@chromium.org | 39e8d93 | 2014-05-29 20:14:48 +0000 | [diff] [blame] | 52 | this->start(); |
commit-bot@chromium.org | a65e2fd | 2014-05-30 17:23:31 +0000 | [diff] [blame] | 53 | if (!FLAGS_dryRun) this->draw(); |
commit-bot@chromium.org | 39e8d93 | 2014-05-29 20:14:48 +0000 | [diff] [blame] | 54 | this->finish(); |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 55 | } |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 56 | SkDELETE(this); |
| 57 | } |
| 58 | |
commit-bot@chromium.org | b0c7156 | 2014-04-30 20:47:30 +0000 | [diff] [blame] | 59 | void CpuTask::spawnChild(CpuTask* task) { |
| 60 | // Run children serially on this (CPU) thread. This tends to save RAM and is usually no slower. |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 61 | // Calling reallySpawnChild() is nearly equivalent, but it'd pointlessly contend on the |
| 62 | // threadpool; reallySpawnChild() is most useful when you want to change threadpools. |
commit-bot@chromium.org | b0c7156 | 2014-04-30 20:47:30 +0000 | [diff] [blame] | 63 | task->run(); |
| 64 | } |
| 65 | |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 66 | GpuTask::GpuTask(Reporter* reporter, TaskRunner* taskRunner) : Task(reporter, taskRunner) {} |
| 67 | |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 68 | void GpuTask::run(GrContextFactory* factory) { |
mtklein | 53e0be6 | 2014-09-17 12:26:18 -0700 | [diff] [blame] | 69 | // If the task says skip, or if we're starting a top-level GPU task and we don't want to, skip. |
| 70 | const bool skip = this->shouldSkip() || (this->depth() == 0 && !FLAGS_gpu); |
| 71 | if (!skip) { |
commit-bot@chromium.org | 39e8d93 | 2014-05-29 20:14:48 +0000 | [diff] [blame] | 72 | this->start(); |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 73 | if (!FLAGS_dryRun) this->draw(factory); |
commit-bot@chromium.org | 39e8d93 | 2014-05-29 20:14:48 +0000 | [diff] [blame] | 74 | this->finish(); |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 75 | if (FLAGS_abandonGpuContext) { |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 76 | factory->abandonContexts(); |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 77 | } |
| 78 | if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) { |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 79 | factory->destroyContexts(); |
Mike Klein | 18515cf | 2014-07-15 16:10:02 -0400 | [diff] [blame] | 80 | } |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 81 | } |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 82 | SkDELETE(this); |
| 83 | } |
| 84 | |
commit-bot@chromium.org | b0c7156 | 2014-04-30 20:47:30 +0000 | [diff] [blame] | 85 | void GpuTask::spawnChild(CpuTask* task) { |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 86 | // 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] | 87 | // It goes on the front of the queue to minimize the time we must hold reference bitmaps in RAM. |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 88 | this->reallySpawnChild(task); |
commit-bot@chromium.org | b0c7156 | 2014-04-30 20:47:30 +0000 | [diff] [blame] | 89 | } |
commit-bot@chromium.org | ef57b7e | 2014-02-28 20:31:31 +0000 | [diff] [blame] | 90 | |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 91 | } // namespace DM |