From e28cef055b3d814384613f1f3d3a0b925cadf3bc Mon Sep 17 00:00:00 2001 From: Siarhei Vishniakou Date: Thu, 9 Jul 2020 19:55:53 -0500 Subject: [PATCH] Use std:shared_ptr for InputChannel Move InputChannel off RefBase, which would allow it to become Parcelable. Bug: 142581626 Test: interact with device after flashing Change-Id: Id05fc99a1f9bb729c452de11fd8628dc32117b77 --- core/jni/android_view_InputChannel.cpp | 30 +++++++-------- core/jni/android_view_InputChannel.h | 7 ++-- core/jni/android_view_InputEventReceiver.cpp | 25 ++++++------ core/jni/android_view_InputEventSender.cpp | 22 +++++------ ...droid_server_input_InputManagerService.cpp | 38 ++++++++++--------- 5 files changed, 63 insertions(+), 59 deletions(-) diff --git a/core/jni/android_view_InputChannel.cpp b/core/jni/android_view_InputChannel.cpp index 8153166d46952..8459410bccd14 100644 --- a/core/jni/android_view_InputChannel.cpp +++ b/core/jni/android_view_InputChannel.cpp @@ -43,25 +43,24 @@ static struct { class NativeInputChannel { public: - explicit NativeInputChannel(const sp& inputChannel); + explicit NativeInputChannel(const std::shared_ptr& inputChannel); ~NativeInputChannel(); - inline sp getInputChannel() { return mInputChannel; } + inline std::shared_ptr getInputChannel() { return mInputChannel; } void setDisposeCallback(InputChannelObjDisposeCallback callback, void* data); void dispose(JNIEnv* env, jobject obj); private: - sp mInputChannel; + std::shared_ptr mInputChannel; InputChannelObjDisposeCallback mDisposeCallback; void* mDisposeData; }; // ---------------------------------------------------------------------------- -NativeInputChannel::NativeInputChannel(const sp& inputChannel) : - mInputChannel(inputChannel), mDisposeCallback(nullptr) { -} +NativeInputChannel::NativeInputChannel(const std::shared_ptr& inputChannel) + : mInputChannel(inputChannel), mDisposeCallback(nullptr) {} NativeInputChannel::~NativeInputChannel() { } @@ -81,7 +80,7 @@ void NativeInputChannel::dispose(JNIEnv* env, jobject obj) { mDisposeCallback = nullptr; mDisposeData = nullptr; } - mInputChannel.clear(); + mInputChannel.reset(); } // ---------------------------------------------------------------------------- @@ -92,7 +91,8 @@ static NativeInputChannel* android_view_InputChannel_getNativeInputChannel(JNIEn return reinterpret_cast(longPtr); } -sp android_view_InputChannel_getInputChannel(JNIEnv* env, jobject inputChannelObj) { +std::shared_ptr android_view_InputChannel_getInputChannel(JNIEnv* env, + jobject inputChannelObj) { NativeInputChannel* nativeInputChannel = android_view_InputChannel_getNativeInputChannel(env, inputChannelObj); return nativeInputChannel != nullptr ? nativeInputChannel->getInputChannel() : nullptr; @@ -109,8 +109,8 @@ void android_view_InputChannel_setDisposeCallback(JNIEnv* env, jobject inputChan } } -static jlong android_view_InputChannel_createInputChannel(JNIEnv* env, - sp inputChannel) { +static jlong android_view_InputChannel_createInputChannel( + JNIEnv* env, std::shared_ptr inputChannel) { std::unique_ptr nativeInputChannel = std::make_unique(inputChannel); @@ -122,8 +122,8 @@ static jlongArray android_view_InputChannel_nativeOpenInputChannelPair(JNIEnv* e ScopedUtfChars nameChars(env, nameObj); std::string name = nameChars.c_str(); - sp serverChannel; - sp clientChannel; + std::shared_ptr serverChannel; + std::shared_ptr clientChannel; status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); if (result) { @@ -180,7 +180,7 @@ static jlong android_view_InputChannel_nativeReadFromParcel(JNIEnv* env, jobject if (parcel) { bool isInitialized = parcel->readInt32(); if (isInitialized) { - sp inputChannel = new InputChannel(); + std::shared_ptr inputChannel = std::make_shared(); inputChannel->readFromParcel(parcel); NativeInputChannel* nativeInputChannel = new NativeInputChannel(inputChannel); return reinterpret_cast(nativeInputChannel); @@ -227,13 +227,13 @@ static jlong android_view_InputChannel_nativeDup(JNIEnv* env, jobject obj, jlong return 0; } - sp inputChannel = nativeInputChannel->getInputChannel(); + std::shared_ptr inputChannel = nativeInputChannel->getInputChannel(); if (inputChannel == nullptr) { jniThrowRuntimeException(env, "NativeInputChannel has no corresponding InputChannel"); return 0; } - sp dupInputChannel = inputChannel->dup(); + std::shared_ptr dupInputChannel = inputChannel->dup(); if (dupInputChannel == nullptr) { std::string message = android::base::StringPrintf( "Could not duplicate input channel %s", inputChannel->getName().c_str()); diff --git a/core/jni/android_view_InputChannel.h b/core/jni/android_view_InputChannel.h index 2ba2dc0516d09..8030c96ab19fb 100644 --- a/core/jni/android_view_InputChannel.h +++ b/core/jni/android_view_InputChannel.h @@ -24,10 +24,11 @@ namespace android { typedef void (*InputChannelObjDisposeCallback)(JNIEnv* env, jobject inputChannelObj, - const sp& inputChannel, void* data); + const std::shared_ptr& inputChannel, + void* data); -extern sp android_view_InputChannel_getInputChannel(JNIEnv* env, - jobject inputChannelObj); +extern std::shared_ptr android_view_InputChannel_getInputChannel( + JNIEnv* env, jobject inputChannelObj); /* Sets a callback that is invoked when the InputChannel DVM object is disposed (or finalized). * This is used to automatically dispose of other native objects in the input dispatcher diff --git a/core/jni/android_view_InputEventReceiver.cpp b/core/jni/android_view_InputEventReceiver.cpp index cc94d6ff5d673..979a69aa3ce2c 100644 --- a/core/jni/android_view_InputEventReceiver.cpp +++ b/core/jni/android_view_InputEventReceiver.cpp @@ -55,9 +55,9 @@ static struct { class NativeInputEventReceiver : public LooperCallback { public: - NativeInputEventReceiver(JNIEnv* env, - jobject receiverWeak, const sp& inputChannel, - const sp& messageQueue); + NativeInputEventReceiver(JNIEnv* env, jobject receiverWeak, + const std::shared_ptr& inputChannel, + const sp& messageQueue); status_t initialize(); void dispose(); @@ -91,13 +91,14 @@ private: virtual int handleEvent(int receiveFd, int events, void* data) override; }; - -NativeInputEventReceiver::NativeInputEventReceiver(JNIEnv* env, - jobject receiverWeak, const sp& inputChannel, - const sp& messageQueue) : - mReceiverWeakGlobal(env->NewGlobalRef(receiverWeak)), - mInputConsumer(inputChannel), mMessageQueue(messageQueue), - mBatchedInputEventPending(false), mFdEvents(0) { +NativeInputEventReceiver::NativeInputEventReceiver( + JNIEnv* env, jobject receiverWeak, const std::shared_ptr& inputChannel, + const sp& messageQueue) + : mReceiverWeakGlobal(env->NewGlobalRef(receiverWeak)), + mInputConsumer(inputChannel), + mMessageQueue(messageQueue), + mBatchedInputEventPending(false), + mFdEvents(0) { if (kDebugDispatchCycle) { ALOGD("channel '%s' ~ Initializing input event receiver.", getInputChannelName().c_str()); } @@ -356,8 +357,8 @@ status_t NativeInputEventReceiver::consumeEvents(JNIEnv* env, static jlong nativeInit(JNIEnv* env, jclass clazz, jobject receiverWeak, jobject inputChannelObj, jobject messageQueueObj) { - sp inputChannel = android_view_InputChannel_getInputChannel(env, - inputChannelObj); + std::shared_ptr inputChannel = + android_view_InputChannel_getInputChannel(env, inputChannelObj); if (inputChannel == nullptr) { jniThrowRuntimeException(env, "InputChannel is not initialized."); return 0; diff --git a/core/jni/android_view_InputEventSender.cpp b/core/jni/android_view_InputEventSender.cpp index 0a2b1d4a661f7..3ca43ce915cfd 100644 --- a/core/jni/android_view_InputEventSender.cpp +++ b/core/jni/android_view_InputEventSender.cpp @@ -48,9 +48,9 @@ static struct { class NativeInputEventSender : public LooperCallback { public: - NativeInputEventSender(JNIEnv* env, - jobject senderWeak, const sp& inputChannel, - const sp& messageQueue); + NativeInputEventSender(JNIEnv* env, jobject senderWeak, + const std::shared_ptr& inputChannel, + const sp& messageQueue); status_t initialize(); void dispose(); @@ -76,12 +76,12 @@ private: status_t receiveFinishedSignals(JNIEnv* env); }; - -NativeInputEventSender::NativeInputEventSender(JNIEnv* env, - jobject senderWeak, const sp& inputChannel, - const sp& messageQueue) : - mSenderWeakGlobal(env->NewGlobalRef(senderWeak)), - mInputPublisher(inputChannel), mMessageQueue(messageQueue), +NativeInputEventSender::NativeInputEventSender(JNIEnv* env, jobject senderWeak, + const std::shared_ptr& inputChannel, + const sp& messageQueue) + : mSenderWeakGlobal(env->NewGlobalRef(senderWeak)), + mInputPublisher(inputChannel), + mMessageQueue(messageQueue), mNextPublishedSeq(1) { if (kDebugDispatchCycle) { ALOGD("channel '%s' ~ Initializing input event sender.", getInputChannelName().c_str()); @@ -249,8 +249,8 @@ status_t NativeInputEventSender::receiveFinishedSignals(JNIEnv* env) { static jlong nativeInit(JNIEnv* env, jclass clazz, jobject senderWeak, jobject inputChannelObj, jobject messageQueueObj) { - sp inputChannel = android_view_InputChannel_getInputChannel(env, - inputChannelObj); + std::shared_ptr inputChannel = + android_view_InputChannel_getInputChannel(env, inputChannelObj); if (inputChannel == NULL) { jniThrowRuntimeException(env, "InputChannel is not initialized."); return 0; diff --git a/services/core/jni/com_android_server_input_InputManagerService.cpp b/services/core/jni/com_android_server_input_InputManagerService.cpp index 7bd455ab82a15..7663a6a4fde78 100644 --- a/services/core/jni/com_android_server_input_InputManagerService.cpp +++ b/services/core/jni/com_android_server_input_InputManagerService.cpp @@ -200,10 +200,10 @@ public: void setDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray); - status_t registerInputChannel(JNIEnv* env, const sp& inputChannel); - status_t registerInputMonitor(JNIEnv* env, const sp& inputChannel, - int32_t displayId, bool isGestureMonitor); - status_t unregisterInputChannel(JNIEnv* env, const sp& inputChannel); + status_t registerInputChannel(JNIEnv* env, const std::shared_ptr& inputChannel); + status_t registerInputMonitor(JNIEnv* env, const std::shared_ptr& inputChannel, + int32_t displayId, bool isGestureMonitor); + status_t unregisterInputChannel(JNIEnv* env, const InputChannel& inputChannel); status_t pilferPointers(const sp& token); void displayRemoved(JNIEnv* env, int32_t displayId); @@ -421,21 +421,22 @@ void NativeInputManager::setDisplayViewports(JNIEnv* env, jobjectArray viewportO InputReaderConfiguration::CHANGE_DISPLAY_INFO); } -status_t NativeInputManager::registerInputChannel(JNIEnv* /* env */, - const sp& inputChannel) { +status_t NativeInputManager::registerInputChannel( + JNIEnv* /* env */, const std::shared_ptr& inputChannel) { ATRACE_CALL(); return mInputManager->getDispatcher()->registerInputChannel(inputChannel); } status_t NativeInputManager::registerInputMonitor(JNIEnv* /* env */, - const sp& inputChannel, int32_t displayId, bool isGestureMonitor) { + const std::shared_ptr& inputChannel, + int32_t displayId, bool isGestureMonitor) { ATRACE_CALL(); return mInputManager->getDispatcher()->registerInputMonitor( inputChannel, displayId, isGestureMonitor); } status_t NativeInputManager::unregisterInputChannel(JNIEnv* /* env */, - const sp& inputChannel) { + const InputChannel& inputChannel) { ATRACE_CALL(); return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel); } @@ -1338,21 +1339,22 @@ static void throwInputChannelNotInitialized(JNIEnv* env) { "inputChannel is not initialized"); } -static void handleInputChannelDisposed(JNIEnv* env, - jobject /* inputChannelObj */, const sp& inputChannel, void* data) { +static void handleInputChannelDisposed(JNIEnv* env, jobject /* inputChannelObj */, + const std::shared_ptr& inputChannel, + void* data) { NativeInputManager* im = static_cast(data); ALOGW("Input channel object '%s' was disposed without first being unregistered with " "the input manager!", inputChannel->getName().c_str()); - im->unregisterInputChannel(env, inputChannel); + im->unregisterInputChannel(env, *inputChannel); } static void nativeRegisterInputChannel(JNIEnv* env, jclass /* clazz */, jlong ptr, jobject inputChannelObj) { NativeInputManager* im = reinterpret_cast(ptr); - sp inputChannel = android_view_InputChannel_getInputChannel(env, - inputChannelObj); + std::shared_ptr inputChannel = + android_view_InputChannel_getInputChannel(env, inputChannelObj); if (inputChannel == nullptr) { throwInputChannelNotInitialized(env); return; @@ -1375,8 +1377,8 @@ static void nativeRegisterInputMonitor(JNIEnv* env, jclass /* clazz */, jlong ptr, jobject inputChannelObj, jint displayId, jboolean isGestureMonitor) { NativeInputManager* im = reinterpret_cast(ptr); - sp inputChannel = android_view_InputChannel_getInputChannel(env, - inputChannelObj); + std::shared_ptr inputChannel = + android_view_InputChannel_getInputChannel(env, inputChannelObj); if (inputChannel == nullptr) { throwInputChannelNotInitialized(env); return; @@ -1401,8 +1403,8 @@ static void nativeUnregisterInputChannel(JNIEnv* env, jclass /* clazz */, jlong ptr, jobject inputChannelObj) { NativeInputManager* im = reinterpret_cast(ptr); - sp inputChannel = android_view_InputChannel_getInputChannel(env, - inputChannelObj); + std::shared_ptr inputChannel = + android_view_InputChannel_getInputChannel(env, inputChannelObj); if (inputChannel == nullptr) { throwInputChannelNotInitialized(env); return; @@ -1410,7 +1412,7 @@ static void nativeUnregisterInputChannel(JNIEnv* env, jclass /* clazz */, android_view_InputChannel_setDisposeCallback(env, inputChannelObj, nullptr, nullptr); - status_t status = im->unregisterInputChannel(env, inputChannel); + status_t status = im->unregisterInputChannel(env, *inputChannel); if (status && status != BAD_VALUE) { // ignore already unregistered channel std::string message; message += StringPrintf("Failed to unregister input channel. status=%d", status);