1 // Copyright (c) 2012 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 "gpu/command_buffer/service/command_buffer_service.h"
9 #include "base/logging.h"
10 #include "base/debug/trace_event.h"
11 #include "gpu/command_buffer/common/cmd_buffer_common.h"
12 #include "gpu/command_buffer/common/command_buffer_shared.h"
13 #include "gpu/command_buffer/service/transfer_buffer_manager.h"
15 using ::base::SharedMemory
;
19 CommandBufferService::CommandBufferService(
20 TransferBufferManagerInterface
* transfer_buffer_manager
)
21 : ring_buffer_id_(-1),
26 transfer_buffer_manager_(transfer_buffer_manager
),
29 error_(error::kNoError
),
30 context_lost_reason_(error::kUnknown
) {
33 CommandBufferService::~CommandBufferService() {
36 bool CommandBufferService::Initialize() {
40 CommandBufferService::State
CommandBufferService::GetLastState() {
42 state
.num_entries
= num_entries_
;
43 state
.get_offset
= get_offset_
;
44 state
.put_offset
= put_offset_
;
47 state
.context_lost_reason
= context_lost_reason_
;
48 state
.generation
= ++generation_
;
53 int32
CommandBufferService::GetLastToken() {
54 return GetLastState().token
;
57 void CommandBufferService::UpdateState() {
59 CommandBufferService::State state
= GetLastState();
60 shared_state_
->Write(state
);
64 void CommandBufferService::WaitForTokenInRange(int32 start
, int32 end
) {
65 DCHECK(error_
!= error::kNoError
|| InRange(start
, end
, token_
));
68 void CommandBufferService::WaitForGetOffsetInRange(int32 start
, int32 end
) {
69 DCHECK(error_
!= error::kNoError
|| InRange(start
, end
, get_offset_
));
72 void CommandBufferService::Flush(int32 put_offset
) {
73 if (put_offset
< 0 || put_offset
> num_entries_
) {
74 error_
= gpu::error::kOutOfBounds
;
78 put_offset_
= put_offset
;
80 if (!put_offset_change_callback_
.is_null())
81 put_offset_change_callback_
.Run();
84 void CommandBufferService::SetGetBuffer(int32 transfer_buffer_id
) {
85 DCHECK_EQ(-1, ring_buffer_id_
);
86 DCHECK_EQ(put_offset_
, get_offset_
); // Only if it's empty.
87 // If the buffer is invalid we handle it gracefully.
88 // This means ring_buffer_ can be NULL.
89 ring_buffer_
= GetTransferBuffer(transfer_buffer_id
);
90 ring_buffer_id_
= transfer_buffer_id
;
91 int32 size
= ring_buffer_
.get() ? ring_buffer_
->size() : 0;
92 num_entries_
= size
/ sizeof(CommandBufferEntry
);
95 if (!get_buffer_change_callback_
.is_null()) {
96 get_buffer_change_callback_
.Run(ring_buffer_id_
);
102 void CommandBufferService::SetSharedStateBuffer(
103 scoped_ptr
<BufferBacking
> shared_state_buffer
) {
104 shared_state_buffer_
= shared_state_buffer
.Pass();
105 DCHECK(shared_state_buffer_
->GetSize() >= sizeof(*shared_state_
));
108 static_cast<CommandBufferSharedState
*>(shared_state_buffer_
->GetMemory());
113 void CommandBufferService::SetGetOffset(int32 get_offset
) {
114 DCHECK(get_offset
>= 0 && get_offset
< num_entries_
);
115 get_offset_
= get_offset
;
118 scoped_refptr
<Buffer
> CommandBufferService::CreateTransferBuffer(size_t size
,
122 scoped_ptr
<SharedMemory
> shared_memory(new SharedMemory());
123 if (!shared_memory
->CreateAndMapAnonymous(size
))
126 static int32 next_id
= 1;
129 if (!RegisterTransferBuffer(
130 *id
, MakeBackingFromSharedMemory(shared_memory
.Pass(), size
))) {
135 return GetTransferBuffer(*id
);
138 void CommandBufferService::DestroyTransferBuffer(int32 id
) {
139 transfer_buffer_manager_
->DestroyTransferBuffer(id
);
140 if (id
== ring_buffer_id_
) {
141 ring_buffer_id_
= -1;
149 scoped_refptr
<Buffer
> CommandBufferService::GetTransferBuffer(int32 id
) {
150 return transfer_buffer_manager_
->GetTransferBuffer(id
);
153 bool CommandBufferService::RegisterTransferBuffer(
155 scoped_ptr
<BufferBacking
> buffer
) {
156 return transfer_buffer_manager_
->RegisterTransferBuffer(id
, buffer
.Pass());
159 void CommandBufferService::SetToken(int32 token
) {
164 void CommandBufferService::SetParseError(error::Error error
) {
165 if (error_
== error::kNoError
) {
167 if (!parse_error_callback_
.is_null())
168 parse_error_callback_
.Run();
172 void CommandBufferService::SetContextLostReason(
173 error::ContextLostReason reason
) {
174 context_lost_reason_
= reason
;
177 void CommandBufferService::SetPutOffsetChangeCallback(
178 const base::Closure
& callback
) {
179 put_offset_change_callback_
= callback
;
182 void CommandBufferService::SetGetBufferChangeCallback(
183 const GetBufferChangedCallback
& callback
) {
184 get_buffer_change_callback_
= callback
;
187 void CommandBufferService::SetParseErrorCallback(
188 const base::Closure
& callback
) {
189 parse_error_callback_
= callback
;