Infobar material design refresh: bg color
[chromium-blink-merge.git] / cc / raster / tile_task_worker_pool.cc
blob758962e8eca9a346cf7198f5cf606df3a6c04a2d
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 {}
37 protected:
38 ~TaskSetFinishedTaskImpl() override {}
40 void TaskSetFinished() {
41 task_runner_->PostTask(FROM_HERE, on_task_set_finished_callback_);
44 private:
45 scoped_refptr<base::SequencedTaskRunner> task_runner_;
46 const base::Closure on_task_set_finished_callback_;
48 DISALLOW_COPY_AND_ASSIGN(TaskSetFinishedTaskImpl);
51 } // namespace
53 // This allows a micro benchmark system to run tasks with highest priority,
54 // since it should finish as quickly as possible.
55 size_t TileTaskWorkerPool::kBenchmarkTaskPriority = 0u;
56 // Task priorities that make sure task set finished tasks run before any
57 // other remaining tasks. This is combined with the task set type to ensure
58 // proper prioritization ordering between task set types.
59 size_t TileTaskWorkerPool::kTaskSetFinishedTaskPriorityBase = 1u;
60 // For correctness, |kTileTaskPriorityBase| must be greater than
61 // |kTaskSetFinishedTaskPriorityBase + kNumberOfTaskSets|.
62 size_t TileTaskWorkerPool::kTileTaskPriorityBase = 10u;
64 TileTaskWorkerPool::TileTaskWorkerPool() {
67 TileTaskWorkerPool::~TileTaskWorkerPool() {
70 // static
71 scoped_refptr<TileTask> TileTaskWorkerPool::CreateTaskSetFinishedTask(
72 base::SequencedTaskRunner* task_runner,
73 const base::Closure& on_task_set_finished_callback) {
74 return make_scoped_refptr(
75 new TaskSetFinishedTaskImpl(task_runner, on_task_set_finished_callback));
78 // static
79 void TileTaskWorkerPool::ScheduleTasksOnOriginThread(TileTaskClient* client,
80 TaskGraph* graph) {
81 TRACE_EVENT0("cc", "TileTaskWorkerPool::ScheduleTasksOnOriginThread");
83 for (TaskGraph::Node::Vector::iterator it = graph->nodes.begin();
84 it != graph->nodes.end(); ++it) {
85 TaskGraph::Node& node = *it;
86 TileTask* task = static_cast<TileTask*>(node.task);
88 if (!task->HasBeenScheduled()) {
89 task->WillSchedule();
90 task->ScheduleOnOriginThread(client);
91 task->DidSchedule();
96 // static
97 void TileTaskWorkerPool::InsertNodeForTask(TaskGraph* graph,
98 TileTask* task,
99 size_t priority,
100 size_t dependencies) {
101 DCHECK(std::find_if(graph->nodes.begin(), graph->nodes.end(),
102 TaskGraph::Node::TaskComparator(task)) ==
103 graph->nodes.end());
104 graph->nodes.push_back(TaskGraph::Node(task, priority, dependencies));
107 // static
108 void TileTaskWorkerPool::InsertNodesForRasterTask(
109 TaskGraph* graph,
110 RasterTask* raster_task,
111 const ImageDecodeTask::Vector& decode_tasks,
112 size_t priority) {
113 size_t dependencies = 0u;
115 // Insert image decode tasks.
116 for (ImageDecodeTask::Vector::const_iterator it = decode_tasks.begin();
117 it != decode_tasks.end(); ++it) {
118 ImageDecodeTask* decode_task = it->get();
120 // Skip if already decoded.
121 if (decode_task->HasCompleted())
122 continue;
124 dependencies++;
126 // Add decode task if it doesn't already exists in graph.
127 TaskGraph::Node::Vector::iterator decode_it =
128 std::find_if(graph->nodes.begin(), graph->nodes.end(),
129 TaskGraph::Node::TaskComparator(decode_task));
130 if (decode_it == graph->nodes.end())
131 InsertNodeForTask(graph, decode_task, priority, 0u);
133 graph->edges.push_back(TaskGraph::Edge(decode_task, raster_task));
136 InsertNodeForTask(graph, raster_task, priority, dependencies);
139 static bool IsSupportedPlaybackToMemoryFormat(ResourceFormat format) {
140 switch (format) {
141 case RGBA_4444:
142 case RGBA_8888:
143 case BGRA_8888:
144 return true;
145 case ALPHA_8:
146 case LUMINANCE_8:
147 case RGB_565:
148 case ETC1:
149 case RED_8:
150 return false;
152 NOTREACHED();
153 return false;
156 class SkipImageFilter : public SkDrawFilter {
157 public:
158 bool filter(SkPaint* paint, Type type) override {
159 if (type == kBitmap_Type)
160 return false;
162 SkShader* shader = paint->getShader();
163 if (!shader)
164 return true;
165 SkShader::BitmapType bitmap_type =
166 shader->asABitmap(nullptr, nullptr, nullptr);
167 // The kDefault_BitmapType is returned for images. Other bitmap types are
168 // simply bitmap representations of colors such as gradients. So, we can
169 // return true and draw for any case except kDefault_BitmapType.
170 return bitmap_type != SkShader::kDefault_BitmapType;
174 // static
175 void TileTaskWorkerPool::PlaybackToMemory(void* memory,
176 ResourceFormat format,
177 const gfx::Size& size,
178 size_t stride,
179 const RasterSource* raster_source,
180 const gfx::Rect& canvas_bitmap_rect,
181 const gfx::Rect& canvas_playback_rect,
182 float scale,
183 bool include_images) {
184 TRACE_EVENT0("cc", "TileTaskWorkerPool::PlaybackToMemory");
186 DCHECK(IsSupportedPlaybackToMemoryFormat(format)) << format;
188 // Uses kPremul_SkAlphaType since the result is not known to be opaque.
189 SkImageInfo info =
190 SkImageInfo::MakeN32(size.width(), size.height(), kPremul_SkAlphaType);
191 SkColorType buffer_color_type = ResourceFormatToSkColorType(format);
192 bool needs_copy = buffer_color_type != info.colorType();
194 // Use unknown pixel geometry to disable LCD text.
195 SkSurfaceProps surface_props(0, kUnknown_SkPixelGeometry);
196 if (raster_source->CanUseLCDText()) {
197 // LegacyFontHost will get LCD text and skia figures out what type to use.
198 surface_props = SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
201 if (!stride)
202 stride = info.minRowBytes();
203 DCHECK_GT(stride, 0u);
205 skia::RefPtr<SkDrawFilter> image_filter;
206 if (!include_images)
207 image_filter = skia::AdoptRef(new SkipImageFilter);
209 if (!needs_copy) {
210 skia::RefPtr<SkSurface> surface = skia::AdoptRef(
211 SkSurface::NewRasterDirect(info, memory, stride, &surface_props));
212 skia::RefPtr<SkCanvas> canvas = skia::SharePtr(surface->getCanvas());
213 canvas->setDrawFilter(image_filter.get());
214 raster_source->PlaybackToCanvas(canvas.get(), canvas_bitmap_rect,
215 canvas_playback_rect, scale);
216 return;
219 skia::RefPtr<SkSurface> surface =
220 skia::AdoptRef(SkSurface::NewRaster(info, &surface_props));
221 skia::RefPtr<SkCanvas> canvas = skia::SharePtr(surface->getCanvas());
222 canvas->setDrawFilter(image_filter.get());
223 // TODO(reveman): Improve partial raster support by reducing the size of
224 // playback rect passed to PlaybackToCanvas. crbug.com/519070
225 raster_source->PlaybackToCanvas(canvas.get(), canvas_bitmap_rect,
226 canvas_bitmap_rect, scale);
229 TRACE_EVENT0("cc", "TileTaskWorkerPool::PlaybackToMemory::ConvertPixels");
231 SkImageInfo dst_info =
232 SkImageInfo::Make(info.width(), info.height(), buffer_color_type,
233 info.alphaType(), info.profileType());
234 // TODO(kaanb): The GL pipeline assumes a 4-byte alignment for the
235 // bitmap data. There will be no need to call SkAlign4 once crbug.com/293728
236 // is fixed.
237 const size_t dst_row_bytes = SkAlign4(dst_info.minRowBytes());
238 DCHECK_EQ(0u, dst_row_bytes % 4);
239 bool success = canvas->readPixels(dst_info, memory, dst_row_bytes, 0, 0);
240 DCHECK_EQ(true, success);
244 } // namespace cc