1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "android_webview/browser/deferred_gpu_command_service.h"
#include "android_webview/browser/gl_view_renderer_manager.h"
#include "android_webview/browser/render_thread_manager.h"
#include "base/command_line.h"
#include "base/lazy_instance.h"
#include "base/strings/string_number_conversions.h"
#include "base/synchronization/lock.h"
#include "base/trace_event/trace_event.h"
#include "content/public/browser/gpu_utils.h"
#include "content/public/common/content_switches.h"
#include "gpu/command_buffer/service/framebuffer_completeness_cache.h"
#include "gpu/command_buffer/service/gpu_preferences.h"
#include "gpu/command_buffer/service/gpu_switches.h"
#include "gpu/command_buffer/service/shader_translator_cache.h"
#include "gpu/command_buffer/service/sync_point_manager.h"
#include "gpu/config/gpu_switches.h"
#include "ui/gl/gl_switches.h"
namespace android_webview {
namespace {
base::LazyInstance<scoped_refptr<DeferredGpuCommandService> >
g_service = LAZY_INSTANCE_INITIALIZER;
} // namespace
base::LazyInstance<base::ThreadLocalBoolean> ScopedAllowGL::allow_gl;
// static
bool ScopedAllowGL::IsAllowed() {
return allow_gl.Get().Get();
}
ScopedAllowGL::ScopedAllowGL() {
DCHECK(!allow_gl.Get().Get());
allow_gl.Get().Set(true);
if (g_service.Get().get())
g_service.Get()->RunTasks();
}
ScopedAllowGL::~ScopedAllowGL() {
allow_gl.Get().Set(false);
DeferredGpuCommandService* service = g_service.Get().get();
if (service) {
service->RunTasks();
if (service->IdleQueueSize()) {
service->RequestProcessGL(true);
}
}
}
// static
void DeferredGpuCommandService::SetInstance() {
if (!g_service.Get().get())
g_service.Get() = new DeferredGpuCommandService;
}
// static
DeferredGpuCommandService* DeferredGpuCommandService::GetInstance() {
DCHECK(g_service.Get().get());
return g_service.Get().get();
}
DeferredGpuCommandService::DeferredGpuCommandService()
: gpu::InProcessCommandBuffer::Service(
content::GetGpuPreferencesFromCommandLine()),
sync_point_manager_(new gpu::SyncPointManager(true)) {
}
DeferredGpuCommandService::~DeferredGpuCommandService() {
base::AutoLock lock(tasks_lock_);
DCHECK(tasks_.empty());
}
// This method can be called on any thread.
// static
void DeferredGpuCommandService::RequestProcessGL(bool for_idle) {
RenderThreadManager* renderer_state =
GLViewRendererManager::GetInstance()->GetMostRecentlyDrawn();
if (!renderer_state) {
LOG(ERROR) << "No hardware renderer. Deadlock likely";
return;
}
renderer_state->ClientRequestInvokeGL(for_idle);
}
// Called from different threads!
void DeferredGpuCommandService::ScheduleTask(const base::Closure& task) {
{
base::AutoLock lock(tasks_lock_);
tasks_.push(task);
}
if (ScopedAllowGL::IsAllowed()) {
RunTasks();
} else {
RequestProcessGL(false);
}
}
size_t DeferredGpuCommandService::IdleQueueSize() {
base::AutoLock lock(tasks_lock_);
return idle_tasks_.size();
}
void DeferredGpuCommandService::ScheduleDelayedWork(
const base::Closure& callback) {
{
base::AutoLock lock(tasks_lock_);
idle_tasks_.push(std::make_pair(base::Time::Now(), callback));
}
RequestProcessGL(true);
}
void DeferredGpuCommandService::PerformIdleWork(bool is_idle) {
TRACE_EVENT1("android_webview",
"DeferredGpuCommandService::PerformIdleWork",
"is_idle",
is_idle);
DCHECK(ScopedAllowGL::IsAllowed());
static const base::TimeDelta kMaxIdleAge =
base::TimeDelta::FromMilliseconds(16);
const base::Time now = base::Time::Now();
size_t queue_size = IdleQueueSize();
while (queue_size--) {
base::Closure task;
{
base::AutoLock lock(tasks_lock_);
if (!is_idle) {
// Only run old tasks if we are not really idle right now.
base::TimeDelta age(now - idle_tasks_.front().first);
if (age < kMaxIdleAge)
break;
}
task = idle_tasks_.front().second;
idle_tasks_.pop();
}
task.Run();
}
}
void DeferredGpuCommandService::PerformAllIdleWork() {
TRACE_EVENT0("android_webview",
"DeferredGpuCommandService::PerformAllIdleWork");
while (IdleQueueSize()) {
PerformIdleWork(true);
}
}
bool DeferredGpuCommandService::UseVirtualizedGLContexts() { return true; }
scoped_refptr<gpu::gles2::ShaderTranslatorCache>
DeferredGpuCommandService::shader_translator_cache() {
if (!shader_translator_cache_.get()) {
shader_translator_cache_ =
new gpu::gles2::ShaderTranslatorCache(gpu_preferences());
}
return shader_translator_cache_;
}
scoped_refptr<gpu::gles2::FramebufferCompletenessCache>
DeferredGpuCommandService::framebuffer_completeness_cache() {
if (!framebuffer_completeness_cache_.get()) {
framebuffer_completeness_cache_ =
new gpu::gles2::FramebufferCompletenessCache;
}
return framebuffer_completeness_cache_;
}
gpu::SyncPointManager* DeferredGpuCommandService::sync_point_manager() {
return sync_point_manager_.get();
}
void DeferredGpuCommandService::RunTasks() {
TRACE_EVENT0("android_webview", "DeferredGpuCommandService::RunTasks");
bool has_more_tasks;
{
base::AutoLock lock(tasks_lock_);
has_more_tasks = tasks_.size() > 0;
}
while (has_more_tasks) {
base::Closure task;
{
base::AutoLock lock(tasks_lock_);
task = tasks_.front();
tasks_.pop();
}
task.Run();
{
base::AutoLock lock(tasks_lock_);
has_more_tasks = tasks_.size() > 0;
}
}
}
void DeferredGpuCommandService::AddRef() const {
base::RefCountedThreadSafe<DeferredGpuCommandService>::AddRef();
}
void DeferredGpuCommandService::Release() const {
base::RefCountedThreadSafe<DeferredGpuCommandService>::Release();
}
bool DeferredGpuCommandService::BlockThreadOnWaitSyncToken() const {
return true;
}
} // namespace android_webview
|