drm_hwcomposer: Simplify DrmAtomicStateManager thread usage

1. Remove PresentTracker class.
2. Use separate mutex for thread synchronization. Before, we have always
   experienced rescheduling due to calling condvar::notify inside the
   mutex, which consumes some number of CPU cycles.

Signed-off-by: Roman Stratiienko <r.stratiienko@gmail.com>
diff --git a/drm/DrmAtomicStateManager.h b/drm/DrmAtomicStateManager.h
index b0b85ac..5f19bcc 100644
--- a/drm/DrmAtomicStateManager.h
+++ b/drm/DrmAtomicStateManager.h
@@ -14,16 +14,12 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_DRM_ATOMIC_STATE_MANAGER_H_
-#define ANDROID_DRM_ATOMIC_STATE_MANAGER_H_
+#pragma once
 
 #include <pthread.h>
 
-#include <functional>
 #include <memory>
 #include <optional>
-#include <sstream>
-#include <tuple>
 
 #include "compositor/DrmKmsPlan.h"
 #include "compositor/LayerData.h"
@@ -49,50 +45,26 @@
   }
 };
 
-class PresentTrackerThread {
- public:
-  explicit PresentTrackerThread(DrmAtomicStateManager *st_man);
-
-  ~PresentTrackerThread();
-
-  void Stop() {
-    /* Exit thread by signalling that object is no longer valid */
-    st_man_ = nullptr;
-    Notify();
-    pt_.detach();
-  }
-
-  void Notify() {
-    cv_.notify_all();
-  }
-
- private:
-  DrmAtomicStateManager *st_man_{};
-
-  void PresentTrackerThreadFn();
-
-  std::condition_variable cv_;
-  std::thread pt_;
-  std::mutex *mutex_;
-};
-
 class DrmAtomicStateManager {
-  friend class PresentTrackerThread;
-
  public:
-  explicit DrmAtomicStateManager(DrmDisplayPipeline *pipe)
-      : pipe_(pipe),
-        ptt_(std::make_unique<PresentTrackerThread>(this).release()){};
+  static auto CreateInstance(DrmDisplayPipeline *pipe)
+      -> std::shared_ptr<DrmAtomicStateManager>;
 
-  DrmAtomicStateManager(const DrmAtomicStateManager &) = delete;
-  ~DrmAtomicStateManager() {
-    ptt_->Stop();
-  }
+  ~DrmAtomicStateManager() = default;
 
   auto ExecuteAtomicCommit(AtomicCommitArgs &args) -> int;
   auto ActivateDisplayUsingDPMS() -> int;
 
+  void StopThread() {
+    {
+      const std::unique_lock lock(mutex_);
+      exit_thread_ = true;
+    }
+    cv_.notify_all();
+  }
+
  private:
+  DrmAtomicStateManager() = default;
   auto CommitFrame(AtomicCommitArgs &args) -> int;
 
   struct KmsState {
@@ -118,18 +90,19 @@
     };
   }
 
-  DrmDisplayPipeline *const pipe_;
+  DrmDisplayPipeline *pipe_{};
 
   void CleanupPriorFrameResources();
 
-  /* Present (swap) tracking */
-  PresentTrackerThread *ptt_;
   KmsState staged_frame_state_;
   UniqueFd last_present_fence_;
   int frames_staged_{};
   int frames_tracked_{};
+
+  void ThreadFn(const std::shared_ptr<DrmAtomicStateManager> &dasm);
+  std::condition_variable cv_;
+  std::mutex mutex_;
+  bool exit_thread_{};
 };
 
 }  // namespace android
-
-#endif  // ANDROID_DRM_DISPLAY_COMPOSITOR_H_