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.
6 #include "base/bind_helpers.h"
7 #include "base/command_line.h"
9 #include "base/json/json_writer.h"
10 #include "base/memory/shared_memory.h"
11 #include "base/time/time.h"
12 #include "base/trace_event/trace_event.h"
13 #include "build/build_config.h"
14 #include "content/common/gpu/gpu_channel.h"
15 #include "content/common/gpu/gpu_channel_manager.h"
16 #include "content/common/gpu/gpu_command_buffer_stub.h"
17 #include "content/common/gpu/gpu_memory_manager.h"
18 #include "content/common/gpu/gpu_memory_tracking.h"
19 #include "content/common/gpu/gpu_messages.h"
20 #include "content/common/gpu/gpu_watchdog.h"
21 #include "content/common/gpu/image_transport_surface.h"
22 #include "content/common/gpu/media/gpu_video_decode_accelerator.h"
23 #include "content/common/gpu/media/gpu_video_encode_accelerator.h"
24 #include "content/public/common/content_client.h"
25 #include "content/public/common/content_switches.h"
26 #include "gpu/command_buffer/common/constants.h"
27 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
28 #include "gpu/command_buffer/common/mailbox.h"
29 #include "gpu/command_buffer/service/gl_context_virtual.h"
30 #include "gpu/command_buffer/service/gl_state_restorer_impl.h"
31 #include "gpu/command_buffer/service/image_factory.h"
32 #include "gpu/command_buffer/service/image_manager.h"
33 #include "gpu/command_buffer/service/logger.h"
34 #include "gpu/command_buffer/service/mailbox_manager.h"
35 #include "gpu/command_buffer/service/memory_tracking.h"
36 #include "gpu/command_buffer/service/query_manager.h"
37 #include "gpu/command_buffer/service/sync_point_manager.h"
38 #include "gpu/command_buffer/service/transfer_buffer_manager.h"
39 #include "gpu/command_buffer/service/valuebuffer_manager.h"
40 #include "ui/gl/gl_bindings.h"
41 #include "ui/gl/gl_switches.h"
44 #include "base/win/win_util.h"
45 #include "content/public/common/sandbox_init.h"
48 #if defined(OS_ANDROID)
49 #include "content/common/gpu/stream_texture_android.h"
53 struct WaitForCommandState
{
54 WaitForCommandState(int32 start
, int32 end
, IPC::Message
* reply
)
55 : start(start
), end(end
), reply(reply
) {}
59 scoped_ptr
<IPC::Message
> reply
;
64 // The GpuCommandBufferMemoryTracker class provides a bridge between the
65 // ContextGroup's memory type managers and the GpuMemoryManager class.
66 class GpuCommandBufferMemoryTracker
: public gpu::gles2::MemoryTracker
{
68 explicit GpuCommandBufferMemoryTracker(GpuChannel
* channel
,
69 uint64_t share_group_tracing_guid
)
71 channel
->gpu_channel_manager()
72 ->gpu_memory_manager()
73 ->CreateTrackingGroup(channel
->GetClientPID(), this)),
74 client_tracing_id_(channel
->client_tracing_id()),
75 client_id_(channel
->client_id()),
76 share_group_tracing_guid_(share_group_tracing_guid
) {}
78 void TrackMemoryAllocatedChange(
81 gpu::gles2::MemoryTracker::Pool pool
) override
{
82 tracking_group_
->TrackMemoryAllocatedChange(
83 old_size
, new_size
, pool
);
86 bool EnsureGPUMemoryAvailable(size_t size_needed
) override
{
87 return tracking_group_
->EnsureGPUMemoryAvailable(size_needed
);
90 uint64_t ClientTracingId() const override
{ return client_tracing_id_
; }
91 int ClientId() const override
{ return client_id_
; }
92 uint64_t ShareGroupTracingGUID() const override
{
93 return share_group_tracing_guid_
;
97 ~GpuCommandBufferMemoryTracker() override
{}
98 scoped_ptr
<GpuMemoryTrackingGroup
> tracking_group_
;
99 const uint64_t client_tracing_id_
;
100 const int client_id_
;
101 const uint64_t share_group_tracing_guid_
;
103 DISALLOW_COPY_AND_ASSIGN(GpuCommandBufferMemoryTracker
);
106 // FastSetActiveURL will shortcut the expensive call to SetActiveURL when the
108 void FastSetActiveURL(const GURL
& url
, size_t url_hash
) {
109 // Leave the previously set URL in the empty case -- empty URLs are given by
110 // BlinkPlatformImpl::createOffscreenGraphicsContext3D. Hopefully the
111 // onscreen context URL was set previously and will show up even when a crash
112 // occurs during offscreen command processing.
115 static size_t g_last_url_hash
= 0;
116 if (url_hash
!= g_last_url_hash
) {
117 g_last_url_hash
= url_hash
;
118 GetContentClient()->SetActiveURL(url
);
122 // The first time polling a fence, delay some extra time to allow other
123 // stubs to process some work, or else the timing of the fences could
124 // allow a pattern of alternating fast and slow frames to occur.
125 const int64 kHandleMoreWorkPeriodMs
= 2;
126 const int64 kHandleMoreWorkPeriodBusyMs
= 1;
128 // Prevents idle work from being starved.
129 const int64 kMaxTimeSinceIdleMs
= 10;
131 class DevToolsChannelData
: public base::trace_event::ConvertableToTraceFormat
{
133 static scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
134 CreateForChannel(GpuChannel
* channel
);
136 void AppendAsTraceFormat(std::string
* out
) const override
{
138 base::JSONWriter::Write(*value_
, &tmp
);
143 explicit DevToolsChannelData(base::Value
* value
) : value_(value
) {}
144 ~DevToolsChannelData() override
{}
145 scoped_ptr
<base::Value
> value_
;
146 DISALLOW_COPY_AND_ASSIGN(DevToolsChannelData
);
149 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
150 DevToolsChannelData::CreateForChannel(GpuChannel
* channel
) {
151 scoped_ptr
<base::DictionaryValue
> res(new base::DictionaryValue
);
152 res
->SetInteger("renderer_pid", channel
->GetClientPID());
153 res
->SetDouble("used_bytes", channel
->GetMemoryUsage());
154 res
->SetDouble("limit_bytes",
155 channel
->gpu_channel_manager()
156 ->gpu_memory_manager()
157 ->GetMaximumClientAllocation());
158 return new DevToolsChannelData(res
.release());
161 void RunOnThread(scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
,
162 const base::Closure
& callback
) {
163 if (task_runner
->BelongsToCurrentThread()) {
166 task_runner
->PostTask(FROM_HERE
, callback
);
170 uint64_t GetCommandBufferID(int channel_id
, int32 route_id
) {
171 return (static_cast<uint64_t>(channel_id
) << 32) | route_id
;
176 GpuCommandBufferStub::GpuCommandBufferStub(
178 base::SingleThreadTaskRunner
* task_runner
,
179 GpuCommandBufferStub
* share_group
,
180 const gfx::GLSurfaceHandle
& handle
,
181 gpu::gles2::MailboxManager
* mailbox_manager
,
182 gpu::gles2::SubscriptionRefSet
* subscription_ref_set
,
183 gpu::ValueStateMap
* pending_valuebuffer_state
,
184 const gfx::Size
& size
,
185 const gpu::gles2::DisallowedFeatures
& disallowed_features
,
186 const std::vector
<int32
>& attribs
,
187 gfx::GpuPreference gpu_preference
,
188 bool use_virtualized_gl_context
,
192 GpuWatchdog
* watchdog
,
194 const GURL
& active_url
)
196 task_runner_(task_runner
),
200 disallowed_features_(disallowed_features
),
201 requested_attribs_(attribs
),
202 gpu_preference_(gpu_preference
),
203 use_virtualized_gl_context_(use_virtualized_gl_context
),
204 command_buffer_id_(GetCommandBufferID(channel
->client_id(), route_id
)),
205 stream_id_(stream_id
),
207 surface_id_(surface_id
),
209 last_flush_count_(0),
210 last_memory_allocation_valid_(false),
212 waiting_for_sync_point_(false),
213 previous_processed_num_(0),
214 active_url_(active_url
),
215 total_gpu_memory_(0) {
216 active_url_hash_
= base::Hash(active_url
.possibly_invalid_spec());
217 FastSetActiveURL(active_url_
, active_url_hash_
);
219 gpu::gles2::ContextCreationAttribHelper attrib_parser
;
220 attrib_parser
.Parse(requested_attribs_
);
223 context_group_
= share_group
->context_group_
;
224 DCHECK(context_group_
->bind_generates_resource() ==
225 attrib_parser
.bind_generates_resource
);
227 context_group_
= new gpu::gles2::ContextGroup(
229 new GpuCommandBufferMemoryTracker(channel
, command_buffer_id_
),
230 channel_
->gpu_channel_manager()->shader_translator_cache(),
231 channel_
->gpu_channel_manager()->framebuffer_completeness_cache(), NULL
,
232 subscription_ref_set
, pending_valuebuffer_state
,
233 attrib_parser
.bind_generates_resource
);
236 use_virtualized_gl_context_
|=
237 context_group_
->feature_info()->workarounds().use_virtualized_gl_contexts
;
239 bool is_offscreen
= surface_id_
== 0;
240 if (is_offscreen
&& initial_size_
.IsEmpty()) {
241 // If we're an offscreen surface with zero width and/or height, set to a
242 // non-zero size so that we have a complete framebuffer for operations like
244 initial_size_
= gfx::Size(1, 1);
248 GpuCommandBufferStub::~GpuCommandBufferStub() {
252 GpuMemoryManager
* GpuCommandBufferStub::GetMemoryManager() const {
253 return channel()->gpu_channel_manager()->gpu_memory_manager();
256 bool GpuCommandBufferStub::OnMessageReceived(const IPC::Message
& message
) {
257 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"),
260 DevToolsChannelData::CreateForChannel(channel()));
261 FastSetActiveURL(active_url_
, active_url_hash_
);
263 bool have_context
= false;
264 // Ensure the appropriate GL context is current before handling any IPC
265 // messages directed at the command buffer. This ensures that the message
266 // handler can assume that the context is current (not necessary for
267 // RetireSyncPoint or WaitSyncPoint).
268 if (decoder_
.get() &&
269 message
.type() != GpuCommandBufferMsg_SetGetBuffer::ID
&&
270 message
.type() != GpuCommandBufferMsg_WaitForTokenInRange::ID
&&
271 message
.type() != GpuCommandBufferMsg_WaitForGetOffsetInRange::ID
&&
272 message
.type() != GpuCommandBufferMsg_RegisterTransferBuffer::ID
&&
273 message
.type() != GpuCommandBufferMsg_DestroyTransferBuffer::ID
&&
274 message
.type() != GpuCommandBufferMsg_RetireSyncPoint::ID
&&
275 message
.type() != GpuCommandBufferMsg_SignalSyncPoint::ID
&&
277 GpuCommandBufferMsg_SetClientHasMemoryAllocationChangedCallback::ID
) {
283 // Always use IPC_MESSAGE_HANDLER_DELAY_REPLY for synchronous message handlers
284 // here. This is so the reply can be delayed if the scheduler is unscheduled.
286 IPC_BEGIN_MESSAGE_MAP(GpuCommandBufferStub
, message
)
287 IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuCommandBufferMsg_Initialize
,
289 IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuCommandBufferMsg_SetGetBuffer
,
291 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_ProduceFrontBuffer
,
292 OnProduceFrontBuffer
);
293 IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuCommandBufferMsg_WaitForTokenInRange
,
294 OnWaitForTokenInRange
);
295 IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuCommandBufferMsg_WaitForGetOffsetInRange
,
296 OnWaitForGetOffsetInRange
);
297 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_AsyncFlush
, OnAsyncFlush
);
298 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_RegisterTransferBuffer
,
299 OnRegisterTransferBuffer
);
300 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_DestroyTransferBuffer
,
301 OnDestroyTransferBuffer
);
302 IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuCommandBufferMsg_CreateVideoDecoder
,
303 OnCreateVideoDecoder
)
304 IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuCommandBufferMsg_CreateVideoEncoder
,
305 OnCreateVideoEncoder
)
306 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_SetSurfaceVisible
,
308 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_RetireSyncPoint
,
310 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_SignalSyncPoint
,
312 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_SignalQuery
,
315 GpuCommandBufferMsg_SetClientHasMemoryAllocationChangedCallback
,
316 OnSetClientHasMemoryAllocationChangedCallback
)
317 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_CreateImage
, OnCreateImage
);
318 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_DestroyImage
, OnDestroyImage
);
319 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_CreateStreamTexture
,
320 OnCreateStreamTexture
)
321 IPC_MESSAGE_UNHANDLED(handled
= false)
322 IPC_END_MESSAGE_MAP()
324 CheckCompleteWaits();
326 // Ensure that any delayed work that was created will be handled.
329 scheduler_
->ProcessPendingQueries();
331 base::TimeDelta::FromMilliseconds(kHandleMoreWorkPeriodMs
));
338 bool GpuCommandBufferStub::Send(IPC::Message
* message
) {
339 return channel_
->Send(message
);
342 bool GpuCommandBufferStub::IsScheduled() {
343 return (!scheduler_
.get() || scheduler_
->scheduled());
346 void GpuCommandBufferStub::PollWork() {
347 // Post another delayed task if we have not yet reached the time at which
348 // we should process delayed work.
349 base::TimeTicks current_time
= base::TimeTicks::Now();
350 DCHECK(!process_delayed_work_time_
.is_null());
351 if (process_delayed_work_time_
> current_time
) {
352 task_runner_
->PostDelayedTask(
353 FROM_HERE
, base::Bind(&GpuCommandBufferStub::PollWork
, AsWeakPtr()),
354 process_delayed_work_time_
- current_time
);
357 process_delayed_work_time_
= base::TimeTicks();
362 void GpuCommandBufferStub::PerformWork() {
363 TRACE_EVENT0("gpu", "GpuCommandBufferStub::PerformWork");
365 FastSetActiveURL(active_url_
, active_url_hash_
);
366 if (decoder_
.get() && !MakeCurrent())
370 uint32_t current_unprocessed_num
=
371 channel()->gpu_channel_manager()->GetUnprocessedOrderNum();
372 // We're idle when no messages were processed or scheduled.
373 bool is_idle
= (previous_processed_num_
== current_unprocessed_num
);
374 if (!is_idle
&& !last_idle_time_
.is_null()) {
375 base::TimeDelta time_since_idle
=
376 base::TimeTicks::Now() - last_idle_time_
;
377 base::TimeDelta max_time_since_idle
=
378 base::TimeDelta::FromMilliseconds(kMaxTimeSinceIdleMs
);
380 // Force idle when it's been too long since last time we were idle.
381 if (time_since_idle
> max_time_since_idle
)
386 last_idle_time_
= base::TimeTicks::Now();
387 scheduler_
->PerformIdleWork();
390 scheduler_
->ProcessPendingQueries();
394 base::TimeDelta::FromMilliseconds(kHandleMoreWorkPeriodBusyMs
));
397 bool GpuCommandBufferStub::HasUnprocessedCommands() {
398 if (command_buffer_
) {
399 gpu::CommandBuffer::State state
= command_buffer_
->GetLastState();
400 return command_buffer_
->GetPutOffset() != state
.get_offset
&&
401 !gpu::error::IsError(state
.error
);
406 void GpuCommandBufferStub::ScheduleDelayedWork(base::TimeDelta delay
) {
407 bool has_more_work
= scheduler_
.get() && (scheduler_
->HasPendingQueries() ||
408 scheduler_
->HasMoreIdleWork());
409 if (!has_more_work
) {
410 last_idle_time_
= base::TimeTicks();
414 base::TimeTicks current_time
= base::TimeTicks::Now();
415 // |process_delayed_work_time_| is set if processing of delayed work is
416 // already scheduled. Just update the time if already scheduled.
417 if (!process_delayed_work_time_
.is_null()) {
418 process_delayed_work_time_
= current_time
+ delay
;
422 // Idle when no messages are processed between now and when
423 // PollWork is called.
424 previous_processed_num_
=
425 channel()->gpu_channel_manager()->GetProcessedOrderNum();
426 if (last_idle_time_
.is_null())
427 last_idle_time_
= current_time
;
429 // IsScheduled() returns true after passing all unschedule fences
430 // and this is when we can start performing idle work. Idle work
431 // is done synchronously so we can set delay to 0 and instead poll
432 // for more work at the rate idle work is performed. This also ensures
433 // that idle work is done as efficiently as possible without any
434 // unnecessary delays.
435 if (scheduler_
.get() && scheduler_
->scheduled() &&
436 scheduler_
->HasMoreIdleWork()) {
437 delay
= base::TimeDelta();
440 process_delayed_work_time_
= current_time
+ delay
;
441 task_runner_
->PostDelayedTask(
442 FROM_HERE
, base::Bind(&GpuCommandBufferStub::PollWork
, AsWeakPtr()),
446 bool GpuCommandBufferStub::MakeCurrent() {
447 if (decoder_
->MakeCurrent())
449 DLOG(ERROR
) << "Context lost because MakeCurrent failed.";
450 command_buffer_
->SetContextLostReason(decoder_
->GetContextLostReason());
451 command_buffer_
->SetParseError(gpu::error::kLostContext
);
456 void GpuCommandBufferStub::Destroy() {
457 if (wait_for_token_
) {
458 Send(wait_for_token_
->reply
.release());
459 wait_for_token_
.reset();
461 if (wait_for_get_offset_
) {
462 Send(wait_for_get_offset_
->reply
.release());
463 wait_for_get_offset_
.reset();
467 GpuChannelManager
* gpu_channel_manager
= channel_
->gpu_channel_manager();
468 if (handle_
.is_null() && !active_url_
.is_empty()) {
469 gpu_channel_manager
->Send(
470 new GpuHostMsg_DidDestroyOffscreenContext(active_url_
));
472 gpu_channel_manager
->Send(
473 new GpuHostMsg_DestroyCommandBuffer(surface_id()));
476 memory_manager_client_state_
.reset();
478 while (!sync_points_
.empty())
479 OnRetireSyncPoint(sync_points_
.front());
482 decoder_
->set_engine(NULL
);
484 // The scheduler has raw references to the decoder and the command buffer so
485 // destroy it before those.
488 bool have_context
= false;
489 if (decoder_
&& decoder_
->GetGLContext()) {
490 // Try to make the context current regardless of whether it was lost, so we
491 // don't leak resources.
492 have_context
= decoder_
->GetGLContext()->MakeCurrent(surface_
.get());
494 FOR_EACH_OBSERVER(DestructionObserver
,
495 destruction_observers_
,
496 OnWillDestroyStub());
499 decoder_
->Destroy(have_context
);
503 command_buffer_
.reset();
505 // Remove this after crbug.com/248395 is sorted out.
509 void GpuCommandBufferStub::OnInitializeFailed(IPC::Message
* reply_message
) {
511 GpuCommandBufferMsg_Initialize::WriteReplyParams(
512 reply_message
, false, gpu::Capabilities());
516 void GpuCommandBufferStub::OnInitialize(
517 base::SharedMemoryHandle shared_state_handle
,
518 IPC::Message
* reply_message
) {
519 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnInitialize");
520 DCHECK(!command_buffer_
.get());
522 scoped_ptr
<base::SharedMemory
> shared_state_shm(
523 new base::SharedMemory(shared_state_handle
, false));
525 command_buffer_
.reset(new gpu::CommandBufferService(
526 context_group_
->transfer_buffer_manager()));
528 bool result
= command_buffer_
->Initialize();
531 decoder_
.reset(::gpu::gles2::GLES2Decoder::Create(context_group_
.get()));
532 scheduler_
.reset(new gpu::GpuScheduler(command_buffer_
.get(),
535 if (preemption_flag_
.get())
536 scheduler_
->SetPreemptByFlag(preemption_flag_
);
538 decoder_
->set_engine(scheduler_
.get());
540 if (!handle_
.is_null()) {
541 #if defined(OS_MACOSX) || defined(UI_COMPOSITOR_IMAGE_TRANSPORT)
543 LOG(ERROR
) << "No software support.";
544 OnInitializeFailed(reply_message
);
549 surface_
= ImageTransportSurface::CreateSurface(
550 channel_
->gpu_channel_manager(),
554 GpuChannelManager
* manager
= channel_
->gpu_channel_manager();
555 surface_
= manager
->GetDefaultOffscreenSurface();
558 if (!surface_
.get()) {
559 DLOG(ERROR
) << "Failed to create surface.";
560 OnInitializeFailed(reply_message
);
564 scoped_refptr
<gfx::GLContext
> context
;
565 if (use_virtualized_gl_context_
&& channel_
->share_group()) {
566 context
= channel_
->share_group()->GetSharedContext();
567 if (!context
.get()) {
568 context
= gfx::GLContext::CreateGLContext(
569 channel_
->share_group(),
570 channel_
->gpu_channel_manager()->GetDefaultOffscreenSurface(),
572 if (!context
.get()) {
573 DLOG(ERROR
) << "Failed to create shared context for virtualization.";
574 OnInitializeFailed(reply_message
);
577 channel_
->share_group()->SetSharedContext(context
.get());
579 // This should be a non-virtual GL context.
580 DCHECK(context
->GetHandle());
581 context
= new gpu::GLContextVirtual(
582 channel_
->share_group(), context
.get(), decoder_
->AsWeakPtr());
583 if (!context
->Initialize(surface_
.get(), gpu_preference_
)) {
584 // TODO(sievers): The real context created above for the default
585 // offscreen surface might not be compatible with this surface.
586 // Need to adjust at least GLX to be able to create the initial context
587 // with a config that is compatible with onscreen and offscreen surfaces.
590 DLOG(ERROR
) << "Failed to initialize virtual GL context.";
591 OnInitializeFailed(reply_message
);
595 if (!context
.get()) {
596 context
= gfx::GLContext::CreateGLContext(
597 channel_
->share_group(), surface_
.get(), gpu_preference_
);
599 if (!context
.get()) {
600 DLOG(ERROR
) << "Failed to create context.";
601 OnInitializeFailed(reply_message
);
605 if (!context
->MakeCurrent(surface_
.get())) {
606 LOG(ERROR
) << "Failed to make context current.";
607 OnInitializeFailed(reply_message
);
611 if (!context
->GetGLStateRestorer()) {
612 context
->SetGLStateRestorer(
613 new gpu::GLStateRestorerImpl(decoder_
->AsWeakPtr()));
616 if (!context
->GetTotalGpuMemory(&total_gpu_memory_
))
617 total_gpu_memory_
= 0;
619 if (!context_group_
->has_program_cache() &&
620 !context_group_
->feature_info()->workarounds().disable_program_cache
) {
621 context_group_
->set_program_cache(
622 channel_
->gpu_channel_manager()->program_cache());
625 // Initialize the decoder with either the view or pbuffer GLContext.
626 if (!decoder_
->Initialize(surface_
,
630 disallowed_features_
,
631 requested_attribs_
)) {
632 DLOG(ERROR
) << "Failed to initialize decoder.";
633 OnInitializeFailed(reply_message
);
637 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
638 switches::kEnableGPUServiceLogging
)) {
639 decoder_
->set_log_commands(true);
642 decoder_
->GetLogger()->SetMsgCallback(
643 base::Bind(&GpuCommandBufferStub::SendConsoleMessage
,
644 base::Unretained(this)));
645 decoder_
->SetShaderCacheCallback(
646 base::Bind(&GpuCommandBufferStub::SendCachedShader
,
647 base::Unretained(this)));
648 decoder_
->SetWaitSyncPointCallback(
649 base::Bind(&GpuCommandBufferStub::OnWaitSyncPoint
,
650 base::Unretained(this)));
652 command_buffer_
->SetPutOffsetChangeCallback(
653 base::Bind(&GpuCommandBufferStub::PutChanged
, base::Unretained(this)));
654 command_buffer_
->SetGetBufferChangeCallback(
655 base::Bind(&gpu::GpuScheduler::SetGetBuffer
,
656 base::Unretained(scheduler_
.get())));
657 command_buffer_
->SetParseErrorCallback(
658 base::Bind(&GpuCommandBufferStub::OnParseError
, base::Unretained(this)));
659 scheduler_
->SetSchedulingChangedCallback(base::Bind(
660 &GpuCommandBufferStub::OnSchedulingChanged
, base::Unretained(this)));
663 scheduler_
->SetCommandProcessedCallback(
664 base::Bind(&GpuCommandBufferStub::OnCommandProcessed
,
665 base::Unretained(this)));
668 const size_t kSharedStateSize
= sizeof(gpu::CommandBufferSharedState
);
669 if (!shared_state_shm
->Map(kSharedStateSize
)) {
670 DLOG(ERROR
) << "Failed to map shared state buffer.";
671 OnInitializeFailed(reply_message
);
674 command_buffer_
->SetSharedStateBuffer(gpu::MakeBackingFromSharedMemory(
675 shared_state_shm
.Pass(), kSharedStateSize
));
677 gpu::Capabilities capabilities
= decoder_
->GetCapabilities();
678 capabilities
.future_sync_points
= channel_
->allow_future_sync_points();
680 GpuCommandBufferMsg_Initialize::WriteReplyParams(
681 reply_message
, true, capabilities
);
684 if (handle_
.is_null() && !active_url_
.is_empty()) {
685 GpuChannelManager
* gpu_channel_manager
= channel_
->gpu_channel_manager();
686 gpu_channel_manager
->Send(new GpuHostMsg_DidCreateOffscreenContext(
693 void GpuCommandBufferStub::OnCreateStreamTexture(
694 uint32 texture_id
, int32 stream_id
, bool* succeeded
) {
695 #if defined(OS_ANDROID)
696 *succeeded
= StreamTexture::Create(this, texture_id
, stream_id
);
702 void GpuCommandBufferStub::SetLatencyInfoCallback(
703 const LatencyInfoCallback
& callback
) {
704 latency_info_callback_
= callback
;
707 int32
GpuCommandBufferStub::GetRequestedAttribute(int attr
) const {
708 // The command buffer is pairs of enum, value
709 // search for the requested attribute, return the value.
710 for (std::vector
<int32
>::const_iterator it
= requested_attribs_
.begin();
711 it
!= requested_attribs_
.end(); ++it
) {
719 void GpuCommandBufferStub::OnSetGetBuffer(int32 shm_id
,
720 IPC::Message
* reply_message
) {
721 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnSetGetBuffer");
723 command_buffer_
->SetGetBuffer(shm_id
);
727 void GpuCommandBufferStub::OnProduceFrontBuffer(const gpu::Mailbox
& mailbox
) {
728 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnProduceFrontBuffer");
730 LOG(ERROR
) << "Can't produce front buffer before initialization.";
734 decoder_
->ProduceFrontBuffer(mailbox
);
737 void GpuCommandBufferStub::OnParseError() {
738 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnParseError");
739 DCHECK(command_buffer_
.get());
740 gpu::CommandBuffer::State state
= command_buffer_
->GetLastState();
741 IPC::Message
* msg
= new GpuCommandBufferMsg_Destroyed(
742 route_id_
, state
.context_lost_reason
, state
.error
);
743 msg
->set_unblock(true);
746 // Tell the browser about this context loss as well, so it can
747 // determine whether client APIs like WebGL need to be immediately
748 // blocked from automatically running.
749 GpuChannelManager
* gpu_channel_manager
= channel_
->gpu_channel_manager();
750 gpu_channel_manager
->Send(new GpuHostMsg_DidLoseContext(
751 handle_
.is_null(), state
.context_lost_reason
, active_url_
));
756 void GpuCommandBufferStub::OnSchedulingChanged(bool scheduled
) {
757 TRACE_EVENT1("gpu", "GpuCommandBufferStub::OnSchedulingChanged", "scheduled",
759 channel_
->OnStubSchedulingChanged(this, scheduled
);
762 void GpuCommandBufferStub::OnWaitForTokenInRange(int32 start
,
764 IPC::Message
* reply_message
) {
765 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnWaitForTokenInRange");
766 DCHECK(command_buffer_
.get());
769 LOG(ERROR
) << "Got WaitForToken command while currently waiting for token.";
771 make_scoped_ptr(new WaitForCommandState(start
, end
, reply_message
));
772 CheckCompleteWaits();
775 void GpuCommandBufferStub::OnWaitForGetOffsetInRange(
778 IPC::Message
* reply_message
) {
779 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnWaitForGetOffsetInRange");
780 DCHECK(command_buffer_
.get());
782 if (wait_for_get_offset_
) {
784 << "Got WaitForGetOffset command while currently waiting for offset.";
786 wait_for_get_offset_
=
787 make_scoped_ptr(new WaitForCommandState(start
, end
, reply_message
));
788 CheckCompleteWaits();
791 void GpuCommandBufferStub::CheckCompleteWaits() {
792 if (wait_for_token_
|| wait_for_get_offset_
) {
793 gpu::CommandBuffer::State state
= command_buffer_
->GetLastState();
794 if (wait_for_token_
&&
795 (gpu::CommandBuffer::InRange(
796 wait_for_token_
->start
, wait_for_token_
->end
, state
.token
) ||
797 state
.error
!= gpu::error::kNoError
)) {
799 GpuCommandBufferMsg_WaitForTokenInRange::WriteReplyParams(
800 wait_for_token_
->reply
.get(), state
);
801 Send(wait_for_token_
->reply
.release());
802 wait_for_token_
.reset();
804 if (wait_for_get_offset_
&&
805 (gpu::CommandBuffer::InRange(wait_for_get_offset_
->start
,
806 wait_for_get_offset_
->end
,
808 state
.error
!= gpu::error::kNoError
)) {
810 GpuCommandBufferMsg_WaitForGetOffsetInRange::WriteReplyParams(
811 wait_for_get_offset_
->reply
.get(), state
);
812 Send(wait_for_get_offset_
->reply
.release());
813 wait_for_get_offset_
.reset();
818 void GpuCommandBufferStub::OnAsyncFlush(
821 const std::vector
<ui::LatencyInfo
>& latency_info
) {
823 "gpu", "GpuCommandBufferStub::OnAsyncFlush", "put_offset", put_offset
);
824 DCHECK(command_buffer_
);
826 // We received this message out-of-order. This should not happen but is here
827 // to catch regressions. Ignore the message.
828 DVLOG_IF(0, flush_count
- last_flush_count_
>= 0x8000000U
)
829 << "Received a Flush message out-of-order";
831 if (flush_count
> last_flush_count_
&&
832 ui::LatencyInfo::Verify(latency_info
,
833 "GpuCommandBufferStub::OnAsyncFlush") &&
834 !latency_info_callback_
.is_null()) {
835 latency_info_callback_
.Run(latency_info
);
838 last_flush_count_
= flush_count
;
839 gpu::CommandBuffer::State pre_state
= command_buffer_
->GetLastState();
840 command_buffer_
->Flush(put_offset
);
841 gpu::CommandBuffer::State post_state
= command_buffer_
->GetLastState();
843 if (pre_state
.get_offset
!= post_state
.get_offset
)
847 void GpuCommandBufferStub::OnRegisterTransferBuffer(
849 base::SharedMemoryHandle transfer_buffer
,
851 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnRegisterTransferBuffer");
853 // Take ownership of the memory and map it into this process.
854 // This validates the size.
855 scoped_ptr
<base::SharedMemory
> shared_memory(
856 new base::SharedMemory(transfer_buffer
, false));
857 if (!shared_memory
->Map(size
)) {
858 DVLOG(0) << "Failed to map shared memory.";
862 if (command_buffer_
) {
863 command_buffer_
->RegisterTransferBuffer(
864 id
, gpu::MakeBackingFromSharedMemory(shared_memory
.Pass(), size
));
868 void GpuCommandBufferStub::OnDestroyTransferBuffer(int32 id
) {
869 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnDestroyTransferBuffer");
872 command_buffer_
->DestroyTransferBuffer(id
);
875 void GpuCommandBufferStub::OnCommandProcessed() {
877 watchdog_
->CheckArmed();
880 void GpuCommandBufferStub::ReportState() { command_buffer_
->UpdateState(); }
882 void GpuCommandBufferStub::PutChanged() {
883 FastSetActiveURL(active_url_
, active_url_hash_
);
884 scheduler_
->PutChanged();
887 void GpuCommandBufferStub::OnCreateVideoDecoder(
888 media::VideoCodecProfile profile
,
889 int32 decoder_route_id
,
890 IPC::Message
* reply_message
) {
891 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnCreateVideoDecoder");
892 GpuVideoDecodeAccelerator
* decoder
= new GpuVideoDecodeAccelerator(
893 decoder_route_id
, this, channel_
->io_task_runner());
894 decoder
->Initialize(profile
, reply_message
);
895 // decoder is registered as a DestructionObserver of this stub and will
896 // self-delete during destruction of this stub.
899 void GpuCommandBufferStub::OnCreateVideoEncoder(
900 media::VideoPixelFormat input_format
,
901 const gfx::Size
& input_visible_size
,
902 media::VideoCodecProfile output_profile
,
903 uint32 initial_bitrate
,
904 int32 encoder_route_id
,
905 IPC::Message
* reply_message
) {
906 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnCreateVideoEncoder");
907 GpuVideoEncodeAccelerator
* encoder
=
908 new GpuVideoEncodeAccelerator(encoder_route_id
, this);
909 encoder
->Initialize(input_format
,
914 // encoder is registered as a DestructionObserver of this stub and will
915 // self-delete during destruction of this stub.
918 void GpuCommandBufferStub::OnSetSurfaceVisible(bool visible
) {
919 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnSetSurfaceVisible");
920 if (memory_manager_client_state_
)
921 memory_manager_client_state_
->SetVisible(visible
);
924 void GpuCommandBufferStub::AddSyncPoint(uint32 sync_point
, bool retire
) {
925 sync_points_
.push_back(sync_point
);
927 OnRetireSyncPoint(sync_point
);
930 void GpuCommandBufferStub::OnRetireSyncPoint(uint32 sync_point
) {
931 DCHECK(!sync_points_
.empty() && sync_points_
.front() == sync_point
);
932 sync_points_
.pop_front();
934 gpu::gles2::MailboxManager
* mailbox_manager
=
935 context_group_
->mailbox_manager();
936 if (mailbox_manager
->UsesSync() && MakeCurrent())
937 mailbox_manager
->PushTextureUpdates(sync_point
);
939 GpuChannelManager
* manager
= channel_
->gpu_channel_manager();
940 manager
->sync_point_manager()->RetireSyncPoint(sync_point
);
943 bool GpuCommandBufferStub::OnWaitSyncPoint(uint32 sync_point
) {
944 DCHECK(!waiting_for_sync_point_
);
945 DCHECK(scheduler_
->scheduled());
948 GpuChannelManager
* manager
= channel_
->gpu_channel_manager();
949 if (manager
->sync_point_manager()->IsSyncPointRetired(sync_point
)) {
950 PullTextureUpdates(sync_point
);
954 TRACE_EVENT_ASYNC_BEGIN1("gpu", "WaitSyncPoint", this, "GpuCommandBufferStub",
957 scheduler_
->SetScheduled(false);
958 waiting_for_sync_point_
= true;
959 manager
->sync_point_manager()->AddSyncPointCallback(
961 base::Bind(&RunOnThread
, task_runner_
,
962 base::Bind(&GpuCommandBufferStub::OnWaitSyncPointCompleted
,
963 this->AsWeakPtr(), sync_point
)));
964 return !waiting_for_sync_point_
;
967 void GpuCommandBufferStub::OnWaitSyncPointCompleted(uint32 sync_point
) {
968 DCHECK(waiting_for_sync_point_
);
969 DCHECK(!scheduler_
->scheduled());
970 TRACE_EVENT_ASYNC_END1("gpu", "WaitSyncPoint", this, "GpuCommandBufferStub",
972 PullTextureUpdates(sync_point
);
973 waiting_for_sync_point_
= false;
974 scheduler_
->SetScheduled(true);
977 void GpuCommandBufferStub::PullTextureUpdates(uint32 sync_point
) {
978 gpu::gles2::MailboxManager
* mailbox_manager
=
979 context_group_
->mailbox_manager();
980 if (mailbox_manager
->UsesSync() && MakeCurrent())
981 mailbox_manager
->PullTextureUpdates(sync_point
);
984 void GpuCommandBufferStub::OnSignalSyncPoint(uint32 sync_point
, uint32 id
) {
985 GpuChannelManager
* manager
= channel_
->gpu_channel_manager();
986 manager
->sync_point_manager()->AddSyncPointCallback(
988 base::Bind(&RunOnThread
, task_runner_
,
989 base::Bind(&GpuCommandBufferStub::OnSignalSyncPointAck
,
990 this->AsWeakPtr(), id
)));
993 void GpuCommandBufferStub::OnSignalSyncPointAck(uint32 id
) {
994 Send(new GpuCommandBufferMsg_SignalSyncPointAck(route_id_
, id
));
997 void GpuCommandBufferStub::OnSignalQuery(uint32 query_id
, uint32 id
) {
999 gpu::gles2::QueryManager
* query_manager
= decoder_
->GetQueryManager();
1000 if (query_manager
) {
1001 gpu::gles2::QueryManager::Query
* query
=
1002 query_manager
->GetQuery(query_id
);
1005 base::Bind(&GpuCommandBufferStub::OnSignalSyncPointAck
,
1012 // Something went wrong, run callback immediately.
1013 OnSignalSyncPointAck(id
);
1017 void GpuCommandBufferStub::OnSetClientHasMemoryAllocationChangedCallback(
1018 bool has_callback
) {
1021 "GpuCommandBufferStub::OnSetClientHasMemoryAllocationChangedCallback");
1023 if (!memory_manager_client_state_
) {
1024 memory_manager_client_state_
.reset(GetMemoryManager()->CreateClientState(
1025 this, surface_id_
!= 0, true));
1028 memory_manager_client_state_
.reset();
1032 void GpuCommandBufferStub::OnCreateImage(int32 id
,
1033 gfx::GpuMemoryBufferHandle handle
,
1035 gfx::BufferFormat format
,
1036 uint32 internalformat
) {
1037 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnCreateImage");
1042 gpu::gles2::ImageManager
* image_manager
= decoder_
->GetImageManager();
1043 DCHECK(image_manager
);
1044 if (image_manager
->LookupImage(id
)) {
1045 LOG(ERROR
) << "Image already exists with same ID.";
1049 if (!gpu::ImageFactory::IsGpuMemoryBufferFormatSupported(
1050 format
, decoder_
->GetCapabilities())) {
1051 LOG(ERROR
) << "Format is not supported.";
1055 if (!gpu::ImageFactory::IsImageSizeValidForGpuMemoryBufferFormat(size
,
1057 LOG(ERROR
) << "Invalid image size for format.";
1061 if (!gpu::ImageFactory::IsImageFormatCompatibleWithGpuMemoryBufferFormat(
1062 internalformat
, format
)) {
1063 LOG(ERROR
) << "Incompatible image format.";
1067 scoped_refptr
<gfx::GLImage
> image
= channel()->CreateImageForGpuMemoryBuffer(
1068 handle
, size
, format
, internalformat
);
1072 image_manager
->AddImage(image
.get(), id
);
1075 void GpuCommandBufferStub::OnDestroyImage(int32 id
) {
1076 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnDestroyImage");
1081 gpu::gles2::ImageManager
* image_manager
= decoder_
->GetImageManager();
1082 DCHECK(image_manager
);
1083 if (!image_manager
->LookupImage(id
)) {
1084 LOG(ERROR
) << "Image with ID doesn't exist.";
1088 image_manager
->RemoveImage(id
);
1091 void GpuCommandBufferStub::SendConsoleMessage(
1093 const std::string
& message
) {
1094 GPUCommandBufferConsoleMessage console_message
;
1095 console_message
.id
= id
;
1096 console_message
.message
= message
;
1097 IPC::Message
* msg
= new GpuCommandBufferMsg_ConsoleMsg(
1098 route_id_
, console_message
);
1099 msg
->set_unblock(true);
1103 void GpuCommandBufferStub::SendCachedShader(
1104 const std::string
& key
, const std::string
& shader
) {
1105 channel_
->CacheShader(key
, shader
);
1108 void GpuCommandBufferStub::AddDestructionObserver(
1109 DestructionObserver
* observer
) {
1110 destruction_observers_
.AddObserver(observer
);
1113 void GpuCommandBufferStub::RemoveDestructionObserver(
1114 DestructionObserver
* observer
) {
1115 destruction_observers_
.RemoveObserver(observer
);
1118 void GpuCommandBufferStub::SetPreemptByFlag(
1119 scoped_refptr
<gpu::PreemptionFlag
> flag
) {
1120 preemption_flag_
= flag
;
1122 scheduler_
->SetPreemptByFlag(preemption_flag_
);
1125 bool GpuCommandBufferStub::GetTotalGpuMemory(uint64
* bytes
) {
1126 *bytes
= total_gpu_memory_
;
1127 return !!total_gpu_memory_
;
1130 gfx::Size
GpuCommandBufferStub::GetSurfaceSize() const {
1131 if (!surface_
.get())
1133 return surface_
->GetSize();
1136 const gpu::gles2::FeatureInfo
* GpuCommandBufferStub::GetFeatureInfo() const {
1137 return context_group_
->feature_info();
1140 gpu::gles2::MemoryTracker
* GpuCommandBufferStub::GetMemoryTracker() const {
1141 return context_group_
->memory_tracker();
1144 void GpuCommandBufferStub::SetMemoryAllocation(
1145 const gpu::MemoryAllocation
& allocation
) {
1146 if (!last_memory_allocation_valid_
||
1147 !allocation
.Equals(last_memory_allocation_
)) {
1148 Send(new GpuCommandBufferMsg_SetMemoryAllocation(
1149 route_id_
, allocation
));
1152 last_memory_allocation_valid_
= true;
1153 last_memory_allocation_
= allocation
;
1156 void GpuCommandBufferStub::SuggestHaveFrontBuffer(
1157 bool suggest_have_frontbuffer
) {
1158 // This can be called outside of OnMessageReceived, so the context needs
1159 // to be made current before calling methods on the surface.
1160 if (surface_
.get() && MakeCurrent())
1161 surface_
->SetFrontbufferAllocation(suggest_have_frontbuffer
);
1164 bool GpuCommandBufferStub::CheckContextLost() {
1165 DCHECK(command_buffer_
);
1166 gpu::CommandBuffer::State state
= command_buffer_
->GetLastState();
1167 bool was_lost
= state
.error
== gpu::error::kLostContext
;
1170 bool was_lost_by_robustness
=
1171 decoder_
&& decoder_
->WasContextLostByRobustnessExtension();
1173 // Work around issues with recovery by allowing a new GPU process to launch.
1174 if ((was_lost_by_robustness
||
1175 context_group_
->feature_info()->workarounds().exit_on_context_lost
) &&
1176 !base::CommandLine::ForCurrentProcess()->HasSwitch(
1177 switches::kSingleProcess
) &&
1178 !base::CommandLine::ForCurrentProcess()->HasSwitch(
1179 switches::kInProcessGPU
)) {
1180 LOG(ERROR
) << "Exiting GPU process because some drivers cannot recover"
1181 << " from problems.";
1183 base::win::SetShouldCrashOnProcessDetach(false);
1188 // Lose all other contexts if the reset was triggered by the robustness
1189 // extension instead of being synthetic.
1190 if (was_lost_by_robustness
&&
1191 (gfx::GLContext::LosesAllContextsOnContextLost() ||
1192 use_virtualized_gl_context_
)) {
1193 channel_
->LoseAllContexts();
1197 CheckCompleteWaits();
1201 void GpuCommandBufferStub::MarkContextLost() {
1202 if (!command_buffer_
||
1203 command_buffer_
->GetLastState().error
== gpu::error::kLostContext
)
1206 command_buffer_
->SetContextLostReason(gpu::error::kUnknown
);
1208 decoder_
->MarkContextLost(gpu::error::kUnknown
);
1209 command_buffer_
->SetParseError(gpu::error::kLostContext
);
1212 void GpuCommandBufferStub::SendSwapBuffersCompleted(
1213 const std::vector
<ui::LatencyInfo
>& latency_info
,
1214 gfx::SwapResult result
) {
1215 Send(new GpuCommandBufferMsg_SwapBuffersCompleted(route_id_
, latency_info
,
1219 void GpuCommandBufferStub::SendUpdateVSyncParameters(base::TimeTicks timebase
,
1220 base::TimeDelta interval
) {
1221 Send(new GpuCommandBufferMsg_UpdateVSyncParameters(route_id_
, timebase
,
1225 } // namespace content