summaryrefslogtreecommitdiffstats
path: root/chromium/cc/resources/rasterizer.h
blob: 17cc94ed6fa8b82f1a17e565f59e0847b2743657 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2014 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_RESOURCES_RASTERIZER_H_
#define CC_RESOURCES_RASTERIZER_H_

#include <vector>

#include "base/callback.h"
#include "cc/resources/resource_format.h"
#include "cc/resources/task_graph_runner.h"

class SkCanvas;

namespace cc {
class ImageDecodeTask;
class RasterTask;
class Resource;

class CC_EXPORT RasterizerTaskClient {
 public:
  virtual SkCanvas* AcquireCanvasForRaster(RasterTask* task) = 0;
  virtual void ReleaseCanvasForRaster(RasterTask* task) = 0;

 protected:
  virtual ~RasterizerTaskClient() {}
};

class CC_EXPORT RasterizerTask : public Task {
 public:
  typedef std::vector<scoped_refptr<RasterizerTask> > Vector;

  virtual void ScheduleOnOriginThread(RasterizerTaskClient* client) = 0;
  virtual void CompleteOnOriginThread(RasterizerTaskClient* client) = 0;
  virtual void RunReplyOnOriginThread() = 0;

  // Type-checking downcast routines.
  virtual ImageDecodeTask* AsImageDecodeTask();
  virtual RasterTask* AsRasterTask();

  void WillSchedule();
  void DidSchedule();
  bool HasBeenScheduled() const;

  void WillComplete();
  void DidComplete();
  bool HasCompleted() const;

 protected:
  RasterizerTask();
  virtual ~RasterizerTask();

  bool did_schedule_;
  bool did_complete_;
};

class CC_EXPORT ImageDecodeTask : public RasterizerTask {
 public:
  typedef std::vector<scoped_refptr<ImageDecodeTask> > Vector;

  // Overridden from RasterizerTask:
  virtual ImageDecodeTask* AsImageDecodeTask() OVERRIDE;

 protected:
  ImageDecodeTask();
  virtual ~ImageDecodeTask();
};

class CC_EXPORT RasterTask : public RasterizerTask {
 public:
  typedef std::vector<scoped_refptr<RasterTask> > Vector;

  // Overridden from RasterizerTask:
  virtual RasterTask* AsRasterTask() OVERRIDE;

  const Resource* resource() const { return resource_; }
  const ImageDecodeTask::Vector& dependencies() const { return dependencies_; }

 protected:
  RasterTask(const Resource* resource, ImageDecodeTask::Vector* dependencies);
  virtual ~RasterTask();

 private:
  const Resource* resource_;
  ImageDecodeTask::Vector dependencies_;
};

class CC_EXPORT RasterizerClient {
 public:
  virtual bool ShouldForceTasksRequiredForActivationToComplete() const = 0;
  virtual void DidFinishRunningTasks() = 0;
  virtual void DidFinishRunningTasksRequiredForActivation() = 0;

 protected:
  virtual ~RasterizerClient() {}
};

struct CC_EXPORT RasterTaskQueue {
  struct CC_EXPORT Item {
    class TaskComparator {
     public:
      explicit TaskComparator(const RasterTask* task) : task_(task) {}

      bool operator()(const Item& item) const { return item.task == task_; }

     private:
      const RasterTask* task_;
    };

    typedef std::vector<Item> Vector;

    Item(RasterTask* task, bool required_for_activation);
    ~Item();

    static bool IsRequiredForActivation(const Item& item) {
      return item.required_for_activation;
    }

    RasterTask* task;
    bool required_for_activation;
  };

  RasterTaskQueue();
  ~RasterTaskQueue();

  void Swap(RasterTaskQueue* other);
  void Reset();

  Item::Vector items;
  size_t required_for_activation_count;
};

// This interface can be used to schedule and run raster tasks. The client will
// be notified asynchronously when the set of tasks marked as "required for
// activation" have finished running and when all scheduled tasks have finished
// running. The client can call CheckForCompletedTasks() at any time to dispatch
// pending completion callbacks for all tasks that have finished running.
class CC_EXPORT Rasterizer {
 public:
  // Set the client instance to be notified when finished running tasks.
  virtual void SetClient(RasterizerClient* client) = 0;

  // Tells the worker pool to shutdown after canceling all previously scheduled
  // tasks. Reply callbacks are still guaranteed to run when
  // CheckForCompletedTasks() is called.
  virtual void Shutdown() = 0;

  // Schedule running of raster tasks in |queue| and all dependencies.
  // Previously scheduled tasks that are not in |queue| will be canceled unless
  // already running. Once scheduled, reply callbacks are guaranteed to run for
  // all tasks even if they later get canceled by another call to
  // ScheduleTasks().
  virtual void ScheduleTasks(RasterTaskQueue* queue) = 0;

  // Check for completed tasks and dispatch reply callbacks.
  virtual void CheckForCompletedTasks() = 0;

 protected:
  virtual ~Rasterizer() {}
};

}  // namespace cc

#endif  // CC_RESOURCES_RASTERIZER_H_