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

Unified Diff: cc/output/context_cache_controller.cc

Issue 2353033003: Idle cleanup for worker context (Closed)
Patch Set: fix windows build 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 side-by-side diff with in-line comments
Download patch
Index: cc/output/context_cache_controller.cc
diff --git a/cc/output/context_cache_controller.cc b/cc/output/context_cache_controller.cc
index 218059dd1780531bd90debf3b0f8884b5a92cb6a..4aa2f46789f2b3a97ef6c9f9b4b4dcf159571e08 100644
--- a/cc/output/context_cache_controller.cc
+++ b/cc/output/context_cache_controller.cc
@@ -7,17 +7,22 @@
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/single_thread_task_runner.h"
+#include "base/synchronization/lock.h"
#include "gpu/command_buffer/client/context_support.h"
#include "third_party/skia/include/gpu/GrContext.h"
namespace cc {
-ContextCacheController::ScopedVisibility::ScopedVisibility() = default;
+namespace {
+static const int kIdleCleanupDelaySeconds = 1;
+} // namespace
-ContextCacheController::ScopedVisibility::~ScopedVisibility() {
+ContextCacheController::ScopedToken::ScopedToken() = default;
+
+ContextCacheController::ScopedToken::~ScopedToken() {
DCHECK(released_);
}
-void ContextCacheController::ScopedVisibility::Release() {
+void ContextCacheController::ScopedToken::Release() {
DCHECK(!released_);
released_ = true;
}
@@ -25,7 +30,10 @@ void ContextCacheController::ScopedVisibility::Release() {
ContextCacheController::ContextCacheController(
gpu::ContextSupport* context_support,
scoped_refptr<base::SingleThreadTaskRunner> task_runner)
- : context_support_(context_support), task_runner_(std::move(task_runner)) {}
+ : context_support_(context_support),
+ task_runner_(std::move(task_runner)),
+ idle_callback_(base::Bind(&ContextCacheController::OnIdle,
+ base::Unretained(this))) {}
ContextCacheController::~ContextCacheController() = default;
@@ -33,6 +41,10 @@ void ContextCacheController::SetGrContext(GrContext* gr_context) {
gr_context_ = gr_context;
}
+void ContextCacheController::SetLock(base::Lock* lock) {
+ context_lock_ = lock;
+}
+
std::unique_ptr<ContextCacheController::ScopedVisibility>
ContextCacheController::ClientBecameVisible() {
bool became_visible = num_clients_visible_ == 0;
@@ -53,20 +65,56 @@ void ContextCacheController::ClientBecameNotVisible(
--num_clients_visible_;
if (num_clients_visible_ == 0) {
+ // We are freeing resources now - cancel any pending idle callbacks.
+ ++current_idle_generation_;
+
if (gr_context_)
gr_context_->freeGpuResources();
context_support_->SetAggressivelyFreeResources(true);
}
}
-std::unique_ptr<ContextCacheController::ScopedVisibility>
-ContextCacheController::CreateScopedVisibilityForTesting() const {
- return base::WrapUnique(new ScopedVisibility());
+std::unique_ptr<ContextCacheController::ScopedBusy>
+ContextCacheController::ClientBecameBusy() {
+ ++num_clients_busy_;
+ // We are busy, cancel any pending idle callbacks.
+ ++current_idle_generation_;
+
+ return base::WrapUnique(new ScopedBusy());
}
-void ContextCacheController::ReleaseScopedVisibilityForTesting(
- std::unique_ptr<ScopedVisibility> scoped_visibility) const {
- scoped_visibility->Release();
+void ContextCacheController::ClientBecameNotBusy(
+ std::unique_ptr<ScopedBusy> scoped_busy) {
+ DCHECK(scoped_busy);
+ scoped_busy->Release();
+
+ DCHECK_GT(num_clients_busy_, 0u);
+ --num_clients_busy_;
+
+ // If we have become idle and we are visible, queue a task to drop resources
+ // after a delay. If are not visible, we have already dropped resources.
+ if (num_clients_busy_ == 0 && num_clients_visible_ > 0 && task_runner_) {
+ task_runner_->PostDelayedTask(
+ FROM_HERE,
+ base::Bind(idle_callback_.callback(), current_idle_generation_),
+ base::TimeDelta::FromSeconds(kIdleCleanupDelaySeconds));
+ }
+}
+
+void ContextCacheController::OnIdle(uint32_t idle_generation) {
+ std::unique_ptr<base::AutoLock> hold;
+ if (context_lock_)
+ hold.reset(new base::AutoLock(*context_lock_));
+
+ if (current_idle_generation_ != idle_generation)
+ return;
+
+ if (gr_context_)
+ gr_context_->freeGpuResources();
+
+ // Toggle SetAggressivelyFreeResources to drop command buffer data.
+ context_support_->SetAggressivelyFreeResources(true);
+ context_support_->SetAggressivelyFreeResources(false);
}
} // namespace cc

Powered by Google App Engine
This is Rietveld 408576698