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/platform_color.h"
16 #include "cc/resources/resource.h"
21 class RasterBufferImpl
: public RasterBuffer
{
23 RasterBufferImpl(ResourceProvider
* resource_provider
,
24 const Resource
* resource
,
25 uint64_t resource_content_id
,
26 uint64_t previous_content_id
)
27 : lock_(resource_provider
, resource
->id()),
29 resource_has_previous_content_(
30 resource_content_id
&& resource_content_id
== previous_content_id
) {
33 // Overridden from RasterBuffer:
34 void Playback(const RasterSource
* raster_source
,
35 const gfx::Rect
& raster_full_rect
,
36 const gfx::Rect
& raster_dirty_rect
,
37 uint64_t new_content_id
,
39 bool include_images
) override
{
40 gfx::Rect playback_rect
= raster_full_rect
;
41 if (resource_has_previous_content_
) {
42 playback_rect
.Intersect(raster_dirty_rect
);
44 DCHECK(!playback_rect
.IsEmpty())
45 << "Why are we rastering a tile that's not dirty?";
48 TileTaskWorkerPool::PlaybackToMemory(
49 lock_
.sk_bitmap().getPixels(), resource_
->format(), resource_
->size(),
50 stride
, raster_source
, raster_full_rect
, playback_rect
, scale
,
55 ResourceProvider::ScopedWriteLockSoftware lock_
;
56 const Resource
* resource_
;
57 bool resource_has_previous_content_
;
59 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl
);
65 scoped_ptr
<TileTaskWorkerPool
> BitmapTileTaskWorkerPool::Create(
66 base::SequencedTaskRunner
* task_runner
,
67 TaskGraphRunner
* task_graph_runner
,
68 ResourceProvider
* resource_provider
) {
69 return make_scoped_ptr
<TileTaskWorkerPool
>(new BitmapTileTaskWorkerPool(
70 task_runner
, task_graph_runner
, resource_provider
));
73 BitmapTileTaskWorkerPool::BitmapTileTaskWorkerPool(
74 base::SequencedTaskRunner
* task_runner
,
75 TaskGraphRunner
* task_graph_runner
,
76 ResourceProvider
* resource_provider
)
77 : task_runner_(task_runner
),
78 task_graph_runner_(task_graph_runner
),
79 namespace_token_(task_graph_runner
->GetNamespaceToken()),
80 resource_provider_(resource_provider
),
81 task_set_finished_weak_ptr_factory_(this) {
84 BitmapTileTaskWorkerPool::~BitmapTileTaskWorkerPool() {
87 TileTaskRunner
* BitmapTileTaskWorkerPool::AsTileTaskRunner() {
91 void BitmapTileTaskWorkerPool::SetClient(TileTaskRunnerClient
* client
) {
95 void BitmapTileTaskWorkerPool::Shutdown() {
96 TRACE_EVENT0("cc", "BitmapTileTaskWorkerPool::Shutdown");
99 task_graph_runner_
->ScheduleTasks(namespace_token_
, &empty
);
100 task_graph_runner_
->WaitForTasksToFinishRunning(namespace_token_
);
103 void BitmapTileTaskWorkerPool::ScheduleTasks(TileTaskQueue
* queue
) {
104 TRACE_EVENT0("cc", "BitmapTileTaskWorkerPool::ScheduleTasks");
106 if (tasks_pending_
.none())
107 TRACE_EVENT_ASYNC_BEGIN0("cc", "ScheduledTasks", this);
109 // Mark all task sets as pending.
110 tasks_pending_
.set();
112 size_t priority
= kTileTaskPriorityBase
;
116 // Cancel existing OnTaskSetFinished callbacks.
117 task_set_finished_weak_ptr_factory_
.InvalidateWeakPtrs();
119 scoped_refptr
<TileTask
> new_task_set_finished_tasks
[kNumberOfTaskSets
];
121 size_t task_count
[kNumberOfTaskSets
] = {0};
123 for (TaskSet task_set
= 0; task_set
< kNumberOfTaskSets
; ++task_set
) {
124 new_task_set_finished_tasks
[task_set
] = CreateTaskSetFinishedTask(
126 base::Bind(&BitmapTileTaskWorkerPool::OnTaskSetFinished
,
127 task_set_finished_weak_ptr_factory_
.GetWeakPtr(), task_set
));
130 for (TileTaskQueue::Item::Vector::const_iterator it
= queue
->items
.begin();
131 it
!= queue
->items
.end(); ++it
) {
132 const TileTaskQueue::Item
& item
= *it
;
133 RasterTask
* task
= item
.task
;
134 DCHECK(!task
->HasCompleted());
136 for (TaskSet task_set
= 0; task_set
< kNumberOfTaskSets
; ++task_set
) {
137 if (!item
.task_sets
[task_set
])
140 ++task_count
[task_set
];
142 graph_
.edges
.push_back(
143 TaskGraph::Edge(task
, new_task_set_finished_tasks
[task_set
].get()));
146 InsertNodesForRasterTask(&graph_
, task
, task
->dependencies(), priority
++);
149 for (TaskSet task_set
= 0; task_set
< kNumberOfTaskSets
; ++task_set
) {
150 InsertNodeForTask(&graph_
, new_task_set_finished_tasks
[task_set
].get(),
151 kTaskSetFinishedTaskPriorityBase
+ task_set
,
152 task_count
[task_set
]);
155 ScheduleTasksOnOriginThread(this, &graph_
);
156 task_graph_runner_
->ScheduleTasks(namespace_token_
, &graph_
);
158 std::copy(new_task_set_finished_tasks
,
159 new_task_set_finished_tasks
+ kNumberOfTaskSets
,
160 task_set_finished_tasks_
);
162 TRACE_EVENT_ASYNC_STEP_INTO1("cc", "ScheduledTasks", this, "running", "state",
166 void BitmapTileTaskWorkerPool::CheckForCompletedTasks() {
167 TRACE_EVENT0("cc", "BitmapTileTaskWorkerPool::CheckForCompletedTasks");
169 task_graph_runner_
->CollectCompletedTasks(namespace_token_
,
171 for (Task::Vector::const_iterator it
= completed_tasks_
.begin();
172 it
!= completed_tasks_
.end(); ++it
) {
173 TileTask
* task
= static_cast<TileTask
*>(it
->get());
175 task
->WillComplete();
176 task
->CompleteOnOriginThread(this);
179 task
->RunReplyOnOriginThread();
181 completed_tasks_
.clear();
184 ResourceFormat
BitmapTileTaskWorkerPool::GetResourceFormat() const {
185 return resource_provider_
->best_texture_format();
188 bool BitmapTileTaskWorkerPool::GetResourceRequiresSwizzle() const {
189 return !PlatformColor::SameComponentOrder(GetResourceFormat());
192 scoped_ptr
<RasterBuffer
> BitmapTileTaskWorkerPool::AcquireBufferForRaster(
193 const Resource
* resource
,
194 uint64_t resource_content_id
,
195 uint64_t previous_content_id
) {
196 return scoped_ptr
<RasterBuffer
>(new RasterBufferImpl(
197 resource_provider_
, resource
, resource_content_id
, previous_content_id
));
200 void BitmapTileTaskWorkerPool::ReleaseBufferForRaster(
201 scoped_ptr
<RasterBuffer
> buffer
) {
202 // Nothing to do here. RasterBufferImpl destructor cleans up after itself.
205 void BitmapTileTaskWorkerPool::OnTaskSetFinished(TaskSet task_set
) {
206 TRACE_EVENT1("cc", "BitmapTileTaskWorkerPool::OnTaskSetFinished", "task_set",
209 DCHECK(tasks_pending_
[task_set
]);
210 tasks_pending_
[task_set
] = false;
211 if (tasks_pending_
.any()) {
212 TRACE_EVENT_ASYNC_STEP_INTO1("cc", "ScheduledTasks", this, "running",
213 "state", StateAsValue());
215 TRACE_EVENT_ASYNC_END0("cc", "ScheduledTasks", this);
217 client_
->DidFinishRunningTileTasks(task_set
);
220 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
221 BitmapTileTaskWorkerPool::StateAsValue() const {
222 scoped_refptr
<base::trace_event::TracedValue
> state
=
223 new base::trace_event::TracedValue();
225 state
->BeginArray("tasks_pending");
226 for (TaskSet task_set
= 0; task_set
< kNumberOfTaskSets
; ++task_set
)
227 state
->AppendBoolean(tasks_pending_
[task_set
]);