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

Commit 90f78f21 authored by Prabir Pradhan's avatar Prabir Pradhan Committed by Android (Google) Code Review
Browse files

Merge "Create InputThread to manage inputflinger threads"

parents c294fddf 5a57cff6
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -83,6 +83,7 @@ cc_library_shared {
    srcs: [
        "InputListener.cpp",
        "InputReaderBase.cpp",
        "InputThread.cpp",
    ],

    shared_libs: [
+60 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "InputThread.h"

namespace android {

namespace {

// Implementation of Thread from libutils.
class InputThreadImpl : public Thread {
public:
    explicit InputThreadImpl(std::function<void()> loop)
          : Thread(/* canCallJava */ true), mThreadLoop(loop) {}

    ~InputThreadImpl() {}

private:
    std::function<void()> mThreadLoop;

    bool threadLoop() override {
        mThreadLoop();
        return true;
    }
};

} // namespace

InputThread::InputThread(std::string name, std::function<void()> loop, std::function<void()> wake)
      : mName(name), mThreadWake(wake) {
    mThread = new InputThreadImpl(loop);
    mThread->run(mName.c_str(), ANDROID_PRIORITY_URGENT_DISPLAY);
}

InputThread::~InputThread() {
    mThread->requestExit();
    if (mThreadWake) {
        mThreadWake();
    }
    mThread->requestExitAndWait();
}

bool InputThread::isCallingThread() {
    return gettid() == mThread->getTid();
}

} // namespace android
 No newline at end of file
+8 −32
Original line number Diff line number Diff line
@@ -325,24 +325,6 @@ static std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inp
    return dispatchEntry;
}

// --- InputDispatcherThread ---

class InputDispatcher::InputDispatcherThread : public Thread {
public:
    explicit InputDispatcherThread(InputDispatcher* dispatcher)
          : Thread(/* canCallJava */ true), mDispatcher(dispatcher) {}

    ~InputDispatcherThread() {}

private:
    InputDispatcher* mDispatcher;

    virtual bool threadLoop() override {
        mDispatcher->dispatchOnce();
        return true;
    }
};

// --- InputDispatcher ---

InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy)
@@ -367,8 +349,6 @@ InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& polic
    mKeyRepeatState.lastKeyEntry = nullptr;

    policy->getDispatcherConfiguration(&mConfig);

    mThread = new InputDispatcherThread(this);
}

InputDispatcher::~InputDispatcher() {
@@ -387,25 +367,21 @@ InputDispatcher::~InputDispatcher() {
}

status_t InputDispatcher::start() {
    if (mThread->isRunning()) {
    if (mThread) {
        return ALREADY_EXISTS;
    }
    return mThread->run("InputDispatcher", PRIORITY_URGENT_DISPLAY);
    mThread = std::make_unique<InputThread>(
            "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); });
    return OK;
}

status_t InputDispatcher::stop() {
    if (!mThread->isRunning()) {
        return OK;
    }
    if (gettid() == mThread->getTid()) {
        ALOGE("InputDispatcher can only be stopped from outside of the InputDispatcherThread!");
    if (mThread && mThread->isCallingThread()) {
        ALOGE("InputDispatcher cannot be stopped from its own thread!");
        return INVALID_OPERATION;
    }
    // Directly calling requestExitAndWait() causes the thread to not exit
    // if mLooper is waiting for a long timeout.
    mThread->requestExit();
    mLooper->wake();
    return mThread->requestExitAndWait();
    mThread.reset();
    return OK;
}

void InputDispatcher::dispatchOnce() {
+2 −2
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
#include "InputDispatcherPolicyInterface.h"
#include "InputState.h"
#include "InputTarget.h"
#include "InputThread.h"
#include "Monitor.h"
#include "TouchState.h"
#include "TouchedWindow.h"
@@ -124,8 +125,7 @@ private:
        STALE,
    };

    class InputDispatcherThread;
    sp<InputDispatcherThread> mThread;
    std::unique_ptr<InputThread> mThread;

    sp<InputDispatcherPolicyInterface> mPolicy;
    android::InputDispatcherConfiguration mConfig;
+46 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _UI_INPUT_THREAD_H
#define _UI_INPUT_THREAD_H

#include <utils/Thread.h>

namespace android {

/* A thread that loops continuously until destructed to process input events.
 *
 * Creating the InputThread starts it immediately. The thread begins looping the loop
 * function until the InputThread is destroyed. The wake function is used to wake anything
 * that sleeps in the loop when it is time for the thread to be destroyed.
 */
class InputThread {
public:
    explicit InputThread(std::string name, std::function<void()> loop,
                         std::function<void()> wake = nullptr);
    virtual ~InputThread();

    bool isCallingThread();

private:
    std::string mName;
    std::function<void()> mThreadWake;
    sp<Thread> mThread;
};

} // namespace android

#endif // _UI_INPUT_THREAD_H
 No newline at end of file
Loading