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/raster/zero_copy_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/raster/raster_buffer.h"
14 #include "cc/resources/platform_color.h"
15 #include "cc/resources/resource.h"
16 #include "ui/gfx/gpu_memory_buffer.h"
21 class RasterBufferImpl
: public RasterBuffer
{
23 RasterBufferImpl(ResourceProvider
* resource_provider
,
24 const Resource
* resource
)
25 : lock_(resource_provider
, resource
->id()), resource_(resource
) {}
27 // Overridden from RasterBuffer:
28 void Playback(const RasterSource
* raster_source
,
29 const gfx::Rect
& raster_full_rect
,
30 const gfx::Rect
& raster_dirty_rect
,
31 uint64_t new_content_id
,
33 bool include_images
) override
{
34 gfx::GpuMemoryBuffer
* gpu_memory_buffer
= lock_
.GetGpuMemoryBuffer();
35 if (!gpu_memory_buffer
)
38 bool rv
= gpu_memory_buffer
->Map(&data
);
41 gpu_memory_buffer
->GetStride(&stride
);
42 // TileTaskWorkerPool::PlaybackToMemory only supports unsigned strides.
44 // TODO(danakj): Implement partial raster with raster_dirty_rect.
45 TileTaskWorkerPool::PlaybackToMemory(
46 data
, resource_
->format(), resource_
->size(),
47 static_cast<size_t>(stride
), raster_source
, raster_full_rect
,
48 raster_full_rect
, scale
, include_images
);
49 gpu_memory_buffer
->Unmap();
53 ResourceProvider::ScopedWriteLockGpuMemoryBuffer lock_
;
54 const Resource
* resource_
;
56 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl
);
62 scoped_ptr
<TileTaskWorkerPool
> ZeroCopyTileTaskWorkerPool::Create(
63 base::SequencedTaskRunner
* task_runner
,
64 TaskGraphRunner
* task_graph_runner
,
65 ResourceProvider
* resource_provider
) {
66 return make_scoped_ptr
<TileTaskWorkerPool
>(new ZeroCopyTileTaskWorkerPool(
67 task_runner
, task_graph_runner
, resource_provider
));
70 ZeroCopyTileTaskWorkerPool::ZeroCopyTileTaskWorkerPool(
71 base::SequencedTaskRunner
* task_runner
,
72 TaskGraphRunner
* task_graph_runner
,
73 ResourceProvider
* resource_provider
)
74 : task_runner_(task_runner
),
75 task_graph_runner_(task_graph_runner
),
76 namespace_token_(task_graph_runner
->GetNamespaceToken()),
77 resource_provider_(resource_provider
),
78 task_set_finished_weak_ptr_factory_(this) {
81 ZeroCopyTileTaskWorkerPool::~ZeroCopyTileTaskWorkerPool() {
84 TileTaskRunner
* ZeroCopyTileTaskWorkerPool::AsTileTaskRunner() {
88 void ZeroCopyTileTaskWorkerPool::SetClient(TileTaskRunnerClient
* client
) {
92 void ZeroCopyTileTaskWorkerPool::Shutdown() {
93 TRACE_EVENT0("cc", "ZeroCopyTileTaskWorkerPool::Shutdown");
96 task_graph_runner_
->ScheduleTasks(namespace_token_
, &empty
);
97 task_graph_runner_
->WaitForTasksToFinishRunning(namespace_token_
);
100 void ZeroCopyTileTaskWorkerPool::ScheduleTasks(TileTaskQueue
* queue
) {
101 TRACE_EVENT0("cc", "ZeroCopyTileTaskWorkerPool::ScheduleTasks");
103 if (tasks_pending_
.none())
104 TRACE_EVENT_ASYNC_BEGIN0("cc", "ScheduledTasks", this);
106 // Mark all task sets as pending.
107 tasks_pending_
.set();
109 size_t priority
= kTileTaskPriorityBase
;
113 // Cancel existing OnTaskSetFinished callbacks.
114 task_set_finished_weak_ptr_factory_
.InvalidateWeakPtrs();
116 scoped_refptr
<TileTask
> new_task_set_finished_tasks
[kNumberOfTaskSets
];
118 size_t task_count
[kNumberOfTaskSets
] = {0};
120 for (TaskSet task_set
= 0; task_set
< kNumberOfTaskSets
; ++task_set
) {
121 new_task_set_finished_tasks
[task_set
] = CreateTaskSetFinishedTask(
123 base::Bind(&ZeroCopyTileTaskWorkerPool::OnTaskSetFinished
,
124 task_set_finished_weak_ptr_factory_
.GetWeakPtr(), task_set
));
127 for (TileTaskQueue::Item::Vector::const_iterator it
= queue
->items
.begin();
128 it
!= queue
->items
.end(); ++it
) {
129 const TileTaskQueue::Item
& item
= *it
;
130 RasterTask
* task
= item
.task
;
131 DCHECK(!task
->HasCompleted());
133 for (TaskSet task_set
= 0; task_set
< kNumberOfTaskSets
; ++task_set
) {
134 if (!item
.task_sets
[task_set
])
137 ++task_count
[task_set
];
139 graph_
.edges
.push_back(
140 TaskGraph::Edge(task
, new_task_set_finished_tasks
[task_set
].get()));
143 InsertNodesForRasterTask(&graph_
, task
, task
->dependencies(), priority
++);
146 for (TaskSet task_set
= 0; task_set
< kNumberOfTaskSets
; ++task_set
) {
147 InsertNodeForTask(&graph_
, new_task_set_finished_tasks
[task_set
].get(),
148 kTaskSetFinishedTaskPriorityBase
+ task_set
,
149 task_count
[task_set
]);
152 ScheduleTasksOnOriginThread(this, &graph_
);
153 task_graph_runner_
->ScheduleTasks(namespace_token_
, &graph_
);
155 std::copy(new_task_set_finished_tasks
,
156 new_task_set_finished_tasks
+ kNumberOfTaskSets
,
157 task_set_finished_tasks_
);
159 TRACE_EVENT_ASYNC_STEP_INTO1("cc", "ScheduledTasks", this, "running", "state",
163 void ZeroCopyTileTaskWorkerPool::CheckForCompletedTasks() {
164 TRACE_EVENT0("cc", "ZeroCopyTileTaskWorkerPool::CheckForCompletedTasks");
166 task_graph_runner_
->CollectCompletedTasks(namespace_token_
,
168 for (Task::Vector::const_iterator it
= completed_tasks_
.begin();
169 it
!= completed_tasks_
.end(); ++it
) {
170 TileTask
* task
= static_cast<TileTask
*>(it
->get());
172 task
->WillComplete();
173 task
->CompleteOnOriginThread(this);
176 task
->RunReplyOnOriginThread();
178 completed_tasks_
.clear();
181 ResourceFormat
ZeroCopyTileTaskWorkerPool::GetResourceFormat() const {
182 return resource_provider_
->best_texture_format();
185 bool ZeroCopyTileTaskWorkerPool::GetResourceRequiresSwizzle() const {
186 return !PlatformColor::SameComponentOrder(GetResourceFormat());
189 scoped_ptr
<RasterBuffer
> ZeroCopyTileTaskWorkerPool::AcquireBufferForRaster(
190 const Resource
* resource
,
191 uint64_t resource_content_id
,
192 uint64_t previous_content_id
) {
193 return make_scoped_ptr
<RasterBuffer
>(
194 new RasterBufferImpl(resource_provider_
, resource
));
197 void ZeroCopyTileTaskWorkerPool::ReleaseBufferForRaster(
198 scoped_ptr
<RasterBuffer
> buffer
) {
199 // Nothing to do here. RasterBufferImpl destructor cleans up after itself.
202 void ZeroCopyTileTaskWorkerPool::OnTaskSetFinished(TaskSet task_set
) {
203 TRACE_EVENT1("cc", "ZeroCopyTileTaskWorkerPool::OnTaskSetFinished",
204 "task_set", task_set
);
206 DCHECK(tasks_pending_
[task_set
]);
207 tasks_pending_
[task_set
] = false;
208 if (tasks_pending_
.any()) {
209 TRACE_EVENT_ASYNC_STEP_INTO1("cc", "ScheduledTasks", this, "running",
210 "state", StateAsValue());
212 TRACE_EVENT_ASYNC_END0("cc", "ScheduledTasks", this);
214 client_
->DidFinishRunningTileTasks(task_set
);
217 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
218 ZeroCopyTileTaskWorkerPool::StateAsValue() const {
219 scoped_refptr
<base::trace_event::TracedValue
> state
=
220 new base::trace_event::TracedValue();
222 state
->BeginArray("tasks_pending");
223 for (TaskSet task_set
= 0; task_set
< kNumberOfTaskSets
; ++task_set
)
224 state
->AppendBoolean(tasks_pending_
[task_set
]);