Don't add an aura tooltip to bubble close buttons on Windows.
[chromium-blink-merge.git] / gpu / command_buffer / service / command_buffer_service.cc
blobf2444b31e9f091fd7f67c9bd41f5bcd583b385ed
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 return NULL;
128 static int32 next_id = 1;
129 *id = next_id++;
131 if (!RegisterTransferBuffer(
132 *id, MakeBackingFromSharedMemory(shared_memory.Pass(), size))) {
133 *id = -1;
134 return NULL;
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;
144 ring_buffer_ = NULL;
145 num_entries_ = 0;
146 get_offset_ = 0;
147 put_offset_ = 0;
151 scoped_refptr<Buffer> CommandBufferService::GetTransferBuffer(int32 id) {
152 return transfer_buffer_manager_->GetTransferBuffer(id);
155 bool CommandBufferService::RegisterTransferBuffer(
156 int32 id,
157 scoped_ptr<BufferBacking> buffer) {
158 return transfer_buffer_manager_->RegisterTransferBuffer(id, buffer.Pass());
161 void CommandBufferService::SetToken(int32 token) {
162 token_ = token;
163 UpdateState();
166 void CommandBufferService::SetParseError(error::Error error) {
167 if (error_ == error::kNoError) {
168 error_ = error;
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() {
180 return put_offset_;
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;
198 } // namespace gpu