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 reusing_raster_resource_(true), |
35 resource_pool->AcquireResource(resource->size(), resource_format)), | 35 sequence_(0) { |
36 lock_(new ResourceProvider::ScopedWriteLockGpuMemoryBuffer( | 36 raster_resource_ = resource_pool->TryAcquireOldResource( |
37 resource_provider_, | 37 data.resource->size(), resource_format, data.previous_tile_id); |
38 raster_resource_->id())), | 38 if (!raster_resource_) { |
39 sequence_(0) {} | 39 raster_resource_ = resource_pool->AcquireResource(data.resource->size(), |
| 40 resource_format); |
| 41 reusing_raster_resource_ = false; |
| 42 } |
| 43 |
| 44 lock_.reset(new ResourceProvider::ScopedWriteLockGpuMemoryBuffer( |
| 45 resource_provider_, raster_resource_->id())); |
| 46 } |
40 | 47 |
41 ~RasterBufferImpl() override { | 48 ~RasterBufferImpl() override { |
42 // Release write lock in case a copy was never scheduled. | 49 // Release write lock in case a copy was never scheduled. |
43 lock_.reset(); | 50 lock_.reset(); |
44 | 51 |
45 // Make sure any scheduled copy operations are issued before we release the | 52 // Make sure any scheduled copy operations are issued before we release the |
46 // raster resource. | 53 // raster resource. |
47 if (sequence_) | 54 if (sequence_) |
48 worker_pool_->AdvanceLastIssuedCopyTo(sequence_); | 55 worker_pool_->AdvanceLastIssuedCopyTo(sequence_); |
49 | 56 |
50 // Return raster resource to pool so it can be used by another RasterBuffer | 57 // Return resources to pool so they can be used by another RasterBuffer |
51 // instance. | 58 // instance. |
52 if (raster_resource_) | 59 if (raster_resource_) { |
53 resource_pool_->ReleaseResource(raster_resource_.Pass()); | 60 resource_pool_->ReleaseResource(raster_resource_.Pass(), |
| 61 tile_task_data_.new_tile_id); |
| 62 } |
54 } | 63 } |
55 | 64 |
56 // Overridden from RasterBuffer: | 65 // Overridden from RasterBuffer: |
57 void Playback(const RasterSource* raster_source, | 66 void Playback(const RasterSource* raster_source, |
58 const gfx::Rect& rect, | 67 const gfx::Rect& rect, |
59 float scale) override { | 68 float scale) override { |
60 sequence_ = worker_pool_->PlaybackAndScheduleCopyOnWorkerThread( | 69 sequence_ = worker_pool_->PlaybackAndScheduleCopyOnWorkerThread( |
61 lock_.Pass(), raster_resource_.Pass(), resource_, raster_source, rect, | 70 reusing_raster_resource_, lock_.Pass(), raster_resource_.Pass(), |
62 scale); | 71 tile_task_data_, raster_source, rect, scale); |
63 } | 72 } |
64 | 73 |
65 private: | 74 private: |
66 OneCopyTileTaskWorkerPool* worker_pool_; | 75 OneCopyTileTaskWorkerPool* worker_pool_; |
67 ResourceProvider* resource_provider_; | 76 ResourceProvider* resource_provider_; |
68 ResourcePool* resource_pool_; | 77 ResourcePool* resource_pool_; |
69 const Resource* resource_; | 78 TileTaskData tile_task_data_; |
| 79 bool reusing_raster_resource_; |
70 scoped_ptr<ScopedResource> raster_resource_; | 80 scoped_ptr<ScopedResource> raster_resource_; |
71 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer> lock_; | 81 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer> lock_; |
72 CopySequenceNumber sequence_; | 82 CopySequenceNumber sequence_; |
73 | 83 |
74 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl); | 84 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl); |
75 }; | 85 }; |
76 | 86 |
77 // Flush interval when performing copy operations. | 87 // Flush interval when performing copy operations. |
78 const int kCopyFlushPeriod = 4; | 88 const int kCopyFlushPeriod = 4; |
79 | 89 |
80 // Number of in-flight copy operations to allow. | 90 // Number of in-flight copy operations to allow. |
81 const int kMaxCopyOperations = 32; | 91 const int kMaxCopyOperations = 32; |
82 | 92 |
83 // Delay been checking for copy operations to complete. | 93 // Delay been checking for copy operations to complete. |
84 const int kCheckForCompletedCopyOperationsTickRateMs = 1; | 94 const int kCheckForCompletedCopyOperationsTickRateMs = 1; |
85 | 95 |
86 // Number of failed attempts to allow before we perform a check that will | 96 // Number of failed attempts to allow before we perform a check that will |
87 // wait for copy operations to complete if needed. | 97 // wait for copy operations to complete if needed. |
88 const int kFailedAttemptsBeforeWaitIfNeeded = 256; | 98 const int kFailedAttemptsBeforeWaitIfNeeded = 256; |
89 | 99 |
90 } // namespace | 100 } // namespace |
91 | 101 |
92 OneCopyTileTaskWorkerPool::CopyOperation::CopyOperation( | 102 OneCopyTileTaskWorkerPool::CopyOperation::CopyOperation( |
93 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer> write_lock, | 103 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer> |
94 scoped_ptr<ScopedResource> src, | 104 raster_resource_write_lock, |
95 const Resource* dst) | 105 scoped_ptr<ScopedResource> raster_resource, |
96 : write_lock(write_lock.Pass()), src(src.Pass()), dst(dst) { | 106 const TileTaskData& data) |
| 107 : raster_resource_write_lock(raster_resource_write_lock.Pass()), |
| 108 raster_resource(raster_resource.Pass()), |
| 109 tile_task_data(data) { |
97 } | 110 } |
98 | 111 |
99 OneCopyTileTaskWorkerPool::CopyOperation::~CopyOperation() { | 112 OneCopyTileTaskWorkerPool::CopyOperation::~CopyOperation() { |
100 } | 113 } |
101 | 114 |
102 // static | 115 // static |
103 scoped_ptr<TileTaskWorkerPool> OneCopyTileTaskWorkerPool::Create( | 116 scoped_ptr<TileTaskWorkerPool> OneCopyTileTaskWorkerPool::Create( |
104 base::SequencedTaskRunner* task_runner, | 117 base::SequencedTaskRunner* task_runner, |
105 TaskGraphRunner* task_graph_runner, | 118 TaskGraphRunner* task_graph_runner, |
106 ContextProvider* context_provider, | 119 ContextProvider* context_provider, |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 task->RunReplyOnOriginThread(); | 268 task->RunReplyOnOriginThread(); |
256 } | 269 } |
257 completed_tasks_.clear(); | 270 completed_tasks_.clear(); |
258 } | 271 } |
259 | 272 |
260 ResourceFormat OneCopyTileTaskWorkerPool::GetResourceFormat() { | 273 ResourceFormat OneCopyTileTaskWorkerPool::GetResourceFormat() { |
261 return resource_provider_->best_texture_format(); | 274 return resource_provider_->best_texture_format(); |
262 } | 275 } |
263 | 276 |
264 scoped_ptr<RasterBuffer> OneCopyTileTaskWorkerPool::AcquireBufferForRaster( | 277 scoped_ptr<RasterBuffer> OneCopyTileTaskWorkerPool::AcquireBufferForRaster( |
265 const Resource* resource) { | 278 const TileTaskData& data) { |
266 DCHECK_EQ(resource->format(), resource_provider_->best_texture_format()); | 279 DCHECK_EQ(data.resource->format(), resource_provider_->best_texture_format()); |
267 return make_scoped_ptr<RasterBuffer>( | 280 return make_scoped_ptr<RasterBuffer>( |
268 new RasterBufferImpl(this, resource_provider_, resource_pool_, | 281 new RasterBufferImpl(this, resource_provider_, resource_pool_, |
269 resource_provider_->best_texture_format(), | 282 resource_provider_->best_texture_format(), data)); |
270 resource)); | |
271 } | 283 } |
272 | 284 |
273 void OneCopyTileTaskWorkerPool::ReleaseBufferForRaster( | 285 void OneCopyTileTaskWorkerPool::ReleaseBufferForRaster( |
274 scoped_ptr<RasterBuffer> buffer) { | 286 scoped_ptr<RasterBuffer> buffer) { |
275 // Nothing to do here. RasterBufferImpl destructor cleans up after itself. | 287 // Nothing to do here. RasterBufferImpl destructor cleans up after itself. |
276 } | 288 } |
277 | 289 |
278 CopySequenceNumber | 290 CopySequenceNumber |
279 OneCopyTileTaskWorkerPool::PlaybackAndScheduleCopyOnWorkerThread( | 291 OneCopyTileTaskWorkerPool::PlaybackAndScheduleCopyOnWorkerThread( |
280 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer> write_lock, | 292 bool reusing_raster_resource, |
281 scoped_ptr<ScopedResource> src, | 293 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer> |
282 const Resource* dst, | 294 raster_resource_write_lock, |
| 295 scoped_ptr<ScopedResource> raster_resource, |
| 296 const TileTaskData& tile_task_data, |
283 const RasterSource* raster_source, | 297 const RasterSource* raster_source, |
284 const gfx::Rect& rect, | 298 const gfx::Rect& rect, |
285 float scale) { | 299 float scale) { |
286 base::AutoLock lock(lock_); | 300 base::AutoLock lock(lock_); |
287 | 301 |
288 int failed_attempts = 0; | 302 int failed_attempts = 0; |
289 while ((scheduled_copy_operation_count_ + issued_copy_operation_count_) >= | 303 while ((scheduled_copy_operation_count_ + issued_copy_operation_count_) >= |
290 kMaxCopyOperations) { | 304 kMaxCopyOperations) { |
291 // Ignore limit when shutdown is set. | 305 // Ignore limit when shutdown is set. |
292 if (shutdown_) | 306 if (shutdown_) |
(...skipping 19 matching lines...) Expand all Loading... |
312 | 326 |
313 // Increment |scheduled_copy_operation_count_| before releasing |lock_|. | 327 // Increment |scheduled_copy_operation_count_| before releasing |lock_|. |
314 ++scheduled_copy_operation_count_; | 328 ++scheduled_copy_operation_count_; |
315 | 329 |
316 // There may be more work available, so wake up another worker thread. | 330 // There may be more work available, so wake up another worker thread. |
317 copy_operation_count_cv_.Signal(); | 331 copy_operation_count_cv_.Signal(); |
318 | 332 |
319 { | 333 { |
320 base::AutoUnlock unlock(lock_); | 334 base::AutoUnlock unlock(lock_); |
321 | 335 |
322 gfx::GpuMemoryBuffer* gpu_memory_buffer = write_lock->GetGpuMemoryBuffer(); | 336 gfx::GpuMemoryBuffer* gpu_memory_buffer = |
| 337 raster_resource_write_lock->GetGpuMemoryBuffer(); |
323 if (gpu_memory_buffer) { | 338 if (gpu_memory_buffer) { |
324 void* data = NULL; | 339 void* raster_data = nullptr; |
325 bool rv = gpu_memory_buffer->Map(&data); | 340 bool rv = gpu_memory_buffer->Map(&raster_data); |
326 DCHECK(rv); | 341 DCHECK(rv); |
327 int stride; | 342 int stride; |
328 gpu_memory_buffer->GetStride(&stride); | 343 gpu_memory_buffer->GetStride(&stride); |
329 TileTaskWorkerPool::PlaybackToMemory(data, src->format(), src->size(), | 344 |
330 stride, raster_source, rect, scale); | 345 gfx::Rect playback_rect = rect; |
| 346 if (reusing_raster_resource) |
| 347 playback_rect.Intersect(tile_task_data.raster_dirty_rect); |
| 348 DCHECK(!playback_rect.IsEmpty()) |
| 349 << "Why are we rastering a tile that's not dirty?"; |
| 350 TileTaskWorkerPool::PlaybackToMemory( |
| 351 raster_data, raster_resource->format(), raster_resource->size(), |
| 352 stride, raster_source, rect, playback_rect, scale); |
331 gpu_memory_buffer->Unmap(); | 353 gpu_memory_buffer->Unmap(); |
332 } | 354 } |
333 } | 355 } |
334 | 356 |
335 pending_copy_operations_.push_back( | 357 pending_copy_operations_.push_back(make_scoped_ptr( |
336 make_scoped_ptr(new CopyOperation(write_lock.Pass(), src.Pass(), dst))); | 358 new CopyOperation(raster_resource_write_lock.Pass(), |
| 359 raster_resource.Pass(), tile_task_data))); |
337 | 360 |
338 // Acquire a sequence number for this copy operation. | 361 // Acquire a sequence number for this copy operation. |
339 CopySequenceNumber sequence = next_copy_operation_sequence_++; | 362 CopySequenceNumber sequence = next_copy_operation_sequence_++; |
340 | 363 |
341 // Post task that will advance last flushed copy operation to |sequence| | 364 // Post task that will advance last flushed copy operation to |sequence| |
342 // if we have reached the flush period. | 365 // if we have reached the flush period. |
343 if ((sequence % kCopyFlushPeriod) == 0) { | 366 if ((sequence % kCopyFlushPeriod) == 0) { |
344 task_runner_->PostTask( | 367 task_runner_->PostTask( |
345 FROM_HERE, | 368 FROM_HERE, |
346 base::Bind(&OneCopyTileTaskWorkerPool::AdvanceLastFlushedCopyTo, | 369 base::Bind(&OneCopyTileTaskWorkerPool::AdvanceLastFlushedCopyTo, |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 // operations from "pending" to "issued" state. | 428 // operations from "pending" to "issued" state. |
406 DCHECK_GE(scheduled_copy_operation_count_, copy_operations.size()); | 429 DCHECK_GE(scheduled_copy_operation_count_, copy_operations.size()); |
407 scheduled_copy_operation_count_ -= copy_operations.size(); | 430 scheduled_copy_operation_count_ -= copy_operations.size(); |
408 issued_copy_operation_count_ += copy_operations.size(); | 431 issued_copy_operation_count_ += copy_operations.size(); |
409 } | 432 } |
410 | 433 |
411 while (!copy_operations.empty()) { | 434 while (!copy_operations.empty()) { |
412 scoped_ptr<CopyOperation> copy_operation = copy_operations.take_front(); | 435 scoped_ptr<CopyOperation> copy_operation = copy_operations.take_front(); |
413 | 436 |
414 // Remove the write lock. | 437 // Remove the write lock. |
415 copy_operation->write_lock.reset(); | 438 copy_operation->raster_resource_write_lock.reset(); |
416 | 439 |
417 // Copy contents of source resource to destination resource. | 440 // Copy contents of source resource to destination resource. |
418 resource_provider_->CopyResource(copy_operation->src->id(), | 441 resource_provider_->CopyResource( |
419 copy_operation->dst->id()); | 442 copy_operation->raster_resource->id(), |
| 443 copy_operation->tile_task_data.resource->id()); |
420 | 444 |
421 // Return source resource to pool where it can be reused once copy | 445 // Return source resource to pool where it can be reused once copy |
422 // operation has completed and resource is no longer busy. | 446 // operation has completed and resource is no longer busy. |
423 resource_pool_->ReleaseResource(copy_operation->src.Pass()); | 447 resource_pool_->ReleaseResource(copy_operation->raster_resource.Pass(), |
| 448 copy_operation->tile_task_data.new_tile_id); |
424 } | 449 } |
425 } | 450 } |
426 | 451 |
427 void OneCopyTileTaskWorkerPool:: | 452 void OneCopyTileTaskWorkerPool:: |
428 ScheduleCheckForCompletedCopyOperationsWithLockAcquired( | 453 ScheduleCheckForCompletedCopyOperationsWithLockAcquired( |
429 bool wait_if_needed) { | 454 bool wait_if_needed) { |
430 lock_.AssertAcquired(); | 455 lock_.AssertAcquired(); |
431 | 456 |
432 if (check_for_completed_copy_operations_pending_) | 457 if (check_for_completed_copy_operations_pending_) |
433 return; | 458 return; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
502 resource_pool_->total_memory_usage_bytes()); | 527 resource_pool_->total_memory_usage_bytes()); |
503 staging_state->SetInteger("pending_copy_count", | 528 staging_state->SetInteger("pending_copy_count", |
504 resource_pool_->total_resource_count() - | 529 resource_pool_->total_resource_count() - |
505 resource_pool_->acquired_resource_count()); | 530 resource_pool_->acquired_resource_count()); |
506 staging_state->SetInteger("bytes_pending_copy", | 531 staging_state->SetInteger("bytes_pending_copy", |
507 resource_pool_->total_memory_usage_bytes() - | 532 resource_pool_->total_memory_usage_bytes() - |
508 resource_pool_->acquired_memory_usage_bytes()); | 533 resource_pool_->acquired_memory_usage_bytes()); |
509 } | 534 } |
510 | 535 |
511 } // namespace cc | 536 } // namespace cc |
OLD | NEW |