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 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 if (last_state_.error != gpu::error::kNoError) | 349 if (last_state_.error != gpu::error::kNoError) |
290 return; | 350 return; |
291 | 351 |
292 Send(new GpuCommandBufferMsg_DestroyTransferBuffer(route_id_, id)); | 352 Send(new GpuCommandBufferMsg_DestroyTransferBuffer(route_id_, id)); |
293 } | 353 } |
294 | 354 |
295 gpu::Capabilities CommandBufferProxyImpl::GetCapabilities() { | 355 gpu::Capabilities CommandBufferProxyImpl::GetCapabilities() { |
296 return capabilities_; | 356 return capabilities_; |
297 } | 357 } |
298 | 358 |
299 gfx::GpuMemoryBuffer* CommandBufferProxyImpl::CreateGpuMemoryBuffer( | 359 int32_t CommandBufferProxyImpl::CreateImage(ClientBuffer buffer, |
| 360 size_t width, |
| 361 size_t height, |
| 362 unsigned internalformat) { |
| 363 if (last_state_.error != gpu::error::kNoError) |
| 364 return -1; |
| 365 |
| 366 int32 new_id = channel_->ReserveImageId(); |
| 367 |
| 368 gfx::GpuMemoryBuffer* gpu_memory_buffer = |
| 369 channel_->gpu_memory_buffer_manager()->GpuMemoryBufferFromClientBuffer( |
| 370 buffer); |
| 371 DCHECK(gpu_memory_buffer); |
| 372 |
| 373 // This handle is owned by the GPU process and must be passed to it or it |
| 374 // will leak. In otherwords, do not early out on error between here and the |
| 375 // sending of the CreateImage IPC below. |
| 376 gfx::GpuMemoryBufferHandle handle = |
| 377 channel_->ShareGpuMemoryBufferToGpuProcess( |
| 378 gpu_memory_buffer->GetHandle()); |
| 379 |
| 380 DCHECK(IsImageFormatCompatibleWithGpuMemoryBufferFormat( |
| 381 gpu_memory_buffer->GetFormat(), internalformat)); |
| 382 if (!Send(new GpuCommandBufferMsg_CreateImage(route_id_, |
| 383 new_id, |
| 384 handle, |
| 385 gfx::Size(width, height), |
| 386 gpu_memory_buffer->GetFormat(), |
| 387 internalformat))) { |
| 388 return -1; |
| 389 } |
| 390 |
| 391 return new_id; |
| 392 } |
| 393 |
| 394 void CommandBufferProxyImpl::DestroyImage(int32 id) { |
| 395 if (last_state_.error != gpu::error::kNoError) |
| 396 return; |
| 397 |
| 398 Send(new GpuCommandBufferMsg_DestroyImage(route_id_, id)); |
| 399 } |
| 400 |
| 401 int32_t CommandBufferProxyImpl::CreateGpuMemoryBufferImage( |
300 size_t width, | 402 size_t width, |
301 size_t height, | 403 size_t height, |
302 unsigned internalformat, | 404 unsigned internalformat, |
303 unsigned usage, | 405 unsigned usage) { |
304 int32* id) { | 406 scoped_ptr<gfx::GpuMemoryBuffer> buffer( |
305 *id = -1; | 407 channel_->gpu_memory_buffer_manager()->AllocateGpuMemoryBuffer( |
| 408 gfx::Size(width, height), |
| 409 ImageFormatToGpuMemoryBufferFormat(internalformat), |
| 410 ImageUsageToGpuMemoryBufferUsage(usage))); |
| 411 if (!buffer) |
| 412 return -1; |
306 | 413 |
307 if (last_state_.error != gpu::error::kNoError) | 414 return CreateImage(buffer->AsClientBuffer(), width, height, internalformat); |
308 return NULL; | |
309 | |
310 scoped_ptr<gfx::GpuMemoryBuffer> buffer( | |
311 channel_->factory()->AllocateGpuMemoryBuffer( | |
312 width, height, internalformat, usage)); | |
313 if (!buffer) | |
314 return NULL; | |
315 | |
316 DCHECK(GpuChannelHost::IsValidGpuMemoryBuffer(buffer->GetHandle())); | |
317 | |
318 int32 new_id = channel_->ReserveGpuMemoryBufferId(); | |
319 | |
320 // This handle is owned by the GPU process and must be passed to it or it | |
321 // will leak. In otherwords, do not early out on error between here and the | |
322 // sending of the RegisterGpuMemoryBuffer IPC below. | |
323 gfx::GpuMemoryBufferHandle handle = | |
324 channel_->ShareGpuMemoryBufferToGpuProcess(buffer->GetHandle()); | |
325 | |
326 if (!Send(new GpuCommandBufferMsg_RegisterGpuMemoryBuffer( | |
327 route_id_, | |
328 new_id, | |
329 handle, | |
330 width, | |
331 height, | |
332 internalformat))) { | |
333 return NULL; | |
334 } | |
335 | |
336 *id = new_id; | |
337 DCHECK(gpu_memory_buffers_.find(new_id) == gpu_memory_buffers_.end()); | |
338 return gpu_memory_buffers_.add(new_id, buffer.Pass()).first->second; | |
339 } | |
340 | |
341 void CommandBufferProxyImpl::DestroyGpuMemoryBuffer(int32 id) { | |
342 if (last_state_.error != gpu::error::kNoError) | |
343 return; | |
344 | |
345 Send(new GpuCommandBufferMsg_UnregisterGpuMemoryBuffer(route_id_, id)); | |
346 | |
347 // Remove the gpu memory buffer from the client side cache. | |
348 DCHECK(gpu_memory_buffers_.find(id) != gpu_memory_buffers_.end()); | |
349 gpu_memory_buffers_.take(id); | |
350 } | 415 } |
351 | 416 |
352 int CommandBufferProxyImpl::GetRouteID() const { | 417 int CommandBufferProxyImpl::GetRouteID() const { |
353 return route_id_; | 418 return route_id_; |
354 } | 419 } |
355 | 420 |
356 uint32 CommandBufferProxyImpl::CreateStreamTexture(uint32 texture_id) { | 421 uint32 CommandBufferProxyImpl::CreateStreamTexture(uint32 texture_id) { |
357 if (last_state_.error != gpu::error::kNoError) | 422 if (last_state_.error != gpu::error::kNoError) |
358 return 0; | 423 return 0; |
359 | 424 |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 if (!ui::LatencyInfo::Verify( | 582 if (!ui::LatencyInfo::Verify( |
518 latency_info, "CommandBufferProxyImpl::OnSwapBuffersCompleted")) { | 583 latency_info, "CommandBufferProxyImpl::OnSwapBuffersCompleted")) { |
519 swap_buffers_completion_callback_.Run(std::vector<ui::LatencyInfo>()); | 584 swap_buffers_completion_callback_.Run(std::vector<ui::LatencyInfo>()); |
520 return; | 585 return; |
521 } | 586 } |
522 swap_buffers_completion_callback_.Run(latency_info); | 587 swap_buffers_completion_callback_.Run(latency_info); |
523 } | 588 } |
524 } | 589 } |
525 | 590 |
526 } // namespace content | 591 } // namespace content |
OLD | NEW |