1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "android_webview/browser/deferred_gpu_command_service.h"
7 #include "android_webview/browser/gl_view_renderer_manager.h"
8 #include "android_webview/browser/shared_renderer_state.h"
9 #include "content/public/browser/android/synchronous_compositor.h"
10 #include "gpu/command_buffer/service/shader_translator_cache.h"
12 namespace android_webview {
15 base::LazyInstance<scoped_refptr<DeferredGpuCommandService> >
16 g_service = LAZY_INSTANCE_INITIALIZER;
19 base::LazyInstance<base::ThreadLocalBoolean> ScopedAllowGL::allow_gl;
22 bool ScopedAllowGL::IsAllowed() {
23 return allow_gl.Get().Get();
26 ScopedAllowGL::ScopedAllowGL() {
27 DCHECK(!allow_gl.Get().Get());
28 allow_gl.Get().Set(true);
31 g_service.Get()->RunTasks();
34 ScopedAllowGL::~ScopedAllowGL() { allow_gl.Get().Set(false); }
37 void DeferredGpuCommandService::SetInstance() {
38 if (!g_service.Get()) {
39 g_service.Get() = new DeferredGpuCommandService;
40 content::SynchronousCompositor::SetGpuService(g_service.Get());
44 DeferredGpuCommandService::DeferredGpuCommandService() {}
46 DeferredGpuCommandService::~DeferredGpuCommandService() {
47 base::AutoLock lock(tasks_lock_);
48 DCHECK(tasks_.empty());
52 void DeferredGpuCommandService::RequestProcessGLOnUIThread() {
53 SharedRendererState* renderer_state =
54 GLViewRendererManager::GetInstance()->GetMostRecentlyDrawn();
55 if (!renderer_state) {
56 LOG(ERROR) << "No hardware renderer. Deadlock likely";
59 renderer_state->ClientRequestDrawGL();
62 // Called from different threads!
63 void DeferredGpuCommandService::ScheduleTask(const base::Closure& task) {
65 base::AutoLock lock(tasks_lock_);
68 if (ScopedAllowGL::IsAllowed()) {
71 RequestProcessGLOnUIThread();
75 void DeferredGpuCommandService::ScheduleIdleWork(
76 const base::Closure& callback) {
77 // TODO(sievers): Should this do anything?
80 bool DeferredGpuCommandService::UseVirtualizedGLContexts() { return true; }
82 scoped_refptr<gpu::gles2::ShaderTranslatorCache>
83 DeferredGpuCommandService::shader_translator_cache() {
84 if (!shader_translator_cache_.get())
85 shader_translator_cache_ = new gpu::gles2::ShaderTranslatorCache;
86 return shader_translator_cache_;
89 void DeferredGpuCommandService::RunTasks() {
92 base::AutoLock lock(tasks_lock_);
93 has_more_tasks = tasks_.size() > 0;
96 while (has_more_tasks) {
99 base::AutoLock lock(tasks_lock_);
100 task = tasks_.front();
105 base::AutoLock lock(tasks_lock_);
106 has_more_tasks = tasks_.size() > 0;
111 void DeferredGpuCommandService::AddRef() const {
112 base::RefCountedThreadSafe<DeferredGpuCommandService>::AddRef();
115 void DeferredGpuCommandService::Release() const {
116 base::RefCountedThreadSafe<DeferredGpuCommandService>::Release();
119 } // namespace android_webview