mac: Don't limit GL_MAX_TEXTURE_SIZE on 10.9.0+.
[chromium-blink-merge.git] / cc / test / ordered_simple_task_runner.cc
blob9cad7970e25154fb1db114c22d352ad2c804cd15
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.
5 #include "cc/test/ordered_simple_task_runner.h"
7 #include <limits>
8 #include <set>
9 #include <sstream>
10 #include <string>
11 #include <vector>
13 #include "base/auto_reset.h"
14 #include "base/debug/trace_event.h"
15 #include "base/debug/trace_event_argument.h"
16 #include "base/strings/string_number_conversions.h"
18 #define TRACE_TASK(function, task) \
19 TRACE_EVENT_INSTANT1( \
20 "cc", function, TRACE_EVENT_SCOPE_THREAD, "task", task.AsValue());
22 #define TRACE_TASK_RUN(function, tag, task)
24 namespace cc {
26 // TestOrderablePendingTask implementation
27 TestOrderablePendingTask::TestOrderablePendingTask()
28 : base::TestPendingTask(),
29 task_id_(TestOrderablePendingTask::task_id_counter++) {
32 TestOrderablePendingTask::TestOrderablePendingTask(
33 const tracked_objects::Location& location,
34 const base::Closure& task,
35 base::TimeTicks post_time,
36 base::TimeDelta delay,
37 TestNestability nestability)
38 : base::TestPendingTask(location, task, post_time, delay, nestability),
39 task_id_(TestOrderablePendingTask::task_id_counter++) {
42 size_t TestOrderablePendingTask::task_id_counter = 0;
44 TestOrderablePendingTask::~TestOrderablePendingTask() {
47 bool TestOrderablePendingTask::operator==(
48 const TestOrderablePendingTask& other) const {
49 return task_id_ == other.task_id_;
52 bool TestOrderablePendingTask::operator<(
53 const TestOrderablePendingTask& other) const {
54 if (*this == other)
55 return false;
57 if (GetTimeToRun() == other.GetTimeToRun()) {
58 return task_id_ < other.task_id_;
60 return ShouldRunBefore(other);
63 scoped_refptr<base::debug::ConvertableToTraceFormat>
64 TestOrderablePendingTask::AsValue() const {
65 scoped_refptr<base::debug::TracedValue> state =
66 new base::debug::TracedValue();
67 AsValueInto(state.get());
68 return state;
71 void TestOrderablePendingTask::AsValueInto(
72 base::debug::TracedValue* state) const {
73 state->SetInteger("id", task_id_);
74 state->SetInteger("run_at", GetTimeToRun().ToInternalValue());
75 state->SetString("posted_from", location.ToString());
78 OrderedSimpleTaskRunner::OrderedSimpleTaskRunner()
79 : advance_now_(true),
80 now_src_(TestNowSource::Create(0)),
81 inside_run_tasks_until_(false) {
84 OrderedSimpleTaskRunner::OrderedSimpleTaskRunner(
85 scoped_refptr<TestNowSource> now_src,
86 bool advance_now)
87 : advance_now_(advance_now),
88 now_src_(now_src),
89 max_tasks_(kAbsoluteMaxTasks),
90 inside_run_tasks_until_(false) {
93 OrderedSimpleTaskRunner::~OrderedSimpleTaskRunner() {}
95 // base::TestSimpleTaskRunner implementation
96 bool OrderedSimpleTaskRunner::PostDelayedTask(
97 const tracked_objects::Location& from_here,
98 const base::Closure& task,
99 base::TimeDelta delay) {
100 DCHECK(thread_checker_.CalledOnValidThread());
101 TestOrderablePendingTask pt(
102 from_here, task, now_src_->Now(), delay, base::TestPendingTask::NESTABLE);
104 TRACE_TASK("OrderedSimpleTaskRunner::PostDelayedTask", pt);
105 pending_tasks_.insert(pt);
106 return true;
109 bool OrderedSimpleTaskRunner::PostNonNestableDelayedTask(
110 const tracked_objects::Location& from_here,
111 const base::Closure& task,
112 base::TimeDelta delay) {
113 DCHECK(thread_checker_.CalledOnValidThread());
114 TestOrderablePendingTask pt(from_here,
115 task,
116 now_src_->Now(),
117 delay,
118 base::TestPendingTask::NON_NESTABLE);
120 TRACE_TASK("OrderedSimpleTaskRunner::PostNonNestableDelayedTask", pt);
121 pending_tasks_.insert(pt);
122 return true;
125 bool OrderedSimpleTaskRunner::RunsTasksOnCurrentThread() const {
126 DCHECK(thread_checker_.CalledOnValidThread());
127 return true;
130 bool OrderedSimpleTaskRunner::HasPendingTasks() const {
131 return pending_tasks_.size() > 0;
134 base::TimeTicks OrderedSimpleTaskRunner::NextTaskTime() {
135 if (pending_tasks_.size() <= 0) {
136 return TestNowSource::kAbsoluteMaxNow;
139 return pending_tasks_.begin()->GetTimeToRun();
142 base::TimeDelta OrderedSimpleTaskRunner::DelayToNextTaskTime() {
143 DCHECK(thread_checker_.CalledOnValidThread());
145 if (pending_tasks_.size() <= 0) {
146 return TestNowSource::kAbsoluteMaxNow - base::TimeTicks();
149 base::TimeDelta delay = NextTaskTime() - now_src_->Now();
150 if (delay > base::TimeDelta())
151 return delay;
152 return base::TimeDelta();
155 const size_t OrderedSimpleTaskRunner::kAbsoluteMaxTasks =
156 std::numeric_limits<size_t>::max();
158 bool OrderedSimpleTaskRunner::RunTasksWhile(
159 base::Callback<bool(void)> condition) {
160 std::vector<base::Callback<bool(void)>> conditions(1);
161 conditions[0] = condition;
162 return RunTasksWhile(conditions);
165 bool OrderedSimpleTaskRunner::RunTasksWhile(
166 const std::vector<base::Callback<bool(void)>>& conditions) {
167 TRACE_EVENT2("cc",
168 "OrderedSimpleTaskRunner::RunPendingTasks",
169 "this",
170 AsValue(),
171 "nested",
172 inside_run_tasks_until_);
173 DCHECK(thread_checker_.CalledOnValidThread());
175 if (inside_run_tasks_until_)
176 return true;
178 base::AutoReset<bool> reset_inside_run_tasks_until_(&inside_run_tasks_until_,
179 true);
181 // Make a copy so we can append some extra run checks.
182 std::vector<base::Callback<bool(void)>> modifiable_conditions(conditions);
184 // Provide a timeout base on number of tasks run so this doesn't loop
185 // forever.
186 modifiable_conditions.push_back(TaskRunCountBelow(max_tasks_));
188 // If to advance now or not
189 if (!advance_now_) {
190 modifiable_conditions.push_back(NowBefore(now_src_->Now()));
191 } else {
192 modifiable_conditions.push_back(AdvanceNow());
195 while (pending_tasks_.size() > 0) {
196 // Check if we should continue to run pending tasks.
197 bool condition_success = true;
198 for (std::vector<base::Callback<bool(void)>>::iterator it =
199 modifiable_conditions.begin();
200 it != modifiable_conditions.end();
201 it++) {
202 condition_success = it->Run();
203 if (!condition_success)
204 break;
207 // Conditions could modify the pending task length, so we need to recheck
208 // that there are tasks to run.
209 if (!condition_success || !HasPendingTasks()) {
210 break;
213 std::set<TestOrderablePendingTask>::iterator task_to_run =
214 pending_tasks_.begin();
216 TRACE_EVENT1("cc",
217 "OrderedSimpleTaskRunner::RunPendingTasks running",
218 "task",
219 task_to_run->AsValue());
220 task_to_run->task.Run();
223 pending_tasks_.erase(task_to_run);
226 return HasPendingTasks();
229 bool OrderedSimpleTaskRunner::RunPendingTasks() {
230 return RunTasksWhile(TaskExistedInitially());
233 bool OrderedSimpleTaskRunner::RunUntilIdle() {
234 return RunTasksWhile(std::vector<base::Callback<bool(void)>>());
237 bool OrderedSimpleTaskRunner::RunUntilTime(base::TimeTicks time) {
238 // If we are not auto advancing, force now forward to the time.
239 if (!advance_now_ && now_src_->Now() < time)
240 now_src_->SetNow(time);
242 // Run tasks
243 bool result = RunTasksWhile(NowBefore(time));
245 // If the next task is after the stopping time and auto-advancing now, then
246 // force time to be the stopping time.
247 if (!result && advance_now_ && now_src_->Now() < time) {
248 now_src_->SetNow(time);
251 return result;
254 bool OrderedSimpleTaskRunner::RunForPeriod(base::TimeDelta period) {
255 return RunUntilTime(now_src_->Now() + period);
258 // base::debug tracing functionality
259 scoped_refptr<base::debug::ConvertableToTraceFormat>
260 OrderedSimpleTaskRunner::AsValue() const {
261 scoped_refptr<base::debug::TracedValue> state =
262 new base::debug::TracedValue();
263 AsValueInto(state.get());
264 return state;
267 void OrderedSimpleTaskRunner::AsValueInto(
268 base::debug::TracedValue* state) const {
269 state->SetInteger("pending_tasks", pending_tasks_.size());
271 state->BeginArray("tasks");
272 for (std::set<TestOrderablePendingTask>::const_iterator it =
273 pending_tasks_.begin();
274 it != pending_tasks_.end();
275 ++it) {
276 state->BeginDictionary();
277 it->AsValueInto(state);
278 state->EndDictionary();
280 state->EndArray();
282 state->BeginDictionary("now_src");
283 now_src_->AsValueInto(state);
284 state->EndDictionary();
287 base::Callback<bool(void)> OrderedSimpleTaskRunner::TaskRunCountBelow(
288 size_t max_tasks) {
289 return base::Bind(&OrderedSimpleTaskRunner::TaskRunCountBelowCallback,
290 max_tasks,
291 base::Owned(new size_t(0)));
294 bool OrderedSimpleTaskRunner::TaskRunCountBelowCallback(size_t max_tasks,
295 size_t* tasks_run) {
296 return (*tasks_run)++ < max_tasks;
299 base::Callback<bool(void)> OrderedSimpleTaskRunner::TaskExistedInitially() {
300 // base::Bind takes a copy of pending_tasks_
301 return base::Bind(&OrderedSimpleTaskRunner::TaskExistedInitiallyCallback,
302 base::Unretained(this),
303 pending_tasks_);
306 bool OrderedSimpleTaskRunner::TaskExistedInitiallyCallback(
307 const std::set<TestOrderablePendingTask>& existing_tasks) {
308 return existing_tasks.find(*pending_tasks_.begin()) != existing_tasks.end();
311 base::Callback<bool(void)> OrderedSimpleTaskRunner::NowBefore(
312 base::TimeTicks stop_at) {
313 return base::Bind(&OrderedSimpleTaskRunner::NowBeforeCallback,
314 base::Unretained(this),
315 stop_at);
317 bool OrderedSimpleTaskRunner::NowBeforeCallback(base::TimeTicks stop_at) {
318 return NextTaskTime() <= stop_at;
321 base::Callback<bool(void)> OrderedSimpleTaskRunner::AdvanceNow() {
322 return base::Bind(&OrderedSimpleTaskRunner::AdvanceNowCallback,
323 base::Unretained(this));
326 bool OrderedSimpleTaskRunner::AdvanceNowCallback() {
327 base::TimeTicks next_task_time = NextTaskTime();
328 if (now_src_->Now() < next_task_time) {
329 now_src_->SetNow(next_task_time);
331 return true;
334 } // namespace cc