aw: Move SharedRendererState out of AwContents
[chromium-blink-merge.git] / cc / resources / zero_copy_raster_worker_pool.cc
blobca2bc9b01e10ed5ec68ba2c5791a60d0eb72d02c
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_raster_worker_pool.h"
7 #include <algorithm>
9 #include "base/debug/trace_event.h"
10 #include "base/debug/trace_event_argument.h"
11 #include "base/strings/stringprintf.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 RasterWorkerPool::PlaybackToMemory(
35 gpu_memory_buffer->Map(), resource_->format(), resource_->size(),
36 gpu_memory_buffer->GetStride(), raster_source, rect, scale);
37 gpu_memory_buffer->Unmap();
40 private:
41 ResourceProvider::ScopedWriteLockGpuMemoryBuffer lock_;
42 const Resource* resource_;
44 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl);
47 } // namespace
49 // static
50 scoped_ptr<RasterWorkerPool> ZeroCopyRasterWorkerPool::Create(
51 base::SequencedTaskRunner* task_runner,
52 TaskGraphRunner* task_graph_runner,
53 ResourceProvider* resource_provider) {
54 return make_scoped_ptr<RasterWorkerPool>(new ZeroCopyRasterWorkerPool(
55 task_runner, task_graph_runner, resource_provider));
58 ZeroCopyRasterWorkerPool::ZeroCopyRasterWorkerPool(
59 base::SequencedTaskRunner* task_runner,
60 TaskGraphRunner* task_graph_runner,
61 ResourceProvider* resource_provider)
62 : task_runner_(task_runner),
63 task_graph_runner_(task_graph_runner),
64 namespace_token_(task_graph_runner->GetNamespaceToken()),
65 resource_provider_(resource_provider),
66 raster_finished_weak_ptr_factory_(this) {
69 ZeroCopyRasterWorkerPool::~ZeroCopyRasterWorkerPool() {
72 Rasterizer* ZeroCopyRasterWorkerPool::AsRasterizer() {
73 return this;
76 void ZeroCopyRasterWorkerPool::SetClient(RasterizerClient* client) {
77 client_ = client;
80 void ZeroCopyRasterWorkerPool::Shutdown() {
81 TRACE_EVENT0("cc", "ZeroCopyRasterWorkerPool::Shutdown");
83 TaskGraph empty;
84 task_graph_runner_->ScheduleTasks(namespace_token_, &empty);
85 task_graph_runner_->WaitForTasksToFinishRunning(namespace_token_);
88 void ZeroCopyRasterWorkerPool::ScheduleTasks(RasterTaskQueue* queue) {
89 TRACE_EVENT0("cc", "ZeroCopyRasterWorkerPool::ScheduleTasks");
91 if (raster_pending_.none())
92 TRACE_EVENT_ASYNC_BEGIN0("cc", "ScheduledTasks", this);
94 // Mark all task sets as pending.
95 raster_pending_.set();
97 unsigned priority = kRasterTaskPriorityBase;
99 graph_.Reset();
101 // Cancel existing OnRasterFinished callbacks.
102 raster_finished_weak_ptr_factory_.InvalidateWeakPtrs();
104 scoped_refptr<RasterizerTask> new_raster_finished_tasks[kNumberOfTaskSets];
106 size_t task_count[kNumberOfTaskSets] = {0};
108 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) {
109 new_raster_finished_tasks[task_set] = CreateRasterFinishedTask(
110 task_runner_.get(),
111 base::Bind(&ZeroCopyRasterWorkerPool::OnRasterFinished,
112 raster_finished_weak_ptr_factory_.GetWeakPtr(),
113 task_set));
116 for (RasterTaskQueue::Item::Vector::const_iterator it = queue->items.begin();
117 it != queue->items.end();
118 ++it) {
119 const RasterTaskQueue::Item& item = *it;
120 RasterTask* task = item.task;
121 DCHECK(!task->HasCompleted());
123 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) {
124 if (!item.task_sets[task_set])
125 continue;
127 ++task_count[task_set];
129 graph_.edges.push_back(
130 TaskGraph::Edge(task, new_raster_finished_tasks[task_set].get()));
133 InsertNodesForRasterTask(&graph_, task, task->dependencies(), priority++);
136 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) {
137 InsertNodeForTask(&graph_,
138 new_raster_finished_tasks[task_set].get(),
139 kRasterFinishedTaskPriority,
140 task_count[task_set]);
143 ScheduleTasksOnOriginThread(this, &graph_);
144 task_graph_runner_->ScheduleTasks(namespace_token_, &graph_);
146 std::copy(new_raster_finished_tasks,
147 new_raster_finished_tasks + kNumberOfTaskSets,
148 raster_finished_tasks_);
150 TRACE_EVENT_ASYNC_STEP_INTO1(
151 "cc", "ScheduledTasks", this, "rasterizing", "state", StateAsValue());
154 void ZeroCopyRasterWorkerPool::CheckForCompletedTasks() {
155 TRACE_EVENT0("cc", "ZeroCopyRasterWorkerPool::CheckForCompletedTasks");
157 task_graph_runner_->CollectCompletedTasks(namespace_token_,
158 &completed_tasks_);
159 for (Task::Vector::const_iterator it = completed_tasks_.begin();
160 it != completed_tasks_.end();
161 ++it) {
162 RasterizerTask* task = static_cast<RasterizerTask*>(it->get());
164 task->WillComplete();
165 task->CompleteOnOriginThread(this);
166 task->DidComplete();
168 task->RunReplyOnOriginThread();
170 completed_tasks_.clear();
173 scoped_ptr<RasterBuffer> ZeroCopyRasterWorkerPool::AcquireBufferForRaster(
174 const Resource* resource) {
175 return make_scoped_ptr<RasterBuffer>(
176 new RasterBufferImpl(resource_provider_, resource));
179 void ZeroCopyRasterWorkerPool::ReleaseBufferForRaster(
180 scoped_ptr<RasterBuffer> buffer) {
181 // Nothing to do here. RasterBufferImpl destructor cleans up after itself.
184 void ZeroCopyRasterWorkerPool::OnRasterFinished(TaskSet task_set) {
185 TRACE_EVENT1(
186 "cc", "ZeroCopyRasterWorkerPool::OnRasterFinished", "task_set", task_set);
188 DCHECK(raster_pending_[task_set]);
189 raster_pending_[task_set] = false;
190 if (raster_pending_.any()) {
191 TRACE_EVENT_ASYNC_STEP_INTO1(
192 "cc", "ScheduledTasks", this, "rasterizing", "state", StateAsValue());
193 } else {
194 TRACE_EVENT_ASYNC_END0("cc", "ScheduledTasks", this);
196 client_->DidFinishRunningTasks(task_set);
199 scoped_refptr<base::debug::ConvertableToTraceFormat>
200 ZeroCopyRasterWorkerPool::StateAsValue() const {
201 scoped_refptr<base::debug::TracedValue> state =
202 new base::debug::TracedValue();
204 state->BeginArray("tasks_pending");
205 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set)
206 state->AppendBoolean(raster_pending_[task_set]);
207 state->EndArray();
208 return state;
211 } // namespace cc