blob: 69afb8335541d546d232b3f9d6119d4961aa7e0f [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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.
4
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +01005#include "cc/resources/pixel_buffer_tile_task_worker_pool.h"
James Robinson646469d2014-10-03 15:33:28 -07006
7#include <algorithm>
8
9#include "base/containers/stack_container.h"
James Robinson646469d2014-10-03 15:33:28 -070010#include "base/strings/stringprintf.h"
Elliot Glayshereae49292015-01-28 10:47:32 -080011#include "base/trace_event/trace_event.h"
12#include "base/trace_event/trace_event_argument.h"
James Robinson646469d2014-10-03 15:33:28 -070013#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"
James Robinson646469d2014-10-03 15:33:28 -070017
18namespace cc {
19namespace {
20
21class RasterBufferImpl : public RasterBuffer {
22 public:
23 RasterBufferImpl(ResourceProvider* resource_provider,
24 const Resource* resource)
25 : resource_provider_(resource_provider),
26 resource_(resource),
James Robinsone2ac7e82014-10-15 13:21:59 -070027 memory_(NULL),
James Robinson646469d2014-10-03 15:33:28 -070028 stride_(0) {
29 resource_provider_->AcquirePixelBuffer(resource_->id());
James Robinsone2ac7e82014-10-15 13:21:59 -070030 memory_ = resource_provider_->MapPixelBuffer(resource_->id(), &stride_);
James Robinson646469d2014-10-03 15:33:28 -070031 }
32
James Robinsone1b30cf2014-10-21 12:25:40 -070033 ~RasterBufferImpl() override {
James Robinson646469d2014-10-03 15:33:28 -070034 resource_provider_->ReleasePixelBuffer(resource_->id());
35 }
36
37 // Overridden from RasterBuffer:
James Robinson53b77582014-10-28 17:00:48 -070038 void Playback(const RasterSource* raster_source,
James Robinsone1b30cf2014-10-21 12:25:40 -070039 const gfx::Rect& rect,
James Robinson1ae030a2014-11-07 08:32:47 -080040 float scale) override {
James Robinsone2ac7e82014-10-15 13:21:59 -070041 if (!memory_)
James Robinson646469d2014-10-03 15:33:28 -070042 return;
43
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +010044 TileTaskWorkerPool::PlaybackToMemory(memory_, resource_->format(),
45 resource_->size(), stride_,
46 raster_source, rect, scale);
James Robinson646469d2014-10-03 15:33:28 -070047 }
48
49 private:
50 ResourceProvider* resource_provider_;
51 const Resource* resource_;
James Robinsone2ac7e82014-10-15 13:21:59 -070052 uint8_t* memory_;
James Robinson646469d2014-10-03 15:33:28 -070053 int stride_;
James Robinson646469d2014-10-03 15:33:28 -070054
55 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl);
56};
57
58const int kCheckForCompletedRasterTasksDelayMs = 6;
59
60const size_t kMaxScheduledRasterTasks = 48;
61
62typedef base::StackVector<RasterTask*, kMaxScheduledRasterTasks>
63 RasterTaskVector;
64
65TaskSetCollection 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;
70 }
71 return task_sets;
72}
73
74void 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]++;
79 }
80}
81
82void 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]--;
87 }
88}
89
90} // namespace
91
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +010092PixelBufferTileTaskWorkerPool::RasterTaskState::RasterTaskState(
James Robinson646469d2014-10-03 15:33:28 -070093 RasterTask* task,
94 const TaskSetCollection& task_sets)
95 : type(UNSCHEDULED), task(task), task_sets(task_sets) {
96}
97
98// static
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +010099scoped_ptr<TileTaskWorkerPool> PixelBufferTileTaskWorkerPool::Create(
James Robinson646469d2014-10-03 15:33:28 -0700100 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) {
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100105 return make_scoped_ptr<TileTaskWorkerPool>(new PixelBufferTileTaskWorkerPool(
106 task_runner, task_graph_runner, context_provider, resource_provider,
107 max_transfer_buffer_usage_bytes));
James Robinson646469d2014-10-03 15:33:28 -0700108}
109
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100110PixelBufferTileTaskWorkerPool::PixelBufferTileTaskWorkerPool(
James Robinson646469d2014-10-03 15:33:28 -0700111 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,
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100128 base::Bind(
129 &PixelBufferTileTaskWorkerPool::CheckForCompletedRasterTasks,
130 base::Unretained(this)),
James Robinson646469d2014-10-03 15:33:28 -0700131 base::TimeDelta::FromMilliseconds(
132 kCheckForCompletedRasterTasksDelayMs)),
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100133 task_set_finished_weak_ptr_factory_(this) {
James Robinson646469d2014-10-03 15:33:28 -0700134 DCHECK(context_provider_);
135 std::fill(task_counts_, task_counts_ + kNumberOfTaskSets, 0);
136}
137
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100138PixelBufferTileTaskWorkerPool::~PixelBufferTileTaskWorkerPool() {
James Robinson646469d2014-10-03 15:33:28 -0700139 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());
144}
145
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100146TileTaskRunner* PixelBufferTileTaskWorkerPool::AsTileTaskRunner() {
147 return this;
148}
James Robinson646469d2014-10-03 15:33:28 -0700149
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100150void PixelBufferTileTaskWorkerPool::SetClient(TileTaskRunnerClient* client) {
James Robinson646469d2014-10-03 15:33:28 -0700151 client_ = client;
152}
153
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100154void PixelBufferTileTaskWorkerPool::Shutdown() {
155 TRACE_EVENT0("cc", "PixelBufferTileTaskWorkerPool::Shutdown");
James Robinson646469d2014-10-03 15:33:28 -0700156
157 shutdown_ = true;
158
159 TaskGraph empty;
160 task_graph_runner_->ScheduleTasks(namespace_token_, &empty);
161 task_graph_runner_->WaitForTasksToFinishRunning(namespace_token_);
162
163 CheckForCompletedRasterizerTasks();
164 CheckForCompletedUploads();
165
James Robinson7f480212014-10-31 10:28:08 -0700166 check_for_completed_raster_task_notifier_.Shutdown();
James Robinson646469d2014-10-03 15:33:28 -0700167
168 for (RasterTaskState::Vector::iterator it = raster_task_states_.begin();
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100169 it != raster_task_states_.end(); ++it) {
James Robinson646469d2014-10-03 15:33:28 -0700170 RasterTaskState& state = *it;
171
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;
176 }
177 }
178 DCHECK_EQ(completed_raster_tasks_.size(), raster_task_states_.size());
179}
180
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100181void PixelBufferTileTaskWorkerPool::ScheduleTasks(TileTaskQueue* queue) {
182 TRACE_EVENT0("cc", "PixelBufferTileTaskWorkerPool::ScheduleTasks");
James Robinson646469d2014-10-03 15:33:28 -0700183
184 if (should_notify_client_if_no_tasks_are_pending_.none())
185 TRACE_EVENT_ASYNC_BEGIN0("cc", "ScheduledTasks", this);
186
187 should_notify_client_if_no_tasks_are_pending_.set();
188 std::fill(task_counts_, task_counts_ + kNumberOfTaskSets, 0);
189
190 // Update raster task state and remove items from old queue.
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100191 for (TileTaskQueue::Item::Vector::const_iterator it = queue->items.begin();
192 it != queue->items.end(); ++it) {
193 const TileTaskQueue::Item& item = *it;
James Robinson646469d2014-10-03 15:33:28 -0700194 RasterTask* task = item.task;
195
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.
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100199 TileTaskQueue::Item::Vector::iterator old_it =
200 std::find_if(raster_tasks_.items.begin(), raster_tasks_.items.end(),
201 TileTaskQueue::Item::TaskComparator(task));
James Robinson646469d2014-10-03 15:33:28 -0700202 if (old_it != raster_tasks_.items.end()) {
203 std::swap(*old_it, raster_tasks_.items.back());
204 raster_tasks_.items.pop_back();
205 }
206
207 RasterTaskState::Vector::iterator state_it =
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100208 std::find_if(raster_task_states_.begin(), raster_task_states_.end(),
James Robinson646469d2014-10-03 15:33:28 -0700209 RasterTaskState::TaskComparator(task));
210 if (state_it != raster_task_states_.end()) {
211 RasterTaskState& state = *state_it;
212
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);
219
220 continue;
221 }
222
223 DCHECK(!task->HasBeenScheduled());
224 raster_task_states_.push_back(RasterTaskState(task, item.task_sets));
225 AddTaskSetsToTaskCounts(task_counts_, item.task_sets);
226 }
227
228 // Determine what tasks in old queue need to be canceled.
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100229 for (TileTaskQueue::Item::Vector::const_iterator it =
James Robinson646469d2014-10-03 15:33:28 -0700230 raster_tasks_.items.begin();
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100231 it != raster_tasks_.items.end(); ++it) {
232 const TileTaskQueue::Item& item = *it;
James Robinson646469d2014-10-03 15:33:28 -0700233 RasterTask* task = item.task;
234
235 RasterTaskState::Vector::iterator state_it =
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100236 std::find_if(raster_task_states_.begin(), raster_task_states_.end(),
James Robinson646469d2014-10-03 15:33:28 -0700237 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;
242
243 RasterTaskState& state = *state_it;
244
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;
253 }
254
255 // No longer in any task set.
256 state.task_sets.reset();
257 }
258
259 raster_tasks_.Swap(queue);
260
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();
267
268 // Schedule new tasks.
269 ScheduleMoreTasks();
270
271 // Reschedule check for completed raster tasks.
272 check_for_completed_raster_task_notifier_.Schedule();
273
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100274 TRACE_EVENT_ASYNC_STEP_INTO1("cc", "ScheduledTasks", this, StateName(),
275 "state", StateAsValue());
James Robinson646469d2014-10-03 15:33:28 -0700276}
277
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100278void PixelBufferTileTaskWorkerPool::CheckForCompletedTasks() {
279 TRACE_EVENT0("cc", "PixelBufferTileTaskWorkerPool::CheckForCompletedTasks");
James Robinson646469d2014-10-03 15:33:28 -0700280
281 CheckForCompletedRasterizerTasks();
282 CheckForCompletedUploads();
283 FlushUploads();
284
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100285 for (TileTask::Vector::const_iterator it =
James Robinson646469d2014-10-03 15:33:28 -0700286 completed_image_decode_tasks_.begin();
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100287 it != completed_image_decode_tasks_.end(); ++it) {
288 TileTask* task = it->get();
James Robinson646469d2014-10-03 15:33:28 -0700289 task->RunReplyOnOriginThread();
290 }
291 completed_image_decode_tasks_.clear();
292
293 for (RasterTask::Vector::const_iterator it = completed_raster_tasks_.begin();
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100294 it != completed_raster_tasks_.end(); ++it) {
James Robinson646469d2014-10-03 15:33:28 -0700295 RasterTask* task = it->get();
296 RasterTaskState::Vector::iterator state_it =
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100297 std::find_if(raster_task_states_.begin(), raster_task_states_.end(),
James Robinson646469d2014-10-03 15:33:28 -0700298 RasterTaskState::TaskComparator(task));
299 DCHECK(state_it != raster_task_states_.end());
300 DCHECK_EQ(RasterTaskState::COMPLETED, state_it->type);
301
302 std::swap(*state_it, raster_task_states_.back());
303 raster_task_states_.pop_back();
304
305 task->RunReplyOnOriginThread();
306 }
307 completed_raster_tasks_.clear();
308}
309
Elliot Glayshereae49292015-01-28 10:47:32 -0800310ResourceFormat PixelBufferTileTaskWorkerPool::GetResourceFormat() {
311 return resource_provider_->memory_efficient_texture_format();
312}
313
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100314scoped_ptr<RasterBuffer> PixelBufferTileTaskWorkerPool::AcquireBufferForRaster(
James Robinson646469d2014-10-03 15:33:28 -0700315 const Resource* resource) {
316 return make_scoped_ptr<RasterBuffer>(
317 new RasterBufferImpl(resource_provider_, resource));
318}
319
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100320void PixelBufferTileTaskWorkerPool::ReleaseBufferForRaster(
James Robinson646469d2014-10-03 15:33:28 -0700321 scoped_ptr<RasterBuffer> buffer) {
322 // Nothing to do here. RasterBufferImpl destructor cleans up after itself.
323}
324
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100325void PixelBufferTileTaskWorkerPool::OnTaskSetFinished(TaskSet task_set) {
326 TRACE_EVENT2("cc", "PixelBufferTileTaskWorkerPool::OnTaskSetFinished",
327 "task_set", task_set,
James Robinson646469d2014-10-03 15:33:28 -0700328 "should_notify_client_if_no_tasks_are_pending",
329 should_notify_client_if_no_tasks_are_pending_[task_set]);
330
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;
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100335 task_set_finished_tasks_pending_[task_set] = false;
James Robinson646469d2014-10-03 15:33:28 -0700336
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();
341}
342
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100343void PixelBufferTileTaskWorkerPool::FlushUploads() {
James Robinson646469d2014-10-03 15:33:28 -0700344 if (!has_performed_uploads_since_last_flush_)
345 return;
346
347 context_provider_->ContextGL()->ShallowFlushCHROMIUM();
348 has_performed_uploads_since_last_flush_ = false;
349}
350
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100351void PixelBufferTileTaskWorkerPool::CheckForCompletedUploads() {
James Robinson646469d2014-10-03 15:33:28 -0700352 RasterTask::Vector tasks_with_completed_uploads;
353
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();
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100357 DCHECK(std::find_if(raster_task_states_.begin(), raster_task_states_.end(),
James Robinson646469d2014-10-03 15:33:28 -0700358 RasterTaskState::TaskComparator(task)) !=
359 raster_task_states_.end());
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100360 DCHECK_EQ(
361 RasterTaskState::UPLOADING,
362 std::find_if(raster_task_states_.begin(), raster_task_states_.end(),
363 RasterTaskState::TaskComparator(task))->type);
James Robinson646469d2014-10-03 15:33:28 -0700364
365 // Uploads complete in the order they are issued.
366 if (!resource_provider_->DidSetPixelsComplete(task->resource()->id()))
367 break;
368
369 tasks_with_completed_uploads.push_back(task);
370 raster_tasks_with_pending_upload_.pop_front();
371 }
372
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();
378
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 =
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100385 std::find_if(raster_task_states_.begin(), raster_task_states_.end(),
James Robinson646469d2014-10-03 15:33:28 -0700386 RasterTaskState::TaskComparator(task));
387 DCHECK(state_it != raster_task_states_.end());
388 const RasterTaskState& state = *state_it;
389
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;
398 }
399
400 ++it;
401 }
402
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();
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100407 it != tasks_with_uploads_to_force.rend(); ++it) {
James Robinson646469d2014-10-03 15:33:28 -0700408 RasterTask* task = it->get();
409
410 resource_provider_->ForceSetPixelsToComplete(task->resource()->id());
411 has_performed_uploads_since_last_flush_ = true;
412 }
413 }
414
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();
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100419 it != tasks_with_completed_uploads.end(); ++it) {
James Robinson646469d2014-10-03 15:33:28 -0700420 RasterTask* task = it->get();
421 RasterTaskState::Vector::iterator state_it =
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100422 std::find_if(raster_task_states_.begin(), raster_task_states_.end(),
James Robinson646469d2014-10-03 15:33:28 -0700423 RasterTaskState::TaskComparator(task));
424 DCHECK(state_it != raster_task_states_.end());
425 RasterTaskState& state = *state_it;
426
427 bytes_pending_upload_ -= task->resource()->bytes();
428
429 task->WillComplete();
430 task->CompleteOnOriginThread(this);
431 task->DidComplete();
432
James Robinson646469d2014-10-03 15:33:28 -0700433 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);
442 }
443}
444
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100445void PixelBufferTileTaskWorkerPool::CheckForCompletedRasterTasks() {
James Robinson646469d2014-10-03 15:33:28 -0700446 TRACE_EVENT0("cc",
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100447 "PixelBufferTileTaskWorkerPool::CheckForCompletedRasterTasks");
James Robinson646469d2014-10-03 15:33:28 -0700448
449 // Since this function can be called directly, cancel any pending checks.
450 check_for_completed_raster_task_notifier_.Cancel();
451
452 DCHECK(should_notify_client_if_no_tasks_are_pending_.any());
453
454 CheckForCompletedRasterizerTasks();
455 CheckForCompletedUploads();
456 FlushUploads();
457
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_ &
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100461 ~task_set_finished_tasks_pending_ & ~PendingTasks();
James Robinson646469d2014-10-03 15:33:28 -0700462
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;
466
467 scheduled_raster_task_count_ = 0;
468 if (PendingRasterTaskCount())
469 ScheduleMoreTasks();
470
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100471 TRACE_EVENT_ASYNC_STEP_INTO1("cc", "ScheduledTasks", this, StateName(),
472 "state", StateAsValue());
James Robinson646469d2014-10-03 15:33:28 -0700473
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();
478
479 if (should_notify_client_if_no_tasks_are_pending_.none())
480 TRACE_EVENT_ASYNC_END0("cc", "ScheduledTasks", this);
481
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]);
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100486 client_->DidFinishRunningTileTasks(task_set);
James Robinson646469d2014-10-03 15:33:28 -0700487 }
488 }
489}
490
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100491void PixelBufferTileTaskWorkerPool::ScheduleMoreTasks() {
492 TRACE_EVENT0("cc", "PixelBufferTileTaskWorkerPool::ScheduleMoreTasks");
James Robinson646469d2014-10-03 15:33:28 -0700493
494 RasterTaskVector tasks[kNumberOfTaskSets];
495
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100496 unsigned priority = kTileTaskPriorityBase;
James Robinson646469d2014-10-03 15:33:28 -0700497
498 graph_.Reset();
499
500 size_t bytes_pending_upload = bytes_pending_upload_;
501 TaskSetCollection did_throttle_raster_tasks;
502 size_t scheduled_raster_task_count = 0;
503
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100504 for (TileTaskQueue::Item::Vector::const_iterator it =
James Robinson646469d2014-10-03 15:33:28 -0700505 raster_tasks_.items.begin();
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100506 it != raster_tasks_.items.end(); ++it) {
507 const TileTaskQueue::Item& item = *it;
James Robinson646469d2014-10-03 15:33:28 -0700508 RasterTask* task = item.task;
509 DCHECK(item.task_sets.any());
510
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 =
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100514 std::find_if(raster_task_states_.begin(), raster_task_states_.end(),
James Robinson646469d2014-10-03 15:33:28 -0700515 RasterTaskState::TaskComparator(task));
516 if (state_it == raster_task_states_.end())
517 continue;
518
519 RasterTaskState& state = *state_it;
520
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;
527 }
528
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;
538 }
539
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;
545 }
546
547 // Throttle raster tasks based on kMaxScheduledRasterTasks.
548 if (scheduled_raster_task_count >= kMaxScheduledRasterTasks) {
549 did_throttle_raster_tasks |= item.task_sets;
550 continue;
551 }
552
553 // Update |bytes_pending_upload| now that task has cleared all
554 // throttling limits.
555 bytes_pending_upload = new_bytes_pending_upload;
556
557 DCHECK(state.type == RasterTaskState::UNSCHEDULED ||
558 state.type == RasterTaskState::SCHEDULED);
559 state.type = RasterTaskState::SCHEDULED;
560
561 InsertNodesForRasterTask(&graph_, task, task->dependencies(), priority++);
562
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);
567 }
568 }
569
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100570 // Cancel existing OnTaskSetFinished callbacks.
571 task_set_finished_weak_ptr_factory_.InvalidateWeakPtrs();
James Robinson646469d2014-10-03 15:33:28 -0700572
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100573 scoped_refptr<TileTask> new_task_set_finished_tasks[kNumberOfTaskSets];
James Robinson646469d2014-10-03 15:33:28 -0700574 size_t scheduled_task_counts[kNumberOfTaskSets] = {0};
575
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]);
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100579 // Schedule OnTaskSetFinished call for task set only when notification is
James Robinson646469d2014-10-03 15:33:28 -0700580 // 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]) {
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100584 new_task_set_finished_tasks[task_set] = CreateTaskSetFinishedTask(
James Robinson646469d2014-10-03 15:33:28 -0700585 task_runner_.get(),
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100586 base::Bind(&PixelBufferTileTaskWorkerPool::OnTaskSetFinished,
587 task_set_finished_weak_ptr_factory_.GetWeakPtr(),
James Robinson646469d2014-10-03 15:33:28 -0700588 task_set));
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100589 task_set_finished_tasks_pending_[task_set] = true;
590 InsertNodeForTask(&graph_, new_task_set_finished_tasks[task_set].get(),
Benjamin Lermancdfc88d2015-02-03 14:35:12 +0100591 kTaskSetFinishedTaskPriorityBase + task_set,
James Robinson646469d2014-10-03 15:33:28 -0700592 scheduled_task_counts[task_set]);
593 for (RasterTaskVector::ContainerType::const_iterator it =
594 tasks[task_set].container().begin();
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100595 it != tasks[task_set].container().end(); ++it) {
James Robinson646469d2014-10-03 15:33:28 -0700596 graph_.edges.push_back(
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100597 TaskGraph::Edge(*it, new_task_set_finished_tasks[task_set].get()));
James Robinson646469d2014-10-03 15:33:28 -0700598 }
599 }
600 }
601
602 DCHECK_LE(scheduled_raster_task_count, PendingRasterTaskCount());
603
604 ScheduleTasksOnOriginThread(this, &graph_);
605 task_graph_runner_->ScheduleTasks(namespace_token_, &graph_);
606
607 scheduled_raster_task_count_ = scheduled_raster_task_count;
608
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100609 std::copy(new_task_set_finished_tasks,
610 new_task_set_finished_tasks + kNumberOfTaskSets,
611 task_set_finished_tasks_);
James Robinson646469d2014-10-03 15:33:28 -0700612}
613
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100614unsigned PixelBufferTileTaskWorkerPool::PendingRasterTaskCount() const {
James Robinson646469d2014-10-03 15:33:28 -0700615 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;
619}
620
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100621TaskSetCollection PixelBufferTileTaskWorkerPool::PendingTasks() const {
James Robinson646469d2014-10-03 15:33:28 -0700622 return NonEmptyTaskSetsFromTaskCounts(task_counts_);
623}
624
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100625const char* PixelBufferTileTaskWorkerPool::StateName() const {
James Robinson646469d2014-10-03 15:33:28 -0700626 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";
632
633 return "finishing";
634}
635
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100636void PixelBufferTileTaskWorkerPool::CheckForCompletedRasterizerTasks() {
637 TRACE_EVENT0(
638 "cc", "PixelBufferTileTaskWorkerPool::CheckForCompletedRasterizerTasks");
James Robinson646469d2014-10-03 15:33:28 -0700639
640 task_graph_runner_->CollectCompletedTasks(namespace_token_,
641 &completed_tasks_);
642 for (Task::Vector::const_iterator it = completed_tasks_.begin();
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100643 it != completed_tasks_.end(); ++it) {
644 TileTask* task = static_cast<TileTask*>(it->get());
James Robinson646469d2014-10-03 15:33:28 -0700645
646 RasterTask* raster_task = task->AsRasterTask();
647 if (!raster_task) {
648 task->WillComplete();
649 task->CompleteOnOriginThread(this);
650 task->DidComplete();
651
652 completed_image_decode_tasks_.push_back(task);
653 continue;
654 }
655
656 RasterTaskState::Vector::iterator state_it =
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100657 std::find_if(raster_task_states_.begin(), raster_task_states_.end(),
James Robinson646469d2014-10-03 15:33:28 -0700658 RasterTaskState::TaskComparator(raster_task));
659 DCHECK(state_it != raster_task_states_.end());
660
661 RasterTaskState& state = *state_it;
662 DCHECK_EQ(RasterTaskState::SCHEDULED, state.type);
663
664 resource_provider_->UnmapPixelBuffer(raster_task->resource()->id());
665
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();
673
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100674 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));
James Robinson646469d2014-10-03 15:33:28 -0700677 if (item_it != raster_tasks_.items.end()) {
678 state.type = RasterTaskState::UNSCHEDULED;
679 continue;
680 }
681
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;
692 }
693
694 resource_provider_->BeginSetPixels(raster_task->resource()->id());
695 has_performed_uploads_since_last_flush_ = true;
696
697 bytes_pending_upload_ += raster_task->resource()->bytes();
698 raster_tasks_with_pending_upload_.push_back(raster_task);
699 state.type = RasterTaskState::UPLOADING;
700 }
701 completed_tasks_.clear();
702}
703
704scoped_refptr<base::debug::ConvertableToTraceFormat>
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100705PixelBufferTileTaskWorkerPool::StateAsValue() const {
James Robinson646469d2014-10-03 15:33:28 -0700706 scoped_refptr<base::debug::TracedValue> state =
707 new base::debug::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;
719}
720
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100721void PixelBufferTileTaskWorkerPool::ThrottleStateAsValueInto(
James Robinson646469d2014-10-03 15:33:28 -0700722 base::debug::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_);
728}
729
730} // namespace cc