Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fd0a68e3 authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

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
parent d3fbc898
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -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;
+1 −1
Original line number Diff line number Diff line
@@ -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();

+2 −4
Original line number Diff line number Diff line
@@ -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) {
+12 −3
Original line number Diff line number Diff line
@@ -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;
    }
+6 −13
Original line number Diff line number Diff line
@@ -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));
Loading