Flutter Engine
raster_thread_merger.h
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 #ifndef FML_SHELL_COMMON_TASK_RUNNER_MERGER_H_
6 #define FML_SHELL_COMMON_TASK_RUNNER_MERGER_H_
7 
8 #include <condition_variable>
9 #include <mutex>
10 
11 #include "flutter/fml/macros.h"
12 #include "flutter/fml/memory/ref_counted.h"
13 #include "flutter/fml/message_loop_task_queues.h"
14 
15 namespace fml {
16 
17 class MessageLoopImpl;
18 
19 enum class RasterThreadStatus {
23 };
24 
26  : public fml::RefCountedThreadSafe<RasterThreadMerger> {
27  public:
28  // Merges the raster thread into platform thread for the duration of
29  // the lease term. Lease is managed by the caller by either calling
30  // |ExtendLeaseTo| or |DecrementLease|.
31  // When the caller merges with a lease term of say 2. The threads
32  // are going to remain merged until 2 invocations of |DecreaseLease|,
33  // unless an |ExtendLeaseTo| gets called.
34  //
35  // If the task queues are the same, we consider them statically merged.
36  // When task queues are statically merged this method becomes no-op.
37  void MergeWithLease(size_t lease_term);
38 
39  // Un-merges the threads now, and resets the lease term to 0.
40  //
41  // Must be executed on the raster task runner.
42  //
43  // If the task queues are the same, we consider them statically merged.
44  // When task queues are statically merged, we never unmerge them and
45  // this method becomes no-op.
46  void UnMergeNow();
47 
48  // If the task queues are the same, we consider them statically merged.
49  // When task queues are statically merged this method becomes no-op.
50  void ExtendLeaseTo(size_t lease_term);
51 
52  // Returns |RasterThreadStatus::kUnmergedNow| if this call resulted in
53  // splitting the raster and platform threads. Reduces the lease term by 1.
54  //
55  // If the task queues are the same, we consider them statically merged.
56  // When task queues are statically merged this method becomes no-op.
57  RasterThreadStatus DecrementLease();
58 
59  bool IsMerged();
60 
61  // Waits until the threads are merged.
62  //
63  // Must run on the platform task runner.
64  void WaitUntilMerged();
65 
66  RasterThreadMerger(fml::TaskQueueId platform_queue_id,
67  fml::TaskQueueId gpu_queue_id);
68 
69  // Returns true if the current thread owns rasterizing.
70  // When the threads are merged, platform thread owns rasterizing.
71  // When un-merged, raster thread owns rasterizing.
72  bool IsOnRasterizingThread() const;
73 
74  // Returns true if the current thread is the platform thread.
75  bool IsOnPlatformThread() const;
76 
77  // Enables the thread merger.
78  void Enable();
79 
80  // Disables the thread merger. Once disabled, any call to
81  // |MergeWithLease| or |UnMergeNow| results in a noop.
82  void Disable();
83 
84  // Whether the thread merger is enabled. By default, the thread merger is
85  // enabled. If false, calls to |MergeWithLease| or |UnMergeNow| results in a
86  // noop.
87  bool IsEnabled();
88 
89  // Registers a callback that can be used to clean up global state right after
90  // the thread configuration has changed.
91  //
92  // For example, it can be used to clear the GL context so it can be used in
93  // the next task from a different thread.
94  void SetMergeUnmergeCallback(const fml::closure& callback);
95 
96  private:
97  static const int kLeaseNotSet;
98  fml::TaskQueueId platform_queue_id_;
99  fml::TaskQueueId gpu_queue_id_;
101  std::atomic_int lease_term_;
102  std::condition_variable merged_condition_;
103  std::mutex lease_term_mutex_;
104  fml::closure merge_unmerge_callback_;
105  bool enabled_;
106 
107  bool IsMergedUnSafe() const;
108 
109  bool IsEnabledUnSafe() const;
110 
111  // The platform_queue_id and gpu_queue_id are exactly the same.
112  // We consider the threads are always merged and cannot be unmerged.
113  bool TaskQueuesAreSame() const;
114 
118 };
119 
120 } // namespace fml
121 
122 #endif // FML_SHELL_COMMON_TASK_RUNNER_MERGER_H_
#define FML_FRIEND_REF_COUNTED_THREAD_SAFE(T)
Definition: ref_counted.h:125
Definition: ascii_trie.cc:9
std::function< void()> closure
Definition: closure.h:14
#define FML_FRIEND_MAKE_REF_COUNTED(T)
Definition: ref_counted.h:131
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27