[go: nahoru, domu]

blob: b3c4cd2a9b53a14c57814bffe62e8c2bd2c58a5e [file] [log] [blame]
Avi Drissman3f7a9d82022-09-08 20:55:421// Copyright 2016 The Chromium Authors
prashant.n49b3e64652016-04-19 07:04:492// 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_RASTER_TASK_H_
6#define CC_RASTER_TASK_H_
7
8#include <stdint.h>
9
vmpstra6b30162017-01-12 20:23:0310#include <string>
prashant.n49b3e64652016-04-19 07:04:4911#include <vector>
12
Keishi Hattoric1b00232022-11-22 09:04:2613#include "base/memory/raw_ptr.h"
prashant.n49b3e64652016-04-19 07:04:4914#include "base/memory/ref_counted.h"
chrishtrac41ff92017-03-17 05:07:3015#include "cc/cc_export.h"
prashant.n49b3e64652016-04-19 07:04:4916
17namespace cc {
prashant.nd67b4b012016-04-25 19:36:0518class Task;
19
prashant.n06e15612016-05-26 17:35:2120// This class provides states to manage life cycle of a task and given below is
21// how it is used by TaskGraphWorkQueue to process life cycle of a task.
Ho Cheung06365192023-09-14 17:09:5422// Task is in kNew state when it is created. When task is added to
23// |ready_to_run_tasks| then its state is changed to kScheduled. Task can be
24// canceled from kNew state (not yet scheduled to run) or from kScheduled state,
25// when new ScheduleTasks() is triggered and its state is changed to kCanceled.
prashant.n06e15612016-05-26 17:35:2126// When task is about to run it is added |running_tasks| and its state is
Ho Cheung06365192023-09-14 17:09:5427// changed to kRunning. Once task finishes running, its state is changed to
28// kFinished. Both kCanceled and kFinished tasks are added to |completed_tasks|.
prashant.n06e15612016-05-26 17:35:2129// ╔═════╗
Ho Cheung06365192023-09-14 17:09:5430// +------║ kNew║------+
prashant.n06e15612016-05-26 17:35:2131// | ╚═════╝ |
32// v v
33// ┌───────────┐ ╔══════════╗
Ho Cheung06365192023-09-14 17:09:5434// │ kScheduled│------> ║ kCanceled║
prashant.n06e15612016-05-26 17:35:2135// └───────────┘ ╚══════════╝
36// |
37// v
38// ┌─────────┐ ╔══════════╗
Ho Cheung06365192023-09-14 17:09:5439// │ kRunning│-------> ║ kFinished║
prashant.n06e15612016-05-26 17:35:2140// └─────────┘ ╚══════════╝
prashant.nd67b4b012016-04-25 19:36:0541class CC_EXPORT TaskState {
42 public:
vmpstra6b30162017-01-12 20:23:0343 bool IsNew() const;
prashant.nd67b4b012016-04-25 19:36:0544 bool IsScheduled() const;
45 bool IsRunning() const;
46 bool IsFinished() const;
47 bool IsCanceled() const;
48
49 // Functions to change the state of task. These functions should be called
50 // only from TaskGraphWorkQueue where the life cycle of a task is decided or
51 // from tests. These functions are not thread-safe. Caller is responsible for
52 // thread safety.
Ho Cheung06365192023-09-14 17:09:5453 void Reset(); // Sets state to kNew.
prashant.nd67b4b012016-04-25 19:36:0554 void DidSchedule();
55 void DidStart();
56 void DidFinish();
57 void DidCancel();
58
vmpstra6b30162017-01-12 20:23:0359 std::string ToString() const;
60
prashant.nd67b4b012016-04-25 19:36:0561 private:
62 friend class Task;
63
64 // Let only Task class create the TaskState.
65 TaskState();
66 ~TaskState();
67
Ho Cheung06365192023-09-14 17:09:5468 enum class Value : uint16_t {
69 kNew,
70 kScheduled,
71 kRunning,
72 kFinished,
73 kCanceled
74 };
prashant.nd67b4b012016-04-25 19:36:0575
76 Value value_;
77};
prashant.n49b3e64652016-04-19 07:04:4978
79// A task which can be run by a TaskGraphRunner. To run a Task, it should be
80// inserted into a TaskGraph, which can then be scheduled on the
81// TaskGraphRunner.
82class CC_EXPORT Task : public base::RefCountedThreadSafe<Task> {
83 public:
84 typedef std::vector<scoped_refptr<Task>> Vector;
85
prashant.nd67b4b012016-04-25 19:36:0586 TaskState& state() { return state_; }
Linwan Songb7f402a2018-12-11 17:50:5787 void set_frame_number(int64_t frame_number) { frame_number_ = frame_number; }
88 int64_t frame_number() { return frame_number_; }
prashant.nd67b4b012016-04-25 19:36:0589
Sreeja Kamishettyfcea16b2023-06-08 18:23:2590 // Unique trace flow id for the given task, used to connect the places where
91 // the task was posted from and the task itself.
92 uint64_t trace_task_id() { return trace_task_id_; }
93 void set_trace_task_id(uint64_t id) { trace_task_id_ = id; }
94
prashant.n49b3e64652016-04-19 07:04:4995 // Subclasses should implement this method. RunOnWorkerThread may be called
96 // on any thread, and subclasses are responsible for locking and thread
97 // safety.
98 virtual void RunOnWorkerThread() = 0;
99
prashant.n49b3e64652016-04-19 07:04:49100 protected:
101 friend class base::RefCountedThreadSafe<Task>;
102
103 Task();
104 virtual ~Task();
105
prashant.nd67b4b012016-04-25 19:36:05106 private:
107 TaskState state_;
Linwan Songb7f402a2018-12-11 17:50:57108 int64_t frame_number_ = -1;
Sreeja Kamishettyfcea16b2023-06-08 18:23:25109 int64_t trace_task_id_ = 0;
prashant.n49b3e64652016-04-19 07:04:49110};
111
112// A task dependency graph describes the order in which to execute a set
113// of tasks. Dependencies are represented as edges. Each node is assigned
114// a category, a priority and a run count that matches the number of
115// dependencies. Priority range from 0 (most favorable scheduling) to UINT16_MAX
116// (least favorable). Categories range from 0 to UINT16_MAX. It is up to the
117// implementation and its consumer to determine the meaning (if any) of a
118// category. A TaskGraphRunner implementation may chose to prioritize certain
119// categories over others, regardless of the individual priorities of tasks.
120struct CC_EXPORT TaskGraph {
vmpstr48f6e1fb2016-09-22 18:35:44121 struct CC_EXPORT Node {
prashant.n49b3e64652016-04-19 07:04:49122 typedef std::vector<Node> Vector;
123
Sreeja Kamishettyfcea16b2023-06-08 18:23:25124 Node(scoped_refptr<Task> new_task,
prashant.n49b3e64652016-04-19 07:04:49125 uint16_t category,
126 uint16_t priority,
vmpstr48f6e1fb2016-09-22 18:35:44127 uint32_t dependencies);
Vladimir Levinf06d1cd72019-03-13 18:24:10128 Node(const Node&) = delete;
vmpstr48f6e1fb2016-09-22 18:35:44129 Node(Node&& other);
130 ~Node();
prashant.n49b3e64652016-04-19 07:04:49131
Vladimir Levinf06d1cd72019-03-13 18:24:10132 Node& operator=(const Node&) = delete;
vmpstr48f6e1fb2016-09-22 18:35:44133 Node& operator=(Node&& other) = default;
134
135 scoped_refptr<Task> task;
prashant.n49b3e64652016-04-19 07:04:49136 uint16_t category;
137 uint16_t priority;
138 uint32_t dependencies;
139 };
140
141 struct Edge {
142 typedef std::vector<Edge> Vector;
143
144 Edge(const Task* task, Task* dependent)
145 : task(task), dependent(dependent) {}
146
Pârise6361d02023-07-19 09:00:43147 raw_ptr<const Task, AcrossTasksDanglingUntriaged> task;
148 raw_ptr<Task, AcrossTasksDanglingUntriaged> dependent;
prashant.n49b3e64652016-04-19 07:04:49149 };
150
151 TaskGraph();
Vladimir Levinf06d1cd72019-03-13 18:24:10152 TaskGraph(const TaskGraph&) = delete;
vmpstr48f6e1fb2016-09-22 18:35:44153 TaskGraph(TaskGraph&& other);
prashant.n49b3e64652016-04-19 07:04:49154 ~TaskGraph();
155
Vladimir Levinf06d1cd72019-03-13 18:24:10156 TaskGraph& operator=(const TaskGraph&) = delete;
157 TaskGraph& operator=(TaskGraph&&) = default;
158
prashant.n49b3e64652016-04-19 07:04:49159 void Swap(TaskGraph* other);
160 void Reset();
161
162 Node::Vector nodes;
163 Edge::Vector edges;
prashant.n49b3e64652016-04-19 07:04:49164};
165
166} // namespace cc
167
168#endif // CC_RASTER_TASK_H_