Rewrite input transport using sockets.

Since we will not longer be modifying events in place, we don't need
to use an ashmem region for input.  Simplified the code to instead
use a socket of type SOCK_SEQPACKET.

This is part of a series of changes to improve input system pipelining.

Bug: 5963420

Change-Id: I05909075ed8b61b93900913e44c6db84857340d8
diff --git a/core/jni/android_app_NativeActivity.cpp b/core/jni/android_app_NativeActivity.cpp
index ac4fe5b..f383c5d 100644
--- a/core/jni/android_app_NativeActivity.cpp
+++ b/core/jni/android_app_NativeActivity.cpp
@@ -130,21 +130,21 @@
 void AInputQueue::attachLooper(ALooper* looper, int ident,
         ALooper_callbackFunc callback, void* data) {
     mLooper = static_cast<android::Looper*>(looper);
-    mLooper->addFd(mConsumer.getChannel()->getReceivePipeFd(),
+    mLooper->addFd(mConsumer.getChannel()->getFd(),
             ident, ALOOPER_EVENT_INPUT, callback, data);
     mLooper->addFd(mDispatchKeyRead,
             ident, ALOOPER_EVENT_INPUT, callback, data);
 }
 
 void AInputQueue::detachLooper() {
-    mLooper->removeFd(mConsumer.getChannel()->getReceivePipeFd());
+    mLooper->removeFd(mConsumer.getChannel()->getFd());
     mLooper->removeFd(mDispatchKeyRead);
 }
 
 int32_t AInputQueue::hasEvents() {
     struct pollfd pfd[2];
 
-    pfd[0].fd = mConsumer.getChannel()->getReceivePipeFd();
+    pfd[0].fd = mConsumer.getChannel()->getFd();
     pfd[0].events = POLLIN;
     pfd[0].revents = 0;
     pfd[1].fd = mDispatchKeyRead;
@@ -200,16 +200,9 @@
             return 0;
         }
     }
-    
-    int32_t res = mConsumer.receiveDispatchSignal();
-    if (res != android::OK) {
-        ALOGE("channel '%s' ~ Failed to receive dispatch signal.  status=%d",
-                mConsumer.getChannel()->getName().string(), res);
-        return -1;
-    }
 
     InputEvent* myEvent = NULL;
-    res = mConsumer.consume(this, &myEvent);
+    status_t res = mConsumer.consume(this, &myEvent);
     if (res != android::OK) {
         ALOGW("channel '%s' ~ Failed to consume input event.  status=%d",
                 mConsumer.getChannel()->getName().string(), res);
@@ -481,11 +474,6 @@
                     android_view_InputChannel_getInputChannel(env, _channel);
             if (ic != NULL) {
                 nativeInputQueue = new AInputQueue(ic, mainWorkWrite);
-                if (nativeInputQueue->getConsumer().initialize() != android::OK) {
-                    delete nativeInputQueue;
-                    nativeInputQueue = NULL;
-                    return UNKNOWN_ERROR;
-                }
             } else {
                 return UNKNOWN_ERROR;
             }
diff --git a/core/jni/android_view_InputChannel.cpp b/core/jni/android_view_InputChannel.cpp
index fce432b..5377425 100644
--- a/core/jni/android_view_InputChannel.cpp
+++ b/core/jni/android_view_InputChannel.cpp
@@ -199,32 +199,16 @@
         bool isInitialized = parcel->readInt32();
         if (isInitialized) {
             String8 name = parcel->readString8();
-            int32_t parcelAshmemFd = parcel->readFileDescriptor();
-            int32_t ashmemFd = dup(parcelAshmemFd);
-            if (ashmemFd < 0) {
-                ALOGE("Error %d dup ashmem fd %d.", errno, parcelAshmemFd);
-            }
-            int32_t parcelReceivePipeFd = parcel->readFileDescriptor();
-            int32_t receivePipeFd = dup(parcelReceivePipeFd);
-            if (receivePipeFd < 0) {
-                ALOGE("Error %d dup receive pipe fd %d.", errno, parcelReceivePipeFd);
-            }
-            int32_t parcelSendPipeFd = parcel->readFileDescriptor();
-            int32_t sendPipeFd = dup(parcelSendPipeFd);
-            if (sendPipeFd < 0) {
-                ALOGE("Error %d dup send pipe fd %d.", errno, parcelSendPipeFd);
-            }
-            if (ashmemFd < 0 || receivePipeFd < 0 || sendPipeFd < 0) {
-                if (ashmemFd >= 0) ::close(ashmemFd);
-                if (receivePipeFd >= 0) ::close(receivePipeFd);
-                if (sendPipeFd >= 0) ::close(sendPipeFd);
+            int32_t rawFd = parcel->readFileDescriptor();
+            int32_t dupFd = dup(rawFd);
+            if (rawFd < 0) {
+                ALOGE("Error %d dup channel fd %d.", errno, rawFd);
                 jniThrowRuntimeException(env,
                         "Could not read input channel file descriptors from parcel.");
                 return;
             }
 
-            InputChannel* inputChannel = new InputChannel(name, ashmemFd,
-                    receivePipeFd, sendPipeFd);
+            InputChannel* inputChannel = new InputChannel(name, dupFd);
             NativeInputChannel* nativeInputChannel = new NativeInputChannel(inputChannel);
 
             android_view_InputChannel_setNativeInputChannel(env, obj, nativeInputChannel);
@@ -243,9 +227,7 @@
 
             parcel->writeInt32(1);
             parcel->writeString8(inputChannel->getName());
-            parcel->writeDupFileDescriptor(inputChannel->getAshmemFd());
-            parcel->writeDupFileDescriptor(inputChannel->getReceivePipeFd());
-            parcel->writeDupFileDescriptor(inputChannel->getSendPipeFd());
+            parcel->writeDupFileDescriptor(inputChannel->getFd());
         } else {
             parcel->writeInt32(0);
         }
diff --git a/core/jni/android_view_InputEventReceiver.cpp b/core/jni/android_view_InputEventReceiver.cpp
index ed0acce..23e1590 100644
--- a/core/jni/android_view_InputEventReceiver.cpp
+++ b/core/jni/android_view_InputEventReceiver.cpp
@@ -83,7 +83,7 @@
     ALOGD("channel '%s' ~ Disposing input event receiver.", getInputChannelName());
 #endif
 
-    mLooper->removeFd(mInputConsumer.getChannel()->getReceivePipeFd());
+    mLooper->removeFd(mInputConsumer.getChannel()->getFd());
     if (mEventInProgress) {
         mInputConsumer.sendFinishedSignal(false); // ignoring result
     }
@@ -93,14 +93,7 @@
 }
 
 status_t NativeInputEventReceiver::initialize() {
-    status_t result = mInputConsumer.initialize();
-    if (result) {
-        ALOGW("Failed to initialize input consumer for input channel '%s', status=%d",
-                getInputChannelName(), result);
-        return result;
-    }
-
-    int32_t receiveFd = mInputConsumer.getChannel()->getReceivePipeFd();
+    int32_t receiveFd = mInputConsumer.getChannel()->getFd();
     mLooper->addFd(receiveFd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this);
     return OK;
 }
@@ -139,13 +132,6 @@
         return 1;
     }
 
-    status_t status = r->mInputConsumer.receiveDispatchSignal();
-    if (status) {
-        ALOGE("channel '%s' ~ Failed to receive dispatch signal.  status=%d",
-                r->getInputChannelName(), status);
-        return 0; // remove the callback
-    }
-
     if (r->mEventInProgress) {
         ALOGW("channel '%s' ~ Publisher sent spurious dispatch signal.",
                 r->getInputChannelName());
@@ -153,7 +139,7 @@
     }
 
     InputEvent* inputEvent;
-    status = r->mInputConsumer.consume(&r->mInputEventFactory, &inputEvent);
+    status_t status = r->mInputConsumer.consume(&r->mInputEventFactory, &inputEvent);
     if (status) {
         ALOGW("channel '%s' ~ Failed to consume input event.  status=%d",
                 r->getInputChannelName(), status);