OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/common/gpu/gpu_channel.h" | 5 #include "content/common/gpu/gpu_channel.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <windows.h> | 8 #include <windows.h> |
9 #endif | 9 #endif |
10 | 10 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 const int64 kMaxPreemptTimeMs = kVsyncIntervalMs; | 65 const int64 kMaxPreemptTimeMs = kVsyncIntervalMs; |
66 | 66 |
67 // Stop the preemption once the time for the longest pending IPC drops | 67 // Stop the preemption once the time for the longest pending IPC drops |
68 // below this threshold. | 68 // below this threshold. |
69 const int64 kStopPreemptThresholdMs = kVsyncIntervalMs; | 69 const int64 kStopPreemptThresholdMs = kVsyncIntervalMs; |
70 | 70 |
71 const uint32_t kOutOfOrderNumber = static_cast<uint32_t>(-1); | 71 const uint32_t kOutOfOrderNumber = static_cast<uint32_t>(-1); |
72 | 72 |
73 } // anonymous namespace | 73 } // anonymous namespace |
74 | 74 |
75 struct GpuChannelMessage { | 75 scoped_refptr<GpuChannelMessageQueue> GpuChannelMessageQueue::Create( |
76 uint32_t order_number; | 76 const base::WeakPtr<GpuChannel>& gpu_channel, |
77 base::TimeTicks time_received; | 77 base::SingleThreadTaskRunner* task_runner) { |
78 IPC::Message message; | 78 return new GpuChannelMessageQueue(gpu_channel, task_runner); |
79 } | |
79 | 80 |
80 // TODO(dyen): Temporary sync point data, remove once new sync point lands. | 81 GpuChannelMessageQueue::GpuChannelMessageQueue( |
81 bool retire_sync_point; | 82 const base::WeakPtr<GpuChannel>& gpu_channel, |
82 uint32 sync_point_number; | 83 base::SingleThreadTaskRunner* task_runner) |
84 : enabled_(true), | |
85 unprocessed_order_num_(0), | |
86 processed_order_num_(0), | |
87 gpu_channel_(gpu_channel), | |
88 task_runner_(task_runner) {} | |
83 | 89 |
84 GpuChannelMessage(uint32_t order_num, const IPC::Message& msg) | 90 GpuChannelMessageQueue::~GpuChannelMessageQueue() { |
85 : order_number(order_num), | 91 DCHECK(channel_messages_.empty()); |
86 time_received(base::TimeTicks::Now()), | 92 DCHECK(out_of_order_messages_.empty()); |
87 message(msg), | 93 } |
88 retire_sync_point(false), | |
89 sync_point_number(0) {} | |
90 }; | |
91 | 94 |
92 class GpuChannelMessageQueue | 95 uint32_t GpuChannelMessageQueue::GetUnprocessedOrderNum() const { |
93 : public base::RefCountedThreadSafe<GpuChannelMessageQueue> { | 96 base::AutoLock auto_lock(channel_messages_lock_); |
94 public: | 97 return unprocessed_order_num_; |
95 static scoped_refptr<GpuChannelMessageQueue> Create( | 98 } |
96 base::WeakPtr<GpuChannel> gpu_channel, | 99 |
97 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | 100 void GpuChannelMessageQueue::PushBackMessage(uint32_t order_number, |
98 return new GpuChannelMessageQueue(gpu_channel, task_runner); | 101 const IPC::Message& message) { |
102 base::AutoLock auto_lock(channel_messages_lock_); | |
103 if (enabled_) { | |
104 PushMessageHelper( | |
105 make_scoped_ptr(new GpuChannelMessage(order_number, message))); | |
106 } | |
107 } | |
108 | |
109 bool GpuChannelMessageQueue::GenerateSyncPointMessage( | |
110 gpu::SyncPointManager* sync_point_manager, | |
111 uint32_t order_number, | |
112 const IPC::Message& message, | |
113 bool retire_sync_point, | |
114 uint32_t* sync_point) { | |
115 DCHECK_EQ(GpuCommandBufferMsg_InsertSyncPoint::ID, message.type()); | |
116 DCHECK(sync_point); | |
117 base::AutoLock auto_lock(channel_messages_lock_); | |
118 if (enabled_) { | |
119 *sync_point = sync_point_manager->GenerateSyncPoint(); | |
120 | |
121 scoped_ptr<GpuChannelMessage> msg = | |
dcheng
2015/09/17 00:41:31
Forgot to add: here and elsewhere, just do
scoped_
sunnyps
2015/09/18 00:35:55
Done.
| |
122 make_scoped_ptr(new GpuChannelMessage(order_number, message)); | |
123 msg->retire_sync_point = retire_sync_point; | |
124 msg->sync_point = *sync_point; | |
125 | |
126 PushMessageHelper(msg.Pass()); | |
127 return true; | |
128 } | |
129 return false; | |
130 } | |
131 | |
132 bool GpuChannelMessageQueue::HasQueuedMessages() const { | |
133 base::AutoLock auto_lock(channel_messages_lock_); | |
134 return HasQueuedMessagesHelper(); | |
135 } | |
136 | |
137 base::TimeTicks GpuChannelMessageQueue::GetNextMessageTimeTick() const { | |
138 base::AutoLock auto_lock(channel_messages_lock_); | |
139 | |
140 base::TimeTicks next_message_tick; | |
141 if (!channel_messages_.empty()) | |
142 next_message_tick = channel_messages_.front()->time_received; | |
143 | |
144 base::TimeTicks next_out_of_order_tick; | |
145 if (!out_of_order_messages_.empty()) | |
146 next_out_of_order_tick = out_of_order_messages_.front()->time_received; | |
147 | |
148 if (next_message_tick.is_null()) | |
149 return next_out_of_order_tick; | |
150 else if (next_out_of_order_tick.is_null()) | |
151 return next_message_tick; | |
152 else | |
153 return std::min(next_message_tick, next_out_of_order_tick); | |
154 } | |
155 | |
156 GpuChannelMessage* GpuChannelMessageQueue::GetNextMessage() const { | |
157 base::AutoLock auto_lock(channel_messages_lock_); | |
158 if (!out_of_order_messages_.empty()) { | |
159 DCHECK_EQ(out_of_order_messages_.front()->order_number, kOutOfOrderNumber); | |
160 return out_of_order_messages_.front(); | |
161 } else if (!channel_messages_.empty()) { | |
162 DCHECK_GT(channel_messages_.front()->order_number, processed_order_num_); | |
163 DCHECK_LE(channel_messages_.front()->order_number, unprocessed_order_num_); | |
164 return channel_messages_.front(); | |
165 } else { | |
166 return nullptr; | |
167 } | |
168 } | |
169 | |
170 bool GpuChannelMessageQueue::MessageProcessed(uint32_t order_number) { | |
171 base::AutoLock auto_lock(channel_messages_lock_); | |
172 if (order_number != kOutOfOrderNumber) { | |
173 DCHECK(!channel_messages_.empty()); | |
174 scoped_ptr<GpuChannelMessage> msg = | |
David Yen
2015/09/17 17:04:27
Since all the scoped_ptrs here are just used in th
sunnyps
2015/09/17 18:31:39
I considered that but ScopedVector is a part of cc
| |
175 make_scoped_ptr(channel_messages_.front()); | |
176 channel_messages_.pop_front(); | |
177 DCHECK_EQ(order_number, msg->order_number); | |
178 processed_order_num_ = order_number; | |
179 } else { | |
180 DCHECK(!out_of_order_messages_.empty()); | |
181 scoped_ptr<GpuChannelMessage> msg = | |
182 make_scoped_ptr(out_of_order_messages_.front()); | |
183 out_of_order_messages_.pop_front(); | |
184 } | |
185 return HasQueuedMessagesHelper(); | |
186 } | |
187 | |
188 void GpuChannelMessageQueue::DeleteAndDisableMessages( | |
189 GpuChannelManager* gpu_channel_manager) { | |
190 { | |
191 base::AutoLock auto_lock(channel_messages_lock_); | |
192 DCHECK(enabled_); | |
193 enabled_ = false; | |
99 } | 194 } |
100 | 195 |
101 uint32_t GetUnprocessedOrderNum() { | 196 // We guarantee that the queues will no longer be modified after enabled_ |
102 base::AutoLock auto_lock(channel_messages_lock_); | 197 // is set to false, it is now safe to modify the queue without the lock. |
103 return unprocessed_order_num_; | 198 // All public facing modifying functions check enabled_ while all |
104 } | 199 // private modifying functions DCHECK(enabled_) to enforce this. |
105 | 200 while (!channel_messages_.empty()) { |
106 void PushBackMessage(uint32_t order_number, const IPC::Message& message) { | 201 scoped_ptr<GpuChannelMessage> msg = |
107 base::AutoLock auto_lock(channel_messages_lock_); | 202 make_scoped_ptr(channel_messages_.front()); |
108 if (enabled_) { | 203 channel_messages_.pop_front(); |
109 PushMessageHelper(order_number, | 204 // This needs to clean up both GpuCommandBufferMsg_InsertSyncPoint and |
110 new GpuChannelMessage(order_number, message)); | 205 // GpuCommandBufferMsg_RetireSyncPoint messages, safer to just check |
206 // if we have a sync point number here. | |
207 if (msg->sync_point) { | |
208 gpu_channel_manager->sync_point_manager()->RetireSyncPoint( | |
209 msg->sync_point); | |
111 } | 210 } |
112 } | 211 } |
212 STLDeleteElements(&out_of_order_messages_); | |
213 } | |
113 | 214 |
114 void PushOutOfOrderMessage(const IPC::Message& message) { | 215 void GpuChannelMessageQueue::ScheduleHandleMessage() { |
115 // These are pushed out of order so should not have any order messages. | 216 task_runner_->PostTask(FROM_HERE, |
116 base::AutoLock auto_lock(channel_messages_lock_); | 217 base::Bind(&GpuChannel::HandleMessage, gpu_channel_)); |
117 if (enabled_) { | 218 } |
118 PushOutOfOrderHelper(new GpuChannelMessage(kOutOfOrderNumber, message)); | 219 |
119 } | 220 void GpuChannelMessageQueue::PushMessageHelper( |
221 scoped_ptr<GpuChannelMessage> msg) { | |
David Yen
2015/09/17 17:04:27
This scoped_ptr doesn't seem necessary, all the ca
sunnyps
2015/09/17 18:31:38
Conceptually this function takes ownership of the
| |
222 channel_messages_lock_.AssertAcquired(); | |
223 DCHECK(enabled_); | |
224 bool had_messages = HasQueuedMessagesHelper(); | |
225 if (msg->order_number != kOutOfOrderNumber) { | |
226 unprocessed_order_num_ = msg->order_number; | |
227 channel_messages_.push_back(msg.release()); | |
228 } else { | |
229 out_of_order_messages_.push_back(msg.release()); | |
120 } | 230 } |
231 if (!had_messages) | |
232 ScheduleHandleMessage(); | |
233 } | |
121 | 234 |
122 bool GenerateSyncPointMessage(gpu::SyncPointManager* sync_point_manager, | 235 bool GpuChannelMessageQueue::HasQueuedMessagesHelper() const { |
123 uint32_t order_number, | 236 channel_messages_lock_.AssertAcquired(); |
124 const IPC::Message& message, | 237 return !channel_messages_.empty() || !out_of_order_messages_.empty(); |
125 bool retire_sync_point, | 238 } |
126 uint32_t* sync_point_number) { | |
127 DCHECK(message.type() == GpuCommandBufferMsg_InsertSyncPoint::ID); | |
128 base::AutoLock auto_lock(channel_messages_lock_); | |
129 if (enabled_) { | |
130 const uint32 sync_point = sync_point_manager->GenerateSyncPoint(); | |
131 | |
132 GpuChannelMessage* msg = new GpuChannelMessage(order_number, message); | |
133 msg->retire_sync_point = retire_sync_point; | |
134 msg->sync_point_number = sync_point; | |
135 | |
136 *sync_point_number = sync_point; | |
137 PushMessageHelper(order_number, msg); | |
138 return true; | |
139 } | |
140 return false; | |
141 } | |
142 | |
143 bool HasQueuedMessages() { | |
144 base::AutoLock auto_lock(channel_messages_lock_); | |
145 return HasQueuedMessagesLocked(); | |
146 } | |
147 | |
148 base::TimeTicks GetNextMessageTimeTick() { | |
149 base::AutoLock auto_lock(channel_messages_lock_); | |
150 | |
151 base::TimeTicks next_message_tick; | |
152 if (!channel_messages_.empty()) | |
153 next_message_tick = channel_messages_.front()->time_received; | |
154 | |
155 base::TimeTicks next_out_of_order_tick; | |
156 if (!out_of_order_messages_.empty()) | |
157 next_out_of_order_tick = out_of_order_messages_.front()->time_received; | |
158 | |
159 if (next_message_tick.is_null()) | |
160 return next_out_of_order_tick; | |
161 else if (next_out_of_order_tick.is_null()) | |
162 return next_message_tick; | |
163 else | |
164 return std::min(next_message_tick, next_out_of_order_tick); | |
165 } | |
166 | |
167 protected: | |
168 virtual ~GpuChannelMessageQueue() { | |
169 DCHECK(channel_messages_.empty()); | |
170 DCHECK(out_of_order_messages_.empty()); | |
171 } | |
172 | |
173 private: | |
174 friend class GpuChannel; | |
175 friend class base::RefCountedThreadSafe<GpuChannelMessageQueue>; | |
176 | |
177 GpuChannelMessageQueue( | |
178 base::WeakPtr<GpuChannel> gpu_channel, | |
179 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | |
180 : enabled_(true), | |
181 unprocessed_order_num_(0), | |
182 gpu_channel_(gpu_channel), | |
183 task_runner_(task_runner) {} | |
184 | |
185 void DeleteAndDisableMessages(GpuChannelManager* gpu_channel_manager) { | |
186 { | |
187 base::AutoLock auto_lock(channel_messages_lock_); | |
188 DCHECK(enabled_); | |
189 enabled_ = false; | |
190 } | |
191 | |
192 // We guarantee that the queues will no longer be modified after enabled_ | |
193 // is set to false, it is now safe to modify the queue without the lock. | |
194 // All public facing modifying functions check enabled_ while all | |
195 // private modifying functions DCHECK(enabled_) to enforce this. | |
196 while (!channel_messages_.empty()) { | |
197 GpuChannelMessage* msg = channel_messages_.front(); | |
198 // This needs to clean up both GpuCommandBufferMsg_InsertSyncPoint and | |
199 // GpuCommandBufferMsg_RetireSyncPoint messages, safer to just check | |
200 // if we have a sync point number here. | |
201 if (msg->sync_point_number) { | |
202 gpu_channel_manager->sync_point_manager()->RetireSyncPoint( | |
203 msg->sync_point_number); | |
204 } | |
205 delete msg; | |
206 channel_messages_.pop_front(); | |
207 } | |
208 STLDeleteElements(&out_of_order_messages_); | |
209 } | |
210 | |
211 void PushUnfinishedMessage(uint32_t order_number, | |
212 const IPC::Message& message) { | |
213 // This is pushed only if it was unfinished, so order number is kept. | |
214 GpuChannelMessage* msg = new GpuChannelMessage(order_number, message); | |
215 base::AutoLock auto_lock(channel_messages_lock_); | |
216 DCHECK(enabled_); | |
217 const bool had_messages = HasQueuedMessagesLocked(); | |
218 if (order_number == kOutOfOrderNumber) | |
219 out_of_order_messages_.push_front(msg); | |
220 else | |
221 channel_messages_.push_front(msg); | |
222 | |
223 if (!had_messages) | |
224 ScheduleHandleMessage(); | |
225 } | |
226 | |
227 void ScheduleHandleMessage() { | |
228 task_runner_->PostTask( | |
229 FROM_HERE, base::Bind(&GpuChannel::HandleMessage, gpu_channel_)); | |
230 } | |
231 | |
232 void PushMessageHelper(uint32_t order_number, GpuChannelMessage* msg) { | |
233 channel_messages_lock_.AssertAcquired(); | |
234 DCHECK(enabled_); | |
235 unprocessed_order_num_ = order_number; | |
236 const bool had_messages = HasQueuedMessagesLocked(); | |
237 channel_messages_.push_back(msg); | |
238 if (!had_messages) | |
239 ScheduleHandleMessage(); | |
240 } | |
241 | |
242 void PushOutOfOrderHelper(GpuChannelMessage* msg) { | |
243 channel_messages_lock_.AssertAcquired(); | |
244 DCHECK(enabled_); | |
245 const bool had_messages = HasQueuedMessagesLocked(); | |
246 out_of_order_messages_.push_back(msg); | |
247 if (!had_messages) | |
248 ScheduleHandleMessage(); | |
249 } | |
250 | |
251 bool HasQueuedMessagesLocked() { | |
252 channel_messages_lock_.AssertAcquired(); | |
253 return !channel_messages_.empty() || !out_of_order_messages_.empty(); | |
254 } | |
255 | |
256 bool enabled_; | |
257 | |
258 // Highest IPC order number seen, set when queued on the IO thread. | |
259 uint32_t unprocessed_order_num_; | |
260 std::deque<GpuChannelMessage*> channel_messages_; | |
261 std::deque<GpuChannelMessage*> out_of_order_messages_; | |
262 | |
263 // This lock protects enabled_, unprocessed_order_num_, and both deques. | |
264 base::Lock channel_messages_lock_; | |
265 | |
266 base::WeakPtr<GpuChannel> gpu_channel_; | |
267 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
268 | |
269 DISALLOW_COPY_AND_ASSIGN(GpuChannelMessageQueue); | |
270 }; | |
271 | 239 |
272 // Begin order numbers at 1 so 0 can mean no orders. | 240 // Begin order numbers at 1 so 0 can mean no orders. |
273 uint32_t GpuChannelMessageFilter::global_order_counter_ = 1; | 241 uint32_t GpuChannelMessageFilter::global_order_counter_ = 1; |
274 | 242 |
275 GpuChannelMessageFilter::GpuChannelMessageFilter( | 243 GpuChannelMessageFilter::GpuChannelMessageFilter( |
276 scoped_refptr<GpuChannelMessageQueue> message_queue, | 244 GpuChannelMessageQueue* message_queue, |
277 gpu::SyncPointManager* sync_point_manager, | 245 gpu::SyncPointManager* sync_point_manager, |
278 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 246 base::SingleThreadTaskRunner* task_runner, |
279 bool future_sync_points) | 247 bool future_sync_points) |
280 : preemption_state_(IDLE), | 248 : preemption_state_(IDLE), |
281 message_queue_(message_queue), | 249 message_queue_(message_queue), |
282 sender_(nullptr), | 250 sender_(nullptr), |
283 peer_pid_(base::kNullProcessId), | 251 peer_pid_(base::kNullProcessId), |
284 sync_point_manager_(sync_point_manager), | 252 sync_point_manager_(sync_point_manager), |
285 task_runner_(task_runner), | 253 task_runner_(task_runner), |
286 a_stub_is_descheduled_(false), | 254 a_stub_is_descheduled_(false), |
287 future_sync_points_(future_sync_points) {} | 255 future_sync_points_(future_sync_points) {} |
288 | 256 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
355 const uint32_t order_number = global_order_counter_++; | 323 const uint32_t order_number = global_order_counter_++; |
356 bool handled = false; | 324 bool handled = false; |
357 if ((message.type() == GpuCommandBufferMsg_RetireSyncPoint::ID) && | 325 if ((message.type() == GpuCommandBufferMsg_RetireSyncPoint::ID) && |
358 !future_sync_points_) { | 326 !future_sync_points_) { |
359 DLOG(ERROR) << "Untrusted client should not send " | 327 DLOG(ERROR) << "Untrusted client should not send " |
360 "GpuCommandBufferMsg_RetireSyncPoint message"; | 328 "GpuCommandBufferMsg_RetireSyncPoint message"; |
361 return true; | 329 return true; |
362 } | 330 } |
363 | 331 |
364 if (message.type() == GpuCommandBufferMsg_InsertSyncPoint::ID) { | 332 if (message.type() == GpuCommandBufferMsg_InsertSyncPoint::ID) { |
365 base::Tuple<bool> retire; | 333 base::Tuple<bool> params; |
366 IPC::Message* reply = IPC::SyncMessage::GenerateReply(&message); | 334 IPC::Message* reply = IPC::SyncMessage::GenerateReply(&message); |
367 if (!GpuCommandBufferMsg_InsertSyncPoint::ReadSendParam(&message, | 335 if (!GpuCommandBufferMsg_InsertSyncPoint::ReadSendParam(&message, |
368 &retire)) { | 336 ¶ms)) { |
369 reply->set_reply_error(); | 337 reply->set_reply_error(); |
370 Send(reply); | 338 Send(reply); |
371 return true; | 339 return true; |
372 } | 340 } |
373 if (!future_sync_points_ && !base::get<0>(retire)) { | 341 bool retire_sync_point = base::get<0>(params); |
342 if (!future_sync_points_ && !retire_sync_point) { | |
374 LOG(ERROR) << "Untrusted contexts can't create future sync points"; | 343 LOG(ERROR) << "Untrusted contexts can't create future sync points"; |
375 reply->set_reply_error(); | 344 reply->set_reply_error(); |
376 Send(reply); | 345 Send(reply); |
377 return true; | 346 return true; |
378 } | 347 } |
379 | 348 |
380 // Message queue must handle the entire sync point generation because the | 349 // Message queue must handle the entire sync point generation because the |
381 // message queue could be disabled from the main thread during generation. | 350 // message queue could be disabled from the main thread during generation. |
382 uint32_t sync_point = 0u; | 351 uint32_t sync_point = 0u; |
383 if (!message_queue_->GenerateSyncPointMessage( | 352 if (!message_queue_->GenerateSyncPointMessage( |
384 sync_point_manager_, order_number, message, base::get<0>(retire), | 353 sync_point_manager_, order_number, message, retire_sync_point, |
385 &sync_point)) { | 354 &sync_point)) { |
386 LOG(ERROR) << "GpuChannel has been destroyed."; | 355 LOG(ERROR) << "GpuChannel has been destroyed."; |
387 reply->set_reply_error(); | 356 reply->set_reply_error(); |
388 Send(reply); | 357 Send(reply); |
389 return true; | 358 return true; |
390 } | 359 } |
391 | 360 |
392 DCHECK_NE(sync_point, 0u); | 361 DCHECK_NE(sync_point, 0u); |
393 GpuCommandBufferMsg_InsertSyncPoint::WriteReplyParams(reply, sync_point); | 362 GpuCommandBufferMsg_InsertSyncPoint::WriteReplyParams(reply, sync_point); |
394 Send(reply); | 363 Send(reply); |
395 handled = true; | 364 handled = true; |
396 } | 365 } |
397 | 366 |
398 // Forward all other messages to the GPU Channel. | 367 // Forward all other messages to the GPU Channel. |
399 if (!handled && !message.is_reply() && !message.should_unblock()) { | 368 if (!handled && !message.is_reply() && !message.should_unblock()) { |
400 if (message.type() == GpuCommandBufferMsg_WaitForTokenInRange::ID || | 369 if (message.type() == GpuCommandBufferMsg_WaitForTokenInRange::ID || |
401 message.type() == GpuCommandBufferMsg_WaitForGetOffsetInRange::ID) { | 370 message.type() == GpuCommandBufferMsg_WaitForGetOffsetInRange::ID) { |
402 // Move Wait commands to the head of the queue, so the renderer | 371 // Move Wait commands to the head of the queue, so the renderer |
403 // doesn't have to wait any longer than necessary. | 372 // doesn't have to wait any longer than necessary. |
404 message_queue_->PushOutOfOrderMessage(message); | 373 message_queue_->PushBackMessage(kOutOfOrderNumber, message); |
405 } else { | 374 } else { |
406 message_queue_->PushBackMessage(order_number, message); | 375 message_queue_->PushBackMessage(order_number, message); |
407 } | 376 } |
408 handled = true; | 377 handled = true; |
409 } | 378 } |
410 | 379 |
411 UpdatePreemptionState(); | 380 UpdatePreemptionState(); |
412 return handled; | 381 return handled; |
413 } | 382 } |
414 | 383 |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
611 task_runner_(task_runner), | 580 task_runner_(task_runner), |
612 io_task_runner_(io_task_runner), | 581 io_task_runner_(io_task_runner), |
613 share_group_(share_group ? share_group : new gfx::GLShareGroup), | 582 share_group_(share_group ? share_group : new gfx::GLShareGroup), |
614 mailbox_manager_(mailbox | 583 mailbox_manager_(mailbox |
615 ? scoped_refptr<gpu::gles2::MailboxManager>(mailbox) | 584 ? scoped_refptr<gpu::gles2::MailboxManager>(mailbox) |
616 : gpu::gles2::MailboxManager::Create()), | 585 : gpu::gles2::MailboxManager::Create()), |
617 subscription_ref_set_(new gpu::gles2::SubscriptionRefSet), | 586 subscription_ref_set_(new gpu::gles2::SubscriptionRefSet), |
618 pending_valuebuffer_state_(new gpu::ValueStateMap), | 587 pending_valuebuffer_state_(new gpu::ValueStateMap), |
619 watchdog_(watchdog), | 588 watchdog_(watchdog), |
620 software_(software), | 589 software_(software), |
621 current_order_num_(0), | |
622 processed_order_num_(0), | |
623 num_stubs_descheduled_(0), | 590 num_stubs_descheduled_(0), |
624 allow_future_sync_points_(allow_future_sync_points), | 591 allow_future_sync_points_(allow_future_sync_points), |
625 allow_real_time_streams_(allow_real_time_streams), | 592 allow_real_time_streams_(allow_real_time_streams), |
626 weak_factory_(this) { | 593 weak_factory_(this) { |
627 DCHECK(gpu_channel_manager); | 594 DCHECK(gpu_channel_manager); |
628 DCHECK(client_id); | 595 DCHECK(client_id); |
629 | 596 |
630 message_queue_ = | 597 message_queue_ = |
631 GpuChannelMessageQueue::Create(weak_factory_.GetWeakPtr(), task_runner); | 598 GpuChannelMessageQueue::Create(weak_factory_.GetWeakPtr(), task_runner); |
632 | 599 |
633 filter_ = new GpuChannelMessageFilter( | 600 filter_ = new GpuChannelMessageFilter( |
634 message_queue_, gpu_channel_manager_->sync_point_manager(), task_runner_, | 601 message_queue_.get(), gpu_channel_manager_->sync_point_manager(), |
635 allow_future_sync_points_); | 602 task_runner_.get(), allow_future_sync_points_); |
636 | 603 |
637 subscription_ref_set_->AddObserver(this); | 604 subscription_ref_set_->AddObserver(this); |
638 } | 605 } |
639 | 606 |
640 GpuChannel::~GpuChannel() { | 607 GpuChannel::~GpuChannel() { |
641 // Clear stubs first because of dependencies. | 608 // Clear stubs first because of dependencies. |
642 stubs_.clear(); | 609 stubs_.clear(); |
643 | 610 |
644 message_queue_->DeleteAndDisableMessages(gpu_channel_manager_); | 611 message_queue_->DeleteAndDisableMessages(gpu_channel_manager_); |
645 | 612 |
(...skipping 23 matching lines...) Expand all Loading... | |
669 | 636 |
670 channel_->AddFilter(filter_.get()); | 637 channel_->AddFilter(filter_.get()); |
671 | 638 |
672 return channel_handle; | 639 return channel_handle; |
673 } | 640 } |
674 | 641 |
675 base::ProcessId GpuChannel::GetClientPID() const { | 642 base::ProcessId GpuChannel::GetClientPID() const { |
676 return channel_->GetPeerPID(); | 643 return channel_->GetPeerPID(); |
677 } | 644 } |
678 | 645 |
646 uint32_t GpuChannel::GetProcessedOrderNum() const { | |
647 return message_queue_->processed_order_num(); | |
648 } | |
649 | |
650 uint32_t GpuChannel::GetUnprocessedOrderNum() const { | |
651 return message_queue_->GetUnprocessedOrderNum(); | |
652 } | |
653 | |
679 bool GpuChannel::OnMessageReceived(const IPC::Message& message) { | 654 bool GpuChannel::OnMessageReceived(const IPC::Message& message) { |
680 // All messages should be pushed to channel_messages_ and handled separately. | 655 // All messages should be pushed to channel_messages_ and handled separately. |
681 NOTREACHED(); | 656 NOTREACHED(); |
682 return false; | 657 return false; |
683 } | 658 } |
684 | 659 |
685 void GpuChannel::OnChannelError() { | 660 void GpuChannel::OnChannelError() { |
686 gpu_channel_manager_->RemoveChannel(client_id_); | 661 gpu_channel_manager_->RemoveChannel(client_id_); |
687 } | 662 } |
688 | 663 |
(...skipping 20 matching lines...) Expand all Loading... | |
709 | 684 |
710 void GpuChannel::OnRemoveSubscription(unsigned int target) { | 685 void GpuChannel::OnRemoveSubscription(unsigned int target) { |
711 gpu_channel_manager()->Send( | 686 gpu_channel_manager()->Send( |
712 new GpuHostMsg_RemoveSubscription(client_id_, target)); | 687 new GpuHostMsg_RemoveSubscription(client_id_, target)); |
713 } | 688 } |
714 | 689 |
715 void GpuChannel::StubSchedulingChanged(bool scheduled) { | 690 void GpuChannel::StubSchedulingChanged(bool scheduled) { |
716 bool a_stub_was_descheduled = num_stubs_descheduled_ > 0; | 691 bool a_stub_was_descheduled = num_stubs_descheduled_ > 0; |
717 if (scheduled) { | 692 if (scheduled) { |
718 num_stubs_descheduled_--; | 693 num_stubs_descheduled_--; |
719 message_queue_->ScheduleHandleMessage(); | 694 ScheduleHandleMessage(); |
720 } else { | 695 } else { |
721 num_stubs_descheduled_++; | 696 num_stubs_descheduled_++; |
722 } | 697 } |
723 DCHECK_LE(num_stubs_descheduled_, stubs_.size()); | 698 DCHECK_LE(num_stubs_descheduled_, stubs_.size()); |
724 bool a_stub_is_descheduled = num_stubs_descheduled_ > 0; | 699 bool a_stub_is_descheduled = num_stubs_descheduled_ > 0; |
725 | 700 |
726 if (a_stub_is_descheduled != a_stub_was_descheduled) { | 701 if (a_stub_is_descheduled != a_stub_was_descheduled) { |
727 if (preempting_flag_.get()) { | 702 if (preempting_flag_.get()) { |
728 io_task_runner_->PostTask( | 703 io_task_runner_->PostTask( |
729 FROM_HERE, | 704 FROM_HERE, |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
857 OnDestroyCommandBuffer) | 832 OnDestroyCommandBuffer) |
858 IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuMsg_CreateJpegDecoder, | 833 IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuMsg_CreateJpegDecoder, |
859 OnCreateJpegDecoder) | 834 OnCreateJpegDecoder) |
860 IPC_MESSAGE_UNHANDLED(handled = false) | 835 IPC_MESSAGE_UNHANDLED(handled = false) |
861 IPC_END_MESSAGE_MAP() | 836 IPC_END_MESSAGE_MAP() |
862 DCHECK(handled) << msg.type(); | 837 DCHECK(handled) << msg.type(); |
863 return handled; | 838 return handled; |
864 } | 839 } |
865 | 840 |
866 void GpuChannel::HandleMessage() { | 841 void GpuChannel::HandleMessage() { |
867 GpuChannelMessage* m = nullptr; | 842 // If we have been preempted by another channel, just post a task to wake up. |
868 GpuCommandBufferStub* stub = nullptr; | 843 if (preempted_flag_ && preempted_flag_->IsSet()) { |
869 bool has_more_messages = false; | 844 ScheduleHandleMessage(); |
870 { | 845 return; |
871 base::AutoLock auto_lock(message_queue_->channel_messages_lock_); | |
872 if (!message_queue_->out_of_order_messages_.empty()) { | |
873 m = message_queue_->out_of_order_messages_.front(); | |
874 DCHECK(m->order_number == kOutOfOrderNumber); | |
875 message_queue_->out_of_order_messages_.pop_front(); | |
876 } else if (!message_queue_->channel_messages_.empty()) { | |
877 m = message_queue_->channel_messages_.front(); | |
878 DCHECK(m->order_number != kOutOfOrderNumber); | |
879 message_queue_->channel_messages_.pop_front(); | |
880 } else { | |
881 // No messages to process | |
882 return; | |
883 } | |
884 | |
885 has_more_messages = message_queue_->HasQueuedMessagesLocked(); | |
886 } | 846 } |
887 | 847 |
888 bool retry_message = false; | 848 GpuChannelMessage* m = message_queue_->GetNextMessage(); |
889 stub = stubs_.get(m->message.routing_id()); | 849 |
890 if (stub) { | 850 // TODO(sunnyps): This could be a DCHECK maybe? |
891 if (!stub->IsScheduled()) { | 851 if (!m) |
892 retry_message = true; | 852 return; |
853 | |
854 uint32_t order_number = m->order_number; | |
855 const IPC::Message& message = m->message; | |
856 int32_t routing_id = message.routing_id(); | |
857 GpuCommandBufferStub* stub = stubs_.get(routing_id); | |
858 | |
859 DCHECK(!stub || stub->IsScheduled()); | |
860 | |
861 DVLOG(1) << "received message @" << &message << " on channel @" << this | |
862 << " with type " << message.type(); | |
863 | |
864 current_order_num_ = order_number; | |
865 | |
866 bool handled = false; | |
867 | |
868 if (routing_id == MSG_ROUTING_CONTROL) { | |
869 handled = OnControlMessageReceived(message); | |
870 } else if (message.type() == GpuCommandBufferMsg_InsertSyncPoint::ID) { | |
871 // TODO(dyen): Temporary handling of old sync points. | |
872 // This must ensure that the sync point will be retired. Normally we'll | |
873 // find the stub based on the routing ID, and associate the sync point | |
874 // with it, but if that fails for any reason (channel or stub already | |
875 // deleted, invalid routing id), we need to retire the sync point | |
876 // immediately. | |
877 if (stub) { | |
878 stub->AddSyncPoint(m->sync_point, m->retire_sync_point); | |
879 } else { | |
880 gpu_channel_manager_->sync_point_manager()->RetireSyncPoint( | |
881 m->sync_point); | |
893 } | 882 } |
894 if (stub->IsPreempted()) { | 883 handled = true; |
895 retry_message = true; | 884 } else { |
896 message_queue_->ScheduleHandleMessage(); | 885 handled = router_.RouteMessage(message); |
897 } | |
898 } | 886 } |
899 | 887 |
900 if (retry_message) { | 888 // Respond to sync messages even if router failed to route. |
901 base::AutoLock auto_lock(message_queue_->channel_messages_lock_); | 889 if (!handled && message.is_sync()) { |
902 if (m->order_number == kOutOfOrderNumber) | 890 IPC::Message* reply = IPC::SyncMessage::GenerateReply(&message); |
903 message_queue_->out_of_order_messages_.push_front(m); | 891 reply->set_reply_error(); |
904 else | 892 Send(reply); |
905 message_queue_->channel_messages_.push_front(m); | 893 handled = true; |
906 return; | |
907 } else if (has_more_messages) { | |
908 message_queue_->ScheduleHandleMessage(); | |
909 } | 894 } |
910 | 895 |
911 scoped_ptr<GpuChannelMessage> scoped_message(m); | 896 // A command buffer may be descheduled or preempted but only in the middle of |
912 const uint32_t order_number = m->order_number; | 897 // a flush. In this case we should not pop the message from the queue. |
913 const int32_t routing_id = m->message.routing_id(); | 898 if (stub && stub->HasUnprocessedCommands()) { |
914 | 899 DCHECK(message.type() == GpuCommandBufferMsg_AsyncFlush::ID); |
915 // TODO(dyen): Temporary handling of old sync points. | 900 // If the stub is still scheduled then we were preempted and need to |
916 // This must ensure that the sync point will be retired. Normally we'll | 901 // schedule a wakeup otherwise some other event will wake us up e.g. sync |
917 // find the stub based on the routing ID, and associate the sync point | 902 // point completion. No DCHECK for preemption flag because that can change |
918 // with it, but if that fails for any reason (channel or stub already | 903 // any time. |
919 // deleted, invalid routing id), we need to retire the sync point | 904 if (stub->IsScheduled()) |
920 // immediately. | 905 ScheduleHandleMessage(); |
921 if (m->message.type() == GpuCommandBufferMsg_InsertSyncPoint::ID) { | 906 return; |
922 const bool retire = m->retire_sync_point; | |
923 const uint32_t sync_point = m->sync_point_number; | |
924 if (stub) { | |
925 stub->AddSyncPoint(sync_point); | |
926 if (retire) { | |
927 m->message = | |
928 GpuCommandBufferMsg_RetireSyncPoint(routing_id, sync_point); | |
929 } | |
930 } else { | |
931 current_order_num_ = order_number; | |
932 gpu_channel_manager_->sync_point_manager()->RetireSyncPoint(sync_point); | |
933 MessageProcessed(order_number); | |
934 return; | |
935 } | |
936 } | 907 } |
937 | 908 |
938 IPC::Message* message = &m->message; | 909 if (message_queue_->MessageProcessed(order_number)) { |
939 bool message_processed = true; | 910 ScheduleHandleMessage(); |
911 } | |
940 | 912 |
941 DVLOG(1) << "received message @" << message << " on channel @" << this | 913 if (preempting_flag_) { |
942 << " with type " << message->type(); | 914 io_task_runner_->PostTask( |
915 FROM_HERE, | |
916 base::Bind(&GpuChannelMessageFilter::OnMessageProcessed, filter_)); | |
917 } | |
918 } | |
943 | 919 |
944 if (order_number != kOutOfOrderNumber) { | 920 void GpuChannel::ScheduleHandleMessage() { |
945 // Make sure this is a valid unprocessed order number. | 921 task_runner_->PostTask(FROM_HERE, base::Bind(&GpuChannel::HandleMessage, |
946 DCHECK(order_number <= GetUnprocessedOrderNum() && | 922 weak_factory_.GetWeakPtr())); |
947 order_number >= GetProcessedOrderNum()); | |
948 | |
949 current_order_num_ = order_number; | |
950 } | |
951 bool result = false; | |
952 if (routing_id == MSG_ROUTING_CONTROL) | |
953 result = OnControlMessageReceived(*message); | |
954 else | |
955 result = router_.RouteMessage(*message); | |
956 | |
957 if (!result) { | |
958 // Respond to sync messages even if router failed to route. | |
959 if (message->is_sync()) { | |
960 IPC::Message* reply = IPC::SyncMessage::GenerateReply(&*message); | |
961 reply->set_reply_error(); | |
962 Send(reply); | |
963 } | |
964 } else { | |
965 // If the command buffer becomes unscheduled as a result of handling the | |
966 // message but still has more commands to process, synthesize an IPC | |
967 // message to flush that command buffer. | |
968 if (stub) { | |
969 if (stub->HasUnprocessedCommands()) { | |
970 message_queue_->PushUnfinishedMessage( | |
971 order_number, GpuCommandBufferMsg_Rescheduled(stub->route_id())); | |
972 message_processed = false; | |
973 } | |
974 } | |
975 } | |
976 if (message_processed) | |
977 MessageProcessed(order_number); | |
978 } | 923 } |
979 | 924 |
980 void GpuChannel::OnCreateOffscreenCommandBuffer( | 925 void GpuChannel::OnCreateOffscreenCommandBuffer( |
981 const gfx::Size& size, | 926 const gfx::Size& size, |
982 const GPUCreateCommandBufferConfig& init_params, | 927 const GPUCreateCommandBufferConfig& init_params, |
983 int32 route_id, | 928 int32 route_id, |
984 bool* succeeded) { | 929 bool* succeeded) { |
985 TRACE_EVENT1("gpu", "GpuChannel::OnCreateOffscreenCommandBuffer", "route_id", | 930 TRACE_EVENT1("gpu", "GpuChannel::OnCreateOffscreenCommandBuffer", "route_id", |
986 route_id); | 931 route_id); |
987 | 932 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1071 } | 1016 } |
1072 } | 1017 } |
1073 | 1018 |
1074 void GpuChannel::OnCreateJpegDecoder(int32 route_id, IPC::Message* reply_msg) { | 1019 void GpuChannel::OnCreateJpegDecoder(int32 route_id, IPC::Message* reply_msg) { |
1075 if (!jpeg_decoder_) { | 1020 if (!jpeg_decoder_) { |
1076 jpeg_decoder_.reset(new GpuJpegDecodeAccelerator(this, io_task_runner_)); | 1021 jpeg_decoder_.reset(new GpuJpegDecodeAccelerator(this, io_task_runner_)); |
1077 } | 1022 } |
1078 jpeg_decoder_->AddClient(route_id, reply_msg); | 1023 jpeg_decoder_->AddClient(route_id, reply_msg); |
1079 } | 1024 } |
1080 | 1025 |
1081 void GpuChannel::MessageProcessed(uint32_t order_number) { | |
1082 if (order_number != kOutOfOrderNumber) { | |
1083 DCHECK(current_order_num_ == order_number); | |
1084 DCHECK(processed_order_num_ < order_number); | |
1085 processed_order_num_ = order_number; | |
1086 } | |
1087 if (preempting_flag_.get()) { | |
1088 io_task_runner_->PostTask( | |
1089 FROM_HERE, | |
1090 base::Bind(&GpuChannelMessageFilter::OnMessageProcessed, filter_)); | |
1091 } | |
1092 } | |
1093 | |
1094 void GpuChannel::CacheShader(const std::string& key, | 1026 void GpuChannel::CacheShader(const std::string& key, |
1095 const std::string& shader) { | 1027 const std::string& shader) { |
1096 gpu_channel_manager_->Send( | 1028 gpu_channel_manager_->Send( |
1097 new GpuHostMsg_CacheShader(client_id_, key, shader)); | 1029 new GpuHostMsg_CacheShader(client_id_, key, shader)); |
1098 } | 1030 } |
1099 | 1031 |
1100 void GpuChannel::AddFilter(IPC::MessageFilter* filter) { | 1032 void GpuChannel::AddFilter(IPC::MessageFilter* filter) { |
1101 io_task_runner_->PostTask( | 1033 io_task_runner_->PostTask( |
1102 FROM_HERE, base::Bind(&GpuChannelMessageFilter::AddChannelFilter, | 1034 FROM_HERE, base::Bind(&GpuChannelMessageFilter::AddChannelFilter, |
1103 filter_, make_scoped_refptr(filter))); | 1035 filter_, make_scoped_refptr(filter))); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1153 client_id_); | 1085 client_id_); |
1154 } | 1086 } |
1155 } | 1087 } |
1156 } | 1088 } |
1157 | 1089 |
1158 void GpuChannel::HandleUpdateValueState( | 1090 void GpuChannel::HandleUpdateValueState( |
1159 unsigned int target, const gpu::ValueState& state) { | 1091 unsigned int target, const gpu::ValueState& state) { |
1160 pending_valuebuffer_state_->UpdateState(target, state); | 1092 pending_valuebuffer_state_->UpdateState(target, state); |
1161 } | 1093 } |
1162 | 1094 |
1163 uint32_t GpuChannel::GetUnprocessedOrderNum() const { | |
1164 return message_queue_->GetUnprocessedOrderNum(); | |
1165 } | |
1166 | |
1167 } // namespace content | 1095 } // namespace content |
OLD | NEW |