Commit 7489ffb2 authored by reveman@chromium.org's avatar reveman@chromium.org

cc: Add RasterWorkerPool class.

    
This moves thread pool related code out of the tile manager and into a
new RasterWorkerPool class. This doesn't change the behavior of the tile
manager in any way it just separates tile management logic from the
thread handling logic, which results in a significant cleanup of the
tile manager.
    
BUG=155209
TEST=manual

Review URL: https://chromiumcodereview.appspot.com/11593030

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@175877 0039d316-1c4b-4281-b951-d872f2087c98
parent 9b5dbf76
...@@ -168,6 +168,8 @@ ...@@ -168,6 +168,8 @@
'quad_culler.cc', 'quad_culler.cc',
'quad_culler.h', 'quad_culler.h',
'quad_sink.h', 'quad_sink.h',
'raster_worker_pool.cc',
'raster_worker_pool.h',
'rate_limiter.cc', 'rate_limiter.cc',
'rate_limiter.h', 'rate_limiter.h',
'region.cc', 'region.cc',
......
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/raster_worker_pool.h"
#include <algorithm>
#include "base/bind.h"
#include "base/debug/trace_event.h"
#include "base/stl_util.h"
#include "base/stringprintf.h"
#include "cc/picture_pile_impl.h"
#include "third_party/skia/include/core/SkDevice.h"
namespace cc {
namespace {
void RunRasterTask(PicturePileImpl* picture_pile,
uint8* buffer,
const gfx::Rect& rect,
float contents_scale,
RenderingStats* stats) {
TRACE_EVENT0("cc", "RunRasterTask");
DCHECK(picture_pile);
DCHECK(buffer);
SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config, rect.width(), rect.height());
bitmap.setPixels(buffer);
SkDevice device(bitmap);
SkCanvas canvas(&device);
picture_pile->Raster(&canvas, rect, contents_scale, stats);
}
void RunImageDecodeTask(skia::LazyPixelRef* pixel_ref, RenderingStats* stats) {
TRACE_EVENT0("cc", "RunImageDecodeTask");
base::TimeTicks decode_begin_time = base::TimeTicks::Now();
pixel_ref->Decode();
stats->totalDeferredImageDecodeCount++;
stats->totalDeferredImageDecodeTimeInSeconds +=
(base::TimeTicks::Now() - decode_begin_time).InSecondsF();
}
const char* kRasterThreadNamePrefix = "CompositorRaster";
// Allow two pending raster tasks per thread. This keeps resource usage
// low while making sure raster threads aren't unnecessarily idle.
const int kNumPendingRasterTasksPerThread = 2;
} // namespace
RasterWorkerPool::Thread::Task::Task(Thread* thread) : thread_(thread) {
thread_->num_pending_tasks_++;
}
RasterWorkerPool::Thread::Task::~Task() {
thread_->rendering_stats_.totalRasterizeTimeInSeconds +=
rendering_stats_.totalRasterizeTimeInSeconds;
thread_->rendering_stats_.totalPixelsRasterized +=
rendering_stats_.totalPixelsRasterized;
thread_->rendering_stats_.totalDeferredImageDecodeTimeInSeconds +=
rendering_stats_.totalDeferredImageDecodeTimeInSeconds;
thread_->rendering_stats_.totalDeferredImageDecodeCount +=
rendering_stats_.totalDeferredImageDecodeCount;
thread_->num_pending_tasks_--;
}
RasterWorkerPool::Thread::Thread(const std::string name)
: base::Thread(name.c_str()),
num_pending_tasks_(0) {
Start();
}
RasterWorkerPool::Thread::~Thread() {
Stop();
}
RasterWorkerPool::RasterWorkerPool(size_t num_raster_threads) {
const std::string thread_name_prefix = kRasterThreadNamePrefix;
while (raster_threads_.size() < num_raster_threads) {
int thread_number = raster_threads_.size() + 1;
raster_threads_.push_back(
new Thread(thread_name_prefix +
StringPrintf("Worker%d", thread_number).c_str()));
}
}
RasterWorkerPool::~RasterWorkerPool() {
STLDeleteElements(&raster_threads_);
}
bool RasterWorkerPool::IsBusy() {
Thread* thread = raster_threads_.front();
return thread->num_pending_tasks() >= kNumPendingRasterTasksPerThread;
}
void RasterWorkerPool::PostRasterTaskAndReply(PicturePileImpl* picture_pile,
uint8* buffer,
const gfx::Rect& rect,
float contents_scale,
const base::Closure& reply) {
Thread::Task* task = CreateTask();
scoped_refptr<PicturePileImpl> picture_pile_clone =
picture_pile->GetCloneForDrawingOnThread(task->thread_);
task->thread_->message_loop_proxy()->PostTaskAndReply(
FROM_HERE,
base::Bind(&RunRasterTask,
base::Unretained(picture_pile_clone.get()),
buffer,
rect,
contents_scale,
&task->rendering_stats_),
base::Bind(&RasterWorkerPool::OnRasterTaskCompleted,
base::Unretained(this),
base::Unretained(task),
picture_pile_clone,
reply));
}
void RasterWorkerPool::PostImageDecodeTaskAndReply(
skia::LazyPixelRef* pixel_ref,
const base::Closure& reply) {
Thread::Task* task = CreateTask();
task->thread_->message_loop_proxy()->PostTaskAndReply(
FROM_HERE,
base::Bind(&RunImageDecodeTask, pixel_ref, &task->rendering_stats_),
base::Bind(&RasterWorkerPool::OnTaskCompleted,
base::Unretained(this),
base::Unretained(task),
reply));
}
void RasterWorkerPool::GetRenderingStats(RenderingStats* stats) {
for (ThreadVector::iterator it = raster_threads_.begin();
it != raster_threads_.end(); ++it) {
Thread* thread = *it;
stats->totalRasterizeTimeInSeconds =
thread->rendering_stats().totalRasterizeTimeInSeconds;
stats->totalPixelsRasterized =
thread->rendering_stats().totalPixelsRasterized;
stats->totalDeferredImageDecodeCount =
thread->rendering_stats().totalDeferredImageDecodeCount;
stats->totalDeferredImageDecodeTimeInSeconds =
thread->rendering_stats().totalDeferredImageDecodeTimeInSeconds;
}
}
RasterWorkerPool::Thread::Task* RasterWorkerPool::CreateTask() {
Thread* thread = raster_threads_.front();
DCHECK(thread->num_pending_tasks() < kNumPendingRasterTasksPerThread);
scoped_ptr<Thread::Task> task(new Thread::Task(thread));
std::sort(raster_threads_.begin(), raster_threads_.end(),
PendingTaskComparator());
return task.release();
}
void RasterWorkerPool::DestroyTask(Thread::Task* task) {
delete task;
std::sort(raster_threads_.begin(), raster_threads_.end(),
PendingTaskComparator());
}
void RasterWorkerPool::OnTaskCompleted(
Thread::Task* task, const base::Closure& reply) {
DestroyTask(task);
reply.Run();
}
void RasterWorkerPool::OnRasterTaskCompleted(
Thread::Task* task,
scoped_refptr<PicturePileImpl> picture_pile,
const base::Closure& reply) {
OnTaskCompleted(task, reply);
}
} // namespace cc
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CC_RASTER_WORKER_POOL_H_
#define CC_RASTER_WORKER_POOL_H_
#include <string>
#include "base/basictypes.h"
#include "base/callback.h"
#include "base/threading/thread.h"
#include "cc/rendering_stats.h"
#include "ui/gfx/rect.h"
namespace skia {
class LazyPixelRef;
}
namespace cc {
class PicturePileImpl;
class RasterWorkerPool {
public:
explicit RasterWorkerPool(size_t num_raster_threads);
virtual ~RasterWorkerPool();
static scoped_ptr<RasterWorkerPool> Create(size_t num_raster_threads) {
return make_scoped_ptr(new RasterWorkerPool(num_raster_threads));
}
bool IsBusy();
void PostRasterTaskAndReply(PicturePileImpl* picture_pile,
uint8* buffer,
const gfx::Rect& rect,
float contents_scale,
const base::Closure& reply);
void PostImageDecodeTaskAndReply(skia::LazyPixelRef* pixel_ref,
const base::Closure& reply);
void GetRenderingStats(RenderingStats* stats);
private:
class Thread : public base::Thread {
public:
class Task {
public:
Task(Thread* thread);
~Task();
private:
friend class RasterWorkerPool;
Thread* thread_;
RenderingStats rendering_stats_;
DISALLOW_COPY_AND_ASSIGN(Task);
};
Thread(const std::string name);
virtual ~Thread();
int num_pending_tasks() const { return num_pending_tasks_; }
RenderingStats& rendering_stats() { return rendering_stats_; }
private:
int num_pending_tasks_;
RenderingStats rendering_stats_;
DISALLOW_COPY_AND_ASSIGN(Thread);
};
class PendingTaskComparator {
public:
bool operator() (const Thread* a, const Thread* b) const {
return a->num_pending_tasks() < b->num_pending_tasks();
}
};
Thread::Task* CreateTask();
void DestroyTask(Thread::Task* task);
void OnTaskCompleted(Thread::Task* task,
const base::Closure& reply);
void OnRasterTaskCompleted(Thread::Task* task,
scoped_refptr<PicturePileImpl> picture_pile,
const base::Closure& reply);
typedef std::vector<Thread*> ThreadVector;
ThreadVector raster_threads_;
DISALLOW_COPY_AND_ASSIGN(RasterWorkerPool);
};
} // namespace cc
#endif // CC_RASTER_WORKER_POOL_H_
This diff is collapsed.
...@@ -12,16 +12,15 @@ ...@@ -12,16 +12,15 @@
#include "base/hash_tables.h" #include "base/hash_tables.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/values.h" #include "base/values.h"
#include "cc/rendering_stats.h"
#include "cc/resource_pool.h" #include "cc/resource_pool.h"
#include "cc/tile_priority.h" #include "cc/tile_priority.h"
namespace cc { namespace cc {
class RasterWorkerPool;
class RasterThread;
class ResourceProvider; class ResourceProvider;
class Tile; class Tile;
class TileVersion; class TileVersion;
struct RenderingStats;
class CC_EXPORT TileManagerClient { class CC_EXPORT TileManagerClient {
public: public:
...@@ -80,7 +79,9 @@ class CC_EXPORT TileManager { ...@@ -80,7 +79,9 @@ class CC_EXPORT TileManager {
size_t num_raster_threads); size_t num_raster_threads);
virtual ~TileManager(); virtual ~TileManager();
const GlobalStateThatImpactsTilePriority& GlobalState() const { return global_state_; } const GlobalStateThatImpactsTilePriority& GlobalState() const {
return global_state_;
}
void SetGlobalState(const GlobalStateThatImpactsTilePriority& state); void SetGlobalState(const GlobalStateThatImpactsTilePriority& state);
void ManageTiles(); void ManageTiles();
...@@ -97,36 +98,34 @@ class CC_EXPORT TileManager { ...@@ -97,36 +98,34 @@ class CC_EXPORT TileManager {
protected: protected:
// Methods called by Tile // Methods called by Tile
friend class Tile; friend class Tile;
void RegisterTile(Tile*); void RegisterTile(Tile* tile);
void UnregisterTile(Tile*); void UnregisterTile(Tile* tile);
void WillModifyTilePriority(Tile*, WhichTree, const TilePriority& new_priority); void WillModifyTilePriority(
Tile* tile, WhichTree tree, const TilePriority& new_priority);
private: private:
void ResetBinCounts(); void ResetBinCounts();
void AssignGpuMemoryToTiles(); void AssignGpuMemoryToTiles();
void FreeResourcesForTile(Tile*); void FreeResourcesForTile(Tile* tile);
void ScheduleManageTiles(); void ScheduleManageTiles();
void ScheduleCheckForCompletedSetPixels(); void ScheduleCheckForCompletedSetPixels();
void DispatchMoreTasks(); void DispatchMoreTasks();
void DispatchOneRasterTask(RasterThread*, scoped_refptr<Tile>); void GatherPixelRefsForTile(Tile* tile);
void DispatchImageDecodeTasksForTile(Tile* tile);
void DispatchOneImageDecodeTask(
scoped_refptr<Tile> tile, skia::LazyPixelRef* pixel_ref);
void OnImageDecodeTaskCompleted(
scoped_refptr<Tile> tile, uint32_t pixel_ref_id);
void DispatchOneRasterTask(scoped_refptr<Tile> tile);
void OnRasterTaskCompleted( void OnRasterTaskCompleted(
scoped_refptr<Tile>, scoped_refptr<Tile> tile,
scoped_ptr<ResourcePool::Resource>, scoped_ptr<ResourcePool::Resource> resource,
scoped_refptr<PicturePileImpl>, int manage_tiles_call_count_when_dispatched);
int manage_tiles_call_count_when_dispatched, void DidFinishTileInitialization(Tile* tile);
RenderingStats*);
void DidFinishTileInitialization(Tile*);
void DispatchImageDecodingTasksForTile(Tile*);
void OnImageDecodingTaskCompleted(scoped_refptr<Tile>,
uint32_t,
RenderingStats*);
void DispatchOneImageDecodingTask(
RasterThread*, scoped_refptr<Tile>, skia::LazyPixelRef*);
void GatherPixelRefsForTile(Tile*);
RasterThread* GetFreeRasterThread();
TileManagerClient* client_; TileManagerClient* client_;
scoped_ptr<ResourcePool> resource_pool_; scoped_ptr<ResourcePool> resource_pool_;
scoped_ptr<RasterWorkerPool> raster_worker_;
bool manage_tiles_pending_; bool manage_tiles_pending_;
int manage_tiles_call_count_; int manage_tiles_call_count_;
bool check_for_completed_set_pixels_pending_; bool check_for_completed_set_pixels_pending_;
...@@ -151,9 +150,6 @@ class CC_EXPORT TileManager { ...@@ -151,9 +150,6 @@ class CC_EXPORT TileManager {
typedef std::queue<scoped_refptr<Tile> > TileQueue; typedef std::queue<scoped_refptr<Tile> > TileQueue;
TileQueue tiles_with_pending_set_pixels_; TileQueue tiles_with_pending_set_pixels_;
typedef ScopedPtrVector<RasterThread> RasterThreadVector;
RasterThreadVector raster_threads_;
RenderingStats rendering_stats_; RenderingStats rendering_stats_;
DISALLOW_COPY_AND_ASSIGN(TileManager); DISALLOW_COPY_AND_ASSIGN(TileManager);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment