Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(111)

Side by Side Diff: cc/output/context_cache_controller.cc

Issue 2353033003: Idle cleanup for worker context (Closed)
Patch Set: nits and use WeakPtrFactory Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « cc/output/context_cache_controller.h ('k') | cc/output/context_cache_controller_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
7 #include "base/logging.h" 8 #include "base/logging.h"
8 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
9 #include "base/single_thread_task_runner.h" 10 #include "base/single_thread_task_runner.h"
11 #include "base/synchronization/lock.h"
10 #include "gpu/command_buffer/client/context_support.h" 12 #include "gpu/command_buffer/client/context_support.h"
11 #include "third_party/skia/include/gpu/GrContext.h" 13 #include "third_party/skia/include/gpu/GrContext.h"
12 14
13 namespace cc { 15 namespace cc {
14 ContextCacheController::ScopedVisibility::ScopedVisibility() = default; 16 namespace {
17 static const int kIdleCleanupDelaySeconds = 1;
18 } // namespace
15 19
16 ContextCacheController::ScopedVisibility::~ScopedVisibility() { 20 ContextCacheController::ScopedToken::ScopedToken() = default;
21
22 ContextCacheController::ScopedToken::~ScopedToken() {
17 DCHECK(released_); 23 DCHECK(released_);
18 } 24 }
19 25
20 void ContextCacheController::ScopedVisibility::Release() { 26 void ContextCacheController::ScopedToken::Release() {
21 DCHECK(!released_); 27 DCHECK(!released_);
22 released_ = true; 28 released_ = true;
23 } 29 }
24 30
25 ContextCacheController::ContextCacheController( 31 ContextCacheController::ContextCacheController(
26 gpu::ContextSupport* context_support, 32 gpu::ContextSupport* context_support,
27 scoped_refptr<base::SingleThreadTaskRunner> task_runner) 33 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
28 : context_support_(context_support), task_runner_(std::move(task_runner)) {} 34 : context_support_(context_support),
35 task_runner_(std::move(task_runner)),
36 weak_factory_(this) {}
jbauman 2016/09/22 02:13:43 You need to do weak_factory_.GetWeakPtr() in the c
ericrk 2016/09/22 18:35:39 Hmm... Good point - If we're sure we always use th
ericrk 2016/09/22 18:40:51 Ah, but we don't... yeah... I see what you mean.
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(&ContextCacheController::OnIdle, weak_factory_.GetWeakPtr(),
100 current_idle_generation_),
101 base::TimeDelta::FromSeconds(kIdleCleanupDelaySeconds));
jbauman 2016/09/22 02:13:42 I think that when the context has a lock, that wil
ericrk 2016/09/22 18:35:39 I like your second option I think - we don't need
102 }
103 }
104
105 void ContextCacheController::OnIdle(uint32_t idle_generation) {
106 std::unique_ptr<base::AutoLock> hold;
jbauman 2016/09/22 02:13:43 This will unnecessarily grab the lock (and block t
ericrk 2016/09/22 18:35:39 Good point - actually meant to do something differ
107 if (context_lock_)
108 hold.reset(new base::AutoLock(*context_lock_));
109
110 if (current_idle_generation_ != idle_generation)
111 return;
112
113 if (gr_context_)
114 gr_context_->freeGpuResources();
115
116 // Toggle SetAggressivelyFreeResources to drop command buffer data.
117 context_support_->SetAggressivelyFreeResources(true);
118 context_support_->SetAggressivelyFreeResources(false);
70 } 119 }
71 120
72 } // namespace cc 121 } // namespace cc
OLDNEW
« no previous file with comments | « cc/output/context_cache_controller.h ('k') | cc/output/context_cache_controller_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698