1 // Copyright 2014 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/raster/bitmap_tile_task_worker_pool.h"
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/playback/raster_source.h"
14 #include "cc/raster/raster_buffer.h"
15 #include "cc/resources/resource.h"
20 class RasterBufferImpl
: public RasterBuffer
{
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 TileTaskWorkerPool::PlaybackToMemory(lock_
.sk_bitmap().getPixels(),
31 resource_
->format(), resource_
->size(),
32 0, raster_source
, rect
, scale
);
36 ResourceProvider::ScopedWriteLockSoftware lock_
;
37 const Resource
* resource_
;
39 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl
);
45 scoped_ptr
<TileTaskWorkerPool
> BitmapTileTaskWorkerPool::Create(
46 base::SequencedTaskRunner
* task_runner
,
47 TaskGraphRunner
* task_graph_runner
,
48 ResourceProvider
* resource_provider
) {
49 return make_scoped_ptr
<TileTaskWorkerPool
>(new BitmapTileTaskWorkerPool(
50 task_runner
, task_graph_runner
, resource_provider
));
53 BitmapTileTaskWorkerPool::BitmapTileTaskWorkerPool(
54 base::SequencedTaskRunner
* task_runner
,
55 TaskGraphRunner
* task_graph_runner
,
56 ResourceProvider
* resource_provider
)
57 : task_runner_(task_runner
),
58 task_graph_runner_(task_graph_runner
),
59 namespace_token_(task_graph_runner
->GetNamespaceToken()),
60 resource_provider_(resource_provider
),
61 task_set_finished_weak_ptr_factory_(this) {
64 BitmapTileTaskWorkerPool::~BitmapTileTaskWorkerPool() {
67 TileTaskRunner
* BitmapTileTaskWorkerPool::AsTileTaskRunner() {
71 void BitmapTileTaskWorkerPool::SetClient(TileTaskRunnerClient
* client
) {
75 void BitmapTileTaskWorkerPool::Shutdown() {
76 TRACE_EVENT0("cc", "BitmapTileTaskWorkerPool::Shutdown");
79 task_graph_runner_
->ScheduleTasks(namespace_token_
, &empty
);
80 task_graph_runner_
->WaitForTasksToFinishRunning(namespace_token_
);
83 void BitmapTileTaskWorkerPool::ScheduleTasks(TileTaskQueue
* queue
) {
84 TRACE_EVENT0("cc", "BitmapTileTaskWorkerPool::ScheduleTasks");
86 if (tasks_pending_
.none())
87 TRACE_EVENT_ASYNC_BEGIN0("cc", "ScheduledTasks", this);
89 // Mark all task sets as pending.
92 unsigned priority
= kTileTaskPriorityBase
;
96 // Cancel existing OnTaskSetFinished callbacks.
97 task_set_finished_weak_ptr_factory_
.InvalidateWeakPtrs();
99 scoped_refptr
<TileTask
> new_task_set_finished_tasks
[kNumberOfTaskSets
];
101 size_t task_count
[kNumberOfTaskSets
] = {0};
103 for (TaskSet task_set
= 0; task_set
< kNumberOfTaskSets
; ++task_set
) {
104 new_task_set_finished_tasks
[task_set
] = CreateTaskSetFinishedTask(
106 base::Bind(&BitmapTileTaskWorkerPool::OnTaskSetFinished
,
107 task_set_finished_weak_ptr_factory_
.GetWeakPtr(), task_set
));
110 for (TileTaskQueue::Item::Vector::const_iterator it
= queue
->items
.begin();
111 it
!= queue
->items
.end(); ++it
) {
112 const TileTaskQueue::Item
& item
= *it
;
113 RasterTask
* task
= item
.task
;
114 DCHECK(!task
->HasCompleted());
116 for (TaskSet task_set
= 0; task_set
< kNumberOfTaskSets
; ++task_set
) {
117 if (!item
.task_sets
[task_set
])
120 ++task_count
[task_set
];
122 graph_
.edges
.push_back(
123 TaskGraph::Edge(task
, new_task_set_finished_tasks
[task_set
].get()));
126 InsertNodesForRasterTask(&graph_
, task
, task
->dependencies(), priority
++);
129 for (TaskSet task_set
= 0; task_set
< kNumberOfTaskSets
; ++task_set
) {
130 InsertNodeForTask(&graph_
, new_task_set_finished_tasks
[task_set
].get(),
131 kTaskSetFinishedTaskPriorityBase
+ task_set
,
132 task_count
[task_set
]);
135 ScheduleTasksOnOriginThread(this, &graph_
);
136 task_graph_runner_
->ScheduleTasks(namespace_token_
, &graph_
);
138 std::copy(new_task_set_finished_tasks
,
139 new_task_set_finished_tasks
+ kNumberOfTaskSets
,
140 task_set_finished_tasks_
);
142 TRACE_EVENT_ASYNC_STEP_INTO1("cc", "ScheduledTasks", this, "running", "state",
146 void BitmapTileTaskWorkerPool::CheckForCompletedTasks() {
147 TRACE_EVENT0("cc", "BitmapTileTaskWorkerPool::CheckForCompletedTasks");
149 task_graph_runner_
->CollectCompletedTasks(namespace_token_
,
151 for (Task::Vector::const_iterator it
= completed_tasks_
.begin();
152 it
!= completed_tasks_
.end(); ++it
) {
153 TileTask
* task
= static_cast<TileTask
*>(it
->get());
155 task
->WillComplete();
156 task
->CompleteOnOriginThread(this);
159 task
->RunReplyOnOriginThread();
161 completed_tasks_
.clear();
164 ResourceFormat
BitmapTileTaskWorkerPool::GetResourceFormat() {
165 return resource_provider_
->best_texture_format();
168 scoped_ptr
<RasterBuffer
> BitmapTileTaskWorkerPool::AcquireBufferForRaster(
169 const Resource
* resource
) {
170 return make_scoped_ptr
<RasterBuffer
>(
171 new RasterBufferImpl(resource_provider_
, resource
));
174 void BitmapTileTaskWorkerPool::ReleaseBufferForRaster(
175 scoped_ptr
<RasterBuffer
> buffer
) {
176 // Nothing to do here. RasterBufferImpl destructor cleans up after itself.
179 void BitmapTileTaskWorkerPool::OnTaskSetFinished(TaskSet task_set
) {
180 TRACE_EVENT1("cc", "BitmapTileTaskWorkerPool::OnTaskSetFinished", "task_set",
183 DCHECK(tasks_pending_
[task_set
]);
184 tasks_pending_
[task_set
] = false;
185 if (tasks_pending_
.any()) {
186 TRACE_EVENT_ASYNC_STEP_INTO1("cc", "ScheduledTasks", this, "running",
187 "state", StateAsValue());
189 TRACE_EVENT_ASYNC_END0("cc", "ScheduledTasks", this);
191 client_
->DidFinishRunningTileTasks(task_set
);
194 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
195 BitmapTileTaskWorkerPool::StateAsValue() const {
196 scoped_refptr
<base::trace_event::TracedValue
> state
=
197 new base::trace_event::TracedValue();
199 state
->BeginArray("tasks_pending");
200 for (TaskSet task_set
= 0; task_set
< kNumberOfTaskSets
; ++task_set
)
201 state
->AppendBoolean(tasks_pending_
[task_set
]);