Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(270)

Side by Side Diff: content/common/gpu/gpu_channel.cc

Issue 1336623004: content/gpu: Simplify gpu channel message handling. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: couple of things missed in the last patch Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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_)
piman 2015/09/11 00:38:33 nit: needs {} per style.
sunnyps 2015/09/11 01:53:22 Done.
104 PushMessageHelper(
105 make_scoped_ptr(new GpuChannelMessage(order_number, message)));
106 }
107
108 bool GpuChannelMessageQueue::GenerateSyncPointMessage(
109 gpu::SyncPointManager* sync_point_manager,
110 uint32_t order_number,
111 const IPC::Message& message,
112 bool retire_sync_point,
113 uint32_t* sync_point) {
114 DCHECK_EQ(message.type(), GpuCommandBufferMsg_InsertSyncPoint::ID);
115 DCHECK(sync_point);
116 base::AutoLock auto_lock(channel_messages_lock_);
117 if (enabled_) {
118 *sync_point = sync_point_manager->GenerateSyncPoint();
119
120 GpuChannelMessage* msg = new GpuChannelMessage(order_number, message);
121 msg->retire_sync_point = retire_sync_point;
122 msg->sync_point = *sync_point;
123
124 PushMessageHelper(make_scoped_ptr(msg));
125 return true;
126 }
127 return false;
128 }
129
130 bool GpuChannelMessageQueue::HasQueuedMessages() const {
131 base::AutoLock auto_lock(channel_messages_lock_);
132 return HasQueuedMessagesHelper();
133 }
134
135 base::TimeTicks GpuChannelMessageQueue::GetNextMessageTimeTick() const {
136 base::AutoLock auto_lock(channel_messages_lock_);
137
138 base::TimeTicks next_message_tick;
139 if (!channel_messages_.empty())
140 next_message_tick = channel_messages_.front()->time_received;
141
142 base::TimeTicks next_out_of_order_tick;
143 if (!out_of_order_messages_.empty())
144 next_out_of_order_tick = out_of_order_messages_.front()->time_received;
145
146 if (next_message_tick.is_null())
147 return next_out_of_order_tick;
148 else if (next_out_of_order_tick.is_null())
149 return next_message_tick;
150 else
151 return std::min(next_message_tick, next_out_of_order_tick);
152 }
153
154 GpuChannelMessage* GpuChannelMessageQueue::GetNextMessage() const {
155 base::AutoLock auto_lock(channel_messages_lock_);
156 if (!out_of_order_messages_.empty()) {
157 return out_of_order_messages_.front();
158 } else if (!channel_messages_.empty()) {
159 return channel_messages_.front();
160 } else {
161 return nullptr;
162 }
163 }
164
165 bool GpuChannelMessageQueue::MessageProcessed(uint32_t order_number) {
166 base::AutoLock auto_lock(channel_messages_lock_);
167 if (order_number != kOutOfOrderNumber) {
168 DCHECK(!channel_messages_.empty());
169 GpuChannelMessage* msg = channel_messages_.front();
170 DCHECK_EQ(order_number, msg->order_number);
171 processed_order_num_ = order_number;
172 channel_messages_.pop_front();
173 delete msg;
174 } else {
175 DCHECK(!out_of_order_messages_.empty());
176 GpuChannelMessage* msg = out_of_order_messages_.front();
177 out_of_order_messages_.pop_front();
178 delete msg;
179 }
180 return HasQueuedMessagesHelper();
181 }
182
183 void GpuChannelMessageQueue::DeleteAndDisableMessages(
184 GpuChannelManager* gpu_channel_manager) {
185 {
186 base::AutoLock auto_lock(channel_messages_lock_);
187 DCHECK(enabled_);
188 enabled_ = false;
99 } 189 }
100 190
101 uint32_t GetUnprocessedOrderNum() { 191 // We guarantee that the queues will no longer be modified after enabled_
102 base::AutoLock auto_lock(channel_messages_lock_); 192 // is set to false, it is now safe to modify the queue without the lock.
103 return unprocessed_order_num_; 193 // All public facing modifying functions check enabled_ while all
194 // private modifying functions DCHECK(enabled_) to enforce this.
195 while (!channel_messages_.empty()) {
196 GpuChannelMessage* msg = channel_messages_.front();
197 // This needs to clean up both GpuCommandBufferMsg_InsertSyncPoint and
198 // GpuCommandBufferMsg_RetireSyncPoint messages, safer to just check
199 // if we have a sync point number here.
200 if (msg->sync_point) {
201 gpu_channel_manager->sync_point_manager()->RetireSyncPoint(
202 msg->sync_point);
203 }
204 delete msg;
205 channel_messages_.pop_front();
104 } 206 }
207 STLDeleteElements(&out_of_order_messages_);
208 }
105 209
106 void PushBackMessage(uint32_t order_number, const IPC::Message& message) { 210 void GpuChannelMessageQueue::ScheduleHandleMessage() {
107 base::AutoLock auto_lock(channel_messages_lock_); 211 task_runner_->PostTask(FROM_HERE,
108 if (enabled_) { 212 base::Bind(&GpuChannel::HandleMessage, gpu_channel_));
109 PushMessageHelper(order_number, 213 }
110 new GpuChannelMessage(order_number, message)); 214
111 } 215 void GpuChannelMessageQueue::PushMessageHelper(
216 scoped_ptr<GpuChannelMessage> msg) {
217 channel_messages_lock_.AssertAcquired();
218 DCHECK(enabled_);
219 bool had_messages = HasQueuedMessagesHelper();
220 if (msg->order_number != kOutOfOrderNumber) {
221 unprocessed_order_num_ = msg->order_number;
222 channel_messages_.push_back(msg.release());
223 } else {
224 out_of_order_messages_.push_back(msg.release());
112 } 225 }
226 if (!had_messages)
227 ScheduleHandleMessage();
228 }
113 229
114 void PushOutOfOrderMessage(const IPC::Message& message) { 230 bool GpuChannelMessageQueue::HasQueuedMessagesHelper() const {
115 // These are pushed out of order so should not have any order messages. 231 channel_messages_lock_.AssertAcquired();
116 base::AutoLock auto_lock(channel_messages_lock_); 232 return !channel_messages_.empty() || !out_of_order_messages_.empty();
117 if (enabled_) { 233 }
118 PushOutOfOrderHelper(new GpuChannelMessage(kOutOfOrderNumber, message));
119 }
120 }
121
122 bool GenerateSyncPointMessage(gpu::SyncPointManager* sync_point_manager,
123 uint32_t order_number,
124 const IPC::Message& message,
125 bool retire_sync_point,
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 234
272 // Begin order numbers at 1 so 0 can mean no orders. 235 // Begin order numbers at 1 so 0 can mean no orders.
273 uint32_t GpuChannelMessageFilter::global_order_counter_ = 1; 236 uint32_t GpuChannelMessageFilter::global_order_counter_ = 1;
274 237
275 GpuChannelMessageFilter::GpuChannelMessageFilter( 238 GpuChannelMessageFilter::GpuChannelMessageFilter(
276 scoped_refptr<GpuChannelMessageQueue> message_queue, 239 GpuChannelMessageQueue* message_queue,
277 gpu::SyncPointManager* sync_point_manager, 240 gpu::SyncPointManager* sync_point_manager,
278 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 241 base::SingleThreadTaskRunner* task_runner,
279 bool future_sync_points) 242 bool future_sync_points)
280 : preemption_state_(IDLE), 243 : preemption_state_(IDLE),
281 message_queue_(message_queue), 244 message_queue_(message_queue),
282 sender_(nullptr), 245 sender_(nullptr),
283 peer_pid_(base::kNullProcessId), 246 peer_pid_(base::kNullProcessId),
284 sync_point_manager_(sync_point_manager), 247 sync_point_manager_(sync_point_manager),
285 task_runner_(task_runner), 248 task_runner_(task_runner),
286 a_stub_is_descheduled_(false), 249 a_stub_is_descheduled_(false),
287 future_sync_points_(future_sync_points) {} 250 future_sync_points_(future_sync_points) {}
288 251
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 const uint32_t order_number = global_order_counter_++; 318 const uint32_t order_number = global_order_counter_++;
356 bool handled = false; 319 bool handled = false;
357 if ((message.type() == GpuCommandBufferMsg_RetireSyncPoint::ID) && 320 if ((message.type() == GpuCommandBufferMsg_RetireSyncPoint::ID) &&
358 !future_sync_points_) { 321 !future_sync_points_) {
359 DLOG(ERROR) << "Untrusted client should not send " 322 DLOG(ERROR) << "Untrusted client should not send "
360 "GpuCommandBufferMsg_RetireSyncPoint message"; 323 "GpuCommandBufferMsg_RetireSyncPoint message";
361 return true; 324 return true;
362 } 325 }
363 326
364 if (message.type() == GpuCommandBufferMsg_InsertSyncPoint::ID) { 327 if (message.type() == GpuCommandBufferMsg_InsertSyncPoint::ID) {
365 base::Tuple<bool> retire; 328 base::Tuple<bool> params;
366 IPC::Message* reply = IPC::SyncMessage::GenerateReply(&message); 329 IPC::Message* reply = IPC::SyncMessage::GenerateReply(&message);
367 if (!GpuCommandBufferMsg_InsertSyncPoint::ReadSendParam(&message, 330 if (!GpuCommandBufferMsg_InsertSyncPoint::ReadSendParam(&message,
368 &retire)) { 331 &params)) {
369 reply->set_reply_error(); 332 reply->set_reply_error();
370 Send(reply); 333 Send(reply);
371 return true; 334 return true;
372 } 335 }
373 if (!future_sync_points_ && !base::get<0>(retire)) { 336 bool retire_sync_point = base::get<0>(params);
337 if (!future_sync_points_ && !retire_sync_point) {
374 LOG(ERROR) << "Untrusted contexts can't create future sync points"; 338 LOG(ERROR) << "Untrusted contexts can't create future sync points";
375 reply->set_reply_error(); 339 reply->set_reply_error();
376 Send(reply); 340 Send(reply);
377 return true; 341 return true;
378 } 342 }
379 343
380 // Message queue must handle the entire sync point generation because the 344 // Message queue must handle the entire sync point generation because the
381 // message queue could be disabled from the main thread during generation. 345 // message queue could be disabled from the main thread during generation.
382 uint32_t sync_point = 0u; 346 uint32_t sync_point = 0u;
383 if (!message_queue_->GenerateSyncPointMessage( 347 if (!message_queue_->GenerateSyncPointMessage(
384 sync_point_manager_, order_number, message, base::get<0>(retire), 348 sync_point_manager_, order_number, message, retire_sync_point,
385 &sync_point)) { 349 &sync_point)) {
386 LOG(ERROR) << "GpuChannel has been destroyed."; 350 LOG(ERROR) << "GpuChannel has been destroyed.";
387 reply->set_reply_error(); 351 reply->set_reply_error();
388 Send(reply); 352 Send(reply);
389 return true; 353 return true;
390 } 354 }
391 355
392 DCHECK_NE(sync_point, 0u); 356 DCHECK_NE(sync_point, 0u);
393 GpuCommandBufferMsg_InsertSyncPoint::WriteReplyParams(reply, sync_point); 357 GpuCommandBufferMsg_InsertSyncPoint::WriteReplyParams(reply, sync_point);
394 Send(reply); 358 Send(reply);
395 handled = true; 359 handled = true;
396 } 360 }
397 361
398 // Forward all other messages to the GPU Channel. 362 // Forward all other messages to the GPU Channel.
399 if (!handled && !message.is_reply() && !message.should_unblock()) { 363 if (!handled && !message.is_reply() && !message.should_unblock()) {
400 if (message.type() == GpuCommandBufferMsg_WaitForTokenInRange::ID || 364 if (message.type() == GpuCommandBufferMsg_WaitForTokenInRange::ID ||
401 message.type() == GpuCommandBufferMsg_WaitForGetOffsetInRange::ID) { 365 message.type() == GpuCommandBufferMsg_WaitForGetOffsetInRange::ID) {
402 // Move Wait commands to the head of the queue, so the renderer 366 // Move Wait commands to the head of the queue, so the renderer
403 // doesn't have to wait any longer than necessary. 367 // doesn't have to wait any longer than necessary.
404 message_queue_->PushOutOfOrderMessage(message); 368 message_queue_->PushBackMessage(kOutOfOrderNumber, message);
405 } else { 369 } else {
406 message_queue_->PushBackMessage(order_number, message); 370 message_queue_->PushBackMessage(order_number, message);
407 } 371 }
408 handled = true; 372 handled = true;
409 } 373 }
410 374
411 UpdatePreemptionState(); 375 UpdatePreemptionState();
412 return handled; 376 return handled;
413 } 377 }
414 378
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 task_runner_(task_runner), 575 task_runner_(task_runner),
612 io_task_runner_(io_task_runner), 576 io_task_runner_(io_task_runner),
613 share_group_(share_group ? share_group : new gfx::GLShareGroup), 577 share_group_(share_group ? share_group : new gfx::GLShareGroup),
614 mailbox_manager_(mailbox 578 mailbox_manager_(mailbox
615 ? scoped_refptr<gpu::gles2::MailboxManager>(mailbox) 579 ? scoped_refptr<gpu::gles2::MailboxManager>(mailbox)
616 : gpu::gles2::MailboxManager::Create()), 580 : gpu::gles2::MailboxManager::Create()),
617 subscription_ref_set_(new gpu::gles2::SubscriptionRefSet), 581 subscription_ref_set_(new gpu::gles2::SubscriptionRefSet),
618 pending_valuebuffer_state_(new gpu::ValueStateMap), 582 pending_valuebuffer_state_(new gpu::ValueStateMap),
619 watchdog_(watchdog), 583 watchdog_(watchdog),
620 software_(software), 584 software_(software),
621 current_order_num_(0),
622 processed_order_num_(0),
623 num_stubs_descheduled_(0), 585 num_stubs_descheduled_(0),
624 allow_future_sync_points_(allow_future_sync_points), 586 allow_future_sync_points_(allow_future_sync_points),
625 allow_real_time_streams_(allow_real_time_streams), 587 allow_real_time_streams_(allow_real_time_streams),
626 weak_factory_(this) { 588 weak_factory_(this) {
627 DCHECK(gpu_channel_manager); 589 DCHECK(gpu_channel_manager);
628 DCHECK(client_id); 590 DCHECK(client_id);
629 591
630 message_queue_ = 592 message_queue_ =
631 GpuChannelMessageQueue::Create(weak_factory_.GetWeakPtr(), task_runner); 593 GpuChannelMessageQueue::Create(weak_factory_.GetWeakPtr(), task_runner);
632 594
633 filter_ = new GpuChannelMessageFilter( 595 filter_ = new GpuChannelMessageFilter(
634 message_queue_, gpu_channel_manager_->sync_point_manager(), task_runner_, 596 message_queue_.get(), gpu_channel_manager_->sync_point_manager(),
635 allow_future_sync_points_); 597 task_runner_.get(), allow_future_sync_points_);
636 598
637 subscription_ref_set_->AddObserver(this); 599 subscription_ref_set_->AddObserver(this);
638 } 600 }
639 601
640 GpuChannel::~GpuChannel() { 602 GpuChannel::~GpuChannel() {
641 // Clear stubs first because of dependencies. 603 // Clear stubs first because of dependencies.
642 stubs_.clear(); 604 stubs_.clear();
643 605
644 message_queue_->DeleteAndDisableMessages(gpu_channel_manager_); 606 message_queue_->DeleteAndDisableMessages(gpu_channel_manager_);
645 607
(...skipping 23 matching lines...) Expand all
669 631
670 channel_->AddFilter(filter_.get()); 632 channel_->AddFilter(filter_.get());
671 633
672 return channel_handle; 634 return channel_handle;
673 } 635 }
674 636
675 base::ProcessId GpuChannel::GetClientPID() const { 637 base::ProcessId GpuChannel::GetClientPID() const {
676 return channel_->GetPeerPID(); 638 return channel_->GetPeerPID();
677 } 639 }
678 640
641 uint32_t GpuChannel::GetProcessedOrderNum() const {
642 return message_queue_->processed_order_num();
643 }
644
645 uint32_t GpuChannel::GetUnprocessedOrderNum() const {
646 return message_queue_->GetUnprocessedOrderNum();
647 }
648
679 bool GpuChannel::OnMessageReceived(const IPC::Message& message) { 649 bool GpuChannel::OnMessageReceived(const IPC::Message& message) {
680 // All messages should be pushed to channel_messages_ and handled separately. 650 // All messages should be pushed to channel_messages_ and handled separately.
681 NOTREACHED(); 651 NOTREACHED();
682 return false; 652 return false;
683 } 653 }
684 654
685 void GpuChannel::OnChannelError() { 655 void GpuChannel::OnChannelError() {
686 gpu_channel_manager_->RemoveChannel(client_id_); 656 gpu_channel_manager_->RemoveChannel(client_id_);
687 } 657 }
688 658
(...skipping 20 matching lines...) Expand all
709 679
710 void GpuChannel::OnRemoveSubscription(unsigned int target) { 680 void GpuChannel::OnRemoveSubscription(unsigned int target) {
711 gpu_channel_manager()->Send( 681 gpu_channel_manager()->Send(
712 new GpuHostMsg_RemoveSubscription(client_id_, target)); 682 new GpuHostMsg_RemoveSubscription(client_id_, target));
713 } 683 }
714 684
715 void GpuChannel::StubSchedulingChanged(bool scheduled) { 685 void GpuChannel::StubSchedulingChanged(bool scheduled) {
716 bool a_stub_was_descheduled = num_stubs_descheduled_ > 0; 686 bool a_stub_was_descheduled = num_stubs_descheduled_ > 0;
717 if (scheduled) { 687 if (scheduled) {
718 num_stubs_descheduled_--; 688 num_stubs_descheduled_--;
719 message_queue_->ScheduleHandleMessage(); 689 ScheduleHandleMessage();
720 } else { 690 } else {
721 num_stubs_descheduled_++; 691 num_stubs_descheduled_++;
722 } 692 }
723 DCHECK_LE(num_stubs_descheduled_, stubs_.size()); 693 DCHECK_LE(num_stubs_descheduled_, stubs_.size());
724 bool a_stub_is_descheduled = num_stubs_descheduled_ > 0; 694 bool a_stub_is_descheduled = num_stubs_descheduled_ > 0;
725 695
726 if (a_stub_is_descheduled != a_stub_was_descheduled) { 696 if (a_stub_is_descheduled != a_stub_was_descheduled) {
727 if (preempting_flag_.get()) { 697 if (preempting_flag_.get()) {
728 io_task_runner_->PostTask( 698 io_task_runner_->PostTask(
729 FROM_HERE, 699 FROM_HERE,
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
857 OnDestroyCommandBuffer) 827 OnDestroyCommandBuffer)
858 IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuMsg_CreateJpegDecoder, 828 IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuMsg_CreateJpegDecoder,
859 OnCreateJpegDecoder) 829 OnCreateJpegDecoder)
860 IPC_MESSAGE_UNHANDLED(handled = false) 830 IPC_MESSAGE_UNHANDLED(handled = false)
861 IPC_END_MESSAGE_MAP() 831 IPC_END_MESSAGE_MAP()
862 DCHECK(handled) << msg.type(); 832 DCHECK(handled) << msg.type();
863 return handled; 833 return handled;
864 } 834 }
865 835
866 void GpuChannel::HandleMessage() { 836 void GpuChannel::HandleMessage() {
867 GpuChannelMessage* m = nullptr; 837 GpuChannelMessage* m = message_queue_->GetNextMessage();
868 GpuCommandBufferStub* stub = nullptr; 838
869 bool has_more_messages = false; 839 // TODO(sunnyps): This could be a DCHECK maybe?
870 { 840 if (!m)
871 base::AutoLock auto_lock(message_queue_->channel_messages_lock_); 841 return;
872 if (!message_queue_->out_of_order_messages_.empty()) { 842
873 m = message_queue_->out_of_order_messages_.front(); 843 uint32_t order_number = m->order_number;
874 DCHECK(m->order_number == kOutOfOrderNumber); 844 IPC::Message& message = m->message;
875 message_queue_->out_of_order_messages_.pop_front(); 845 int32_t routing_id = message.routing_id();
876 } else if (!message_queue_->channel_messages_.empty()) { 846 GpuCommandBufferStub* stub = stubs_.get(routing_id);
877 m = message_queue_->channel_messages_.front(); 847
878 DCHECK(m->order_number != kOutOfOrderNumber); 848 DVLOG(1) << "received message @" << &message << " on channel @" << this
879 message_queue_->channel_messages_.pop_front(); 849 << " with type " << message.type();
piman 2015/09/11 00:38:33 Are you saying that we have a strong guarantee tha
sunnyps 2015/09/11 01:53:22 I think this is true most of the time - the messag
piman 2015/09/11 02:30:07 What about the example I mentioned? If on a previo
sunnyps 2015/09/11 06:21:59 I misunderstood your original comment thinking it
850
851 bool handled = false;
852
853 if (routing_id == MSG_ROUTING_CONTROL) {
854 handled = OnControlMessageReceived(message);
855 } else if (message.type() == GpuCommandBufferMsg_InsertSyncPoint::ID) {
856 // TODO(dyen): Temporary handling of old sync points.
857 // This must ensure that the sync point will be retired. Normally we'll
858 // find the stub based on the routing ID, and associate the sync point
859 // with it, but if that fails for any reason (channel or stub already
860 // deleted, invalid routing id), we need to retire the sync point
861 // immediately.
862 if (stub) {
863 stub->AddSyncPoint(m->sync_point, m->retire_sync_point);
880 } else { 864 } else {
881 // No messages to process 865 gpu_channel_manager_->sync_point_manager()->RetireSyncPoint(
882 return; 866 m->sync_point);
883 } 867 }
884 868 handled = true;
885 has_more_messages = message_queue_->HasQueuedMessagesLocked(); 869 } else {
870 handled = router_.RouteMessage(message);
886 } 871 }
887 872
888 bool retry_message = false; 873 // Respond to sync messages even if router failed to route.
889 stub = stubs_.get(m->message.routing_id()); 874 if (!handled && message.is_sync()) {
890 if (stub) { 875 IPC::Message* reply = IPC::SyncMessage::GenerateReply(&message);
891 if (!stub->IsScheduled()) { 876 reply->set_reply_error();
892 retry_message = true; 877 Send(reply);
893 } 878 handled = true;
894 if (stub->IsPreempted()) {
895 retry_message = true;
896 message_queue_->ScheduleHandleMessage();
897 }
898 } 879 }
899 880
900 if (retry_message) { 881 // A command buffer may be descheduled or preempted but only in the middle of
901 base::AutoLock auto_lock(message_queue_->channel_messages_lock_); 882 // a flush. In this case we should not pop the message from the queue.
902 if (m->order_number == kOutOfOrderNumber) 883 if (stub && stub->HasUnprocessedCommands()) {
903 message_queue_->out_of_order_messages_.push_front(m); 884 DCHECK(message.type() == GpuCommandBufferMsg_AsyncFlush::ID);
904 else 885 // If the stub was preempted then we need to schedule a wakeup otherwise
905 message_queue_->channel_messages_.push_front(m); 886 // some other event will wake us up e.g. sync point completion.
887 if (stub->IsPreempted())
888 ScheduleHandleMessage();
906 return; 889 return;
907 } else if (has_more_messages) {
908 message_queue_->ScheduleHandleMessage();
909 } 890 }
910 891
911 scoped_ptr<GpuChannelMessage> scoped_message(m); 892 if (message_queue_->MessageProcessed(order_number)) {
912 const uint32_t order_number = m->order_number; 893 ScheduleHandleMessage();
913 const int32_t routing_id = m->message.routing_id();
914
915 // TODO(dyen): Temporary handling of old sync points.
916 // This must ensure that the sync point will be retired. Normally we'll
917 // find the stub based on the routing ID, and associate the sync point
918 // with it, but if that fails for any reason (channel or stub already
919 // deleted, invalid routing id), we need to retire the sync point
920 // immediately.
921 if (m->message.type() == GpuCommandBufferMsg_InsertSyncPoint::ID) {
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 } 894 }
937 895
938 IPC::Message* message = &m->message; 896 if (preempting_flag_) {
939 bool message_processed = true; 897 io_task_runner_->PostTask(
898 FROM_HERE,
899 base::Bind(&GpuChannelMessageFilter::OnMessageProcessed, filter_));
900 }
901 }
940 902
941 DVLOG(1) << "received message @" << message << " on channel @" << this 903 void GpuChannel::ScheduleHandleMessage() {
942 << " with type " << message->type(); 904 task_runner_->PostTask(FROM_HERE, base::Bind(&GpuChannel::HandleMessage,
943 905 weak_factory_.GetWeakPtr()));
944 if (order_number != kOutOfOrderNumber) {
945 // Make sure this is a valid unprocessed order number.
946 DCHECK(order_number <= GetUnprocessedOrderNum() &&
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 } 906 }
979 907
980 void GpuChannel::OnCreateOffscreenCommandBuffer( 908 void GpuChannel::OnCreateOffscreenCommandBuffer(
981 const gfx::Size& size, 909 const gfx::Size& size,
982 const GPUCreateCommandBufferConfig& init_params, 910 const GPUCreateCommandBufferConfig& init_params,
983 int32 route_id, 911 int32 route_id,
984 bool* succeeded) { 912 bool* succeeded) {
985 TRACE_EVENT1("gpu", "GpuChannel::OnCreateOffscreenCommandBuffer", "route_id", 913 TRACE_EVENT1("gpu", "GpuChannel::OnCreateOffscreenCommandBuffer", "route_id",
986 route_id); 914 route_id);
987 915
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
1071 } 999 }
1072 } 1000 }
1073 1001
1074 void GpuChannel::OnCreateJpegDecoder(int32 route_id, IPC::Message* reply_msg) { 1002 void GpuChannel::OnCreateJpegDecoder(int32 route_id, IPC::Message* reply_msg) {
1075 if (!jpeg_decoder_) { 1003 if (!jpeg_decoder_) {
1076 jpeg_decoder_.reset(new GpuJpegDecodeAccelerator(this, io_task_runner_)); 1004 jpeg_decoder_.reset(new GpuJpegDecodeAccelerator(this, io_task_runner_));
1077 } 1005 }
1078 jpeg_decoder_->AddClient(route_id, reply_msg); 1006 jpeg_decoder_->AddClient(route_id, reply_msg);
1079 } 1007 }
1080 1008
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, 1009 void GpuChannel::CacheShader(const std::string& key,
1095 const std::string& shader) { 1010 const std::string& shader) {
1096 gpu_channel_manager_->Send( 1011 gpu_channel_manager_->Send(
1097 new GpuHostMsg_CacheShader(client_id_, key, shader)); 1012 new GpuHostMsg_CacheShader(client_id_, key, shader));
1098 } 1013 }
1099 1014
1100 void GpuChannel::AddFilter(IPC::MessageFilter* filter) { 1015 void GpuChannel::AddFilter(IPC::MessageFilter* filter) {
1101 io_task_runner_->PostTask( 1016 io_task_runner_->PostTask(
1102 FROM_HERE, base::Bind(&GpuChannelMessageFilter::AddChannelFilter, 1017 FROM_HERE, base::Bind(&GpuChannelMessageFilter::AddChannelFilter,
1103 filter_, make_scoped_refptr(filter))); 1018 filter_, make_scoped_refptr(filter)));
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
1153 client_id_); 1068 client_id_);
1154 } 1069 }
1155 } 1070 }
1156 } 1071 }
1157 1072
1158 void GpuChannel::HandleUpdateValueState( 1073 void GpuChannel::HandleUpdateValueState(
1159 unsigned int target, const gpu::ValueState& state) { 1074 unsigned int target, const gpu::ValueState& state) {
1160 pending_valuebuffer_state_->UpdateState(target, state); 1075 pending_valuebuffer_state_->UpdateState(target, state);
1161 } 1076 }
1162 1077
1163 uint32_t GpuChannel::GetUnprocessedOrderNum() const {
1164 return message_queue_->GetUnprocessedOrderNum();
1165 }
1166
1167 } // namespace content 1078 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698