Properly initialize MotionClassifier
InputClassifier will now play a more active role in managing the
lifecycle of MotionClassifier.
In the previous version of the code, we had a circular reference:
MotionClassifier owned a wp<InputClassifier>, that sometimes got
promoted to sp<>.
But the InputClassifier was the official owner of MotionClassifier, and
owned a unique_ptr<MotionClassifier>.
The owner of InputClassifier in real code is InputManager, and in test
code, it's the test class. When the owner of InputClassifier destroyed
InputClassifier, this could sometimes happen at the time when the
MotionClassifier also held a reference to the InputClassifier. That
meant that the proper owner of InputClassifier was not invoking the
destructor.
Instead, the MotionClassifier was invoking the InputClassifier
destructor.
To fix the situation, we now do the following:
1. InputClassifier will never die before MotionClassifier.
2. MotionClassifier constructor is now allowed to block. It would block
for some time because calling getService takes a while. To account for
this, InputClassifier launches a new thread to create MotionClassifier.
3. When MotionClassifier is ready to process events, InputClassifier
updates mMotionClassifier, which makes it non-null.
4. We now create a separate death recipient, which is co-owned by
InputClassifier and MotionClassifier. This is done so that the refcount
of the deathrecipient does not affect the refcount of InputClassifier,
and thus enforces the ownership of InputClassifier by the InputManager.
Now, no one can call ~InputClassifier except for its real owner.
5. MotionClassifier will subscribe the death recipient to the death of the
HAL. InputClassifier will delete MotionClassifier if HAL dies.
MotionClassifier no longer holds on to the death recipient.
6. We move the loop of the MotionClassifier thread to focus only on
processing events. That thread will no longer do any initialization.
7. Remove the thread check inside MotionClassifier. It isn't really
useful, now that there's only 1 function for the entire thread.
Ownership summary: Both InputClassifier and MotionClassifier own
DeathRecipient. DeathRecipient has a reference to InputClassifier. Thus,
we must guarantee that DeathRecipient dies before InputClassifier.
InputClassifier owns MotionClassifier. This is OK, since even if
InputClassifier dies, it will first delete MotionClassifier. That will
cause MotionClassifier to release 1 refCount from DeathRecipient. That
means the only reference remaining to DeathRecipient will be inside
InputClassifier, so InputClassifier will always be alive until
DeathRecipient is dead. Similar argument applies if MotionClassifier and
DeathRecipient die in different order (as observed from
InputClassifier).
Tests:
1) Manually running inputflinger_tests on cuttlefish:
build/launch cuttlefish using go/acloud
m inputflinger_tests
adb push out/target/product/vsoc_x86/data/nativetest/inputflinger_tests/inputflinger_tests /data/nativetest/inputflinger_tests/inputflinger_tests
adb shell
/data/nativetest/inputflinger_tests # ./inputflinger_tests --gtest_filter=*InputClassifierTest* --gtest_repeat=1000 --gtest_break_on_failure
2) Boot flame and open logcat. Observe in logcat:
StartInputManagerService took to complete: 2ms
Previously, in synchronous approach ( b/130184032) it was
about 100 ms (so we did not regress).
3) Kill the HAL while system_server is running, and dumpsys input before
and after:
adb shell dumpsys input (observe that MotionClassifier is non-null on flame)
adb shell -t killall android.hardware.input.classifier@1.0-service
adb shell dumpsys input (observe that MotionCLassifier is null)
Bug: 148311342
Test: see "Tests" section above
Change-Id: Ic76b82bd5f2cd374e3b001400eb495ca36de7353
Merged-In: Ic76b82bd5f2cd374e3b001400eb495ca36de7353
(cherry picked from commit 1652397ab77904092509a0315ad026f0b4f10a18)
diff --git a/services/inputflinger/InputClassifier.h b/services/inputflinger/InputClassifier.h
index 47e20db..b1769b9 100644
--- a/services/inputflinger/InputClassifier.h
+++ b/services/inputflinger/InputClassifier.h
@@ -19,8 +19,8 @@
#include <android-base/thread_annotations.h>
#include <utils/RefBase.h>
-#include <unordered_map>
#include <thread>
+#include <unordered_map>
#include "BlockingQueue.h"
#include "InputListener.h"
@@ -113,23 +113,23 @@
*/
class MotionClassifier final : public MotionClassifierInterface {
public:
- /**
- * The deathRecipient will be subscribed to the HAL death. If the death recipient
- * owns MotionClassifier and receives HAL death, it should delete its copy of it.
- * The callback serviceDied will also be sent if the MotionClassifier itself fails
- * to initialize. If the MotionClassifier fails to initialize, it is not useful, and
- * should be deleted.
- * If no death recipient is supplied, then the registration step will be skipped, so there will
- * be no listeners registered for the HAL death. This is useful for testing
- * MotionClassifier in isolation.
+ /*
+ * Create an instance of MotionClassifier.
+ * The death recipient, if provided, will be subscribed to the HAL death.
+ * The death recipient could be used to destroy MotionClassifier.
+ *
+ * This function should be called asynchronously, because getService takes a long time.
*/
- explicit MotionClassifier(sp<android::hardware::hidl_death_recipient> deathRecipient = nullptr);
+ static std::unique_ptr<MotionClassifierInterface> create(
+ sp<android::hardware::hidl_death_recipient> deathRecipient);
+
~MotionClassifier();
/**
* Classifies events asynchronously; that is, it doesn't block events on a classification,
- * but instead sends them over to the classifier HAL and after a classification is
- * determined, it then marks the next event it sees in the stream with it.
+ * but instead sends them over to the classifier HAL. After a classification of a specific
+ * event is determined, MotionClassifier then marks the next event in the stream with this
+ * classification.
*
* Therefore, it is acceptable to have the classifications be delayed by 1-2 events
* in a particular gesture.
@@ -141,15 +141,9 @@
virtual void dump(std::string& dump) override;
private:
- /**
- * Initialize MotionClassifier.
- * Return true if initializaion is successful.
- */
- bool init();
- /**
- * Entity that will be notified of the HAL death (most likely InputClassifier).
- */
- wp<android::hardware::hidl_death_recipient> mDeathRecipient;
+ friend class MotionClassifierTest; // to create MotionClassifier with a test HAL implementation
+ explicit MotionClassifier(
+ sp<android::hardware::input::classifier::V1_0::IInputClassifier> service);
// The events that need to be sent to the HAL.
BlockingQueue<ClassifierEvent> mEvents;
@@ -164,14 +158,9 @@
*/
std::thread mHalThread;
/**
- * Print an error message if the caller is not on the InputClassifier thread.
- * Caller must supply the name of the calling function as __func__
+ * Process events and call the InputClassifier HAL
*/
- void ensureHalThread(const char* function);
- /**
- * Call the InputClassifier HAL
- */
- void callInputClassifierHal();
+ void processEvents();
/**
* Access to the InputClassifier HAL. May be null if init() hasn't completed yet.
* When init() successfully completes, mService is guaranteed to remain non-null and to not
@@ -223,19 +212,15 @@
const char* getServiceStatus() REQUIRES(mLock);
};
-
/**
* Implementation of the InputClassifierInterface.
* Represents a separate stage of input processing. All of the input events go through this stage.
* Acts as a passthrough for all input events except for motion events.
* The events of motion type are sent to MotionClassifier.
*/
-class InputClassifier : public InputClassifierInterface,
- public android::hardware::hidl_death_recipient {
+class InputClassifier : public InputClassifierInterface {
public:
explicit InputClassifier(const sp<InputListenerInterface>& listener);
- // Some of the constructor logic is finished in onFirstRef
- virtual void onFirstRef() override;
virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override;
virtual void notifyKey(const NotifyKeyArgs* args) override;
@@ -243,17 +228,44 @@
virtual void notifySwitch(const NotifySwitchArgs* args) override;
virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) override;
- virtual void serviceDied(uint64_t cookie,
- const wp<android::hidl::base::V1_0::IBase>& who) override;
-
virtual void dump(std::string& dump) override;
+ ~InputClassifier();
+
private:
// Protect access to mMotionClassifier, since it may become null via a hidl callback
std::mutex mLock;
- std::unique_ptr<MotionClassifierInterface> mMotionClassifier GUARDED_BY(mLock);
// The next stage to pass input events to
sp<InputListenerInterface> mListener;
+
+ std::unique_ptr<MotionClassifierInterface> mMotionClassifier GUARDED_BY(mLock);
+ std::thread mInitializeMotionClassifierThread;
+ /**
+ * Set the value of mMotionClassifier.
+ * This is called from 2 different threads:
+ * 1) mInitializeMotionClassifierThread, when we have constructed a MotionClassifier
+ * 2) A binder thread of the HalDeathRecipient, which is created when HAL dies. This would cause
+ * mMotionClassifier to become nullptr.
+ */
+ void setMotionClassifier(std::unique_ptr<MotionClassifierInterface> motionClassifier);
+
+ /**
+ * The deathRecipient will call setMotionClassifier(null) when the HAL dies.
+ */
+ class HalDeathRecipient : public android::hardware::hidl_death_recipient {
+ public:
+ explicit HalDeathRecipient(InputClassifier& parent);
+ virtual void serviceDied(uint64_t cookie,
+ const wp<android::hidl::base::V1_0::IBase>& who) override;
+
+ private:
+ InputClassifier& mParent;
+ };
+ // We retain a reference to death recipient, because the death recipient will be calling
+ // ~MotionClassifier if the HAL dies.
+ // If we don't retain a reference, and MotionClassifier is the only owner of the death
+ // recipient, the serviceDied call will cause death recipient to call its own destructor.
+ sp<HalDeathRecipient> mHalDeathRecipient;
};
} // namespace android