Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / cc / raster / tile_task_worker_pool.cc
blobe980c8525de94255dfbb797c3c5b8e895169f64f
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 TRACE_EVENT0("cc", "TileTaskWorkerPool::PlaybackToMemory");
187 DCHECK(IsSupportedPlaybackToMemoryFormat(format)) << format;
189 // Uses kPremul_SkAlphaType since the result is not known to be opaque.
190 SkImageInfo info =
191 SkImageInfo::MakeN32(size.width(), size.height(), kPremul_SkAlphaType);
192 SkColorType buffer_color_type = ResourceFormatToSkColorType(format);
193 bool needs_copy = buffer_color_type != info.colorType();
195 // Use unknown pixel geometry to disable LCD text.
196 SkSurfaceProps surface_props(0, kUnknown_SkPixelGeometry);
197 if (raster_source->CanUseLCDText()) {
198 // LegacyFontHost will get LCD text and skia figures out what type to use.
199 surface_props = SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
202 if (!stride)
203 stride = info.minRowBytes();
204 DCHECK_GT(stride, 0u);
206 skia::RefPtr<SkDrawFilter> image_filter;
207 if (!include_images)
208 image_filter = skia::AdoptRef(new SkipImageFilter);
210 if (!needs_copy) {
211 skia::RefPtr<SkSurface> surface = skia::AdoptRef(
212 SkSurface::NewRasterDirect(info, memory, stride, &surface_props));
213 skia::RefPtr<SkCanvas> canvas = skia::SharePtr(surface->getCanvas());
214 canvas->setDrawFilter(image_filter.get());
215 raster_source->PlaybackToCanvas(canvas.get(), canvas_bitmap_rect,
216 canvas_playback_rect, scale);
217 return;
220 skia::RefPtr<SkSurface> surface =
221 skia::AdoptRef(SkSurface::NewRaster(info, &surface_props));
222 skia::RefPtr<SkCanvas> canvas = skia::SharePtr(surface->getCanvas());
223 canvas->setDrawFilter(image_filter.get());
224 // TODO(reveman): Improve partial raster support by reducing the size of
225 // playback rect passed to PlaybackToCanvas. crbug.com/519070
226 raster_source->PlaybackToCanvas(canvas.get(), canvas_bitmap_rect,
227 canvas_bitmap_rect, scale);
230 TRACE_EVENT0("cc", "TileTaskWorkerPool::PlaybackToMemory::ConvertPixels");
232 SkImageInfo dst_info =
233 SkImageInfo::Make(info.width(), info.height(), buffer_color_type,
234 info.alphaType(), info.profileType());
235 // TODO(kaanb): The GL pipeline assumes a 4-byte alignment for the
236 // bitmap data. There will be no need to call SkAlign4 once crbug.com/293728
237 // is fixed.
238 const size_t dst_row_bytes = SkAlign4(dst_info.minRowBytes());
239 DCHECK_EQ(0u, dst_row_bytes % 4);
240 bool success = canvas->readPixels(dst_info, memory, dst_row_bytes, 0, 0);
241 DCHECK_EQ(true, success);
245 } // namespace cc