Add ICU message format support
[chromium-blink-merge.git] / cc / raster / tile_task_worker_pool.cc
blobf9862fbab1a203c56774038e5a86f0718787630f
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 PaintHasBitmap(const SkPaint& paint) {
160 SkShader* shader = paint.getShader();
161 if (!shader)
162 return false;
163 if (shader->asAGradient(nullptr) == SkShader::kNone_GradientType)
164 return false;
165 if (shader->asABitmap(nullptr, nullptr, nullptr) !=
166 SkShader::kNone_BitmapType)
167 return true;
168 return false;
171 bool filter(SkPaint* paint, Type type) override {
172 if (type == kBitmap_Type)
173 return false;
174 if (PaintHasBitmap(*paint))
175 return false;
176 return true;
180 // static
181 void TileTaskWorkerPool::PlaybackToMemory(void* memory,
182 ResourceFormat format,
183 const gfx::Size& size,
184 size_t stride,
185 const RasterSource* raster_source,
186 const gfx::Rect& canvas_bitmap_rect,
187 const gfx::Rect& canvas_playback_rect,
188 float scale,
189 bool include_images) {
190 DCHECK(IsSupportedPlaybackToMemoryFormat(format)) << format;
192 // Uses kPremul_SkAlphaType since the result is not known to be opaque.
193 SkImageInfo info =
194 SkImageInfo::MakeN32(size.width(), size.height(), kPremul_SkAlphaType);
195 SkColorType buffer_color_type = ResourceFormatToSkColorType(format);
196 bool needs_copy = buffer_color_type != info.colorType();
198 // Use unknown pixel geometry to disable LCD text.
199 SkSurfaceProps surface_props(0, kUnknown_SkPixelGeometry);
200 if (raster_source->CanUseLCDText()) {
201 // LegacyFontHost will get LCD text and skia figures out what type to use.
202 surface_props = SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
205 if (!stride)
206 stride = info.minRowBytes();
207 DCHECK_GT(stride, 0u);
209 skia::RefPtr<SkDrawFilter> image_filter;
210 if (!include_images)
211 image_filter = skia::AdoptRef(new SkipImageFilter);
213 if (!needs_copy) {
214 skia::RefPtr<SkSurface> surface = skia::AdoptRef(
215 SkSurface::NewRasterDirect(info, memory, stride, &surface_props));
216 skia::RefPtr<SkCanvas> canvas = skia::SharePtr(surface->getCanvas());
217 canvas->setDrawFilter(image_filter.get());
218 raster_source->PlaybackToCanvas(canvas.get(), canvas_bitmap_rect,
219 canvas_playback_rect, scale);
220 return;
223 skia::RefPtr<SkSurface> surface =
224 skia::AdoptRef(SkSurface::NewRaster(info, &surface_props));
225 skia::RefPtr<SkCanvas> canvas = skia::SharePtr(surface->getCanvas());
226 canvas->setDrawFilter(image_filter.get());
227 raster_source->PlaybackToCanvas(canvas.get(), canvas_bitmap_rect,
228 canvas_playback_rect, scale);
230 SkImageInfo dst_info =
231 SkImageInfo::Make(info.width(), info.height(), buffer_color_type,
232 info.alphaType(), info.profileType());
233 // TODO(kaanb): The GL pipeline assumes a 4-byte alignment for the
234 // bitmap data. There will be no need to call SkAlign4 once crbug.com/293728
235 // is fixed.
236 const size_t dst_row_bytes = SkAlign4(dst_info.minRowBytes());
237 DCHECK_EQ(0u, dst_row_bytes % 4);
238 bool success = canvas->readPixels(dst_info, memory, dst_row_bytes, 0, 0);
239 DCHECK_EQ(true, success);
242 } // namespace cc