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/trace_event/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
.get_offset
= get_offset_
;
45 state
.context_lost_reason
= context_lost_reason_
;
46 state
.generation
= ++generation_
;
51 int32
CommandBufferService::GetLastToken() {
52 return GetLastState().token
;
55 void CommandBufferService::UpdateState() {
57 CommandBufferService::State state
= GetLastState();
58 shared_state_
->Write(state
);
62 void CommandBufferService::WaitForTokenInRange(int32 start
, int32 end
) {
63 DCHECK(error_
!= error::kNoError
|| InRange(start
, end
, token_
));
66 void CommandBufferService::WaitForGetOffsetInRange(int32 start
, int32 end
) {
67 DCHECK(error_
!= error::kNoError
|| InRange(start
, end
, get_offset_
));
70 void CommandBufferService::Flush(int32 put_offset
) {
71 if (put_offset
< 0 || put_offset
> num_entries_
) {
72 error_
= gpu::error::kOutOfBounds
;
76 put_offset_
= put_offset
;
78 if (!put_offset_change_callback_
.is_null())
79 put_offset_change_callback_
.Run();
82 void CommandBufferService::OrderingBarrier(int32 put_offset
) {
86 void CommandBufferService::SetGetBuffer(int32 transfer_buffer_id
) {
87 DCHECK_EQ(-1, ring_buffer_id_
);
88 DCHECK_EQ(put_offset_
, get_offset_
); // Only if it's empty.
89 // If the buffer is invalid we handle it gracefully.
90 // This means ring_buffer_ can be NULL.
91 ring_buffer_
= GetTransferBuffer(transfer_buffer_id
);
92 ring_buffer_id_
= transfer_buffer_id
;
93 int32 size
= ring_buffer_
.get() ? ring_buffer_
->size() : 0;
94 num_entries_
= size
/ sizeof(CommandBufferEntry
);
97 if (!get_buffer_change_callback_
.is_null()) {
98 get_buffer_change_callback_
.Run(ring_buffer_id_
);
104 void CommandBufferService::SetSharedStateBuffer(
105 scoped_ptr
<BufferBacking
> shared_state_buffer
) {
106 shared_state_buffer_
= shared_state_buffer
.Pass();
107 DCHECK(shared_state_buffer_
->GetSize() >= sizeof(*shared_state_
));
110 static_cast<CommandBufferSharedState
*>(shared_state_buffer_
->GetMemory());
115 void CommandBufferService::SetGetOffset(int32 get_offset
) {
116 DCHECK(get_offset
>= 0 && get_offset
< num_entries_
);
117 get_offset_
= get_offset
;
120 scoped_refptr
<Buffer
> CommandBufferService::CreateTransferBuffer(size_t size
,
124 scoped_ptr
<SharedMemory
> shared_memory(new SharedMemory());
125 if (!shared_memory
->CreateAndMapAnonymous(size
))
128 static int32 next_id
= 1;
131 if (!RegisterTransferBuffer(
132 *id
, MakeBackingFromSharedMemory(shared_memory
.Pass(), size
))) {
137 return GetTransferBuffer(*id
);
140 void CommandBufferService::DestroyTransferBuffer(int32 id
) {
141 transfer_buffer_manager_
->DestroyTransferBuffer(id
);
142 if (id
== ring_buffer_id_
) {
143 ring_buffer_id_
= -1;
151 scoped_refptr
<Buffer
> CommandBufferService::GetTransferBuffer(int32 id
) {
152 return transfer_buffer_manager_
->GetTransferBuffer(id
);
155 bool CommandBufferService::RegisterTransferBuffer(
157 scoped_ptr
<BufferBacking
> buffer
) {
158 return transfer_buffer_manager_
->RegisterTransferBuffer(id
, buffer
.Pass());
161 void CommandBufferService::SetToken(int32 token
) {
166 void CommandBufferService::SetParseError(error::Error error
) {
167 if (error_
== error::kNoError
) {
169 if (!parse_error_callback_
.is_null())
170 parse_error_callback_
.Run();
174 void CommandBufferService::SetContextLostReason(
175 error::ContextLostReason reason
) {
176 context_lost_reason_
= reason
;
179 int32
CommandBufferService::GetPutOffset() {
183 void CommandBufferService::SetPutOffsetChangeCallback(
184 const base::Closure
& callback
) {
185 put_offset_change_callback_
= callback
;
188 void CommandBufferService::SetGetBufferChangeCallback(
189 const GetBufferChangedCallback
& callback
) {
190 get_buffer_change_callback_
= callback
;
193 void CommandBufferService::SetParseErrorCallback(
194 const base::Closure
& callback
) {
195 parse_error_callback_
= callback
;