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

Commit e50ae003 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Move batching logic from consumeBatchedInputEvents" into main

parents c1ae8999 b41d38e4
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -16,8 +16,8 @@

#pragma once

#include <input/InputTransport.h>
#include <utils/Looper.h>
#include "InputTransport.h"

namespace android {

@@ -181,6 +181,16 @@ private:
     * `consumeBatchedInputEvents`.
     */
    std::map<DeviceId, std::queue<InputMessage>> mBatches;
    /**
     * Creates a MotionEvent by consuming samples from the provided queue. If one message has
     * eventTime > frameTime, all subsequent messages in the queue will be skipped. It is assumed
     * that messages are queued in chronological order. In other words, only events that occurred
     * prior to the requested frameTime will be consumed.
     * @param frameTime the time up to which to consume events
     * @param messages the queue of messages to consume from
     */
    std::pair<std::unique_ptr<MotionEvent>, std::optional<uint32_t>> createBatchedMotionEvent(
            const nsecs_t frameTime, std::queue<InputMessage>& messages);
    /**
     * A map from a single sequence number to several sequence numbers. This is needed because of
     * batching. When batching is enabled, a single MotionEvent will contain several samples. Each
+23 −22
Original line number Diff line number Diff line
@@ -445,6 +445,27 @@ void InputConsumerNoResampling::handleMessage(const InputMessage& msg) const {
    }
}

std::pair<std::unique_ptr<MotionEvent>, std::optional<uint32_t>>
InputConsumerNoResampling::createBatchedMotionEvent(const nsecs_t frameTime,
                                                    std::queue<InputMessage>& messages) {
    std::unique_ptr<MotionEvent> motionEvent;
    std::optional<uint32_t> firstSeqForBatch;
    while (!messages.empty() && !(messages.front().body.motion.eventTime > frameTime)) {
        if (motionEvent == nullptr) {
            motionEvent = createMotionEvent(messages.front());
            firstSeqForBatch = messages.front().header.seq;
            const auto [_, inserted] = mBatchedSequenceNumbers.insert({*firstSeqForBatch, {}});
            LOG_IF(FATAL, !inserted)
                    << "The sequence " << messages.front().header.seq << " was already present!";
        } else {
            addSample(*motionEvent, messages.front());
            mBatchedSequenceNumbers[*firstSeqForBatch].push_back(messages.front().header.seq);
        }
        messages.pop();
    }
    return std::make_pair(std::move(motionEvent), firstSeqForBatch);
}

bool InputConsumerNoResampling::consumeBatchedInputEvents(
        std::optional<nsecs_t> requestedFrameTime) {
    ensureCalledOnLooperThread(__func__);
@@ -452,28 +473,8 @@ bool InputConsumerNoResampling::consumeBatchedInputEvents(
    // infinite frameTime.
    const nsecs_t frameTime = requestedFrameTime.value_or(std::numeric_limits<nsecs_t>::max());
    bool producedEvents = false;
    for (auto& [deviceId, messages] : mBatches) {
        std::unique_ptr<MotionEvent> motion;
        std::optional<uint32_t> firstSeqForBatch;
        std::vector<uint32_t> sequences;
        while (!messages.empty()) {
            const InputMessage& msg = messages.front();
            if (msg.body.motion.eventTime > frameTime) {
                break;
            }
            if (motion == nullptr) {
                motion = createMotionEvent(msg);
                firstSeqForBatch = msg.header.seq;
                const auto [_, inserted] = mBatchedSequenceNumbers.insert({*firstSeqForBatch, {}});
                if (!inserted) {
                    LOG(FATAL) << "The sequence " << msg.header.seq << " was already present!";
                }
            } else {
                addSample(*motion, msg);
                mBatchedSequenceNumbers[*firstSeqForBatch].push_back(msg.header.seq);
            }
            messages.pop();
        }
    for (auto& [_, messages] : mBatches) {
        auto [motion, firstSeqForBatch] = createBatchedMotionEvent(frameTime, messages);
        if (motion != nullptr) {
            LOG_ALWAYS_FATAL_IF(!firstSeqForBatch.has_value());
            mCallbacks.onMotionEvent(std::move(motion), *firstSeqForBatch);