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/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/synchronization/lock.h" |
10 #include "gpu/command_buffer/client/context_support.h" | 11 #include "gpu/command_buffer/client/context_support.h" |
11 #include "third_party/skia/include/gpu/GrContext.h" | 12 #include "third_party/skia/include/gpu/GrContext.h" |
12 | 13 |
13 namespace cc { | 14 namespace cc { |
14 ContextCacheController::ScopedVisibility::ScopedVisibility() = default; | 15 namespace { |
| 16 static const int kIdleCleanupDelaySeconds = 1; |
| 17 } // namespace |
15 | 18 |
16 ContextCacheController::ScopedVisibility::~ScopedVisibility() { | 19 ContextCacheController::ScopedToken::ScopedToken() = default; |
| 20 |
| 21 ContextCacheController::ScopedToken::~ScopedToken() { |
17 DCHECK(released_); | 22 DCHECK(released_); |
18 } | 23 } |
19 | 24 |
20 void ContextCacheController::ScopedVisibility::Release() { | 25 void ContextCacheController::ScopedToken::Release() { |
21 DCHECK(!released_); | 26 DCHECK(!released_); |
22 released_ = true; | 27 released_ = true; |
23 } | 28 } |
24 | 29 |
25 ContextCacheController::ContextCacheController( | 30 ContextCacheController::ContextCacheController( |
26 gpu::ContextSupport* context_support, | 31 gpu::ContextSupport* context_support, |
27 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 32 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
28 : context_support_(context_support), task_runner_(std::move(task_runner)) {} | 33 : context_support_(context_support), |
| 34 task_runner_(std::move(task_runner)), |
| 35 idle_callback_(base::Bind(&ContextCacheController::OnIdle, |
| 36 base::Unretained(this))) {} |
29 | 37 |
30 ContextCacheController::~ContextCacheController() = default; | 38 ContextCacheController::~ContextCacheController() = default; |
31 | 39 |
32 void ContextCacheController::SetGrContext(GrContext* gr_context) { | 40 void ContextCacheController::SetGrContext(GrContext* gr_context) { |
33 gr_context_ = gr_context; | 41 gr_context_ = gr_context; |
34 } | 42 } |
35 | 43 |
| 44 void ContextCacheController::SetLock(base::Lock* lock) { |
| 45 context_lock_ = lock; |
| 46 } |
| 47 |
36 std::unique_ptr<ContextCacheController::ScopedVisibility> | 48 std::unique_ptr<ContextCacheController::ScopedVisibility> |
37 ContextCacheController::ClientBecameVisible() { | 49 ContextCacheController::ClientBecameVisible() { |
38 bool became_visible = num_clients_visible_ == 0; | 50 bool became_visible = num_clients_visible_ == 0; |
39 ++num_clients_visible_; | 51 ++num_clients_visible_; |
40 | 52 |
41 if (became_visible) | 53 if (became_visible) |
42 context_support_->SetAggressivelyFreeResources(false); | 54 context_support_->SetAggressivelyFreeResources(false); |
43 | 55 |
44 return base::WrapUnique(new ScopedVisibility()); | 56 return base::WrapUnique(new ScopedVisibility()); |
45 } | 57 } |
46 | 58 |
47 void ContextCacheController::ClientBecameNotVisible( | 59 void ContextCacheController::ClientBecameNotVisible( |
48 std::unique_ptr<ScopedVisibility> scoped_visibility) { | 60 std::unique_ptr<ScopedVisibility> scoped_visibility) { |
49 DCHECK(scoped_visibility); | 61 DCHECK(scoped_visibility); |
50 scoped_visibility->Release(); | 62 scoped_visibility->Release(); |
51 | 63 |
52 DCHECK_GT(num_clients_visible_, 0u); | 64 DCHECK_GT(num_clients_visible_, 0u); |
53 --num_clients_visible_; | 65 --num_clients_visible_; |
54 | 66 |
55 if (num_clients_visible_ == 0) { | 67 if (num_clients_visible_ == 0) { |
| 68 // We are freeing resources now - cancel any pending idle callbacks. |
| 69 ++current_idle_generation_; |
| 70 |
56 if (gr_context_) | 71 if (gr_context_) |
57 gr_context_->freeGpuResources(); | 72 gr_context_->freeGpuResources(); |
58 context_support_->SetAggressivelyFreeResources(true); | 73 context_support_->SetAggressivelyFreeResources(true); |
59 } | 74 } |
60 } | 75 } |
61 | 76 |
62 std::unique_ptr<ContextCacheController::ScopedVisibility> | 77 std::unique_ptr<ContextCacheController::ScopedBusy> |
63 ContextCacheController::CreateScopedVisibilityForTesting() const { | 78 ContextCacheController::ClientBecameBusy() { |
64 return base::WrapUnique(new ScopedVisibility()); | 79 ++num_clients_busy_; |
| 80 // We are busy, cancel any pending idle callbacks. |
| 81 ++current_idle_generation_; |
| 82 |
| 83 return base::WrapUnique(new ScopedBusy()); |
65 } | 84 } |
66 | 85 |
67 void ContextCacheController::ReleaseScopedVisibilityForTesting( | 86 void ContextCacheController::ClientBecameNotBusy( |
68 std::unique_ptr<ScopedVisibility> scoped_visibility) const { | 87 std::unique_ptr<ScopedBusy> scoped_busy) { |
69 scoped_visibility->Release(); | 88 DCHECK(scoped_busy); |
| 89 scoped_busy->Release(); |
| 90 |
| 91 DCHECK_GT(num_clients_busy_, 0u); |
| 92 --num_clients_busy_; |
| 93 |
| 94 // If we have become idle and we are visible, queue a task to drop resources |
| 95 // after a delay. If are not visible, we have already dropped resources. |
| 96 if (num_clients_busy_ == 0 && num_clients_visible_ > 0 && task_runner_) { |
| 97 task_runner_->PostDelayedTask( |
| 98 FROM_HERE, |
| 99 base::Bind(idle_callback_.callback(), current_idle_generation_), |
| 100 base::TimeDelta::FromSeconds(kIdleCleanupDelaySeconds)); |
| 101 } |
| 102 } |
| 103 |
| 104 void ContextCacheController::OnIdle(uint32_t idle_generation) { |
| 105 std::unique_ptr<base::AutoLock> hold; |
| 106 if (context_lock_) |
| 107 hold.reset(new base::AutoLock(*context_lock_)); |
| 108 |
| 109 if (current_idle_generation_ != idle_generation) |
| 110 return; |
| 111 |
| 112 if (gr_context_) |
| 113 gr_context_->freeGpuResources(); |
| 114 |
| 115 // Toggle SetAggressivelyFreeResources to drop command buffer data. |
| 116 context_support_->SetAggressivelyFreeResources(true); |
| 117 context_support_->SetAggressivelyFreeResources(false); |
70 } | 118 } |
71 | 119 |
72 } // namespace cc | 120 } // namespace cc |
OLD | NEW |