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
This commit is contained in:
Siarhei Vishniakou
2020-07-09 19:55:53 -05:00
parent 7313181cb1
commit e28cef055b
5 changed files with 63 additions and 59 deletions

View File

@@ -43,25 +43,24 @@ static struct {
class NativeInputChannel {
public:
explicit NativeInputChannel(const sp<InputChannel>& inputChannel);
explicit NativeInputChannel(const std::shared_ptr<InputChannel>& inputChannel);
~NativeInputChannel();
inline sp<InputChannel> getInputChannel() { return mInputChannel; }
inline std::shared_ptr<InputChannel> getInputChannel() { return mInputChannel; }
void setDisposeCallback(InputChannelObjDisposeCallback callback, void* data);
void dispose(JNIEnv* env, jobject obj);
private:
sp<InputChannel> mInputChannel;
std::shared_ptr<InputChannel> mInputChannel;
InputChannelObjDisposeCallback mDisposeCallback;
void* mDisposeData;
};
// ----------------------------------------------------------------------------
NativeInputChannel::NativeInputChannel(const sp<InputChannel>& inputChannel) :
mInputChannel(inputChannel), mDisposeCallback(nullptr) {
}
NativeInputChannel::NativeInputChannel(const std::shared_ptr<InputChannel>& 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<NativeInputChannel*>(longPtr);
}
sp<InputChannel> android_view_InputChannel_getInputChannel(JNIEnv* env, jobject inputChannelObj) {
std::shared_ptr<InputChannel> 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> inputChannel) {
static jlong android_view_InputChannel_createInputChannel(
JNIEnv* env, std::shared_ptr<InputChannel> inputChannel) {
std::unique_ptr<NativeInputChannel> nativeInputChannel =
std::make_unique<NativeInputChannel>(inputChannel);
@@ -122,8 +122,8 @@ static jlongArray android_view_InputChannel_nativeOpenInputChannelPair(JNIEnv* e
ScopedUtfChars nameChars(env, nameObj);
std::string name = nameChars.c_str();
sp<InputChannel> serverChannel;
sp<InputChannel> clientChannel;
std::shared_ptr<InputChannel> serverChannel;
std::shared_ptr<InputChannel> 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> inputChannel = new InputChannel();
std::shared_ptr<InputChannel> inputChannel = std::make_shared<InputChannel>();
inputChannel->readFromParcel(parcel);
NativeInputChannel* nativeInputChannel = new NativeInputChannel(inputChannel);
return reinterpret_cast<jlong>(nativeInputChannel);
@@ -227,13 +227,13 @@ static jlong android_view_InputChannel_nativeDup(JNIEnv* env, jobject obj, jlong
return 0;
}
sp<InputChannel> inputChannel = nativeInputChannel->getInputChannel();
std::shared_ptr<InputChannel> inputChannel = nativeInputChannel->getInputChannel();
if (inputChannel == nullptr) {
jniThrowRuntimeException(env, "NativeInputChannel has no corresponding InputChannel");
return 0;
}
sp<InputChannel> dupInputChannel = inputChannel->dup();
std::shared_ptr<InputChannel> dupInputChannel = inputChannel->dup();
if (dupInputChannel == nullptr) {
std::string message = android::base::StringPrintf(
"Could not duplicate input channel %s", inputChannel->getName().c_str());

View File

@@ -24,10 +24,11 @@
namespace android {
typedef void (*InputChannelObjDisposeCallback)(JNIEnv* env, jobject inputChannelObj,
const sp<InputChannel>& inputChannel, void* data);
const std::shared_ptr<InputChannel>& inputChannel,
void* data);
extern sp<InputChannel> android_view_InputChannel_getInputChannel(JNIEnv* env,
jobject inputChannelObj);
extern std::shared_ptr<InputChannel> 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

View File

@@ -55,9 +55,9 @@ static struct {
class NativeInputEventReceiver : public LooperCallback {
public:
NativeInputEventReceiver(JNIEnv* env,
jobject receiverWeak, const sp<InputChannel>& inputChannel,
const sp<MessageQueue>& messageQueue);
NativeInputEventReceiver(JNIEnv* env, jobject receiverWeak,
const std::shared_ptr<InputChannel>& inputChannel,
const sp<MessageQueue>& 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>& inputChannel,
const sp<MessageQueue>& messageQueue) :
mReceiverWeakGlobal(env->NewGlobalRef(receiverWeak)),
mInputConsumer(inputChannel), mMessageQueue(messageQueue),
mBatchedInputEventPending(false), mFdEvents(0) {
NativeInputEventReceiver::NativeInputEventReceiver(
JNIEnv* env, jobject receiverWeak, const std::shared_ptr<InputChannel>& inputChannel,
const sp<MessageQueue>& 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> inputChannel = android_view_InputChannel_getInputChannel(env,
inputChannelObj);
std::shared_ptr<InputChannel> inputChannel =
android_view_InputChannel_getInputChannel(env, inputChannelObj);
if (inputChannel == nullptr) {
jniThrowRuntimeException(env, "InputChannel is not initialized.");
return 0;

View File

@@ -48,9 +48,9 @@ static struct {
class NativeInputEventSender : public LooperCallback {
public:
NativeInputEventSender(JNIEnv* env,
jobject senderWeak, const sp<InputChannel>& inputChannel,
const sp<MessageQueue>& messageQueue);
NativeInputEventSender(JNIEnv* env, jobject senderWeak,
const std::shared_ptr<InputChannel>& inputChannel,
const sp<MessageQueue>& 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>& inputChannel,
const sp<MessageQueue>& messageQueue) :
mSenderWeakGlobal(env->NewGlobalRef(senderWeak)),
mInputPublisher(inputChannel), mMessageQueue(messageQueue),
NativeInputEventSender::NativeInputEventSender(JNIEnv* env, jobject senderWeak,
const std::shared_ptr<InputChannel>& inputChannel,
const sp<MessageQueue>& 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> inputChannel = android_view_InputChannel_getInputChannel(env,
inputChannelObj);
std::shared_ptr<InputChannel> inputChannel =
android_view_InputChannel_getInputChannel(env, inputChannelObj);
if (inputChannel == NULL) {
jniThrowRuntimeException(env, "InputChannel is not initialized.");
return 0;

View File

@@ -200,10 +200,10 @@ public:
void setDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray);
status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
status_t registerInputMonitor(JNIEnv* env, const sp<InputChannel>& inputChannel,
int32_t displayId, bool isGestureMonitor);
status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
status_t registerInputChannel(JNIEnv* env, const std::shared_ptr<InputChannel>& inputChannel);
status_t registerInputMonitor(JNIEnv* env, const std::shared_ptr<InputChannel>& inputChannel,
int32_t displayId, bool isGestureMonitor);
status_t unregisterInputChannel(JNIEnv* env, const InputChannel& inputChannel);
status_t pilferPointers(const sp<IBinder>& 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>& inputChannel) {
status_t NativeInputManager::registerInputChannel(
JNIEnv* /* env */, const std::shared_ptr<InputChannel>& inputChannel) {
ATRACE_CALL();
return mInputManager->getDispatcher()->registerInputChannel(inputChannel);
}
status_t NativeInputManager::registerInputMonitor(JNIEnv* /* env */,
const sp<InputChannel>& inputChannel, int32_t displayId, bool isGestureMonitor) {
const std::shared_ptr<InputChannel>& inputChannel,
int32_t displayId, bool isGestureMonitor) {
ATRACE_CALL();
return mInputManager->getDispatcher()->registerInputMonitor(
inputChannel, displayId, isGestureMonitor);
}
status_t NativeInputManager::unregisterInputChannel(JNIEnv* /* env */,
const sp<InputChannel>& 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>& inputChannel, void* data) {
static void handleInputChannelDisposed(JNIEnv* env, jobject /* inputChannelObj */,
const std::shared_ptr<InputChannel>& inputChannel,
void* data) {
NativeInputManager* im = static_cast<NativeInputManager*>(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<NativeInputManager*>(ptr);
sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
inputChannelObj);
std::shared_ptr<InputChannel> 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<NativeInputManager*>(ptr);
sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
inputChannelObj);
std::shared_ptr<InputChannel> 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<NativeInputManager*>(ptr);
sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
inputChannelObj);
std::shared_ptr<InputChannel> 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);