| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/common/gpu/client/command_buffer_proxy_impl.h" | 5 #include "content/common/gpu/client/command_buffer_proxy_impl.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/shared_memory.h" | 10 #include "base/memory/shared_memory.h" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "cc/resources/gpu_memory_buffer_manager.h" |
| 12 #include "content/common/child_process_messages.h" | 13 #include "content/common/child_process_messages.h" |
| 13 #include "content/common/gpu/client/gpu_channel_host.h" | 14 #include "content/common/gpu/client/gpu_channel_host.h" |
| 14 #include "content/common/gpu/client/gpu_video_decode_accelerator_host.h" | 15 #include "content/common/gpu/client/gpu_video_decode_accelerator_host.h" |
| 15 #include "content/common/gpu/client/gpu_video_encode_accelerator_host.h" | 16 #include "content/common/gpu/client/gpu_video_encode_accelerator_host.h" |
| 16 #include "content/common/gpu/gpu_messages.h" | 17 #include "content/common/gpu/gpu_messages.h" |
| 17 #include "content/common/view_messages.h" | 18 #include "content/common/view_messages.h" |
| 18 #include "gpu/command_buffer/common/cmd_buffer_common.h" | 19 #include "gpu/command_buffer/common/cmd_buffer_common.h" |
| 19 #include "gpu/command_buffer/common/command_buffer_shared.h" | 20 #include "gpu/command_buffer/common/command_buffer_shared.h" |
| 20 #include "gpu/command_buffer/common/gpu_memory_allocation.h" | 21 #include "gpu/command_buffer/common/gpu_memory_allocation.h" |
| 21 #include "ui/gfx/size.h" | 22 #include "ui/gfx/size.h" |
| 23 #include "ui/gl/gl_bindings.h" |
| 22 | 24 |
| 23 namespace content { | 25 namespace content { |
| 26 namespace { |
| 27 |
| 28 gfx::GpuMemoryBuffer::Format ImageFormatToGpuMemoryBufferFormat( |
| 29 unsigned internalformat) { |
| 30 switch (internalformat) { |
| 31 case GL_RGB: |
| 32 return gfx::GpuMemoryBuffer::RGBX_8888; |
| 33 case GL_RGBA: |
| 34 return gfx::GpuMemoryBuffer::RGBA_8888; |
| 35 default: |
| 36 NOTREACHED(); |
| 37 return gfx::GpuMemoryBuffer::RGBA_8888; |
| 38 } |
| 39 } |
| 40 |
| 41 gfx::GpuMemoryBuffer::Usage ImageUsageToGpuMemoryBufferUsage(unsigned usage) { |
| 42 switch (usage) { |
| 43 case GL_MAP_CHROMIUM: |
| 44 return gfx::GpuMemoryBuffer::MAP; |
| 45 case GL_SCANOUT_CHROMIUM: |
| 46 return gfx::GpuMemoryBuffer::SCANOUT; |
| 47 default: |
| 48 NOTREACHED(); |
| 49 return gfx::GpuMemoryBuffer::MAP; |
| 50 } |
| 51 } |
| 52 |
| 53 bool IsImageFormatCompatibleWithGpuMemoryBufferFormat( |
| 54 gfx::GpuMemoryBuffer::Format format, |
| 55 unsigned internalformat) { |
| 56 switch (internalformat) { |
| 57 case GL_RGB: |
| 58 switch (format) { |
| 59 case gfx::GpuMemoryBuffer::RGBX_8888: |
| 60 return true; |
| 61 case gfx::GpuMemoryBuffer::RGBA_8888: |
| 62 case gfx::GpuMemoryBuffer::BGRA_8888: |
| 63 return false; |
| 64 } |
| 65 NOTREACHED(); |
| 66 return false; |
| 67 case GL_RGBA: |
| 68 switch (format) { |
| 69 case gfx::GpuMemoryBuffer::RGBX_8888: |
| 70 return false; |
| 71 case gfx::GpuMemoryBuffer::RGBA_8888: |
| 72 case gfx::GpuMemoryBuffer::BGRA_8888: |
| 73 return true; |
| 74 } |
| 75 NOTREACHED(); |
| 76 return false; |
| 77 default: |
| 78 NOTREACHED(); |
| 79 return false; |
| 80 } |
| 81 } |
| 82 |
| 83 } // namespace |
| 24 | 84 |
| 25 CommandBufferProxyImpl::CommandBufferProxyImpl( | 85 CommandBufferProxyImpl::CommandBufferProxyImpl( |
| 26 GpuChannelHost* channel, | 86 GpuChannelHost* channel, |
| 27 int route_id) | 87 int route_id) |
| 28 : channel_(channel), | 88 : channel_(channel), |
| 29 route_id_(route_id), | 89 route_id_(route_id), |
| 30 flush_count_(0), | 90 flush_count_(0), |
| 31 last_put_offset_(-1), | 91 last_put_offset_(-1), |
| 32 next_signal_id_(0) { | 92 next_signal_id_(0) { |
| 33 } | 93 } |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 if (last_state_.error != gpu::error::kNoError) | 342 if (last_state_.error != gpu::error::kNoError) |
| 283 return; | 343 return; |
| 284 | 344 |
| 285 Send(new GpuCommandBufferMsg_DestroyTransferBuffer(route_id_, id)); | 345 Send(new GpuCommandBufferMsg_DestroyTransferBuffer(route_id_, id)); |
| 286 } | 346 } |
| 287 | 347 |
| 288 gpu::Capabilities CommandBufferProxyImpl::GetCapabilities() { | 348 gpu::Capabilities CommandBufferProxyImpl::GetCapabilities() { |
| 289 return capabilities_; | 349 return capabilities_; |
| 290 } | 350 } |
| 291 | 351 |
| 292 gfx::GpuMemoryBuffer* CommandBufferProxyImpl::CreateGpuMemoryBuffer( | 352 int32_t CommandBufferProxyImpl::CreateImage(ClientBuffer buffer, |
| 353 size_t width, |
| 354 size_t height, |
| 355 unsigned internalformat) { |
| 356 if (last_state_.error != gpu::error::kNoError) |
| 357 return -1; |
| 358 |
| 359 int32 new_id = channel_->ReserveImageId(); |
| 360 |
| 361 gfx::GpuMemoryBuffer* gpu_memory_buffer = |
| 362 channel_->gpu_memory_buffer_manager()->GpuMemoryBufferFromClientBuffer( |
| 363 buffer); |
| 364 DCHECK(gpu_memory_buffer); |
| 365 |
| 366 // This handle is owned by the GPU process and must be passed to it or it |
| 367 // will leak. In otherwords, do not early out on error between here and the |
| 368 // sending of the CreateImage IPC below. |
| 369 gfx::GpuMemoryBufferHandle handle = |
| 370 channel_->ShareGpuMemoryBufferToGpuProcess( |
| 371 gpu_memory_buffer->GetHandle()); |
| 372 |
| 373 DCHECK(IsImageFormatCompatibleWithGpuMemoryBufferFormat( |
| 374 gpu_memory_buffer->GetFormat(), internalformat)); |
| 375 if (!Send(new GpuCommandBufferMsg_CreateImage(route_id_, |
| 376 new_id, |
| 377 handle, |
| 378 gfx::Size(width, height), |
| 379 gpu_memory_buffer->GetFormat(), |
| 380 internalformat))) { |
| 381 return -1; |
| 382 } |
| 383 |
| 384 return new_id; |
| 385 } |
| 386 |
| 387 void CommandBufferProxyImpl::DestroyImage(int32 id) { |
| 388 if (last_state_.error != gpu::error::kNoError) |
| 389 return; |
| 390 |
| 391 Send(new GpuCommandBufferMsg_DestroyImage(route_id_, id)); |
| 392 } |
| 393 |
| 394 int32_t CommandBufferProxyImpl::CreateGpuMemoryBufferImage( |
| 293 size_t width, | 395 size_t width, |
| 294 size_t height, | 396 size_t height, |
| 295 unsigned internalformat, | 397 unsigned internalformat, |
| 296 unsigned usage, | 398 unsigned usage) { |
| 297 int32* id) { | 399 scoped_ptr<gfx::GpuMemoryBuffer> buffer( |
| 298 *id = -1; | 400 channel_->gpu_memory_buffer_manager()->AllocateGpuMemoryBuffer( |
| 401 gfx::Size(width, height), |
| 402 ImageFormatToGpuMemoryBufferFormat(internalformat), |
| 403 ImageUsageToGpuMemoryBufferUsage(usage))); |
| 404 if (!buffer) |
| 405 return -1; |
| 299 | 406 |
| 300 if (last_state_.error != gpu::error::kNoError) | 407 return CreateImage(buffer->AsClientBuffer(), width, height, internalformat); |
| 301 return NULL; | |
| 302 | |
| 303 scoped_ptr<gfx::GpuMemoryBuffer> buffer( | |
| 304 channel_->factory()->AllocateGpuMemoryBuffer( | |
| 305 width, height, internalformat, usage)); | |
| 306 if (!buffer) | |
| 307 return NULL; | |
| 308 | |
| 309 DCHECK(GpuChannelHost::IsValidGpuMemoryBuffer(buffer->GetHandle())); | |
| 310 | |
| 311 int32 new_id = channel_->ReserveGpuMemoryBufferId(); | |
| 312 | |
| 313 // This handle is owned by the GPU process and must be passed to it or it | |
| 314 // will leak. In otherwords, do not early out on error between here and the | |
| 315 // sending of the RegisterGpuMemoryBuffer IPC below. | |
| 316 gfx::GpuMemoryBufferHandle handle = | |
| 317 channel_->ShareGpuMemoryBufferToGpuProcess(buffer->GetHandle()); | |
| 318 | |
| 319 if (!Send(new GpuCommandBufferMsg_RegisterGpuMemoryBuffer( | |
| 320 route_id_, | |
| 321 new_id, | |
| 322 handle, | |
| 323 width, | |
| 324 height, | |
| 325 internalformat))) { | |
| 326 return NULL; | |
| 327 } | |
| 328 | |
| 329 *id = new_id; | |
| 330 DCHECK(gpu_memory_buffers_.find(new_id) == gpu_memory_buffers_.end()); | |
| 331 return gpu_memory_buffers_.add(new_id, buffer.Pass()).first->second; | |
| 332 } | |
| 333 | |
| 334 void CommandBufferProxyImpl::DestroyGpuMemoryBuffer(int32 id) { | |
| 335 if (last_state_.error != gpu::error::kNoError) | |
| 336 return; | |
| 337 | |
| 338 Send(new GpuCommandBufferMsg_UnregisterGpuMemoryBuffer(route_id_, id)); | |
| 339 | |
| 340 // Remove the gpu memory buffer from the client side cache. | |
| 341 DCHECK(gpu_memory_buffers_.find(id) != gpu_memory_buffers_.end()); | |
| 342 gpu_memory_buffers_.take(id); | |
| 343 } | 408 } |
| 344 | 409 |
| 345 int CommandBufferProxyImpl::GetRouteID() const { | 410 int CommandBufferProxyImpl::GetRouteID() const { |
| 346 return route_id_; | 411 return route_id_; |
| 347 } | 412 } |
| 348 | 413 |
| 349 uint32 CommandBufferProxyImpl::CreateStreamTexture(uint32 texture_id) { | 414 uint32 CommandBufferProxyImpl::CreateStreamTexture(uint32 texture_id) { |
| 350 if (last_state_.error != gpu::error::kNoError) | 415 if (last_state_.error != gpu::error::kNoError) |
| 351 return 0; | 416 return 0; |
| 352 | 417 |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 if (last_state_.error == gpu::error::kNoError) | 563 if (last_state_.error == gpu::error::kNoError) |
| 499 shared_state()->Read(&last_state_); | 564 shared_state()->Read(&last_state_); |
| 500 } | 565 } |
| 501 | 566 |
| 502 gpu::CommandBufferSharedState* CommandBufferProxyImpl::shared_state() const { | 567 gpu::CommandBufferSharedState* CommandBufferProxyImpl::shared_state() const { |
| 503 return reinterpret_cast<gpu::CommandBufferSharedState*>( | 568 return reinterpret_cast<gpu::CommandBufferSharedState*>( |
| 504 shared_state_shm_->memory()); | 569 shared_state_shm_->memory()); |
| 505 } | 570 } |
| 506 | 571 |
| 507 } // namespace content | 572 } // namespace content |
| OLD | NEW |