| 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_raster_worker_pool.h" | 5 #include "cc/resources/one_copy_raster_worker_pool.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> |
| 8 | 9 |
| 9 #include "base/debug/trace_event.h" | 10 #include "base/debug/trace_event.h" |
| 10 #include "base/debug/trace_event_argument.h" | 11 #include "base/debug/trace_event_argument.h" |
| 11 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 12 #include "cc/debug/traced_value.h" | 13 #include "cc/debug/traced_value.h" |
| 13 #include "cc/resources/raster_buffer.h" | 14 #include "cc/resources/raster_buffer.h" |
| 14 #include "cc/resources/resource_pool.h" | 15 #include "cc/resources/resource_pool.h" |
| 15 #include "cc/resources/scoped_resource.h" | 16 #include "cc/resources/scoped_resource.h" |
| 16 #include "gpu/command_buffer/client/gles2_interface.h" | 17 #include "gpu/command_buffer/client/gles2_interface.h" |
| 17 #include "ui/gfx/gpu_memory_buffer.h" | 18 #include "ui/gfx/gpu_memory_buffer.h" |
| 18 | 19 |
| 19 namespace cc { | 20 namespace cc { |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 class RasterBufferImpl : public RasterBuffer { | 23 class RasterBufferImpl : public RasterBuffer { |
| 23 public: | 24 public: |
| 24 RasterBufferImpl(ResourceProvider* resource_provider, | 25 RasterBufferImpl(OneCopyRasterWorkerPool* worker_pool, |
| 26 ResourceProvider* resource_provider, |
| 25 ResourcePool* resource_pool, | 27 ResourcePool* resource_pool, |
| 26 const Resource* resource) | 28 const Resource* resource) |
| 27 : resource_provider_(resource_provider), | 29 : worker_pool_(worker_pool), |
| 30 resource_provider_(resource_provider), |
| 28 resource_pool_(resource_pool), | 31 resource_pool_(resource_pool), |
| 29 resource_(resource), | 32 resource_(resource), |
| 30 raster_resource_(resource_pool->AcquireResource(resource->size())), | 33 raster_resource_(resource_pool->AcquireResource(resource->size())), |
| 31 lock_(new ResourceProvider::ScopedWriteLockGpuMemoryBuffer( | 34 lock_(new ResourceProvider::ScopedWriteLockGpuMemoryBuffer( |
| 32 resource_provider_, | 35 resource_provider_, |
| 33 raster_resource_->id())) {} | 36 raster_resource_->id())), |
| 37 sequence_(0) {} |
| 34 | 38 |
| 35 ~RasterBufferImpl() override { | 39 ~RasterBufferImpl() override { |
| 36 // First unlock raster resource. | 40 // Release write lock in case a copy was never scheduled. |
| 37 lock_.reset(); | 41 lock_.reset(); |
| 38 | 42 |
| 39 // Copy contents of raster resource to |resource_|. | 43 // Make sure any scheduled copy operations are issued before we release the |
| 40 resource_provider_->CopyResource(raster_resource_->id(), resource_->id()); | 44 // raster resource. |
| 45 if (sequence_) |
| 46 worker_pool_->AdvanceLastIssuedCopyTo(sequence_); |
| 41 | 47 |
| 42 // Return raster resource to pool so it can be used by another RasterBuffer | 48 // Return raster resource to pool so it can be used by another RasterBuffer |
| 43 // instance. | 49 // instance. |
| 44 resource_pool_->ReleaseResource(raster_resource_.Pass()); | 50 resource_pool_->ReleaseResource(raster_resource_.Pass()); |
| 45 } | 51 } |
| 46 | 52 |
| 47 // Overridden from RasterBuffer: | 53 // Overridden from RasterBuffer: |
| 48 void Playback(const PicturePileImpl* picture_pile, | 54 void Playback(const PicturePileImpl* picture_pile, |
| 49 const gfx::Rect& rect, | 55 const gfx::Rect& rect, |
| 50 float scale, | 56 float scale, |
| 51 RenderingStatsInstrumentation* stats) override { | 57 RenderingStatsInstrumentation* stats) override { |
| 52 gfx::GpuMemoryBuffer* gpu_memory_buffer = lock_->gpu_memory_buffer(); | 58 gfx::GpuMemoryBuffer* gpu_memory_buffer = lock_->gpu_memory_buffer(); |
| 53 if (!gpu_memory_buffer) | 59 if (!gpu_memory_buffer) |
| 54 return; | 60 return; |
| 55 | 61 |
| 56 RasterWorkerPool::PlaybackToMemory(gpu_memory_buffer->Map(), | 62 RasterWorkerPool::PlaybackToMemory(gpu_memory_buffer->Map(), |
| 57 resource_->format(), | 63 raster_resource_->format(), |
| 58 resource_->size(), | 64 raster_resource_->size(), |
| 59 gpu_memory_buffer->GetStride(), | 65 gpu_memory_buffer->GetStride(), |
| 60 picture_pile, | 66 picture_pile, |
| 61 rect, | 67 rect, |
| 62 scale, | 68 scale, |
| 63 stats); | 69 stats); |
| 64 gpu_memory_buffer->Unmap(); | 70 gpu_memory_buffer->Unmap(); |
| 71 |
| 72 sequence_ = worker_pool_->ScheduleCopyOnWorkerThread( |
| 73 lock_.Pass(), raster_resource_.get(), resource_); |
| 65 } | 74 } |
| 66 | 75 |
| 67 private: | 76 private: |
| 77 OneCopyRasterWorkerPool* worker_pool_; |
| 68 ResourceProvider* resource_provider_; | 78 ResourceProvider* resource_provider_; |
| 69 ResourcePool* resource_pool_; | 79 ResourcePool* resource_pool_; |
| 70 const Resource* resource_; | 80 const Resource* resource_; |
| 71 scoped_ptr<ScopedResource> raster_resource_; | 81 scoped_ptr<ScopedResource> raster_resource_; |
| 72 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer> lock_; | 82 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer> lock_; |
| 83 CopySequenceNumber sequence_; |
| 73 | 84 |
| 74 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl); | 85 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl); |
| 75 }; | 86 }; |
| 76 | 87 |
| 88 // Flush interval when performing copy operations. |
| 89 const int kCopyFlushPeriod = 4; |
| 90 |
| 77 } // namespace | 91 } // namespace |
| 78 | 92 |
| 93 OneCopyRasterWorkerPool::CopyOperation::CopyOperation( |
| 94 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer> write_lock, |
| 95 ResourceProvider::ResourceId src, |
| 96 ResourceProvider::ResourceId dst) |
| 97 : write_lock(write_lock.Pass()), src(src), dst(dst) { |
| 98 } |
| 99 |
| 100 OneCopyRasterWorkerPool::CopyOperation::~CopyOperation() { |
| 101 } |
| 102 |
| 79 // static | 103 // static |
| 80 scoped_ptr<RasterWorkerPool> OneCopyRasterWorkerPool::Create( | 104 scoped_ptr<RasterWorkerPool> OneCopyRasterWorkerPool::Create( |
| 81 base::SequencedTaskRunner* task_runner, | 105 base::SequencedTaskRunner* task_runner, |
| 82 TaskGraphRunner* task_graph_runner, | 106 TaskGraphRunner* task_graph_runner, |
| 83 ContextProvider* context_provider, | 107 ContextProvider* context_provider, |
| 84 ResourceProvider* resource_provider, | 108 ResourceProvider* resource_provider, |
| 85 ResourcePool* resource_pool) { | 109 ResourcePool* resource_pool) { |
| 86 return make_scoped_ptr<RasterWorkerPool>( | 110 return make_scoped_ptr<RasterWorkerPool>( |
| 87 new OneCopyRasterWorkerPool(task_runner, | 111 new OneCopyRasterWorkerPool(task_runner, |
| 88 task_graph_runner, | 112 task_graph_runner, |
| 89 context_provider, | 113 context_provider, |
| 90 resource_provider, | 114 resource_provider, |
| 91 resource_pool)); | 115 resource_pool)); |
| 92 } | 116 } |
| 93 | 117 |
| 94 OneCopyRasterWorkerPool::OneCopyRasterWorkerPool( | 118 OneCopyRasterWorkerPool::OneCopyRasterWorkerPool( |
| 95 base::SequencedTaskRunner* task_runner, | 119 base::SequencedTaskRunner* task_runner, |
| 96 TaskGraphRunner* task_graph_runner, | 120 TaskGraphRunner* task_graph_runner, |
| 97 ContextProvider* context_provider, | 121 ContextProvider* context_provider, |
| 98 ResourceProvider* resource_provider, | 122 ResourceProvider* resource_provider, |
| 99 ResourcePool* resource_pool) | 123 ResourcePool* resource_pool) |
| 100 : task_runner_(task_runner), | 124 : task_runner_(task_runner), |
| 101 task_graph_runner_(task_graph_runner), | 125 task_graph_runner_(task_graph_runner), |
| 102 namespace_token_(task_graph_runner->GetNamespaceToken()), | 126 namespace_token_(task_graph_runner->GetNamespaceToken()), |
| 103 context_provider_(context_provider), | 127 context_provider_(context_provider), |
| 104 resource_provider_(resource_provider), | 128 resource_provider_(resource_provider), |
| 105 resource_pool_(resource_pool), | 129 resource_pool_(resource_pool), |
| 130 last_issued_copy_operation_(0), |
| 131 last_flushed_copy_operation_(0), |
| 132 next_copy_operation_sequence_(1), |
| 133 weak_ptr_factory_(this), |
| 106 raster_finished_weak_ptr_factory_(this) { | 134 raster_finished_weak_ptr_factory_(this) { |
| 107 DCHECK(context_provider_); | 135 DCHECK(context_provider_); |
| 108 } | 136 } |
| 109 | 137 |
| 110 OneCopyRasterWorkerPool::~OneCopyRasterWorkerPool() { | 138 OneCopyRasterWorkerPool::~OneCopyRasterWorkerPool() { |
| 111 } | 139 } |
| 112 | 140 |
| 113 Rasterizer* OneCopyRasterWorkerPool::AsRasterizer() { | 141 Rasterizer* OneCopyRasterWorkerPool::AsRasterizer() { |
| 114 return this; | 142 return this; |
| 115 } | 143 } |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 | 222 |
| 195 TRACE_EVENT_ASYNC_STEP_INTO1( | 223 TRACE_EVENT_ASYNC_STEP_INTO1( |
| 196 "cc", "ScheduledTasks", this, "rasterizing", "state", StateAsValue()); | 224 "cc", "ScheduledTasks", this, "rasterizing", "state", StateAsValue()); |
| 197 } | 225 } |
| 198 | 226 |
| 199 void OneCopyRasterWorkerPool::CheckForCompletedTasks() { | 227 void OneCopyRasterWorkerPool::CheckForCompletedTasks() { |
| 200 TRACE_EVENT0("cc", "OneCopyRasterWorkerPool::CheckForCompletedTasks"); | 228 TRACE_EVENT0("cc", "OneCopyRasterWorkerPool::CheckForCompletedTasks"); |
| 201 | 229 |
| 202 task_graph_runner_->CollectCompletedTasks(namespace_token_, | 230 task_graph_runner_->CollectCompletedTasks(namespace_token_, |
| 203 &completed_tasks_); | 231 &completed_tasks_); |
| 232 |
| 204 for (Task::Vector::const_iterator it = completed_tasks_.begin(); | 233 for (Task::Vector::const_iterator it = completed_tasks_.begin(); |
| 205 it != completed_tasks_.end(); | 234 it != completed_tasks_.end(); |
| 206 ++it) { | 235 ++it) { |
| 207 RasterizerTask* task = static_cast<RasterizerTask*>(it->get()); | 236 RasterizerTask* task = static_cast<RasterizerTask*>(it->get()); |
| 208 | 237 |
| 209 task->WillComplete(); | 238 task->WillComplete(); |
| 210 task->CompleteOnOriginThread(this); | 239 task->CompleteOnOriginThread(this); |
| 211 task->DidComplete(); | 240 task->DidComplete(); |
| 212 | 241 |
| 213 task->RunReplyOnOriginThread(); | 242 task->RunReplyOnOriginThread(); |
| 214 } | 243 } |
| 215 completed_tasks_.clear(); | 244 completed_tasks_.clear(); |
| 216 | |
| 217 context_provider_->ContextGL()->ShallowFlushCHROMIUM(); | |
| 218 } | 245 } |
| 219 | 246 |
| 220 scoped_ptr<RasterBuffer> OneCopyRasterWorkerPool::AcquireBufferForRaster( | 247 scoped_ptr<RasterBuffer> OneCopyRasterWorkerPool::AcquireBufferForRaster( |
| 221 const Resource* resource) { | 248 const Resource* resource) { |
| 222 DCHECK_EQ(resource->format(), resource_pool_->resource_format()); | 249 DCHECK_EQ(resource->format(), resource_pool_->resource_format()); |
| 223 return make_scoped_ptr<RasterBuffer>( | 250 return make_scoped_ptr<RasterBuffer>( |
| 224 new RasterBufferImpl(resource_provider_, resource_pool_, resource)); | 251 new RasterBufferImpl(this, resource_provider_, resource_pool_, resource)); |
| 225 } | 252 } |
| 226 | 253 |
| 227 void OneCopyRasterWorkerPool::ReleaseBufferForRaster( | 254 void OneCopyRasterWorkerPool::ReleaseBufferForRaster( |
| 228 scoped_ptr<RasterBuffer> buffer) { | 255 scoped_ptr<RasterBuffer> buffer) { |
| 229 // Nothing to do here. RasterBufferImpl destructor cleans up after itself. | 256 // Nothing to do here. RasterBufferImpl destructor cleans up after itself. |
| 230 } | 257 } |
| 231 | 258 |
| 259 CopySequenceNumber OneCopyRasterWorkerPool::ScheduleCopyOnWorkerThread( |
| 260 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer> write_lock, |
| 261 const Resource* src, |
| 262 const Resource* dst) { |
| 263 CopySequenceNumber sequence; |
| 264 |
| 265 { |
| 266 base::AutoLock lock(lock_); |
| 267 |
| 268 sequence = next_copy_operation_sequence_++; |
| 269 |
| 270 pending_copy_operations_.push_back(make_scoped_ptr( |
| 271 new CopyOperation(write_lock.Pass(), src->id(), dst->id()))); |
| 272 } |
| 273 |
| 274 // Post task that will advance last flushed copy operation to |sequence| |
| 275 // if we have reached the flush period. |
| 276 if ((sequence % kCopyFlushPeriod) == 0) { |
| 277 task_runner_->PostTask( |
| 278 FROM_HERE, |
| 279 base::Bind(&OneCopyRasterWorkerPool::AdvanceLastFlushedCopyTo, |
| 280 weak_ptr_factory_.GetWeakPtr(), |
| 281 sequence)); |
| 282 } |
| 283 |
| 284 return sequence; |
| 285 } |
| 286 |
| 287 void OneCopyRasterWorkerPool::AdvanceLastIssuedCopyTo( |
| 288 CopySequenceNumber sequence) { |
| 289 if (last_issued_copy_operation_ >= sequence) |
| 290 return; |
| 291 |
| 292 IssueCopyOperations(sequence - last_issued_copy_operation_); |
| 293 last_issued_copy_operation_ = sequence; |
| 294 } |
| 295 |
| 296 void OneCopyRasterWorkerPool::AdvanceLastFlushedCopyTo( |
| 297 CopySequenceNumber sequence) { |
| 298 if (last_flushed_copy_operation_ >= sequence) |
| 299 return; |
| 300 |
| 301 AdvanceLastIssuedCopyTo(sequence); |
| 302 |
| 303 // Flush all issued copy operations. |
| 304 context_provider_->ContextGL()->ShallowFlushCHROMIUM(); |
| 305 last_flushed_copy_operation_ = last_issued_copy_operation_; |
| 306 } |
| 307 |
| 232 void OneCopyRasterWorkerPool::OnRasterFinished(TaskSet task_set) { | 308 void OneCopyRasterWorkerPool::OnRasterFinished(TaskSet task_set) { |
| 233 TRACE_EVENT1( | 309 TRACE_EVENT1( |
| 234 "cc", "OneCopyRasterWorkerPool::OnRasterFinished", "task_set", task_set); | 310 "cc", "OneCopyRasterWorkerPool::OnRasterFinished", "task_set", task_set); |
| 235 | 311 |
| 236 DCHECK(raster_pending_[task_set]); | 312 DCHECK(raster_pending_[task_set]); |
| 237 raster_pending_[task_set] = false; | 313 raster_pending_[task_set] = false; |
| 238 if (raster_pending_.any()) { | 314 if (raster_pending_.any()) { |
| 239 TRACE_EVENT_ASYNC_STEP_INTO1( | 315 TRACE_EVENT_ASYNC_STEP_INTO1( |
| 240 "cc", "ScheduledTasks", this, "rasterizing", "state", StateAsValue()); | 316 "cc", "ScheduledTasks", this, "rasterizing", "state", StateAsValue()); |
| 241 } else { | 317 } else { |
| 242 TRACE_EVENT_ASYNC_END0("cc", "ScheduledTasks", this); | 318 TRACE_EVENT_ASYNC_END0("cc", "ScheduledTasks", this); |
| 243 } | 319 } |
| 244 client_->DidFinishRunningTasks(task_set); | 320 client_->DidFinishRunningTasks(task_set); |
| 245 } | 321 } |
| 246 | 322 |
| 323 void OneCopyRasterWorkerPool::IssueCopyOperations(int64 count) { |
| 324 TRACE_EVENT1( |
| 325 "cc", "OneCopyRasterWorkerPool::IssueCopyOperations", "count", count); |
| 326 |
| 327 CopyOperation::Deque copy_operations; |
| 328 |
| 329 { |
| 330 base::AutoLock lock(lock_); |
| 331 |
| 332 for (int64 i = 0; i < count; ++i) { |
| 333 DCHECK(!pending_copy_operations_.empty()); |
| 334 copy_operations.push_back(pending_copy_operations_.take_front()); |
| 335 } |
| 336 } |
| 337 |
| 338 while (!copy_operations.empty()) { |
| 339 scoped_ptr<CopyOperation> copy_operation = copy_operations.take_front(); |
| 340 |
| 341 // Remove the write lock. |
| 342 copy_operation->write_lock.reset(); |
| 343 |
| 344 // Copy contents of source resource to destination resource. |
| 345 resource_provider_->CopyResource(copy_operation->src, copy_operation->dst); |
| 346 } |
| 347 } |
| 348 |
| 247 scoped_refptr<base::debug::ConvertableToTraceFormat> | 349 scoped_refptr<base::debug::ConvertableToTraceFormat> |
| 248 OneCopyRasterWorkerPool::StateAsValue() const { | 350 OneCopyRasterWorkerPool::StateAsValue() const { |
| 249 scoped_refptr<base::debug::TracedValue> state = | 351 scoped_refptr<base::debug::TracedValue> state = |
| 250 new base::debug::TracedValue(); | 352 new base::debug::TracedValue(); |
| 251 | 353 |
| 252 state->BeginArray("tasks_pending"); | 354 state->BeginArray("tasks_pending"); |
| 253 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) | 355 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) |
| 254 state->AppendBoolean(raster_pending_[task_set]); | 356 state->AppendBoolean(raster_pending_[task_set]); |
| 255 state->EndArray(); | 357 state->EndArray(); |
| 256 state->BeginDictionary("staging_state"); | 358 state->BeginDictionary("staging_state"); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 267 resource_pool_->total_memory_usage_bytes()); | 369 resource_pool_->total_memory_usage_bytes()); |
| 268 staging_state->SetInteger("pending_copy_count", | 370 staging_state->SetInteger("pending_copy_count", |
| 269 resource_pool_->total_resource_count() - | 371 resource_pool_->total_resource_count() - |
| 270 resource_pool_->acquired_resource_count()); | 372 resource_pool_->acquired_resource_count()); |
| 271 staging_state->SetInteger("bytes_pending_copy", | 373 staging_state->SetInteger("bytes_pending_copy", |
| 272 resource_pool_->total_memory_usage_bytes() - | 374 resource_pool_->total_memory_usage_bytes() - |
| 273 resource_pool_->acquired_memory_usage_bytes()); | 375 resource_pool_->acquired_memory_usage_bytes()); |
| 274 } | 376 } |
| 275 | 377 |
| 276 } // namespace cc | 378 } // namespace cc |
| OLD | NEW |