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_lib.h"
32 #include "gpu/command_buffer/client/gles2_trace_implementation.h"
33 #include "gpu/command_buffer/client/transfer_buffer.h"
34 #include "gpu/command_buffer/common/constants.h"
35 #include "gpu/command_buffer/common/gpu_memory_allocation.h"
36 #include "gpu/command_buffer/common/mailbox.h"
37 #include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.h"
38 #include "third_party/skia/include/core/SkTypes.h"
44 static base::LazyInstance
<base::Lock
>::Leaky
45 g_default_share_groups_lock
= LAZY_INSTANCE_INITIALIZER
;
47 typedef std::map
<GpuChannelHost
*,
48 scoped_refptr
<WebGraphicsContext3DCommandBufferImpl::ShareGroup
> >
50 static base::LazyInstance
<ShareGroupMap
> g_default_share_groups
=
51 LAZY_INSTANCE_INITIALIZER
;
53 scoped_refptr
<WebGraphicsContext3DCommandBufferImpl::ShareGroup
>
54 GetDefaultShareGroupForHost(GpuChannelHost
* host
) {
55 base::AutoLock
lock(g_default_share_groups_lock
.Get());
57 ShareGroupMap
& share_groups
= g_default_share_groups
.Get();
58 ShareGroupMap::iterator it
= share_groups
.find(host
);
59 if (it
== share_groups
.end()) {
60 scoped_refptr
<WebGraphicsContext3DCommandBufferImpl::ShareGroup
> group
=
61 new WebGraphicsContext3DCommandBufferImpl::ShareGroup();
62 share_groups
[host
] = group
;
68 // Singleton used to initialize and terminate the gles2 library.
69 class GLES2Initializer
{
80 DISALLOW_COPY_AND_ASSIGN(GLES2Initializer
);
83 ////////////////////////////////////////////////////////////////////////////////
85 base::LazyInstance
<GLES2Initializer
> g_gles2_initializer
=
86 LAZY_INSTANCE_INITIALIZER
;
88 ////////////////////////////////////////////////////////////////////////////////
90 } // namespace anonymous
92 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits::SharedMemoryLimits()
93 : command_buffer_size(kDefaultCommandBufferSize
),
94 start_transfer_buffer_size(kDefaultStartTransferBufferSize
),
95 min_transfer_buffer_size(kDefaultMinTransferBufferSize
),
96 max_transfer_buffer_size(kDefaultMaxTransferBufferSize
),
97 mapped_memory_reclaim_limit(gpu::gles2::GLES2Implementation::kNoLimit
) {}
99 WebGraphicsContext3DCommandBufferImpl::ShareGroup::ShareGroup() {
102 WebGraphicsContext3DCommandBufferImpl::ShareGroup::~ShareGroup() {
103 DCHECK(contexts_
.empty());
106 WebGraphicsContext3DCommandBufferImpl::WebGraphicsContext3DCommandBufferImpl(
108 const GURL
& active_url
,
109 GpuChannelHost
* host
,
110 const Attributes
& attributes
,
111 bool lose_context_when_out_of_memory
,
112 const SharedMemoryLimits
& limits
,
113 WebGraphicsContext3DCommandBufferImpl
* share_context
)
114 : lose_context_when_out_of_memory_(lose_context_when_out_of_memory
),
115 attributes_(attributes
),
118 surface_id_(surface_id
),
119 active_url_(active_url
),
120 gpu_preference_(attributes
.preferDiscreteGPU
? gfx::PreferDiscreteGpu
121 : gfx::PreferIntegratedGpu
),
122 weak_ptr_factory_(this),
123 mem_limits_(limits
) {
125 DCHECK(!attributes_
.shareResources
);
126 share_group_
= share_context
->share_group_
;
128 share_group_
= attributes_
.shareResources
129 ? GetDefaultShareGroupForHost(host
)
130 : scoped_refptr
<WebGraphicsContext3DCommandBufferImpl::ShareGroup
>(
135 WebGraphicsContext3DCommandBufferImpl::
136 ~WebGraphicsContext3DCommandBufferImpl() {
138 real_gl_
->SetErrorMessageCallback(NULL
);
144 bool WebGraphicsContext3DCommandBufferImpl::MaybeInitializeGL() {
148 if (initialize_failed_
)
151 TRACE_EVENT0("gpu", "WebGfxCtx3DCmdBfrImpl::MaybeInitializeGL");
153 if (!CreateContext(surface_id_
!= 0)) {
155 initialize_failed_
= true;
159 if (gl_
&& attributes_
.webGL
)
160 gl_
->EnableFeatureCHROMIUM("webgl_enable_glsl_webgl_validation");
162 command_buffer_
->SetChannelErrorCallback(
163 base::Bind(&WebGraphicsContext3DCommandBufferImpl::OnGpuChannelLost
,
164 weak_ptr_factory_
.GetWeakPtr()));
166 command_buffer_
->SetOnConsoleMessageCallback(
167 base::Bind(&WebGraphicsContext3DCommandBufferImpl::OnErrorMessage
,
168 weak_ptr_factory_
.GetWeakPtr()));
170 real_gl_
->SetErrorMessageCallback(getErrorMessageCallback());
177 bool WebGraphicsContext3DCommandBufferImpl::InitializeCommandBuffer(
178 bool onscreen
, WebGraphicsContext3DCommandBufferImpl
* share_context
) {
182 CommandBufferProxyImpl
* share_group_command_buffer
= NULL
;
185 share_group_command_buffer
= share_context
->command_buffer_
.get();
188 ::gpu::gles2::ContextCreationAttribHelper attribs_for_gles2
;
189 ConvertAttributes(attributes_
, &attribs_for_gles2
);
190 attribs_for_gles2
.lose_context_when_out_of_memory
=
191 lose_context_when_out_of_memory_
;
192 DCHECK(attribs_for_gles2
.buffer_preserved
);
193 std::vector
<int32
> attribs
;
194 attribs_for_gles2
.Serialize(&attribs
);
196 // Create a proxy to a command buffer in the GPU process.
198 command_buffer_
.reset(host_
->CreateViewCommandBuffer(
200 share_group_command_buffer
,
205 command_buffer_
.reset(host_
->CreateOffscreenCommandBuffer(
207 share_group_command_buffer
,
213 if (!command_buffer_
) {
214 DLOG(ERROR
) << "GpuChannelHost failed to create command buffer.";
218 DVLOG_IF(1, gpu::error::IsError(command_buffer_
->GetLastError()))
219 << "Context dead on arrival. Last error: "
220 << command_buffer_
->GetLastError();
221 // Initialize the command buffer.
222 bool result
= command_buffer_
->Initialize();
223 LOG_IF(ERROR
, !result
) << "CommandBufferProxy::Initialize failed.";
227 bool WebGraphicsContext3DCommandBufferImpl::CreateContext(bool onscreen
) {
228 TRACE_EVENT0("gpu", "WebGfxCtx3DCmdBfrImpl::CreateContext");
229 // Ensure the gles2 library is initialized first in a thread safe way.
230 g_gles2_initializer
.Get();
232 scoped_refptr
<gpu::gles2::ShareGroup
> gles2_share_group
;
234 scoped_ptr
<base::AutoLock
> share_group_lock
;
235 bool add_to_share_group
= false;
236 if (!command_buffer_
) {
237 WebGraphicsContext3DCommandBufferImpl
* share_context
= NULL
;
239 share_group_lock
.reset(new base::AutoLock(share_group_
->lock()));
240 share_context
= share_group_
->GetAnyContextLocked();
242 if (!InitializeCommandBuffer(onscreen
, share_context
)) {
243 LOG(ERROR
) << "Failed to initialize command buffer.";
248 gles2_share_group
= share_context
->GetImplementation()->share_group();
250 add_to_share_group
= true;
253 // Create the GLES2 helper, which writes the command buffer protocol.
254 gles2_helper_
.reset(new gpu::gles2::GLES2CmdHelper(command_buffer_
.get()));
255 if (!gles2_helper_
->Initialize(mem_limits_
.command_buffer_size
)) {
256 LOG(ERROR
) << "Failed to initialize GLES2CmdHelper.";
260 if (attributes_
.noAutomaticFlushes
)
261 gles2_helper_
->SetAutomaticFlushes(false);
262 // Create a transfer buffer used to copy resources between the renderer
263 // process and the GPU process.
264 transfer_buffer_
.reset(new gpu::TransferBuffer(gles2_helper_
.get()));
268 // Create the object exposing the OpenGL API.
269 bool bind_generates_resources
= false;
271 new gpu::gles2::GLES2Implementation(gles2_helper_
.get(),
272 gles2_share_group
.get(),
273 transfer_buffer_
.get(),
274 bind_generates_resources
,
275 lose_context_when_out_of_memory_
,
276 command_buffer_
.get()));
277 setGLInterface(real_gl_
.get());
279 if (!real_gl_
->Initialize(
280 mem_limits_
.start_transfer_buffer_size
,
281 mem_limits_
.min_transfer_buffer_size
,
282 mem_limits_
.max_transfer_buffer_size
,
283 mem_limits_
.mapped_memory_reclaim_limit
)) {
284 LOG(ERROR
) << "Failed to initialize GLES2Implementation.";
288 if (add_to_share_group
)
289 share_group_
->AddContextLocked(this);
291 if (CommandLine::ForCurrentProcess()->HasSwitch(
292 switches::kEnableGpuClientTracing
)) {
293 trace_gl_
.reset(new gpu::gles2::GLES2TraceImplementation(GetGLInterface()));
294 setGLInterface(trace_gl_
.get());
299 bool WebGraphicsContext3DCommandBufferImpl::makeContextCurrent() {
300 if (!MaybeInitializeGL()) {
301 DLOG(ERROR
) << "Failed to initialize context.";
304 gles2::SetGLContext(GetGLInterface());
305 if (gpu::error::IsError(command_buffer_
->GetLastError())) {
306 LOG(ERROR
) << "Context dead on arrival. Last error: "
307 << command_buffer_
->GetLastError();
314 void WebGraphicsContext3DCommandBufferImpl::Destroy() {
315 share_group_
->RemoveContext(this);
317 gpu::gles2::GLES2Interface
* gl
= GetGLInterface();
319 // First flush the context to ensure that any pending frees of resources
320 // are completed. Otherwise, if this context is part of a share group,
321 // those resources might leak. Also, any remaining side effects of commands
322 // issued on this context might not be visible to other contexts in the
325 setGLInterface(NULL
);
330 transfer_buffer_
.reset();
331 gles2_helper_
.reset();
334 if (command_buffer_
) {
336 host_
->DestroyCommandBuffer(command_buffer_
.release());
337 command_buffer_
.reset();
344 WebGraphicsContext3DCommandBufferImpl::GetContextSupport() {
345 return real_gl_
.get();
348 bool WebGraphicsContext3DCommandBufferImpl::isContextLost() {
349 return initialize_failed_
||
350 (command_buffer_
&& IsCommandBufferContextLost()) ||
351 context_lost_reason_
!= GL_NO_ERROR
;
354 WGC3Denum
WebGraphicsContext3DCommandBufferImpl::getGraphicsResetStatusARB() {
355 if (IsCommandBufferContextLost() &&
356 context_lost_reason_
== GL_NO_ERROR
) {
357 return GL_UNKNOWN_CONTEXT_RESET_ARB
;
360 return context_lost_reason_
;
363 bool WebGraphicsContext3DCommandBufferImpl::IsCommandBufferContextLost() {
364 // If the channel shut down unexpectedly, let that supersede the
365 // command buffer's state.
366 if (host_
.get() && host_
->IsLost())
368 gpu::CommandBuffer::State state
= command_buffer_
->GetLastState();
369 return state
.error
== gpu::error::kLostContext
;
373 WebGraphicsContext3DCommandBufferImpl
*
374 WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext(
375 GpuChannelHost
* host
,
376 const WebGraphicsContext3D::Attributes
& attributes
,
377 bool lose_context_when_out_of_memory
,
378 const GURL
& active_url
,
379 const SharedMemoryLimits
& limits
,
380 WebGraphicsContext3DCommandBufferImpl
* share_context
) {
384 if (share_context
&& share_context
->IsCommandBufferContextLost())
387 return new WebGraphicsContext3DCommandBufferImpl(
392 lose_context_when_out_of_memory
,
399 WGC3Denum
convertReason(gpu::error::ContextLostReason reason
) {
401 case gpu::error::kGuilty
:
402 return GL_GUILTY_CONTEXT_RESET_ARB
;
403 case gpu::error::kInnocent
:
404 return GL_INNOCENT_CONTEXT_RESET_ARB
;
405 case gpu::error::kUnknown
:
406 return GL_UNKNOWN_CONTEXT_RESET_ARB
;
410 return GL_UNKNOWN_CONTEXT_RESET_ARB
;
413 } // anonymous namespace
415 void WebGraphicsContext3DCommandBufferImpl::OnGpuChannelLost() {
416 context_lost_reason_
= convertReason(
417 command_buffer_
->GetLastState().context_lost_reason
);
418 if (context_lost_callback_
) {
419 context_lost_callback_
->onContextLost();
422 share_group_
->RemoveAllContexts();
426 base::AutoLock
lock(g_default_share_groups_lock
.Get());
427 g_default_share_groups
.Get().erase(host_
.get());
431 } // namespace content