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"
8 #include "base/debug/trace_event.h"
10 #include "base/memory/shared_memory.h"
11 #include "base/time/time.h"
12 #include "build/build_config.h"
13 #include "content/common/gpu/gpu_channel.h"
14 #include "content/common/gpu/gpu_channel_manager.h"
15 #include "content/common/gpu/gpu_command_buffer_stub.h"
16 #include "content/common/gpu/gpu_memory_manager.h"
17 #include "content/common/gpu/gpu_memory_tracking.h"
18 #include "content/common/gpu/gpu_messages.h"
19 #include "content/common/gpu/gpu_watchdog.h"
20 #include "content/common/gpu/image_transport_surface.h"
21 #include "content/common/gpu/media/gl_surface_capturer.h"
22 #include "content/common/gpu/media/gpu_video_decode_accelerator.h"
23 #include "content/common/gpu/sync_point_manager.h"
24 #include "content/public/common/content_client.h"
25 #include "gpu/command_buffer/common/constants.h"
26 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
27 #include "gpu/command_buffer/common/mailbox.h"
28 #include "gpu/command_buffer/service/gl_context_virtual.h"
29 #include "gpu/command_buffer/service/gl_state_restorer_impl.h"
30 #include "gpu/command_buffer/service/logger.h"
31 #include "gpu/command_buffer/service/memory_tracking.h"
32 #include "gpu/command_buffer/service/query_manager.h"
33 #include "ui/gl/gl_bindings.h"
34 #include "ui/gl/gl_switches.h"
37 #include "content/public/common/sandbox_init.h"
40 #if defined(OS_ANDROID)
41 #include "content/common/gpu/stream_texture_manager_android.h"
47 // The GpuCommandBufferMemoryTracker class provides a bridge between the
48 // ContextGroup's memory type managers and the GpuMemoryManager class.
49 class GpuCommandBufferMemoryTracker
: public gpu::gles2::MemoryTracker
{
51 explicit GpuCommandBufferMemoryTracker(GpuChannel
* channel
) :
52 tracking_group_(channel
->gpu_channel_manager()->gpu_memory_manager()->
53 CreateTrackingGroup(channel
->renderer_pid(), this)) {
56 virtual void TrackMemoryAllocatedChange(
59 gpu::gles2::MemoryTracker::Pool pool
) OVERRIDE
{
60 tracking_group_
->TrackMemoryAllocatedChange(
61 old_size
, new_size
, pool
);
64 virtual bool EnsureGPUMemoryAvailable(size_t size_needed
) OVERRIDE
{
65 return tracking_group_
->EnsureGPUMemoryAvailable(size_needed
);
69 virtual ~GpuCommandBufferMemoryTracker() {
71 scoped_ptr
<GpuMemoryTrackingGroup
> tracking_group_
;
73 DISALLOW_COPY_AND_ASSIGN(GpuCommandBufferMemoryTracker
);
76 // FastSetActiveURL will shortcut the expensive call to SetActiveURL when the
78 void FastSetActiveURL(const GURL
& url
, size_t url_hash
) {
79 // Leave the previously set URL in the empty case -- empty URLs are given by
80 // WebKitPlatformSupportImpl::createOffscreenGraphicsContext3D. Hopefully the
81 // onscreen context URL was set previously and will show up even when a crash
82 // occurs during offscreen command processing.
85 static size_t g_last_url_hash
= 0;
86 if (url_hash
!= g_last_url_hash
) {
87 g_last_url_hash
= url_hash
;
88 GetContentClient()->SetActiveURL(url
);
92 // The first time polling a fence, delay some extra time to allow other
93 // stubs to process some work, or else the timing of the fences could
94 // allow a pattern of alternating fast and slow frames to occur.
95 const int64 kHandleMoreWorkPeriodMs
= 2;
96 const int64 kHandleMoreWorkPeriodBusyMs
= 1;
98 // Prevents idle work from being starved.
99 const int64 kMaxTimeSinceIdleMs
= 10;
103 GpuCommandBufferStub::GpuCommandBufferStub(
105 GpuCommandBufferStub
* share_group
,
106 const gfx::GLSurfaceHandle
& handle
,
107 gpu::gles2::MailboxManager
* mailbox_manager
,
108 gpu::gles2::ImageManager
* image_manager
,
109 const gfx::Size
& size
,
110 const gpu::gles2::DisallowedFeatures
& disallowed_features
,
111 const std::vector
<int32
>& attribs
,
112 gfx::GpuPreference gpu_preference
,
113 bool use_virtualized_gl_context
,
116 GpuWatchdog
* watchdog
,
118 const GURL
& active_url
)
122 disallowed_features_(disallowed_features
),
123 requested_attribs_(attribs
),
124 gpu_preference_(gpu_preference
),
125 use_virtualized_gl_context_(use_virtualized_gl_context
),
127 surface_id_(surface_id
),
129 last_flush_count_(0),
130 last_memory_allocation_valid_(false),
132 sync_point_wait_count_(0),
133 delayed_work_scheduled_(false),
134 previous_messages_processed_(0),
135 active_url_(active_url
),
136 total_gpu_memory_(0) {
137 active_url_hash_
= base::Hash(active_url
.possibly_invalid_spec());
138 FastSetActiveURL(active_url_
, active_url_hash_
);
140 context_group_
= share_group
->context_group_
;
142 gpu::StreamTextureManager
* stream_texture_manager
= NULL
;
143 #if defined(OS_ANDROID)
144 stream_texture_manager
= channel_
->stream_texture_manager();
146 context_group_
= new gpu::gles2::ContextGroup(
149 new GpuCommandBufferMemoryTracker(channel
),
150 stream_texture_manager
,
154 use_virtualized_gl_context_
|=
155 context_group_
->feature_info()->workarounds().use_virtualized_gl_contexts
;
158 GpuCommandBufferStub::~GpuCommandBufferStub() {
161 GpuChannelManager
* gpu_channel_manager
= channel_
->gpu_channel_manager();
162 gpu_channel_manager
->Send(new GpuHostMsg_DestroyCommandBuffer(surface_id()));
165 GpuMemoryManager
* GpuCommandBufferStub::GetMemoryManager() {
166 return channel()->gpu_channel_manager()->gpu_memory_manager();
169 bool GpuCommandBufferStub::OnMessageReceived(const IPC::Message
& message
) {
170 FastSetActiveURL(active_url_
, active_url_hash_
);
172 // Ensure the appropriate GL context is current before handling any IPC
173 // messages directed at the command buffer. This ensures that the message
174 // handler can assume that the context is current (not necessary for
175 // Echo, RetireSyncPoint, or WaitSyncPoint).
176 if (decoder_
.get() &&
177 message
.type() != GpuCommandBufferMsg_Echo::ID
&&
178 message
.type() != GpuCommandBufferMsg_GetStateFast::ID
&&
179 message
.type() != GpuCommandBufferMsg_RetireSyncPoint::ID
&&
180 message
.type() != GpuCommandBufferMsg_SetLatencyInfo::ID
) {
185 // Always use IPC_MESSAGE_HANDLER_DELAY_REPLY for synchronous message handlers
186 // here. This is so the reply can be delayed if the scheduler is unscheduled.
188 IPC_BEGIN_MESSAGE_MAP(GpuCommandBufferStub
, message
)
189 IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuCommandBufferMsg_Initialize
,
191 IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuCommandBufferMsg_SetGetBuffer
,
193 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_ProduceFrontBuffer
,
194 OnProduceFrontBuffer
);
195 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_Echo
, OnEcho
);
196 IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuCommandBufferMsg_GetState
, OnGetState
);
197 IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuCommandBufferMsg_GetStateFast
,
199 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_AsyncFlush
, OnAsyncFlush
);
200 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_SetLatencyInfo
, OnSetLatencyInfo
);
201 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_Rescheduled
, OnRescheduled
);
202 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_RegisterTransferBuffer
,
203 OnRegisterTransferBuffer
);
204 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_DestroyTransferBuffer
,
205 OnDestroyTransferBuffer
);
206 IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuCommandBufferMsg_GetTransferBuffer
,
207 OnGetTransferBuffer
);
208 IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuCommandBufferMsg_CreateVideoDecoder
,
209 OnCreateVideoDecoder
)
210 IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuCommandBufferMsg_CreateSurfaceCapturer
,
211 OnCreateSurfaceCapturer
)
212 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_SetSurfaceVisible
,
214 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_DiscardBackbuffer
,
216 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_EnsureBackbuffer
,
218 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_RetireSyncPoint
,
220 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_SignalSyncPoint
,
222 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_SignalQuery
,
224 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_SendClientManagedMemoryStats
,
225 OnReceivedClientManagedMemoryStats
)
227 GpuCommandBufferMsg_SetClientHasMemoryAllocationChangedCallback
,
228 OnSetClientHasMemoryAllocationChangedCallback
)
229 IPC_MESSAGE_UNHANDLED(handled
= false)
230 IPC_END_MESSAGE_MAP()
232 // Ensure that any delayed work that was created will be handled.
233 ScheduleDelayedWork(kHandleMoreWorkPeriodMs
);
239 bool GpuCommandBufferStub::Send(IPC::Message
* message
) {
240 return channel_
->Send(message
);
243 bool GpuCommandBufferStub::IsScheduled() {
244 return (!scheduler_
.get() || scheduler_
->IsScheduled());
247 bool GpuCommandBufferStub::HasMoreWork() {
248 return scheduler_
.get() && scheduler_
->HasMoreWork();
251 void GpuCommandBufferStub::PollWork() {
252 TRACE_EVENT0("gpu", "GpuCommandBufferStub::PollWork");
253 delayed_work_scheduled_
= false;
254 FastSetActiveURL(active_url_
, active_url_hash_
);
255 if (decoder_
.get() && !MakeCurrent())
259 bool fences_complete
= scheduler_
->PollUnscheduleFences();
260 // Perform idle work if all fences are complete.
261 if (fences_complete
) {
262 uint64 current_messages_processed
=
263 channel()->gpu_channel_manager()->MessagesProcessed();
264 // We're idle when no messages were processed or scheduled.
266 (previous_messages_processed_
== current_messages_processed
) &&
267 !channel()->gpu_channel_manager()->HandleMessagesScheduled();
268 if (!is_idle
&& !last_idle_time_
.is_null()) {
269 base::TimeDelta time_since_idle
= base::TimeTicks::Now() -
271 base::TimeDelta max_time_since_idle
=
272 base::TimeDelta::FromMilliseconds(kMaxTimeSinceIdleMs
);
274 // Force idle when it's been too long since last time we were idle.
275 if (time_since_idle
> max_time_since_idle
)
280 last_idle_time_
= base::TimeTicks::Now();
281 scheduler_
->PerformIdleWork();
285 ScheduleDelayedWork(kHandleMoreWorkPeriodBusyMs
);
288 bool GpuCommandBufferStub::HasUnprocessedCommands() {
289 if (command_buffer_
) {
290 gpu::CommandBuffer::State state
= command_buffer_
->GetLastState();
291 return state
.put_offset
!= state
.get_offset
&&
292 !gpu::error::IsError(state
.error
);
297 void GpuCommandBufferStub::ScheduleDelayedWork(int64 delay
) {
298 if (!HasMoreWork()) {
299 last_idle_time_
= base::TimeTicks();
303 if (delayed_work_scheduled_
)
305 delayed_work_scheduled_
= true;
307 // Idle when no messages are processed between now and when
308 // PollWork is called.
309 previous_messages_processed_
=
310 channel()->gpu_channel_manager()->MessagesProcessed();
311 if (last_idle_time_
.is_null())
312 last_idle_time_
= base::TimeTicks::Now();
314 // IsScheduled() returns true after passing all unschedule fences
315 // and this is when we can start performing idle work. Idle work
316 // is done synchronously so we can set delay to 0 and instead poll
317 // for more work at the rate idle work is performed. This also ensures
318 // that idle work is done as efficiently as possible without any
319 // unnecessary delays.
320 if (scheduler_
.get() &&
321 scheduler_
->IsScheduled() &&
322 scheduler_
->HasMoreIdleWork()) {
326 base::MessageLoop::current()->PostDelayedTask(
328 base::Bind(&GpuCommandBufferStub::PollWork
, AsWeakPtr()),
329 base::TimeDelta::FromMilliseconds(delay
));
332 void GpuCommandBufferStub::OnEcho(const IPC::Message
& message
) {
333 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnEcho");
334 Send(new IPC::Message(message
));
337 bool GpuCommandBufferStub::MakeCurrent() {
338 if (decoder_
->MakeCurrent())
340 DLOG(ERROR
) << "Context lost because MakeCurrent failed.";
341 command_buffer_
->SetContextLostReason(decoder_
->GetContextLostReason());
342 command_buffer_
->SetParseError(gpu::error::kLostContext
);
347 void GpuCommandBufferStub::Destroy() {
348 if (handle_
.is_null() && !active_url_
.is_empty()) {
349 GpuChannelManager
* gpu_channel_manager
= channel_
->gpu_channel_manager();
350 gpu_channel_manager
->Send(new GpuHostMsg_DidDestroyOffscreenContext(
354 memory_manager_client_state_
.reset();
356 while (!sync_points_
.empty())
357 OnRetireSyncPoint(sync_points_
.front());
360 decoder_
->set_engine(NULL
);
362 // The scheduler has raw references to the decoder and the command buffer so
363 // destroy it before those.
366 bool have_context
= false;
367 if (decoder_
&& command_buffer_
&&
368 command_buffer_
->GetState().error
!= gpu::error::kLostContext
)
369 have_context
= decoder_
->MakeCurrent();
370 FOR_EACH_OBSERVER(DestructionObserver
,
371 destruction_observers_
,
372 OnWillDestroyStub());
375 decoder_
->Destroy(have_context
);
379 command_buffer_
.reset();
381 // Remove this after crbug.com/248395 is sorted out.
385 void GpuCommandBufferStub::OnInitializeFailed(IPC::Message
* reply_message
) {
387 GpuCommandBufferMsg_Initialize::WriteReplyParams(reply_message
, false);
391 void GpuCommandBufferStub::OnInitialize(
392 base::SharedMemoryHandle shared_state_handle
,
393 IPC::Message
* reply_message
) {
394 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnInitialize");
395 DCHECK(!command_buffer_
.get());
397 scoped_ptr
<base::SharedMemory
> shared_state_shm(
398 new base::SharedMemory(shared_state_handle
, false));
400 command_buffer_
.reset(new gpu::CommandBufferService(
401 context_group_
->transfer_buffer_manager()));
403 if (!command_buffer_
->Initialize()) {
404 DLOG(ERROR
) << "CommandBufferService failed to initialize.\n";
405 OnInitializeFailed(reply_message
);
409 decoder_
.reset(::gpu::gles2::GLES2Decoder::Create(context_group_
.get()));
411 scheduler_
.reset(new gpu::GpuScheduler(command_buffer_
.get(),
414 if (preemption_flag_
.get())
415 scheduler_
->SetPreemptByFlag(preemption_flag_
);
417 decoder_
->set_engine(scheduler_
.get());
419 if (!handle_
.is_null()) {
420 #if defined(OS_MACOSX) || defined(UI_COMPOSITOR_IMAGE_TRANSPORT)
422 DLOG(ERROR
) << "No software support.\n";
423 OnInitializeFailed(reply_message
);
428 surface_
= ImageTransportSurface::CreateSurface(
429 channel_
->gpu_channel_manager(),
433 GpuChannelManager
* manager
= channel_
->gpu_channel_manager();
434 surface_
= manager
->GetDefaultOffscreenSurface();
437 if (!surface_
.get()) {
438 DLOG(ERROR
) << "Failed to create surface.\n";
439 OnInitializeFailed(reply_message
);
443 scoped_refptr
<gfx::GLContext
> context
;
444 if (use_virtualized_gl_context_
&& channel_
->share_group()) {
445 context
= channel_
->share_group()->GetSharedContext();
446 if (!context
.get()) {
447 context
= gfx::GLContext::CreateGLContext(
448 channel_
->share_group(),
449 channel_
->gpu_channel_manager()->GetDefaultOffscreenSurface(),
451 channel_
->share_group()->SetSharedContext(context
.get());
453 // This should be a non-virtual GL context.
454 DCHECK(context
->GetHandle());
455 context
= new gpu::GLContextVirtual(
456 channel_
->share_group(), context
.get(), decoder_
->AsWeakPtr());
457 if (!context
->Initialize(surface_
.get(), gpu_preference_
)) {
458 // TODO(sievers): The real context created above for the default
459 // offscreen surface might not be compatible with this surface.
460 // Need to adjust at least GLX to be able to create the initial context
461 // with a config that is compatible with onscreen and offscreen surfaces.
464 DLOG(ERROR
) << "Failed to initialize virtual GL context.";
465 OnInitializeFailed(reply_message
);
469 if (!context
.get()) {
470 context
= gfx::GLContext::CreateGLContext(
471 channel_
->share_group(), surface_
.get(), gpu_preference_
);
473 if (!context
.get()) {
474 DLOG(ERROR
) << "Failed to create context.\n";
475 OnInitializeFailed(reply_message
);
479 if (!context
->MakeCurrent(surface_
.get())) {
480 LOG(ERROR
) << "Failed to make context current.";
481 OnInitializeFailed(reply_message
);
485 if (!context
->GetGLStateRestorer()) {
486 context
->SetGLStateRestorer(
487 new gpu::GLStateRestorerImpl(decoder_
->AsWeakPtr()));
490 if (!context
->GetTotalGpuMemory(&total_gpu_memory_
))
491 total_gpu_memory_
= 0;
493 if (!context_group_
->has_program_cache()) {
494 context_group_
->set_program_cache(
495 channel_
->gpu_channel_manager()->program_cache());
498 // Initialize the decoder with either the view or pbuffer GLContext.
499 if (!decoder_
->Initialize(surface_
,
503 disallowed_features_
,
504 requested_attribs_
)) {
505 DLOG(ERROR
) << "Failed to initialize decoder.";
506 OnInitializeFailed(reply_message
);
510 if (CommandLine::ForCurrentProcess()->HasSwitch(
511 switches::kEnableGPUServiceLogging
)) {
512 decoder_
->set_log_commands(true);
515 decoder_
->GetLogger()->SetMsgCallback(
516 base::Bind(&GpuCommandBufferStub::SendConsoleMessage
,
517 base::Unretained(this)));
518 decoder_
->SetShaderCacheCallback(
519 base::Bind(&GpuCommandBufferStub::SendCachedShader
,
520 base::Unretained(this)));
521 decoder_
->SetWaitSyncPointCallback(
522 base::Bind(&GpuCommandBufferStub::OnWaitSyncPoint
,
523 base::Unretained(this)));
525 command_buffer_
->SetPutOffsetChangeCallback(
526 base::Bind(&GpuCommandBufferStub::PutChanged
, base::Unretained(this)));
527 command_buffer_
->SetGetBufferChangeCallback(
528 base::Bind(&gpu::GpuScheduler::SetGetBuffer
,
529 base::Unretained(scheduler_
.get())));
530 command_buffer_
->SetParseErrorCallback(
531 base::Bind(&GpuCommandBufferStub::OnParseError
, base::Unretained(this)));
532 scheduler_
->SetSchedulingChangedCallback(
533 base::Bind(&GpuChannel::StubSchedulingChanged
,
534 base::Unretained(channel_
)));
537 scheduler_
->SetCommandProcessedCallback(
538 base::Bind(&GpuCommandBufferStub::OnCommandProcessed
,
539 base::Unretained(this)));
542 if (!command_buffer_
->SetSharedStateBuffer(shared_state_shm
.Pass())) {
543 DLOG(ERROR
) << "Failed to map shared stae buffer.";
544 OnInitializeFailed(reply_message
);
548 GpuCommandBufferMsg_Initialize::WriteReplyParams(reply_message
, true);
551 if (handle_
.is_null() && !active_url_
.is_empty()) {
552 GpuChannelManager
* gpu_channel_manager
= channel_
->gpu_channel_manager();
553 gpu_channel_manager
->Send(new GpuHostMsg_DidCreateOffscreenContext(
558 void GpuCommandBufferStub::OnSetLatencyInfo(
559 const ui::LatencyInfo
& latency_info
) {
560 if (!latency_info_callback_
.is_null())
561 latency_info_callback_
.Run(latency_info
);
564 void GpuCommandBufferStub::SetLatencyInfoCallback(
565 const LatencyInfoCallback
& callback
) {
566 latency_info_callback_
= callback
;
569 void GpuCommandBufferStub::OnSetGetBuffer(int32 shm_id
,
570 IPC::Message
* reply_message
) {
571 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnSetGetBuffer");
573 command_buffer_
->SetGetBuffer(shm_id
);
577 void GpuCommandBufferStub::OnProduceFrontBuffer(const gpu::Mailbox
& mailbox
) {
578 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnProduceFrontBuffer");
580 LOG(ERROR
) << "Can't produce front buffer before initialization.";
582 if (!decoder_
->ProduceFrontBuffer(mailbox
))
583 LOG(ERROR
) << "Failed to produce front buffer.";
586 void GpuCommandBufferStub::OnGetState(IPC::Message
* reply_message
) {
587 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnGetState");
588 if (command_buffer_
) {
589 gpu::CommandBuffer::State state
= command_buffer_
->GetState();
591 GpuCommandBufferMsg_GetState::WriteReplyParams(reply_message
, state
);
593 DLOG(ERROR
) << "no command_buffer.";
594 reply_message
->set_reply_error();
599 void GpuCommandBufferStub::OnParseError() {
600 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnParseError");
601 DCHECK(command_buffer_
.get());
602 gpu::CommandBuffer::State state
= command_buffer_
->GetState();
603 IPC::Message
* msg
= new GpuCommandBufferMsg_Destroyed(
604 route_id_
, state
.context_lost_reason
);
605 msg
->set_unblock(true);
608 // Tell the browser about this context loss as well, so it can
609 // determine whether client APIs like WebGL need to be immediately
610 // blocked from automatically running.
611 GpuChannelManager
* gpu_channel_manager
= channel_
->gpu_channel_manager();
612 gpu_channel_manager
->Send(new GpuHostMsg_DidLoseContext(
613 handle_
.is_null(), state
.context_lost_reason
, active_url_
));
618 void GpuCommandBufferStub::OnGetStateFast(IPC::Message
* reply_message
) {
619 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnGetStateFast");
620 DCHECK(command_buffer_
.get());
622 gpu::CommandBuffer::State state
= command_buffer_
->GetState();
623 GpuCommandBufferMsg_GetStateFast::WriteReplyParams(reply_message
, state
);
627 void GpuCommandBufferStub::OnAsyncFlush(int32 put_offset
,
628 uint32 flush_count
) {
629 TRACE_EVENT1("gpu", "GpuCommandBufferStub::OnAsyncFlush",
630 "put_offset", put_offset
);
631 DCHECK(command_buffer_
.get());
632 if (flush_count
- last_flush_count_
< 0x8000000U
) {
633 last_flush_count_
= flush_count
;
634 command_buffer_
->Flush(put_offset
);
636 // We received this message out-of-order. This should not happen but is here
637 // to catch regressions. Ignore the message.
638 NOTREACHED() << "Received a Flush message out-of-order";
644 void GpuCommandBufferStub::OnRescheduled() {
645 gpu::CommandBuffer::State pre_state
= command_buffer_
->GetLastState();
646 command_buffer_
->Flush(pre_state
.put_offset
);
647 gpu::CommandBuffer::State post_state
= command_buffer_
->GetLastState();
649 if (pre_state
.get_offset
!= post_state
.get_offset
)
653 void GpuCommandBufferStub::OnRegisterTransferBuffer(
655 base::SharedMemoryHandle transfer_buffer
,
657 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnRegisterTransferBuffer");
658 base::SharedMemory
shared_memory(transfer_buffer
, false);
661 command_buffer_
->RegisterTransferBuffer(id
, &shared_memory
, size
);
664 void GpuCommandBufferStub::OnDestroyTransferBuffer(int32 id
) {
665 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnDestroyTransferBuffer");
668 command_buffer_
->DestroyTransferBuffer(id
);
671 void GpuCommandBufferStub::OnGetTransferBuffer(
673 IPC::Message
* reply_message
) {
674 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnGetTransferBuffer");
675 if (command_buffer_
) {
676 base::SharedMemoryHandle transfer_buffer
= base::SharedMemoryHandle();
679 gpu::Buffer buffer
= command_buffer_
->GetTransferBuffer(id
);
680 if (buffer
.shared_memory
) {
682 transfer_buffer
= NULL
;
683 BrokerDuplicateHandle(buffer
.shared_memory
->handle(),
684 channel_
->renderer_pid(), &transfer_buffer
, FILE_MAP_READ
|
686 DCHECK(transfer_buffer
!= NULL
);
688 buffer
.shared_memory
->ShareToProcess(channel_
->renderer_pid(),
694 GpuCommandBufferMsg_GetTransferBuffer::WriteReplyParams(reply_message
,
698 reply_message
->set_reply_error();
703 void GpuCommandBufferStub::OnCommandProcessed() {
705 watchdog_
->CheckArmed();
708 void GpuCommandBufferStub::ReportState() {
709 if (!CheckContextLost())
710 command_buffer_
->UpdateState();
713 void GpuCommandBufferStub::PutChanged() {
714 FastSetActiveURL(active_url_
, active_url_hash_
);
715 scheduler_
->PutChanged();
718 void GpuCommandBufferStub::OnCreateVideoDecoder(
719 media::VideoCodecProfile profile
,
720 IPC::Message
* reply_message
) {
721 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnCreateVideoDecoder");
722 int decoder_route_id
= channel_
->GenerateRouteID();
723 GpuVideoDecodeAccelerator
* decoder
= new GpuVideoDecodeAccelerator(
724 decoder_route_id
, this, channel_
->io_message_loop());
725 decoder
->Initialize(profile
, reply_message
);
726 // decoder is registered as a DestructionObserver of this stub and will
727 // self-delete during destruction of this stub.
730 void GpuCommandBufferStub::OnCreateSurfaceCapturer(
731 IPC::Message
* reply_message
) {
732 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnCreateSurfaceCapturer");
733 int capturer_route_id
= channel_
->GenerateRouteID();
734 new GLSurfaceCapturer(capturer_route_id
, this);
735 // The capturer is registered as a DestructionObserver of this stub and will
736 // self-delete during destruction of this stub.
737 GpuCommandBufferMsg_CreateSurfaceCapturer::WriteReplyParams(
738 reply_message
, capturer_route_id
);
742 void GpuCommandBufferStub::OnSetSurfaceVisible(bool visible
) {
743 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnSetSurfaceVisible");
744 if (memory_manager_client_state_
)
745 memory_manager_client_state_
->SetVisible(visible
);
748 void GpuCommandBufferStub::OnDiscardBackbuffer() {
749 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnDiscardBackbuffer");
752 if (surface_
->DeferDraws()) {
753 DCHECK(!IsScheduled());
754 channel_
->RequeueMessage();
756 if (!surface_
->SetBackbufferAllocation(false))
757 channel_
->DestroySoon();
761 void GpuCommandBufferStub::OnEnsureBackbuffer() {
762 TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnEnsureBackbuffer");
765 if (surface_
->DeferDraws()) {
766 DCHECK(!IsScheduled());
767 channel_
->RequeueMessage();
769 if (!surface_
->SetBackbufferAllocation(true))
770 channel_
->DestroySoon();
774 void GpuCommandBufferStub::AddSyncPoint(uint32 sync_point
) {
775 sync_points_
.push_back(sync_point
);
778 void GpuCommandBufferStub::OnRetireSyncPoint(uint32 sync_point
) {
779 DCHECK(!sync_points_
.empty() && sync_points_
.front() == sync_point
);
780 sync_points_
.pop_front();
781 GpuChannelManager
* manager
= channel_
->gpu_channel_manager();
782 manager
->sync_point_manager()->RetireSyncPoint(sync_point
);
785 bool GpuCommandBufferStub::OnWaitSyncPoint(uint32 sync_point
) {
786 if (sync_point_wait_count_
== 0) {
787 TRACE_EVENT_ASYNC_BEGIN1("gpu", "WaitSyncPoint", this,
788 "GpuCommandBufferStub", this);
790 scheduler_
->SetScheduled(false);
791 ++sync_point_wait_count_
;
792 GpuChannelManager
* manager
= channel_
->gpu_channel_manager();
793 manager
->sync_point_manager()->AddSyncPointCallback(
795 base::Bind(&GpuCommandBufferStub::OnSyncPointRetired
,
797 return scheduler_
->IsScheduled();
800 void GpuCommandBufferStub::OnSyncPointRetired() {
801 --sync_point_wait_count_
;
802 if (sync_point_wait_count_
== 0) {
803 TRACE_EVENT_ASYNC_END1("gpu", "WaitSyncPoint", this,
804 "GpuCommandBufferStub", this);
806 scheduler_
->SetScheduled(true);
809 void GpuCommandBufferStub::OnSignalSyncPoint(uint32 sync_point
, uint32 id
) {
810 GpuChannelManager
* manager
= channel_
->gpu_channel_manager();
811 manager
->sync_point_manager()->AddSyncPointCallback(
813 base::Bind(&GpuCommandBufferStub::OnSignalSyncPointAck
,
818 void GpuCommandBufferStub::OnSignalSyncPointAck(uint32 id
) {
819 Send(new GpuCommandBufferMsg_SignalSyncPointAck(route_id_
, id
));
822 void GpuCommandBufferStub::OnSignalQuery(uint32 query_id
, uint32 id
) {
824 gpu::gles2::QueryManager
* query_manager
= decoder_
->GetQueryManager();
826 gpu::gles2::QueryManager::Query
* query
=
827 query_manager
->GetQuery(query_id
);
830 base::Bind(&GpuCommandBufferStub::OnSignalSyncPointAck
,
837 // Something went wrong, run callback immediately.
838 OnSignalSyncPointAck(id
);
842 void GpuCommandBufferStub::OnReceivedClientManagedMemoryStats(
843 const GpuManagedMemoryStats
& stats
) {
846 "GpuCommandBufferStub::OnReceivedClientManagedMemoryStats");
847 if (memory_manager_client_state_
)
848 memory_manager_client_state_
->SetManagedMemoryStats(stats
);
851 void GpuCommandBufferStub::OnSetClientHasMemoryAllocationChangedCallback(
855 "GpuCommandBufferStub::OnSetClientHasMemoryAllocationChangedCallback");
857 if (!memory_manager_client_state_
) {
858 memory_manager_client_state_
.reset(GetMemoryManager()->CreateClientState(
859 this, surface_id_
!= 0, true));
862 memory_manager_client_state_
.reset();
866 void GpuCommandBufferStub::SendConsoleMessage(
868 const std::string
& message
) {
869 GPUCommandBufferConsoleMessage console_message
;
870 console_message
.id
= id
;
871 console_message
.message
= message
;
872 IPC::Message
* msg
= new GpuCommandBufferMsg_ConsoleMsg(
873 route_id_
, console_message
);
874 msg
->set_unblock(true);
878 void GpuCommandBufferStub::SendCachedShader(
879 const std::string
& key
, const std::string
& shader
) {
880 channel_
->CacheShader(key
, shader
);
883 void GpuCommandBufferStub::AddDestructionObserver(
884 DestructionObserver
* observer
) {
885 destruction_observers_
.AddObserver(observer
);
888 void GpuCommandBufferStub::RemoveDestructionObserver(
889 DestructionObserver
* observer
) {
890 destruction_observers_
.RemoveObserver(observer
);
893 void GpuCommandBufferStub::SetPreemptByFlag(
894 scoped_refptr
<gpu::PreemptionFlag
> flag
) {
895 preemption_flag_
= flag
;
897 scheduler_
->SetPreemptByFlag(preemption_flag_
);
900 bool GpuCommandBufferStub::GetTotalGpuMemory(uint64
* bytes
) {
901 *bytes
= total_gpu_memory_
;
902 return !!total_gpu_memory_
;
905 gfx::Size
GpuCommandBufferStub::GetSurfaceSize() const {
908 return surface_
->GetSize();
911 gpu::gles2::MemoryTracker
* GpuCommandBufferStub::GetMemoryTracker() const {
912 return context_group_
->memory_tracker();
915 void GpuCommandBufferStub::SetMemoryAllocation(
916 const GpuMemoryAllocation
& allocation
) {
917 if (!last_memory_allocation_valid_
||
918 !allocation
.renderer_allocation
.Equals(
919 last_memory_allocation_
.renderer_allocation
)) {
920 Send(new GpuCommandBufferMsg_SetMemoryAllocation(
921 route_id_
, allocation
.renderer_allocation
));
924 if (!last_memory_allocation_valid_
||
925 !allocation
.browser_allocation
.Equals(
926 last_memory_allocation_
.browser_allocation
)) {
927 // This can be called outside of OnMessageReceived, so the context needs
928 // to be made current before calling methods on the surface.
929 if (surface_
.get() && MakeCurrent())
930 surface_
->SetFrontbufferAllocation(
931 allocation
.browser_allocation
.suggest_have_frontbuffer
);
934 last_memory_allocation_valid_
= true;
935 last_memory_allocation_
= allocation
;
938 bool GpuCommandBufferStub::CheckContextLost() {
939 DCHECK(command_buffer_
);
940 gpu::CommandBuffer::State state
= command_buffer_
->GetState();
941 bool was_lost
= state
.error
== gpu::error::kLostContext
;
942 // Lose all other contexts if the reset was triggered by the robustness
943 // extension instead of being synthetic.
944 if (was_lost
&& decoder_
&& decoder_
->WasContextLostByRobustnessExtension() &&
945 (gfx::GLContext::LosesAllContextsOnContextLost() ||
946 use_virtualized_gl_context_
))
947 channel_
->LoseAllContexts();
951 void GpuCommandBufferStub::MarkContextLost() {
952 if (!command_buffer_
||
953 command_buffer_
->GetState().error
== gpu::error::kLostContext
)
956 command_buffer_
->SetContextLostReason(gpu::error::kUnknown
);
958 decoder_
->LoseContext(GL_UNKNOWN_CONTEXT_RESET_ARB
);
959 command_buffer_
->SetParseError(gpu::error::kLostContext
);
962 } // namespace content