OLD | NEW |
| (Empty) |
1 // Copyright 2013 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 #ifndef CC_WORKER_POOL_H_ | |
6 #define CC_WORKER_POOL_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/cancelable_callback.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/memory/weak_ptr.h" | |
13 #include "base/message_loop.h" | |
14 #include "cc/cc_export.h" | |
15 #include "cc/scoped_ptr_deque.h" | |
16 | |
17 namespace cc { | |
18 struct RenderingStats; | |
19 | |
20 namespace internal { | |
21 | |
22 class WorkerPoolTask { | |
23 public: | |
24 virtual ~WorkerPoolTask(); | |
25 | |
26 virtual bool IsCheap() = 0; | |
27 | |
28 virtual void Run(RenderingStats* rendering_stats) = 0; | |
29 | |
30 virtual void RunOnThread( | |
31 RenderingStats* rendering_stats, unsigned thread_index) = 0; | |
32 | |
33 void DidComplete(); | |
34 | |
35 protected: | |
36 WorkerPoolTask(const base::Closure& reply); | |
37 | |
38 const base::Closure reply_; | |
39 }; | |
40 | |
41 } // namespace internal | |
42 | |
43 class CC_EXPORT WorkerPoolClient { | |
44 public: | |
45 virtual void DidFinishDispatchingWorkerPoolCompletionCallbacks() = 0; | |
46 | |
47 protected: | |
48 virtual ~WorkerPoolClient() {} | |
49 }; | |
50 | |
51 // A worker thread pool that runs rendering tasks and guarantees completion | |
52 // of all pending tasks at shutdown. | |
53 class WorkerPool { | |
54 public: | |
55 typedef base::Callback<void(RenderingStats*)> Callback; | |
56 | |
57 virtual ~WorkerPool(); | |
58 | |
59 static scoped_ptr<WorkerPool> Create( | |
60 WorkerPoolClient* client, | |
61 size_t num_threads, | |
62 base::TimeDelta check_for_completed_tasks_delay, | |
63 const std::string& thread_name_prefix) { | |
64 return make_scoped_ptr(new WorkerPool(client, | |
65 num_threads, | |
66 check_for_completed_tasks_delay, | |
67 thread_name_prefix)); | |
68 } | |
69 | |
70 // Tells the worker pool to shutdown and returns once all pending tasks have | |
71 // completed. | |
72 void Shutdown(); | |
73 | |
74 // Posts |task| to worker pool. On completion, |reply| | |
75 // is posted to the thread that called PostTaskAndReply(). | |
76 void PostTaskAndReply(const Callback& task, const base::Closure& reply); | |
77 | |
78 // Set time limit for running cheap tasks. | |
79 void SetRunCheapTasksTimeLimit(base::TimeTicks run_cheap_tasks_time_limit); | |
80 | |
81 // Toggle rendering stats collection. | |
82 void SetRecordRenderingStats(bool record_rendering_stats); | |
83 | |
84 // Collect rendering stats of all completed tasks. | |
85 void GetRenderingStats(RenderingStats* stats); | |
86 | |
87 protected: | |
88 WorkerPool(WorkerPoolClient* client, | |
89 size_t num_threads, | |
90 base::TimeDelta check_for_completed_tasks_delay, | |
91 const std::string& thread_name_prefix); | |
92 | |
93 void PostTask(scoped_ptr<internal::WorkerPoolTask> task); | |
94 | |
95 private: | |
96 class Inner; | |
97 friend class Inner; | |
98 | |
99 void OnTaskCompleted(); | |
100 void OnIdle(); | |
101 void ScheduleCheckForCompletedTasks(); | |
102 void CheckForCompletedTasks(); | |
103 void CancelCheckForCompletedTasks(); | |
104 void DispatchCompletionCallbacks(); | |
105 void ScheduleRunCheapTasks(); | |
106 void RunCheapTasks(); | |
107 | |
108 WorkerPoolClient* client_; | |
109 scoped_refptr<base::MessageLoopProxy> origin_loop_; | |
110 base::WeakPtrFactory<WorkerPool> weak_ptr_factory_; | |
111 base::TimeTicks check_for_completed_tasks_time_; | |
112 base::TimeDelta check_for_completed_tasks_delay_; | |
113 base::CancelableClosure check_for_completed_tasks_callback_; | |
114 bool check_for_completed_tasks_pending_; | |
115 const base::Closure run_cheap_tasks_callback_; | |
116 base::TimeTicks run_cheap_tasks_time_limit_; | |
117 bool run_cheap_tasks_pending_; | |
118 | |
119 // Holds all completed tasks for which we have not yet dispatched | |
120 // reply callbacks. | |
121 ScopedPtrDeque<internal::WorkerPoolTask> completed_tasks_; | |
122 | |
123 // Hide the gory details of the worker pool in |inner_|. | |
124 const scoped_ptr<Inner> inner_; | |
125 | |
126 DISALLOW_COPY_AND_ASSIGN(WorkerPool); | |
127 }; | |
128 | |
129 } // namespace cc | |
130 | |
131 #endif // CC_WORKER_POOL_H_ | |
OLD | NEW |