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
3// found in the LICENSE file.
4
5#ifndef CC_RESOURCES_RASTERIZER_H_
6#define CC_RESOURCES_RASTERIZER_H_
7
8#include <bitset>
9#include <vector>
10
11#include "base/callback.h"
12#include "cc/resources/resource_format.h"
13#include "cc/resources/task_graph_runner.h"
14
15namespace cc {
16class ImageDecodeTask;
17class RasterTask;
18class Resource;
19class RasterBuffer;
20
21class CC_EXPORT RasterizerTaskClient {
22 public:
23  virtual scoped_ptr<RasterBuffer> AcquireBufferForRaster(
24      const Resource* resource) = 0;
25  virtual void ReleaseBufferForRaster(scoped_ptr<RasterBuffer> buffer) = 0;
26
27 protected:
28  virtual ~RasterizerTaskClient() {}
29};
30
31class CC_EXPORT RasterizerTask : public Task {
32 public:
33  typedef std::vector<scoped_refptr<RasterizerTask> > Vector;
34
35  virtual void ScheduleOnOriginThread(RasterizerTaskClient* client) = 0;
36  virtual void CompleteOnOriginThread(RasterizerTaskClient* client) = 0;
37  virtual void RunReplyOnOriginThread() = 0;
38
39  // Type-checking downcast routines.
40  virtual ImageDecodeTask* AsImageDecodeTask();
41  virtual RasterTask* AsRasterTask();
42
43  void WillSchedule();
44  void DidSchedule();
45  bool HasBeenScheduled() const;
46
47  void WillComplete();
48  void DidComplete();
49  bool HasCompleted() const;
50
51 protected:
52  RasterizerTask();
53  virtual ~RasterizerTask();
54
55  bool did_schedule_;
56  bool did_complete_;
57};
58
59class CC_EXPORT ImageDecodeTask : public RasterizerTask {
60 public:
61  typedef std::vector<scoped_refptr<ImageDecodeTask> > Vector;
62
63  // Overridden from RasterizerTask:
64  virtual ImageDecodeTask* AsImageDecodeTask() OVERRIDE;
65
66 protected:
67  ImageDecodeTask();
68  virtual ~ImageDecodeTask();
69};
70
71class CC_EXPORT RasterTask : public RasterizerTask {
72 public:
73  typedef std::vector<scoped_refptr<RasterTask> > Vector;
74
75  // Overridden from RasterizerTask:
76  virtual RasterTask* AsRasterTask() OVERRIDE;
77
78  const Resource* resource() const { return resource_; }
79  const ImageDecodeTask::Vector& dependencies() const { return dependencies_; }
80
81 protected:
82  RasterTask(const Resource* resource, ImageDecodeTask::Vector* dependencies);
83  virtual ~RasterTask();
84
85 private:
86  const Resource* resource_;
87  ImageDecodeTask::Vector dependencies_;
88};
89
90static const size_t kNumberOfTaskSets = 2;
91typedef size_t TaskSet;
92typedef std::bitset<kNumberOfTaskSets> TaskSetCollection;
93
94class CC_EXPORT RasterizerClient {
95 public:
96  virtual void DidFinishRunningTasks(TaskSet task_set) = 0;
97  virtual TaskSetCollection TasksThatShouldBeForcedToComplete() const = 0;
98
99 protected:
100  virtual ~RasterizerClient() {}
101};
102
103struct CC_EXPORT RasterTaskQueue {
104  struct CC_EXPORT Item {
105    class TaskComparator {
106     public:
107      explicit TaskComparator(const RasterTask* task) : task_(task) {}
108
109      bool operator()(const Item& item) const { return item.task == task_; }
110
111     private:
112      const RasterTask* task_;
113    };
114
115    typedef std::vector<Item> Vector;
116
117    Item(RasterTask* task, const TaskSetCollection& task_sets);
118    ~Item();
119
120    RasterTask* task;
121    TaskSetCollection task_sets;
122  };
123
124  RasterTaskQueue();
125  ~RasterTaskQueue();
126
127  void Swap(RasterTaskQueue* other);
128  void Reset();
129
130  Item::Vector items;
131};
132
133// This interface can be used to schedule and run raster tasks. The client will
134// be notified asynchronously when the set of tasks marked as "required for
135// activation" have finished running and when all scheduled tasks have finished
136// running. The client can call CheckForCompletedTasks() at any time to dispatch
137// pending completion callbacks for all tasks that have finished running.
138class CC_EXPORT Rasterizer {
139 public:
140  // Set the client instance to be notified when finished running tasks.
141  virtual void SetClient(RasterizerClient* client) = 0;
142
143  // Tells the worker pool to shutdown after canceling all previously scheduled
144  // tasks. Reply callbacks are still guaranteed to run when
145  // CheckForCompletedTasks() is called.
146  virtual void Shutdown() = 0;
147
148  // Schedule running of raster tasks in |queue| and all dependencies.
149  // Previously scheduled tasks that are not in |queue| will be canceled unless
150  // already running. Once scheduled, reply callbacks are guaranteed to run for
151  // all tasks even if they later get canceled by another call to
152  // ScheduleTasks().
153  virtual void ScheduleTasks(RasterTaskQueue* queue) = 0;
154
155  // Check for completed tasks and dispatch reply callbacks.
156  virtual void CheckForCompletedTasks() = 0;
157
158 protected:
159  virtual ~Rasterizer() {}
160};
161
162}  // namespace cc
163
164#endif  // CC_RESOURCES_RASTERIZER_H_
165