forked from muhleder/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpu_thread_merger.cc
74 lines (60 loc) · 2.14 KB
/
gpu_thread_merger.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#define FML_USED_ON_EMBEDDER
#include "flutter/fml/gpu_thread_merger.h"
#include "flutter/fml/message_loop_impl.h"
namespace fml {
const int GpuThreadMerger::kLeaseNotSet = -1;
GpuThreadMerger::GpuThreadMerger(fml::TaskQueueId platform_queue_id,
fml::TaskQueueId gpu_queue_id)
: platform_queue_id_(platform_queue_id),
gpu_queue_id_(gpu_queue_id),
task_queues_(fml::MessageLoopTaskQueues::GetInstance()),
lease_term_(kLeaseNotSet) {
is_merged_ = task_queues_->Owns(platform_queue_id_, gpu_queue_id_);
}
void GpuThreadMerger::MergeWithLease(size_t lease_term) {
FML_DCHECK(lease_term > 0) << "lease_term should be positive.";
if (!is_merged_) {
is_merged_ = task_queues_->Merge(platform_queue_id_, gpu_queue_id_);
lease_term_ = lease_term;
}
}
bool GpuThreadMerger::IsOnRasterizingThread() {
const auto current_queue_id = MessageLoop::GetCurrentTaskQueueId();
if (is_merged_) {
return current_queue_id == platform_queue_id_;
} else {
return current_queue_id == gpu_queue_id_;
}
}
void GpuThreadMerger::ExtendLeaseTo(size_t lease_term) {
FML_DCHECK(lease_term > 0) << "lease_term should be positive.";
if (lease_term_ != kLeaseNotSet && (int)lease_term > lease_term_) {
lease_term_ = lease_term;
}
}
bool GpuThreadMerger::IsMerged() const {
return is_merged_;
}
GpuThreadStatus GpuThreadMerger::DecrementLease() {
if (!is_merged_) {
return GpuThreadStatus::kRemainsUnmerged;
}
// we haven't been set to merge.
if (lease_term_ == kLeaseNotSet) {
return GpuThreadStatus::kRemainsUnmerged;
}
FML_DCHECK(lease_term_ > 0)
<< "lease_term should always be positive when merged.";
lease_term_--;
if (lease_term_ == 0) {
bool success = task_queues_->Unmerge(platform_queue_id_);
FML_CHECK(success) << "Unable to un-merge the GPU and platform threads.";
is_merged_ = false;
return GpuThreadStatus::kUnmergedNow;
}
return GpuThreadStatus::kRemainsMerged;
}
} // namespace fml