blob: 6c3f5cc19a60d97efa343f8f3b26c3d7a478838f [file] [log] [blame]
[email protected]246a70452010-03-05 21:53:501// Copyright (c) 2010 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.
4
5#if defined(ENABLE_GPU)
6
7#include "base/process_util.h"
8#include "base/shared_memory.h"
9#include "chrome/common/gpu_messages.h"
10#include "chrome/gpu/gpu_channel.h"
11#include "chrome/gpu/gpu_command_buffer_stub.h"
12
13using gpu::Buffer;
14
15GpuCommandBufferStub::GpuCommandBufferStub(GpuChannel* channel,
16 int32 route_id)
17 : channel_(channel),
18 route_id_(route_id) {
19}
20
21GpuCommandBufferStub::~GpuCommandBufferStub() {
22}
23
24void GpuCommandBufferStub::OnMessageReceived(const IPC::Message& message) {
25 IPC_BEGIN_MESSAGE_MAP(GpuCommandBufferStub, message)
26 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_Initialize, OnInitialize);
27 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_GetState, OnGetState);
28 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_AsyncGetState, OnAsyncGetState);
29 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_Flush, OnFlush);
30 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_AsyncFlush, OnAsyncFlush);
31 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_CreateTransferBuffer,
32 OnCreateTransferBuffer);
33 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_DestroyTransferBuffer,
34 OnDestroyTransferBuffer);
35 IPC_MESSAGE_HANDLER(GpuCommandBufferMsg_GetTransferBuffer,
36 OnGetTransferBuffer);
37 IPC_MESSAGE_UNHANDLED_ERROR()
38 IPC_END_MESSAGE_MAP()
39}
40
41bool GpuCommandBufferStub::Send(IPC::Message* message) {
42 return channel_->Send(message);
43}
44
45void GpuCommandBufferStub::OnInitialize(
46 int32 size,
47 base::SharedMemoryHandle* ring_buffer) {
48 DCHECK(!command_buffer_.get());
49
50 *ring_buffer = base::SharedMemory::NULLHandle();
51
52 command_buffer_.reset(new gpu::CommandBufferService);
53
54 // Initialize the CommandBufferService and GPUProcessor.
55 if (command_buffer_->Initialize(size)) {
56 Buffer buffer = command_buffer_->GetRingBuffer();
57 if (buffer.shared_memory) {
58 processor_ = new gpu::GPUProcessor(command_buffer_.get());
59 if (processor_->Initialize(gfx::kNullPluginWindow)) {
60 command_buffer_->SetPutOffsetChangeCallback(
61 NewCallback(processor_.get(),
62 &gpu::GPUProcessor::ProcessCommands));
63
64 // Assume service is responsible for duplicating the handle from the
65 // calling process.
66 buffer.shared_memory->ShareToProcess(channel_->renderer_handle(),
67 ring_buffer);
68 } else {
69 processor_ = NULL;
70 command_buffer_.reset();
71 }
72 }
73 }
74}
75
76void GpuCommandBufferStub::OnGetState(gpu::CommandBuffer::State* state) {
77 *state = command_buffer_->GetState();
78}
79
80void GpuCommandBufferStub::OnAsyncGetState() {
81 gpu::CommandBuffer::State state = command_buffer_->GetState();
82 Send(new GpuCommandBufferMsg_UpdateState(route_id_, state));
83}
84
85void GpuCommandBufferStub::OnFlush(int32 put_offset,
86 gpu::CommandBuffer::State* state) {
87 *state = command_buffer_->Flush(put_offset);
88}
89
90void GpuCommandBufferStub::OnAsyncFlush(int32 put_offset) {
91 gpu::CommandBuffer::State state = command_buffer_->Flush(put_offset);
92 Send(new GpuCommandBufferMsg_UpdateState(route_id_, state));
93}
94
95void GpuCommandBufferStub::OnCreateTransferBuffer(int32 size, int32* id) {
96 *id = command_buffer_->CreateTransferBuffer(size);
97}
98
99void GpuCommandBufferStub::OnDestroyTransferBuffer(int32 id) {
100 command_buffer_->DestroyTransferBuffer(id);
101}
102
103void GpuCommandBufferStub::OnGetTransferBuffer(
104 int32 id,
105 base::SharedMemoryHandle* transfer_buffer,
106 uint32* size) {
107 *transfer_buffer = base::SharedMemoryHandle();
108 *size = 0;
109
110 Buffer buffer = command_buffer_->GetTransferBuffer(id);
111 if (buffer.shared_memory) {
112 // Assume service is responsible for duplicating the handle to the calling
113 // process.
114 buffer.shared_memory->ShareToProcess(channel_->renderer_handle(),
115 transfer_buffer);
116 *size = buffer.shared_memory->max_size();
117 }
118}
119
120#endif // ENABLE_GPU