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
)) {
126 if (error_
== error::kNoError
)
127 error_
= gpu::error::kOutOfBounds
;
131 static int32 next_id
= 1;
134 if (!RegisterTransferBuffer(
135 *id
, MakeBackingFromSharedMemory(shared_memory
.Pass(), size
))) {
136 if (error_
== error::kNoError
)
137 error_
= gpu::error::kOutOfBounds
;
142 return GetTransferBuffer(*id
);
145 void CommandBufferService::DestroyTransferBuffer(int32 id
) {
146 transfer_buffer_manager_
->DestroyTransferBuffer(id
);
147 if (id
== ring_buffer_id_
) {
148 ring_buffer_id_
= -1;
156 scoped_refptr
<Buffer
> CommandBufferService::GetTransferBuffer(int32 id
) {
157 return transfer_buffer_manager_
->GetTransferBuffer(id
);
160 bool CommandBufferService::RegisterTransferBuffer(
162 scoped_ptr
<BufferBacking
> buffer
) {
163 return transfer_buffer_manager_
->RegisterTransferBuffer(id
, buffer
.Pass());
166 void CommandBufferService::SetToken(int32 token
) {
171 void CommandBufferService::SetParseError(error::Error error
) {
172 if (error_
== error::kNoError
) {
174 if (!parse_error_callback_
.is_null())
175 parse_error_callback_
.Run();
179 void CommandBufferService::SetContextLostReason(
180 error::ContextLostReason reason
) {
181 context_lost_reason_
= reason
;
184 int32
CommandBufferService::GetPutOffset() {
188 void CommandBufferService::SetPutOffsetChangeCallback(
189 const base::Closure
& callback
) {
190 put_offset_change_callback_
= callback
;
193 void CommandBufferService::SetGetBufferChangeCallback(
194 const GetBufferChangedCallback
& callback
) {
195 get_buffer_change_callback_
= callback
;
198 void CommandBufferService::SetParseErrorCallback(
199 const base::Closure
& callback
) {
200 parse_error_callback_
= callback
;