Reland "[Dr-Dc]:Gpu Scheduler changes to support multiple threads."

This is a reland of fdffca5138fa5ceba49109824bcfa9ca9d1ac02b after
a fix in TRACE_EVENT* in scheduler which was causing trace events
to not finish.

Original change's description:
> [Dr-Dc]:Gpu Scheduler changes to support multiple threads.
>
> Dr-Dc a.k.a direct-rendering display compositor enables use of
> 2 gpu threads instead of 1 gpu main thread.
>
> This change is to update gpu scheduler to handle multiple gpu threads.
> Primary changes are :
> 1. A sequence can be created on any thread.
> 2. Each thread will have its own priority queue to schedule sequences
> created on that thread.
> 3. GpuInProcessThreadService::CreateSequence() can be called from viz
> thread so make sure its on the gpu thread.
> 4. Update all scheduler unit tests as well as all affected unit tests
> due to scheduler changes.
>
> This change should not affect any functional or performance changes
> to current/legacy features.
>
> Bug: 1186278
> Change-Id: Icfa9277b907a59b744e2598f2d057c3389616932
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2748416
> Reviewed-by: kylechar <kylechar@chromium.org>
> Reviewed-by: Sunny Sachanandani <sunnyps@chromium.org>
> Commit-Queue: vikas soni <vikassoni@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#872554}

Bug: 1186278
Change-Id: I69ad5c837876bbb5f374e379ca3d7d48a4289f1b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2827523
Reviewed-by: kylechar <kylechar@chromium.org>
Reviewed-by: Sunny Sachanandani <sunnyps@chromium.org>
Commit-Queue: vikas soni <vikassoni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#883101}
diff --git a/gpu/command_buffer/service/scheduler.h b/gpu/command_buffer/service/scheduler.h
index 427dce8..92cdcbb 100644
--- a/gpu/command_buffer/service/scheduler.h
+++ b/gpu/command_buffer/service/scheduler.h
@@ -16,7 +16,6 @@
 #include "base/memory/ref_counted.h"
 #include "base/memory/weak_ptr.h"
 #include "base/synchronization/lock.h"
-#include "base/threading/thread_checker.h"
 #include "base/time/time.h"
 #include "gpu/command_buffer/common/command_buffer_id.h"
 #include "gpu/command_buffer/common/scheduling_priority.h"
@@ -57,14 +56,13 @@
     ReportingCallback report_callback;
   };
 
-  Scheduler(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
-            SyncPointManager* sync_point_manager,
+  Scheduler(SyncPointManager* sync_point_manager,
             const GpuPreferences& gpu_preferences);
 
-  virtual ~Scheduler();
+  ~Scheduler();
 
   // Create a sequence with given priority. Returns an identifier for the
-  // sequence that can be used with SyncPonintManager for creating sync point
+  // sequence that can be used with SyncPointManager for creating sync point
   // release clients. Sequences start off as enabled (see |EnableSequence|).
   // Sequence could be created outside of GPU thread.
   SequenceId CreateSequence(SchedulingPriority priority);
@@ -102,15 +100,14 @@
   // If the sequence should yield so that a higher priority sequence may run.
   bool ShouldYield(SequenceId sequence_id);
 
-  base::WeakPtr<Scheduler> AsWeakPtr();
-
   // Takes and resets current accumulated blocking time. Not available on all
   // platforms. Must be enabled with --enable-gpu-blocked-time.
   // Returns TimeDelta::Min() when not available.
   base::TimeDelta TakeTotalBlockingTime();
 
- private:
+  base::SingleThreadTaskRunner* GetTaskRunnerForTesting(SequenceId sequence_id);
 
+ private:
   struct SchedulingState {
     static bool Comparator(const SchedulingState& lhs,
                            const SchedulingState& rhs) {
@@ -138,6 +135,7 @@
    public:
     Sequence(Scheduler* scheduler,
              SequenceId sequence_id,
+             base::PlatformThreadId thread_id,
              SchedulingPriority priority,
              scoped_refptr<SyncPointOrderData> order_data);
 
@@ -149,6 +147,8 @@
       return order_data_;
     }
 
+    base::PlatformThreadId thread_id() const { return thread_id_; }
+
     bool enabled() const { return enabled_; }
 
     bool scheduled() const { return running_state_ == SCHEDULED; }
@@ -312,6 +312,7 @@
 
     Scheduler* const scheduler_;
     const SequenceId sequence_id_;
+    const base::PlatformThreadId thread_id_;
 
     const SchedulingPriority default_priority_;
     SchedulingPriority current_priority_;
@@ -347,44 +348,44 @@
 
   void TryScheduleSequence(Sequence* sequence);
 
-  void RebuildSchedulingQueue();
+  // If the scheduling queue needs to be rebuild because a sequence changed
+  // priority.
+  std::vector<SchedulingState>& RebuildSchedulingQueueIfNeeded(
+      base::PlatformThreadId thread_id);
 
   Sequence* GetSequence(SequenceId sequence_id);
 
   void RunNextTask();
 
-  scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
-
   SyncPointManager* const sync_point_manager_;
-
   mutable base::Lock lock_;
+  base::flat_map<SequenceId, std::unique_ptr<Sequence>> sequence_map_
+      GUARDED_BY(lock_);
 
-  // The following are protected by |lock_|.
-  bool running_ = false;
+  // Each thread will have its own priority queue to schedule sequences
+  // created on that thread.
+  struct PerThreadState {
+    PerThreadState();
+    PerThreadState(PerThreadState&&);
+    ~PerThreadState();
+    PerThreadState& operator=(PerThreadState&&);
+    // Used as a priority queue for scheduling sequences. Min heap of
+    // SchedulingState with highest priority (lowest order) in front.
+    std::vector<SchedulingState> scheduling_queue;
+    bool rebuild_scheduling_queue = false;
 
-  base::flat_map<SequenceId, std::unique_ptr<Sequence>> sequences_;
-
-  // Used as a priority queue for scheduling sequences. Min heap of
-  // SchedulingState with highest priority (lowest order) in front.
-  std::vector<SchedulingState> scheduling_queue_;
-
-  // If the scheduling queue needs to be rebuild because a sequence changed
-  // priority.
-  bool rebuild_scheduling_queue_ = false;
+    scoped_refptr<base::SingleThreadTaskRunner> task_runner;
+    bool running = false;
+  };
+  base::flat_map<base::PlatformThreadId, PerThreadState> per_thread_state_map_;
 
   // Accumulated time the thread was blocked during running task
   base::TimeDelta total_blocked_time_;
   const bool blocked_time_collection_enabled_;
 
-  base::ThreadChecker thread_checker_;
-
   // Indicate when the next task run was scheduled
   base::TimeTicks run_next_task_scheduled_;
 
-  // Invalidated on main thread.
-  base::WeakPtr<Scheduler> weak_ptr_;
-  base::WeakPtrFactory<Scheduler> weak_factory_{this};
-
  private:
   FRIEND_TEST_ALL_PREFIXES(SchedulerTest, StreamPriorities);
   FRIEND_TEST_ALL_PREFIXES(SchedulerTest, StreamDestroyRemovesPriorities);