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/sync_dispatcher.h" | |
12 #include "mojo/services/gles2/command_buffer_type_conversions.h" | 11 #include "mojo/services/gles2/command_buffer_type_conversions.h" |
13 #include "mojo/services/gles2/mojo_buffer_backing.h" | 12 #include "mojo/services/gles2/mojo_buffer_backing.h" |
14 | 13 |
15 namespace mojo { | 14 namespace mojo { |
16 namespace gles2 { | 15 namespace gles2 { |
17 | 16 |
18 namespace { | 17 namespace { |
19 | 18 |
20 bool CreateMapAndDupSharedBuffer(size_t size, | 19 bool CreateMapAndDupSharedBuffer(size_t size, |
21 void** memory, | 20 void** memory, |
(...skipping 10 matching lines...) Expand all Loading... |
32 DCHECK(duped->is_valid()); | 31 DCHECK(duped->is_valid()); |
33 | 32 |
34 result = mojo::MapBuffer( | 33 result = mojo::MapBuffer( |
35 handle->get(), 0, size, memory, MOJO_MAP_BUFFER_FLAG_NONE); | 34 handle->get(), 0, size, memory, MOJO_MAP_BUFFER_FLAG_NONE); |
36 if (result != MOJO_RESULT_OK) | 35 if (result != MOJO_RESULT_OK) |
37 return false; | 36 return false; |
38 DCHECK(*memory); | 37 DCHECK(*memory); |
39 | 38 |
40 return true; | 39 return true; |
41 } | 40 } |
42 } | 41 |
| 42 } // namespace |
43 | 43 |
44 CommandBufferDelegate::~CommandBufferDelegate() {} | 44 CommandBufferDelegate::~CommandBufferDelegate() {} |
45 | 45 |
46 void CommandBufferDelegate::ContextLost() {} | 46 void CommandBufferDelegate::ContextLost() {} |
47 void CommandBufferDelegate::DrawAnimationFrame() {} | 47 void CommandBufferDelegate::DrawAnimationFrame() {} |
48 | 48 |
| 49 class CommandBufferClientImpl::SyncClientImpl |
| 50 : public InterfaceImpl<CommandBufferSyncClient> { |
| 51 public: |
| 52 SyncClientImpl() : initialized_successfully_(false) {} |
| 53 |
| 54 bool WaitForInitialization() { |
| 55 if (!WaitForIncomingMethodCall()) |
| 56 return false; |
| 57 return initialized_successfully_; |
| 58 } |
| 59 |
| 60 CommandBufferStatePtr WaitForProgress() { |
| 61 if (!WaitForIncomingMethodCall()) |
| 62 return CommandBufferStatePtr(); |
| 63 return command_buffer_state_.Pass(); |
| 64 } |
| 65 |
| 66 private: |
| 67 // CommandBufferSyncClient methods: |
| 68 virtual void DidInitialize(bool success) OVERRIDE { |
| 69 initialized_successfully_ = success; |
| 70 } |
| 71 virtual void DidMakeProgress(CommandBufferStatePtr state) OVERRIDE { |
| 72 command_buffer_state_ = state.Pass(); |
| 73 } |
| 74 |
| 75 bool initialized_successfully_; |
| 76 CommandBufferStatePtr command_buffer_state_; |
| 77 }; |
| 78 |
49 CommandBufferClientImpl::CommandBufferClientImpl( | 79 CommandBufferClientImpl::CommandBufferClientImpl( |
50 CommandBufferDelegate* delegate, | 80 CommandBufferDelegate* delegate, |
51 const MojoAsyncWaiter* async_waiter, | 81 const MojoAsyncWaiter* async_waiter, |
52 ScopedMessagePipeHandle command_buffer_handle) | 82 ScopedMessagePipeHandle command_buffer_handle) |
53 : delegate_(delegate), | 83 : delegate_(delegate), |
54 shared_state_(NULL), | 84 shared_state_(NULL), |
55 last_put_offset_(-1), | 85 last_put_offset_(-1), |
56 next_transfer_buffer_id_(0), | 86 next_transfer_buffer_id_(0), |
57 initialize_result_(false), | |
58 async_waiter_(async_waiter) { | 87 async_waiter_(async_waiter) { |
59 command_buffer_.Bind(command_buffer_handle.Pass(), async_waiter); | 88 command_buffer_.Bind(command_buffer_handle.Pass(), async_waiter); |
60 command_buffer_.set_error_handler(this); | 89 command_buffer_.set_error_handler(this); |
61 command_buffer_.set_client(this); | 90 command_buffer_.set_client(this); |
62 } | 91 } |
63 | 92 |
64 CommandBufferClientImpl::~CommandBufferClientImpl() {} | 93 CommandBufferClientImpl::~CommandBufferClientImpl() {} |
65 | 94 |
66 bool CommandBufferClientImpl::Initialize() { | 95 bool CommandBufferClientImpl::Initialize() { |
67 const size_t kSharedStateSize = sizeof(gpu::CommandBufferSharedState); | 96 const size_t kSharedStateSize = sizeof(gpu::CommandBufferSharedState); |
68 void* memory = NULL; | 97 void* memory = NULL; |
69 mojo::ScopedSharedBufferHandle duped; | 98 mojo::ScopedSharedBufferHandle duped; |
70 bool result = CreateMapAndDupSharedBuffer( | 99 bool result = CreateMapAndDupSharedBuffer( |
71 kSharedStateSize, &memory, &shared_state_handle_, &duped); | 100 kSharedStateSize, &memory, &shared_state_handle_, &duped); |
72 if (!result) | 101 if (!result) |
73 return false; | 102 return false; |
74 | 103 |
75 shared_state_ = static_cast<gpu::CommandBufferSharedState*>(memory); | 104 shared_state_ = static_cast<gpu::CommandBufferSharedState*>(memory); |
76 | 105 |
77 shared_state()->Initialize(); | 106 shared_state()->Initialize(); |
78 | 107 |
79 // TODO(darin): We need better sugar for sync calls. | 108 CommandBufferSyncClientPtr sync_client; |
80 MessagePipe sync_pipe; | 109 sync_client_impl_.reset( |
81 sync_dispatcher_.reset(new SyncDispatcher<CommandBufferSyncClient>( | 110 BindToProxy(new SyncClientImpl(), &sync_client, async_waiter_)); |
82 sync_pipe.handle0.Pass(), this)); | 111 |
83 CommandBufferSyncClientPtr sync_client = | |
84 MakeProxy<CommandBufferSyncClient>(sync_pipe.handle1.Pass(), | |
85 async_waiter_); | |
86 command_buffer_->Initialize(sync_client.Pass(), duped.Pass()); | 112 command_buffer_->Initialize(sync_client.Pass(), duped.Pass()); |
| 113 |
87 // Wait for DidInitialize to come on the sync client pipe. | 114 // Wait for DidInitialize to come on the sync client pipe. |
88 if (!sync_dispatcher_->WaitAndDispatchOneMessage()) { | 115 if (!sync_client_impl_->WaitForInitialization()) { |
89 VLOG(1) << "Channel encountered error while creating command buffer"; | 116 VLOG(1) << "Channel encountered error while creating command buffer"; |
90 return false; | 117 return false; |
91 } | 118 } |
92 return initialize_result_; | 119 return true; |
93 } | 120 } |
94 | 121 |
95 gpu::CommandBuffer::State CommandBufferClientImpl::GetLastState() { | 122 gpu::CommandBuffer::State CommandBufferClientImpl::GetLastState() { |
96 return last_state_; | 123 return last_state_; |
97 } | 124 } |
98 | 125 |
99 int32 CommandBufferClientImpl::GetLastToken() { | 126 int32 CommandBufferClientImpl::GetLastToken() { |
100 TryUpdateState(); | 127 TryUpdateState(); |
101 return last_state_.token; | 128 return last_state_.token; |
102 } | 129 } |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 } | 253 } |
227 | 254 |
228 void CommandBufferClientImpl::RequestAnimationFrames() { | 255 void CommandBufferClientImpl::RequestAnimationFrames() { |
229 command_buffer_->RequestAnimationFrames(); | 256 command_buffer_->RequestAnimationFrames(); |
230 } | 257 } |
231 | 258 |
232 void CommandBufferClientImpl::CancelAnimationFrames() { | 259 void CommandBufferClientImpl::CancelAnimationFrames() { |
233 command_buffer_->CancelAnimationFrames(); | 260 command_buffer_->CancelAnimationFrames(); |
234 } | 261 } |
235 | 262 |
236 void CommandBufferClientImpl::DidInitialize(bool success) { | |
237 initialize_result_ = success; | |
238 } | |
239 | |
240 void CommandBufferClientImpl::DidMakeProgress(CommandBufferStatePtr state) { | |
241 if (state->generation - last_state_.generation < 0x80000000U) | |
242 last_state_ = state.To<State>(); | |
243 } | |
244 | |
245 void CommandBufferClientImpl::DidDestroy() { | 263 void CommandBufferClientImpl::DidDestroy() { |
246 LostContext(gpu::error::kUnknown); | 264 LostContext(gpu::error::kUnknown); |
247 } | 265 } |
248 | 266 |
249 void CommandBufferClientImpl::LostContext(int32_t lost_reason) { | 267 void CommandBufferClientImpl::LostContext(int32_t lost_reason) { |
250 last_state_.error = gpu::error::kLostContext; | 268 last_state_.error = gpu::error::kLostContext; |
251 last_state_.context_lost_reason = | 269 last_state_.context_lost_reason = |
252 static_cast<gpu::error::ContextLostReason>(lost_reason); | 270 static_cast<gpu::error::ContextLostReason>(lost_reason); |
253 delegate_->ContextLost(); | 271 delegate_->ContextLost(); |
254 } | 272 } |
255 | 273 |
| 274 void CommandBufferClientImpl::DrawAnimationFrame() { |
| 275 delegate_->DrawAnimationFrame(); |
| 276 } |
| 277 |
256 void CommandBufferClientImpl::OnConnectionError() { | 278 void CommandBufferClientImpl::OnConnectionError() { |
257 LostContext(gpu::error::kUnknown); | 279 LostContext(gpu::error::kUnknown); |
258 } | 280 } |
259 | 281 |
260 void CommandBufferClientImpl::TryUpdateState() { | 282 void CommandBufferClientImpl::TryUpdateState() { |
261 if (last_state_.error == gpu::error::kNoError) | 283 if (last_state_.error == gpu::error::kNoError) |
262 shared_state()->Read(&last_state_); | 284 shared_state()->Read(&last_state_); |
263 } | 285 } |
264 | 286 |
265 void CommandBufferClientImpl::MakeProgressAndUpdateState() { | 287 void CommandBufferClientImpl::MakeProgressAndUpdateState() { |
266 command_buffer_->MakeProgress(last_state_.get_offset); | 288 command_buffer_->MakeProgress(last_state_.get_offset); |
267 if (!sync_dispatcher_->WaitAndDispatchOneMessage()) { | 289 |
| 290 CommandBufferStatePtr state = sync_client_impl_->WaitForProgress(); |
| 291 if (!state.get()) { |
268 VLOG(1) << "Channel encountered error while waiting for command buffer"; | 292 VLOG(1) << "Channel encountered error while waiting for command buffer"; |
269 // TODO(piman): is it ok for this to re-enter? | 293 // TODO(piman): is it ok for this to re-enter? |
270 DidDestroy(); | 294 DidDestroy(); |
271 return; | 295 return; |
272 } | 296 } |
273 } | |
274 | 297 |
275 void CommandBufferClientImpl::DrawAnimationFrame() { | 298 if (state->generation - last_state_.generation < 0x80000000U) |
276 delegate_->DrawAnimationFrame(); | 299 last_state_ = state.To<State>(); |
277 } | 300 } |
278 | 301 |
279 } // namespace gles2 | 302 } // namespace gles2 |
280 } // namespace mojo | 303 } // namespace mojo |
OLD | NEW |