1 | // Copyright 2013 The Flutter 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 | #define FML_USED_ON_EMBEDDER |
6 | |
7 | #include "flutter/fml/task_source.h" |
8 | |
9 | namespace fml { |
10 | |
11 | TaskSource::TaskSource(TaskQueueId task_queue_id) |
12 | : task_queue_id_(task_queue_id) {} |
13 | |
14 | TaskSource::~TaskSource() { |
15 | ShutDown(); |
16 | } |
17 | |
18 | void TaskSource::ShutDown() { |
19 | primary_task_queue_ = {}; |
20 | secondary_task_queue_ = {}; |
21 | } |
22 | |
23 | void TaskSource::RegisterTask(const DelayedTask& task) { |
24 | switch (task.GetTaskSourceGrade()) { |
25 | case TaskSourceGrade::kUserInteraction: |
26 | primary_task_queue_.push(v: task); |
27 | break; |
28 | case TaskSourceGrade::kUnspecified: |
29 | primary_task_queue_.push(v: task); |
30 | break; |
31 | case TaskSourceGrade::kDartMicroTasks: |
32 | secondary_task_queue_.push(v: task); |
33 | break; |
34 | } |
35 | } |
36 | |
37 | void TaskSource::PopTask(TaskSourceGrade grade) { |
38 | switch (grade) { |
39 | case TaskSourceGrade::kUserInteraction: |
40 | primary_task_queue_.pop(); |
41 | break; |
42 | case TaskSourceGrade::kUnspecified: |
43 | primary_task_queue_.pop(); |
44 | break; |
45 | case TaskSourceGrade::kDartMicroTasks: |
46 | secondary_task_queue_.pop(); |
47 | break; |
48 | } |
49 | } |
50 | |
51 | size_t TaskSource::GetNumPendingTasks() const { |
52 | size_t size = primary_task_queue_.size(); |
53 | if (secondary_pause_requests_ == 0) { |
54 | size += secondary_task_queue_.size(); |
55 | } |
56 | return size; |
57 | } |
58 | |
59 | bool TaskSource::IsEmpty() const { |
60 | return GetNumPendingTasks() == 0; |
61 | } |
62 | |
63 | TaskSource::TopTask TaskSource::Top() const { |
64 | FML_CHECK(!IsEmpty()); |
65 | if (secondary_pause_requests_ > 0 || secondary_task_queue_.empty()) { |
66 | const auto& primary_top = primary_task_queue_.top(); |
67 | return { |
68 | .task_queue_id = task_queue_id_, |
69 | .task = primary_top, |
70 | }; |
71 | } else if (primary_task_queue_.empty()) { |
72 | const auto& secondary_top = secondary_task_queue_.top(); |
73 | return { |
74 | .task_queue_id = task_queue_id_, |
75 | .task = secondary_top, |
76 | }; |
77 | } else { |
78 | const auto& primary_top = primary_task_queue_.top(); |
79 | const auto& secondary_top = secondary_task_queue_.top(); |
80 | if (primary_top > secondary_top) { |
81 | return { |
82 | .task_queue_id = task_queue_id_, |
83 | .task = secondary_top, |
84 | }; |
85 | } else { |
86 | return { |
87 | .task_queue_id = task_queue_id_, |
88 | .task = primary_top, |
89 | }; |
90 | } |
91 | } |
92 | } |
93 | |
94 | void TaskSource::PauseSecondary() { |
95 | secondary_pause_requests_++; |
96 | } |
97 | |
98 | void TaskSource::ResumeSecondary() { |
99 | secondary_pause_requests_--; |
100 | FML_DCHECK(secondary_pause_requests_ >= 0); |
101 | } |
102 | |
103 | } // namespace fml |
104 |