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

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: dcheng's final comments 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
« no previous file with comments | « content/common/gpu/gpu_channel.h ('k') | content/common/gpu/gpu_channel_manager.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_) {
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((uint32_t)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(
122 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(channel_messages_.front());
175 channel_messages_.pop_front();
176 DCHECK_EQ(order_number, msg->order_number);
177 processed_order_num_ = order_number;
178 } else {
179 DCHECK(!out_of_order_messages_.empty());
180 scoped_ptr<GpuChannelMessage> msg(out_of_order_messages_.front());
181 out_of_order_messages_.pop_front();
182 }
183 return HasQueuedMessagesHelper();
184 }
185
186 void GpuChannelMessageQueue::DeleteAndDisableMessages(
187 GpuChannelManager* gpu_channel_manager) {
188 {
189 base::AutoLock auto_lock(channel_messages_lock_);
190 DCHECK(enabled_);
191 enabled_ = false;
99 } 192 }
100 193
101 uint32_t GetUnprocessedOrderNum() { 194 // We guarantee that the queues will no longer be modified after enabled_
102 base::AutoLock auto_lock(channel_messages_lock_); 195 // is set to false, it is now safe to modify the queue without the lock.
103 return unprocessed_order_num_; 196 // All public facing modifying functions check enabled_ while all
104 } 197 // private modifying functions DCHECK(enabled_) to enforce this.
105 198 while (!channel_messages_.empty()) {
106 void PushBackMessage(uint32_t order_number, const IPC::Message& message) { 199 scoped_ptr<GpuChannelMessage> msg(channel_messages_.front());
107 base::AutoLock auto_lock(channel_messages_lock_); 200 channel_messages_.pop_front();
108 if (enabled_) { 201 // This needs to clean up both GpuCommandBufferMsg_InsertSyncPoint and
109 PushMessageHelper(order_number, 202 // GpuCommandBufferMsg_RetireSyncPoint messages, safer to just check
110 new GpuChannelMessage(order_number, message)); 203 // if we have a sync point number here.
204 if (msg->sync_point) {
205 gpu_channel_manager->sync_point_manager()->RetireSyncPoint(
206 msg->sync_point);
111 } 207 }
112 } 208 }
209 STLDeleteElements(&out_of_order_messages_);
210 }
113 211
114 void PushOutOfOrderMessage(const IPC::Message& message) { 212 void GpuChannelMessageQueue::ScheduleHandleMessage() {
115 // These are pushed out of order so should not have any order messages. 213 task_runner_->PostTask(FROM_HERE,
116 base::AutoLock auto_lock(channel_messages_lock_); 214 base::Bind(&GpuChannel::HandleMessage, gpu_channel_));
117 if (enabled_) { 215 }
118 PushOutOfOrderHelper(new GpuChannelMessage(kOutOfOrderNumber, message)); 216
119 } 217 void GpuChannelMessageQueue::PushMessageHelper(
218 scoped_ptr<GpuChannelMessage> msg) {
219 channel_messages_lock_.AssertAcquired();
220 DCHECK(enabled_);
221 bool had_messages = HasQueuedMessagesHelper();
222 if (msg->order_number != kOutOfOrderNumber) {
223 unprocessed_order_num_ = msg->order_number;
224 channel_messages_.push_back(msg.release());
225 } else {
226 out_of_order_messages_.push_back(msg.release());
120 } 227 }
228 if (!had_messages)
229 ScheduleHandleMessage();
230 }
121 231
122 bool GenerateSyncPointMessage(gpu::SyncPointManager* sync_point_manager, 232 bool GpuChannelMessageQueue::HasQueuedMessagesHelper() const {
123 uint32_t order_number, 233 channel_messages_lock_.AssertAcquired();
124 const IPC::Message& message, 234 return !channel_messages_.empty() || !out_of_order_messages_.empty();
125 bool retire_sync_point, 235 }
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 236
272 // Begin order numbers at 1 so 0 can mean no orders. 237 // Begin order numbers at 1 so 0 can mean no orders.
273 uint32_t GpuChannelMessageFilter::global_order_counter_ = 1; 238 uint32_t GpuChannelMessageFilter::global_order_counter_ = 1;
274 239
275 GpuChannelMessageFilter::GpuChannelMessageFilter( 240 GpuChannelMessageFilter::GpuChannelMessageFilter(
276 scoped_refptr<GpuChannelMessageQueue> message_queue, 241 GpuChannelMessageQueue* message_queue,
277 gpu::SyncPointManager* sync_point_manager, 242 gpu::SyncPointManager* sync_point_manager,
278 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 243 base::SingleThreadTaskRunner* task_runner,
279 bool future_sync_points) 244 bool future_sync_points)
280 : preemption_state_(IDLE), 245 : preemption_state_(IDLE),
281 message_queue_(message_queue), 246 message_queue_(message_queue),
282 sender_(nullptr), 247 sender_(nullptr),
283 peer_pid_(base::kNullProcessId), 248 peer_pid_(base::kNullProcessId),
284 sync_point_manager_(sync_point_manager), 249 sync_point_manager_(sync_point_manager),
285 task_runner_(task_runner), 250 task_runner_(task_runner),
286 a_stub_is_descheduled_(false), 251 a_stub_is_descheduled_(false),
287 future_sync_points_(future_sync_points) {} 252 future_sync_points_(future_sync_points) {}
288 253
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 const uint32_t order_number = global_order_counter_++; 320 const uint32_t order_number = global_order_counter_++;
356 bool handled = false; 321 bool handled = false;
357 if ((message.type() == GpuCommandBufferMsg_RetireSyncPoint::ID) && 322 if ((message.type() == GpuCommandBufferMsg_RetireSyncPoint::ID) &&
358 !future_sync_points_) { 323 !future_sync_points_) {
359 DLOG(ERROR) << "Untrusted client should not send " 324 DLOG(ERROR) << "Untrusted client should not send "
360 "GpuCommandBufferMsg_RetireSyncPoint message"; 325 "GpuCommandBufferMsg_RetireSyncPoint message";
361 return true; 326 return true;
362 } 327 }
363 328
364 if (message.type() == GpuCommandBufferMsg_InsertSyncPoint::ID) { 329 if (message.type() == GpuCommandBufferMsg_InsertSyncPoint::ID) {
365 base::Tuple<bool> retire; 330 base::Tuple<bool> params;
366 IPC::Message* reply = IPC::SyncMessage::GenerateReply(&message); 331 IPC::Message* reply = IPC::SyncMessage::GenerateReply(&message);
367 if (!GpuCommandBufferMsg_InsertSyncPoint::ReadSendParam(&message, 332 if (!GpuCommandBufferMsg_InsertSyncPoint::ReadSendParam(&message,
368 &retire)) { 333 &params)) {
369 reply->set_reply_error(); 334 reply->set_reply_error();
370 Send(reply); 335 Send(reply);
371 return true; 336 return true;
372 } 337 }
373 if (!future_sync_points_ && !base::get<0>(retire)) { 338 bool retire_sync_point = base::get<0>(params);
339 if (!future_sync_points_ && !retire_sync_point) {
374 LOG(ERROR) << "Untrusted contexts can't create future sync points"; 340 LOG(ERROR) << "Untrusted contexts can't create future sync points";
375 reply->set_reply_error(); 341 reply->set_reply_error();
376 Send(reply); 342 Send(reply);
377 return true; 343 return true;
378 } 344 }
379 345
380 // Message queue must handle the entire sync point generation because the 346 // Message queue must handle the entire sync point generation because the
381 // message queue could be disabled from the main thread during generation. 347 // message queue could be disabled from the main thread during generation.
382 uint32_t sync_point = 0u; 348 uint32_t sync_point = 0u;
383 if (!message_queue_->GenerateSyncPointMessage( 349 if (!message_queue_->GenerateSyncPointMessage(
384 sync_point_manager_, order_number, message, base::get<0>(retire), 350 sync_point_manager_, order_number, message, retire_sync_point,
385 &sync_point)) { 351 &sync_point)) {
386 LOG(ERROR) << "GpuChannel has been destroyed."; 352 LOG(ERROR) << "GpuChannel has been destroyed.";
387 reply->set_reply_error(); 353 reply->set_reply_error();
388 Send(reply); 354 Send(reply);
389 return true; 355 return true;
390 } 356 }
391 357
392 DCHECK_NE(sync_point, 0u); 358 DCHECK_NE(sync_point, 0u);
393 GpuCommandBufferMsg_InsertSyncPoint::WriteReplyParams(reply, sync_point); 359 GpuCommandBufferMsg_InsertSyncPoint::WriteReplyParams(reply, sync_point);
394 Send(reply); 360 Send(reply);
395 handled = true; 361 handled = true;
396 } 362 }
397 363
398 // Forward all other messages to the GPU Channel. 364 // Forward all other messages to the GPU Channel.
399 if (!handled && !message.is_reply() && !message.should_unblock()) { 365 if (!handled && !message.is_reply() && !message.should_unblock()) {
400 if (message.type() == GpuCommandBufferMsg_WaitForTokenInRange::ID || 366 if (message.type() == GpuCommandBufferMsg_WaitForTokenInRange::ID ||
401 message.type() == GpuCommandBufferMsg_WaitForGetOffsetInRange::ID) { 367 message.type() == GpuCommandBufferMsg_WaitForGetOffsetInRange::ID) {
402 // Move Wait commands to the head of the queue, so the renderer 368 // Move Wait commands to the head of the queue, so the renderer
403 // doesn't have to wait any longer than necessary. 369 // doesn't have to wait any longer than necessary.
404 message_queue_->PushOutOfOrderMessage(message); 370 message_queue_->PushBackMessage(kOutOfOrderNumber, message);
405 } else { 371 } else {
406 message_queue_->PushBackMessage(order_number, message); 372 message_queue_->PushBackMessage(order_number, message);
407 } 373 }
408 handled = true; 374 handled = true;
409 } 375 }
410 376
411 UpdatePreemptionState(); 377 UpdatePreemptionState();
412 return handled; 378 return handled;
413 } 379 }
414 380
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 task_runner_(task_runner), 577 task_runner_(task_runner),
612 io_task_runner_(io_task_runner), 578 io_task_runner_(io_task_runner),
613 share_group_(share_group ? share_group : new gfx::GLShareGroup), 579 share_group_(share_group ? share_group : new gfx::GLShareGroup),
614 mailbox_manager_(mailbox 580 mailbox_manager_(mailbox
615 ? scoped_refptr<gpu::gles2::MailboxManager>(mailbox) 581 ? scoped_refptr<gpu::gles2::MailboxManager>(mailbox)
616 : gpu::gles2::MailboxManager::Create()), 582 : gpu::gles2::MailboxManager::Create()),
617 subscription_ref_set_(new gpu::gles2::SubscriptionRefSet), 583 subscription_ref_set_(new gpu::gles2::SubscriptionRefSet),
618 pending_valuebuffer_state_(new gpu::ValueStateMap), 584 pending_valuebuffer_state_(new gpu::ValueStateMap),
619 watchdog_(watchdog), 585 watchdog_(watchdog),
620 software_(software), 586 software_(software),
621 current_order_num_(0),
622 processed_order_num_(0),
623 num_stubs_descheduled_(0), 587 num_stubs_descheduled_(0),
624 allow_future_sync_points_(allow_future_sync_points), 588 allow_future_sync_points_(allow_future_sync_points),
625 allow_real_time_streams_(allow_real_time_streams), 589 allow_real_time_streams_(allow_real_time_streams),
626 weak_factory_(this) { 590 weak_factory_(this) {
627 DCHECK(gpu_channel_manager); 591 DCHECK(gpu_channel_manager);
628 DCHECK(client_id); 592 DCHECK(client_id);
629 593
630 message_queue_ = 594 message_queue_ =
631 GpuChannelMessageQueue::Create(weak_factory_.GetWeakPtr(), task_runner); 595 GpuChannelMessageQueue::Create(weak_factory_.GetWeakPtr(), task_runner);
632 596
633 filter_ = new GpuChannelMessageFilter( 597 filter_ = new GpuChannelMessageFilter(
634 message_queue_, gpu_channel_manager_->sync_point_manager(), task_runner_, 598 message_queue_.get(), gpu_channel_manager_->sync_point_manager(),
635 allow_future_sync_points_); 599 task_runner_.get(), allow_future_sync_points_);
636 600
637 subscription_ref_set_->AddObserver(this); 601 subscription_ref_set_->AddObserver(this);
638 } 602 }
639 603
640 GpuChannel::~GpuChannel() { 604 GpuChannel::~GpuChannel() {
641 // Clear stubs first because of dependencies. 605 // Clear stubs first because of dependencies.
642 stubs_.clear(); 606 stubs_.clear();
643 607
644 message_queue_->DeleteAndDisableMessages(gpu_channel_manager_); 608 message_queue_->DeleteAndDisableMessages(gpu_channel_manager_);
645 609
(...skipping 23 matching lines...) Expand all
669 633
670 channel_->AddFilter(filter_.get()); 634 channel_->AddFilter(filter_.get());
671 635
672 return channel_handle; 636 return channel_handle;
673 } 637 }
674 638
675 base::ProcessId GpuChannel::GetClientPID() const { 639 base::ProcessId GpuChannel::GetClientPID() const {
676 return channel_->GetPeerPID(); 640 return channel_->GetPeerPID();
677 } 641 }
678 642
643 uint32_t GpuChannel::GetProcessedOrderNum() const {
644 return message_queue_->processed_order_num();
645 }
646
647 uint32_t GpuChannel::GetUnprocessedOrderNum() const {
648 return message_queue_->GetUnprocessedOrderNum();
649 }
650
679 bool GpuChannel::OnMessageReceived(const IPC::Message& message) { 651 bool GpuChannel::OnMessageReceived(const IPC::Message& message) {
680 // All messages should be pushed to channel_messages_ and handled separately. 652 // All messages should be pushed to channel_messages_ and handled separately.
681 NOTREACHED(); 653 NOTREACHED();
682 return false; 654 return false;
683 } 655 }
684 656
685 void GpuChannel::OnChannelError() { 657 void GpuChannel::OnChannelError() {
686 gpu_channel_manager_->RemoveChannel(client_id_); 658 gpu_channel_manager_->RemoveChannel(client_id_);
687 } 659 }
688 660
(...skipping 20 matching lines...) Expand all
709 681
710 void GpuChannel::OnRemoveSubscription(unsigned int target) { 682 void GpuChannel::OnRemoveSubscription(unsigned int target) {
711 gpu_channel_manager()->Send( 683 gpu_channel_manager()->Send(
712 new GpuHostMsg_RemoveSubscription(client_id_, target)); 684 new GpuHostMsg_RemoveSubscription(client_id_, target));
713 } 685 }
714 686
715 void GpuChannel::StubSchedulingChanged(bool scheduled) { 687 void GpuChannel::StubSchedulingChanged(bool scheduled) {
716 bool a_stub_was_descheduled = num_stubs_descheduled_ > 0; 688 bool a_stub_was_descheduled = num_stubs_descheduled_ > 0;
717 if (scheduled) { 689 if (scheduled) {
718 num_stubs_descheduled_--; 690 num_stubs_descheduled_--;
719 message_queue_->ScheduleHandleMessage(); 691 ScheduleHandleMessage();
720 } else { 692 } else {
721 num_stubs_descheduled_++; 693 num_stubs_descheduled_++;
722 } 694 }
723 DCHECK_LE(num_stubs_descheduled_, stubs_.size()); 695 DCHECK_LE(num_stubs_descheduled_, stubs_.size());
724 bool a_stub_is_descheduled = num_stubs_descheduled_ > 0; 696 bool a_stub_is_descheduled = num_stubs_descheduled_ > 0;
725 697
726 if (a_stub_is_descheduled != a_stub_was_descheduled) { 698 if (a_stub_is_descheduled != a_stub_was_descheduled) {
727 if (preempting_flag_.get()) { 699 if (preempting_flag_.get()) {
728 io_task_runner_->PostTask( 700 io_task_runner_->PostTask(
729 FROM_HERE, 701 FROM_HERE,
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
857 OnDestroyCommandBuffer) 829 OnDestroyCommandBuffer)
858 IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuMsg_CreateJpegDecoder, 830 IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuMsg_CreateJpegDecoder,
859 OnCreateJpegDecoder) 831 OnCreateJpegDecoder)
860 IPC_MESSAGE_UNHANDLED(handled = false) 832 IPC_MESSAGE_UNHANDLED(handled = false)
861 IPC_END_MESSAGE_MAP() 833 IPC_END_MESSAGE_MAP()
862 DCHECK(handled) << msg.type(); 834 DCHECK(handled) << msg.type();
863 return handled; 835 return handled;
864 } 836 }
865 837
866 void GpuChannel::HandleMessage() { 838 void GpuChannel::HandleMessage() {
867 GpuChannelMessage* m = nullptr; 839 // If we have been preempted by another channel, just post a task to wake up.
868 GpuCommandBufferStub* stub = nullptr; 840 if (preempted_flag_ && preempted_flag_->IsSet()) {
869 bool has_more_messages = false; 841 ScheduleHandleMessage();
870 { 842 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 } 843 }
887 844
888 bool retry_message = false; 845 GpuChannelMessage* m = message_queue_->GetNextMessage();
889 stub = stubs_.get(m->message.routing_id()); 846
890 if (stub) { 847 // TODO(sunnyps): This could be a DCHECK maybe?
891 if (!stub->IsScheduled()) { 848 if (!m)
892 retry_message = true; 849 return;
850
851 uint32_t order_number = m->order_number;
852 const IPC::Message& message = m->message;
853 int32_t routing_id = message.routing_id();
854 GpuCommandBufferStub* stub = stubs_.get(routing_id);
855
856 DCHECK(!stub || stub->IsScheduled());
857
858 DVLOG(1) << "received message @" << &message << " on channel @" << this
859 << " with type " << message.type();
860
861 current_order_num_ = order_number;
862
863 bool handled = false;
864
865 if (routing_id == MSG_ROUTING_CONTROL) {
866 handled = OnControlMessageReceived(message);
867 } else if (message.type() == GpuCommandBufferMsg_InsertSyncPoint::ID) {
868 // TODO(dyen): Temporary handling of old sync points.
869 // This must ensure that the sync point will be retired. Normally we'll
870 // find the stub based on the routing ID, and associate the sync point
871 // with it, but if that fails for any reason (channel or stub already
872 // deleted, invalid routing id), we need to retire the sync point
873 // immediately.
874 if (stub) {
875 stub->AddSyncPoint(m->sync_point, m->retire_sync_point);
876 } else {
877 gpu_channel_manager_->sync_point_manager()->RetireSyncPoint(
878 m->sync_point);
893 } 879 }
894 if (stub->IsPreempted()) { 880 handled = true;
895 retry_message = true; 881 } else {
896 message_queue_->ScheduleHandleMessage(); 882 handled = router_.RouteMessage(message);
897 }
898 } 883 }
899 884
900 if (retry_message) { 885 // Respond to sync messages even if router failed to route.
901 base::AutoLock auto_lock(message_queue_->channel_messages_lock_); 886 if (!handled && message.is_sync()) {
902 if (m->order_number == kOutOfOrderNumber) 887 IPC::Message* reply = IPC::SyncMessage::GenerateReply(&message);
903 message_queue_->out_of_order_messages_.push_front(m); 888 reply->set_reply_error();
904 else 889 Send(reply);
905 message_queue_->channel_messages_.push_front(m); 890 handled = true;
906 return;
907 } else if (has_more_messages) {
908 message_queue_->ScheduleHandleMessage();
909 } 891 }
910 892
911 scoped_ptr<GpuChannelMessage> scoped_message(m); 893 // A command buffer may be descheduled or preempted but only in the middle of
912 const uint32_t order_number = m->order_number; 894 // a flush. In this case we should not pop the message from the queue.
913 const int32_t routing_id = m->message.routing_id(); 895 if (stub && stub->HasUnprocessedCommands() &&
914 896 order_number != kOutOfOrderNumber) {
915 // TODO(dyen): Temporary handling of old sync points. 897 DCHECK_EQ((uint32_t)GpuCommandBufferMsg_AsyncFlush::ID, message.type());
916 // This must ensure that the sync point will be retired. Normally we'll 898 // If the stub is still scheduled then we were preempted and need to
917 // find the stub based on the routing ID, and associate the sync point 899 // schedule a wakeup otherwise some other event will wake us up e.g. sync
918 // with it, but if that fails for any reason (channel or stub already 900 // point completion. No DCHECK for preemption flag because that can change
919 // deleted, invalid routing id), we need to retire the sync point 901 // any time.
920 // immediately. 902 if (stub->IsScheduled())
921 if (m->message.type() == GpuCommandBufferMsg_InsertSyncPoint::ID) { 903 ScheduleHandleMessage();
922 const bool retire = m->retire_sync_point; 904 return;
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 } 905 }
937 906
938 IPC::Message* message = &m->message; 907 if (message_queue_->MessageProcessed(order_number)) {
939 bool message_processed = true; 908 ScheduleHandleMessage();
909 }
940 910
941 DVLOG(1) << "received message @" << message << " on channel @" << this 911 if (preempting_flag_) {
942 << " with type " << message->type(); 912 io_task_runner_->PostTask(
913 FROM_HERE,
914 base::Bind(&GpuChannelMessageFilter::OnMessageProcessed, filter_));
915 }
916 }
943 917
944 if (order_number != kOutOfOrderNumber) { 918 void GpuChannel::ScheduleHandleMessage() {
945 // Make sure this is a valid unprocessed order number. 919 task_runner_->PostTask(FROM_HERE, base::Bind(&GpuChannel::HandleMessage,
946 DCHECK(order_number <= GetUnprocessedOrderNum() && 920 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 } 921 }
979 922
980 void GpuChannel::OnCreateOffscreenCommandBuffer( 923 void GpuChannel::OnCreateOffscreenCommandBuffer(
981 const gfx::Size& size, 924 const gfx::Size& size,
982 const GPUCreateCommandBufferConfig& init_params, 925 const GPUCreateCommandBufferConfig& init_params,
983 int32 route_id, 926 int32 route_id,
984 bool* succeeded) { 927 bool* succeeded) {
985 TRACE_EVENT1("gpu", "GpuChannel::OnCreateOffscreenCommandBuffer", "route_id", 928 TRACE_EVENT1("gpu", "GpuChannel::OnCreateOffscreenCommandBuffer", "route_id",
986 route_id); 929 route_id);
987 930
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
1071 } 1014 }
1072 } 1015 }
1073 1016
1074 void GpuChannel::OnCreateJpegDecoder(int32 route_id, IPC::Message* reply_msg) { 1017 void GpuChannel::OnCreateJpegDecoder(int32 route_id, IPC::Message* reply_msg) {
1075 if (!jpeg_decoder_) { 1018 if (!jpeg_decoder_) {
1076 jpeg_decoder_.reset(new GpuJpegDecodeAccelerator(this, io_task_runner_)); 1019 jpeg_decoder_.reset(new GpuJpegDecodeAccelerator(this, io_task_runner_));
1077 } 1020 }
1078 jpeg_decoder_->AddClient(route_id, reply_msg); 1021 jpeg_decoder_->AddClient(route_id, reply_msg);
1079 } 1022 }
1080 1023
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, 1024 void GpuChannel::CacheShader(const std::string& key,
1095 const std::string& shader) { 1025 const std::string& shader) {
1096 gpu_channel_manager_->Send( 1026 gpu_channel_manager_->Send(
1097 new GpuHostMsg_CacheShader(client_id_, key, shader)); 1027 new GpuHostMsg_CacheShader(client_id_, key, shader));
1098 } 1028 }
1099 1029
1100 void GpuChannel::AddFilter(IPC::MessageFilter* filter) { 1030 void GpuChannel::AddFilter(IPC::MessageFilter* filter) {
1101 io_task_runner_->PostTask( 1031 io_task_runner_->PostTask(
1102 FROM_HERE, base::Bind(&GpuChannelMessageFilter::AddChannelFilter, 1032 FROM_HERE, base::Bind(&GpuChannelMessageFilter::AddChannelFilter,
1103 filter_, make_scoped_refptr(filter))); 1033 filter_, make_scoped_refptr(filter)));
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
1153 client_id_); 1083 client_id_);
1154 } 1084 }
1155 } 1085 }
1156 } 1086 }
1157 1087
1158 void GpuChannel::HandleUpdateValueState( 1088 void GpuChannel::HandleUpdateValueState(
1159 unsigned int target, const gpu::ValueState& state) { 1089 unsigned int target, const gpu::ValueState& state) {
1160 pending_valuebuffer_state_->UpdateState(target, state); 1090 pending_valuebuffer_state_->UpdateState(target, state);
1161 } 1091 }
1162 1092
1163 uint32_t GpuChannel::GetUnprocessedOrderNum() const {
1164 return message_queue_->GetUnprocessedOrderNum();
1165 }
1166
1167 } // namespace content 1093 } // namespace content
OLDNEW
« no previous file with comments | « content/common/gpu/gpu_channel.h ('k') | content/common/gpu/gpu_channel_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698