Make tracking_id const inside PalmFilterStroke

An instance of PalmFilterStroke is only supposed to be used for a specific tracking_id. This is enforced by checking (and crashing) that
the tracking_id of the slot always matches the current tracking_id.

The api 'SetTrackingId' is used to establish this for the stroke, but
it's easy to misuse, because there's no way to force the user to always set it before adding a sample.

To simplify the code, provide the tracking_id in the constructor and
make it const. This way, the user is forced to decide what the value
will be for the lifetime of the object, and won't be able to change it
after the fact.

Bug: 198472780
Test: (on android) atest libpalmrejection_test
Test: autoninja -C out/Default evdev
Merged-In: Ia3f281946ffa82ea48c3f4c32f9563b294e9221a
Change-Id: Ia3f281946ffa82ea48c3f4c32f9563b294e9221a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3328909
Reviewed-by: Xinglong Luan <alanlxl@chromium.org>
Commit-Queue: Siarhei Vishniakou <svv@google.com>
Cr-Commit-Position: refs/heads/main@{#1024112}
(cherry picked from commit 49b0d6a8296303c4dc497b5e1d30399b2002328f)
diff --git a/ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter.cc b/ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter.cc
index 55637dc..1e5724c 100644
--- a/ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter.cc
+++ b/ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter.cc
@@ -161,8 +161,8 @@
       DCHECK(strokes_.count(tracking_id) == 0)
           << " Tracking id " << tracking_id;
 
-      strokes_.emplace(tracking_id, PalmFilterStroke(model_->config()));
-      strokes_.find(tracking_id)->second.SetTrackingId(tracking_id);
+      strokes_.emplace(tracking_id,
+                       PalmFilterStroke(model_->config(), tracking_id));
       tracking_ids_[slot] = tracking_id;
       is_palm_.set(slot, false);
       is_delay_.set(slot, false);
@@ -244,7 +244,7 @@
       LOG(DFATAL) << "Unable to find marked stroke.";
       continue;
     }
-    auto& stroke = lookup->second;
+    const auto& stroke = lookup->second;
     if (stroke.samples_seen() < model_->config().min_sample_count) {
       // in very short strokes: we use a heuristic.
       is_palm_.set(slot, IsHeuristicPalmStroke(stroke));
diff --git a/ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter_util.cc b/ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter_util.cc
index 042f361..d1813ef 100644
--- a/ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter_util.cc
+++ b/ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter_util.cc
@@ -142,17 +142,16 @@
 }
 
 PalmFilterStroke::PalmFilterStroke(
-    const NeuralStylusPalmDetectionFilterModelConfig& model_config)
-    : max_sample_count_(model_config.max_sample_count),
+    const NeuralStylusPalmDetectionFilterModelConfig& model_config,
+    int tracking_id)
+    : tracking_id_(tracking_id),
+      max_sample_count_(model_config.max_sample_count),
       resample_period_(model_config.resample_period) {}
 PalmFilterStroke::PalmFilterStroke(const PalmFilterStroke& other) = default;
 PalmFilterStroke::PalmFilterStroke(PalmFilterStroke&& other) = default;
 PalmFilterStroke::~PalmFilterStroke() {}
 
 void PalmFilterStroke::ProcessSample(const PalmFilterSample& sample) {
-  if (samples_seen_ == 0) {
-    tracking_id_ = sample.tracking_id;
-  }
   DCHECK_EQ(tracking_id_, sample.tracking_id);
   if (resample_period_.has_value()) {
     Resample(sample);
@@ -231,10 +230,6 @@
   return samples_seen_;
 }
 
-void PalmFilterStroke::SetTrackingId(int tracking_id) {
-  tracking_id_ = tracking_id;
-}
-
 float PalmFilterStroke::MaxMajorRadius() const {
   float maximum = 0.0;
   for (const auto& sample : samples_) {
diff --git a/ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter_util.h b/ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter_util.h
index 2486078..47f0c33 100644
--- a/ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter_util.h
+++ b/ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter_util.h
@@ -74,7 +74,8 @@
 class COMPONENT_EXPORT(EVDEV) PalmFilterStroke {
  public:
   explicit PalmFilterStroke(
-      const NeuralStylusPalmDetectionFilterModelConfig& model_config);
+      const NeuralStylusPalmDetectionFilterModelConfig& model_config,
+      int tracking_id);
   PalmFilterStroke(const PalmFilterStroke& other);
   PalmFilterStroke(PalmFilterStroke&& other);
   ~PalmFilterStroke();
@@ -84,7 +85,6 @@
   float BiggestSize() const;
   // If no elements in stroke, returns 0.0;
   float MaxMajorRadius() const;
-  void SetTrackingId(int tracking_id);
   const std::deque<PalmFilterSample>& samples() const;
   uint64_t samples_seen() const;
   int tracking_id() const;
@@ -98,7 +98,7 @@
   void Resample(const PalmFilterSample& sample);
 
   std::deque<PalmFilterSample> samples_;
-  int tracking_id_ = 0;
+  const int tracking_id_;
   /**
    * How many total samples have been reported for this stroke. This is
    * different from samples_.size() because samples_ will get pruned to only
diff --git a/ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter_util_unittest.cc b/ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter_util_unittest.cc
index 41364ae..ac3c841 100644
--- a/ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter_util_unittest.cc
+++ b/ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter_util_unittest.cc
@@ -234,8 +234,8 @@
 }
 
 TEST_P(NeuralStylusPalmDetectionFilterUtilTest, PalmFilterStrokeTest) {
-  PalmFilterStroke stroke(model_config_);
-  EXPECT_EQ(0, stroke.tracking_id());
+  PalmFilterStroke stroke(model_config_, /*tracking_id*/ 55);
+  touch_.tracking_id = 55;
   // With no points, center is 0.
   EXPECT_EQ(gfx::PointF(0., 0.), stroke.GetCentroid());
 
@@ -272,14 +272,13 @@
     ASSERT_FLOAT_EQ(expected_centroid.x(), stroke.GetCentroid().x())
         << "failed at i " << i;
   }
-  stroke.SetTrackingId(55);
-  EXPECT_EQ(55, stroke.tracking_id());
 }
 
 TEST_P(NeuralStylusPalmDetectionFilterUtilTest,
        PalmFilterStrokeBiggestSizeTest) {
-  PalmFilterStroke stroke(model_config_);
-  PalmFilterStroke no_minor_stroke(model_config_);
+  PalmFilterStroke stroke(model_config_, /*tracking_id*/ 0);
+  PalmFilterStroke no_minor_stroke(model_config_, /*tracking_id*/ 0);
+  touch_.tracking_id = stroke.tracking_id();
   EXPECT_EQ(0, stroke.BiggestSize());
 
   base::TimeTicks time = base::TimeTicks() + base::Seconds(30);
@@ -327,7 +326,8 @@
 }
 
 TEST_P(NeuralStylusPalmDetectionFilterUtilTest, StrokeGetMaxMajorTest) {
-  PalmFilterStroke stroke(model_config_);
+  PalmFilterStroke stroke(model_config_, /*tracking_id*/ 0);
+  touch_.tracking_id = stroke.tracking_id();
   EXPECT_FLOAT_EQ(0, stroke.MaxMajorRadius());
   base::TimeTicks time = base::TimeTicks::UnixEpoch() + base::Seconds(30);
   const PalmFilterDeviceInfo nocturne_distilled =
@@ -377,7 +377,7 @@
   model_config_.resample_period = base::Milliseconds(8);
   base::TimeTicks down_time = base::TimeTicks::UnixEpoch() + base::Seconds(30);
 
-  PalmFilterStroke stroke(model_config_);
+  PalmFilterStroke stroke(model_config_, /*tracking_id*/ 0);
   const PalmFilterDeviceInfo device_info;
 
   // Initially, no samples
@@ -386,6 +386,7 @@
 
   // Add first sample at time = T
   InProgressTouchEvdev touch_;
+  touch_.tracking_id = stroke.tracking_id();
   PalmFilterSample sample =
       CreatePalmFilterSample(touch_, down_time, model_config_, device_info);
   stroke.ProcessSample(sample);
@@ -416,12 +417,13 @@
   model_config_.max_sample_count = 3;
   model_config_.resample_period = base::Milliseconds(8);
 
-  PalmFilterStroke stroke(model_config_);
+  PalmFilterStroke stroke(model_config_, /*tracking_id*/ 0);
   PalmFilterDeviceInfo device_info;
   device_info.minor_radius_supported = true;
 
   // Add first sample at time = T
   InProgressTouchEvdev touch_;
+  touch_.tracking_id = stroke.tracking_id();
   touch_.x = 1;
   touch_.y = 2;
   touch_.major = 4;
@@ -471,12 +473,13 @@
   model_config_.max_sample_count = 3;
   model_config_.resample_period = base::Milliseconds(8);
 
-  PalmFilterStroke stroke(model_config_);
+  PalmFilterStroke stroke(model_config_, /*tracking_id*/ 0);
   PalmFilterDeviceInfo device_info;
   device_info.minor_radius_supported = true;
 
   // Add first sample at time = T
   InProgressTouchEvdev touch_;
+  touch_.tracking_id = stroke.tracking_id();
   touch_.x = 0;
   touch_.y = 10;
   touch_.major = 200;