diff options
author | 2023-02-28 13:25:36 -0800 | |
---|---|---|
committer | 2023-03-04 21:51:07 +0000 | |
commit | fd0a68e34b72b7a1daa07f0e5ed0a167d596a343 (patch) | |
tree | 96160d4a5d04fc1ca850f0244cec681e3defdd8f | |
parent | d3fbc898a508441a7fe2796a260ada810da5d415 (diff) |
Use current directory to load prediction model
Before this CL, the testing binary could not execute properly when
invoked directly. This is because the current working directory does not
get set.
To fix this, use the model file from /system/etc when __ANDROID__ is
defined, and refer to the current directory for all other cases.
Test: m libinput_tests && $ANDROID_HOST_OUT/nativetest64/libinput_tests/libinput_tests
Test: atest libinput_tests
Test: atest --host libinput_tests
Bug: 271134652
Change-Id: I0547ca9666eab153208a581fb60f6bef656b946a
-rw-r--r-- | include/input/MotionPredictor.h | 3 | ||||
-rw-r--r-- | include/input/TfLiteMotionPredictor.h | 2 | ||||
-rw-r--r-- | libs/input/MotionPredictor.cpp | 6 | ||||
-rw-r--r-- | libs/input/TfLiteMotionPredictor.cpp | 15 | ||||
-rw-r--r-- | libs/input/tests/MotionPredictor_test.cpp | 19 | ||||
-rw-r--r-- | libs/input/tests/TfLiteMotionPredictor_test.cpp | 18 |
6 files changed, 25 insertions, 38 deletions
diff --git a/include/input/MotionPredictor.h b/include/input/MotionPredictor.h index fdaffc8c5d..de8ddcabeb 100644 --- a/include/input/MotionPredictor.h +++ b/include/input/MotionPredictor.h @@ -67,7 +67,7 @@ public: * checkEnableMotionPredition: the function to check whether the prediction should run. Used to * provide an additional way of turning prediction on and off. Can be toggled at runtime. */ - MotionPredictor(nsecs_t predictionTimestampOffsetNanos, const char* modelPath = nullptr, + MotionPredictor(nsecs_t predictionTimestampOffsetNanos, std::function<bool()> checkEnableMotionPrediction = isMotionPredictionEnabled); /** * Record the actual motion received by the view. This event will be used for calculating the @@ -82,7 +82,6 @@ public: private: const nsecs_t mPredictionTimestampOffsetNanos; - const std::string mModelPath; const std::function<bool()> mCheckMotionPredictionEnabled; std::unique_ptr<TfLiteMotionPredictorModel> mModel; diff --git a/include/input/TfLiteMotionPredictor.h b/include/input/TfLiteMotionPredictor.h index 54e2851a7a..7de551b417 100644 --- a/include/input/TfLiteMotionPredictor.h +++ b/include/input/TfLiteMotionPredictor.h @@ -99,7 +99,7 @@ private: class TfLiteMotionPredictorModel { public: // Creates a model from an encoded Flatbuffer model. - static std::unique_ptr<TfLiteMotionPredictorModel> create(const char* modelPath); + static std::unique_ptr<TfLiteMotionPredictorModel> create(); ~TfLiteMotionPredictorModel(); diff --git a/libs/input/MotionPredictor.cpp b/libs/input/MotionPredictor.cpp index 4b36eae6f6..b4151c6ea1 100644 --- a/libs/input/MotionPredictor.cpp +++ b/libs/input/MotionPredictor.cpp @@ -35,7 +35,6 @@ namespace android { namespace { -const char DEFAULT_MODEL_PATH[] = "/system/etc/motion_predictor_model.fb"; const int64_t PREDICTION_INTERVAL_NANOS = 12500000 / 3; // TODO(b/266747937): Get this from the model. @@ -62,10 +61,9 @@ TfLiteMotionPredictorSample::Point convertPrediction( // --- MotionPredictor --- -MotionPredictor::MotionPredictor(nsecs_t predictionTimestampOffsetNanos, const char* modelPath, +MotionPredictor::MotionPredictor(nsecs_t predictionTimestampOffsetNanos, std::function<bool()> checkMotionPredictionEnabled) : mPredictionTimestampOffsetNanos(predictionTimestampOffsetNanos), - mModelPath(modelPath == nullptr ? DEFAULT_MODEL_PATH : modelPath), mCheckMotionPredictionEnabled(std::move(checkMotionPredictionEnabled)) {} android::base::Result<void> MotionPredictor::record(const MotionEvent& event) { @@ -86,7 +84,7 @@ android::base::Result<void> MotionPredictor::record(const MotionEvent& event) { // Initialise the model now that it's likely to be used. if (!mModel) { - mModel = TfLiteMotionPredictorModel::create(mModelPath.c_str()); + mModel = TfLiteMotionPredictorModel::create(); } if (mBuffers == nullptr) { diff --git a/libs/input/TfLiteMotionPredictor.cpp b/libs/input/TfLiteMotionPredictor.cpp index 10510d675c..691e87c366 100644 --- a/libs/input/TfLiteMotionPredictor.cpp +++ b/libs/input/TfLiteMotionPredictor.cpp @@ -30,6 +30,7 @@ #include <type_traits> #include <utility> +#include <android-base/file.h> #include <android-base/logging.h> #include <android-base/mapped_file.h> #define ATRACE_TAG ATRACE_TAG_INPUT @@ -60,6 +61,14 @@ constexpr char OUTPUT_R[] = "r"; constexpr char OUTPUT_PHI[] = "phi"; constexpr char OUTPUT_PRESSURE[] = "pressure"; +std::string getModelPath() { +#if defined(__ANDROID__) + return "/system/etc/motion_predictor_model.fb"; +#else + return base::GetExecutableDirectory() + "/motion_predictor_model.fb"; +#endif +} + // A TFLite ErrorReporter that logs to logcat. class LoggingErrorReporter : public tflite::ErrorReporter { public: @@ -206,9 +215,9 @@ void TfLiteMotionPredictorBuffers::pushSample(int64_t timestamp, mInputOrientation.pushBack(orientation); } -std::unique_ptr<TfLiteMotionPredictorModel> TfLiteMotionPredictorModel::create( - const char* modelPath) { - const int fd = open(modelPath, O_RDONLY); +std::unique_ptr<TfLiteMotionPredictorModel> TfLiteMotionPredictorModel::create() { + const std::string modelPath = getModelPath(); + const int fd = open(modelPath.c_str(), O_RDONLY); if (fd == -1) { PLOG(FATAL) << "Could not read model from " << modelPath; } diff --git a/libs/input/tests/MotionPredictor_test.cpp b/libs/input/tests/MotionPredictor_test.cpp index 73abac4ca8..c61efbf9ed 100644 --- a/libs/input/tests/MotionPredictor_test.cpp +++ b/libs/input/tests/MotionPredictor_test.cpp @@ -30,13 +30,6 @@ using ::testing::IsEmpty; using ::testing::SizeIs; using ::testing::UnorderedElementsAre; -const char MODEL_PATH[] = -#if defined(__ANDROID__) - "/system/etc/motion_predictor_model.fb"; -#else - "motion_predictor_model.fb"; -#endif - constexpr int32_t DOWN = AMOTION_EVENT_ACTION_DOWN; constexpr int32_t MOVE = AMOTION_EVENT_ACTION_MOVE; constexpr int32_t UP = AMOTION_EVENT_ACTION_UP; @@ -73,14 +66,14 @@ static MotionEvent getMotionEvent(int32_t action, float x, float y, } TEST(MotionPredictorTest, IsPredictionAvailable) { - MotionPredictor predictor(/*predictionTimestampOffsetNanos=*/0, MODEL_PATH, + MotionPredictor predictor(/*predictionTimestampOffsetNanos=*/0, []() { return true /*enable prediction*/; }); ASSERT_TRUE(predictor.isPredictionAvailable(/*deviceId=*/1, AINPUT_SOURCE_STYLUS)); ASSERT_FALSE(predictor.isPredictionAvailable(/*deviceId=*/1, AINPUT_SOURCE_TOUCHSCREEN)); } TEST(MotionPredictorTest, Offset) { - MotionPredictor predictor(/*predictionTimestampOffsetNanos=*/1, MODEL_PATH, + MotionPredictor predictor(/*predictionTimestampOffsetNanos=*/1, []() { return true /*enable prediction*/; }); predictor.record(getMotionEvent(DOWN, 0, 1, 30ms)); predictor.record(getMotionEvent(MOVE, 0, 2, 35ms)); @@ -90,7 +83,7 @@ TEST(MotionPredictorTest, Offset) { } TEST(MotionPredictorTest, FollowsGesture) { - MotionPredictor predictor(/*predictionTimestampOffsetNanos=*/0, MODEL_PATH, + MotionPredictor predictor(/*predictionTimestampOffsetNanos=*/0, []() { return true /*enable prediction*/; }); // MOVE without a DOWN is ignored. @@ -107,7 +100,7 @@ TEST(MotionPredictorTest, FollowsGesture) { } TEST(MotionPredictorTest, MultipleDevicesNotSupported) { - MotionPredictor predictor(/*predictionTimestampOffsetNanos=*/0, MODEL_PATH, + MotionPredictor predictor(/*predictionTimestampOffsetNanos=*/0, []() { return true /*enable prediction*/; }); ASSERT_TRUE(predictor.record(getMotionEvent(DOWN, 1, 3, 0ms, /*deviceId=*/0)).ok()); @@ -120,7 +113,7 @@ TEST(MotionPredictorTest, MultipleDevicesNotSupported) { } TEST(MotionPredictorTest, IndividualGesturesFromDifferentDevicesAreSupported) { - MotionPredictor predictor(/*predictionTimestampOffsetNanos=*/0, MODEL_PATH, + MotionPredictor predictor(/*predictionTimestampOffsetNanos=*/0, []() { return true /*enable prediction*/; }); ASSERT_TRUE(predictor.record(getMotionEvent(DOWN, 1, 3, 0ms, /*deviceId=*/0)).ok()); @@ -135,7 +128,7 @@ TEST(MotionPredictorTest, IndividualGesturesFromDifferentDevicesAreSupported) { } TEST(MotionPredictorTest, FlagDisablesPrediction) { - MotionPredictor predictor(/*predictionTimestampOffsetNanos=*/0, MODEL_PATH, + MotionPredictor predictor(/*predictionTimestampOffsetNanos=*/0, []() { return false /*disable prediction*/; }); predictor.record(getMotionEvent(DOWN, 0, 1, 30ms)); predictor.record(getMotionEvent(MOVE, 0, 1, 35ms)); diff --git a/libs/input/tests/TfLiteMotionPredictor_test.cpp b/libs/input/tests/TfLiteMotionPredictor_test.cpp index 454f2aaac4..6e76ac1e52 100644 --- a/libs/input/tests/TfLiteMotionPredictor_test.cpp +++ b/libs/input/tests/TfLiteMotionPredictor_test.cpp @@ -21,7 +21,6 @@ #include <iterator> #include <string> -#include <android-base/file.h> #include <gmock/gmock.h> #include <gtest/gtest.h> #include <input/TfLiteMotionPredictor.h> @@ -33,14 +32,6 @@ using ::testing::Each; using ::testing::ElementsAre; using ::testing::FloatNear; -std::string getModelPath() { -#if defined(__ANDROID__) - return "/system/etc/motion_predictor_model.fb"; -#else - return base::GetExecutableDirectory() + "/motion_predictor_model.fb"; -#endif -} - TEST(TfLiteMotionPredictorTest, BuffersReadiness) { TfLiteMotionPredictorBuffers buffers(/*inputLength=*/5); ASSERT_FALSE(buffers.isReady()); @@ -92,8 +83,7 @@ TEST(TfLiteMotionPredictorTest, BuffersRecentData) { } TEST(TfLiteMotionPredictorTest, BuffersCopyTo) { - std::unique_ptr<TfLiteMotionPredictorModel> model = - TfLiteMotionPredictorModel::create(getModelPath().c_str()); + std::unique_ptr<TfLiteMotionPredictorModel> model = TfLiteMotionPredictorModel::create(); TfLiteMotionPredictorBuffers buffers(model->inputLength()); buffers.pushSample(/*timestamp=*/1, @@ -137,8 +127,7 @@ TEST(TfLiteMotionPredictorTest, BuffersCopyTo) { } TEST(TfLiteMotionPredictorTest, ModelInputOutputLength) { - std::unique_ptr<TfLiteMotionPredictorModel> model = - TfLiteMotionPredictorModel::create(getModelPath().c_str()); + std::unique_ptr<TfLiteMotionPredictorModel> model = TfLiteMotionPredictorModel::create(); ASSERT_GT(model->inputLength(), 0u); const int inputLength = model->inputLength(); @@ -155,8 +144,7 @@ TEST(TfLiteMotionPredictorTest, ModelInputOutputLength) { } TEST(TfLiteMotionPredictorTest, ModelOutput) { - std::unique_ptr<TfLiteMotionPredictorModel> model = - TfLiteMotionPredictorModel::create(getModelPath().c_str()); + std::unique_ptr<TfLiteMotionPredictorModel> model = TfLiteMotionPredictorModel::create(); TfLiteMotionPredictorBuffers buffers(model->inputLength()); buffers.pushSample(/*timestamp=*/1, {.position = {.x = 100, .y = 200}, .pressure = 0.2}); |