Flutter Engine
raster_thread_merger.cc
Go to the documentation of this file.
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/raster_thread_merger.h"
8 
9 #include "flutter/fml/message_loop_impl.h"
10 
11 namespace fml {
12 
13 RasterThreadMerger::RasterThreadMerger(fml::TaskQueueId platform_queue_id,
14  fml::TaskQueueId gpu_queue_id)
15  : RasterThreadMerger(
16  MakeRefCounted<SharedThreadMerger>(platform_queue_id, gpu_queue_id),
17  platform_queue_id,
18  gpu_queue_id) {}
19 
20 RasterThreadMerger::RasterThreadMerger(
22  fml::TaskQueueId platform_queue_id,
23  fml::TaskQueueId gpu_queue_id)
24  : platform_queue_id_(platform_queue_id),
25  gpu_queue_id_(gpu_queue_id),
26  shared_merger_(shared_merger) {}
27 
29  merge_unmerge_callback_ = callback;
30 }
31 
34  return shared_merger_;
35 }
36 
39  const fml::RefPtr<fml::RasterThreadMerger>& parent_merger,
40  TaskQueueId platform_id,
41  TaskQueueId raster_id) {
42  if (parent_merger && parent_merger->platform_queue_id_ == platform_id &&
43  parent_merger->gpu_queue_id_ == raster_id) {
44  auto shared_merger = parent_merger->GetSharedRasterThreadMerger();
45  return fml::MakeRefCounted<RasterThreadMerger>(shared_merger, platform_id,
46  raster_id);
47  } else {
48  return fml::MakeRefCounted<RasterThreadMerger>(platform_id, raster_id);
49  }
50 }
51 
52 void RasterThreadMerger::MergeWithLease(size_t lease_term) {
53  std::scoped_lock lock(mutex_);
54  if (TaskQueuesAreSame()) {
55  return;
56  }
57  if (!IsEnabledUnSafe()) {
58  return;
59  }
60  FML_DCHECK(lease_term > 0) << "lease_term should be positive.";
61 
62  if (IsMergedUnSafe()) {
63  merged_condition_.notify_one();
64  return;
65  }
66 
67  bool success = shared_merger_->MergeWithLease(this, lease_term);
68  if (success && merge_unmerge_callback_ != nullptr) {
69  merge_unmerge_callback_();
70  }
71 
72  merged_condition_.notify_one();
73 }
74 
76  std::scoped_lock lock(mutex_);
77 
78  if (TaskQueuesAreSame()) {
79  return;
80  }
81  if (!IsEnabledUnSafe()) {
82  return;
83  }
84  bool success = shared_merger_->UnMergeNowIfLastOne(this);
85  if (success && merge_unmerge_callback_ != nullptr) {
86  merge_unmerge_callback_();
87  }
88 }
89 
91  return MessageLoop::GetCurrentTaskQueueId() == platform_queue_id_;
92 }
93 
95  std::scoped_lock lock(mutex_);
96 
97  if (IsMergedUnSafe()) {
98  return IsOnPlatformThread();
99  } else {
100  return !IsOnPlatformThread();
101  }
102 }
103 
104 void RasterThreadMerger::ExtendLeaseTo(size_t lease_term) {
105  FML_DCHECK(lease_term > 0) << "lease_term should be positive.";
106  if (TaskQueuesAreSame()) {
107  return;
108  }
109  std::scoped_lock lock(mutex_);
110  if (!IsEnabledUnSafe()) {
111  return;
112  }
113  shared_merger_->ExtendLeaseTo(this, lease_term);
114 }
115 
117  std::scoped_lock lock(mutex_);
118  return IsMergedUnSafe();
119 }
120 
122  std::scoped_lock lock(mutex_);
123  shared_merger_->SetEnabledUnSafe(true);
124 }
125 
127  std::scoped_lock lock(mutex_);
128  shared_merger_->SetEnabledUnSafe(false);
129 }
130 
132  std::scoped_lock lock(mutex_);
133  return IsEnabledUnSafe();
134 }
135 
136 bool RasterThreadMerger::IsEnabledUnSafe() const {
137  return shared_merger_->IsEnabledUnSafe();
138 }
139 
140 bool RasterThreadMerger::IsMergedUnSafe() const {
141  return TaskQueuesAreSame() || shared_merger_->IsMergedUnSafe();
142 }
143 
144 bool RasterThreadMerger::TaskQueuesAreSame() const {
145  return platform_queue_id_ == gpu_queue_id_;
146 }
147 
149  if (TaskQueuesAreSame()) {
150  return;
151  }
153  std::unique_lock<std::mutex> lock(mutex_);
154  merged_condition_.wait(lock, [&] { return IsMergedUnSafe(); });
155 }
156 
158  if (TaskQueuesAreSame()) {
160  }
161  std::scoped_lock lock(mutex_);
162  if (!IsMergedUnSafe()) {
164  }
165  if (!IsEnabledUnSafe()) {
167  }
168  bool unmerged_after_decrement = shared_merger_->DecrementLease(this);
169  if (unmerged_after_decrement) {
170  if (merge_unmerge_callback_ != nullptr) {
171  merge_unmerge_callback_();
172  }
174  }
175 
177 }
178 
179 } // namespace fml
static fml::RefPtr< fml::RasterThreadMerger > CreateOrShareThreadMerger(const fml::RefPtr< fml::RasterThreadMerger > &parent_merger, TaskQueueId platform_id, TaskQueueId raster_id)
#define FML_DCHECK(condition)
Definition: logging.h:86
FlKeyEvent FlKeyResponderAsyncCallback callback
Definition: ascii_trie.cc:9
void MergeWithLease(size_t lease_term)
static TaskQueueId GetCurrentTaskQueueId()
Definition: message_loop.cc:76
std::function< void()> closure
Definition: closure.h:14
RasterThreadStatus DecrementLease()
void SetMergeUnmergeCallback(const fml::closure &callback)
#define FML_CHECK(condition)
Definition: logging.h:68
const fml::RefPtr< SharedThreadMerger > & GetSharedRasterThreadMerger() const
void ExtendLeaseTo(size_t lease_term)