[email protected] | 04d58ce | 2011-09-29 13:12:23 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #if defined(ENABLE_GPU) |
| 6 | |
[email protected] | 80c4975 | 2011-04-18 23:55:10 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 9d37f06 | 2011-11-22 01:24:52 | [diff] [blame^] | 8 | #include "base/bind_helpers.h" |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 9 | #include "base/command_line.h" |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 10 | #include "base/debug/trace_event.h" |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 11 | #include "base/shared_memory.h" |
[email protected] | 6217d39 | 2010-03-25 22:08:35 | [diff] [blame] | 12 | #include "build/build_config.h" |
[email protected] | f24a1e2b | 2011-04-08 01:48:48 | [diff] [blame] | 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" |
[email protected] | 202b54ff | 2011-04-22 21:36:38 | [diff] [blame] | 16 | #include "content/common/gpu/gpu_messages.h" |
[email protected] | 2dcf702 | 2011-04-15 19:20:41 | [diff] [blame] | 17 | #include "content/common/gpu/gpu_watchdog.h" |
[email protected] | 16aa68e | 2011-11-21 23:40:02 | [diff] [blame] | 18 | #include "gpu/command_buffer/common/constants.h" |
[email protected] | 16aa68e | 2011-11-21 23:40:02 | [diff] [blame] | 19 | #include "ui/gfx/gl/gl_switches.h" |
[email protected] | dc68312 | 2011-11-18 02:22:59 | [diff] [blame] | 20 | |
[email protected] | bc4a343 | 2011-11-21 23:55:56 | [diff] [blame] | 21 | #if defined(OS_MACOSX) || defined(UI_COMPOSITOR_IMAGE_TRANSPORT) |
| 22 | #include "content/common/gpu/image_transport_surface.h" |
| 23 | #endif |
| 24 | |
[email protected] | a3ded6d | 2010-10-19 06:44:39 | [diff] [blame] | 25 | GpuCommandBufferStub::GpuCommandBufferStub( |
| 26 | GpuChannel* channel, |
[email protected] | 3b1ecc26 | 2011-08-03 22:49:57 | [diff] [blame] | 27 | GpuCommandBufferStub* share_group, |
[email protected] | a3ded6d | 2010-10-19 06:44:39 | [diff] [blame] | 28 | gfx::PluginWindowHandle handle, |
[email protected] | a3ded6d | 2010-10-19 06:44:39 | [diff] [blame] | 29 | const gfx::Size& size, |
[email protected] | e82fb79 | 2011-09-22 00:33:29 | [diff] [blame] | 30 | const gpu::gles2::DisallowedFeatures& disallowed_features, |
[email protected] | a3ded6d | 2010-10-19 06:44:39 | [diff] [blame] | 31 | const std::string& allowed_extensions, |
| 32 | const std::vector<int32>& attribs, |
[email protected] | 276f8906 | 2011-10-13 22:55:50 | [diff] [blame] | 33 | gfx::GpuPreference gpu_preference, |
[email protected] | a3ded6d | 2010-10-19 06:44:39 | [diff] [blame] | 34 | int32 route_id, |
| 35 | int32 renderer_id, |
[email protected] | 808f7fe7 | 2011-03-23 03:49:02 | [diff] [blame] | 36 | int32 render_view_id, |
[email protected] | f81f595 | 2011-07-21 18:52:47 | [diff] [blame] | 37 | GpuWatchdog* watchdog, |
| 38 | bool software) |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 39 | : channel_(channel), |
[email protected] | 6ebf2fc | 2010-07-01 01:06:32 | [diff] [blame] | 40 | handle_(handle), |
[email protected] | 6217d39 | 2010-03-25 22:08:35 | [diff] [blame] | 41 | initial_size_(size), |
[email protected] | e82fb79 | 2011-09-22 00:33:29 | [diff] [blame] | 42 | disallowed_features_(disallowed_features), |
[email protected] | bc51875 | 2010-10-20 21:38:27 | [diff] [blame] | 43 | allowed_extensions_(allowed_extensions), |
[email protected] | 34ff8b0c | 2010-10-01 20:06:02 | [diff] [blame] | 44 | requested_attribs_(attribs), |
[email protected] | 276f8906 | 2011-10-13 22:55:50 | [diff] [blame] | 45 | gpu_preference_(gpu_preference), |
[email protected] | 77e74db | 2010-08-04 17:46:23 | [diff] [blame] | 46 | route_id_(route_id), |
[email protected] | f81f595 | 2011-07-21 18:52:47 | [diff] [blame] | 47 | software_(software), |
[email protected] | a3847714 | 2011-05-28 02:45:58 | [diff] [blame] | 48 | last_flush_count_(0), |
[email protected] | 77e74db | 2010-08-04 17:46:23 | [diff] [blame] | 49 | renderer_id_(renderer_id), |
[email protected] | 808f7fe7 | 2011-03-23 03:49:02 | [diff] [blame] | 50 | render_view_id_(render_view_id), |
[email protected] | e7da6aef | 2011-06-22 19:29:03 | [diff] [blame] | 51 | parent_stub_for_initialization_(), |
| 52 | parent_texture_for_initialization_(0), |
[email protected] | 35a5b75 | 2011-11-17 23:58:58 | [diff] [blame] | 53 | watchdog_(watchdog) { |
[email protected] | bf5a8d13 | 2011-08-16 08:39:35 | [diff] [blame] | 54 | if (share_group) { |
[email protected] | 3b1ecc26 | 2011-08-03 22:49:57 | [diff] [blame] | 55 | context_group_ = share_group->context_group_; |
[email protected] | bf5a8d13 | 2011-08-16 08:39:35 | [diff] [blame] | 56 | } else { |
| 57 | // TODO(gman): this needs to be false for everything but Pepper. |
| 58 | bool bind_generates_resource = true; |
| 59 | context_group_ = new gpu::gles2::ContextGroup(bind_generates_resource); |
| 60 | } |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | GpuCommandBufferStub::~GpuCommandBufferStub() { |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 64 | Destroy(); |
[email protected] | d8a58e4 | 2011-01-27 15:24:25 | [diff] [blame] | 65 | |
[email protected] | f24a1e2b | 2011-04-08 01:48:48 | [diff] [blame] | 66 | GpuChannelManager* gpu_channel_manager = channel_->gpu_channel_manager(); |
| 67 | gpu_channel_manager->Send(new GpuHostMsg_DestroyCommandBuffer( |
[email protected] | d8a58e4 | 2011-01-27 15:24:25 | [diff] [blame] | 68 | handle_, renderer_id_, render_view_id_)); |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 69 | } |
| 70 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 71 | bool GpuCommandBufferStub::OnMessageReceived(const IPC::Message& message) { |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 72 | // Ensure the appropriate GL context is current before handling any IPC |
| 73 | // messages directed at the command buffer. This ensures that the message |
| 74 | // handler can assume that the context is current. |
| 75 | if (decoder_.get()) { |
| 76 | if (!decoder_->MakeCurrent()) { |
[email protected] | cc63025a | 2011-10-29 02:16:24 | [diff] [blame] | 77 | DLOG(ERROR) << "Context lost because MakeCurrent failed."; |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 78 | command_buffer_->SetContextLostReason(decoder_->GetContextLostReason()); |
| 79 | command_buffer_->SetParseError(gpu::error::kLostContext); |
[email protected] | 8b34cf4 | 2011-09-23 20:53:58 | [diff] [blame] | 80 | if (gfx::GLContext::LosesAllContextsOnContextLost()) |
| 81 | channel_->LoseAllContexts(); |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 82 | return false; |
| 83 | } |
| 84 | } |
| 85 | |
[email protected] | 310bed51 | 2011-05-09 21:12:25 | [diff] [blame] | 86 | // Always use IPC_MESSAGE_HANDLER_DELAY_REPLY for synchronous message handlers |
| 87 | // here. This is so the reply can be delayed if the scheduler is unscheduled. |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 88 | bool handled = true; |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 89 | IPC_BEGIN_MESSAGE_MAP(GpuCommandBufferStub, message) |
[email protected] | 310bed51 | 2011-05-09 21:12:25 | [diff] [blame] | 90 | IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuCommandBufferMsg_Initialize, |
| 91 | OnInitialize); |
[email protected] | 3c644d8 | 2011-06-20 19:58:24 | [diff] [blame] | 92 | IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuCommandBufferMsg_SetParent, |
| 93 | OnSetParent); |
[email protected] | 310bed51 | 2011-05-09 21:12:25 | [diff] [blame] | 94 | IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuCommandBufferMsg_GetState, OnGetState); |
[email protected] | 451e966 | 2011-10-10 21:14:34 | [diff] [blame] | 95 | IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuCommandBufferMsg_GetStateFast, |
| 96 | OnGetStateFast); |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 97 | IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_AsyncFlush, OnAsyncFlush); |
[email protected] | d0f02c4 | 2011-07-21 21:40:48 | [diff] [blame] | 98 | IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_Rescheduled, OnRescheduled); |
[email protected] | 310bed51 | 2011-05-09 21:12:25 | [diff] [blame] | 99 | IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuCommandBufferMsg_CreateTransferBuffer, |
| 100 | OnCreateTransferBuffer); |
| 101 | IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuCommandBufferMsg_RegisterTransferBuffer, |
| 102 | OnRegisterTransferBuffer); |
| 103 | IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuCommandBufferMsg_DestroyTransferBuffer, |
| 104 | OnDestroyTransferBuffer); |
| 105 | IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuCommandBufferMsg_GetTransferBuffer, |
| 106 | OnGetTransferBuffer); |
[email protected] | c842306 | 2011-07-29 19:25:38 | [diff] [blame] | 107 | IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuCommandBufferMsg_CreateVideoDecoder, |
| 108 | OnCreateVideoDecoder) |
[email protected] | a13da24 | 2011-07-07 19:48:17 | [diff] [blame] | 109 | IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_DestroyVideoDecoder, |
| 110 | OnDestroyVideoDecoder) |
[email protected] | 8cc980c5 | 2011-10-14 20:35:51 | [diff] [blame] | 111 | IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_SetSurfaceVisible, |
| 112 | OnSetSurfaceVisible) |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 113 | IPC_MESSAGE_UNHANDLED(handled = false) |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 114 | IPC_END_MESSAGE_MAP() |
[email protected] | a13da24 | 2011-07-07 19:48:17 | [diff] [blame] | 115 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 116 | DCHECK(handled); |
| 117 | return handled; |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | bool GpuCommandBufferStub::Send(IPC::Message* message) { |
| 121 | return channel_->Send(message); |
| 122 | } |
| 123 | |
[email protected] | d0f02c4 | 2011-07-21 21:40:48 | [diff] [blame] | 124 | bool GpuCommandBufferStub::IsScheduled() { |
| 125 | return !scheduler_.get() || scheduler_->IsScheduled(); |
| 126 | } |
| 127 | |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 128 | void GpuCommandBufferStub::Destroy() { |
| 129 | // The scheduler has raw references to the decoder and the command buffer so |
| 130 | // destroy it before those. |
[email protected] | 4ee0c305 | 2011-09-16 19:30:36 | [diff] [blame] | 131 | scheduler_.reset(); |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 132 | |
| 133 | if (decoder_.get()) { |
| 134 | decoder_->Destroy(); |
| 135 | decoder_.reset(); |
| 136 | } |
| 137 | |
[email protected] | 4ee0c305 | 2011-09-16 19:30:36 | [diff] [blame] | 138 | command_buffer_.reset(); |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 139 | |
| 140 | context_ = NULL; |
| 141 | surface_ = NULL; |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void GpuCommandBufferStub::OnInitializeFailed(IPC::Message* reply_message) { |
| 145 | Destroy(); |
[email protected] | 2e7bbf2 | 2011-07-22 18:41:29 | [diff] [blame] | 146 | GpuCommandBufferMsg_Initialize::WriteReplyParams(reply_message, false); |
| 147 | Send(reply_message); |
| 148 | } |
| 149 | |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 150 | void GpuCommandBufferStub::OnInitialize( |
[email protected] | 36dbfe1 | 2011-02-28 23:36:54 | [diff] [blame] | 151 | base::SharedMemoryHandle ring_buffer, |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 152 | int32 size, |
[email protected] | 310bed51 | 2011-05-09 21:12:25 | [diff] [blame] | 153 | IPC::Message* reply_message) { |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 154 | DCHECK(!command_buffer_.get()); |
| 155 | |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 156 | command_buffer_.reset(new gpu::CommandBufferService); |
| 157 | |
[email protected] | 36dbfe1 | 2011-02-28 23:36:54 | [diff] [blame] | 158 | #if defined(OS_WIN) |
| 159 | // Windows dups the shared memory handle it receives into the current process |
| 160 | // and closes it when this variable goes out of scope. |
| 161 | base::SharedMemory shared_memory(ring_buffer, |
| 162 | false, |
| 163 | channel_->renderer_process()); |
| 164 | #else |
| 165 | // POSIX receives a dup of the shared memory handle and closes the dup when |
| 166 | // this variable goes out of scope. |
| 167 | base::SharedMemory shared_memory(ring_buffer, false); |
| 168 | #endif |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 169 | |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 170 | if (!command_buffer_->Initialize(&shared_memory, size)) { |
| 171 | OnInitializeFailed(reply_message); |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | decoder_.reset(::gpu::gles2::GLES2Decoder::Create(context_group_.get())); |
| 176 | |
| 177 | scheduler_.reset(new gpu::GpuScheduler(command_buffer_.get(), |
| 178 | decoder_.get(), |
| 179 | NULL)); |
| 180 | |
| 181 | decoder_->set_engine(scheduler_.get()); |
| 182 | |
| 183 | if (handle_) { |
[email protected] | 839d517 | 2011-10-13 17:18:11 | [diff] [blame] | 184 | #if defined(OS_MACOSX) || defined(UI_COMPOSITOR_IMAGE_TRANSPORT) |
[email protected] | 2e7bbf2 | 2011-07-22 18:41:29 | [diff] [blame] | 185 | if (software_) { |
| 186 | OnInitializeFailed(reply_message); |
| 187 | return; |
| 188 | } |
| 189 | |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 190 | surface_ = ImageTransportSurface::CreateSurface( |
| 191 | channel_->gpu_channel_manager(), |
| 192 | render_view_id_, |
| 193 | renderer_id_, |
[email protected] | 04d58ce | 2011-09-29 13:12:23 | [diff] [blame] | 194 | route_id_, |
| 195 | handle_); |
[email protected] | bc4a343 | 2011-11-21 23:55:56 | [diff] [blame] | 196 | #elif defined(OS_WIN) || defined(OS_LINUX) || defined(OS_OPENBSD) |
| 197 | surface_ = gfx::GLSurface::CreateViewGLSurface(software_, handle_); |
| 198 | #endif |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 199 | } else { |
| 200 | surface_ = gfx::GLSurface::CreateOffscreenGLSurface(software_, |
| 201 | gfx::Size(1, 1)); |
| 202 | } |
| 203 | |
| 204 | if (!surface_.get()) { |
[email protected] | cdcd5bd | 2011-10-15 00:19:38 | [diff] [blame] | 205 | // Ensure the decoder is not destroyed if it is not initialized. |
| 206 | decoder_.reset(); |
| 207 | |
[email protected] | cc63025a | 2011-10-29 02:16:24 | [diff] [blame] | 208 | DLOG(ERROR) << "Failed to create surface.\n"; |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 209 | OnInitializeFailed(reply_message); |
| 210 | return; |
| 211 | } |
| 212 | |
[email protected] | 276f8906 | 2011-10-13 22:55:50 | [diff] [blame] | 213 | gfx::GpuPreference gpu_preference = |
| 214 | channel_->ShouldPreferDiscreteGpu() ? |
| 215 | gfx::PreferDiscreteGpu : gpu_preference_; |
| 216 | |
| 217 | context_ = gfx::GLContext::CreateGLContext( |
| 218 | channel_->share_group(), |
| 219 | surface_.get(), |
| 220 | gpu_preference); |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 221 | if (!context_.get()) { |
[email protected] | cdcd5bd | 2011-10-15 00:19:38 | [diff] [blame] | 222 | // Ensure the decoder is not destroyed if it is not initialized. |
| 223 | decoder_.reset(); |
| 224 | |
[email protected] | cc63025a | 2011-10-29 02:16:24 | [diff] [blame] | 225 | DLOG(ERROR) << "Failed to create context.\n"; |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 226 | OnInitializeFailed(reply_message); |
| 227 | return; |
| 228 | } |
[email protected] | 4bbe6d0c | 2011-09-16 18:58:54 | [diff] [blame] | 229 | |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 230 | // Initialize the decoder with either the view or pbuffer GLContext. |
| 231 | if (!decoder_->Initialize(surface_.get(), |
| 232 | context_.get(), |
| 233 | initial_size_, |
[email protected] | e82fb79 | 2011-09-22 00:33:29 | [diff] [blame] | 234 | disallowed_features_, |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 235 | allowed_extensions_.c_str(), |
| 236 | requested_attribs_)) { |
[email protected] | cc63025a | 2011-10-29 02:16:24 | [diff] [blame] | 237 | DLOG(ERROR) << "Failed to initialize decoder."; |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 238 | OnInitializeFailed(reply_message); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 243 | switches::kEnableGPUServiceLogging)) { |
| 244 | decoder_->set_debug(true); |
| 245 | } |
| 246 | |
[email protected] | ebe7144 | 2011-09-30 02:11:57 | [diff] [blame] | 247 | SetSwapInterval(); |
| 248 | |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 249 | command_buffer_->SetPutOffsetChangeCallback( |
[email protected] | 9d37f06 | 2011-11-22 01:24:52 | [diff] [blame^] | 250 | base::Bind(&gpu::GpuScheduler::PutChanged, |
| 251 | base::Unretained(scheduler_.get()))); |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 252 | command_buffer_->SetParseErrorCallback( |
[email protected] | 9d37f06 | 2011-11-22 01:24:52 | [diff] [blame^] | 253 | base::Bind(&GpuCommandBufferStub::OnParseError, base::Unretained(this))); |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 254 | scheduler_->SetScheduledCallback( |
[email protected] | 9d37f06 | 2011-11-22 01:24:52 | [diff] [blame^] | 255 | base::Bind(&GpuChannel::OnScheduled, base::Unretained(channel_))); |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 256 | |
[email protected] | bc4a343 | 2011-11-21 23:55:56 | [diff] [blame] | 257 | // On platforms that use an ImageTransportSurface, the surface |
| 258 | // handles co-ordinating the resize with the browser process. The |
| 259 | // surface sets it's own resize callback, so we shouldn't do it here. |
| 260 | #if !defined(OS_MACOSX) && !defined(UI_COMPOSITOR_IMAGE_TRANSPORT) |
| 261 | if (handle_ != gfx::kNullPluginWindow) { |
| 262 | decoder_->SetResizeCallback( |
[email protected] | 9d37f06 | 2011-11-22 01:24:52 | [diff] [blame^] | 263 | base::Bind(&GpuCommandBufferStub::OnResize, base::Unretained(this))); |
[email protected] | bc4a343 | 2011-11-21 23:55:56 | [diff] [blame] | 264 | } |
| 265 | #endif |
| 266 | |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 267 | if (watchdog_) { |
| 268 | scheduler_->SetCommandProcessedCallback( |
[email protected] | 9d37f06 | 2011-11-22 01:24:52 | [diff] [blame^] | 269 | base::Bind(&GpuCommandBufferStub::OnCommandProcessed, |
| 270 | base::Unretained(this))); |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | if (parent_stub_for_initialization_) { |
| 274 | decoder_->SetParent(parent_stub_for_initialization_->decoder_.get(), |
| 275 | parent_texture_for_initialization_); |
| 276 | parent_stub_for_initialization_.reset(); |
| 277 | parent_texture_for_initialization_ = 0; |
| 278 | } |
[email protected] | 310bed51 | 2011-05-09 21:12:25 | [diff] [blame] | 279 | |
[email protected] | 2e7bbf2 | 2011-07-22 18:41:29 | [diff] [blame] | 280 | GpuCommandBufferMsg_Initialize::WriteReplyParams(reply_message, true); |
[email protected] | 310bed51 | 2011-05-09 21:12:25 | [diff] [blame] | 281 | Send(reply_message); |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 282 | } |
| 283 | |
[email protected] | 3c644d8 | 2011-06-20 19:58:24 | [diff] [blame] | 284 | void GpuCommandBufferStub::OnSetParent(int32 parent_route_id, |
| 285 | uint32 parent_texture_id, |
| 286 | IPC::Message* reply_message) { |
[email protected] | 3c644d8 | 2011-06-20 19:58:24 | [diff] [blame] | 287 | |
[email protected] | e7da6aef | 2011-06-22 19:29:03 | [diff] [blame] | 288 | GpuCommandBufferStub* parent_stub = NULL; |
| 289 | if (parent_route_id != MSG_ROUTING_NONE) { |
| 290 | parent_stub = channel_->LookupCommandBuffer(parent_route_id); |
[email protected] | 3c644d8 | 2011-06-20 19:58:24 | [diff] [blame] | 291 | } |
| 292 | |
[email protected] | e7da6aef | 2011-06-22 19:29:03 | [diff] [blame] | 293 | bool result = true; |
| 294 | if (scheduler_.get()) { |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 295 | gpu::gles2::GLES2Decoder* parent_decoder = |
| 296 | parent_stub ? parent_stub->decoder_.get() : NULL; |
| 297 | result = decoder_->SetParent(parent_decoder, parent_texture_id); |
[email protected] | e7da6aef | 2011-06-22 19:29:03 | [diff] [blame] | 298 | } else { |
| 299 | // If we don't have a scheduler, it means that Initialize hasn't been called |
| 300 | // yet. Keep around the requested parent stub and texture so that we can set |
| 301 | // it in Initialize(). |
| 302 | parent_stub_for_initialization_ = parent_stub ? |
| 303 | parent_stub->AsWeakPtr() : base::WeakPtr<GpuCommandBufferStub>(); |
| 304 | parent_texture_for_initialization_ = parent_texture_id; |
| 305 | } |
[email protected] | 3c644d8 | 2011-06-20 19:58:24 | [diff] [blame] | 306 | GpuCommandBufferMsg_SetParent::WriteReplyParams(reply_message, result); |
| 307 | Send(reply_message); |
| 308 | } |
| 309 | |
[email protected] | 310bed51 | 2011-05-09 21:12:25 | [diff] [blame] | 310 | void GpuCommandBufferStub::OnGetState(IPC::Message* reply_message) { |
| 311 | gpu::CommandBuffer::State state = command_buffer_->GetState(); |
| 312 | if (state.error == gpu::error::kLostContext && |
[email protected] | ef16c17 | 2011-04-28 23:37:14 | [diff] [blame] | 313 | gfx::GLContext::LosesAllContextsOnContextLost()) |
| 314 | channel_->LoseAllContexts(); |
[email protected] | 310bed51 | 2011-05-09 21:12:25 | [diff] [blame] | 315 | |
| 316 | GpuCommandBufferMsg_GetState::WriteReplyParams(reply_message, state); |
| 317 | Send(reply_message); |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 318 | } |
| 319 | |
[email protected] | 0a68ac87 | 2011-06-07 04:48:27 | [diff] [blame] | 320 | void GpuCommandBufferStub::OnParseError() { |
| 321 | TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnParseError"); |
[email protected] | 38d139d | 2011-07-14 00:38:43 | [diff] [blame] | 322 | gpu::CommandBuffer::State state = command_buffer_->GetState(); |
| 323 | IPC::Message* msg = new GpuCommandBufferMsg_Destroyed( |
| 324 | route_id_, state.context_lost_reason); |
[email protected] | 0a68ac87 | 2011-06-07 04:48:27 | [diff] [blame] | 325 | msg->set_unblock(true); |
| 326 | Send(msg); |
| 327 | } |
| 328 | |
[email protected] | 451e966 | 2011-10-10 21:14:34 | [diff] [blame] | 329 | void GpuCommandBufferStub::OnGetStateFast(IPC::Message* reply_message) { |
| 330 | TRACE_EVENT0("gpu", "GpuCommandBufferStub::OnGetStateFast"); |
[email protected] | d0f02c4 | 2011-07-21 21:40:48 | [diff] [blame] | 331 | gpu::CommandBuffer::State state = command_buffer_->GetState(); |
[email protected] | 451e966 | 2011-10-10 21:14:34 | [diff] [blame] | 332 | if (state.error == gpu::error::kLostContext && |
| 333 | gfx::GLContext::LosesAllContextsOnContextLost()) |
| 334 | channel_->LoseAllContexts(); |
[email protected] | 1d4ea84 | 2011-07-20 01:11:51 | [diff] [blame] | 335 | |
[email protected] | 451e966 | 2011-10-10 21:14:34 | [diff] [blame] | 336 | GpuCommandBufferMsg_GetStateFast::WriteReplyParams(reply_message, state); |
| 337 | Send(reply_message); |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 338 | } |
| 339 | |
[email protected] | 451e966 | 2011-10-10 21:14:34 | [diff] [blame] | 340 | void GpuCommandBufferStub::OnAsyncFlush(int32 put_offset, |
| 341 | uint32 flush_count) { |
| 342 | TRACE_EVENT1("gpu", "GpuCommandBufferStub::OnAsyncFlush", |
| 343 | "put_offset", put_offset); |
[email protected] | fde205d | 2011-05-31 22:29:50 | [diff] [blame] | 344 | if (flush_count - last_flush_count_ < 0x8000000U) { |
[email protected] | a3847714 | 2011-05-28 02:45:58 | [diff] [blame] | 345 | last_flush_count_ = flush_count; |
| 346 | command_buffer_->Flush(put_offset); |
| 347 | } else { |
[email protected] | fde205d | 2011-05-31 22:29:50 | [diff] [blame] | 348 | // We received this message out-of-order. This should not happen but is here |
| 349 | // to catch regressions. Ignore the message. |
| 350 | NOTREACHED() << "Received a Flush message out-of-order"; |
[email protected] | a3847714 | 2011-05-28 02:45:58 | [diff] [blame] | 351 | } |
[email protected] | d0f02c4 | 2011-07-21 21:40:48 | [diff] [blame] | 352 | |
| 353 | ReportState(); |
| 354 | } |
| 355 | |
| 356 | void GpuCommandBufferStub::OnRescheduled() { |
| 357 | gpu::CommandBuffer::State state = command_buffer_->GetLastState(); |
| 358 | command_buffer_->Flush(state.put_offset); |
| 359 | |
| 360 | ReportState(); |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 361 | } |
| 362 | |
[email protected] | ce9eea60 | 2011-04-12 20:09:57 | [diff] [blame] | 363 | void GpuCommandBufferStub::OnCreateTransferBuffer(int32 size, |
| 364 | int32 id_request, |
[email protected] | 310bed51 | 2011-05-09 21:12:25 | [diff] [blame] | 365 | IPC::Message* reply_message) { |
| 366 | int32 id = command_buffer_->CreateTransferBuffer(size, id_request); |
| 367 | GpuCommandBufferMsg_CreateTransferBuffer::WriteReplyParams(reply_message, id); |
| 368 | Send(reply_message); |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 369 | } |
| 370 | |
[email protected] | 0100b7a | 2011-02-24 22:54:50 | [diff] [blame] | 371 | void GpuCommandBufferStub::OnRegisterTransferBuffer( |
| 372 | base::SharedMemoryHandle transfer_buffer, |
| 373 | size_t size, |
[email protected] | ce9eea60 | 2011-04-12 20:09:57 | [diff] [blame] | 374 | int32 id_request, |
[email protected] | 310bed51 | 2011-05-09 21:12:25 | [diff] [blame] | 375 | IPC::Message* reply_message) { |
[email protected] | 0100b7a | 2011-02-24 22:54:50 | [diff] [blame] | 376 | #if defined(OS_WIN) |
[email protected] | 7df1d1a | 2011-02-26 02:07:57 | [diff] [blame] | 377 | // Windows dups the shared memory handle it receives into the current process |
| 378 | // and closes it when this variable goes out of scope. |
[email protected] | 0100b7a | 2011-02-24 22:54:50 | [diff] [blame] | 379 | base::SharedMemory shared_memory(transfer_buffer, |
| 380 | false, |
| 381 | channel_->renderer_process()); |
| 382 | #else |
[email protected] | 7df1d1a | 2011-02-26 02:07:57 | [diff] [blame] | 383 | // POSIX receives a dup of the shared memory handle and closes the dup when |
| 384 | // this variable goes out of scope. |
[email protected] | 0100b7a | 2011-02-24 22:54:50 | [diff] [blame] | 385 | base::SharedMemory shared_memory(transfer_buffer, false); |
| 386 | #endif |
[email protected] | 7df1d1a | 2011-02-26 02:07:57 | [diff] [blame] | 387 | |
[email protected] | 310bed51 | 2011-05-09 21:12:25 | [diff] [blame] | 388 | int32 id = command_buffer_->RegisterTransferBuffer(&shared_memory, |
| 389 | size, |
| 390 | id_request); |
| 391 | |
| 392 | GpuCommandBufferMsg_RegisterTransferBuffer::WriteReplyParams(reply_message, |
| 393 | id); |
| 394 | Send(reply_message); |
[email protected] | 0100b7a | 2011-02-24 22:54:50 | [diff] [blame] | 395 | } |
| 396 | |
[email protected] | 310bed51 | 2011-05-09 21:12:25 | [diff] [blame] | 397 | void GpuCommandBufferStub::OnDestroyTransferBuffer( |
| 398 | int32 id, |
| 399 | IPC::Message* reply_message) { |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 400 | command_buffer_->DestroyTransferBuffer(id); |
[email protected] | 310bed51 | 2011-05-09 21:12:25 | [diff] [blame] | 401 | Send(reply_message); |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | void GpuCommandBufferStub::OnGetTransferBuffer( |
| 405 | int32 id, |
[email protected] | 310bed51 | 2011-05-09 21:12:25 | [diff] [blame] | 406 | IPC::Message* reply_message) { |
| 407 | base::SharedMemoryHandle transfer_buffer = base::SharedMemoryHandle(); |
| 408 | uint32 size = 0; |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 409 | |
[email protected] | 0100b7a | 2011-02-24 22:54:50 | [diff] [blame] | 410 | // Fail if the renderer process has not provided its process handle. |
| 411 | if (!channel_->renderer_process()) |
| 412 | return; |
| 413 | |
[email protected] | 79311e8 | 2011-09-20 00:40:50 | [diff] [blame] | 414 | gpu::Buffer buffer = command_buffer_->GetTransferBuffer(id); |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 415 | if (buffer.shared_memory) { |
| 416 | // Assume service is responsible for duplicating the handle to the calling |
| 417 | // process. |
[email protected] | 0100b7a | 2011-02-24 22:54:50 | [diff] [blame] | 418 | buffer.shared_memory->ShareToProcess(channel_->renderer_process(), |
[email protected] | 310bed51 | 2011-05-09 21:12:25 | [diff] [blame] | 419 | &transfer_buffer); |
| 420 | size = buffer.size; |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 421 | } |
[email protected] | 310bed51 | 2011-05-09 21:12:25 | [diff] [blame] | 422 | |
| 423 | GpuCommandBufferMsg_GetTransferBuffer::WriteReplyParams(reply_message, |
| 424 | transfer_buffer, |
| 425 | size); |
| 426 | Send(reply_message); |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 427 | } |
| 428 | |
[email protected] | 310bed51 | 2011-05-09 21:12:25 | [diff] [blame] | 429 | void GpuCommandBufferStub::OnCommandProcessed() { |
| 430 | if (watchdog_) |
| 431 | watchdog_->CheckArmed(); |
| 432 | } |
| 433 | |
[email protected] | bc4a343 | 2011-11-21 23:55:56 | [diff] [blame] | 434 | void GpuCommandBufferStub::OnResize(gfx::Size size) { |
| 435 | if (handle_ == gfx::kNullPluginWindow) |
| 436 | return; |
| 437 | |
| 438 | #if defined(TOOLKIT_USES_GTK) && !defined(UI_COMPOSITOR_IMAGE_TRANSPORT) || \ |
| 439 | defined(OS_WIN) |
| 440 | GpuChannelManager* gpu_channel_manager = channel_->gpu_channel_manager(); |
| 441 | |
| 442 | // On Windows, Linux, we need to coordinate resizing of onscreen |
| 443 | // contexts with the resizing of the actual OS-level window. We do this by |
| 444 | // sending a resize message to the browser process and descheduling the |
| 445 | // context until the ViewResized message comes back in reply. |
| 446 | // Send the resize message if needed |
| 447 | gpu_channel_manager->Send( |
| 448 | new GpuHostMsg_ResizeView(renderer_id_, |
| 449 | render_view_id_, |
| 450 | route_id_, |
| 451 | size)); |
| 452 | |
| 453 | scheduler_->SetScheduled(false); |
| 454 | #endif |
| 455 | } |
| 456 | |
| 457 | void GpuCommandBufferStub::ViewResized() { |
| 458 | #if defined(TOOLKIT_USES_GTK) && !defined(UI_COMPOSITOR_IMAGE_TRANSPORT) || \ |
| 459 | defined(OS_WIN) |
| 460 | DCHECK(handle_ != gfx::kNullPluginWindow); |
| 461 | scheduler_->SetScheduled(true); |
| 462 | #endif |
| 463 | |
| 464 | #if defined(OS_WIN) |
| 465 | // Recreate the view surface to match the window size. Swap chains do not |
| 466 | // automatically resize with window size with D3D. |
| 467 | context_->ReleaseCurrent(surface_.get()); |
| 468 | if (surface_.get()) { |
| 469 | surface_->Destroy(); |
| 470 | surface_->Initialize(); |
| 471 | SetSwapInterval(); |
| 472 | } |
| 473 | #endif |
| 474 | } |
| 475 | |
[email protected] | ef16c17 | 2011-04-28 23:37:14 | [diff] [blame] | 476 | void GpuCommandBufferStub::ReportState() { |
| 477 | gpu::CommandBuffer::State state = command_buffer_->GetState(); |
| 478 | if (state.error == gpu::error::kLostContext && |
| 479 | gfx::GLContext::LosesAllContextsOnContextLost()) { |
| 480 | channel_->LoseAllContexts(); |
| 481 | } else { |
| 482 | IPC::Message* msg = new GpuCommandBufferMsg_UpdateState(route_id_, state); |
| 483 | msg->set_unblock(true); |
| 484 | Send(msg); |
| 485 | } |
| 486 | } |
| 487 | |
[email protected] | bc4a343 | 2011-11-21 23:55:56 | [diff] [blame] | 488 | void GpuCommandBufferStub::SetSwapInterval() { |
| 489 | #if !defined(OS_MACOSX) && !defined(UI_COMPOSITOR_IMAGE_TRANSPORT) |
| 490 | // Set up swap interval for onscreen contexts. |
| 491 | if (!surface_->IsOffscreen()) { |
| 492 | decoder_->MakeCurrent(); |
| 493 | if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableGpuVsync)) |
| 494 | context_->SetSwapInterval(0); |
| 495 | else |
| 496 | context_->SetSwapInterval(1); |
| 497 | } |
| 498 | #endif |
| 499 | } |
| 500 | |
[email protected] | a13da24 | 2011-07-07 19:48:17 | [diff] [blame] | 501 | void GpuCommandBufferStub::OnCreateVideoDecoder( |
[email protected] | 2ffc31a | 2011-09-01 03:18:28 | [diff] [blame] | 502 | media::VideoDecodeAccelerator::Profile profile, |
[email protected] | c842306 | 2011-07-29 19:25:38 | [diff] [blame] | 503 | IPC::Message* reply_message) { |
[email protected] | 502e605 | 2011-08-17 00:17:23 | [diff] [blame] | 504 | int decoder_route_id = channel_->GenerateRouteID(); |
| 505 | GpuCommandBufferMsg_CreateVideoDecoder::WriteReplyParams( |
| 506 | reply_message, decoder_route_id); |
| 507 | GpuVideoDecodeAccelerator* decoder = |
| 508 | new GpuVideoDecodeAccelerator(this, decoder_route_id, this); |
| 509 | video_decoders_.AddWithID(decoder, decoder_route_id); |
| 510 | channel_->AddRoute(decoder_route_id, decoder); |
[email protected] | 2ffc31a | 2011-09-01 03:18:28 | [diff] [blame] | 511 | decoder->Initialize(profile, reply_message); |
[email protected] | a13da24 | 2011-07-07 19:48:17 | [diff] [blame] | 512 | } |
| 513 | |
[email protected] | 502e605 | 2011-08-17 00:17:23 | [diff] [blame] | 514 | void GpuCommandBufferStub::OnDestroyVideoDecoder(int decoder_route_id) { |
| 515 | channel_->RemoveRoute(decoder_route_id); |
| 516 | video_decoders_.Remove(decoder_route_id); |
[email protected] | a13da24 | 2011-07-07 19:48:17 | [diff] [blame] | 517 | } |
| 518 | |
[email protected] | 8cc980c5 | 2011-10-14 20:35:51 | [diff] [blame] | 519 | void GpuCommandBufferStub::OnSetSurfaceVisible(bool visible) { |
| 520 | surface_->SetVisible(visible); |
| 521 | } |
| 522 | |
[email protected] | 8e5cc28 | 2010-12-05 18:11:39 | [diff] [blame] | 523 | #endif // defined(ENABLE_GPU) |