Re-land: C++ readability review
[chromium-blink-merge.git] / cc / resources / zero_copy_tile_task_worker_pool.cc
blob42e8ada4cd7b34fdf4c7dedd0094dce4122c9cfc
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/zero_copy_tile_task_worker_pool.h"
7 #include <algorithm>
9 #include "base/strings/stringprintf.h"
10 #include "base/trace_event/trace_event.h"
11 #include "base/trace_event/trace_event_argument.h"
12 #include "cc/debug/traced_value.h"
13 #include "cc/resources/raster_buffer.h"
14 #include "cc/resources/resource.h"
15 #include "ui/gfx/gpu_memory_buffer.h"
17 namespace cc {
18 namespace {
20 class RasterBufferImpl : public RasterBuffer {
21 public:
22 RasterBufferImpl(ResourceProvider* resource_provider,
23 const Resource* resource)
24 : lock_(resource_provider, resource->id()), resource_(resource) {}
26 // Overridden from RasterBuffer:
27 void Playback(const RasterSource* raster_source,
28 const gfx::Rect& rect,
29 float scale) override {
30 gfx::GpuMemoryBuffer* gpu_memory_buffer = lock_.GetGpuMemoryBuffer();
31 if (!gpu_memory_buffer)
32 return;
34 void* data = NULL;
35 bool rv = gpu_memory_buffer->Map(&data);
36 DCHECK(rv);
37 uint32 stride;
38 gpu_memory_buffer->GetStride(&stride);
39 TileTaskWorkerPool::PlaybackToMemory(data, resource_->format(),
40 resource_->size(), stride,
41 raster_source, rect, scale);
42 gpu_memory_buffer->Unmap();
45 private:
46 ResourceProvider::ScopedWriteLockGpuMemoryBuffer lock_;
47 const Resource* resource_;
49 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl);
52 } // namespace
54 // static
55 scoped_ptr<TileTaskWorkerPool> ZeroCopyTileTaskWorkerPool::Create(
56 base::SequencedTaskRunner* task_runner,
57 TaskGraphRunner* task_graph_runner,
58 ResourceProvider* resource_provider) {
59 return make_scoped_ptr<TileTaskWorkerPool>(new ZeroCopyTileTaskWorkerPool(
60 task_runner, task_graph_runner, resource_provider));
63 ZeroCopyTileTaskWorkerPool::ZeroCopyTileTaskWorkerPool(
64 base::SequencedTaskRunner* task_runner,
65 TaskGraphRunner* task_graph_runner,
66 ResourceProvider* resource_provider)
67 : task_runner_(task_runner),
68 task_graph_runner_(task_graph_runner),
69 namespace_token_(task_graph_runner->GetNamespaceToken()),
70 resource_provider_(resource_provider),
71 task_set_finished_weak_ptr_factory_(this) {
74 ZeroCopyTileTaskWorkerPool::~ZeroCopyTileTaskWorkerPool() {
77 TileTaskRunner* ZeroCopyTileTaskWorkerPool::AsTileTaskRunner() {
78 return this;
81 void ZeroCopyTileTaskWorkerPool::SetClient(TileTaskRunnerClient* client) {
82 client_ = client;
85 void ZeroCopyTileTaskWorkerPool::Shutdown() {
86 TRACE_EVENT0("cc", "ZeroCopyTileTaskWorkerPool::Shutdown");
88 TaskGraph empty;
89 task_graph_runner_->ScheduleTasks(namespace_token_, &empty);
90 task_graph_runner_->WaitForTasksToFinishRunning(namespace_token_);
93 void ZeroCopyTileTaskWorkerPool::ScheduleTasks(TileTaskQueue* queue) {
94 TRACE_EVENT0("cc", "ZeroCopyTileTaskWorkerPool::ScheduleTasks");
96 if (tasks_pending_.none())
97 TRACE_EVENT_ASYNC_BEGIN0("cc", "ScheduledTasks", this);
99 // Mark all task sets as pending.
100 tasks_pending_.set();
102 unsigned priority = kTileTaskPriorityBase;
104 graph_.Reset();
106 // Cancel existing OnTaskSetFinished callbacks.
107 task_set_finished_weak_ptr_factory_.InvalidateWeakPtrs();
109 scoped_refptr<TileTask> new_task_set_finished_tasks[kNumberOfTaskSets];
111 size_t task_count[kNumberOfTaskSets] = {0};
113 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) {
114 new_task_set_finished_tasks[task_set] = CreateTaskSetFinishedTask(
115 task_runner_.get(),
116 base::Bind(&ZeroCopyTileTaskWorkerPool::OnTaskSetFinished,
117 task_set_finished_weak_ptr_factory_.GetWeakPtr(), task_set));
120 for (TileTaskQueue::Item::Vector::const_iterator it = queue->items.begin();
121 it != queue->items.end(); ++it) {
122 const TileTaskQueue::Item& item = *it;
123 RasterTask* task = item.task;
124 DCHECK(!task->HasCompleted());
126 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) {
127 if (!item.task_sets[task_set])
128 continue;
130 ++task_count[task_set];
132 graph_.edges.push_back(
133 TaskGraph::Edge(task, new_task_set_finished_tasks[task_set].get()));
136 InsertNodesForRasterTask(&graph_, task, task->dependencies(), priority++);
139 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) {
140 InsertNodeForTask(&graph_, new_task_set_finished_tasks[task_set].get(),
141 kTaskSetFinishedTaskPriorityBase + task_set,
142 task_count[task_set]);
145 ScheduleTasksOnOriginThread(this, &graph_);
146 task_graph_runner_->ScheduleTasks(namespace_token_, &graph_);
148 std::copy(new_task_set_finished_tasks,
149 new_task_set_finished_tasks + kNumberOfTaskSets,
150 task_set_finished_tasks_);
152 TRACE_EVENT_ASYNC_STEP_INTO1("cc", "ScheduledTasks", this, "running", "state",
153 StateAsValue());
156 void ZeroCopyTileTaskWorkerPool::CheckForCompletedTasks() {
157 TRACE_EVENT0("cc", "ZeroCopyTileTaskWorkerPool::CheckForCompletedTasks");
159 task_graph_runner_->CollectCompletedTasks(namespace_token_,
160 &completed_tasks_);
161 for (Task::Vector::const_iterator it = completed_tasks_.begin();
162 it != completed_tasks_.end(); ++it) {
163 TileTask* task = static_cast<TileTask*>(it->get());
165 task->WillComplete();
166 task->CompleteOnOriginThread(this);
167 task->DidComplete();
169 task->RunReplyOnOriginThread();
171 completed_tasks_.clear();
174 ResourceFormat ZeroCopyTileTaskWorkerPool::GetResourceFormat() {
175 return resource_provider_->best_texture_format();
178 scoped_ptr<RasterBuffer> ZeroCopyTileTaskWorkerPool::AcquireBufferForRaster(
179 const Resource* resource) {
180 return make_scoped_ptr<RasterBuffer>(
181 new RasterBufferImpl(resource_provider_, resource));
184 void ZeroCopyTileTaskWorkerPool::ReleaseBufferForRaster(
185 scoped_ptr<RasterBuffer> buffer) {
186 // Nothing to do here. RasterBufferImpl destructor cleans up after itself.
189 void ZeroCopyTileTaskWorkerPool::OnTaskSetFinished(TaskSet task_set) {
190 TRACE_EVENT1("cc", "ZeroCopyTileTaskWorkerPool::OnTaskSetFinished",
191 "task_set", task_set);
193 DCHECK(tasks_pending_[task_set]);
194 tasks_pending_[task_set] = false;
195 if (tasks_pending_.any()) {
196 TRACE_EVENT_ASYNC_STEP_INTO1("cc", "ScheduledTasks", this, "running",
197 "state", StateAsValue());
198 } else {
199 TRACE_EVENT_ASYNC_END0("cc", "ScheduledTasks", this);
201 client_->DidFinishRunningTileTasks(task_set);
204 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
205 ZeroCopyTileTaskWorkerPool::StateAsValue() const {
206 scoped_refptr<base::trace_event::TracedValue> state =
207 new base::trace_event::TracedValue();
209 state->BeginArray("tasks_pending");
210 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set)
211 state->AppendBoolean(tasks_pending_[task_set]);
212 state->EndArray();
213 return state;
216 } // namespace cc