Infobar material design refresh: bg color
[chromium-blink-merge.git] / gpu / command_buffer / service / command_buffer_service.cc
blob0ea4b54e2cfe77f84eb58b001cd2f7240633639c
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"
7 #include <limits>
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;
17 namespace gpu {
19 CommandBufferService::CommandBufferService(
20 TransferBufferManagerInterface* transfer_buffer_manager)
21 : ring_buffer_id_(-1),
22 shared_state_(NULL),
23 num_entries_(0),
24 get_offset_(0),
25 put_offset_(0),
26 transfer_buffer_manager_(transfer_buffer_manager),
27 token_(0),
28 generation_(0),
29 error_(error::kNoError),
30 context_lost_reason_(error::kUnknown) {
33 CommandBufferService::~CommandBufferService() {
36 bool CommandBufferService::Initialize() {
37 return true;
40 CommandBufferService::State CommandBufferService::GetLastState() {
41 State state;
42 state.get_offset = get_offset_;
43 state.token = token_;
44 state.error = error_;
45 state.context_lost_reason = context_lost_reason_;
46 state.generation = ++generation_;
48 return state;
51 int32 CommandBufferService::GetLastToken() {
52 return GetLastState().token;
55 void CommandBufferService::UpdateState() {
56 if (shared_state_) {
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;
73 return;
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) {
83 Flush(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);
95 put_offset_ = 0;
96 SetGetOffset(0);
97 if (!get_buffer_change_callback_.is_null()) {
98 get_buffer_change_callback_.Run(ring_buffer_id_);
101 UpdateState();
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_));
109 shared_state_ =
110 static_cast<CommandBufferSharedState*>(shared_state_buffer_->GetMemory());
112 UpdateState();
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,
121 int32* id) {
122 *id = -1;
124 scoped_ptr<SharedMemory> shared_memory(new SharedMemory());
125 if (!shared_memory->CreateAndMapAnonymous(size)) {
126 if (error_ == error::kNoError)
127 error_ = gpu::error::kOutOfBounds;
128 return NULL;
131 static int32 next_id = 1;
132 *id = next_id++;
134 if (!RegisterTransferBuffer(
135 *id, MakeBackingFromSharedMemory(shared_memory.Pass(), size))) {
136 if (error_ == error::kNoError)
137 error_ = gpu::error::kOutOfBounds;
138 *id = -1;
139 return NULL;
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;
149 ring_buffer_ = NULL;
150 num_entries_ = 0;
151 get_offset_ = 0;
152 put_offset_ = 0;
156 scoped_refptr<Buffer> CommandBufferService::GetTransferBuffer(int32 id) {
157 return transfer_buffer_manager_->GetTransferBuffer(id);
160 bool CommandBufferService::RegisterTransferBuffer(
161 int32 id,
162 scoped_ptr<BufferBacking> buffer) {
163 return transfer_buffer_manager_->RegisterTransferBuffer(id, buffer.Pass());
166 void CommandBufferService::SetToken(int32 token) {
167 token_ = token;
168 UpdateState();
171 void CommandBufferService::SetParseError(error::Error error) {
172 if (error_ == error::kNoError) {
173 error_ = error;
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() {
185 return put_offset_;
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;
203 } // namespace gpu