Componentize tab_node_pool to sync_driver
[chromium-blink-merge.git] / cc / raster / tile_task_worker_pool.cc
blob8829e2c6881681bcfc8e029f344988ffcdac2a87
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/tile_task_worker_pool.h"
7 #include <algorithm>
9 #include "base/trace_event/trace_event.h"
10 #include "cc/playback/raster_source.h"
11 #include "skia/ext/refptr.h"
12 #include "third_party/skia/include/core/SkCanvas.h"
13 #include "third_party/skia/include/core/SkDrawFilter.h"
14 #include "third_party/skia/include/core/SkSurface.h"
16 namespace cc {
17 namespace {
19 class TaskSetFinishedTaskImpl : public TileTask {
20 public:
21 explicit TaskSetFinishedTaskImpl(
22 base::SequencedTaskRunner* task_runner,
23 const base::Closure& on_task_set_finished_callback)
24 : task_runner_(task_runner),
25 on_task_set_finished_callback_(on_task_set_finished_callback) {}
27 // Overridden from Task:
28 void RunOnWorkerThread() override {
29 TRACE_EVENT0("cc", "TaskSetFinishedTaskImpl::RunOnWorkerThread");
30 TaskSetFinished();
33 // Overridden from TileTask:
34 void ScheduleOnOriginThread(TileTaskClient* client) override {}
35 void CompleteOnOriginThread(TileTaskClient* client) override {}
36 void RunReplyOnOriginThread() override {}
38 protected:
39 ~TaskSetFinishedTaskImpl() override {}
41 void TaskSetFinished() {
42 task_runner_->PostTask(FROM_HERE, on_task_set_finished_callback_);
45 private:
46 scoped_refptr<base::SequencedTaskRunner> task_runner_;
47 const base::Closure on_task_set_finished_callback_;
49 DISALLOW_COPY_AND_ASSIGN(TaskSetFinishedTaskImpl);
52 } // namespace
54 // This allows a micro benchmark system to run tasks with highest priority,
55 // since it should finish as quickly as possible.
56 size_t TileTaskWorkerPool::kBenchmarkTaskPriority = 0u;
57 // Task priorities that make sure task set finished tasks run before any
58 // other remaining tasks. This is combined with the task set type to ensure
59 // proper prioritization ordering between task set types.
60 size_t TileTaskWorkerPool::kTaskSetFinishedTaskPriorityBase = 1u;
61 // For correctness, |kTileTaskPriorityBase| must be greater than
62 // |kTaskSetFinishedTaskPriorityBase + kNumberOfTaskSets|.
63 size_t TileTaskWorkerPool::kTileTaskPriorityBase = 10u;
65 TileTaskWorkerPool::TileTaskWorkerPool() {
68 TileTaskWorkerPool::~TileTaskWorkerPool() {
71 // static
72 scoped_refptr<TileTask> TileTaskWorkerPool::CreateTaskSetFinishedTask(
73 base::SequencedTaskRunner* task_runner,
74 const base::Closure& on_task_set_finished_callback) {
75 return make_scoped_refptr(
76 new TaskSetFinishedTaskImpl(task_runner, on_task_set_finished_callback));
79 // static
80 void TileTaskWorkerPool::ScheduleTasksOnOriginThread(TileTaskClient* client,
81 TaskGraph* graph) {
82 TRACE_EVENT0("cc", "TileTaskWorkerPool::ScheduleTasksOnOriginThread");
84 for (TaskGraph::Node::Vector::iterator it = graph->nodes.begin();
85 it != graph->nodes.end(); ++it) {
86 TaskGraph::Node& node = *it;
87 TileTask* task = static_cast<TileTask*>(node.task);
89 if (!task->HasBeenScheduled()) {
90 task->WillSchedule();
91 task->ScheduleOnOriginThread(client);
92 task->DidSchedule();
97 // static
98 void TileTaskWorkerPool::InsertNodeForTask(TaskGraph* graph,
99 TileTask* task,
100 size_t priority,
101 size_t dependencies) {
102 DCHECK(std::find_if(graph->nodes.begin(), graph->nodes.end(),
103 TaskGraph::Node::TaskComparator(task)) ==
104 graph->nodes.end());
105 graph->nodes.push_back(TaskGraph::Node(task, priority, dependencies));
108 // static
109 void TileTaskWorkerPool::InsertNodesForRasterTask(
110 TaskGraph* graph,
111 RasterTask* raster_task,
112 const ImageDecodeTask::Vector& decode_tasks,
113 size_t priority) {
114 size_t dependencies = 0u;
116 // Insert image decode tasks.
117 for (ImageDecodeTask::Vector::const_iterator it = decode_tasks.begin();
118 it != decode_tasks.end(); ++it) {
119 ImageDecodeTask* decode_task = it->get();
121 // Skip if already decoded.
122 if (decode_task->HasCompleted())
123 continue;
125 dependencies++;
127 // Add decode task if it doesn't already exists in graph.
128 TaskGraph::Node::Vector::iterator decode_it =
129 std::find_if(graph->nodes.begin(), graph->nodes.end(),
130 TaskGraph::Node::TaskComparator(decode_task));
131 if (decode_it == graph->nodes.end())
132 InsertNodeForTask(graph, decode_task, priority, 0u);
134 graph->edges.push_back(TaskGraph::Edge(decode_task, raster_task));
137 InsertNodeForTask(graph, raster_task, priority, dependencies);
140 static bool IsSupportedPlaybackToMemoryFormat(ResourceFormat format) {
141 switch (format) {
142 case RGBA_4444:
143 case RGBA_8888:
144 case BGRA_8888:
145 return true;
146 case ALPHA_8:
147 case LUMINANCE_8:
148 case RGB_565:
149 case ETC1:
150 case RED_8:
151 return false;
153 NOTREACHED();
154 return false;
157 class SkipImageFilter : public SkDrawFilter {
158 public:
159 bool filter(SkPaint* paint, Type type) override {
160 if (type == kBitmap_Type)
161 return false;
163 SkShader* shader = paint->getShader();
164 if (!shader)
165 return true;
166 SkShader::BitmapType bitmap_type =
167 shader->asABitmap(nullptr, nullptr, nullptr);
168 // The kDefault_BitmapType is returned for images. Other bitmap types are
169 // simply bitmap representations of colors such as gradients. So, we can
170 // return true and draw for any case except kDefault_BitmapType.
171 return bitmap_type != SkShader::kDefault_BitmapType;
175 // static
176 void TileTaskWorkerPool::PlaybackToMemory(void* memory,
177 ResourceFormat format,
178 const gfx::Size& size,
179 size_t stride,
180 const RasterSource* raster_source,
181 const gfx::Rect& canvas_bitmap_rect,
182 const gfx::Rect& canvas_playback_rect,
183 float scale,
184 bool include_images) {
185 DCHECK(IsSupportedPlaybackToMemoryFormat(format)) << format;
187 // Uses kPremul_SkAlphaType since the result is not known to be opaque.
188 SkImageInfo info =
189 SkImageInfo::MakeN32(size.width(), size.height(), kPremul_SkAlphaType);
190 SkColorType buffer_color_type = ResourceFormatToSkColorType(format);
191 bool needs_copy = buffer_color_type != info.colorType();
193 // Use unknown pixel geometry to disable LCD text.
194 SkSurfaceProps surface_props(0, kUnknown_SkPixelGeometry);
195 if (raster_source->CanUseLCDText()) {
196 // LegacyFontHost will get LCD text and skia figures out what type to use.
197 surface_props = SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
200 if (!stride)
201 stride = info.minRowBytes();
202 DCHECK_GT(stride, 0u);
204 skia::RefPtr<SkDrawFilter> image_filter;
205 if (!include_images)
206 image_filter = skia::AdoptRef(new SkipImageFilter);
208 if (!needs_copy) {
209 skia::RefPtr<SkSurface> surface = skia::AdoptRef(
210 SkSurface::NewRasterDirect(info, memory, stride, &surface_props));
211 skia::RefPtr<SkCanvas> canvas = skia::SharePtr(surface->getCanvas());
212 canvas->setDrawFilter(image_filter.get());
213 raster_source->PlaybackToCanvas(canvas.get(), canvas_bitmap_rect,
214 canvas_playback_rect, scale);
215 return;
218 skia::RefPtr<SkSurface> surface =
219 skia::AdoptRef(SkSurface::NewRaster(info, &surface_props));
220 skia::RefPtr<SkCanvas> canvas = skia::SharePtr(surface->getCanvas());
221 canvas->setDrawFilter(image_filter.get());
222 raster_source->PlaybackToCanvas(canvas.get(), canvas_bitmap_rect,
223 canvas_playback_rect, scale);
225 SkImageInfo dst_info =
226 SkImageInfo::Make(info.width(), info.height(), buffer_color_type,
227 info.alphaType(), info.profileType());
228 // TODO(kaanb): The GL pipeline assumes a 4-byte alignment for the
229 // bitmap data. There will be no need to call SkAlign4 once crbug.com/293728
230 // is fixed.
231 const size_t dst_row_bytes = SkAlign4(dst_info.minRowBytes());
232 DCHECK_EQ(0u, dst_row_bytes % 4);
233 bool success = canvas->readPixels(dst_info, memory, dst_row_bytes, 0, 0);
234 DCHECK_EQ(true, success);
237 } // namespace cc