Chromium Code Reviews| 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_->IssueCopy(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 // Number of resources to copy at a time. | |
| 89 const int kBatchSize = 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 base::AutoLock lock(lock_); | |
| 264 | |
| 265 CopySequenceNumber sequence = next_copy_operation_sequence_++; | |
| 266 | |
| 267 pending_copy_operations_.push_back(make_scoped_ptr( | |
| 268 new CopyOperation(write_lock.Pass(), src->id(), dst->id()))); | |
| 269 | |
| 270 // Post task that will flush a batch of copy operations whenever the number | |
| 271 // of pending operations is a multiple of the batch size. | |
| 272 if ((pending_copy_operations_.size() % kBatchSize) == 0) { | |
|
vmpstr
2014/10/22 23:25:16
We only flush if the pending size grows to be 4? W
reveman
2014/10/23 15:38:55
Good idea, I changed it to what you suggested here
| |
| 273 task_runner_->PostTask(FROM_HERE, | |
| 274 base::Bind(&OneCopyRasterWorkerPool::FlushCopy, | |
| 275 weak_ptr_factory_.GetWeakPtr(), | |
| 276 sequence)); | |
| 277 } | |
| 278 | |
| 279 return sequence; | |
| 280 } | |
| 281 | |
| 282 void OneCopyRasterWorkerPool::IssueCopy(CopySequenceNumber sequence) { | |
|
vmpstr
2014/10/22 23:25:16
I think these functions warrant a bit more comment
reveman
2014/10/23 15:38:55
I changed the naming of these functions in latest
| |
| 283 if (last_issued_copy_operation_ >= sequence) | |
| 284 return; | |
| 285 | |
| 286 IssueCopyOperations(sequence - last_issued_copy_operation_); | |
| 287 last_issued_copy_operation_ = sequence; | |
| 288 } | |
| 289 | |
| 290 void OneCopyRasterWorkerPool::FlushCopy(CopySequenceNumber sequence) { | |
| 291 if (last_flushed_copy_operation_ >= sequence) | |
| 292 return; | |
| 293 | |
| 294 IssueCopy(sequence); | |
| 295 | |
| 296 // Flush all issued copy operations. | |
| 297 context_provider_->ContextGL()->ShallowFlushCHROMIUM(); | |
| 298 last_flushed_copy_operation_ = last_issued_copy_operation_; | |
| 299 } | |
| 300 | |
| 232 void OneCopyRasterWorkerPool::OnRasterFinished(TaskSet task_set) { | 301 void OneCopyRasterWorkerPool::OnRasterFinished(TaskSet task_set) { |
| 233 TRACE_EVENT1( | 302 TRACE_EVENT1( |
| 234 "cc", "OneCopyRasterWorkerPool::OnRasterFinished", "task_set", task_set); | 303 "cc", "OneCopyRasterWorkerPool::OnRasterFinished", "task_set", task_set); |
| 235 | 304 |
| 236 DCHECK(raster_pending_[task_set]); | 305 DCHECK(raster_pending_[task_set]); |
| 237 raster_pending_[task_set] = false; | 306 raster_pending_[task_set] = false; |
| 238 if (raster_pending_.any()) { | 307 if (raster_pending_.any()) { |
| 239 TRACE_EVENT_ASYNC_STEP_INTO1( | 308 TRACE_EVENT_ASYNC_STEP_INTO1( |
| 240 "cc", "ScheduledTasks", this, "rasterizing", "state", StateAsValue()); | 309 "cc", "ScheduledTasks", this, "rasterizing", "state", StateAsValue()); |
| 241 } else { | 310 } else { |
| 242 TRACE_EVENT_ASYNC_END0("cc", "ScheduledTasks", this); | 311 TRACE_EVENT_ASYNC_END0("cc", "ScheduledTasks", this); |
| 243 } | 312 } |
| 244 client_->DidFinishRunningTasks(task_set); | 313 client_->DidFinishRunningTasks(task_set); |
| 245 } | 314 } |
| 246 | 315 |
| 316 void OneCopyRasterWorkerPool::IssueCopyOperations(int64 count) { | |
|
vmpstr
2014/10/22 23:25:16
I'm not sure if there's a better name for this? Ma
reveman
2014/10/23 15:38:55
Kept this as is in latest patch after changing the
| |
| 317 TRACE_EVENT1( | |
| 318 "cc", "OneCopyRasterWorkerPool::IssueCopyOperations", "count", count); | |
| 319 | |
| 320 CopyOperation::Deque copy_operations; | |
| 321 | |
| 322 { | |
| 323 base::AutoLock lock(lock_); | |
| 324 | |
| 325 for (int64 i = 0; i < count; ++i) { | |
| 326 DCHECK(!pending_copy_operations_.empty()); | |
| 327 copy_operations.push_back(pending_copy_operations_.take_front()); | |
| 328 } | |
| 329 } | |
| 330 | |
| 331 while (!copy_operations.empty()) { | |
| 332 scoped_ptr<CopyOperation> copy_operation = copy_operations.take_front(); | |
| 333 | |
| 334 // Remove the write lock. | |
| 335 copy_operation->write_lock.reset(); | |
| 336 | |
| 337 // Copy contents of source resource to destination resource. | |
| 338 resource_provider_->CopyResource(copy_operation->src, copy_operation->dst); | |
| 339 } | |
| 340 } | |
| 341 | |
| 247 scoped_refptr<base::debug::ConvertableToTraceFormat> | 342 scoped_refptr<base::debug::ConvertableToTraceFormat> |
| 248 OneCopyRasterWorkerPool::StateAsValue() const { | 343 OneCopyRasterWorkerPool::StateAsValue() const { |
| 249 scoped_refptr<base::debug::TracedValue> state = | 344 scoped_refptr<base::debug::TracedValue> state = |
| 250 new base::debug::TracedValue(); | 345 new base::debug::TracedValue(); |
| 251 | 346 |
| 252 state->BeginArray("tasks_pending"); | 347 state->BeginArray("tasks_pending"); |
| 253 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) | 348 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) |
| 254 state->AppendBoolean(raster_pending_[task_set]); | 349 state->AppendBoolean(raster_pending_[task_set]); |
| 255 state->EndArray(); | 350 state->EndArray(); |
| 256 state->BeginDictionary("staging_state"); | 351 state->BeginDictionary("staging_state"); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 267 resource_pool_->total_memory_usage_bytes()); | 362 resource_pool_->total_memory_usage_bytes()); |
| 268 staging_state->SetInteger("pending_copy_count", | 363 staging_state->SetInteger("pending_copy_count", |
| 269 resource_pool_->total_resource_count() - | 364 resource_pool_->total_resource_count() - |
| 270 resource_pool_->acquired_resource_count()); | 365 resource_pool_->acquired_resource_count()); |
| 271 staging_state->SetInteger("bytes_pending_copy", | 366 staging_state->SetInteger("bytes_pending_copy", |
| 272 resource_pool_->total_memory_usage_bytes() - | 367 resource_pool_->total_memory_usage_bytes() - |
| 273 resource_pool_->acquired_memory_usage_bytes()); | 368 resource_pool_->acquired_memory_usage_bytes()); |
| 274 } | 369 } |
| 275 | 370 |
| 276 } // namespace cc | 371 } // namespace cc |
| OLD | NEW |