Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2038af30 authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

Use InputEventAssigner to assign input to frame

When a frame is determined to be caused by an input event, it will be
labeled with a specific input event id. The newly added entity,
InputEventAssigner, is responsible for determining which input
event we should use.

For regular ACTION_MOVE events, we will take the latest input event as
the event that has caused the frame. For the initial gesture
(ACTION_DOWN), we will use the first event (the ACTION_DOWN itself) to
figure out the down latency. This will allow us to split up 'down' and
'scroll' latencies.

Bug: 169866723
Test: looked at the data printed locally to logcat
Test: atest InputTests
Change-Id: I8513a36960e5d652d07655d1267e758d0c59ced7
parent 2b416cd6
Loading
Loading
Loading
Loading
+92 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2021 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.
 */

package android.view;

import static android.os.IInputConstants.INVALID_INPUT_EVENT_ID;
import static android.view.InputDevice.SOURCE_TOUCHSCREEN;

/**
 * Process input events and assign input event id to a specific frame.
 *
 * The assigned input event id is determined by where the current gesture is relative to the vsync.
 * In the middle of the gesture (we already processed some input events, and already received at
 * least 1 vsync), the latest InputEvent is assigned to the next frame.
 * If a gesture just started, then the ACTION_DOWN event will be assigned to the next frame.
 *
 * Consider the following sequence:
 * DOWN -> VSYNC 1 -> MOVE 1 -> MOVE 2 -> VSYNC 2.
 *
 * For VSYNC 1, we will assign the "DOWN" input event.
 * For VSYNC 2, we will assign the "MOVE 2" input event.
 *
 * Consider another sequence:
 * DOWN -> MOVE 1 -> MOVE 2 -> VSYNC 1 -> MOVE 3 -> VSYNC 2.
 *
 * For VSYNC 1, we will still assign the "DOWN" input event. That means that "MOVE 1" and "MOVE 2"
 * events are not attributed to any frame.
 * For VSYNC 2, the "MOVE 3" input event will be assigned.
 *
 * @hide
 */
public class InputEventAssigner {
    private static final String TAG = "InputEventAssigner";
    private boolean mHasUnprocessedDown = false;
    private int mEventId = INVALID_INPUT_EVENT_ID;

    /**
     * Notify InputEventAssigner that the Choreographer callback has been processed. This will reset
     * the 'down' state to assign the latest input event to the current frame.
     */
    public void onChoreographerCallback() {
        // Mark completion of this frame. Use newest input event from now on.
        mHasUnprocessedDown = false;
    }

    /**
     * Process the provided input event to determine which event id to assign to the current frame.
     * @param event the input event currently being processed
     * @return the id of the input event to use for the current frame
     */
    public int processEvent(InputEvent event) {
        if (event instanceof KeyEvent) {
            // We will not do any special handling for key events
            return event.getId();
        }

        if (event instanceof MotionEvent) {
            MotionEvent motionEvent = (MotionEvent) event;
            final int action = motionEvent.getActionMasked();

            if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
                mHasUnprocessedDown = false;
            }
            if (motionEvent.isFromSource(SOURCE_TOUCHSCREEN) && action == MotionEvent.ACTION_DOWN) {
                mHasUnprocessedDown = true;
                mEventId = event.getId();
                // This will remain 'true' even if we receive a MOVE event, as long as choreographer
                // hasn't invoked the 'CALLBACK_INPUT' callback.
            }
            // Don't update the event id if we haven't processed DOWN yet.
            if (!mHasUnprocessedDown) {
                mEventId = event.getId();
            }
            return mEventId;
        }

        throw new IllegalArgumentException("Received unexpected " + event);
    }
}
+1 −0
Original line number Original line Diff line number Diff line
@@ -3589,6 +3589,7 @@ public final class MotionEvent extends InputEvent implements Parcelable {
            msg.append(", deviceId=").append(getDeviceId());
            msg.append(", deviceId=").append(getDeviceId());
            msg.append(", source=0x").append(Integer.toHexString(getSource()));
            msg.append(", source=0x").append(Integer.toHexString(getSource()));
            msg.append(", displayId=").append(getDisplayId());
            msg.append(", displayId=").append(getDisplayId());
            msg.append(", eventId=").append(getId());
        }
        }
        msg.append(" }");
        msg.append(" }");
        return msg.toString();
        return msg.toString();
+14 −25
Original line number Original line Diff line number Diff line
@@ -17,6 +17,7 @@
package android.view;
package android.view;


import android.graphics.FrameInfo;
import android.graphics.FrameInfo;
import android.os.IInputConstants;


/**
/**
 * The timing information of events taking place in ViewRootImpl
 * The timing information of events taking place in ViewRootImpl
@@ -24,32 +25,14 @@ import android.graphics.FrameInfo;
 */
 */
public class ViewFrameInfo {
public class ViewFrameInfo {
    public long drawStart;
    public long drawStart;
    public long oldestInputEventTime; // the time of the oldest input event consumed for this frame

    public long newestInputEventTime; // the time of the newest input event consumed for this frame

    // Various flags set to provide extra metadata about the current frame. See flag definitions
    // Various flags set to provide extra metadata about the current frame. See flag definitions
    // inside FrameInfo.
    // inside FrameInfo.
    // @see android.graphics.FrameInfo.FLAG_WINDOW_LAYOUT_CHANGED
    // @see android.graphics.FrameInfo.FLAG_WINDOW_LAYOUT_CHANGED
    public long flags;
    public long flags;


    /**
    private int mInputEventId;
     * Update the oldest event time.
     * @param eventTime the time of the input event
     */
    public void updateOldestInputEvent(long eventTime) {
        if (oldestInputEventTime == 0 || eventTime < oldestInputEventTime) {
            oldestInputEventTime = eventTime;
        }
    }

    /**
     * Update the newest event time.
     * @param eventTime the time of the input event
     */
    public void updateNewestInputEvent(long eventTime) {
        if (newestInputEventTime == 0 || eventTime > newestInputEventTime) {
            newestInputEventTime = eventTime;
        }
    }


    /**
    /**
     * Populate the missing fields using the data from ViewFrameInfo
     * Populate the missing fields using the data from ViewFrameInfo
@@ -58,8 +41,7 @@ public class ViewFrameInfo {
    public void populateFrameInfo(FrameInfo frameInfo) {
    public void populateFrameInfo(FrameInfo frameInfo) {
        frameInfo.frameInfo[FrameInfo.FLAGS] |= flags;
        frameInfo.frameInfo[FrameInfo.FLAGS] |= flags;
        frameInfo.frameInfo[FrameInfo.DRAW_START] = drawStart;
        frameInfo.frameInfo[FrameInfo.DRAW_START] = drawStart;
        // TODO(b/169866723): Use InputEventAssigner
        frameInfo.frameInfo[FrameInfo.INPUT_EVENT_ID] = mInputEventId;
        frameInfo.frameInfo[FrameInfo.INPUT_EVENT_ID] = newestInputEventTime;
    }
    }


    /**
    /**
@@ -67,8 +49,7 @@ public class ViewFrameInfo {
     */
     */
    public void reset() {
    public void reset() {
        drawStart = 0;
        drawStart = 0;
        oldestInputEventTime = 0;
        mInputEventId = IInputConstants.INVALID_INPUT_EVENT_ID;
        newestInputEventTime = 0;
        flags = 0;
        flags = 0;
    }
    }


@@ -78,4 +59,12 @@ public class ViewFrameInfo {
    public void markDrawStart() {
    public void markDrawStart() {
        drawStart = System.nanoTime();
        drawStart = System.nanoTime();
    }
    }

    /**
     * Assign the value for input event id
     * @param eventId the id of the input event
     */
    public void setInputEvent(int eventId) {
        mInputEventId = eventId;
    }
}
}
+7 −10
Original line number Original line Diff line number Diff line
@@ -448,6 +448,7 @@ public final class ViewRootImpl implements ViewParent,
    FallbackEventHandler mFallbackEventHandler;
    FallbackEventHandler mFallbackEventHandler;
    final Choreographer mChoreographer;
    final Choreographer mChoreographer;
    protected final ViewFrameInfo mViewFrameInfo = new ViewFrameInfo();
    protected final ViewFrameInfo mViewFrameInfo = new ViewFrameInfo();
    private final InputEventAssigner mInputEventAssigner = new InputEventAssigner();


    /**
    /**
     * Update the Choreographer's FrameInfo object with the timing information for the current
     * Update the Choreographer's FrameInfo object with the timing information for the current
@@ -8327,16 +8328,7 @@ public final class ViewRootImpl implements ViewParent,
            Trace.traceCounter(Trace.TRACE_TAG_INPUT, mPendingInputEventQueueLengthCounterName,
            Trace.traceCounter(Trace.TRACE_TAG_INPUT, mPendingInputEventQueueLengthCounterName,
                    mPendingInputEventCount);
                    mPendingInputEventCount);


            long eventTime = q.mEvent.getEventTimeNano();
            mViewFrameInfo.setInputEvent(mInputEventAssigner.processEvent(q.mEvent));
            long oldestEventTime = eventTime;
            if (q.mEvent instanceof MotionEvent) {
                MotionEvent me = (MotionEvent)q.mEvent;
                if (me.getHistorySize() > 0) {
                    oldestEventTime = me.getHistoricalEventTimeNano(0);
                }
            }
            mViewFrameInfo.updateOldestInputEvent(oldestEventTime);
            mViewFrameInfo.updateNewestInputEvent(eventTime);


            deliverInputEvent(q);
            deliverInputEvent(q);
        }
        }
@@ -8472,6 +8464,11 @@ public final class ViewRootImpl implements ViewParent,
            consumedBatches = false;
            consumedBatches = false;
        }
        }
        doProcessInputEvents();
        doProcessInputEvents();
        if (consumedBatches) {
            // Must be done after we processed the input events, to mark the completion of the frame
            // from the input point of view
            mInputEventAssigner.onChoreographerCallback();
        }
        return consumedBatches;
        return consumedBatches;
    }
    }


+4 −0
Original line number Original line Diff line number Diff line
@@ -462,6 +462,7 @@ cc_defaults {
    whole_static_libs: ["libskia"],
    whole_static_libs: ["libskia"],


    srcs: [
    srcs: [
        ":inputconstants_aidl",
        "canvas/CanvasFrontend.cpp",
        "canvas/CanvasFrontend.cpp",
        "canvas/CanvasOpBuffer.cpp",
        "canvas/CanvasOpBuffer.cpp",
        "canvas/CanvasOpRasterizer.cpp",
        "canvas/CanvasOpRasterizer.cpp",
@@ -588,6 +589,9 @@ cc_library {
            version_script: "libhwui.map.txt",
            version_script: "libhwui.map.txt",
        }
        }
    },
    },
    shared_libs: [
        "libbinder",
    ],
}
}


cc_library_static {
cc_library_static {
Loading