Add a string for translation.
[chromium-blink-merge.git] / mojo / gles2 / command_buffer_client_impl.cc
bloba08f1b9cc29ffb3052ac540240bc09e0411ea444
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
3 // found in the LICENSE file.
5 #include "mojo/gles2/command_buffer_client_impl.h"
7 #include <limits>
9 #include "base/logging.h"
10 #include "base/process/process_handle.h"
11 #include "mojo/public/cpp/bindings/allocation_scope.h"
12 #include "mojo/public/cpp/bindings/sync_dispatcher.h"
13 #include "mojo/services/gles2/command_buffer_type_conversions.h"
14 #include "mojo/services/gles2/mojo_buffer_backing.h"
16 namespace mojo {
17 namespace gles2 {
19 namespace {
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());
30 result = mojo::DuplicateBuffer(handle->get(), NULL, duped);
31 if (result != MOJO_RESULT_OK)
32 return false;
33 DCHECK(duped->is_valid());
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);
41 return true;
45 CommandBufferDelegate::~CommandBufferDelegate() {}
47 void CommandBufferDelegate::ContextLost() {}
48 void CommandBufferDelegate::DrawAnimationFrame() {}
50 CommandBufferClientImpl::CommandBufferClientImpl(
51 CommandBufferDelegate* delegate,
52 MojoAsyncWaiter* async_waiter,
53 ScopedCommandBufferHandle command_buffer_handle)
54 : delegate_(delegate),
55 command_buffer_(command_buffer_handle.Pass(), this, this, async_waiter),
56 shared_state_(NULL),
57 last_put_offset_(-1),
58 next_transfer_buffer_id_(0),
59 initialize_result_(false) {}
61 CommandBufferClientImpl::~CommandBufferClientImpl() {}
63 bool CommandBufferClientImpl::Initialize() {
64 const size_t kSharedStateSize = sizeof(gpu::CommandBufferSharedState);
65 void* memory = NULL;
66 mojo::ScopedSharedBufferHandle duped;
67 bool result = CreateMapAndDupSharedBuffer(
68 kSharedStateSize, &memory, &shared_state_handle_, &duped);
69 if (!result)
70 return false;
72 shared_state_ = static_cast<gpu::CommandBufferSharedState*>(memory);
74 shared_state()->Initialize();
76 InterfacePipe<CommandBufferSyncClient, NoInterface> sync_pipe;
77 sync_dispatcher_.reset(new SyncDispatcher<CommandBufferSyncClient>(
78 sync_pipe.handle_to_peer.Pass(), this));
79 AllocationScope scope;
80 command_buffer_->Initialize(sync_pipe.handle_to_self.Pass(), duped.Pass());
81 // Wait for DidInitialize to come on the sync client pipe.
82 if (!sync_dispatcher_->WaitAndDispatchOneMessage()) {
83 VLOG(1) << "Channel encountered error while creating command buffer";
84 return false;
86 return initialize_result_;
89 gpu::CommandBuffer::State CommandBufferClientImpl::GetState() {
90 MakeProgressAndUpdateState();
91 return last_state_;
94 gpu::CommandBuffer::State CommandBufferClientImpl::GetLastState() {
95 return last_state_;
98 int32 CommandBufferClientImpl::GetLastToken() {
99 TryUpdateState();
100 return last_state_.token;
103 void CommandBufferClientImpl::Flush(int32 put_offset) {
104 if (last_put_offset_ == put_offset)
105 return;
107 last_put_offset_ = put_offset;
108 command_buffer_->Flush(put_offset);
111 void CommandBufferClientImpl::WaitForTokenInRange(int32 start, int32 end) {
112 TryUpdateState();
113 while (!InRange(start, end, last_state_.token) &&
114 last_state_.error == gpu::error::kNoError) {
115 MakeProgressAndUpdateState();
116 TryUpdateState();
120 void CommandBufferClientImpl::WaitForGetOffsetInRange(int32 start, int32 end) {
121 TryUpdateState();
122 while (!InRange(start, end, last_state_.get_offset) &&
123 last_state_.error == gpu::error::kNoError) {
124 MakeProgressAndUpdateState();
125 TryUpdateState();
129 void CommandBufferClientImpl::SetGetBuffer(int32 shm_id) {
130 command_buffer_->SetGetBuffer(shm_id);
131 last_put_offset_ = -1;
134 scoped_refptr<gpu::Buffer> CommandBufferClientImpl::CreateTransferBuffer(
135 size_t size,
136 int32* id) {
137 if (size >= std::numeric_limits<uint32_t>::max())
138 return NULL;
140 void* memory = NULL;
141 mojo::ScopedSharedBufferHandle handle;
142 mojo::ScopedSharedBufferHandle duped;
143 if (!CreateMapAndDupSharedBuffer(size, &memory, &handle, &duped))
144 return NULL;
146 *id = ++next_transfer_buffer_id_;
148 AllocationScope scope;
149 command_buffer_->RegisterTransferBuffer(
150 *id, duped.Pass(), static_cast<uint32_t>(size));
152 scoped_ptr<gpu::BufferBacking> backing(
153 new MojoBufferBacking(handle.Pass(), memory, size));
154 scoped_refptr<gpu::Buffer> buffer(new gpu::Buffer(backing.Pass()));
155 return buffer;
158 void CommandBufferClientImpl::DestroyTransferBuffer(int32 id) {
159 command_buffer_->DestroyTransferBuffer(id);
162 gpu::Capabilities CommandBufferClientImpl::GetCapabilities() {
163 // TODO(piman)
164 NOTIMPLEMENTED();
165 return gpu::Capabilities();
168 gfx::GpuMemoryBuffer* CommandBufferClientImpl::CreateGpuMemoryBuffer(
169 size_t width,
170 size_t height,
171 unsigned internalformat,
172 int32* id) {
173 // TODO(piman)
174 NOTIMPLEMENTED();
175 return NULL;
178 void CommandBufferClientImpl::DestroyGpuMemoryBuffer(int32 id) {
179 // TODO(piman)
180 NOTIMPLEMENTED();
183 uint32 CommandBufferClientImpl::InsertSyncPoint() {
184 // TODO(piman)
185 NOTIMPLEMENTED();
186 return 0;
189 void CommandBufferClientImpl::SignalSyncPoint(uint32 sync_point,
190 const base::Closure& callback) {
191 // TODO(piman)
192 NOTIMPLEMENTED();
195 void CommandBufferClientImpl::SignalQuery(uint32 query,
196 const base::Closure& callback) {
197 // TODO(piman)
198 NOTIMPLEMENTED();
201 void CommandBufferClientImpl::SetSurfaceVisible(bool visible) {
202 // TODO(piman)
203 NOTIMPLEMENTED();
206 void CommandBufferClientImpl::SendManagedMemoryStats(
207 const gpu::ManagedMemoryStats& stats) {
208 // TODO(piman)
209 NOTIMPLEMENTED();
212 void CommandBufferClientImpl::Echo(const base::Closure& callback) {
213 command_buffer_->Echo(callback);
216 uint32 CommandBufferClientImpl::CreateStreamTexture(uint32 texture_id) {
217 // TODO(piman)
218 NOTIMPLEMENTED();
219 return 0;
222 void CommandBufferClientImpl::RequestAnimationFrames() {
223 command_buffer_->RequestAnimationFrames();
226 void CommandBufferClientImpl::CancelAnimationFrames() {
227 command_buffer_->CancelAnimationFrames();
230 void CommandBufferClientImpl::DidInitialize(bool success) {
231 initialize_result_ = success;
234 void CommandBufferClientImpl::DidMakeProgress(const CommandBufferState& state) {
235 if (state.generation() - last_state_.generation < 0x80000000U)
236 last_state_ = state;
239 void CommandBufferClientImpl::DidDestroy() {
240 LostContext(gpu::error::kUnknown);
243 void CommandBufferClientImpl::LostContext(int32_t lost_reason) {
244 last_state_.error = gpu::error::kLostContext;
245 last_state_.context_lost_reason =
246 static_cast<gpu::error::ContextLostReason>(lost_reason);
247 delegate_->ContextLost();
250 void CommandBufferClientImpl::OnError() { LostContext(gpu::error::kUnknown); }
252 void CommandBufferClientImpl::TryUpdateState() {
253 if (last_state_.error == gpu::error::kNoError)
254 shared_state()->Read(&last_state_);
257 void CommandBufferClientImpl::MakeProgressAndUpdateState() {
258 command_buffer_->MakeProgress(last_state_.get_offset);
259 if (!sync_dispatcher_->WaitAndDispatchOneMessage()) {
260 VLOG(1) << "Channel encountered error while waiting for command buffer";
261 // TODO(piman): is it ok for this to re-enter?
262 DidDestroy();
263 return;
267 void CommandBufferClientImpl::DrawAnimationFrame() {
268 delegate_->DrawAnimationFrame();
271 } // namespace gles2
272 } // namespace mojo