Postpone loading the TFLite model until a supported event is recorded.

Bug: 267050081
Test: atest libinput_tests
Change-Id: I09666da123a58786e8a6d47d4c29a475e92f2bbf
diff --git a/libs/input/MotionPredictor.cpp b/libs/input/MotionPredictor.cpp
index 0f889e8..7d11ef2 100644
--- a/libs/input/MotionPredictor.cpp
+++ b/libs/input/MotionPredictor.cpp
@@ -65,9 +65,8 @@
 MotionPredictor::MotionPredictor(nsecs_t predictionTimestampOffsetNanos, const char* modelPath,
                                  std::function<bool()> checkMotionPredictionEnabled)
       : mPredictionTimestampOffsetNanos(predictionTimestampOffsetNanos),
-        mCheckMotionPredictionEnabled(std::move(checkMotionPredictionEnabled)),
-        mModel(TfLiteMotionPredictorModel::create(modelPath == nullptr ? DEFAULT_MODEL_PATH
-                                                                       : modelPath)) {}
+        mModelPath(modelPath == nullptr ? DEFAULT_MODEL_PATH : modelPath),
+        mCheckMotionPredictionEnabled(std::move(checkMotionPredictionEnabled)) {}
 
 void MotionPredictor::record(const MotionEvent& event) {
     if (!isPredictionAvailable(event.getDeviceId(), event.getSource())) {
@@ -76,6 +75,11 @@
         return;
     }
 
+    // Initialise the model now that it's likely to be used.
+    if (!mModel) {
+        mModel = TfLiteMotionPredictorModel::create(mModelPath.c_str());
+    }
+
     TfLiteMotionPredictorBuffers& buffers =
             mDeviceBuffers.try_emplace(event.getDeviceId(), mModel->inputLength()).first->second;
 
@@ -130,6 +134,7 @@
             continue;
         }
 
+        LOG_ALWAYS_FATAL_IF(!mModel);
         buffer.copyTo(*mModel);
         LOG_ALWAYS_FATAL_IF(!mModel->invoke());