OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mojo/gles2/command_buffer_client_impl.h" | 5 #include "mojo/gles2/command_buffer_client_impl.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/process/process_handle.h" | 10 #include "base/process/process_handle.h" |
11 #include "mojo/public/cpp/bindings/allocation_scope.h" | 11 #include "mojo/public/cpp/bindings/allocation_scope.h" |
12 #include "mojo/public/cpp/bindings/sync_dispatcher.h" | 12 #include "mojo/public/cpp/bindings/sync_dispatcher.h" |
13 #include "mojo/services/gles2/command_buffer_type_conversions.h" | 13 #include "mojo/services/gles2/command_buffer_type_conversions.h" |
| 14 #include "mojo/services/gles2/mojo_buffer_backing.h" |
14 | 15 |
15 namespace mojo { | 16 namespace mojo { |
16 namespace gles2 { | 17 namespace gles2 { |
17 | 18 |
| 19 namespace { |
| 20 |
| 21 bool CreateMapAndDupSharedBuffer(size_t size, |
| 22 void** memory, |
| 23 mojo::ScopedSharedBufferHandle* handle, |
| 24 mojo::ScopedSharedBufferHandle* duped) { |
| 25 MojoResult result = mojo::CreateSharedBuffer(NULL, size, handle); |
| 26 if (result != MOJO_RESULT_OK) |
| 27 return false; |
| 28 DCHECK(handle->is_valid()); |
| 29 |
| 30 result = mojo::DuplicateBuffer(handle->get(), NULL, duped); |
| 31 if (result != MOJO_RESULT_OK) |
| 32 return false; |
| 33 DCHECK(duped->is_valid()); |
| 34 |
| 35 result = mojo::MapBuffer( |
| 36 handle->get(), 0, size, memory, MOJO_MAP_BUFFER_FLAG_NONE); |
| 37 if (result != MOJO_RESULT_OK) |
| 38 return false; |
| 39 DCHECK(*memory); |
| 40 |
| 41 return true; |
| 42 } |
| 43 } |
| 44 |
18 CommandBufferDelegate::~CommandBufferDelegate() {} | 45 CommandBufferDelegate::~CommandBufferDelegate() {} |
19 | 46 |
20 void CommandBufferDelegate::ContextLost() {} | 47 void CommandBufferDelegate::ContextLost() {} |
21 void CommandBufferDelegate::DrawAnimationFrame() {} | 48 void CommandBufferDelegate::DrawAnimationFrame() {} |
22 | 49 |
23 CommandBufferClientImpl::CommandBufferClientImpl( | 50 CommandBufferClientImpl::CommandBufferClientImpl( |
24 CommandBufferDelegate* delegate, | 51 CommandBufferDelegate* delegate, |
25 MojoAsyncWaiter* async_waiter, | 52 MojoAsyncWaiter* async_waiter, |
26 ScopedCommandBufferHandle command_buffer_handle) | 53 ScopedCommandBufferHandle command_buffer_handle) |
27 : delegate_(delegate), | 54 : delegate_(delegate), |
28 command_buffer_(command_buffer_handle.Pass(), this, this, async_waiter), | 55 command_buffer_(command_buffer_handle.Pass(), this, this, async_waiter), |
| 56 shared_state_(NULL), |
29 last_put_offset_(-1), | 57 last_put_offset_(-1), |
30 next_transfer_buffer_id_(0), | 58 next_transfer_buffer_id_(0), |
31 initialize_result_(false) {} | 59 initialize_result_(false) {} |
32 | 60 |
33 CommandBufferClientImpl::~CommandBufferClientImpl() {} | 61 CommandBufferClientImpl::~CommandBufferClientImpl() {} |
34 | 62 |
35 bool CommandBufferClientImpl::Initialize() { | 63 bool CommandBufferClientImpl::Initialize() { |
36 shared_state_shm_.reset(new base::SharedMemory); | 64 const size_t kSharedStateSize = sizeof(gpu::CommandBufferSharedState); |
37 if (!shared_state_shm_->CreateAndMapAnonymous( | 65 void* memory = NULL; |
38 sizeof(gpu::CommandBufferSharedState))) | 66 mojo::ScopedSharedBufferHandle duped; |
| 67 bool result = CreateMapAndDupSharedBuffer( |
| 68 kSharedStateSize, &memory, &shared_state_handle_, &duped); |
| 69 if (!result) |
39 return false; | 70 return false; |
40 | 71 |
41 base::SharedMemoryHandle handle; | 72 shared_state_ = static_cast<gpu::CommandBufferSharedState*>(memory); |
42 shared_state_shm_->ShareToProcess(base::GetCurrentProcessHandle(), &handle); | |
43 if (!base::SharedMemory::IsHandleValid(handle)) | |
44 return false; | |
45 | 73 |
46 shared_state()->Initialize(); | 74 shared_state()->Initialize(); |
47 | 75 |
48 InterfacePipe<CommandBufferSyncClient, NoInterface> sync_pipe; | 76 InterfacePipe<CommandBufferSyncClient, NoInterface> sync_pipe; |
49 sync_dispatcher_.reset(new SyncDispatcher<CommandBufferSyncClient>( | 77 sync_dispatcher_.reset(new SyncDispatcher<CommandBufferSyncClient>( |
50 sync_pipe.handle_to_peer.Pass(), this)); | 78 sync_pipe.handle_to_peer.Pass(), this)); |
51 AllocationScope scope; | 79 AllocationScope scope; |
52 command_buffer_->Initialize(sync_pipe.handle_to_self.Pass(), handle); | 80 command_buffer_->Initialize(sync_pipe.handle_to_self.Pass(), duped.Pass()); |
53 // Wait for DidInitialize to come on the sync client pipe. | 81 // Wait for DidInitialize to come on the sync client pipe. |
54 if (!sync_dispatcher_->WaitAndDispatchOneMessage()) { | 82 if (!sync_dispatcher_->WaitAndDispatchOneMessage()) { |
55 VLOG(1) << "Channel encountered error while creating command buffer"; | 83 VLOG(1) << "Channel encountered error while creating command buffer"; |
56 return false; | 84 return false; |
57 } | 85 } |
58 return initialize_result_; | 86 return initialize_result_; |
59 } | 87 } |
60 | 88 |
61 gpu::CommandBuffer::State CommandBufferClientImpl::GetState() { | 89 gpu::CommandBuffer::State CommandBufferClientImpl::GetState() { |
62 MakeProgressAndUpdateState(); | 90 MakeProgressAndUpdateState(); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 command_buffer_->SetGetBuffer(shm_id); | 130 command_buffer_->SetGetBuffer(shm_id); |
103 last_put_offset_ = -1; | 131 last_put_offset_ = -1; |
104 } | 132 } |
105 | 133 |
106 scoped_refptr<gpu::Buffer> CommandBufferClientImpl::CreateTransferBuffer( | 134 scoped_refptr<gpu::Buffer> CommandBufferClientImpl::CreateTransferBuffer( |
107 size_t size, | 135 size_t size, |
108 int32* id) { | 136 int32* id) { |
109 if (size >= std::numeric_limits<uint32_t>::max()) | 137 if (size >= std::numeric_limits<uint32_t>::max()) |
110 return NULL; | 138 return NULL; |
111 | 139 |
112 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory); | 140 void* memory = NULL; |
113 if (!shared_memory->CreateAndMapAnonymous(size)) | 141 mojo::ScopedSharedBufferHandle handle; |
114 return NULL; | 142 mojo::ScopedSharedBufferHandle duped; |
115 | 143 if (!CreateMapAndDupSharedBuffer(size, &memory, &handle, &duped)) |
116 base::SharedMemoryHandle handle; | |
117 shared_memory->ShareToProcess(base::GetCurrentProcessHandle(), &handle); | |
118 if (!base::SharedMemory::IsHandleValid(handle)) | |
119 return NULL; | 144 return NULL; |
120 | 145 |
121 *id = ++next_transfer_buffer_id_; | 146 *id = ++next_transfer_buffer_id_; |
122 | 147 |
123 AllocationScope scope; | 148 AllocationScope scope; |
124 command_buffer_->RegisterTransferBuffer( | 149 command_buffer_->RegisterTransferBuffer( |
125 *id, handle, static_cast<uint32_t>(size)); | 150 *id, duped.Pass(), static_cast<uint32_t>(size)); |
126 | 151 |
127 scoped_refptr<gpu::Buffer> buffer = | 152 scoped_ptr<gpu::BufferBacking> backing( |
128 gpu::MakeBufferFromSharedMemory(shared_memory.Pass(), size); | 153 new MojoBufferBacking(handle.Pass(), memory, size)); |
| 154 scoped_refptr<gpu::Buffer> buffer(new gpu::Buffer(backing.Pass())); |
129 return buffer; | 155 return buffer; |
130 } | 156 } |
131 | 157 |
132 void CommandBufferClientImpl::DestroyTransferBuffer(int32 id) { | 158 void CommandBufferClientImpl::DestroyTransferBuffer(int32 id) { |
133 command_buffer_->DestroyTransferBuffer(id); | 159 command_buffer_->DestroyTransferBuffer(id); |
134 } | 160 } |
135 | 161 |
136 gpu::Capabilities CommandBufferClientImpl::GetCapabilities() { | 162 gpu::Capabilities CommandBufferClientImpl::GetCapabilities() { |
137 // TODO(piman) | 163 // TODO(piman) |
138 NOTIMPLEMENTED(); | 164 NOTIMPLEMENTED(); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 return; | 263 return; |
238 } | 264 } |
239 } | 265 } |
240 | 266 |
241 void CommandBufferClientImpl::DrawAnimationFrame() { | 267 void CommandBufferClientImpl::DrawAnimationFrame() { |
242 delegate_->DrawAnimationFrame(); | 268 delegate_->DrawAnimationFrame(); |
243 } | 269 } |
244 | 270 |
245 } // namespace gles2 | 271 } // namespace gles2 |
246 } // namespace mojo | 272 } // namespace mojo |
OLD | NEW |