1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/gles2/command_buffer_client_impl.h"
#include <stddef.h>
#include <stdint.h>
#include <limits>
#include <utility>
#include "base/logging.h"
#include "base/process/process_handle.h"
#include "base/threading/thread_restrictions.h"
#include "components/mus/gles2/command_buffer_type_conversions.h"
#include "components/mus/gles2/mojo_buffer_backing.h"
#include "components/mus/gles2/mojo_gpu_memory_buffer.h"
#include "gpu/command_buffer/common/sync_token.h"
#include "gpu/command_buffer/service/image_factory.h"
#include "mojo/platform_handle/platform_handle_functions.h"
namespace gles2 {
namespace {
bool CreateMapAndDupSharedBuffer(size_t size,
void** memory,
mojo::ScopedSharedBufferHandle* handle,
mojo::ScopedSharedBufferHandle* duped) {
MojoResult result = mojo::CreateSharedBuffer(NULL, size, handle);
if (result != MOJO_RESULT_OK)
return false;
DCHECK(handle->is_valid());
result = mojo::DuplicateBuffer(handle->get(), NULL, duped);
if (result != MOJO_RESULT_OK)
return false;
DCHECK(duped->is_valid());
result = mojo::MapBuffer(
handle->get(), 0, size, memory, MOJO_MAP_BUFFER_FLAG_NONE);
if (result != MOJO_RESULT_OK)
return false;
DCHECK(*memory);
return true;
}
template <typename T>
void Copy(T* output, T input) {
*output = std::move(input);
}
} // namespace
CommandBufferDelegate::~CommandBufferDelegate() {}
void CommandBufferDelegate::ContextLost() {}
CommandBufferClientImpl::CommandBufferClientImpl(
CommandBufferDelegate* delegate,
const std::vector<int32_t>& attribs,
const MojoAsyncWaiter* async_waiter,
mojo::ScopedMessagePipeHandle command_buffer_handle)
: delegate_(delegate),
attribs_(attribs),
observer_binding_(this),
command_buffer_id_(0),
shared_state_(NULL),
last_put_offset_(-1),
next_transfer_buffer_id_(0),
next_image_id_(0),
next_fence_sync_release_(1),
flushed_fence_sync_release_(0),
async_waiter_(async_waiter) {
command_buffer_.Bind(mojo::InterfacePtrInfo<mus::mojom::CommandBuffer>(
std::move(command_buffer_handle), 0u),
async_waiter);
command_buffer_.set_connection_error_handler(
[this]() { DidLoseContext(gpu::error::kUnknown); });
}
CommandBufferClientImpl::~CommandBufferClientImpl() {}
bool CommandBufferClientImpl::Initialize() {
const size_t kSharedStateSize = sizeof(gpu::CommandBufferSharedState);
void* memory = NULL;
mojo::ScopedSharedBufferHandle duped;
bool result = CreateMapAndDupSharedBuffer(
kSharedStateSize, &memory, &shared_state_handle_, &duped);
if (!result)
return false;
shared_state_ = static_cast<gpu::CommandBufferSharedState*>(memory);
shared_state()->Initialize();
mus::mojom::CommandBufferLostContextObserverPtr observer_ptr;
observer_binding_.Bind(GetProxy(&observer_ptr), async_waiter_);
mus::mojom::CommandBufferInfoPtr info;
command_buffer_->Initialize(
std::move(observer_ptr), std::move(duped),
mojo::Array<int32_t>::From(attribs_),
base::Bind(&Copy<mus::mojom::CommandBufferInfoPtr>, &info));
base::ThreadRestrictions::ScopedAllowWait wait;
if (!command_buffer_.WaitForIncomingResponse()) {
VLOG(1) << "Channel encountered error while creating command buffer.";
return false;
}
if (!info) {
VLOG(1) << "Command buffer cannot be initialized successfully.";
return false;
}
DCHECK_EQ(gpu::CommandBufferNamespace::MOJO, info->command_buffer_namespace);
command_buffer_id_ = info->command_buffer_id;
capabilities_ = info->capabilities.To<gpu::Capabilities>();
return true;
}
gpu::CommandBuffer::State CommandBufferClientImpl::GetLastState() {
return last_state_;
}
int32_t CommandBufferClientImpl::GetLastToken() {
TryUpdateState();
return last_state_.token;
}
void CommandBufferClientImpl::Flush(int32_t put_offset) {
if (last_put_offset_ == put_offset)
return;
last_put_offset_ = put_offset;
command_buffer_->Flush(put_offset);
flushed_fence_sync_release_ = next_fence_sync_release_ - 1;
}
void CommandBufferClientImpl::OrderingBarrier(int32_t put_offset) {
// TODO(jamesr): Implement this more efficiently.
Flush(put_offset);
}
void CommandBufferClientImpl::WaitForTokenInRange(int32_t start, int32_t end) {
TryUpdateState();
while (!InRange(start, end, last_state_.token) &&
last_state_.error == gpu::error::kNoError) {
MakeProgressAndUpdateState();
}
}
void CommandBufferClientImpl::WaitForGetOffsetInRange(int32_t start,
int32_t end) {
TryUpdateState();
while (!InRange(start, end, last_state_.get_offset) &&
last_state_.error == gpu::error::kNoError) {
MakeProgressAndUpdateState();
}
}
void CommandBufferClientImpl::SetGetBuffer(int32_t shm_id) {
command_buffer_->SetGetBuffer(shm_id);
last_put_offset_ = -1;
}
scoped_refptr<gpu::Buffer> CommandBufferClientImpl::CreateTransferBuffer(
size_t size,
int32_t* id) {
if (size >= std::numeric_limits<uint32_t>::max())
return NULL;
void* memory = NULL;
mojo::ScopedSharedBufferHandle handle;
mojo::ScopedSharedBufferHandle duped;
if (!CreateMapAndDupSharedBuffer(size, &memory, &handle, &duped)) {
if (last_state_.error == gpu::error::kNoError)
last_state_.error = gpu::error::kLostContext;
return NULL;
}
*id = ++next_transfer_buffer_id_;
command_buffer_->RegisterTransferBuffer(*id, std::move(duped),
static_cast<uint32_t>(size));
scoped_ptr<gpu::BufferBacking> backing(
new mus::MojoBufferBacking(std::move(handle), memory, size));
scoped_refptr<gpu::Buffer> buffer(new gpu::Buffer(std::move(backing)));
return buffer;
}
void CommandBufferClientImpl::DestroyTransferBuffer(int32_t id) {
command_buffer_->DestroyTransferBuffer(id);
}
gpu::Capabilities CommandBufferClientImpl::GetCapabilities() {
return capabilities_;
}
int32_t CommandBufferClientImpl::CreateImage(ClientBuffer buffer,
size_t width,
size_t height,
unsigned internalformat) {
int32_t new_id = ++next_image_id_;
mojo::SizePtr size = mojo::Size::New();
size->width = static_cast<int32_t>(width);
size->height = static_cast<int32_t>(height);
mus::MojoGpuMemoryBufferImpl* gpu_memory_buffer =
mus::MojoGpuMemoryBufferImpl::FromClientBuffer(buffer);
gfx::GpuMemoryBufferHandle handle = gpu_memory_buffer->GetHandle();
bool requires_sync_point = false;
if (handle.type != gfx::SHARED_MEMORY_BUFFER) {
requires_sync_point = true;
NOTIMPLEMENTED();
return -1;
}
base::SharedMemoryHandle dupd_handle =
base::SharedMemory::DuplicateHandle(handle.handle);
#if defined(OS_WIN)
HANDLE platform_handle = dupd_handle.GetHandle();
#else
int platform_handle = dupd_handle.fd;
#endif
MojoHandle mojo_handle = MOJO_HANDLE_INVALID;
MojoResult create_result = MojoCreatePlatformHandleWrapper(
platform_handle, &mojo_handle);
if (create_result != MOJO_RESULT_OK) {
NOTIMPLEMENTED();
return -1;
}
mojo::ScopedHandle scoped_handle;
scoped_handle.reset(mojo::Handle(mojo_handle));
command_buffer_->CreateImage(
new_id, std::move(scoped_handle), handle.type, std::move(size),
static_cast<int32_t>(gpu_memory_buffer->GetFormat()), internalformat);
if (requires_sync_point) {
NOTIMPLEMENTED();
// TODO(jam): need to support this if we support types other than
// SHARED_MEMORY_BUFFER.
//gpu_memory_buffer_manager->SetDestructionSyncPoint(gpu_memory_buffer,
// InsertSyncPoint());
}
return new_id;
}
void CommandBufferClientImpl::DestroyImage(int32_t id) {
command_buffer_->DestroyImage(id);
}
int32_t CommandBufferClientImpl::CreateGpuMemoryBufferImage(
size_t width,
size_t height,
unsigned internalformat,
unsigned usage) {
scoped_ptr<gfx::GpuMemoryBuffer> buffer(mus::MojoGpuMemoryBufferImpl::Create(
gfx::Size(static_cast<int>(width), static_cast<int>(height)),
gpu::ImageFactory::DefaultBufferFormatForImageFormat(internalformat),
gfx::BufferUsage::SCANOUT));
if (!buffer)
return -1;
return CreateImage(buffer->AsClientBuffer(), width, height, internalformat);
}
void CommandBufferClientImpl::SignalQuery(uint32_t query,
const base::Closure& callback) {
// TODO(piman)
NOTIMPLEMENTED();
}
void CommandBufferClientImpl::DidLoseContext(int32_t lost_reason) {
last_state_.error = gpu::error::kLostContext;
last_state_.context_lost_reason =
static_cast<gpu::error::ContextLostReason>(lost_reason);
delegate_->ContextLost();
}
void CommandBufferClientImpl::TryUpdateState() {
if (last_state_.error == gpu::error::kNoError)
shared_state()->Read(&last_state_);
}
void CommandBufferClientImpl::MakeProgressAndUpdateState() {
mus::mojom::CommandBufferStatePtr state;
command_buffer_->MakeProgress(
last_state_.get_offset,
base::Bind(&Copy<mus::mojom::CommandBufferStatePtr>, &state));
base::ThreadRestrictions::ScopedAllowWait wait;
if (!command_buffer_.WaitForIncomingResponse()) {
VLOG(1) << "Channel encountered error while waiting for command buffer.";
// TODO(piman): is it ok for this to re-enter?
DidLoseContext(gpu::error::kUnknown);
return;
}
if (state->generation - last_state_.generation < 0x80000000U)
last_state_ = state.To<State>();
}
void CommandBufferClientImpl::SetLock(base::Lock* lock) {
}
bool CommandBufferClientImpl::IsGpuChannelLost() {
// This is only possible for out-of-process command buffers.
return false;
}
void CommandBufferClientImpl::EnsureWorkVisible() {
// This is only relevant for out-of-process command buffers.
}
gpu::CommandBufferNamespace CommandBufferClientImpl::GetNamespaceID() const {
return gpu::CommandBufferNamespace::MOJO;
}
uint64_t CommandBufferClientImpl::GetCommandBufferID() const {
return command_buffer_id_;
}
int32_t CommandBufferClientImpl::GetExtraCommandBufferData() const {
return 0;
}
uint64_t CommandBufferClientImpl::GenerateFenceSyncRelease() {
return next_fence_sync_release_++;
}
bool CommandBufferClientImpl::IsFenceSyncRelease(uint64_t release) {
return release != 0 && release < next_fence_sync_release_;
}
bool CommandBufferClientImpl::IsFenceSyncFlushed(uint64_t release) {
return release != 0 && release <= flushed_fence_sync_release_;
}
bool CommandBufferClientImpl::IsFenceSyncFlushReceived(uint64_t release) {
return IsFenceSyncFlushed(release);
}
void CommandBufferClientImpl::SignalSyncToken(const gpu::SyncToken& sync_token,
const base::Closure& callback) {
// TODO(dyen)
NOTIMPLEMENTED();
}
bool CommandBufferClientImpl::CanWaitUnverifiedSyncToken(
const gpu::SyncToken* sync_token) {
// Right now, MOJO_LOCAL is only used by trusted code, so it is safe to wait
// on a sync token in MOJO_LOCAL command buffer.
if (sync_token->namespace_id() == gpu::CommandBufferNamespace::MOJO_LOCAL)
return true;
// It is also safe to wait on the same context.
if (sync_token->namespace_id() == gpu::CommandBufferNamespace::MOJO &&
sync_token->command_buffer_id() == GetCommandBufferID())
return true;
return false;
}
} // namespace gles2
|