Enable right clicking on the applist doodle web contents and log the data.
[chromium-blink-merge.git] / cc / resources / tile_task_worker_pool.cc
blob277538648ba9b22fb0e56cceaf4ea716a9985052
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"
7 #include <algorithm>
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"
19 namespace cc {
20 namespace {
22 base::ThreadPriority g_worker_thread_priority = base::kThreadPriority_Normal;
24 class TileTaskGraphRunner : public TaskGraphRunner,
25 public base::DelegateSimpleThread::Delegate {
26 public:
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()));
35 worker->Start();
36 worker->SetThreadPriority(g_worker_thread_priority);
37 workers_.push_back(worker.Pass());
41 ~TileTaskGraphRunner() override { NOTREACHED(); }
43 private:
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 {
58 public:
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");
68 TaskSetFinished();
71 // Overridden from TileTask:
72 void ScheduleOnOriginThread(TileTaskClient* client) override {}
73 void CompleteOnOriginThread(TileTaskClient* client) override {}
74 void RunReplyOnOriginThread() override {}
76 protected:
77 ~TaskSetFinishedTaskImpl() override {}
79 void TaskSetFinished() {
80 task_runner_->PostTask(FROM_HERE, on_task_set_finished_callback_);
83 private:
84 scoped_refptr<base::SequencedTaskRunner> task_runner_;
85 const base::Closure on_task_set_finished_callback_;
87 DISALLOW_COPY_AND_ASSIGN(TaskSetFinishedTaskImpl);
90 } // namespace
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() {
109 // static
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;
117 // static
118 int TileTaskWorkerPool::GetNumWorkerThreads() {
119 if (!g_num_worker_threads)
120 g_num_worker_threads = kDefaultNumWorkerThreads;
122 return g_num_worker_threads;
125 // static
126 void TileTaskWorkerPool::SetWorkerThreadPriority(
127 base::ThreadPriority priority) {
128 g_worker_thread_priority = priority;
131 // static
132 TaskGraphRunner* TileTaskWorkerPool::GetTaskGraphRunner() {
133 return g_task_graph_runner.Pointer();
136 // static
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));
144 // static
145 void TileTaskWorkerPool::ScheduleTasksOnOriginThread(TileTaskClient* client,
146 TaskGraph* graph) {
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);
157 task->DidSchedule();
162 // static
163 void TileTaskWorkerPool::InsertNodeForTask(TaskGraph* graph,
164 TileTask* task,
165 unsigned priority,
166 size_t dependencies) {
167 DCHECK(std::find_if(graph->nodes.begin(), graph->nodes.end(),
168 TaskGraph::Node::TaskComparator(task)) ==
169 graph->nodes.end());
170 graph->nodes.push_back(TaskGraph::Node(task, priority, dependencies));
173 // static
174 void TileTaskWorkerPool::InsertNodesForRasterTask(
175 TaskGraph* graph,
176 RasterTask* raster_task,
177 const ImageDecodeTask::Vector& decode_tasks,
178 unsigned priority) {
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())
188 continue;
190 dependencies++;
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) {
206 switch (format) {
207 case RGBA_4444:
208 case RGBA_8888:
209 case BGRA_8888:
210 return true;
211 case ALPHA_8:
212 case LUMINANCE_8:
213 case RGB_565:
214 case ETC1:
215 case RED_8:
216 return false;
218 NOTREACHED();
219 return false;
222 // static
223 void TileTaskWorkerPool::PlaybackToMemory(void* memory,
224 ResourceFormat format,
225 const gfx::Size& size,
226 int stride,
227 const RasterSource* raster_source,
228 const gfx::Rect& rect,
229 float scale) {
230 DCHECK(IsSupportedPlaybackToMemoryFormat(format)) << format;
232 // Uses kPremul_SkAlphaType since the result is not known to be opaque.
233 SkImageInfo info =
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);
245 if (!stride)
246 stride = info.minRowBytes();
248 if (!needs_copy) {
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);
253 return;
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
265 // is fixed.
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);
272 } // namespace cc