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
,
41 float scale
) override
{
42 TRACE_EVENT0("cc", "RasterBufferImpl::Playback");
43 ContextProvider
* context_provider
= rasterizer_
->resource_provider()
45 ->worker_context_provider();
47 // The context lock must be held while accessing the context on a
49 base::AutoLock
context_lock(*context_provider
->GetLock());
51 // Allow this worker thread to bind to context_provider.
52 context_provider
->DetachFromThread();
54 gfx::Rect playback_rect
= raster_full_rect
;
55 if (resource_has_previous_content_
) {
56 playback_rect
.Intersect(raster_dirty_rect
);
58 DCHECK(!playback_rect
.IsEmpty())
59 << "Why are we rastering a tile that's not dirty?";
61 // TODO(danakj): Implement partial raster with raster_dirty_rect.
62 // Rasterize source into resource.
63 rasterizer_
->RasterizeSource(&lock_
, raster_source
, raster_full_rect
,
64 playback_rect
, scale
);
66 // Barrier to sync worker context output to cc context.
67 context_provider
->ContextGL()->OrderingBarrierCHROMIUM();
69 // Allow compositor thread to bind to context_provider.
70 context_provider
->DetachFromThread();
74 GpuRasterizer
* rasterizer_
;
75 ResourceProvider::ScopedWriteLockGr lock_
;
76 bool resource_has_previous_content_
;
78 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl
);
84 scoped_ptr
<TileTaskWorkerPool
> GpuTileTaskWorkerPool::Create(
85 base::SequencedTaskRunner
* task_runner
,
86 TaskGraphRunner
* task_graph_runner
,
87 ContextProvider
* context_provider
,
88 ResourceProvider
* resource_provider
,
89 bool use_distance_field_text
,
90 int gpu_rasterization_msaa_sample_count
) {
91 return make_scoped_ptr
<TileTaskWorkerPool
>(new GpuTileTaskWorkerPool(
92 task_runner
, task_graph_runner
, context_provider
, resource_provider
,
93 use_distance_field_text
, gpu_rasterization_msaa_sample_count
));
96 GpuTileTaskWorkerPool::GpuTileTaskWorkerPool(
97 base::SequencedTaskRunner
* task_runner
,
98 TaskGraphRunner
* task_graph_runner
,
99 ContextProvider
* context_provider
,
100 ResourceProvider
* resource_provider
,
101 bool use_distance_field_text
,
102 int gpu_rasterization_msaa_sample_count
)
103 : task_runner_(task_runner
),
104 task_graph_runner_(task_graph_runner
),
105 namespace_token_(task_graph_runner_
->GetNamespaceToken()),
106 rasterizer_(new GpuRasterizer(context_provider
,
108 use_distance_field_text
,
109 gpu_rasterization_msaa_sample_count
)),
110 task_set_finished_weak_ptr_factory_(this),
111 weak_ptr_factory_(this) {
114 GpuTileTaskWorkerPool::~GpuTileTaskWorkerPool() {
115 DCHECK_EQ(0u, completed_tasks_
.size());
118 TileTaskRunner
* GpuTileTaskWorkerPool::AsTileTaskRunner() {
122 void GpuTileTaskWorkerPool::SetClient(TileTaskRunnerClient
* client
) {
126 void GpuTileTaskWorkerPool::Shutdown() {
127 TRACE_EVENT0("cc", "GpuTileTaskWorkerPool::Shutdown");
130 task_graph_runner_
->ScheduleTasks(namespace_token_
, &empty
);
131 task_graph_runner_
->WaitForTasksToFinishRunning(namespace_token_
);
134 void GpuTileTaskWorkerPool::ScheduleTasks(TileTaskQueue
* queue
) {
135 TRACE_EVENT0("cc", "GpuTileTaskWorkerPool::ScheduleTasks");
137 // Mark all task sets as pending.
138 tasks_pending_
.set();
140 size_t priority
= kTileTaskPriorityBase
;
144 // Cancel existing OnTaskSetFinished callbacks.
145 task_set_finished_weak_ptr_factory_
.InvalidateWeakPtrs();
147 scoped_refptr
<TileTask
> new_task_set_finished_tasks
[kNumberOfTaskSets
];
149 size_t task_count
[kNumberOfTaskSets
] = {0};
151 for (TaskSet task_set
= 0; task_set
< kNumberOfTaskSets
; ++task_set
) {
152 new_task_set_finished_tasks
[task_set
] = CreateTaskSetFinishedTask(
154 base::Bind(&GpuTileTaskWorkerPool::OnTaskSetFinished
,
155 task_set_finished_weak_ptr_factory_
.GetWeakPtr(), task_set
));
158 for (TileTaskQueue::Item::Vector::const_iterator it
= queue
->items
.begin();
159 it
!= queue
->items
.end(); ++it
) {
160 const TileTaskQueue::Item
& item
= *it
;
161 RasterTask
* task
= item
.task
;
162 DCHECK(!task
->HasCompleted());
164 for (TaskSet task_set
= 0; task_set
< kNumberOfTaskSets
; ++task_set
) {
165 if (!item
.task_sets
[task_set
])
168 ++task_count
[task_set
];
170 graph_
.edges
.push_back(
171 TaskGraph::Edge(task
, new_task_set_finished_tasks
[task_set
].get()));
174 InsertNodesForRasterTask(&graph_
, task
, task
->dependencies(), priority
++);
177 for (TaskSet task_set
= 0; task_set
< kNumberOfTaskSets
; ++task_set
) {
178 InsertNodeForTask(&graph_
, new_task_set_finished_tasks
[task_set
].get(),
179 kTaskSetFinishedTaskPriorityBase
+ task_set
,
180 task_count
[task_set
]);
183 ScheduleTasksOnOriginThread(this, &graph_
);
185 // Barrier to sync any new resources to the worker context.
186 rasterizer_
->resource_provider()
190 ->OrderingBarrierCHROMIUM();
192 task_graph_runner_
->ScheduleTasks(namespace_token_
, &graph_
);
194 std::copy(new_task_set_finished_tasks
,
195 new_task_set_finished_tasks
+ kNumberOfTaskSets
,
196 task_set_finished_tasks_
);
199 void GpuTileTaskWorkerPool::CheckForCompletedTasks() {
200 TRACE_EVENT0("cc", "GpuTileTaskWorkerPool::CheckForCompletedTasks");
202 task_graph_runner_
->CollectCompletedTasks(namespace_token_
,
204 CompleteTasks(completed_tasks_
);
205 completed_tasks_
.clear();
208 ResourceFormat
GpuTileTaskWorkerPool::GetResourceFormat() const {
209 return rasterizer_
->resource_provider()->best_render_buffer_format();
212 bool GpuTileTaskWorkerPool::GetResourceRequiresSwizzle() const {
213 // This doesn't require a swizzle because we rasterize to the correct format.
217 void GpuTileTaskWorkerPool::CompleteTasks(const Task::Vector
& tasks
) {
218 for (auto& task
: tasks
) {
219 TileTask
* tile_task
= static_cast<TileTask
*>(task
.get());
221 tile_task
->WillComplete();
222 tile_task
->CompleteOnOriginThread(this);
223 tile_task
->DidComplete();
225 tile_task
->RunReplyOnOriginThread();
227 completed_tasks_
.clear();
230 scoped_ptr
<RasterBuffer
> GpuTileTaskWorkerPool::AcquireBufferForRaster(
231 const Resource
* resource
,
232 uint64_t resource_content_id
,
233 uint64_t previous_content_id
) {
234 return scoped_ptr
<RasterBuffer
>(new RasterBufferImpl(
235 rasterizer_
.get(), resource
, resource_content_id
, previous_content_id
));
238 void GpuTileTaskWorkerPool::ReleaseBufferForRaster(
239 scoped_ptr
<RasterBuffer
> buffer
) {
240 // Nothing to do here. RasterBufferImpl destructor cleans up after itself.
243 void GpuTileTaskWorkerPool::OnTaskSetFinished(TaskSet task_set
) {
244 TRACE_EVENT1("cc", "GpuTileTaskWorkerPool::OnTaskSetFinished", "task_set",
247 DCHECK(tasks_pending_
[task_set
]);
248 tasks_pending_
[task_set
] = false;
249 client_
->DidFinishRunningTileTasks(task_set
);