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"
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"
19 class TaskSetFinishedTaskImpl
: public TileTask
{
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");
33 // Overridden from TileTask:
34 void ScheduleOnOriginThread(TileTaskClient
* client
) override
{}
35 void CompleteOnOriginThread(TileTaskClient
* client
) override
{}
36 void RunReplyOnOriginThread() override
{}
39 ~TaskSetFinishedTaskImpl() override
{}
41 void TaskSetFinished() {
42 task_runner_
->PostTask(FROM_HERE
, on_task_set_finished_callback_
);
46 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
47 const base::Closure on_task_set_finished_callback_
;
49 DISALLOW_COPY_AND_ASSIGN(TaskSetFinishedTaskImpl
);
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() {
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
));
80 void TileTaskWorkerPool::ScheduleTasksOnOriginThread(TileTaskClient
* client
,
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()) {
91 task
->ScheduleOnOriginThread(client
);
98 void TileTaskWorkerPool::InsertNodeForTask(TaskGraph
* graph
,
101 size_t dependencies
) {
102 DCHECK(std::find_if(graph
->nodes
.begin(), graph
->nodes
.end(),
103 TaskGraph::Node::TaskComparator(task
)) ==
105 graph
->nodes
.push_back(TaskGraph::Node(task
, priority
, dependencies
));
109 void TileTaskWorkerPool::InsertNodesForRasterTask(
111 RasterTask
* raster_task
,
112 const ImageDecodeTask::Vector
& decode_tasks
,
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())
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
) {
157 class SkipImageFilter
: public SkDrawFilter
{
159 bool PaintHasBitmap(const SkPaint
& paint
) {
160 SkShader
* shader
= paint
.getShader();
163 if (shader
->asAGradient(nullptr) == SkShader::kNone_GradientType
)
165 if (shader
->asABitmap(nullptr, nullptr, nullptr) !=
166 SkShader::kNone_BitmapType
)
171 bool filter(SkPaint
* paint
, Type type
) override
{
172 if (type
== kBitmap_Type
)
174 if (PaintHasBitmap(*paint
))
181 void TileTaskWorkerPool::PlaybackToMemory(void* memory
,
182 ResourceFormat format
,
183 const gfx::Size
& size
,
185 const RasterSource
* raster_source
,
186 const gfx::Rect
& canvas_bitmap_rect
,
187 const gfx::Rect
& canvas_playback_rect
,
189 bool include_images
) {
190 DCHECK(IsSupportedPlaybackToMemoryFormat(format
)) << format
;
192 // Uses kPremul_SkAlphaType since the result is not known to be opaque.
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
);
206 stride
= info
.minRowBytes();
207 DCHECK_GT(stride
, 0u);
209 skia::RefPtr
<SkDrawFilter
> image_filter
;
211 image_filter
= skia::AdoptRef(new SkipImageFilter
);
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
);
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
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
);