summaryrefslogtreecommitdiffstats
path: root/cc/raster_worker_pool.h
blob: 0c4d3867088e7a0c8ed5c7508935c6376ad4c031 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// 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_