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 filter(SkPaint
* paint
, Type type
) override
{
160 if (type
== kBitmap_Type
)
163 SkShader
* shader
= paint
->getShader();
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
;
176 void TileTaskWorkerPool::PlaybackToMemory(void* memory
,
177 ResourceFormat format
,
178 const gfx::Size
& size
,
180 const RasterSource
* raster_source
,
181 const gfx::Rect
& canvas_bitmap_rect
,
182 const gfx::Rect
& canvas_playback_rect
,
184 bool include_images
) {
185 DCHECK(IsSupportedPlaybackToMemoryFormat(format
)) << format
;
187 // Uses kPremul_SkAlphaType since the result is not known to be opaque.
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
);
201 stride
= info
.minRowBytes();
202 DCHECK_GT(stride
, 0u);
204 skia::RefPtr
<SkDrawFilter
> image_filter
;
206 image_filter
= skia::AdoptRef(new SkipImageFilter
);
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
);
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
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
);