forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raster_thread_merger.cc
159 lines (133 loc) · 4.16 KB
/
raster_thread_merger.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#define FML_USED_ON_EMBEDDER
#include "flutter/fml/raster_thread_merger.h"
#include "flutter/fml/message_loop_impl.h"
namespace fml {
const int RasterThreadMerger::kLeaseNotSet = -1;
RasterThreadMerger::RasterThreadMerger(fml::TaskQueueId platform_queue_id,
fml::TaskQueueId gpu_queue_id)
: platform_queue_id_(platform_queue_id),
gpu_queue_id_(gpu_queue_id),
task_queues_(fml::MessageLoopTaskQueues::GetInstance()),
lease_term_(kLeaseNotSet),
enabled_(true) {
FML_CHECK(!task_queues_->Owns(platform_queue_id_, gpu_queue_id_));
}
void RasterThreadMerger::SetMergeUnmergeCallback(const fml::closure& callback) {
merge_unmerge_callback_ = callback;
}
void RasterThreadMerger::MergeWithLease(size_t lease_term) {
std::scoped_lock lock(lease_term_mutex_);
if (TaskQueuesAreSame()) {
return;
}
if (!IsEnabledUnSafe()) {
return;
}
FML_DCHECK(lease_term > 0) << "lease_term should be positive.";
if (IsMergedUnSafe()) {
merged_condition_.notify_one();
return;
}
bool success = task_queues_->Merge(platform_queue_id_, gpu_queue_id_);
if (success && merge_unmerge_callback_ != nullptr) {
merge_unmerge_callback_();
}
FML_CHECK(success) << "Unable to merge the raster and platform threads.";
lease_term_ = lease_term;
merged_condition_.notify_one();
}
void RasterThreadMerger::UnMergeNow() {
std::scoped_lock lock(lease_term_mutex_);
if (TaskQueuesAreSame()) {
return;
}
if (!IsEnabledUnSafe()) {
return;
}
lease_term_ = 0;
bool success = task_queues_->Unmerge(platform_queue_id_);
if (success && merge_unmerge_callback_ != nullptr) {
merge_unmerge_callback_();
}
FML_CHECK(success) << "Unable to un-merge the raster and platform threads.";
}
bool RasterThreadMerger::IsOnPlatformThread() const {
return MessageLoop::GetCurrentTaskQueueId() == platform_queue_id_;
}
bool RasterThreadMerger::IsOnRasterizingThread() const {
if (IsMergedUnSafe()) {
return IsOnPlatformThread();
} else {
return !IsOnPlatformThread();
}
}
void RasterThreadMerger::ExtendLeaseTo(size_t lease_term) {
if (TaskQueuesAreSame()) {
return;
}
std::scoped_lock lock(lease_term_mutex_);
FML_DCHECK(IsMergedUnSafe()) << "lease_term should be positive.";
if (lease_term_ != kLeaseNotSet &&
static_cast<int>(lease_term) > lease_term_) {
lease_term_ = lease_term;
}
}
bool RasterThreadMerger::IsMerged() {
std::scoped_lock lock(lease_term_mutex_);
return IsMergedUnSafe();
}
void RasterThreadMerger::Enable() {
std::scoped_lock lock(lease_term_mutex_);
enabled_ = true;
}
void RasterThreadMerger::Disable() {
std::scoped_lock lock(lease_term_mutex_);
enabled_ = false;
}
bool RasterThreadMerger::IsEnabled() {
std::scoped_lock lock(lease_term_mutex_);
return IsEnabledUnSafe();
}
bool RasterThreadMerger::IsEnabledUnSafe() const {
return enabled_;
}
bool RasterThreadMerger::IsMergedUnSafe() const {
return lease_term_ > 0 || TaskQueuesAreSame();
}
bool RasterThreadMerger::TaskQueuesAreSame() const {
return platform_queue_id_ == gpu_queue_id_;
}
void RasterThreadMerger::WaitUntilMerged() {
if (TaskQueuesAreSame()) {
return;
}
FML_CHECK(IsOnPlatformThread());
std::unique_lock<std::mutex> lock(lease_term_mutex_);
merged_condition_.wait(lock, [&] { return IsMergedUnSafe(); });
}
RasterThreadStatus RasterThreadMerger::DecrementLease() {
if (TaskQueuesAreSame()) {
return RasterThreadStatus::kRemainsMerged;
}
std::unique_lock<std::mutex> lock(lease_term_mutex_);
if (!IsMergedUnSafe()) {
return RasterThreadStatus::kRemainsUnmerged;
}
if (!IsEnabledUnSafe()) {
return RasterThreadStatus::kRemainsMerged;
}
FML_DCHECK(lease_term_ > 0)
<< "lease_term should always be positive when merged.";
lease_term_--;
if (lease_term_ == 0) {
// |UnMergeNow| is going to acquire the lock again.
lock.unlock();
UnMergeNow();
return RasterThreadStatus::kUnmergedNow;
}
return RasterThreadStatus::kRemainsMerged;
}
} // namespace fml