This sets up API to release OutputSurface from LTHClient.
[chromium-blink-merge.git] / cc / raster / gpu_tile_task_worker_pool.cc
blob0f3d6077813207b1eaed9d80dcc52e1e3268ba50
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"
7 #include <algorithm>
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"
21 namespace cc {
22 namespace {
24 class RasterBufferImpl : public RasterBuffer {
25 public:
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,
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()
47 ->output_surface()
48 ->worker_context_provider();
49 DCHECK(context_provider);
51 ContextProvider::ScopedContextLock scoped_context(context_provider);
53 gfx::Rect playback_rect = raster_full_rect;
54 if (resource_has_previous_content_) {
55 playback_rect.Intersect(raster_dirty_rect);
57 DCHECK(!playback_rect.IsEmpty())
58 << "Why are we rastering a tile that's not dirty?";
60 // TODO(danakj): Implement partial raster with raster_dirty_rect.
61 // Rasterize source into resource.
62 rasterizer_->RasterizeSource(&lock_, raster_source, raster_full_rect,
63 playback_rect, scale);
65 // Barrier to sync worker context output to cc context.
66 scoped_context.ContextGL()->OrderingBarrierCHROMIUM();
69 private:
70 GpuRasterizer* rasterizer_;
71 ResourceProvider::ScopedWriteLockGr lock_;
72 bool resource_has_previous_content_;
74 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl);
77 } // namespace
79 // static
80 scoped_ptr<TileTaskWorkerPool> GpuTileTaskWorkerPool::Create(
81 base::SequencedTaskRunner* task_runner,
82 TaskGraphRunner* task_graph_runner,
83 ContextProvider* context_provider,
84 ResourceProvider* resource_provider,
85 bool use_distance_field_text,
86 int gpu_rasterization_msaa_sample_count) {
87 return make_scoped_ptr<TileTaskWorkerPool>(new GpuTileTaskWorkerPool(
88 task_runner, task_graph_runner, context_provider, resource_provider,
89 use_distance_field_text, gpu_rasterization_msaa_sample_count));
92 GpuTileTaskWorkerPool::GpuTileTaskWorkerPool(
93 base::SequencedTaskRunner* task_runner,
94 TaskGraphRunner* task_graph_runner,
95 ContextProvider* context_provider,
96 ResourceProvider* resource_provider,
97 bool use_distance_field_text,
98 int gpu_rasterization_msaa_sample_count)
99 : task_runner_(task_runner),
100 task_graph_runner_(task_graph_runner),
101 namespace_token_(task_graph_runner_->GetNamespaceToken()),
102 rasterizer_(new GpuRasterizer(context_provider,
103 resource_provider,
104 use_distance_field_text,
105 gpu_rasterization_msaa_sample_count)),
106 task_set_finished_weak_ptr_factory_(this),
107 weak_ptr_factory_(this) {
110 GpuTileTaskWorkerPool::~GpuTileTaskWorkerPool() {
111 DCHECK_EQ(0u, completed_tasks_.size());
114 TileTaskRunner* GpuTileTaskWorkerPool::AsTileTaskRunner() {
115 return this;
118 void GpuTileTaskWorkerPool::SetClient(TileTaskRunnerClient* client) {
119 client_ = client;
122 void GpuTileTaskWorkerPool::Shutdown() {
123 TRACE_EVENT0("cc", "GpuTileTaskWorkerPool::Shutdown");
125 TaskGraph empty;
126 task_graph_runner_->ScheduleTasks(namespace_token_, &empty);
127 task_graph_runner_->WaitForTasksToFinishRunning(namespace_token_);
130 void GpuTileTaskWorkerPool::ScheduleTasks(TileTaskQueue* queue) {
131 TRACE_EVENT0("cc", "GpuTileTaskWorkerPool::ScheduleTasks");
133 // Mark all task sets as pending.
134 tasks_pending_.set();
136 size_t priority = kTileTaskPriorityBase;
138 graph_.Reset();
140 // Cancel existing OnTaskSetFinished callbacks.
141 task_set_finished_weak_ptr_factory_.InvalidateWeakPtrs();
143 scoped_refptr<TileTask> new_task_set_finished_tasks[kNumberOfTaskSets];
145 size_t task_count[kNumberOfTaskSets] = {0};
147 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) {
148 new_task_set_finished_tasks[task_set] = CreateTaskSetFinishedTask(
149 task_runner_.get(),
150 base::Bind(&GpuTileTaskWorkerPool::OnTaskSetFinished,
151 task_set_finished_weak_ptr_factory_.GetWeakPtr(), task_set));
154 for (TileTaskQueue::Item::Vector::const_iterator it = queue->items.begin();
155 it != queue->items.end(); ++it) {
156 const TileTaskQueue::Item& item = *it;
157 RasterTask* task = item.task;
158 DCHECK(!task->HasCompleted());
160 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) {
161 if (!item.task_sets[task_set])
162 continue;
164 ++task_count[task_set];
166 graph_.edges.push_back(
167 TaskGraph::Edge(task, new_task_set_finished_tasks[task_set].get()));
170 InsertNodesForRasterTask(&graph_, task, task->dependencies(), priority++);
173 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) {
174 InsertNodeForTask(&graph_, new_task_set_finished_tasks[task_set].get(),
175 kTaskSetFinishedTaskPriorityBase + task_set,
176 task_count[task_set]);
179 ScheduleTasksOnOriginThread(this, &graph_);
181 // Barrier to sync any new resources to the worker context.
182 rasterizer_->resource_provider()
183 ->output_surface()
184 ->context_provider()
185 ->ContextGL()
186 ->OrderingBarrierCHROMIUM();
188 task_graph_runner_->ScheduleTasks(namespace_token_, &graph_);
190 std::copy(new_task_set_finished_tasks,
191 new_task_set_finished_tasks + kNumberOfTaskSets,
192 task_set_finished_tasks_);
195 void GpuTileTaskWorkerPool::CheckForCompletedTasks() {
196 TRACE_EVENT0("cc", "GpuTileTaskWorkerPool::CheckForCompletedTasks");
198 task_graph_runner_->CollectCompletedTasks(namespace_token_,
199 &completed_tasks_);
200 CompleteTasks(completed_tasks_);
201 completed_tasks_.clear();
204 ResourceFormat GpuTileTaskWorkerPool::GetResourceFormat() const {
205 return rasterizer_->resource_provider()->best_render_buffer_format();
208 bool GpuTileTaskWorkerPool::GetResourceRequiresSwizzle() const {
209 // This doesn't require a swizzle because we rasterize to the correct format.
210 return false;
213 void GpuTileTaskWorkerPool::CompleteTasks(const Task::Vector& tasks) {
214 for (auto& task : tasks) {
215 TileTask* tile_task = static_cast<TileTask*>(task.get());
217 tile_task->WillComplete();
218 tile_task->CompleteOnOriginThread(this);
219 tile_task->DidComplete();
221 completed_tasks_.clear();
224 scoped_ptr<RasterBuffer> GpuTileTaskWorkerPool::AcquireBufferForRaster(
225 const Resource* resource,
226 uint64_t resource_content_id,
227 uint64_t previous_content_id) {
228 return scoped_ptr<RasterBuffer>(new RasterBufferImpl(
229 rasterizer_.get(), resource, resource_content_id, previous_content_id));
232 void GpuTileTaskWorkerPool::ReleaseBufferForRaster(
233 scoped_ptr<RasterBuffer> buffer) {
234 // Nothing to do here. RasterBufferImpl destructor cleans up after itself.
237 void GpuTileTaskWorkerPool::OnTaskSetFinished(TaskSet task_set) {
238 TRACE_EVENT1("cc", "GpuTileTaskWorkerPool::OnTaskSetFinished", "task_set",
239 task_set);
241 DCHECK(tasks_pending_[task_set]);
242 tasks_pending_[task_set] = false;
243 client_->DidFinishRunningTileTasks(task_set);
246 } // namespace cc