chrome: Remove unneeded dependencies on libxml.
[chromium-blink-merge.git] / cc / raster / one_copy_tile_task_worker_pool.cc
blob78e6b106ce94308185987f79f3d47d8bd282c630
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/one_copy_tile_task_worker_pool.h"
7 #include <algorithm>
8 #include <limits>
10 #include "base/strings/stringprintf.h"
11 #include "base/trace_event/trace_event.h"
12 #include "base/trace_event/trace_event_argument.h"
13 #include "cc/base/math_util.h"
14 #include "cc/debug/traced_value.h"
15 #include "cc/raster/raster_buffer.h"
16 #include "cc/resources/platform_color.h"
17 #include "cc/resources/resource_pool.h"
18 #include "cc/resources/scoped_resource.h"
19 #include "gpu/command_buffer/client/gles2_interface.h"
20 #include "ui/gfx/gpu_memory_buffer.h"
22 namespace cc {
23 namespace {
25 class RasterBufferImpl : public RasterBuffer {
26 public:
27 RasterBufferImpl(OneCopyTileTaskWorkerPool* worker_pool,
28 ResourceProvider* resource_provider,
29 ResourcePool* resource_pool,
30 ResourceFormat resource_format,
31 const Resource* output_resource,
32 uint64_t previous_content_id)
33 : worker_pool_(worker_pool),
34 resource_provider_(resource_provider),
35 resource_pool_(resource_pool),
36 output_resource_(output_resource),
37 raster_content_id_(0),
38 sequence_(0) {
39 if (worker_pool->have_persistent_gpu_memory_buffers() &&
40 previous_content_id) {
41 raster_resource_ =
42 resource_pool->TryAcquireResourceWithContentId(previous_content_id);
44 if (raster_resource_) {
45 raster_content_id_ = previous_content_id;
46 DCHECK_EQ(resource_format, raster_resource_->format());
47 DCHECK_EQ(output_resource->size().ToString(),
48 raster_resource_->size().ToString());
49 } else {
50 raster_resource_ = resource_pool->AcquireResource(output_resource->size(),
51 resource_format);
54 lock_.reset(new ResourceProvider::ScopedWriteLockGpuMemoryBuffer(
55 resource_provider_, raster_resource_->id()));
58 ~RasterBufferImpl() override {
59 // Release write lock in case a copy was never scheduled.
60 lock_.reset();
62 // Make sure any scheduled copy operations are issued before we release the
63 // raster resource.
64 if (sequence_)
65 worker_pool_->AdvanceLastIssuedCopyTo(sequence_);
67 // Return resources to pool so they can be used by another RasterBuffer
68 // instance.
69 resource_pool_->ReleaseResource(raster_resource_.Pass(),
70 raster_content_id_);
73 // Overridden from RasterBuffer:
74 void Playback(const RasterSource* raster_source,
75 const gfx::Rect& raster_full_rect,
76 const gfx::Rect& raster_dirty_rect,
77 uint64_t new_content_id,
78 float scale) override {
79 // If there's a raster_content_id_, we are reusing a resource with that
80 // content id.
81 bool reusing_raster_resource = raster_content_id_ != 0;
82 sequence_ = worker_pool_->PlaybackAndScheduleCopyOnWorkerThread(
83 reusing_raster_resource, lock_.Pass(), raster_resource_.get(),
84 output_resource_, raster_source, raster_full_rect, raster_dirty_rect,
85 scale);
86 // Store the content id of the resource to return to the pool.
87 raster_content_id_ = new_content_id;
90 private:
91 OneCopyTileTaskWorkerPool* worker_pool_;
92 ResourceProvider* resource_provider_;
93 ResourcePool* resource_pool_;
94 const Resource* output_resource_;
95 uint64_t raster_content_id_;
96 scoped_ptr<ScopedResource> raster_resource_;
97 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer> lock_;
98 CopySequenceNumber sequence_;
100 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl);
103 // Number of in-flight copy operations to allow.
104 const int kMaxCopyOperations = 32;
106 // Delay been checking for copy operations to complete.
107 const int kCheckForCompletedCopyOperationsTickRateMs = 1;
109 // Number of failed attempts to allow before we perform a check that will
110 // wait for copy operations to complete if needed.
111 const int kFailedAttemptsBeforeWaitIfNeeded = 256;
113 // 4MiB is the size of 4 512x512 tiles, which has proven to be a good
114 // default batch size for copy operations.
115 const int kMaxBytesPerCopyOperation = 1024 * 1024 * 4;
117 } // namespace
119 OneCopyTileTaskWorkerPool::CopyOperation::CopyOperation(
120 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer> src_write_lock,
121 const Resource* src,
122 const Resource* dst,
123 const gfx::Rect& rect)
124 : src_write_lock(src_write_lock.Pass()), src(src), dst(dst), rect(rect) {
127 OneCopyTileTaskWorkerPool::CopyOperation::~CopyOperation() {
130 // static
131 scoped_ptr<TileTaskWorkerPool> OneCopyTileTaskWorkerPool::Create(
132 base::SequencedTaskRunner* task_runner,
133 TaskGraphRunner* task_graph_runner,
134 ContextProvider* context_provider,
135 ResourceProvider* resource_provider,
136 ResourcePool* resource_pool,
137 int max_copy_texture_chromium_size,
138 bool have_persistent_gpu_memory_buffers) {
139 return make_scoped_ptr<TileTaskWorkerPool>(new OneCopyTileTaskWorkerPool(
140 task_runner, task_graph_runner, context_provider, resource_provider,
141 resource_pool, max_copy_texture_chromium_size,
142 have_persistent_gpu_memory_buffers));
145 OneCopyTileTaskWorkerPool::OneCopyTileTaskWorkerPool(
146 base::SequencedTaskRunner* task_runner,
147 TaskGraphRunner* task_graph_runner,
148 ContextProvider* context_provider,
149 ResourceProvider* resource_provider,
150 ResourcePool* resource_pool,
151 int max_copy_texture_chromium_size,
152 bool have_persistent_gpu_memory_buffers)
153 : task_runner_(task_runner),
154 task_graph_runner_(task_graph_runner),
155 namespace_token_(task_graph_runner->GetNamespaceToken()),
156 context_provider_(context_provider),
157 resource_provider_(resource_provider),
158 resource_pool_(resource_pool),
159 max_bytes_per_copy_operation_(
160 max_copy_texture_chromium_size
161 ? std::min(kMaxBytesPerCopyOperation,
162 max_copy_texture_chromium_size)
163 : kMaxBytesPerCopyOperation),
164 have_persistent_gpu_memory_buffers_(have_persistent_gpu_memory_buffers),
165 last_issued_copy_operation_(0),
166 last_flushed_copy_operation_(0),
167 lock_(),
168 copy_operation_count_cv_(&lock_),
169 bytes_scheduled_since_last_flush_(0),
170 issued_copy_operation_count_(0),
171 next_copy_operation_sequence_(1),
172 check_for_completed_copy_operations_pending_(false),
173 shutdown_(false),
174 weak_ptr_factory_(this),
175 task_set_finished_weak_ptr_factory_(this) {
176 DCHECK(context_provider_);
179 OneCopyTileTaskWorkerPool::~OneCopyTileTaskWorkerPool() {
180 DCHECK_EQ(pending_copy_operations_.size(), 0u);
183 TileTaskRunner* OneCopyTileTaskWorkerPool::AsTileTaskRunner() {
184 return this;
187 void OneCopyTileTaskWorkerPool::SetClient(TileTaskRunnerClient* client) {
188 client_ = client;
191 void OneCopyTileTaskWorkerPool::Shutdown() {
192 TRACE_EVENT0("cc", "OneCopyTileTaskWorkerPool::Shutdown");
195 base::AutoLock lock(lock_);
197 shutdown_ = true;
198 copy_operation_count_cv_.Signal();
201 TaskGraph empty;
202 task_graph_runner_->ScheduleTasks(namespace_token_, &empty);
203 task_graph_runner_->WaitForTasksToFinishRunning(namespace_token_);
206 void OneCopyTileTaskWorkerPool::ScheduleTasks(TileTaskQueue* queue) {
207 TRACE_EVENT0("cc", "OneCopyTileTaskWorkerPool::ScheduleTasks");
209 #if DCHECK_IS_ON()
211 base::AutoLock lock(lock_);
212 DCHECK(!shutdown_);
214 #endif
216 if (tasks_pending_.none())
217 TRACE_EVENT_ASYNC_BEGIN0("cc", "ScheduledTasks", this);
219 // Mark all task sets as pending.
220 tasks_pending_.set();
222 size_t priority = kTileTaskPriorityBase;
224 graph_.Reset();
226 // Cancel existing OnTaskSetFinished callbacks.
227 task_set_finished_weak_ptr_factory_.InvalidateWeakPtrs();
229 scoped_refptr<TileTask> new_task_set_finished_tasks[kNumberOfTaskSets];
231 size_t task_count[kNumberOfTaskSets] = {0};
233 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) {
234 new_task_set_finished_tasks[task_set] = CreateTaskSetFinishedTask(
235 task_runner_.get(),
236 base::Bind(&OneCopyTileTaskWorkerPool::OnTaskSetFinished,
237 task_set_finished_weak_ptr_factory_.GetWeakPtr(), task_set));
240 resource_pool_->CheckBusyResources(false);
242 for (TileTaskQueue::Item::Vector::const_iterator it = queue->items.begin();
243 it != queue->items.end(); ++it) {
244 const TileTaskQueue::Item& item = *it;
245 RasterTask* task = item.task;
246 DCHECK(!task->HasCompleted());
248 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) {
249 if (!item.task_sets[task_set])
250 continue;
252 ++task_count[task_set];
254 graph_.edges.push_back(
255 TaskGraph::Edge(task, new_task_set_finished_tasks[task_set].get()));
258 InsertNodesForRasterTask(&graph_, task, task->dependencies(), priority++);
261 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) {
262 InsertNodeForTask(&graph_, new_task_set_finished_tasks[task_set].get(),
263 kTaskSetFinishedTaskPriorityBase + task_set,
264 task_count[task_set]);
267 ScheduleTasksOnOriginThread(this, &graph_);
268 task_graph_runner_->ScheduleTasks(namespace_token_, &graph_);
270 std::copy(new_task_set_finished_tasks,
271 new_task_set_finished_tasks + kNumberOfTaskSets,
272 task_set_finished_tasks_);
274 resource_pool_->ReduceResourceUsage();
276 TRACE_EVENT_ASYNC_STEP_INTO1("cc", "ScheduledTasks", this, "running", "state",
277 StateAsValue());
280 void OneCopyTileTaskWorkerPool::CheckForCompletedTasks() {
281 TRACE_EVENT0("cc", "OneCopyTileTaskWorkerPool::CheckForCompletedTasks");
283 task_graph_runner_->CollectCompletedTasks(namespace_token_,
284 &completed_tasks_);
286 for (Task::Vector::const_iterator it = completed_tasks_.begin();
287 it != completed_tasks_.end(); ++it) {
288 TileTask* task = static_cast<TileTask*>(it->get());
290 task->WillComplete();
291 task->CompleteOnOriginThread(this);
292 task->DidComplete();
294 task->RunReplyOnOriginThread();
296 completed_tasks_.clear();
299 ResourceFormat OneCopyTileTaskWorkerPool::GetResourceFormat() const {
300 return resource_provider_->best_texture_format();
303 bool OneCopyTileTaskWorkerPool::GetResourceRequiresSwizzle() const {
304 return !PlatformColor::SameComponentOrder(GetResourceFormat());
307 scoped_ptr<RasterBuffer> OneCopyTileTaskWorkerPool::AcquireBufferForRaster(
308 const Resource* resource,
309 uint64_t resource_content_id,
310 uint64_t previous_content_id) {
311 // TODO(danakj): If resource_content_id != 0, we only need to copy/upload
312 // the dirty rect.
313 DCHECK_EQ(resource->format(), resource_provider_->best_texture_format());
314 return make_scoped_ptr<RasterBuffer>(
315 new RasterBufferImpl(this, resource_provider_, resource_pool_,
316 resource_provider_->best_texture_format(), resource,
317 previous_content_id));
320 void OneCopyTileTaskWorkerPool::ReleaseBufferForRaster(
321 scoped_ptr<RasterBuffer> buffer) {
322 // Nothing to do here. RasterBufferImpl destructor cleans up after itself.
325 CopySequenceNumber
326 OneCopyTileTaskWorkerPool::PlaybackAndScheduleCopyOnWorkerThread(
327 bool reusing_raster_resource,
328 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer>
329 raster_resource_write_lock,
330 const Resource* raster_resource,
331 const Resource* output_resource,
332 const RasterSource* raster_source,
333 const gfx::Rect& raster_full_rect,
334 const gfx::Rect& raster_dirty_rect,
335 float scale) {
336 gfx::GpuMemoryBuffer* gpu_memory_buffer =
337 raster_resource_write_lock->GetGpuMemoryBuffer();
338 if (gpu_memory_buffer) {
339 void* data = NULL;
340 bool rv = gpu_memory_buffer->Map(&data);
341 DCHECK(rv);
342 int stride;
343 gpu_memory_buffer->GetStride(&stride);
344 // TileTaskWorkerPool::PlaybackToMemory only supports unsigned strides.
345 DCHECK_GE(stride, 0);
347 gfx::Rect playback_rect = raster_full_rect;
348 if (reusing_raster_resource) {
349 playback_rect.Intersect(raster_dirty_rect);
351 DCHECK(!playback_rect.IsEmpty())
352 << "Why are we rastering a tile that's not dirty?";
353 TileTaskWorkerPool::PlaybackToMemory(
354 data, raster_resource->format(), raster_resource->size(),
355 static_cast<size_t>(stride), raster_source, raster_full_rect,
356 playback_rect, scale);
357 gpu_memory_buffer->Unmap();
360 base::AutoLock lock(lock_);
362 CopySequenceNumber sequence = 0;
363 int bytes_per_row = (BitsPerPixel(raster_resource->format()) *
364 raster_resource->size().width()) /
366 int chunk_size_in_rows =
367 std::max(1, max_bytes_per_copy_operation_ / bytes_per_row);
368 // Align chunk size to 4. Required to support compressed texture formats.
369 chunk_size_in_rows = MathUtil::RoundUp(chunk_size_in_rows, 4);
370 int y = 0;
371 int height = raster_resource->size().height();
372 while (y < height) {
373 int failed_attempts = 0;
374 while ((pending_copy_operations_.size() + issued_copy_operation_count_) >=
375 kMaxCopyOperations) {
376 // Ignore limit when shutdown is set.
377 if (shutdown_)
378 break;
380 ++failed_attempts;
382 // Schedule a check that will also wait for operations to complete
383 // after too many failed attempts.
384 bool wait_if_needed = failed_attempts > kFailedAttemptsBeforeWaitIfNeeded;
386 // Schedule a check for completed copy operations if too many operations
387 // are currently in-flight.
388 ScheduleCheckForCompletedCopyOperationsWithLockAcquired(wait_if_needed);
391 TRACE_EVENT0("cc", "WaitingForCopyOperationsToComplete");
393 // Wait for in-flight copy operations to drop below limit.
394 copy_operation_count_cv_.Wait();
398 // There may be more work available, so wake up another worker thread.
399 copy_operation_count_cv_.Signal();
401 // Copy at most |chunk_size_in_rows|.
402 int rows_to_copy = std::min(chunk_size_in_rows, height - y);
403 DCHECK_GT(rows_to_copy, 0);
405 // |raster_resource_write_lock| is passed to the first copy operation as it
406 // needs to be released before we can issue a copy.
407 pending_copy_operations_.push_back(make_scoped_ptr(new CopyOperation(
408 raster_resource_write_lock.Pass(), raster_resource, output_resource,
409 gfx::Rect(0, y, raster_resource->size().width(), rows_to_copy))));
410 y += rows_to_copy;
412 // Acquire a sequence number for this copy operation.
413 sequence = next_copy_operation_sequence_++;
415 // Increment |bytes_scheduled_since_last_flush_| by the amount of memory
416 // used for this copy operation.
417 bytes_scheduled_since_last_flush_ += rows_to_copy * bytes_per_row;
419 // Post task that will advance last flushed copy operation to |sequence|
420 // when |bytes_scheduled_since_last_flush_| has reached
421 // |max_bytes_per_copy_operation_|.
422 if (bytes_scheduled_since_last_flush_ >= max_bytes_per_copy_operation_) {
423 task_runner_->PostTask(
424 FROM_HERE,
425 base::Bind(&OneCopyTileTaskWorkerPool::AdvanceLastFlushedCopyTo,
426 weak_ptr_factory_.GetWeakPtr(), sequence));
427 bytes_scheduled_since_last_flush_ = 0;
431 return sequence;
434 void OneCopyTileTaskWorkerPool::AdvanceLastIssuedCopyTo(
435 CopySequenceNumber sequence) {
436 if (last_issued_copy_operation_ >= sequence)
437 return;
439 IssueCopyOperations(sequence - last_issued_copy_operation_);
440 last_issued_copy_operation_ = sequence;
443 void OneCopyTileTaskWorkerPool::AdvanceLastFlushedCopyTo(
444 CopySequenceNumber sequence) {
445 if (last_flushed_copy_operation_ >= sequence)
446 return;
448 AdvanceLastIssuedCopyTo(sequence);
450 // Flush all issued copy operations.
451 context_provider_->ContextGL()->ShallowFlushCHROMIUM();
452 last_flushed_copy_operation_ = last_issued_copy_operation_;
455 void OneCopyTileTaskWorkerPool::OnTaskSetFinished(TaskSet task_set) {
456 TRACE_EVENT1("cc", "OneCopyTileTaskWorkerPool::OnTaskSetFinished", "task_set",
457 task_set);
459 DCHECK(tasks_pending_[task_set]);
460 tasks_pending_[task_set] = false;
461 if (tasks_pending_.any()) {
462 TRACE_EVENT_ASYNC_STEP_INTO1("cc", "ScheduledTasks", this, "running",
463 "state", StateAsValue());
464 } else {
465 TRACE_EVENT_ASYNC_END0("cc", "ScheduledTasks", this);
467 client_->DidFinishRunningTileTasks(task_set);
470 void OneCopyTileTaskWorkerPool::IssueCopyOperations(int64 count) {
471 TRACE_EVENT1("cc", "OneCopyTileTaskWorkerPool::IssueCopyOperations", "count",
472 count);
474 CopyOperation::Deque copy_operations;
477 base::AutoLock lock(lock_);
479 for (int64 i = 0; i < count; ++i) {
480 DCHECK(!pending_copy_operations_.empty());
481 copy_operations.push_back(pending_copy_operations_.take_front());
484 // Increment |issued_copy_operation_count_| to reflect the transition of
485 // copy operations from "pending" to "issued" state.
486 issued_copy_operation_count_ += copy_operations.size();
489 while (!copy_operations.empty()) {
490 scoped_ptr<CopyOperation> copy_operation = copy_operations.take_front();
492 // Remove the write lock.
493 copy_operation->src_write_lock.reset();
495 // Copy contents of source resource to destination resource.
496 resource_provider_->CopyResource(copy_operation->src->id(),
497 copy_operation->dst->id(),
498 copy_operation->rect);
502 void OneCopyTileTaskWorkerPool::
503 ScheduleCheckForCompletedCopyOperationsWithLockAcquired(
504 bool wait_if_needed) {
505 lock_.AssertAcquired();
507 if (check_for_completed_copy_operations_pending_)
508 return;
510 base::TimeTicks now = base::TimeTicks::Now();
512 // Schedule a check for completed copy operations as soon as possible but
513 // don't allow two consecutive checks to be scheduled to run less than the
514 // tick rate apart.
515 base::TimeTicks next_check_for_completed_copy_operations_time =
516 std::max(last_check_for_completed_copy_operations_time_ +
517 base::TimeDelta::FromMilliseconds(
518 kCheckForCompletedCopyOperationsTickRateMs),
519 now);
521 task_runner_->PostDelayedTask(
522 FROM_HERE,
523 base::Bind(&OneCopyTileTaskWorkerPool::CheckForCompletedCopyOperations,
524 weak_ptr_factory_.GetWeakPtr(), wait_if_needed),
525 next_check_for_completed_copy_operations_time - now);
527 last_check_for_completed_copy_operations_time_ =
528 next_check_for_completed_copy_operations_time;
529 check_for_completed_copy_operations_pending_ = true;
532 void OneCopyTileTaskWorkerPool::CheckForCompletedCopyOperations(
533 bool wait_if_needed) {
534 TRACE_EVENT1("cc",
535 "OneCopyTileTaskWorkerPool::CheckForCompletedCopyOperations",
536 "wait_if_needed", wait_if_needed);
538 resource_pool_->CheckBusyResources(wait_if_needed);
541 base::AutoLock lock(lock_);
543 DCHECK(check_for_completed_copy_operations_pending_);
544 check_for_completed_copy_operations_pending_ = false;
546 // The number of busy resources in the pool reflects the number of issued
547 // copy operations that have not yet completed.
548 issued_copy_operation_count_ = resource_pool_->busy_resource_count();
550 // There may be work blocked on too many in-flight copy operations, so wake
551 // up a worker thread.
552 copy_operation_count_cv_.Signal();
556 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
557 OneCopyTileTaskWorkerPool::StateAsValue() const {
558 scoped_refptr<base::trace_event::TracedValue> state =
559 new base::trace_event::TracedValue();
561 state->BeginArray("tasks_pending");
562 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set)
563 state->AppendBoolean(tasks_pending_[task_set]);
564 state->EndArray();
565 state->BeginDictionary("staging_state");
566 StagingStateAsValueInto(state.get());
567 state->EndDictionary();
569 return state;
572 void OneCopyTileTaskWorkerPool::StagingStateAsValueInto(
573 base::trace_event::TracedValue* staging_state) const {
574 staging_state->SetInteger(
575 "staging_resource_count",
576 static_cast<int>(resource_pool_->total_resource_count()));
577 staging_state->SetInteger(
578 "bytes_used_for_staging_resources",
579 static_cast<int>(resource_pool_->total_memory_usage_bytes()));
580 staging_state->SetInteger(
581 "pending_copy_count",
582 static_cast<int>(resource_pool_->total_resource_count() -
583 resource_pool_->acquired_resource_count()));
584 staging_state->SetInteger(
585 "bytes_pending_copy",
586 static_cast<int>(resource_pool_->total_memory_usage_bytes() -
587 resource_pool_->acquired_memory_usage_bytes()));
590 } // namespace cc