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

Commit 5b75da13 authored by Siarhei Vishniakou's avatar Siarhei Vishniakou Committed by Automerger Merge Worker
Browse files

Use current directory to load prediction model am: fd0a68e3 am: 8e6c21d2

parents 87d01fc7 8e6c21d2
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