OLD | NEW |
(Empty) | |
| 1 // Copyright 2012 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. |
| 4 |
| 5 #include "cc/raster_worker_pool.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/debug/trace_event.h" |
| 11 #include "base/stl_util.h" |
| 12 #include "base/stringprintf.h" |
| 13 #include "cc/picture_pile_impl.h" |
| 14 #include "third_party/skia/include/core/SkDevice.h" |
| 15 |
| 16 namespace cc { |
| 17 |
| 18 namespace { |
| 19 |
| 20 void RunRasterTask(PicturePileImpl* picture_pile, |
| 21 uint8* buffer, |
| 22 const gfx::Rect& rect, |
| 23 float contents_scale, |
| 24 RenderingStats* stats) { |
| 25 TRACE_EVENT0("cc", "RunRasterTask"); |
| 26 DCHECK(picture_pile); |
| 27 DCHECK(buffer); |
| 28 SkBitmap bitmap; |
| 29 bitmap.setConfig(SkBitmap::kARGB_8888_Config, rect.width(), rect.height()); |
| 30 bitmap.setPixels(buffer); |
| 31 SkDevice device(bitmap); |
| 32 SkCanvas canvas(&device); |
| 33 picture_pile->Raster(&canvas, rect, contents_scale, stats); |
| 34 } |
| 35 |
| 36 void RunImageDecodeTask(skia::LazyPixelRef* pixel_ref, RenderingStats* stats) { |
| 37 TRACE_EVENT0("cc", "RunImageDecodeTask"); |
| 38 base::TimeTicks decode_begin_time = base::TimeTicks::Now(); |
| 39 pixel_ref->Decode(); |
| 40 stats->totalDeferredImageDecodeCount++; |
| 41 stats->totalDeferredImageDecodeTimeInSeconds += |
| 42 (base::TimeTicks::Now() - decode_begin_time).InSecondsF(); |
| 43 } |
| 44 |
| 45 const char* kRasterThreadNamePrefix = "CompositorRaster"; |
| 46 |
| 47 // Allow two pending raster tasks per thread. This keeps resource usage |
| 48 // low while making sure raster threads aren't unnecessarily idle. |
| 49 const int kNumPendingRasterTasksPerThread = 2; |
| 50 |
| 51 } // namespace |
| 52 |
| 53 RasterWorkerPool::Thread::Task::Task(Thread* thread) : thread_(thread) { |
| 54 thread_->num_pending_tasks_++; |
| 55 } |
| 56 |
| 57 RasterWorkerPool::Thread::Task::~Task() { |
| 58 thread_->rendering_stats_.totalRasterizeTimeInSeconds += |
| 59 rendering_stats_.totalRasterizeTimeInSeconds; |
| 60 thread_->rendering_stats_.totalPixelsRasterized += |
| 61 rendering_stats_.totalPixelsRasterized; |
| 62 thread_->rendering_stats_.totalDeferredImageDecodeTimeInSeconds += |
| 63 rendering_stats_.totalDeferredImageDecodeTimeInSeconds; |
| 64 thread_->rendering_stats_.totalDeferredImageDecodeCount += |
| 65 rendering_stats_.totalDeferredImageDecodeCount; |
| 66 |
| 67 thread_->num_pending_tasks_--; |
| 68 } |
| 69 |
| 70 RasterWorkerPool::Thread::Thread(const std::string name) |
| 71 : base::Thread(name.c_str()), |
| 72 num_pending_tasks_(0) { |
| 73 Start(); |
| 74 } |
| 75 |
| 76 RasterWorkerPool::Thread::~Thread() { |
| 77 Stop(); |
| 78 } |
| 79 |
| 80 RasterWorkerPool::RasterWorkerPool(size_t num_raster_threads) { |
| 81 const std::string thread_name_prefix = kRasterThreadNamePrefix; |
| 82 while (raster_threads_.size() < num_raster_threads) { |
| 83 int thread_number = raster_threads_.size() + 1; |
| 84 raster_threads_.push_back( |
| 85 new Thread(thread_name_prefix + |
| 86 StringPrintf("Worker%d", thread_number).c_str())); |
| 87 } |
| 88 } |
| 89 |
| 90 RasterWorkerPool::~RasterWorkerPool() { |
| 91 STLDeleteElements(&raster_threads_); |
| 92 } |
| 93 |
| 94 bool RasterWorkerPool::IsBusy() { |
| 95 Thread* thread = raster_threads_.front(); |
| 96 return thread->num_pending_tasks() >= kNumPendingRasterTasksPerThread; |
| 97 } |
| 98 |
| 99 void RasterWorkerPool::PostRasterTaskAndReply(PicturePileImpl* picture_pile, |
| 100 uint8* buffer, |
| 101 const gfx::Rect& rect, |
| 102 float contents_scale, |
| 103 const base::Closure& reply) { |
| 104 Thread::Task* task = CreateTask(); |
| 105 |
| 106 scoped_refptr<PicturePileImpl> picture_pile_clone = |
| 107 picture_pile->GetCloneForDrawingOnThread(task->thread_); |
| 108 |
| 109 task->thread_->message_loop_proxy()->PostTaskAndReply( |
| 110 FROM_HERE, |
| 111 base::Bind(&RunRasterTask, |
| 112 base::Unretained(picture_pile_clone.get()), |
| 113 buffer, |
| 114 rect, |
| 115 contents_scale, |
| 116 &task->rendering_stats_), |
| 117 base::Bind(&RasterWorkerPool::OnRasterTaskCompleted, |
| 118 base::Unretained(this), |
| 119 base::Unretained(task), |
| 120 picture_pile_clone, |
| 121 reply)); |
| 122 } |
| 123 |
| 124 void RasterWorkerPool::PostImageDecodeTaskAndReply( |
| 125 skia::LazyPixelRef* pixel_ref, |
| 126 const base::Closure& reply) { |
| 127 Thread::Task* task = CreateTask(); |
| 128 |
| 129 task->thread_->message_loop_proxy()->PostTaskAndReply( |
| 130 FROM_HERE, |
| 131 base::Bind(&RunImageDecodeTask, pixel_ref, &task->rendering_stats_), |
| 132 base::Bind(&RasterWorkerPool::OnTaskCompleted, |
| 133 base::Unretained(this), |
| 134 base::Unretained(task), |
| 135 reply)); |
| 136 } |
| 137 |
| 138 void RasterWorkerPool::GetRenderingStats(RenderingStats* stats) { |
| 139 for (ThreadVector::iterator it = raster_threads_.begin(); |
| 140 it != raster_threads_.end(); ++it) { |
| 141 Thread* thread = *it; |
| 142 stats->totalRasterizeTimeInSeconds = |
| 143 thread->rendering_stats().totalRasterizeTimeInSeconds; |
| 144 stats->totalPixelsRasterized = |
| 145 thread->rendering_stats().totalPixelsRasterized; |
| 146 stats->totalDeferredImageDecodeCount = |
| 147 thread->rendering_stats().totalDeferredImageDecodeCount; |
| 148 stats->totalDeferredImageDecodeTimeInSeconds = |
| 149 thread->rendering_stats().totalDeferredImageDecodeTimeInSeconds; |
| 150 } |
| 151 } |
| 152 |
| 153 RasterWorkerPool::Thread::Task* RasterWorkerPool::CreateTask() { |
| 154 Thread* thread = raster_threads_.front(); |
| 155 DCHECK(thread->num_pending_tasks() < kNumPendingRasterTasksPerThread); |
| 156 |
| 157 scoped_ptr<Thread::Task> task(new Thread::Task(thread)); |
| 158 std::sort(raster_threads_.begin(), raster_threads_.end(), |
| 159 PendingTaskComparator()); |
| 160 return task.release(); |
| 161 } |
| 162 |
| 163 void RasterWorkerPool::DestroyTask(Thread::Task* task) { |
| 164 delete task; |
| 165 std::sort(raster_threads_.begin(), raster_threads_.end(), |
| 166 PendingTaskComparator()); |
| 167 } |
| 168 |
| 169 void RasterWorkerPool::OnTaskCompleted( |
| 170 Thread::Task* task, const base::Closure& reply) { |
| 171 DestroyTask(task); |
| 172 reply.Run(); |
| 173 } |
| 174 |
| 175 void RasterWorkerPool::OnRasterTaskCompleted( |
| 176 Thread::Task* task, |
| 177 scoped_refptr<PicturePileImpl> picture_pile, |
| 178 const base::Closure& reply) { |
| 179 OnTaskCompleted(task, reply); |
| 180 } |
| 181 |
| 182 } // namespace cc |
OLD | NEW |