Better compat mode part one: start scaling windows.

First step of improving app screen size compatibility mode.  When
running in compat mode, an application's windows are scaled up on
the screen rather than being small with 1:1 pixels.

Currently we scale the application to fill the entire screen, so
don't use an even pixel scaling.  Though this may have some
negative impact on the appearance (it looks okay to me), it has a
big benefit of allowing us to now treat these apps as normal
full-screens apps and do the normal transition animations as you
move in and out and around in them.

This introduces fun stuff in the input system to take care of
modifying pointer coordinates to account for the app window
surface scaling.  The input dispatcher is told about the scale
that is being applied to each window and, when there is one,
adjusts pointer events appropriately as they are being sent
to the transport.

Also modified is CompatibilityInfo, which has been greatly
simplified to not be so insane and incomprehendible.  It is
now simple -- when constructed it determines if the given app
is compatible with the current screen size and density, and
that is that.

There are new APIs on ActivityManagerService to put applications
that we would traditionally consider compatible with larger screens
in compatibility mode.  This is the start of a facility to have
a UI affordance for a user to switch apps in and out of
compatibility.

To test switching of modes, there is a new variation of the "am"
command to do this: am screen-compat [on|off] [package]

This mode switching has the fundamentals of restarting activities
when it is changed, though the state still needs to be persisted
and the overall mode switch cleaned up.

For the few small apps I have tested, things mostly seem to be
working well.  I know of one problem with the text selection
handles being drawn at the wrong position because at some point
the window offset is being scaled incorrectly.  There are
probably other similar issues around the interaction between
two windows because the different window coordinate spaces are
done in a hacky way instead of being formally integrated into
the window manager layout process.

Change-Id: Ie038e3746b448135117bd860859d74e360938557
diff --git a/services/input/InputDispatcher.cpp b/services/input/InputDispatcher.cpp
index 19295e6d..f8a5cfb 100644
--- a/services/input/InputDispatcher.cpp
+++ b/services/input/InputDispatcher.cpp
@@ -156,6 +156,14 @@
     return true;
 }
 
+static void scalePointerCoords(const PointerCoords* inCoords, size_t count, float scaleFactor,
+        PointerCoords* outCoords) {
+   for (size_t i = 0; i < count; i++) {
+       outCoords[i] = inCoords[i];
+       outCoords[i].scale(scaleFactor);
+   }
+}
+
 static void dumpRegion(String8& dump, const SkRegion& region) {
     if (region.isEmpty()) {
         dump.append("<empty>");
@@ -1494,6 +1502,7 @@
     target.flags = targetFlags;
     target.xOffset = - window->frameLeft;
     target.yOffset = - window->frameTop;
+    target.scaleFactor = window->scaleFactor;
     target.pointerIds = pointerIds;
 }
 
@@ -1506,6 +1515,7 @@
         target.flags = 0;
         target.xOffset = 0;
         target.yOffset = 0;
+        target.scaleFactor = 1.0f;
     }
 }
 
@@ -1607,12 +1617,12 @@
         bool resumeWithAppendedMotionSample) {
 #if DEBUG_DISPATCH_CYCLE
     LOGD("channel '%s' ~ prepareDispatchCycle - flags=%d, "
-            "xOffset=%f, yOffset=%f, "
+            "xOffset=%f, yOffset=%f, scaleFactor=%f"
             "pointerIds=0x%x, "
             "resumeWithAppendedMotionSample=%s",
             connection->getInputChannelName(), inputTarget->flags,
             inputTarget->xOffset, inputTarget->yOffset,
-            inputTarget->pointerIds.value,
+            inputTarget->scaleFactor, inputTarget->pointerIds.value,
             toString(resumeWithAppendedMotionSample));
 #endif
 
@@ -1693,8 +1703,19 @@
             // consumed the motion event (or if the channel is broken).
             MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry);
             MotionSample* appendedMotionSample = motionEntry->lastSample;
-            status_t status = connection->inputPublisher.appendMotionSample(
-                    appendedMotionSample->eventTime, appendedMotionSample->pointerCoords);
+            status_t status;
+            if (motionEventDispatchEntry->scaleFactor == 1.0f) {
+                status = connection->inputPublisher.appendMotionSample(
+                        appendedMotionSample->eventTime, appendedMotionSample->pointerCoords);
+            } else {
+                PointerCoords scaledCoords[MAX_POINTERS];
+                for (size_t i = 0; i < motionEntry->pointerCount; i++) {
+                    scaledCoords[i] = appendedMotionSample->pointerCoords[i];
+                    scaledCoords[i].scale(motionEventDispatchEntry->scaleFactor);
+                }
+                status = connection->inputPublisher.appendMotionSample(
+                        appendedMotionSample->eventTime, scaledCoords);
+            }
             if (status == OK) {
 #if DEBUG_BATCHING
                 LOGD("channel '%s' ~ Successfully streamed new motion sample.",
@@ -1731,7 +1752,8 @@
     // This is a new event.
     // Enqueue a new dispatch entry onto the outbound queue for this connection.
     DispatchEntry* dispatchEntry = mAllocator.obtainDispatchEntry(eventEntry, // increments ref
-            inputTarget->flags, inputTarget->xOffset, inputTarget->yOffset);
+            inputTarget->flags, inputTarget->xOffset, inputTarget->yOffset,
+            inputTarget->scaleFactor);
     if (dispatchEntry->hasForegroundTarget()) {
         incrementPendingForegroundDispatchesLocked(eventEntry);
     }
@@ -1827,24 +1849,34 @@
             firstMotionSample = & motionEntry->firstSample;
         }
 
+        PointerCoords scaledCoords[MAX_POINTERS];
+        const PointerCoords* usingCoords = firstMotionSample->pointerCoords;
+
         // Set the X and Y offset depending on the input source.
-        float xOffset, yOffset;
+        float xOffset, yOffset, scaleFactor;
         if (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) {
-            xOffset = dispatchEntry->xOffset;
-            yOffset = dispatchEntry->yOffset;
+            scaleFactor = dispatchEntry->scaleFactor;
+            xOffset = dispatchEntry->xOffset * scaleFactor;
+            yOffset = dispatchEntry->yOffset * scaleFactor;
+            if (scaleFactor != 1.0f) {
+                for (size_t i = 0; i < motionEntry->pointerCount; i++) {
+                    scaledCoords[i] = firstMotionSample->pointerCoords[i];
+                    scaledCoords[i].scale(scaleFactor);
+                }
+                usingCoords = scaledCoords;
+            }
         } else {
             xOffset = 0.0f;
             yOffset = 0.0f;
+            scaleFactor = 1.0f;
         }
 
         // Publish the motion event and the first motion sample.
         status = connection->inputPublisher.publishMotionEvent(motionEntry->deviceId,
                 motionEntry->source, action, flags, motionEntry->edgeFlags, motionEntry->metaState,
-                xOffset, yOffset,
-                motionEntry->xPrecision, motionEntry->yPrecision,
+                xOffset, yOffset, motionEntry->xPrecision, motionEntry->yPrecision,
                 motionEntry->downTime, firstMotionSample->eventTime,
-                motionEntry->pointerCount, motionEntry->pointerIds,
-                firstMotionSample->pointerCoords);
+                motionEntry->pointerCount, motionEntry->pointerIds, usingCoords);
 
         if (status) {
             LOGE("channel '%s' ~ Could not publish motion event, "
@@ -1857,7 +1889,7 @@
         MotionSample* nextMotionSample = firstMotionSample->next;
         for (; nextMotionSample != NULL; nextMotionSample = nextMotionSample->next) {
             status = connection->inputPublisher.appendMotionSample(
-                    nextMotionSample->eventTime, nextMotionSample->pointerCoords);
+                    nextMotionSample->eventTime, usingCoords);
             if (status == NO_MEMORY) {
 #if DEBUG_DISPATCH_CYCLE
                     LOGD("channel '%s' ~ Shared memory buffer full.  Some motion samples will "
@@ -2095,18 +2127,21 @@
             }
 
             int32_t xOffset, yOffset;
+            float scaleFactor;
             const InputWindow* window = getWindowLocked(connection->inputChannel);
             if (window) {
                 xOffset = -window->frameLeft;
                 yOffset = -window->frameTop;
+                scaleFactor = window->scaleFactor;
             } else {
                 xOffset = 0;
                 yOffset = 0;
+                scaleFactor = 1.0f;
             }
 
             DispatchEntry* cancelationDispatchEntry =
                     mAllocator.obtainDispatchEntry(cancelationEventEntry, // increments ref
-                    0, xOffset, yOffset);
+                    0, xOffset, yOffset, scaleFactor);
             connection->outboundQueue.enqueueAtTail(cancelationDispatchEntry);
 
             mAllocator.releaseEventEntry(cancelationEventEntry);
@@ -2957,7 +2992,7 @@
             const InputWindow& window = mWindows[i];
             dump.appendFormat(INDENT2 "%d: name='%s', paused=%s, hasFocus=%s, hasWallpaper=%s, "
                     "visible=%s, canReceiveKeys=%s, flags=0x%08x, type=0x%08x, layer=%d, "
-                    "frame=[%d,%d][%d,%d], "
+                    "frame=[%d,%d][%d,%d], scale=%f, "
                     "touchableRegion=",
                     i, window.name.string(),
                     toString(window.paused),
@@ -2968,7 +3003,8 @@
                     window.layoutParamsFlags, window.layoutParamsType,
                     window.layer,
                     window.frameLeft, window.frameTop,
-                    window.frameRight, window.frameBottom);
+                    window.frameRight, window.frameBottom,
+                    window.scaleFactor);
             dumpRegion(dump, window.touchableRegion);
             dump.appendFormat(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%0.3fms\n",
                     window.ownerPid, window.ownerUid,
@@ -3460,13 +3496,14 @@
 
 InputDispatcher::DispatchEntry* InputDispatcher::Allocator::obtainDispatchEntry(
         EventEntry* eventEntry,
-        int32_t targetFlags, float xOffset, float yOffset) {
+        int32_t targetFlags, float xOffset, float yOffset, float scaleFactor) {
     DispatchEntry* entry = mDispatchEntryPool.alloc();
     entry->eventEntry = eventEntry;
     eventEntry->refCount += 1;
     entry->targetFlags = targetFlags;
     entry->xOffset = xOffset;
     entry->yOffset = yOffset;
+    entry->scaleFactor = scaleFactor;
     entry->inProgress = false;
     entry->headMotionSample = NULL;
     entry->tailMotionSample = NULL;