forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raster_thread_merger.cc
181 lines (150 loc) · 4.81 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// 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 <utility>
#include "flutter/fml/message_loop_impl.h"
namespace fml {
RasterThreadMerger::RasterThreadMerger(fml::TaskQueueId platform_queue_id,
fml::TaskQueueId gpu_queue_id)
: RasterThreadMerger(
MakeRefCounted<SharedThreadMerger>(platform_queue_id, gpu_queue_id),
platform_queue_id,
gpu_queue_id) {}
RasterThreadMerger::RasterThreadMerger(
fml::RefPtr<fml::SharedThreadMerger> shared_merger,
fml::TaskQueueId platform_queue_id,
fml::TaskQueueId gpu_queue_id)
: platform_queue_id_(platform_queue_id),
gpu_queue_id_(gpu_queue_id),
shared_merger_(std::move(shared_merger)) {}
void RasterThreadMerger::SetMergeUnmergeCallback(const fml::closure& callback) {
merge_unmerge_callback_ = callback;
}
const fml::RefPtr<fml::SharedThreadMerger>&
RasterThreadMerger::GetSharedRasterThreadMerger() const {
return shared_merger_;
}
fml::RefPtr<fml::RasterThreadMerger>
RasterThreadMerger::CreateOrShareThreadMerger(
const fml::RefPtr<fml::RasterThreadMerger>& parent_merger,
TaskQueueId platform_id,
TaskQueueId raster_id) {
if (parent_merger && parent_merger->platform_queue_id_ == platform_id &&
parent_merger->gpu_queue_id_ == raster_id) {
auto shared_merger = parent_merger->GetSharedRasterThreadMerger();
return fml::MakeRefCounted<RasterThreadMerger>(shared_merger, platform_id,
raster_id);
} else {
return fml::MakeRefCounted<RasterThreadMerger>(platform_id, raster_id);
}
}
void RasterThreadMerger::MergeWithLease(size_t lease_term) {
std::scoped_lock lock(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 = shared_merger_->MergeWithLease(this, lease_term);
if (success && merge_unmerge_callback_ != nullptr) {
merge_unmerge_callback_();
}
merged_condition_.notify_one();
}
void RasterThreadMerger::UnMergeNowIfLastOne() {
std::scoped_lock lock(mutex_);
if (TaskQueuesAreSame()) {
return;
}
if (!IsEnabledUnSafe()) {
return;
}
bool success = shared_merger_->UnMergeNowIfLastOne(this);
if (success && merge_unmerge_callback_ != nullptr) {
merge_unmerge_callback_();
}
}
bool RasterThreadMerger::IsOnPlatformThread() const {
return MessageLoop::GetCurrentTaskQueueId() == platform_queue_id_;
}
bool RasterThreadMerger::IsOnRasterizingThread() {
std::scoped_lock lock(mutex_);
if (IsMergedUnSafe()) {
return IsOnPlatformThread();
} else {
return !IsOnPlatformThread();
}
}
void RasterThreadMerger::ExtendLeaseTo(size_t lease_term) {
FML_DCHECK(lease_term > 0) << "lease_term should be positive.";
if (TaskQueuesAreSame()) {
return;
}
std::scoped_lock lock(mutex_);
if (!IsEnabledUnSafe()) {
return;
}
shared_merger_->ExtendLeaseTo(this, lease_term);
}
bool RasterThreadMerger::IsMerged() {
std::scoped_lock lock(mutex_);
return IsMergedUnSafe();
}
void RasterThreadMerger::Enable() {
std::scoped_lock lock(mutex_);
shared_merger_->SetEnabledUnSafe(true);
}
void RasterThreadMerger::Disable() {
std::scoped_lock lock(mutex_);
shared_merger_->SetEnabledUnSafe(false);
}
bool RasterThreadMerger::IsEnabled() {
std::scoped_lock lock(mutex_);
return IsEnabledUnSafe();
}
bool RasterThreadMerger::IsEnabledUnSafe() const {
return shared_merger_->IsEnabledUnSafe();
}
bool RasterThreadMerger::IsMergedUnSafe() const {
return TaskQueuesAreSame() || shared_merger_->IsMergedUnSafe();
}
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(mutex_);
merged_condition_.wait(lock, [&] { return IsMergedUnSafe(); });
}
RasterThreadStatus RasterThreadMerger::DecrementLease() {
if (TaskQueuesAreSame()) {
return RasterThreadStatus::kRemainsMerged;
}
std::scoped_lock lock(mutex_);
if (!IsMergedUnSafe()) {
return RasterThreadStatus::kRemainsUnmerged;
}
if (!IsEnabledUnSafe()) {
return RasterThreadStatus::kRemainsMerged;
}
bool unmerged_after_decrement = shared_merger_->DecrementLease(this);
if (unmerged_after_decrement) {
if (merge_unmerge_callback_ != nullptr) {
merge_unmerge_callback_();
}
return RasterThreadStatus::kUnmergedNow;
}
return RasterThreadStatus::kRemainsMerged;
}
} // namespace fml