Re-land: C++ readability review
[chromium-blink-merge.git] / cc / resources / pixel_buffer_tile_task_worker_pool.cc
blobf74e066e1a33e058c510199580ad5979dde9abdc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/resources/pixel_buffer_tile_task_worker_pool.h"
7 #include <algorithm>
9 #include "base/containers/stack_container.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/trace_event/trace_event.h"
12 #include "base/trace_event/trace_event_argument.h"
13 #include "cc/debug/traced_value.h"
14 #include "cc/resources/raster_buffer.h"
15 #include "cc/resources/resource.h"
16 #include "gpu/command_buffer/client/gles2_interface.h"
18 namespace cc {
19 namespace {
21 class RasterBufferImpl : public RasterBuffer {
22 public:
23 RasterBufferImpl(ResourceProvider* resource_provider,
24 const Resource* resource)
25 : resource_provider_(resource_provider),
26 resource_(resource),
27 memory_(NULL),
28 stride_(0) {
29 resource_provider_->AcquirePixelBuffer(resource_->id());
30 memory_ = resource_provider_->MapPixelBuffer(resource_->id(), &stride_);
33 ~RasterBufferImpl() override {
34 resource_provider_->ReleasePixelBuffer(resource_->id());
37 // Overridden from RasterBuffer:
38 void Playback(const RasterSource* raster_source,
39 const gfx::Rect& rect,
40 float scale) override {
41 if (!memory_)
42 return;
44 TileTaskWorkerPool::PlaybackToMemory(memory_, resource_->format(),
45 resource_->size(), stride_,
46 raster_source, rect, scale);
49 private:
50 ResourceProvider* resource_provider_;
51 const Resource* resource_;
52 uint8_t* memory_;
53 int stride_;
55 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl);
58 const int kCheckForCompletedRasterTasksDelayMs = 6;
60 const size_t kMaxScheduledRasterTasks = 48;
62 typedef base::StackVector<RasterTask*, kMaxScheduledRasterTasks>
63 RasterTaskVector;
65 TaskSetCollection NonEmptyTaskSetsFromTaskCounts(const size_t* task_counts) {
66 TaskSetCollection task_sets;
67 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) {
68 if (task_counts[task_set])
69 task_sets[task_set] = true;
71 return task_sets;
74 void AddTaskSetsToTaskCounts(size_t* task_counts,
75 const TaskSetCollection& task_sets) {
76 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) {
77 if (task_sets[task_set])
78 task_counts[task_set]++;
82 void RemoveTaskSetsFromTaskCounts(size_t* task_counts,
83 const TaskSetCollection& task_sets) {
84 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) {
85 if (task_sets[task_set])
86 task_counts[task_set]--;
90 } // namespace
92 PixelBufferTileTaskWorkerPool::RasterTaskState::RasterTaskState(
93 RasterTask* task,
94 const TaskSetCollection& task_sets)
95 : type(UNSCHEDULED), task(task), task_sets(task_sets) {
98 // static
99 scoped_ptr<TileTaskWorkerPool> PixelBufferTileTaskWorkerPool::Create(
100 base::SequencedTaskRunner* task_runner,
101 TaskGraphRunner* task_graph_runner,
102 ContextProvider* context_provider,
103 ResourceProvider* resource_provider,
104 size_t max_transfer_buffer_usage_bytes) {
105 return make_scoped_ptr<TileTaskWorkerPool>(new PixelBufferTileTaskWorkerPool(
106 task_runner, task_graph_runner, context_provider, resource_provider,
107 max_transfer_buffer_usage_bytes));
110 PixelBufferTileTaskWorkerPool::PixelBufferTileTaskWorkerPool(
111 base::SequencedTaskRunner* task_runner,
112 TaskGraphRunner* task_graph_runner,
113 ContextProvider* context_provider,
114 ResourceProvider* resource_provider,
115 size_t max_transfer_buffer_usage_bytes)
116 : task_runner_(task_runner),
117 task_graph_runner_(task_graph_runner),
118 namespace_token_(task_graph_runner->GetNamespaceToken()),
119 context_provider_(context_provider),
120 resource_provider_(resource_provider),
121 shutdown_(false),
122 scheduled_raster_task_count_(0u),
123 bytes_pending_upload_(0u),
124 max_bytes_pending_upload_(max_transfer_buffer_usage_bytes),
125 has_performed_uploads_since_last_flush_(false),
126 check_for_completed_raster_task_notifier_(
127 task_runner,
128 base::Bind(
129 &PixelBufferTileTaskWorkerPool::CheckForCompletedRasterTasks,
130 base::Unretained(this)),
131 base::TimeDelta::FromMilliseconds(
132 kCheckForCompletedRasterTasksDelayMs)),
133 task_set_finished_weak_ptr_factory_(this) {
134 DCHECK(context_provider_);
135 std::fill(task_counts_, task_counts_ + kNumberOfTaskSets, 0);
138 PixelBufferTileTaskWorkerPool::~PixelBufferTileTaskWorkerPool() {
139 DCHECK_EQ(0u, raster_task_states_.size());
140 DCHECK_EQ(0u, raster_tasks_with_pending_upload_.size());
141 DCHECK_EQ(0u, completed_raster_tasks_.size());
142 DCHECK_EQ(0u, completed_image_decode_tasks_.size());
143 DCHECK(NonEmptyTaskSetsFromTaskCounts(task_counts_).none());
146 TileTaskRunner* PixelBufferTileTaskWorkerPool::AsTileTaskRunner() {
147 return this;
150 void PixelBufferTileTaskWorkerPool::SetClient(TileTaskRunnerClient* client) {
151 client_ = client;
154 void PixelBufferTileTaskWorkerPool::Shutdown() {
155 TRACE_EVENT0("cc", "PixelBufferTileTaskWorkerPool::Shutdown");
157 shutdown_ = true;
159 TaskGraph empty;
160 task_graph_runner_->ScheduleTasks(namespace_token_, &empty);
161 task_graph_runner_->WaitForTasksToFinishRunning(namespace_token_);
163 CheckForCompletedRasterizerTasks();
164 CheckForCompletedUploads();
166 check_for_completed_raster_task_notifier_.Shutdown();
168 for (RasterTaskState::Vector::iterator it = raster_task_states_.begin();
169 it != raster_task_states_.end(); ++it) {
170 RasterTaskState& state = *it;
172 // All unscheduled tasks need to be canceled.
173 if (state.type == RasterTaskState::UNSCHEDULED) {
174 completed_raster_tasks_.push_back(state.task);
175 state.type = RasterTaskState::COMPLETED;
178 DCHECK_EQ(completed_raster_tasks_.size(), raster_task_states_.size());
181 void PixelBufferTileTaskWorkerPool::ScheduleTasks(TileTaskQueue* queue) {
182 TRACE_EVENT0("cc", "PixelBufferTileTaskWorkerPool::ScheduleTasks");
184 if (should_notify_client_if_no_tasks_are_pending_.none())
185 TRACE_EVENT_ASYNC_BEGIN0("cc", "ScheduledTasks", this);
187 should_notify_client_if_no_tasks_are_pending_.set();
188 std::fill(task_counts_, task_counts_ + kNumberOfTaskSets, 0);
190 // Update raster task state and remove items from old queue.
191 for (TileTaskQueue::Item::Vector::const_iterator it = queue->items.begin();
192 it != queue->items.end(); ++it) {
193 const TileTaskQueue::Item& item = *it;
194 RasterTask* task = item.task;
196 // Remove any old items that are associated with this task. The result is
197 // that the old queue is left with all items not present in this queue,
198 // which we use below to determine what tasks need to be canceled.
199 TileTaskQueue::Item::Vector::iterator old_it =
200 std::find_if(raster_tasks_.items.begin(), raster_tasks_.items.end(),
201 TileTaskQueue::Item::TaskComparator(task));
202 if (old_it != raster_tasks_.items.end()) {
203 std::swap(*old_it, raster_tasks_.items.back());
204 raster_tasks_.items.pop_back();
207 RasterTaskState::Vector::iterator state_it =
208 std::find_if(raster_task_states_.begin(), raster_task_states_.end(),
209 RasterTaskState::TaskComparator(task));
210 if (state_it != raster_task_states_.end()) {
211 RasterTaskState& state = *state_it;
213 state.task_sets = item.task_sets;
214 // |raster_tasks_required_for_activation_count| accounts for all tasks
215 // that need to complete before we can send a "ready to activate" signal.
216 // Tasks that have already completed should not be part of this count.
217 if (state.type != RasterTaskState::COMPLETED)
218 AddTaskSetsToTaskCounts(task_counts_, item.task_sets);
220 continue;
223 DCHECK(!task->HasBeenScheduled());
224 raster_task_states_.push_back(RasterTaskState(task, item.task_sets));
225 AddTaskSetsToTaskCounts(task_counts_, item.task_sets);
228 // Determine what tasks in old queue need to be canceled.
229 for (TileTaskQueue::Item::Vector::const_iterator it =
230 raster_tasks_.items.begin();
231 it != raster_tasks_.items.end(); ++it) {
232 const TileTaskQueue::Item& item = *it;
233 RasterTask* task = item.task;
235 RasterTaskState::Vector::iterator state_it =
236 std::find_if(raster_task_states_.begin(), raster_task_states_.end(),
237 RasterTaskState::TaskComparator(task));
238 // We've already processed completion if we can't find a RasterTaskState for
239 // this task.
240 if (state_it == raster_task_states_.end())
241 continue;
243 RasterTaskState& state = *state_it;
245 // Unscheduled task can be canceled.
246 if (state.type == RasterTaskState::UNSCHEDULED) {
247 DCHECK(!task->HasBeenScheduled());
248 DCHECK(std::find(completed_raster_tasks_.begin(),
249 completed_raster_tasks_.end(),
250 task) == completed_raster_tasks_.end());
251 completed_raster_tasks_.push_back(task);
252 state.type = RasterTaskState::COMPLETED;
255 // No longer in any task set.
256 state.task_sets.reset();
259 raster_tasks_.Swap(queue);
261 // Check for completed tasks when ScheduleTasks() is called as
262 // priorities might have changed and this maximizes the number
263 // of top priority tasks that are scheduled.
264 CheckForCompletedRasterizerTasks();
265 CheckForCompletedUploads();
266 FlushUploads();
268 // Schedule new tasks.
269 ScheduleMoreTasks();
271 // Reschedule check for completed raster tasks.
272 check_for_completed_raster_task_notifier_.Schedule();
274 TRACE_EVENT_ASYNC_STEP_INTO1("cc", "ScheduledTasks", this, StateName(),
275 "state", StateAsValue());
278 void PixelBufferTileTaskWorkerPool::CheckForCompletedTasks() {
279 TRACE_EVENT0("cc", "PixelBufferTileTaskWorkerPool::CheckForCompletedTasks");
281 CheckForCompletedRasterizerTasks();
282 CheckForCompletedUploads();
283 FlushUploads();
285 for (TileTask::Vector::const_iterator it =
286 completed_image_decode_tasks_.begin();
287 it != completed_image_decode_tasks_.end(); ++it) {
288 TileTask* task = it->get();
289 task->RunReplyOnOriginThread();
291 completed_image_decode_tasks_.clear();
293 for (RasterTask::Vector::const_iterator it = completed_raster_tasks_.begin();
294 it != completed_raster_tasks_.end(); ++it) {
295 RasterTask* task = it->get();
296 RasterTaskState::Vector::iterator state_it =
297 std::find_if(raster_task_states_.begin(), raster_task_states_.end(),
298 RasterTaskState::TaskComparator(task));
299 DCHECK(state_it != raster_task_states_.end());
300 DCHECK_EQ(RasterTaskState::COMPLETED, state_it->type);
302 std::swap(*state_it, raster_task_states_.back());
303 raster_task_states_.pop_back();
305 task->RunReplyOnOriginThread();
307 completed_raster_tasks_.clear();
310 ResourceFormat PixelBufferTileTaskWorkerPool::GetResourceFormat() {
311 return resource_provider_->memory_efficient_texture_format();
314 scoped_ptr<RasterBuffer> PixelBufferTileTaskWorkerPool::AcquireBufferForRaster(
315 const Resource* resource) {
316 return make_scoped_ptr<RasterBuffer>(
317 new RasterBufferImpl(resource_provider_, resource));
320 void PixelBufferTileTaskWorkerPool::ReleaseBufferForRaster(
321 scoped_ptr<RasterBuffer> buffer) {
322 // Nothing to do here. RasterBufferImpl destructor cleans up after itself.
325 void PixelBufferTileTaskWorkerPool::OnTaskSetFinished(TaskSet task_set) {
326 TRACE_EVENT2("cc", "PixelBufferTileTaskWorkerPool::OnTaskSetFinished",
327 "task_set", task_set,
328 "should_notify_client_if_no_tasks_are_pending",
329 should_notify_client_if_no_tasks_are_pending_[task_set]);
331 // There's no need to call CheckForCompletedRasterTasks() if the client has
332 // already been notified.
333 if (!should_notify_client_if_no_tasks_are_pending_[task_set])
334 return;
335 task_set_finished_tasks_pending_[task_set] = false;
337 // This reduces latency between the time when all tasks required for
338 // activation have finished running and the time when the client is
339 // notified.
340 CheckForCompletedRasterTasks();
343 void PixelBufferTileTaskWorkerPool::FlushUploads() {
344 if (!has_performed_uploads_since_last_flush_)
345 return;
347 context_provider_->ContextGL()->ShallowFlushCHROMIUM();
348 has_performed_uploads_since_last_flush_ = false;
351 void PixelBufferTileTaskWorkerPool::CheckForCompletedUploads() {
352 RasterTask::Vector tasks_with_completed_uploads;
354 // First check if any have completed.
355 while (!raster_tasks_with_pending_upload_.empty()) {
356 RasterTask* task = raster_tasks_with_pending_upload_.front().get();
357 DCHECK(std::find_if(raster_task_states_.begin(), raster_task_states_.end(),
358 RasterTaskState::TaskComparator(task)) !=
359 raster_task_states_.end());
360 DCHECK_EQ(
361 RasterTaskState::UPLOADING,
362 std::find_if(raster_task_states_.begin(), raster_task_states_.end(),
363 RasterTaskState::TaskComparator(task))->type);
365 // Uploads complete in the order they are issued.
366 if (!resource_provider_->DidSetPixelsComplete(task->resource()->id()))
367 break;
369 tasks_with_completed_uploads.push_back(task);
370 raster_tasks_with_pending_upload_.pop_front();
373 DCHECK(client_);
374 TaskSetCollection tasks_that_should_be_forced_to_complete =
375 client_->TasksThatShouldBeForcedToComplete();
376 bool should_force_some_uploads_to_complete =
377 shutdown_ || tasks_that_should_be_forced_to_complete.any();
379 if (should_force_some_uploads_to_complete) {
380 RasterTask::Vector tasks_with_uploads_to_force;
381 RasterTaskDeque::iterator it = raster_tasks_with_pending_upload_.begin();
382 while (it != raster_tasks_with_pending_upload_.end()) {
383 RasterTask* task = it->get();
384 RasterTaskState::Vector::const_iterator state_it =
385 std::find_if(raster_task_states_.begin(), raster_task_states_.end(),
386 RasterTaskState::TaskComparator(task));
387 DCHECK(state_it != raster_task_states_.end());
388 const RasterTaskState& state = *state_it;
390 // Force all uploads to complete for which the client requests to do so.
391 // During shutdown, force all pending uploads to complete.
392 if (shutdown_ ||
393 (state.task_sets & tasks_that_should_be_forced_to_complete).any()) {
394 tasks_with_uploads_to_force.push_back(task);
395 tasks_with_completed_uploads.push_back(task);
396 it = raster_tasks_with_pending_upload_.erase(it);
397 continue;
400 ++it;
403 // Force uploads in reverse order. Since forcing can cause a wait on
404 // all previous uploads, we would rather wait only once downstream.
405 for (RasterTask::Vector::reverse_iterator it =
406 tasks_with_uploads_to_force.rbegin();
407 it != tasks_with_uploads_to_force.rend(); ++it) {
408 RasterTask* task = it->get();
410 resource_provider_->ForceSetPixelsToComplete(task->resource()->id());
411 has_performed_uploads_since_last_flush_ = true;
415 // Release shared memory and move tasks with completed uploads
416 // to |completed_raster_tasks_|.
417 for (RasterTask::Vector::const_iterator it =
418 tasks_with_completed_uploads.begin();
419 it != tasks_with_completed_uploads.end(); ++it) {
420 RasterTask* task = it->get();
421 RasterTaskState::Vector::iterator state_it =
422 std::find_if(raster_task_states_.begin(), raster_task_states_.end(),
423 RasterTaskState::TaskComparator(task));
424 DCHECK(state_it != raster_task_states_.end());
425 RasterTaskState& state = *state_it;
427 bytes_pending_upload_ -= task->resource()->bytes();
429 task->WillComplete();
430 task->CompleteOnOriginThread(this);
431 task->DidComplete();
433 DCHECK(std::find(completed_raster_tasks_.begin(),
434 completed_raster_tasks_.end(),
435 task) == completed_raster_tasks_.end());
436 completed_raster_tasks_.push_back(task);
437 state.type = RasterTaskState::COMPLETED;
438 // Triggers if the current task belongs to a set that should be empty.
439 DCHECK((state.task_sets & ~NonEmptyTaskSetsFromTaskCounts(task_counts_))
440 .none());
441 RemoveTaskSetsFromTaskCounts(task_counts_, state.task_sets);
445 void PixelBufferTileTaskWorkerPool::CheckForCompletedRasterTasks() {
446 TRACE_EVENT0("cc",
447 "PixelBufferTileTaskWorkerPool::CheckForCompletedRasterTasks");
449 // Since this function can be called directly, cancel any pending checks.
450 check_for_completed_raster_task_notifier_.Cancel();
452 DCHECK(should_notify_client_if_no_tasks_are_pending_.any());
454 CheckForCompletedRasterizerTasks();
455 CheckForCompletedUploads();
456 FlushUploads();
458 // Determine what client notifications to generate.
459 TaskSetCollection will_notify_client_that_no_tasks_are_pending =
460 should_notify_client_if_no_tasks_are_pending_ &
461 ~task_set_finished_tasks_pending_ & ~PendingTasks();
463 // Adjust the need to generate notifications before scheduling more tasks.
464 should_notify_client_if_no_tasks_are_pending_ &=
465 ~will_notify_client_that_no_tasks_are_pending;
467 scheduled_raster_task_count_ = 0;
468 if (PendingRasterTaskCount())
469 ScheduleMoreTasks();
471 TRACE_EVENT_ASYNC_STEP_INTO1("cc", "ScheduledTasks", this, StateName(),
472 "state", StateAsValue());
474 // Schedule another check for completed raster tasks while there are
475 // pending raster tasks or pending uploads.
476 if (PendingTasks().any())
477 check_for_completed_raster_task_notifier_.Schedule();
479 if (should_notify_client_if_no_tasks_are_pending_.none())
480 TRACE_EVENT_ASYNC_END0("cc", "ScheduledTasks", this);
482 // Generate client notifications.
483 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) {
484 if (will_notify_client_that_no_tasks_are_pending[task_set]) {
485 DCHECK(!PendingTasks()[task_set]);
486 client_->DidFinishRunningTileTasks(task_set);
491 void PixelBufferTileTaskWorkerPool::ScheduleMoreTasks() {
492 TRACE_EVENT0("cc", "PixelBufferTileTaskWorkerPool::ScheduleMoreTasks");
494 RasterTaskVector tasks[kNumberOfTaskSets];
496 unsigned priority = kTileTaskPriorityBase;
498 graph_.Reset();
500 size_t bytes_pending_upload = bytes_pending_upload_;
501 TaskSetCollection did_throttle_raster_tasks;
502 size_t scheduled_raster_task_count = 0;
504 for (TileTaskQueue::Item::Vector::const_iterator it =
505 raster_tasks_.items.begin();
506 it != raster_tasks_.items.end(); ++it) {
507 const TileTaskQueue::Item& item = *it;
508 RasterTask* task = item.task;
509 DCHECK(item.task_sets.any());
511 // |raster_task_states_| contains the state of all tasks that we have not
512 // yet run reply callbacks for.
513 RasterTaskState::Vector::iterator state_it =
514 std::find_if(raster_task_states_.begin(), raster_task_states_.end(),
515 RasterTaskState::TaskComparator(task));
516 if (state_it == raster_task_states_.end())
517 continue;
519 RasterTaskState& state = *state_it;
521 // Skip task if completed.
522 if (state.type == RasterTaskState::COMPLETED) {
523 DCHECK(std::find(completed_raster_tasks_.begin(),
524 completed_raster_tasks_.end(),
525 task) != completed_raster_tasks_.end());
526 continue;
529 // All raster tasks need to be throttled by bytes of pending uploads,
530 // but if it's the only task allow it to complete no matter what its size,
531 // to prevent starvation of the task queue.
532 size_t new_bytes_pending_upload = bytes_pending_upload;
533 new_bytes_pending_upload += task->resource()->bytes();
534 if (new_bytes_pending_upload > max_bytes_pending_upload_ &&
535 bytes_pending_upload) {
536 did_throttle_raster_tasks |= item.task_sets;
537 continue;
540 // If raster has finished, just update |bytes_pending_upload|.
541 if (state.type == RasterTaskState::UPLOADING) {
542 DCHECK(!task->HasCompleted());
543 bytes_pending_upload = new_bytes_pending_upload;
544 continue;
547 // Throttle raster tasks based on kMaxScheduledRasterTasks.
548 if (scheduled_raster_task_count >= kMaxScheduledRasterTasks) {
549 did_throttle_raster_tasks |= item.task_sets;
550 continue;
553 // Update |bytes_pending_upload| now that task has cleared all
554 // throttling limits.
555 bytes_pending_upload = new_bytes_pending_upload;
557 DCHECK(state.type == RasterTaskState::UNSCHEDULED ||
558 state.type == RasterTaskState::SCHEDULED);
559 state.type = RasterTaskState::SCHEDULED;
561 InsertNodesForRasterTask(&graph_, task, task->dependencies(), priority++);
563 ++scheduled_raster_task_count;
564 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) {
565 if (item.task_sets[task_set])
566 tasks[task_set].container().push_back(task);
570 // Cancel existing OnTaskSetFinished callbacks.
571 task_set_finished_weak_ptr_factory_.InvalidateWeakPtrs();
573 scoped_refptr<TileTask> new_task_set_finished_tasks[kNumberOfTaskSets];
574 size_t scheduled_task_counts[kNumberOfTaskSets] = {0};
576 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) {
577 scheduled_task_counts[task_set] = tasks[task_set].container().size();
578 DCHECK_LE(scheduled_task_counts[task_set], task_counts_[task_set]);
579 // Schedule OnTaskSetFinished call for task set only when notification is
580 // pending and throttling is not preventing all pending tasks in the set
581 // from being scheduled.
582 if (!did_throttle_raster_tasks[task_set] &&
583 should_notify_client_if_no_tasks_are_pending_[task_set]) {
584 new_task_set_finished_tasks[task_set] = CreateTaskSetFinishedTask(
585 task_runner_.get(),
586 base::Bind(&PixelBufferTileTaskWorkerPool::OnTaskSetFinished,
587 task_set_finished_weak_ptr_factory_.GetWeakPtr(),
588 task_set));
589 task_set_finished_tasks_pending_[task_set] = true;
590 InsertNodeForTask(&graph_, new_task_set_finished_tasks[task_set].get(),
591 kTaskSetFinishedTaskPriorityBase + task_set,
592 scheduled_task_counts[task_set]);
593 for (RasterTaskVector::ContainerType::const_iterator it =
594 tasks[task_set].container().begin();
595 it != tasks[task_set].container().end(); ++it) {
596 graph_.edges.push_back(
597 TaskGraph::Edge(*it, new_task_set_finished_tasks[task_set].get()));
602 DCHECK_LE(scheduled_raster_task_count, PendingRasterTaskCount());
604 ScheduleTasksOnOriginThread(this, &graph_);
605 task_graph_runner_->ScheduleTasks(namespace_token_, &graph_);
607 scheduled_raster_task_count_ = scheduled_raster_task_count;
609 std::copy(new_task_set_finished_tasks,
610 new_task_set_finished_tasks + kNumberOfTaskSets,
611 task_set_finished_tasks_);
614 unsigned PixelBufferTileTaskWorkerPool::PendingRasterTaskCount() const {
615 unsigned num_completed_raster_tasks =
616 raster_tasks_with_pending_upload_.size() + completed_raster_tasks_.size();
617 DCHECK_GE(raster_task_states_.size(), num_completed_raster_tasks);
618 return raster_task_states_.size() - num_completed_raster_tasks;
621 TaskSetCollection PixelBufferTileTaskWorkerPool::PendingTasks() const {
622 return NonEmptyTaskSetsFromTaskCounts(task_counts_);
625 const char* PixelBufferTileTaskWorkerPool::StateName() const {
626 if (scheduled_raster_task_count_)
627 return "rasterizing";
628 if (PendingRasterTaskCount())
629 return "throttled";
630 if (!raster_tasks_with_pending_upload_.empty())
631 return "waiting_for_uploads";
633 return "finishing";
636 void PixelBufferTileTaskWorkerPool::CheckForCompletedRasterizerTasks() {
637 TRACE_EVENT0(
638 "cc", "PixelBufferTileTaskWorkerPool::CheckForCompletedRasterizerTasks");
640 task_graph_runner_->CollectCompletedTasks(namespace_token_,
641 &completed_tasks_);
642 for (Task::Vector::const_iterator it = completed_tasks_.begin();
643 it != completed_tasks_.end(); ++it) {
644 TileTask* task = static_cast<TileTask*>(it->get());
646 RasterTask* raster_task = task->AsRasterTask();
647 if (!raster_task) {
648 task->WillComplete();
649 task->CompleteOnOriginThread(this);
650 task->DidComplete();
652 completed_image_decode_tasks_.push_back(task);
653 continue;
656 RasterTaskState::Vector::iterator state_it =
657 std::find_if(raster_task_states_.begin(), raster_task_states_.end(),
658 RasterTaskState::TaskComparator(raster_task));
659 DCHECK(state_it != raster_task_states_.end());
661 RasterTaskState& state = *state_it;
662 DCHECK_EQ(RasterTaskState::SCHEDULED, state.type);
664 resource_provider_->UnmapPixelBuffer(raster_task->resource()->id());
666 if (!raster_task->HasFinishedRunning()) {
667 // When priorites change, a raster task can be canceled as a result of
668 // no longer being of high enough priority to fit in our throttled
669 // raster task budget. The task has not yet completed in this case.
670 raster_task->WillComplete();
671 raster_task->CompleteOnOriginThread(this);
672 raster_task->DidComplete();
674 TileTaskQueue::Item::Vector::const_iterator item_it =
675 std::find_if(raster_tasks_.items.begin(), raster_tasks_.items.end(),
676 TileTaskQueue::Item::TaskComparator(raster_task));
677 if (item_it != raster_tasks_.items.end()) {
678 state.type = RasterTaskState::UNSCHEDULED;
679 continue;
682 DCHECK(std::find(completed_raster_tasks_.begin(),
683 completed_raster_tasks_.end(),
684 raster_task) == completed_raster_tasks_.end());
685 completed_raster_tasks_.push_back(raster_task);
686 state.type = RasterTaskState::COMPLETED;
687 // Triggers if the current task belongs to a set that should be empty.
688 DCHECK((state.task_sets & ~NonEmptyTaskSetsFromTaskCounts(task_counts_))
689 .none());
690 RemoveTaskSetsFromTaskCounts(task_counts_, state.task_sets);
691 continue;
694 resource_provider_->BeginSetPixels(raster_task->resource()->id());
695 has_performed_uploads_since_last_flush_ = true;
697 bytes_pending_upload_ += raster_task->resource()->bytes();
698 raster_tasks_with_pending_upload_.push_back(raster_task);
699 state.type = RasterTaskState::UPLOADING;
701 completed_tasks_.clear();
704 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
705 PixelBufferTileTaskWorkerPool::StateAsValue() const {
706 scoped_refptr<base::trace_event::TracedValue> state =
707 new base::trace_event::TracedValue();
708 state->SetInteger("completed_count", completed_raster_tasks_.size());
709 state->BeginArray("pending_count");
710 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set)
711 state->AppendInteger(task_counts_[task_set]);
712 state->EndArray();
713 state->SetInteger("pending_upload_count",
714 raster_tasks_with_pending_upload_.size());
715 state->BeginDictionary("throttle_state");
716 ThrottleStateAsValueInto(state.get());
717 state->EndDictionary();
718 return state;
721 void PixelBufferTileTaskWorkerPool::ThrottleStateAsValueInto(
722 base::trace_event::TracedValue* throttle_state) const {
723 throttle_state->SetInteger("bytes_available_for_upload",
724 max_bytes_pending_upload_ - bytes_pending_upload_);
725 throttle_state->SetInteger("bytes_pending_upload", bytes_pending_upload_);
726 throttle_state->SetInteger("scheduled_raster_task_count",
727 scheduled_raster_task_count_);
730 } // namespace cc