1 // Copyright (c) 2012 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 "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
7 #include "third_party/khronos/GLES2/gl2.h"
8 #ifndef GL_GLEXT_PROTOTYPES
9 #define GL_GLEXT_PROTOTYPES 1
11 #include "third_party/khronos/GLES2/gl2ext.h"
16 #include "base/atomicops.h"
17 #include "base/bind.h"
18 #include "base/command_line.h"
19 #include "base/debug/trace_event.h"
20 #include "base/lazy_instance.h"
21 #include "base/logging.h"
22 #include "base/message_loop/message_loop.h"
23 #include "base/metrics/field_trial.h"
24 #include "base/metrics/histogram.h"
25 #include "content/common/gpu/client/gpu_channel_host.h"
26 #include "content/public/common/content_constants.h"
27 #include "content/public/common/content_switches.h"
28 #include "gpu/GLES2/gl2extchromium.h"
29 #include "gpu/command_buffer/client/gles2_cmd_helper.h"
30 #include "gpu/command_buffer/client/gles2_implementation.h"
31 #include "gpu/command_buffer/client/gles2_trace_implementation.h"
32 #include "gpu/command_buffer/client/transfer_buffer.h"
33 #include "gpu/command_buffer/common/constants.h"
34 #include "gpu/command_buffer/common/gpu_memory_allocation.h"
35 #include "gpu/command_buffer/common/mailbox.h"
36 #include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.h"
37 #include "third_party/skia/include/core/SkTypes.h"
43 static base::LazyInstance<base::Lock>::Leaky
44 g_default_share_groups_lock = LAZY_INSTANCE_INITIALIZER;
46 typedef std::map<GpuChannelHost*,
47 scoped_refptr<WebGraphicsContext3DCommandBufferImpl::ShareGroup> >
49 static base::LazyInstance<ShareGroupMap> g_default_share_groups =
50 LAZY_INSTANCE_INITIALIZER;
52 scoped_refptr<WebGraphicsContext3DCommandBufferImpl::ShareGroup>
53 GetDefaultShareGroupForHost(GpuChannelHost* host) {
54 base::AutoLock lock(g_default_share_groups_lock.Get());
56 ShareGroupMap& share_groups = g_default_share_groups.Get();
57 ShareGroupMap::iterator it = share_groups.find(host);
58 if (it == share_groups.end()) {
59 scoped_refptr<WebGraphicsContext3DCommandBufferImpl::ShareGroup> group =
60 new WebGraphicsContext3DCommandBufferImpl::ShareGroup();
61 share_groups[host] = group;
67 } // namespace anonymous
69 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits::SharedMemoryLimits()
70 : command_buffer_size(kDefaultCommandBufferSize),
71 start_transfer_buffer_size(kDefaultStartTransferBufferSize),
72 min_transfer_buffer_size(kDefaultMinTransferBufferSize),
73 max_transfer_buffer_size(kDefaultMaxTransferBufferSize),
74 mapped_memory_reclaim_limit(gpu::gles2::GLES2Implementation::kNoLimit) {}
76 WebGraphicsContext3DCommandBufferImpl::ShareGroup::ShareGroup() {
79 WebGraphicsContext3DCommandBufferImpl::ShareGroup::~ShareGroup() {
80 DCHECK(contexts_.empty());
83 WebGraphicsContext3DCommandBufferImpl::WebGraphicsContext3DCommandBufferImpl(
85 const GURL& active_url,
87 const Attributes& attributes,
88 bool lose_context_when_out_of_memory,
89 const SharedMemoryLimits& limits,
90 WebGraphicsContext3DCommandBufferImpl* share_context)
91 : lose_context_when_out_of_memory_(lose_context_when_out_of_memory),
92 attributes_(attributes),
95 surface_id_(surface_id),
96 active_url_(active_url),
97 gpu_preference_(attributes.preferDiscreteGPU ? gfx::PreferDiscreteGpu
98 : gfx::PreferIntegratedGpu),
100 weak_ptr_factory_(this) {
102 DCHECK(!attributes_.shareResources);
103 share_group_ = share_context->share_group_;
105 share_group_ = attributes_.shareResources
106 ? GetDefaultShareGroupForHost(host)
107 : scoped_refptr<WebGraphicsContext3DCommandBufferImpl::ShareGroup>(
112 WebGraphicsContext3DCommandBufferImpl::
113 ~WebGraphicsContext3DCommandBufferImpl() {
115 real_gl_->SetErrorMessageCallback(NULL);
121 bool WebGraphicsContext3DCommandBufferImpl::MaybeInitializeGL() {
125 if (initialize_failed_)
128 TRACE_EVENT0("gpu", "WebGfxCtx3DCmdBfrImpl::MaybeInitializeGL");
130 if (!CreateContext(surface_id_ != 0)) {
132 initialize_failed_ = true;
136 if (gl_ && attributes_.webGL)
137 gl_->EnableFeatureCHROMIUM("webgl_enable_glsl_webgl_validation");
139 command_buffer_->SetChannelErrorCallback(
140 base::Bind(&WebGraphicsContext3DCommandBufferImpl::OnGpuChannelLost,
141 weak_ptr_factory_.GetWeakPtr()));
143 command_buffer_->SetOnConsoleMessageCallback(
144 base::Bind(&WebGraphicsContext3DCommandBufferImpl::OnErrorMessage,
145 weak_ptr_factory_.GetWeakPtr()));
147 real_gl_->SetErrorMessageCallback(getErrorMessageCallback());
154 bool WebGraphicsContext3DCommandBufferImpl::InitializeCommandBuffer(
155 bool onscreen, WebGraphicsContext3DCommandBufferImpl* share_context) {
159 CommandBufferProxyImpl* share_group_command_buffer = NULL;
162 share_group_command_buffer = share_context->command_buffer_.get();
165 ::gpu::gles2::ContextCreationAttribHelper attribs_for_gles2;
166 ConvertAttributes(attributes_, &attribs_for_gles2);
167 attribs_for_gles2.lose_context_when_out_of_memory =
168 lose_context_when_out_of_memory_;
169 DCHECK(attribs_for_gles2.buffer_preserved);
170 std::vector<int32> attribs;
171 attribs_for_gles2.Serialize(&attribs);
173 // Create a proxy to a command buffer in the GPU process.
175 command_buffer_.reset(host_->CreateViewCommandBuffer(
177 share_group_command_buffer,
182 command_buffer_.reset(host_->CreateOffscreenCommandBuffer(
184 share_group_command_buffer,
190 if (!command_buffer_) {
191 DLOG(ERROR) << "GpuChannelHost failed to create command buffer.";
195 DVLOG_IF(1, gpu::error::IsError(command_buffer_->GetLastError()))
196 << "Context dead on arrival. Last error: "
197 << command_buffer_->GetLastError();
198 // Initialize the command buffer.
199 bool result = command_buffer_->Initialize();
200 LOG_IF(ERROR, !result) << "CommandBufferProxy::Initialize failed.";
204 bool WebGraphicsContext3DCommandBufferImpl::CreateContext(bool onscreen) {
205 TRACE_EVENT0("gpu", "WebGfxCtx3DCmdBfrImpl::CreateContext");
206 scoped_refptr<gpu::gles2::ShareGroup> gles2_share_group;
208 scoped_ptr<base::AutoLock> share_group_lock;
209 bool add_to_share_group = false;
210 if (!command_buffer_) {
211 WebGraphicsContext3DCommandBufferImpl* share_context = NULL;
213 share_group_lock.reset(new base::AutoLock(share_group_->lock()));
214 share_context = share_group_->GetAnyContextLocked();
216 if (!InitializeCommandBuffer(onscreen, share_context)) {
217 LOG(ERROR) << "Failed to initialize command buffer.";
222 gles2_share_group = share_context->GetImplementation()->share_group();
224 add_to_share_group = true;
227 // Create the GLES2 helper, which writes the command buffer protocol.
228 gles2_helper_.reset(new gpu::gles2::GLES2CmdHelper(command_buffer_.get()));
229 if (!gles2_helper_->Initialize(mem_limits_.command_buffer_size)) {
230 LOG(ERROR) << "Failed to initialize GLES2CmdHelper.";
234 if (attributes_.noAutomaticFlushes)
235 gles2_helper_->SetAutomaticFlushes(false);
236 // Create a transfer buffer used to copy resources between the renderer
237 // process and the GPU process.
238 transfer_buffer_ .reset(new gpu::TransferBuffer(gles2_helper_.get()));
242 // Create the object exposing the OpenGL API.
243 bool bind_generates_resources = false;
245 new gpu::gles2::GLES2Implementation(gles2_helper_.get(),
246 gles2_share_group.get(),
247 transfer_buffer_.get(),
248 bind_generates_resources,
249 lose_context_when_out_of_memory_,
250 command_buffer_.get()));
251 setGLInterface(real_gl_.get());
253 if (!real_gl_->Initialize(
254 mem_limits_.start_transfer_buffer_size,
255 mem_limits_.min_transfer_buffer_size,
256 mem_limits_.max_transfer_buffer_size,
257 mem_limits_.mapped_memory_reclaim_limit)) {
258 LOG(ERROR) << "Failed to initialize GLES2Implementation.";
262 if (add_to_share_group)
263 share_group_->AddContextLocked(this);
265 if (CommandLine::ForCurrentProcess()->HasSwitch(
266 switches::kEnableGpuClientTracing)) {
267 trace_gl_.reset(new gpu::gles2::GLES2TraceImplementation(GetGLInterface()));
268 setGLInterface(trace_gl_.get());
273 bool WebGraphicsContext3DCommandBufferImpl::InitializeOnCurrentThread() {
274 if (!MaybeInitializeGL()) {
275 DLOG(ERROR) << "Failed to initialize context.";
278 if (gpu::error::IsError(command_buffer_->GetLastError())) {
279 LOG(ERROR) << "Context dead on arrival. Last error: "
280 << command_buffer_->GetLastError();
287 void WebGraphicsContext3DCommandBufferImpl::Destroy() {
288 share_group_->RemoveContext(this);
290 gpu::gles2::GLES2Interface* gl = GetGLInterface();
292 // First flush the context to ensure that any pending frees of resources
293 // are completed. Otherwise, if this context is part of a share group,
294 // those resources might leak. Also, any remaining side effects of commands
295 // issued on this context might not be visible to other contexts in the
298 setGLInterface(NULL);
303 transfer_buffer_.reset();
304 gles2_helper_.reset();
307 if (command_buffer_) {
309 host_->DestroyCommandBuffer(command_buffer_.release());
310 command_buffer_.reset();
317 WebGraphicsContext3DCommandBufferImpl::GetContextSupport() {
318 return real_gl_.get();
321 bool WebGraphicsContext3DCommandBufferImpl::isContextLost() {
322 return initialize_failed_ ||
323 (command_buffer_ && IsCommandBufferContextLost()) ||
324 context_lost_reason_ != GL_NO_ERROR;
327 WGC3Denum WebGraphicsContext3DCommandBufferImpl::getGraphicsResetStatusARB() {
328 if (IsCommandBufferContextLost() &&
329 context_lost_reason_ == GL_NO_ERROR) {
330 return GL_UNKNOWN_CONTEXT_RESET_ARB;
333 return context_lost_reason_;
336 bool WebGraphicsContext3DCommandBufferImpl::IsCommandBufferContextLost() {
337 // If the channel shut down unexpectedly, let that supersede the
338 // command buffer's state.
339 if (host_.get() && host_->IsLost())
341 gpu::CommandBuffer::State state = command_buffer_->GetLastState();
342 return state.error == gpu::error::kLostContext;
346 WebGraphicsContext3DCommandBufferImpl*
347 WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext(
348 GpuChannelHost* host,
349 const WebGraphicsContext3D::Attributes& attributes,
350 bool lose_context_when_out_of_memory,
351 const GURL& active_url,
352 const SharedMemoryLimits& limits,
353 WebGraphicsContext3DCommandBufferImpl* share_context) {
357 if (share_context && share_context->IsCommandBufferContextLost())
360 return new WebGraphicsContext3DCommandBufferImpl(
365 lose_context_when_out_of_memory,
372 WGC3Denum convertReason(gpu::error::ContextLostReason reason) {
374 case gpu::error::kGuilty:
375 return GL_GUILTY_CONTEXT_RESET_ARB;
376 case gpu::error::kInnocent:
377 return GL_INNOCENT_CONTEXT_RESET_ARB;
378 case gpu::error::kUnknown:
379 return GL_UNKNOWN_CONTEXT_RESET_ARB;
383 return GL_UNKNOWN_CONTEXT_RESET_ARB;
386 } // anonymous namespace
388 void WebGraphicsContext3DCommandBufferImpl::OnGpuChannelLost() {
389 context_lost_reason_ = convertReason(
390 command_buffer_->GetLastState().context_lost_reason);
391 if (context_lost_callback_) {
392 context_lost_callback_->onContextLost();
395 share_group_->RemoveAllContexts();
399 base::AutoLock lock(g_default_share_groups_lock.Get());
400 g_default_share_groups.Get().erase(host_.get());
404 } // namespace content