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