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/tile_task_worker_pool.h"
9 #include "base/lazy_instance.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/threading/simple_thread.h"
12 #include "base/trace_event/trace_event.h"
13 #include "cc/base/scoped_ptr_deque.h"
14 #include "cc/resources/raster_source.h"
15 #include "skia/ext/refptr.h"
16 #include "third_party/skia/include/core/SkCanvas.h"
17 #include "third_party/skia/include/core/SkSurface.h"
22 base::ThreadPriority g_worker_thread_priority
= base::kThreadPriority_Normal
;
24 class TileTaskGraphRunner
: public TaskGraphRunner
,
25 public base::DelegateSimpleThread::Delegate
{
27 TileTaskGraphRunner() {
28 size_t num_threads
= TileTaskWorkerPool::GetNumWorkerThreads();
29 while (workers_
.size() < num_threads
) {
30 scoped_ptr
<base::DelegateSimpleThread
> worker
=
31 make_scoped_ptr(new base::DelegateSimpleThread(
32 this, base::StringPrintf(
33 "CompositorTileWorker%u",
34 static_cast<unsigned>(workers_
.size() + 1)).c_str()));
36 worker
->SetThreadPriority(g_worker_thread_priority
);
37 workers_
.push_back(worker
.Pass());
41 ~TileTaskGraphRunner() override
{ NOTREACHED(); }
44 // Overridden from base::DelegateSimpleThread::Delegate:
45 void Run() override
{ TaskGraphRunner::Run(); }
47 ScopedPtrDeque
<base::DelegateSimpleThread
> workers_
;
50 base::LazyInstance
<TileTaskGraphRunner
>::Leaky g_task_graph_runner
=
51 LAZY_INSTANCE_INITIALIZER
;
53 const int kDefaultNumWorkerThreads
= 1;
55 int g_num_worker_threads
= 0;
57 class TaskSetFinishedTaskImpl
: public TileTask
{
59 explicit TaskSetFinishedTaskImpl(
60 base::SequencedTaskRunner
* task_runner
,
61 const base::Closure
& on_task_set_finished_callback
)
62 : task_runner_(task_runner
),
63 on_task_set_finished_callback_(on_task_set_finished_callback
) {}
65 // Overridden from Task:
66 void RunOnWorkerThread() override
{
67 TRACE_EVENT0("cc", "TaskSetFinishedTaskImpl::RunOnWorkerThread");
71 // Overridden from TileTask:
72 void ScheduleOnOriginThread(TileTaskClient
* client
) override
{}
73 void CompleteOnOriginThread(TileTaskClient
* client
) override
{}
74 void RunReplyOnOriginThread() override
{}
77 ~TaskSetFinishedTaskImpl() override
{}
79 void TaskSetFinished() {
80 task_runner_
->PostTask(FROM_HERE
, on_task_set_finished_callback_
);
84 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
85 const base::Closure on_task_set_finished_callback_
;
87 DISALLOW_COPY_AND_ASSIGN(TaskSetFinishedTaskImpl
);
92 // This allows a micro benchmark system to run tasks with highest priority,
93 // since it should finish as quickly as possible.
94 unsigned TileTaskWorkerPool::kBenchmarkTaskPriority
= 0u;
95 // Task priorities that make sure task set finished tasks run before any
96 // other remaining tasks. This is combined with the task set type to ensure
97 // proper prioritization ordering between task set types.
98 unsigned TileTaskWorkerPool::kTaskSetFinishedTaskPriorityBase
= 1u;
99 // For correctness, |kTileTaskPriorityBase| must be greater than
100 // |kTaskSetFinishedTaskPriorityBase + kNumberOfTaskSets|.
101 unsigned TileTaskWorkerPool::kTileTaskPriorityBase
= 10u;
103 TileTaskWorkerPool::TileTaskWorkerPool() {
106 TileTaskWorkerPool::~TileTaskWorkerPool() {
110 void TileTaskWorkerPool::SetNumWorkerThreads(int num_threads
) {
111 DCHECK_LT(0, num_threads
);
112 DCHECK_EQ(0, g_num_worker_threads
);
114 g_num_worker_threads
= num_threads
;
118 int TileTaskWorkerPool::GetNumWorkerThreads() {
119 if (!g_num_worker_threads
)
120 g_num_worker_threads
= kDefaultNumWorkerThreads
;
122 return g_num_worker_threads
;
126 void TileTaskWorkerPool::SetWorkerThreadPriority(
127 base::ThreadPriority priority
) {
128 g_worker_thread_priority
= priority
;
132 TaskGraphRunner
* TileTaskWorkerPool::GetTaskGraphRunner() {
133 return g_task_graph_runner
.Pointer();
137 scoped_refptr
<TileTask
> TileTaskWorkerPool::CreateTaskSetFinishedTask(
138 base::SequencedTaskRunner
* task_runner
,
139 const base::Closure
& on_task_set_finished_callback
) {
140 return make_scoped_refptr(
141 new TaskSetFinishedTaskImpl(task_runner
, on_task_set_finished_callback
));
145 void TileTaskWorkerPool::ScheduleTasksOnOriginThread(TileTaskClient
* client
,
147 TRACE_EVENT0("cc", "TileTaskWorkerPool::ScheduleTasksOnOriginThread");
149 for (TaskGraph::Node::Vector::iterator it
= graph
->nodes
.begin();
150 it
!= graph
->nodes
.end(); ++it
) {
151 TaskGraph::Node
& node
= *it
;
152 TileTask
* task
= static_cast<TileTask
*>(node
.task
);
154 if (!task
->HasBeenScheduled()) {
155 task
->WillSchedule();
156 task
->ScheduleOnOriginThread(client
);
163 void TileTaskWorkerPool::InsertNodeForTask(TaskGraph
* graph
,
166 size_t dependencies
) {
167 DCHECK(std::find_if(graph
->nodes
.begin(), graph
->nodes
.end(),
168 TaskGraph::Node::TaskComparator(task
)) ==
170 graph
->nodes
.push_back(TaskGraph::Node(task
, priority
, dependencies
));
174 void TileTaskWorkerPool::InsertNodesForRasterTask(
176 RasterTask
* raster_task
,
177 const ImageDecodeTask::Vector
& decode_tasks
,
179 size_t dependencies
= 0u;
181 // Insert image decode tasks.
182 for (ImageDecodeTask::Vector::const_iterator it
= decode_tasks
.begin();
183 it
!= decode_tasks
.end(); ++it
) {
184 ImageDecodeTask
* decode_task
= it
->get();
186 // Skip if already decoded.
187 if (decode_task
->HasCompleted())
192 // Add decode task if it doesn't already exists in graph.
193 TaskGraph::Node::Vector::iterator decode_it
=
194 std::find_if(graph
->nodes
.begin(), graph
->nodes
.end(),
195 TaskGraph::Node::TaskComparator(decode_task
));
196 if (decode_it
== graph
->nodes
.end())
197 InsertNodeForTask(graph
, decode_task
, priority
, 0u);
199 graph
->edges
.push_back(TaskGraph::Edge(decode_task
, raster_task
));
202 InsertNodeForTask(graph
, raster_task
, priority
, dependencies
);
205 static bool IsSupportedPlaybackToMemoryFormat(ResourceFormat format
) {
223 void TileTaskWorkerPool::PlaybackToMemory(void* memory
,
224 ResourceFormat format
,
225 const gfx::Size
& size
,
227 const RasterSource
* raster_source
,
228 const gfx::Rect
& rect
,
230 DCHECK(IsSupportedPlaybackToMemoryFormat(format
)) << format
;
232 // Uses kPremul_SkAlphaType since the result is not known to be opaque.
234 SkImageInfo::MakeN32(size
.width(), size
.height(), kPremul_SkAlphaType
);
235 SkColorType buffer_color_type
= ResourceFormatToSkColorType(format
);
236 bool needs_copy
= buffer_color_type
!= info
.colorType();
238 // Use unknown pixel geometry to disable LCD text.
239 SkSurfaceProps
surface_props(0, kUnknown_SkPixelGeometry
);
240 if (raster_source
->CanUseLCDText()) {
241 // LegacyFontHost will get LCD text and skia figures out what type to use.
242 surface_props
= SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType
);
246 stride
= info
.minRowBytes();
249 skia::RefPtr
<SkSurface
> surface
= skia::AdoptRef(
250 SkSurface::NewRasterDirect(info
, memory
, stride
, &surface_props
));
251 skia::RefPtr
<SkCanvas
> canvas
= skia::SharePtr(surface
->getCanvas());
252 raster_source
->PlaybackToCanvas(canvas
.get(), rect
, scale
);
256 skia::RefPtr
<SkSurface
> surface
=
257 skia::AdoptRef(SkSurface::NewRaster(info
, &surface_props
));
258 skia::RefPtr
<SkCanvas
> canvas
= skia::SharePtr(surface
->getCanvas());
259 raster_source
->PlaybackToCanvas(canvas
.get(), rect
, scale
);
261 SkImageInfo dst_info
= info
;
262 dst_info
.fColorType
= buffer_color_type
;
263 // TODO(kaanb): The GL pipeline assumes a 4-byte alignment for the
264 // bitmap data. There will be no need to call SkAlign4 once crbug.com/293728
266 const size_t dst_row_bytes
= SkAlign4(dst_info
.minRowBytes());
267 DCHECK_EQ(0u, dst_row_bytes
% 4);
268 bool success
= canvas
->readPixels(dst_info
, memory
, dst_row_bytes
, 0, 0);
269 DCHECK_EQ(true, success
);