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/gpu_thread_merger.h" 8 #include "flutter/fml/message_loop_impl.h" 9 10 namespace fml { 11 12 const int GpuThreadMerger::kLeaseNotSet = -1; 13 GpuThreadMerger(fml::TaskQueueId platform_queue_id,fml::TaskQueueId gpu_queue_id)14GpuThreadMerger::GpuThreadMerger(fml::TaskQueueId platform_queue_id, 15 fml::TaskQueueId gpu_queue_id) 16 : platform_queue_id_(platform_queue_id), 17 gpu_queue_id_(gpu_queue_id), 18 task_queues_(fml::MessageLoopTaskQueues::GetInstance()), 19 lease_term_(kLeaseNotSet) { 20 is_merged_ = task_queues_->Owns(platform_queue_id_, gpu_queue_id_); 21 } 22 MergeWithLease(size_t lease_term)23void GpuThreadMerger::MergeWithLease(size_t lease_term) { 24 FML_DCHECK(lease_term > 0) << "lease_term should be positive."; 25 if (!is_merged_) { 26 is_merged_ = task_queues_->Merge(platform_queue_id_, gpu_queue_id_); 27 lease_term_ = lease_term; 28 } 29 } 30 IsOnRasterizingThread()31bool GpuThreadMerger::IsOnRasterizingThread() { 32 const auto current_queue_id = MessageLoop::GetCurrentTaskQueueId(); 33 if (is_merged_) { 34 return current_queue_id == platform_queue_id_; 35 } else { 36 return current_queue_id == gpu_queue_id_; 37 } 38 } 39 ExtendLeaseTo(size_t lease_term)40void GpuThreadMerger::ExtendLeaseTo(size_t lease_term) { 41 FML_DCHECK(lease_term > 0) << "lease_term should be positive."; 42 if (lease_term_ != kLeaseNotSet && (int)lease_term > lease_term_) { 43 lease_term_ = lease_term; 44 } 45 } 46 IsMerged() const47bool GpuThreadMerger::IsMerged() const { 48 return is_merged_; 49 } 50 DecrementLease()51GpuThreadStatus GpuThreadMerger::DecrementLease() { 52 if (!is_merged_) { 53 return GpuThreadStatus::kRemainsUnmerged; 54 } 55 56 // we haven't been set to merge. 57 if (lease_term_ == kLeaseNotSet) { 58 return GpuThreadStatus::kRemainsUnmerged; 59 } 60 61 FML_DCHECK(lease_term_ > 0) 62 << "lease_term should always be positive when merged."; 63 lease_term_--; 64 if (lease_term_ == 0) { 65 bool success = task_queues_->Unmerge(platform_queue_id_); 66 FML_CHECK(success) << "Unable to un-merge the GPU and platform threads."; 67 is_merged_ = false; 68 return GpuThreadStatus::kUnmergedNow; 69 } 70 71 return GpuThreadStatus::kRemainsMerged; 72 } 73 74 } // namespace fml 75