1 // Copyright 2014 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/gpu_tile_task_worker_pool.h"
9 #include "base/trace_event/trace_event.h"
10 #include "cc/playback/raster_source.h"
11 #include "cc/raster/gpu_rasterizer.h"
12 #include "cc/raster/raster_buffer.h"
13 #include "cc/raster/scoped_gpu_raster.h"
14 #include "cc/resources/resource.h"
15 #include "gpu/command_buffer/client/gles2_interface.h"
16 #include "third_party/skia/include/core/SkMultiPictureDraw.h"
17 #include "third_party/skia/include/core/SkPictureRecorder.h"
18 #include "third_party/skia/include/core/SkSurface.h"
19 #include "third_party/skia/include/gpu/GrContext.h"
24 class RasterBufferImpl
: public RasterBuffer
{
26 RasterBufferImpl(GpuRasterizer
* rasterizer
,
27 const Resource
* resource
,
28 uint64_t resource_content_id
,
29 uint64_t previous_content_id
)
30 : rasterizer_(rasterizer
),
31 lock_(rasterizer
->resource_provider(), resource
->id()),
32 resource_has_previous_content_(
33 resource_content_id
&& resource_content_id
== previous_content_id
) {
36 // Overridden from RasterBuffer:
37 void Playback(const RasterSource
* raster_source
,
38 const gfx::Rect
& raster_full_rect
,
39 const gfx::Rect
& raster_dirty_rect
,
40 uint64_t new_content_id
,
42 bool include_images
) override
{
43 TRACE_EVENT0("cc", "RasterBufferImpl::Playback");
44 // GPU raster doesn't do low res tiles, so should always include images.
45 DCHECK(include_images
);
46 ContextProvider
* context_provider
= rasterizer_
->resource_provider()
48 ->worker_context_provider();
50 // The context lock must be held while accessing the context on a
52 base::AutoLock
context_lock(*context_provider
->GetLock());
54 // Allow this worker thread to bind to context_provider.
55 context_provider
->DetachFromThread();
57 gfx::Rect playback_rect
= raster_full_rect
;
58 if (resource_has_previous_content_
) {
59 playback_rect
.Intersect(raster_dirty_rect
);
61 DCHECK(!playback_rect
.IsEmpty())
62 << "Why are we rastering a tile that's not dirty?";
64 // TODO(danakj): Implement partial raster with raster_dirty_rect.
65 // Rasterize source into resource.
66 rasterizer_
->RasterizeSource(&lock_
, raster_source
, raster_full_rect
,
67 playback_rect
, scale
);
69 // Barrier to sync worker context output to cc context.
70 context_provider
->ContextGL()->OrderingBarrierCHROMIUM();
72 // Allow compositor thread to bind to context_provider.
73 context_provider
->DetachFromThread();
77 GpuRasterizer
* rasterizer_
;
78 ResourceProvider::ScopedWriteLockGr lock_
;
79 bool resource_has_previous_content_
;
81 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl
);
87 scoped_ptr
<TileTaskWorkerPool
> GpuTileTaskWorkerPool::Create(
88 base::SequencedTaskRunner
* task_runner
,
89 TaskGraphRunner
* task_graph_runner
,
90 ContextProvider
* context_provider
,
91 ResourceProvider
* resource_provider
,
92 bool use_distance_field_text
,
93 int gpu_rasterization_msaa_sample_count
) {
94 return make_scoped_ptr
<TileTaskWorkerPool
>(new GpuTileTaskWorkerPool(
95 task_runner
, task_graph_runner
, context_provider
, resource_provider
,
96 use_distance_field_text
, gpu_rasterization_msaa_sample_count
));
99 GpuTileTaskWorkerPool::GpuTileTaskWorkerPool(
100 base::SequencedTaskRunner
* task_runner
,
101 TaskGraphRunner
* task_graph_runner
,
102 ContextProvider
* context_provider
,
103 ResourceProvider
* resource_provider
,
104 bool use_distance_field_text
,
105 int gpu_rasterization_msaa_sample_count
)
106 : task_runner_(task_runner
),
107 task_graph_runner_(task_graph_runner
),
108 namespace_token_(task_graph_runner_
->GetNamespaceToken()),
109 rasterizer_(new GpuRasterizer(context_provider
,
111 use_distance_field_text
,
112 gpu_rasterization_msaa_sample_count
)),
113 task_set_finished_weak_ptr_factory_(this),
114 weak_ptr_factory_(this) {
117 GpuTileTaskWorkerPool::~GpuTileTaskWorkerPool() {
118 DCHECK_EQ(0u, completed_tasks_
.size());
121 TileTaskRunner
* GpuTileTaskWorkerPool::AsTileTaskRunner() {
125 void GpuTileTaskWorkerPool::SetClient(TileTaskRunnerClient
* client
) {
129 void GpuTileTaskWorkerPool::Shutdown() {
130 TRACE_EVENT0("cc", "GpuTileTaskWorkerPool::Shutdown");
133 task_graph_runner_
->ScheduleTasks(namespace_token_
, &empty
);
134 task_graph_runner_
->WaitForTasksToFinishRunning(namespace_token_
);
137 void GpuTileTaskWorkerPool::ScheduleTasks(TileTaskQueue
* queue
) {
138 TRACE_EVENT0("cc", "GpuTileTaskWorkerPool::ScheduleTasks");
140 // Mark all task sets as pending.
141 tasks_pending_
.set();
143 size_t priority
= kTileTaskPriorityBase
;
147 // Cancel existing OnTaskSetFinished callbacks.
148 task_set_finished_weak_ptr_factory_
.InvalidateWeakPtrs();
150 scoped_refptr
<TileTask
> new_task_set_finished_tasks
[kNumberOfTaskSets
];
152 size_t task_count
[kNumberOfTaskSets
] = {0};
154 for (TaskSet task_set
= 0; task_set
< kNumberOfTaskSets
; ++task_set
) {
155 new_task_set_finished_tasks
[task_set
] = CreateTaskSetFinishedTask(
157 base::Bind(&GpuTileTaskWorkerPool::OnTaskSetFinished
,
158 task_set_finished_weak_ptr_factory_
.GetWeakPtr(), task_set
));
161 for (TileTaskQueue::Item::Vector::const_iterator it
= queue
->items
.begin();
162 it
!= queue
->items
.end(); ++it
) {
163 const TileTaskQueue::Item
& item
= *it
;
164 RasterTask
* task
= item
.task
;
165 DCHECK(!task
->HasCompleted());
167 for (TaskSet task_set
= 0; task_set
< kNumberOfTaskSets
; ++task_set
) {
168 if (!item
.task_sets
[task_set
])
171 ++task_count
[task_set
];
173 graph_
.edges
.push_back(
174 TaskGraph::Edge(task
, new_task_set_finished_tasks
[task_set
].get()));
177 InsertNodesForRasterTask(&graph_
, task
, task
->dependencies(), priority
++);
180 for (TaskSet task_set
= 0; task_set
< kNumberOfTaskSets
; ++task_set
) {
181 InsertNodeForTask(&graph_
, new_task_set_finished_tasks
[task_set
].get(),
182 kTaskSetFinishedTaskPriorityBase
+ task_set
,
183 task_count
[task_set
]);
186 ScheduleTasksOnOriginThread(this, &graph_
);
188 // Barrier to sync any new resources to the worker context.
189 rasterizer_
->resource_provider()
193 ->OrderingBarrierCHROMIUM();
195 task_graph_runner_
->ScheduleTasks(namespace_token_
, &graph_
);
197 std::copy(new_task_set_finished_tasks
,
198 new_task_set_finished_tasks
+ kNumberOfTaskSets
,
199 task_set_finished_tasks_
);
202 void GpuTileTaskWorkerPool::CheckForCompletedTasks() {
203 TRACE_EVENT0("cc", "GpuTileTaskWorkerPool::CheckForCompletedTasks");
205 task_graph_runner_
->CollectCompletedTasks(namespace_token_
,
207 CompleteTasks(completed_tasks_
);
208 completed_tasks_
.clear();
211 ResourceFormat
GpuTileTaskWorkerPool::GetResourceFormat() const {
212 return rasterizer_
->resource_provider()->best_render_buffer_format();
215 bool GpuTileTaskWorkerPool::GetResourceRequiresSwizzle() const {
216 // This doesn't require a swizzle because we rasterize to the correct format.
220 void GpuTileTaskWorkerPool::CompleteTasks(const Task::Vector
& tasks
) {
221 for (auto& task
: tasks
) {
222 TileTask
* tile_task
= static_cast<TileTask
*>(task
.get());
224 tile_task
->WillComplete();
225 tile_task
->CompleteOnOriginThread(this);
226 tile_task
->DidComplete();
228 tile_task
->RunReplyOnOriginThread();
230 completed_tasks_
.clear();
233 scoped_ptr
<RasterBuffer
> GpuTileTaskWorkerPool::AcquireBufferForRaster(
234 const Resource
* resource
,
235 uint64_t resource_content_id
,
236 uint64_t previous_content_id
) {
237 return scoped_ptr
<RasterBuffer
>(new RasterBufferImpl(
238 rasterizer_
.get(), resource
, resource_content_id
, previous_content_id
));
241 void GpuTileTaskWorkerPool::ReleaseBufferForRaster(
242 scoped_ptr
<RasterBuffer
> buffer
) {
243 // Nothing to do here. RasterBufferImpl destructor cleans up after itself.
246 void GpuTileTaskWorkerPool::OnTaskSetFinished(TaskSet task_set
) {
247 TRACE_EVENT1("cc", "GpuTileTaskWorkerPool::OnTaskSetFinished", "task_set",
250 DCHECK(tasks_pending_
[task_set
]);
251 tasks_pending_
[task_set
] = false;
252 client_
->DidFinishRunningTileTasks(task_set
);