OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/mus/gles2/command_buffer_task_runner.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/threading/thread_task_runner_handle.h" | |
9 #include "components/mus/gles2/command_buffer_driver.h" | |
10 | |
11 namespace mus { | |
12 | |
13 CommandBufferTaskRunner::CommandBufferTaskRunner() | |
14 : task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
15 need_post_task_(true) { | |
16 } | |
17 | |
18 bool CommandBufferTaskRunner::PostTask( | |
19 const CommandBufferDriver* driver, | |
20 const TaskCallback& task) { | |
21 base::AutoLock locker(lock_); | |
22 driver_map_[driver].push_back(task); | |
23 ScheduleTaskIfNecessaryLocked(); | |
24 return true; | |
25 } | |
26 | |
27 CommandBufferTaskRunner::~CommandBufferTaskRunner() {} | |
28 | |
29 bool CommandBufferTaskRunner::RunOneTaskInternalLocked() { | |
30 DCHECK(thread_checker_.CalledOnValidThread()); | |
31 lock_.AssertAcquired(); | |
32 | |
33 for (auto it = driver_map_.begin(); it != driver_map_.end(); ++it) { | |
34 if (!it->first->IsScheduled()) | |
35 continue; | |
36 TaskQueue& task_queue = it->second; | |
37 DCHECK(!task_queue.empty()); | |
38 const TaskCallback& callback = task_queue.front(); | |
39 bool complete = false; | |
40 { | |
41 base::AutoUnlock unlocker(lock_); | |
42 complete = callback.Run(); | |
43 } | |
44 if (complete) { | |
45 // Only remove the task if it is complete. | |
46 task_queue.pop_front(); | |
47 if (task_queue.empty()) | |
48 driver_map_.erase(it); | |
49 } | |
50 return true; | |
51 } | |
52 return false; | |
53 } | |
54 | |
55 void CommandBufferTaskRunner::ScheduleTaskIfNecessaryLocked() { | |
56 lock_.AssertAcquired(); | |
57 if (!need_post_task_) | |
58 return; | |
59 task_runner()->PostTask( | |
60 FROM_HERE, | |
61 base::Bind(&CommandBufferTaskRunner::RunCommandBufferTask, this)); | |
62 need_post_task_ = false; | |
63 } | |
64 | |
65 void CommandBufferTaskRunner::RunCommandBufferTask() { | |
66 DCHECK(thread_checker_.CalledOnValidThread()); | |
67 base::AutoLock locker(lock_); | |
68 | |
69 // Try executing all tasks in |driver_map_| until |RunOneTaskInternalLock()| | |
70 // returns false (Returning false means |driver_map_| is empty or all tasks | |
71 // in it aren't executable currently). | |
72 while (RunOneTaskInternalLocked()) | |
73 ; | |
74 need_post_task_ = true; | |
75 } | |
76 | |
77 } // namespace mus | |
OLD | NEW |