Don't add an aura tooltip to bubble close buttons on Windows.
[chromium-blink-merge.git] / gpu / command_buffer / service / gpu_scheduler.cc
blob8570fcded91f55baf56752ad555a95e9517679df
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/gpu_scheduler.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/compiler_specific.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/time/time.h"
12 #include "base/trace_event/trace_event.h"
13 #include "ui/gl/gl_bindings.h"
14 #include "ui/gl/gl_fence.h"
15 #include "ui/gl/gl_switches.h"
17 #if defined(OS_WIN)
18 #include "base/win/windows_version.h"
19 #endif
21 using ::base::SharedMemory;
23 namespace gpu {
25 #if defined(OS_WIN)
26 const int64 kRescheduleTimeOutDelay = 1000;
27 #endif
29 GpuScheduler::GpuScheduler(CommandBufferServiceBase* command_buffer,
30 AsyncAPIInterface* handler,
31 gles2::GLES2Decoder* decoder)
32 : command_buffer_(command_buffer),
33 handler_(handler),
34 decoder_(decoder),
35 unscheduled_count_(0),
36 rescheduled_count_(0),
37 was_preempted_(false),
38 reschedule_task_factory_(this) {}
40 GpuScheduler::~GpuScheduler() {
43 void GpuScheduler::PutChanged() {
44 TRACE_EVENT1(
45 "gpu", "GpuScheduler:PutChanged",
46 "decoder", decoder_ ? decoder_->GetLogger()->GetLogPrefix() : "None");
48 CommandBuffer::State state = command_buffer_->GetLastState();
50 // If there is no parser, exit.
51 if (!parser_.get()) {
52 DCHECK_EQ(state.get_offset, command_buffer_->GetPutOffset());
53 return;
56 parser_->set_put(command_buffer_->GetPutOffset());
57 if (state.error != error::kNoError)
58 return;
60 // One of the unschedule fence tasks might have unscheduled us.
61 if (!IsScheduled())
62 return;
64 base::TimeTicks begin_time(base::TimeTicks::Now());
65 error::Error error = error::kNoError;
66 if (decoder_)
67 decoder_->BeginDecoding();
68 while (!parser_->IsEmpty()) {
69 if (IsPreempted())
70 break;
72 DCHECK(IsScheduled());
74 error = parser_->ProcessCommands(CommandParser::kParseCommandsSlice);
76 if (error == error::kDeferCommandUntilLater) {
77 DCHECK_GT(unscheduled_count_, 0);
78 break;
81 // TODO(piman): various classes duplicate various pieces of state, leading
82 // to needlessly complex update logic. It should be possible to simply
83 // share the state across all of them.
84 command_buffer_->SetGetOffset(static_cast<int32>(parser_->get()));
86 if (error::IsError(error)) {
87 command_buffer_->SetContextLostReason(decoder_->GetContextLostReason());
88 command_buffer_->SetParseError(error);
89 break;
92 if (!command_processed_callback_.is_null())
93 command_processed_callback_.Run();
95 if (unscheduled_count_ > 0)
96 break;
99 if (decoder_) {
100 if (!error::IsError(error) && decoder_->WasContextLost()) {
101 command_buffer_->SetContextLostReason(decoder_->GetContextLostReason());
102 command_buffer_->SetParseError(error::kLostContext);
104 decoder_->EndDecoding();
105 decoder_->AddProcessingCommandsTime(base::TimeTicks::Now() - begin_time);
109 void GpuScheduler::SetScheduled(bool scheduled) {
110 TRACE_EVENT2("gpu", "GpuScheduler:SetScheduled", "this", this,
111 "new unscheduled_count_",
112 unscheduled_count_ + (scheduled? -1 : 1));
113 if (scheduled) {
114 // If the scheduler was rescheduled after a timeout, ignore the subsequent
115 // calls to SetScheduled when they eventually arrive until they are all
116 // accounted for.
117 if (rescheduled_count_ > 0) {
118 --rescheduled_count_;
119 return;
120 } else {
121 --unscheduled_count_;
124 DCHECK_GE(unscheduled_count_, 0);
126 if (unscheduled_count_ == 0) {
127 TRACE_EVENT_ASYNC_END1("gpu", "ProcessingSwap", this,
128 "GpuScheduler", this);
129 // When the scheduler transitions from the unscheduled to the scheduled
130 // state, cancel the task that would reschedule it after a timeout.
131 reschedule_task_factory_.InvalidateWeakPtrs();
133 if (!scheduling_changed_callback_.is_null())
134 scheduling_changed_callback_.Run(true);
136 } else {
137 ++unscheduled_count_;
138 if (unscheduled_count_ == 1) {
139 TRACE_EVENT_ASYNC_BEGIN1("gpu", "ProcessingSwap", this,
140 "GpuScheduler", this);
141 #if defined(OS_WIN)
142 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
143 // When the scheduler transitions from scheduled to unscheduled, post a
144 // delayed task that it will force it back into a scheduled state after
145 // a timeout. This should only be necessary on pre-Vista.
146 base::MessageLoop::current()->PostDelayedTask(
147 FROM_HERE,
148 base::Bind(&GpuScheduler::RescheduleTimeOut,
149 reschedule_task_factory_.GetWeakPtr()),
150 base::TimeDelta::FromMilliseconds(kRescheduleTimeOutDelay));
152 #endif
153 if (!scheduling_changed_callback_.is_null())
154 scheduling_changed_callback_.Run(false);
159 bool GpuScheduler::IsScheduled() {
160 return unscheduled_count_ == 0;
163 bool GpuScheduler::HasMoreWork() {
164 return (decoder_ && decoder_->ProcessPendingQueries(false)) ||
165 HasMoreIdleWork();
168 void GpuScheduler::SetSchedulingChangedCallback(
169 const SchedulingChangedCallback& callback) {
170 scheduling_changed_callback_ = callback;
173 scoped_refptr<Buffer> GpuScheduler::GetSharedMemoryBuffer(int32 shm_id) {
174 return command_buffer_->GetTransferBuffer(shm_id);
177 void GpuScheduler::set_token(int32 token) {
178 command_buffer_->SetToken(token);
181 bool GpuScheduler::SetGetBuffer(int32 transfer_buffer_id) {
182 scoped_refptr<Buffer> ring_buffer =
183 command_buffer_->GetTransferBuffer(transfer_buffer_id);
184 if (!ring_buffer.get()) {
185 return false;
188 if (!parser_.get()) {
189 parser_.reset(new CommandParser(handler_));
192 parser_->SetBuffer(
193 ring_buffer->memory(), ring_buffer->size(), 0, ring_buffer->size());
195 SetGetOffset(0);
196 return true;
199 bool GpuScheduler::SetGetOffset(int32 offset) {
200 if (parser_->set_get(offset)) {
201 command_buffer_->SetGetOffset(static_cast<int32>(parser_->get()));
202 return true;
204 return false;
207 int32 GpuScheduler::GetGetOffset() {
208 return parser_->get();
211 void GpuScheduler::SetCommandProcessedCallback(
212 const base::Closure& callback) {
213 command_processed_callback_ = callback;
216 bool GpuScheduler::IsPreempted() {
217 if (!preemption_flag_.get())
218 return false;
220 if (!was_preempted_ && preemption_flag_->IsSet()) {
221 TRACE_COUNTER_ID1("gpu", "GpuScheduler::Preempted", this, 1);
222 was_preempted_ = true;
223 } else if (was_preempted_ && !preemption_flag_->IsSet()) {
224 TRACE_COUNTER_ID1("gpu", "GpuScheduler::Preempted", this, 0);
225 was_preempted_ = false;
228 return preemption_flag_->IsSet();
231 bool GpuScheduler::HasMoreIdleWork() {
232 return (decoder_ && decoder_->HasMoreIdleWork());
235 void GpuScheduler::PerformIdleWork() {
236 if (!decoder_)
237 return;
238 decoder_->PerformIdleWork();
241 void GpuScheduler::RescheduleTimeOut() {
242 int new_count = unscheduled_count_ + rescheduled_count_;
244 rescheduled_count_ = 0;
246 while (unscheduled_count_)
247 SetScheduled(true);
249 rescheduled_count_ = new_count;
252 } // namespace gpu