Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "android_webview/browser/deferred_gpu_command_service.h" | 5 #include "android_webview/browser/deferred_gpu_command_service.h" |
| 6 | 6 |
| 7 #include "android_webview/browser/gl_view_renderer_manager.h" | 7 #include "android_webview/browser/gl_view_renderer_manager.h" |
| 8 #include "android_webview/browser/shared_renderer_state.h" | 8 #include "android_webview/browser/shared_renderer_state.h" |
| 9 #include "base/synchronization/lock.h" | |
| 9 #include "content/public/browser/android/synchronous_compositor.h" | 10 #include "content/public/browser/android/synchronous_compositor.h" |
| 10 #include "gpu/command_buffer/service/shader_translator_cache.h" | 11 #include "gpu/command_buffer/service/shader_translator_cache.h" |
| 11 | 12 |
| 12 namespace android_webview { | 13 namespace android_webview { |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 16 | |
| 17 // TODO(boliu): Consider using base/atomicops.h. | |
| 18 class ThreadSafeBool { | |
| 19 public: | |
| 20 ThreadSafeBool(); | |
| 21 void set(bool boolean); | |
|
no sievers
2014/05/27 20:12:48
nit: caps Set()/Get()
| |
| 22 bool get(); | |
| 23 | |
| 24 private: | |
| 25 base::Lock lock_; | |
| 26 bool boolean_; | |
| 27 DISALLOW_COPY_AND_ASSIGN(ThreadSafeBool); | |
| 28 }; | |
| 29 | |
| 30 ThreadSafeBool::ThreadSafeBool() : boolean_(false) { | |
| 31 } | |
| 32 | |
| 33 void ThreadSafeBool::set(bool boolean) { | |
| 34 base::AutoLock lock(lock_); | |
| 35 boolean_ = boolean; | |
| 36 } | |
| 37 | |
| 38 bool ThreadSafeBool::get() { | |
| 39 base::AutoLock lock(lock_); | |
| 40 return boolean_; | |
| 41 } | |
| 42 | |
| 43 base::LazyInstance<ThreadSafeBool> g_need_schedule = LAZY_INSTANCE_INITIALIZER; | |
| 44 base::LazyInstance<ThreadSafeBool> g_request_pending = | |
|
no sievers
2014/05/27 20:12:48
Does it work pretty much as good if you combine th
| |
| 45 LAZY_INSTANCE_INITIALIZER; | |
| 46 | |
| 15 base::LazyInstance<scoped_refptr<DeferredGpuCommandService> > | 47 base::LazyInstance<scoped_refptr<DeferredGpuCommandService> > |
| 16 g_service = LAZY_INSTANCE_INITIALIZER; | 48 g_service = LAZY_INSTANCE_INITIALIZER; |
| 17 } // namespace | 49 } // namespace |
| 18 | 50 |
| 19 base::LazyInstance<base::ThreadLocalBoolean> ScopedAllowGL::allow_gl; | 51 base::LazyInstance<base::ThreadLocalBoolean> ScopedAllowGL::allow_gl; |
| 20 | 52 |
| 21 // static | 53 // static |
| 22 bool ScopedAllowGL::IsAllowed() { | 54 bool ScopedAllowGL::IsAllowed() { |
| 23 return allow_gl.Get().Get(); | 55 return allow_gl.Get().Get(); |
| 24 } | 56 } |
| 25 | 57 |
| 58 // static | |
| 59 bool ScopedAllowGL::NeedSchedule() { | |
| 60 return g_need_schedule.Get().get(); | |
| 61 } | |
| 62 | |
| 26 ScopedAllowGL::ScopedAllowGL() { | 63 ScopedAllowGL::ScopedAllowGL() { |
| 27 DCHECK(!allow_gl.Get().Get()); | 64 DCHECK(!allow_gl.Get().Get()); |
| 28 allow_gl.Get().Set(true); | 65 allow_gl.Get().Set(true); |
| 66 g_need_schedule.Get().set(false); | |
| 67 g_request_pending.Get().set(false); | |
| 29 | 68 |
| 30 if (g_service.Get()) | 69 if (g_service.Get()) |
| 31 g_service.Get()->RunTasks(); | 70 g_service.Get()->RunTasks(); |
| 32 } | 71 } |
| 33 | 72 |
| 34 ScopedAllowGL::~ScopedAllowGL() { allow_gl.Get().Set(false); } | 73 ScopedAllowGL::~ScopedAllowGL() { |
| 74 allow_gl.Get().Set(false); | |
| 75 g_need_schedule.Get().set(true); | |
| 76 | |
| 77 if (g_service.Get()) | |
| 78 g_service.Get()->RunTasks(); | |
| 79 } | |
| 35 | 80 |
| 36 // static | 81 // static |
| 37 void DeferredGpuCommandService::SetInstance() { | 82 void DeferredGpuCommandService::SetInstance() { |
| 38 if (!g_service.Get()) { | 83 if (!g_service.Get()) { |
| 39 g_service.Get() = new DeferredGpuCommandService; | 84 g_service.Get() = new DeferredGpuCommandService; |
| 40 content::SynchronousCompositor::SetGpuService(g_service.Get()); | 85 content::SynchronousCompositor::SetGpuService(g_service.Get()); |
| 86 // Initialize global booleans. | |
| 87 g_request_pending.Get().set(false); | |
| 88 g_need_schedule.Get().set(true); | |
| 41 } | 89 } |
| 42 } | 90 } |
| 43 | 91 |
| 44 // static | 92 // static |
| 45 DeferredGpuCommandService* DeferredGpuCommandService::GetInstance() { | 93 DeferredGpuCommandService* DeferredGpuCommandService::GetInstance() { |
| 46 DCHECK(g_service.Get().get()); | 94 DCHECK(g_service.Get().get()); |
| 47 return g_service.Get().get(); | 95 return g_service.Get().get(); |
| 48 } | 96 } |
| 49 | 97 |
| 50 DeferredGpuCommandService::DeferredGpuCommandService() {} | 98 DeferredGpuCommandService::DeferredGpuCommandService() {} |
| 51 | 99 |
| 52 DeferredGpuCommandService::~DeferredGpuCommandService() { | 100 DeferredGpuCommandService::~DeferredGpuCommandService() { |
| 53 base::AutoLock lock(tasks_lock_); | 101 base::AutoLock lock(tasks_lock_); |
| 54 DCHECK(tasks_.empty()); | 102 DCHECK(tasks_.empty()); |
| 55 } | 103 } |
| 56 | 104 |
| 57 // This method can be called on any thread. | 105 // This method can be called on any thread. |
| 58 // static | 106 // static |
| 59 void DeferredGpuCommandService::RequestProcessGL() { | 107 void DeferredGpuCommandService::RequestProcessGL() { |
| 60 SharedRendererState* renderer_state = | 108 SharedRendererState* renderer_state = |
| 61 GLViewRendererManager::GetInstance()->GetMostRecentlyDrawn(); | 109 GLViewRendererManager::GetInstance()->GetMostRecentlyDrawn(); |
| 62 if (!renderer_state) { | 110 if (!renderer_state) { |
| 63 LOG(ERROR) << "No hardware renderer. Deadlock likely"; | 111 LOG(ERROR) << "No hardware renderer. Deadlock likely"; |
| 64 return; | 112 return; |
| 65 } | 113 } |
| 66 renderer_state->ClientRequestDrawGL(); | 114 |
| 115 if (!g_request_pending.Get().get()) { | |
| 116 g_request_pending.Get().set(true); | |
| 117 renderer_state->ClientRequestDrawGL(); | |
| 118 } | |
| 67 } | 119 } |
| 68 | 120 |
| 69 // Called from different threads! | 121 // Called from different threads! |
| 70 void DeferredGpuCommandService::ScheduleTask(const base::Closure& task) { | 122 void DeferredGpuCommandService::ScheduleTask(const base::Closure& task) { |
| 71 { | 123 { |
| 72 base::AutoLock lock(tasks_lock_); | 124 base::AutoLock lock(tasks_lock_); |
| 73 tasks_.push(task); | 125 tasks_.push(task); |
| 74 } | 126 } |
| 75 if (ScopedAllowGL::IsAllowed()) { | 127 if (ScopedAllowGL::IsAllowed()) { |
| 76 RunTasks(); | 128 RunTasks(); |
| 77 } else { | 129 } else if (ScopedAllowGL::NeedSchedule()) { |
| 78 // TODO(boliu): Improve this to avoid PostTask storm. | |
| 79 RequestProcessGL(); | 130 RequestProcessGL(); |
| 80 } | 131 } |
| 81 } | 132 } |
| 82 | 133 |
| 83 void DeferredGpuCommandService::ScheduleIdleWork( | 134 void DeferredGpuCommandService::ScheduleIdleWork( |
| 84 const base::Closure& callback) { | 135 const base::Closure& callback) { |
| 85 // TODO(sievers): Should this do anything? | 136 // TODO(sievers): Should this do anything? |
| 86 } | 137 } |
| 87 | 138 |
| 88 bool DeferredGpuCommandService::UseVirtualizedGLContexts() { return true; } | 139 bool DeferredGpuCommandService::UseVirtualizedGLContexts() { return true; } |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 118 | 169 |
| 119 void DeferredGpuCommandService::AddRef() const { | 170 void DeferredGpuCommandService::AddRef() const { |
| 120 base::RefCountedThreadSafe<DeferredGpuCommandService>::AddRef(); | 171 base::RefCountedThreadSafe<DeferredGpuCommandService>::AddRef(); |
| 121 } | 172 } |
| 122 | 173 |
| 123 void DeferredGpuCommandService::Release() const { | 174 void DeferredGpuCommandService::Release() const { |
| 124 base::RefCountedThreadSafe<DeferredGpuCommandService>::Release(); | 175 base::RefCountedThreadSafe<DeferredGpuCommandService>::Release(); |
| 125 } | 176 } |
| 126 | 177 |
| 127 } // namespace android_webview | 178 } // namespace android_webview |
| OLD | NEW |