OLD | NEW |
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/output/context_cache_controller.h" | 5 #include "cc/output/context_cache_controller.h" |
6 | 6 |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | 7 #include "base/logging.h" |
9 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
10 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
11 #include "base/synchronization/lock.h" | |
12 #include "gpu/command_buffer/client/context_support.h" | 10 #include "gpu/command_buffer/client/context_support.h" |
13 #include "third_party/skia/include/gpu/GrContext.h" | 11 #include "third_party/skia/include/gpu/GrContext.h" |
14 | 12 |
15 namespace cc { | 13 namespace cc { |
16 namespace { | 14 ContextCacheController::ScopedVisibility::ScopedVisibility() = default; |
17 static const int kIdleCleanupDelaySeconds = 1; | |
18 } // namespace | |
19 | 15 |
20 ContextCacheController::ScopedToken::ScopedToken() = default; | 16 ContextCacheController::ScopedVisibility::~ScopedVisibility() { |
21 | |
22 ContextCacheController::ScopedToken::~ScopedToken() { | |
23 DCHECK(released_); | 17 DCHECK(released_); |
24 } | 18 } |
25 | 19 |
26 void ContextCacheController::ScopedToken::Release() { | 20 void ContextCacheController::ScopedVisibility::Release() { |
27 DCHECK(!released_); | 21 DCHECK(!released_); |
28 released_ = true; | 22 released_ = true; |
29 } | 23 } |
30 | 24 |
31 ContextCacheController::ContextCacheController( | 25 ContextCacheController::ContextCacheController( |
32 gpu::ContextSupport* context_support, | 26 gpu::ContextSupport* context_support, |
33 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 27 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
34 : context_support_(context_support), | 28 : context_support_(context_support), task_runner_(std::move(task_runner)) {} |
35 task_runner_(std::move(task_runner)), | |
36 weak_factory_(this) { | |
37 // The |weak_factory_| can only be used from a single thread. We | |
38 // create/destroy this class and run callbacks on a single thread, but we | |
39 // want to be able to post callbacks from multiple threads. We need a weak | |
40 // ptr to post callbacks, so acquire one here, while we're on the right | |
41 // thread. | |
42 weak_ptr_ = weak_factory_.GetWeakPtr(); | |
43 } | |
44 | 29 |
45 ContextCacheController::~ContextCacheController() = default; | 30 ContextCacheController::~ContextCacheController() = default; |
46 | 31 |
47 void ContextCacheController::SetGrContext(GrContext* gr_context) { | 32 void ContextCacheController::SetGrContext(GrContext* gr_context) { |
48 gr_context_ = gr_context; | 33 gr_context_ = gr_context; |
49 } | 34 } |
50 | 35 |
51 void ContextCacheController::SetLock(base::Lock* lock) { | |
52 context_lock_ = lock; | |
53 } | |
54 | |
55 std::unique_ptr<ContextCacheController::ScopedVisibility> | 36 std::unique_ptr<ContextCacheController::ScopedVisibility> |
56 ContextCacheController::ClientBecameVisible() { | 37 ContextCacheController::ClientBecameVisible() { |
57 if (context_lock_) | |
58 context_lock_->AssertAcquired(); | |
59 | |
60 bool became_visible = num_clients_visible_ == 0; | 38 bool became_visible = num_clients_visible_ == 0; |
61 ++num_clients_visible_; | 39 ++num_clients_visible_; |
62 | 40 |
63 if (became_visible) | 41 if (became_visible) |
64 context_support_->SetAggressivelyFreeResources(false); | 42 context_support_->SetAggressivelyFreeResources(false); |
65 | 43 |
66 return base::WrapUnique(new ScopedVisibility()); | 44 return base::WrapUnique(new ScopedVisibility()); |
67 } | 45 } |
68 | 46 |
69 void ContextCacheController::ClientBecameNotVisible( | 47 void ContextCacheController::ClientBecameNotVisible( |
70 std::unique_ptr<ScopedVisibility> scoped_visibility) { | 48 std::unique_ptr<ScopedVisibility> scoped_visibility) { |
71 DCHECK(scoped_visibility); | 49 DCHECK(scoped_visibility); |
72 scoped_visibility->Release(); | 50 scoped_visibility->Release(); |
73 | 51 |
74 if (context_lock_) | |
75 context_lock_->AssertAcquired(); | |
76 | |
77 DCHECK_GT(num_clients_visible_, 0u); | 52 DCHECK_GT(num_clients_visible_, 0u); |
78 --num_clients_visible_; | 53 --num_clients_visible_; |
79 | 54 |
80 if (num_clients_visible_ == 0) { | 55 if (num_clients_visible_ == 0) { |
81 // We are freeing resources now - cancel any pending idle callbacks. | |
82 InvalidatePendingIdleCallbacks(); | |
83 | |
84 if (gr_context_) | 56 if (gr_context_) |
85 gr_context_->freeGpuResources(); | 57 gr_context_->freeGpuResources(); |
86 context_support_->SetAggressivelyFreeResources(true); | 58 context_support_->SetAggressivelyFreeResources(true); |
87 } | 59 } |
88 } | 60 } |
89 | 61 |
90 std::unique_ptr<ContextCacheController::ScopedBusy> | 62 std::unique_ptr<ContextCacheController::ScopedVisibility> |
91 ContextCacheController::ClientBecameBusy() { | 63 ContextCacheController::CreateScopedVisibilityForTesting() const { |
92 if (context_lock_) | 64 return base::WrapUnique(new ScopedVisibility()); |
93 context_lock_->AssertAcquired(); | |
94 | |
95 ++num_clients_busy_; | |
96 // We are busy, cancel any pending idle callbacks. | |
97 InvalidatePendingIdleCallbacks(); | |
98 | |
99 return base::WrapUnique(new ScopedBusy()); | |
100 } | 65 } |
101 | 66 |
102 void ContextCacheController::ClientBecameNotBusy( | 67 void ContextCacheController::ReleaseScopedVisibilityForTesting( |
103 std::unique_ptr<ScopedBusy> scoped_busy) { | 68 std::unique_ptr<ScopedVisibility> scoped_visibility) const { |
104 DCHECK(scoped_busy); | 69 scoped_visibility->Release(); |
105 scoped_busy->Release(); | |
106 | |
107 if (context_lock_) | |
108 context_lock_->AssertAcquired(); | |
109 | |
110 DCHECK_GT(num_clients_busy_, 0u); | |
111 --num_clients_busy_; | |
112 | |
113 // If we have become idle and we are visible, queue a task to drop resources | |
114 // after a delay. If are not visible, we have already dropped resources. | |
115 if (num_clients_busy_ == 0 && num_clients_visible_ > 0 && task_runner_) { | |
116 // If we already have a callback pending, don't post a new one. The pending | |
117 // callback will handle posting a new callback itself. This prevents us from | |
118 // flooding the system with tasks. | |
119 if (!callback_pending_) { | |
120 { | |
121 base::AutoLock hold(current_idle_generation_lock_); | |
122 PostIdleCallback(current_idle_generation_); | |
123 } | |
124 callback_pending_ = true; | |
125 } | |
126 } | |
127 } | |
128 | |
129 void ContextCacheController::PostIdleCallback( | |
130 uint32_t current_idle_generation) const { | |
131 task_runner_->PostDelayedTask( | |
132 FROM_HERE, base::Bind(&ContextCacheController::OnIdle, weak_ptr_, | |
133 current_idle_generation), | |
134 base::TimeDelta::FromSeconds(kIdleCleanupDelaySeconds)); | |
135 } | |
136 | |
137 void ContextCacheController::InvalidatePendingIdleCallbacks() { | |
138 base::AutoLock hold(current_idle_generation_lock_); | |
139 ++current_idle_generation_; | |
140 } | |
141 | |
142 void ContextCacheController::OnIdle(uint32_t idle_generation) { | |
143 // First check if we should run our idle callback at all. If we have become | |
144 // busy since scheduling, just schedule another idle callback and return. | |
145 { | |
146 base::AutoLock hold(current_idle_generation_lock_); | |
147 if (current_idle_generation_ != idle_generation) { | |
148 PostIdleCallback(current_idle_generation_); | |
149 return; | |
150 } | |
151 } | |
152 | |
153 // Try to acquire the context lock - if we can't acquire it then we've become | |
154 // busy since checking |current_idle_generation_| above. In this case, just | |
155 // re-post our idle callback and return. | |
156 if (context_lock_ && !context_lock_->Try()) { | |
157 base::AutoLock hold(current_idle_generation_lock_); | |
158 PostIdleCallback(current_idle_generation_); | |
159 return; | |
160 } | |
161 | |
162 if (gr_context_) | |
163 gr_context_->freeGpuResources(); | |
164 | |
165 // Toggle SetAggressivelyFreeResources to drop command buffer data. | |
166 context_support_->SetAggressivelyFreeResources(true); | |
167 context_support_->SetAggressivelyFreeResources(false); | |
168 | |
169 callback_pending_ = false; | |
170 | |
171 if (context_lock_) | |
172 context_lock_->Release(); | |
173 } | 70 } |
174 | 71 |
175 } // namespace cc | 72 } // namespace cc |
OLD | NEW |