OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017 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 GPU_COMMAND_BUFFER_SERVICE_SCHEDULER_H_ |
| 6 #define GPU_COMMAND_BUFFER_SERVICE_SCHEDULER_H_ |
| 7 |
| 8 #include <queue> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/callback.h" |
| 12 #include "base/containers/flat_map.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/synchronization/lock.h" |
| 16 #include "base/threading/thread_checker.h" |
| 17 #include "gpu/command_buffer/common/scheduling_priority.h" |
| 18 #include "gpu/command_buffer/common/sync_token.h" |
| 19 #include "gpu/command_buffer/service/sequence_id.h" |
| 20 #include "gpu/gpu_export.h" |
| 21 |
| 22 namespace base { |
| 23 class SingleThreadTaskRunner; |
| 24 namespace trace_event { |
| 25 class ConvertableToTraceFormat; |
| 26 } |
| 27 } |
| 28 |
| 29 namespace gpu { |
| 30 class SyncPointManager; |
| 31 |
| 32 class GPU_EXPORT Scheduler { |
| 33 public: |
| 34 Scheduler(scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 35 SyncPointManager* sync_point_manager); |
| 36 |
| 37 virtual ~Scheduler(); |
| 38 |
| 39 // Create a sequence with given priority. Returns an identifier for the |
| 40 // sequence that can be used with SyncPonintManager for creating sync point |
| 41 // release clients. Sequences start off as enabled (see |EnableSequence|). |
| 42 SequenceId CreateSequence(SchedulingPriority priority); |
| 43 |
| 44 // Destroy the sequence and run any scheduled tasks immediately. |
| 45 void DestroySequence(SequenceId sequence_id); |
| 46 |
| 47 // Enables the sequence so that its tasks may be scheduled. |
| 48 void EnableSequence(SequenceId sequence_id); |
| 49 |
| 50 // Disables the sequence. |
| 51 void DisableSequence(SequenceId sequence_id); |
| 52 |
| 53 // Schedules task (closure) to run on the sequence. The task is blocked until |
| 54 // the sync token fences are released or determined to be invalid. Tasks are |
| 55 // run in the order in which they are submitted. |
| 56 void ScheduleTask(SequenceId sequence_id, |
| 57 base::OnceClosure closure, |
| 58 const std::vector<SyncToken>& sync_token_fences); |
| 59 |
| 60 // Continue running task on the sequence with the closure. This must be called |
| 61 // while running a previously scheduled task. |
| 62 void ContinueTask(SequenceId sequence_id, base::OnceClosure closure); |
| 63 |
| 64 // If the sequence should yield so that a higher priority sequence may run. |
| 65 bool ShouldYield(SequenceId sequence_id); |
| 66 |
| 67 private: |
| 68 class Sequence; |
| 69 |
| 70 struct SchedulingState { |
| 71 static bool Comparator(const SchedulingState& lhs, |
| 72 const SchedulingState& rhs) { |
| 73 return rhs.RunsBefore(lhs); |
| 74 } |
| 75 |
| 76 SchedulingState(); |
| 77 SchedulingState(const SchedulingState& other); |
| 78 ~SchedulingState(); |
| 79 |
| 80 bool RunsBefore(const SchedulingState& other) const { |
| 81 return std::tie(priority, order_num) < |
| 82 std::tie(other.priority, other.order_num); |
| 83 } |
| 84 |
| 85 std::unique_ptr<base::trace_event::ConvertableToTraceFormat> AsValue() |
| 86 const; |
| 87 |
| 88 SequenceId sequence_id; |
| 89 SchedulingPriority priority = SchedulingPriority::kLowest; |
| 90 uint32_t order_num = 0; |
| 91 }; |
| 92 |
| 93 void SyncTokenFenceReleased(const SyncToken& sync_token, |
| 94 uint32_t order_num, |
| 95 SequenceId release_sequence_id, |
| 96 SequenceId waiting_sequence_id); |
| 97 |
| 98 void TryScheduleSequence(Sequence* sequence); |
| 99 |
| 100 void RebuildSchedulingQueue(); |
| 101 |
| 102 Sequence* GetSequence(SequenceId sequence_id); |
| 103 |
| 104 void RunNextTask(); |
| 105 |
| 106 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 107 |
| 108 SyncPointManager* const sync_point_manager_; |
| 109 |
| 110 mutable base::Lock lock_; |
| 111 |
| 112 // The following are protected by |lock_|. |
| 113 bool running_ = false; |
| 114 |
| 115 base::flat_map<SequenceId, std::unique_ptr<Sequence>> sequences_; |
| 116 |
| 117 // Used as a priority queue for scheduling sequences. Min heap of |
| 118 // SchedulingState with highest priority (lowest order) in front. |
| 119 std::vector<SchedulingState> scheduling_queue_; |
| 120 |
| 121 // If the running sequence should yield so that a higher priority sequence can |
| 122 // run. |
| 123 bool should_yield_ = false; |
| 124 |
| 125 // If the scheduling queue needs to be rebuild because a sequence changed |
| 126 // priority. |
| 127 bool rebuild_scheduling_queue_ = false; |
| 128 |
| 129 base::ThreadChecker thread_checker_; |
| 130 |
| 131 base::WeakPtrFactory<Scheduler> weak_factory_; |
| 132 |
| 133 DISALLOW_COPY_AND_ASSIGN(Scheduler); |
| 134 }; |
| 135 |
| 136 } // namespace gpu |
| 137 |
| 138 #endif // GPU_COMMAND_BUFFER_SERVICE_SCHEDULER_H_ |
OLD | NEW |