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 "flutter/fml/macros.h"
9#include "flutter/fml/memory/ref_counted.h"
10#include "flutter/fml/message_loop_task_queues.h"
11
12namespace fml {
13
14class MessageLoopImpl;
15
16enum class RasterThreadStatus {
17 kRemainsMerged,
18 kRemainsUnmerged,
19 kUnmergedNow
20};
21
22class RasterThreadMerger
23 : public fml::RefCountedThreadSafe<RasterThreadMerger> {
24 public:
25 // Merges the raster thread into platform thread for the duration of
26 // the lease term. Lease is managed by the caller by either calling
27 // |ExtendLeaseTo| or |DecrementLease|.
28 // When the caller merges with a lease term of say 2. The threads
29 // are going to remain merged until 2 invocations of |DecreaseLease|,
30 // unless an |ExtendLeaseTo| gets called.
31 void MergeWithLease(size_t lease_term);
32
33 void ExtendLeaseTo(size_t lease_term);
34
35 // Returns |RasterThreadStatus::kUnmergedNow| if this call resulted in
36 // splitting the raster and platform threads. Reduces the lease term by 1.
37 RasterThreadStatus DecrementLease();
38
39 bool IsMerged() const;
40
41 RasterThreadMerger(fml::TaskQueueId platform_queue_id,
42 fml::TaskQueueId gpu_queue_id);
43
44 // Returns true if the current thread owns rasterizing.
45 // When the threads are merged, platform thread owns rasterizing.
46 // When un-merged, raster thread owns rasterizing.
47 bool IsOnRasterizingThread() const;
48
49 // Returns true if the current thread is the platform thread.
50 bool IsOnPlatformThread() const;
51
52 private:
53 static const int kLeaseNotSet;
54 fml::TaskQueueId platform_queue_id_;
55 fml::TaskQueueId gpu_queue_id_;
56 fml::RefPtr<fml::MessageLoopTaskQueues> task_queues_;
57 std::atomic_int lease_term_;
58 bool is_merged_;
59
60 FML_FRIEND_REF_COUNTED_THREAD_SAFE(RasterThreadMerger);
61 FML_FRIEND_MAKE_REF_COUNTED(RasterThreadMerger);
62 FML_DISALLOW_COPY_AND_ASSIGN(RasterThreadMerger);
63};
64
65} // namespace fml
66
67#endif // FML_SHELL_COMMON_TASK_RUNNER_MERGER_H_
68