Add uniqueId to Virtual Display and pass through to inputflinger (1/2)

This CL adds :
1) Adds uniqueId (protected via system/sig permission) to virtual
displays.
2) Add support for N virtual display viewports into inputflinger.
3) Set the virtual display's viewports in inputflinger if it has the
uniqueId value set to non-null. (a) Moving the new viewport from java to
native inputflinger and (b) adding "uniqueId" value to viewports makes
up the great majority of this change.
4) From the inputflinger side, we also read in a new value from the
input device configuration files called 'touch.displayId'.
5) When touch.displayId and the virtual display's uniqueId match,
inputflinger links the two.

Test: Start VR and ensure that the virtual viewport shows up when running
'adb shell dump input".  Run a VR app, and ensure that the virtual input
device is associated with the new virtual viewport.
Test: com.android.server.display.DisplayManagerServiceTest

Bug: 36051620
Change-Id: Id728d6e7292feaa1d8de7660bc6a2ec90fa1ff3c
diff --git a/core/jni/android_hardware_display_DisplayViewport.cpp b/core/jni/android_hardware_display_DisplayViewport.cpp
new file mode 100644
index 0000000..1823a2c
--- /dev/null
+++ b/core/jni/android_hardware_display_DisplayViewport.cpp
@@ -0,0 +1,120 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+
+#define LOG_TAG "DisplayViewport-JNI"
+
+#include "JNIHelp.h"
+#include "core_jni_helpers.h"
+
+#include <android_hardware_display_DisplayViewport.h>
+#include <android_runtime/AndroidRuntime.h>
+#include <android_runtime/Log.h>
+#include <utils/Log.h>
+
+#include <ScopedUtfChars.h>
+
+namespace android {
+
+// ----------------------------------------------------------------------------
+
+static struct {
+    jclass clazz;
+
+    jfieldID displayId;
+    jfieldID orientation;
+    jfieldID logicalFrame;
+    jfieldID physicalFrame;
+    jfieldID deviceWidth;
+    jfieldID deviceHeight;
+    jfieldID uniqueId;
+} gDisplayViewportClassInfo;
+
+static struct {
+    jfieldID left;
+    jfieldID top;
+    jfieldID right;
+    jfieldID bottom;
+} gRectClassInfo;
+
+// ----------------------------------------------------------------------------
+
+status_t android_hardware_display_DisplayViewport_toNative(JNIEnv* env, jobject viewportObj,
+        DisplayViewport* viewport) {
+    viewport->displayId = env->GetIntField(viewportObj, gDisplayViewportClassInfo.displayId);
+    viewport->orientation = env->GetIntField(viewportObj, gDisplayViewportClassInfo.orientation);
+    viewport->deviceWidth = env->GetIntField(viewportObj, gDisplayViewportClassInfo.deviceWidth);
+    viewport->deviceHeight = env->GetIntField(viewportObj, gDisplayViewportClassInfo.deviceHeight);
+
+    jstring uniqueId =
+            jstring(env->GetObjectField(viewportObj, gDisplayViewportClassInfo.uniqueId));
+    if (uniqueId != nullptr) {
+        viewport->uniqueId.setTo(ScopedUtfChars(env, uniqueId).c_str());
+    }
+
+    jobject logicalFrameObj =
+            env->GetObjectField(viewportObj, gDisplayViewportClassInfo.logicalFrame);
+    viewport->logicalLeft = env->GetIntField(logicalFrameObj, gRectClassInfo.left);
+    viewport->logicalTop = env->GetIntField(logicalFrameObj, gRectClassInfo.top);
+    viewport->logicalRight = env->GetIntField(logicalFrameObj, gRectClassInfo.right);
+    viewport->logicalBottom = env->GetIntField(logicalFrameObj, gRectClassInfo.bottom);
+
+    jobject physicalFrameObj =
+            env->GetObjectField(viewportObj, gDisplayViewportClassInfo.physicalFrame);
+    viewport->physicalLeft = env->GetIntField(physicalFrameObj, gRectClassInfo.left);
+    viewport->physicalTop = env->GetIntField(physicalFrameObj, gRectClassInfo.top);
+    viewport->physicalRight = env->GetIntField(physicalFrameObj, gRectClassInfo.right);
+    viewport->physicalBottom = env->GetIntField(physicalFrameObj, gRectClassInfo.bottom);
+
+    return OK;
+}
+
+// ----------------------------------------------------------------------------
+
+int register_android_hardware_display_DisplayViewport(JNIEnv* env) {
+    jclass clazz = FindClassOrDie(env, "android/hardware/display/DisplayViewport");
+    gDisplayViewportClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
+
+    gDisplayViewportClassInfo.displayId = GetFieldIDOrDie(env,
+            gDisplayViewportClassInfo.clazz, "displayId", "I");
+
+    gDisplayViewportClassInfo.orientation = GetFieldIDOrDie(env,
+            gDisplayViewportClassInfo.clazz, "orientation", "I");
+
+    gDisplayViewportClassInfo.deviceWidth = GetFieldIDOrDie(env,
+            gDisplayViewportClassInfo.clazz, "deviceWidth", "I");
+
+    gDisplayViewportClassInfo.deviceHeight = GetFieldIDOrDie(env,
+            gDisplayViewportClassInfo.clazz, "deviceHeight", "I");
+
+    gDisplayViewportClassInfo.logicalFrame = GetFieldIDOrDie(env,
+            gDisplayViewportClassInfo.clazz, "logicalFrame", "Landroid/graphics/Rect;");
+
+    gDisplayViewportClassInfo.physicalFrame = GetFieldIDOrDie(env,
+            gDisplayViewportClassInfo.clazz, "physicalFrame", "Landroid/graphics/Rect;");
+
+    gDisplayViewportClassInfo.uniqueId = GetFieldIDOrDie(env,
+            gDisplayViewportClassInfo.clazz, "uniqueId", "Ljava/lang/String;");
+
+    clazz = FindClassOrDie(env, "android/graphics/Rect");
+    gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I");
+    gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I");
+    gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I");
+    gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I");
+
+    return 0;
+}
+
+} // namespace android