OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/resources/one_copy_tile_task_worker_pool.h" | 5 #include "cc/resources/one_copy_tile_task_worker_pool.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
11 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
12 #include "base/trace_event/trace_event_argument.h" | 12 #include "base/trace_event/trace_event_argument.h" |
13 #include "cc/debug/traced_value.h" | 13 #include "cc/debug/traced_value.h" |
14 #include "cc/resources/raster_buffer.h" | 14 #include "cc/resources/raster_buffer.h" |
15 #include "cc/resources/resource_pool.h" | 15 #include "cc/resources/resource_pool.h" |
16 #include "cc/resources/scoped_resource.h" | 16 #include "cc/resources/scoped_resource.h" |
17 #include "gpu/command_buffer/client/gles2_interface.h" | 17 #include "gpu/command_buffer/client/gles2_interface.h" |
18 #include "ui/gfx/gpu_memory_buffer.h" | 18 #include "ui/gfx/gpu_memory_buffer.h" |
19 | 19 |
20 namespace cc { | 20 namespace cc { |
21 namespace { | 21 namespace { |
22 | 22 |
23 class RasterBufferImpl : public RasterBuffer { | 23 class RasterBufferImpl : public RasterBuffer { |
24 public: | 24 public: |
25 RasterBufferImpl(OneCopyTileTaskWorkerPool* worker_pool, | 25 RasterBufferImpl(OneCopyTileTaskWorkerPool* worker_pool, |
26 ResourceProvider* resource_provider, | 26 ResourceProvider* resource_provider, |
27 ResourcePool* resource_pool, | 27 ResourcePool* resource_pool, |
28 ResourceFormat resource_format, | 28 ResourceFormat resource_format, |
29 const Resource* resource) | 29 const TileTaskData& data) |
30 : worker_pool_(worker_pool), | 30 : worker_pool_(worker_pool), |
31 resource_provider_(resource_provider), | 31 resource_provider_(resource_provider), |
32 resource_pool_(resource_pool), | 32 resource_pool_(resource_pool), |
33 resource_(resource), | 33 tile_task_data_(data), |
34 raster_resource_( | 34 raster_resource_(resource_pool->AcquireResource(data.resource->size(), |
35 resource_pool->AcquireResource(resource->size(), resource_format)), | 35 resource_format)), |
| 36 previous_frame_raster_resource_( |
| 37 resource_pool->TryAcquireOldResource(data.resource->size(), |
| 38 resource_format, |
| 39 data.previous_tile_id)), |
36 lock_(new ResourceProvider::ScopedWriteLockGpuMemoryBuffer( | 40 lock_(new ResourceProvider::ScopedWriteLockGpuMemoryBuffer( |
37 resource_provider_, | 41 resource_provider_, |
38 raster_resource_->id())), | 42 raster_resource_->id())), |
39 sequence_(0) {} | 43 sequence_(0) { |
| 44 if (previous_frame_raster_resource_) { |
| 45 previous_frame_lock_.reset( |
| 46 new ResourceProvider::ScopedReadLockGpuMemoryBuffer( |
| 47 resource_provider_, previous_frame_raster_resource_->id())); |
| 48 } |
| 49 } |
40 | 50 |
41 ~RasterBufferImpl() override { | 51 ~RasterBufferImpl() override { |
42 // Release write lock in case a copy was never scheduled. | 52 // Release write/read lock in case a copy was never scheduled. |
43 lock_.reset(); | 53 lock_.reset(); |
| 54 previous_frame_lock_.reset(); |
44 | 55 |
45 // Make sure any scheduled copy operations are issued before we release the | 56 // Make sure any scheduled copy operations are issued before we release the |
46 // raster resource. | 57 // raster resource. |
47 if (sequence_) | 58 if (sequence_) |
48 worker_pool_->AdvanceLastIssuedCopyTo(sequence_); | 59 worker_pool_->AdvanceLastIssuedCopyTo(sequence_); |
49 | 60 |
50 // Return raster resource to pool so it can be used by another RasterBuffer | 61 // Return resources to pool so they can be used by another RasterBuffer |
51 // instance. | 62 // instance. |
52 if (raster_resource_) | 63 if (raster_resource_) { |
53 resource_pool_->ReleaseResource(raster_resource_.Pass()); | 64 resource_pool_->ReleaseResource(raster_resource_.Pass(), |
| 65 tile_task_data_.new_tile_id); |
| 66 } |
| 67 if (previous_frame_raster_resource_) { |
| 68 resource_pool_->ReleaseResource(raster_resource_.Pass(), |
| 69 tile_task_data_.previous_tile_id); |
| 70 } |
54 } | 71 } |
55 | 72 |
56 // Overridden from RasterBuffer: | 73 // Overridden from RasterBuffer: |
57 void Playback(const RasterSource* raster_source, | 74 void Playback(const RasterSource* raster_source, |
58 const gfx::Rect& rect, | 75 const gfx::Rect& rect, |
59 float scale) override { | 76 float scale) override { |
60 sequence_ = worker_pool_->PlaybackAndScheduleCopyOnWorkerThread( | 77 sequence_ = worker_pool_->PlaybackAndScheduleCopyOnWorkerThread( |
61 lock_.Pass(), raster_resource_.Pass(), resource_, raster_source, rect, | 78 lock_.Pass(), raster_resource_.Pass(), previous_frame_lock_.Pass(), |
62 scale); | 79 previous_frame_raster_resource_.Pass(), tile_task_data_, raster_source, |
| 80 rect, scale); |
63 } | 81 } |
64 | 82 |
65 private: | 83 private: |
66 OneCopyTileTaskWorkerPool* worker_pool_; | 84 OneCopyTileTaskWorkerPool* worker_pool_; |
67 ResourceProvider* resource_provider_; | 85 ResourceProvider* resource_provider_; |
68 ResourcePool* resource_pool_; | 86 ResourcePool* resource_pool_; |
69 const Resource* resource_; | 87 TileTaskData tile_task_data_; |
70 scoped_ptr<ScopedResource> raster_resource_; | 88 scoped_ptr<ScopedResource> raster_resource_; |
| 89 scoped_ptr<ScopedResource> previous_frame_raster_resource_; |
71 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer> lock_; | 90 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer> lock_; |
| 91 scoped_ptr<ResourceProvider::ScopedReadLockGpuMemoryBuffer> |
| 92 previous_frame_lock_; |
72 CopySequenceNumber sequence_; | 93 CopySequenceNumber sequence_; |
73 | 94 |
74 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl); | 95 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl); |
75 }; | 96 }; |
76 | 97 |
77 // Flush interval when performing copy operations. | 98 // Flush interval when performing copy operations. |
78 const int kCopyFlushPeriod = 4; | 99 const int kCopyFlushPeriod = 4; |
79 | 100 |
80 // Number of in-flight copy operations to allow. | 101 // Number of in-flight copy operations to allow. |
81 const int kMaxCopyOperations = 32; | 102 const int kMaxCopyOperations = 32; |
82 | 103 |
83 // Delay been checking for copy operations to complete. | 104 // Delay been checking for copy operations to complete. |
84 const int kCheckForCompletedCopyOperationsTickRateMs = 1; | 105 const int kCheckForCompletedCopyOperationsTickRateMs = 1; |
85 | 106 |
86 // Number of failed attempts to allow before we perform a check that will | 107 // Number of failed attempts to allow before we perform a check that will |
87 // wait for copy operations to complete if needed. | 108 // wait for copy operations to complete if needed. |
88 const int kFailedAttemptsBeforeWaitIfNeeded = 256; | 109 const int kFailedAttemptsBeforeWaitIfNeeded = 256; |
89 | 110 |
90 } // namespace | 111 } // namespace |
91 | 112 |
92 OneCopyTileTaskWorkerPool::CopyOperation::CopyOperation( | 113 OneCopyTileTaskWorkerPool::CopyOperation::CopyOperation( |
93 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer> write_lock, | 114 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer> write_lock, |
94 scoped_ptr<ScopedResource> src, | 115 scoped_ptr<ScopedResource> src, |
95 const Resource* dst) | 116 scoped_ptr<ResourceProvider::ScopedReadLockGpuMemoryBuffer> |
96 : write_lock(write_lock.Pass()), src(src.Pass()), dst(dst) { | 117 previous_frame_read_lock, |
| 118 scoped_ptr<ScopedResource> previous_frame_src, |
| 119 const TileTaskData& data) |
| 120 : write_lock(write_lock.Pass()), |
| 121 src(src.Pass()), |
| 122 previous_frame_read_lock(previous_frame_read_lock.Pass()), |
| 123 previous_frame_src(previous_frame_src.Pass()), |
| 124 tile_task_data(data) { |
97 } | 125 } |
98 | 126 |
99 OneCopyTileTaskWorkerPool::CopyOperation::~CopyOperation() { | 127 OneCopyTileTaskWorkerPool::CopyOperation::~CopyOperation() { |
100 } | 128 } |
101 | 129 |
102 // static | 130 // static |
103 scoped_ptr<TileTaskWorkerPool> OneCopyTileTaskWorkerPool::Create( | 131 scoped_ptr<TileTaskWorkerPool> OneCopyTileTaskWorkerPool::Create( |
104 base::SequencedTaskRunner* task_runner, | 132 base::SequencedTaskRunner* task_runner, |
105 TaskGraphRunner* task_graph_runner, | 133 TaskGraphRunner* task_graph_runner, |
106 ContextProvider* context_provider, | 134 ContextProvider* context_provider, |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 task->RunReplyOnOriginThread(); | 283 task->RunReplyOnOriginThread(); |
256 } | 284 } |
257 completed_tasks_.clear(); | 285 completed_tasks_.clear(); |
258 } | 286 } |
259 | 287 |
260 ResourceFormat OneCopyTileTaskWorkerPool::GetResourceFormat() { | 288 ResourceFormat OneCopyTileTaskWorkerPool::GetResourceFormat() { |
261 return resource_provider_->best_texture_format(); | 289 return resource_provider_->best_texture_format(); |
262 } | 290 } |
263 | 291 |
264 scoped_ptr<RasterBuffer> OneCopyTileTaskWorkerPool::AcquireBufferForRaster( | 292 scoped_ptr<RasterBuffer> OneCopyTileTaskWorkerPool::AcquireBufferForRaster( |
265 const Resource* resource) { | 293 const TileTaskData& data) { |
266 DCHECK_EQ(resource->format(), resource_provider_->best_texture_format()); | 294 DCHECK_EQ(data.resource->format(), resource_provider_->best_texture_format()); |
267 return make_scoped_ptr<RasterBuffer>( | 295 return make_scoped_ptr<RasterBuffer>( |
268 new RasterBufferImpl(this, resource_provider_, resource_pool_, | 296 new RasterBufferImpl(this, resource_provider_, resource_pool_, |
269 resource_provider_->best_texture_format(), | 297 resource_provider_->best_texture_format(), data)); |
270 resource)); | |
271 } | 298 } |
272 | 299 |
273 void OneCopyTileTaskWorkerPool::ReleaseBufferForRaster( | 300 void OneCopyTileTaskWorkerPool::ReleaseBufferForRaster( |
274 scoped_ptr<RasterBuffer> buffer) { | 301 scoped_ptr<RasterBuffer> buffer) { |
275 // Nothing to do here. RasterBufferImpl destructor cleans up after itself. | 302 // Nothing to do here. RasterBufferImpl destructor cleans up after itself. |
276 } | 303 } |
277 | 304 |
278 CopySequenceNumber | 305 CopySequenceNumber |
279 OneCopyTileTaskWorkerPool::PlaybackAndScheduleCopyOnWorkerThread( | 306 OneCopyTileTaskWorkerPool::PlaybackAndScheduleCopyOnWorkerThread( |
280 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer> write_lock, | 307 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer> write_lock, |
281 scoped_ptr<ScopedResource> src, | 308 scoped_ptr<ScopedResource> src, |
282 const Resource* dst, | 309 scoped_ptr<ResourceProvider::ScopedReadLockGpuMemoryBuffer> |
| 310 previous_frame_read_lock, |
| 311 scoped_ptr<ScopedResource> previous_frame_src, |
| 312 const TileTaskData& tile_task_data, |
283 const RasterSource* raster_source, | 313 const RasterSource* raster_source, |
284 const gfx::Rect& rect, | 314 const gfx::Rect& rect, |
285 float scale) { | 315 float scale) { |
286 base::AutoLock lock(lock_); | 316 base::AutoLock lock(lock_); |
287 | 317 |
288 int failed_attempts = 0; | 318 int failed_attempts = 0; |
289 while ((scheduled_copy_operation_count_ + issued_copy_operation_count_) >= | 319 while ((scheduled_copy_operation_count_ + issued_copy_operation_count_) >= |
290 kMaxCopyOperations) { | 320 kMaxCopyOperations) { |
291 // Ignore limit when shutdown is set. | 321 // Ignore limit when shutdown is set. |
292 if (shutdown_) | 322 if (shutdown_) |
(...skipping 26 matching lines...) Expand all Loading... |
319 { | 349 { |
320 base::AutoUnlock unlock(lock_); | 350 base::AutoUnlock unlock(lock_); |
321 | 351 |
322 gfx::GpuMemoryBuffer* gpu_memory_buffer = write_lock->GetGpuMemoryBuffer(); | 352 gfx::GpuMemoryBuffer* gpu_memory_buffer = write_lock->GetGpuMemoryBuffer(); |
323 if (gpu_memory_buffer) { | 353 if (gpu_memory_buffer) { |
324 void* data = NULL; | 354 void* data = NULL; |
325 bool rv = gpu_memory_buffer->Map(&data); | 355 bool rv = gpu_memory_buffer->Map(&data); |
326 DCHECK(rv); | 356 DCHECK(rv); |
327 int stride; | 357 int stride; |
328 gpu_memory_buffer->GetStride(&stride); | 358 gpu_memory_buffer->GetStride(&stride); |
| 359 |
| 360 // Copy from the previous_frame_src to src to reuse raster work if |
| 361 // possible. |
| 362 bool copied_from_previous = false; |
| 363 if (previous_frame_src) { |
| 364 gfx::GpuMemoryBuffer* previous_gpu_memory_buffer = |
| 365 previous_frame_read_lock->GetGpuMemoryBuffer(); |
| 366 if (previous_gpu_memory_buffer) { |
| 367 void* previous_data = NULL; |
| 368 bool prv = previous_gpu_memory_buffer->Map(&previous_data); |
| 369 DCHECK(prv); |
| 370 int previous_stride; |
| 371 previous_gpu_memory_buffer->GetStride(&previous_stride); |
| 372 |
| 373 if (previous_stride == stride) { |
| 374 size_t bytes = static_cast<size_t>(stride) * src->size().height(); |
| 375 memcpy(data, previous_data, bytes); |
| 376 copied_from_previous = true; |
| 377 } |
| 378 previous_gpu_memory_buffer->Unmap(); |
| 379 } |
| 380 } |
| 381 |
| 382 gfx::Rect playback_rect = rect; |
| 383 if (copied_from_previous) { |
| 384 LOG(ERROR) << tile_task_data.raster_dirty_rect.ToString(); |
| 385 playback_rect.Intersect(tile_task_data.raster_dirty_rect); |
| 386 } |
| 387 DCHECK(!playback_rect.IsEmpty()) |
| 388 << "Why are we rastering a tile that's not dirty?"; |
329 TileTaskWorkerPool::PlaybackToMemory(data, src->format(), src->size(), | 389 TileTaskWorkerPool::PlaybackToMemory(data, src->format(), src->size(), |
330 stride, raster_source, rect, scale); | 390 stride, raster_source, rect, |
| 391 playback_rect, scale); |
331 gpu_memory_buffer->Unmap(); | 392 gpu_memory_buffer->Unmap(); |
332 } | 393 } |
333 } | 394 } |
334 | 395 |
335 pending_copy_operations_.push_back( | 396 pending_copy_operations_.push_back(make_scoped_ptr(new CopyOperation( |
336 make_scoped_ptr(new CopyOperation(write_lock.Pass(), src.Pass(), dst))); | 397 write_lock.Pass(), src.Pass(), previous_frame_read_lock.Pass(), |
| 398 previous_frame_src.Pass(), tile_task_data))); |
337 | 399 |
338 // Acquire a sequence number for this copy operation. | 400 // Acquire a sequence number for this copy operation. |
339 CopySequenceNumber sequence = next_copy_operation_sequence_++; | 401 CopySequenceNumber sequence = next_copy_operation_sequence_++; |
340 | 402 |
341 // Post task that will advance last flushed copy operation to |sequence| | 403 // Post task that will advance last flushed copy operation to |sequence| |
342 // if we have reached the flush period. | 404 // if we have reached the flush period. |
343 if ((sequence % kCopyFlushPeriod) == 0) { | 405 if ((sequence % kCopyFlushPeriod) == 0) { |
344 task_runner_->PostTask( | 406 task_runner_->PostTask( |
345 FROM_HERE, | 407 FROM_HERE, |
346 base::Bind(&OneCopyTileTaskWorkerPool::AdvanceLastFlushedCopyTo, | 408 base::Bind(&OneCopyTileTaskWorkerPool::AdvanceLastFlushedCopyTo, |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 issued_copy_operation_count_ += copy_operations.size(); | 470 issued_copy_operation_count_ += copy_operations.size(); |
409 } | 471 } |
410 | 472 |
411 while (!copy_operations.empty()) { | 473 while (!copy_operations.empty()) { |
412 scoped_ptr<CopyOperation> copy_operation = copy_operations.take_front(); | 474 scoped_ptr<CopyOperation> copy_operation = copy_operations.take_front(); |
413 | 475 |
414 // Remove the write lock. | 476 // Remove the write lock. |
415 copy_operation->write_lock.reset(); | 477 copy_operation->write_lock.reset(); |
416 | 478 |
417 // Copy contents of source resource to destination resource. | 479 // Copy contents of source resource to destination resource. |
418 resource_provider_->CopyResource(copy_operation->src->id(), | 480 resource_provider_->CopyResource( |
419 copy_operation->dst->id()); | 481 copy_operation->src->id(), |
| 482 copy_operation->tile_task_data.resource->id()); |
420 | 483 |
421 // Return source resource to pool where it can be reused once copy | 484 // Return source resource to pool where it can be reused once copy |
422 // operation has completed and resource is no longer busy. | 485 // operation has completed and resource is no longer busy. |
423 resource_pool_->ReleaseResource(copy_operation->src.Pass()); | 486 resource_pool_->ReleaseResource(copy_operation->src.Pass(), |
| 487 copy_operation->tile_task_data.new_tile_id); |
| 488 if (copy_operation->previous_frame_src) { |
| 489 resource_pool_->ReleaseResource( |
| 490 copy_operation->previous_frame_src.Pass(), |
| 491 copy_operation->tile_task_data.previous_tile_id); |
| 492 } |
424 } | 493 } |
425 } | 494 } |
426 | 495 |
427 void OneCopyTileTaskWorkerPool:: | 496 void OneCopyTileTaskWorkerPool:: |
428 ScheduleCheckForCompletedCopyOperationsWithLockAcquired( | 497 ScheduleCheckForCompletedCopyOperationsWithLockAcquired( |
429 bool wait_if_needed) { | 498 bool wait_if_needed) { |
430 lock_.AssertAcquired(); | 499 lock_.AssertAcquired(); |
431 | 500 |
432 if (check_for_completed_copy_operations_pending_) | 501 if (check_for_completed_copy_operations_pending_) |
433 return; | 502 return; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
502 resource_pool_->total_memory_usage_bytes()); | 571 resource_pool_->total_memory_usage_bytes()); |
503 staging_state->SetInteger("pending_copy_count", | 572 staging_state->SetInteger("pending_copy_count", |
504 resource_pool_->total_resource_count() - | 573 resource_pool_->total_resource_count() - |
505 resource_pool_->acquired_resource_count()); | 574 resource_pool_->acquired_resource_count()); |
506 staging_state->SetInteger("bytes_pending_copy", | 575 staging_state->SetInteger("bytes_pending_copy", |
507 resource_pool_->total_memory_usage_bytes() - | 576 resource_pool_->total_memory_usage_bytes() - |
508 resource_pool_->acquired_memory_usage_bytes()); | 577 resource_pool_->acquired_memory_usage_bytes()); |
509 } | 578 } |
510 | 579 |
511 } // namespace cc | 580 } // namespace cc |
OLD | NEW |