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

Commit dae52792 authored by Prabir Pradhan's avatar Prabir Pradhan
Browse files

InputTracer: Trace motion and key events

Bug: 210460522
Test: manual with perfetto
Change-Id: I4bcac0b8bba5132d0e68815923ab8f89b2bede84
parent 2707f8c2
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -28,6 +28,13 @@ flag {
  bug: "297192727"
 }

 flag {
  name: "enable_input_event_tracing"
  namespace: "input"
  description: "Set to true to enable input event tracing, including always-on tracing on non-user builds"
  bug: "210460522"
}

flag {
  name: "enable_multi_device_input"
  namespace: "input"
+1 −0
Original line number Diff line number Diff line
@@ -110,6 +110,7 @@ cc_defaults {
    ],
    static_libs: [
        "libattestation",
        "libperfetto_client_experimental",
        "libpalmrejection",
        "libui-types",
    ],
+2 −0
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ filegroup {
        "Monitor.cpp",
        "TouchedWindow.cpp",
        "TouchState.cpp",
        "trace/*.cpp",
    ],
}

@@ -72,6 +73,7 @@ cc_defaults {
    static_libs: [
        "libattestation",
        "libgui_window_info_static",
        "libperfetto_client_experimental",
    ],
    target: {
        android: {
+14 −2
Original line number Diff line number Diff line
@@ -51,6 +51,8 @@
#include "Connection.h"
#include "DebugConfig.h"
#include "InputDispatcher.h"
#include "trace/InputTracer.h"
#include "trace/InputTracingPerfettoBackend.h"

#define INDENT "  "
#define INDENT2 "    "
@@ -75,6 +77,14 @@ namespace android::inputdispatcher {

namespace {

// Input tracing is only available on debuggable builds (userdebug and eng) when the feature
// flag is enabled. When the flag is changed, tracing will only be available after reboot.
bool isInputTracingEnabled() {
    static const std::string buildType = base::GetProperty("ro.build.type", "user");
    static const bool isUserdebugOrEng = buildType == "userdebug" || buildType == "eng";
    return input_flags::enable_input_event_tracing() && isUserdebugOrEng;
}

template <class Entry>
void ensureEventTraced(const Entry& entry) {
    if (!entry.traceTracker) {
@@ -804,7 +814,9 @@ int32_t getUserActivityEventType(const EventEntry& eventEntry) {
// --- InputDispatcher ---

InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy)
      : InputDispatcher(policy, nullptr) {}
      : InputDispatcher(policy,
                        isInputTracingEnabled() ? std::make_unique<trace::impl::PerfettoBackend>()
                                                : nullptr) {}

InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy,
                                 std::unique_ptr<trace::InputTracingBackendInterface> traceBackend)
@@ -833,7 +845,7 @@ InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy,
    mKeyRepeatState.lastKeyEntry = nullptr;

    if (traceBackend) {
        // TODO: Create input tracer instance.
        mTracer = std::make_unique<trace::impl::InputTracer>(std::move(traceBackend));
    }

    mLastUserActivityTimes.fill(0);
+74 −0
Original line number Diff line number Diff line
/*
 * Copyright 2024 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.
 */

#include "AndroidInputEventProtoConverter.h"

#include <android-base/logging.h>
#include <perfetto/trace/android/android_input_event.pbzero.h>

namespace android::inputdispatcher::trace {

void AndroidInputEventProtoConverter::toProtoMotionEvent(const TracedMotionEvent& event,
                                                         proto::AndroidMotionEvent& outProto) {
    outProto.set_event_id(event.id);
    outProto.set_event_time_nanos(event.eventTime);
    outProto.set_down_time_nanos(event.downTime);
    outProto.set_source(event.source);
    outProto.set_action(event.action);
    outProto.set_device_id(event.deviceId);
    outProto.set_display_id(event.displayId);
    outProto.set_classification(static_cast<int32_t>(event.classification));
    outProto.set_cursor_position_x(event.xCursorPosition);
    outProto.set_cursor_position_y(event.yCursorPosition);
    outProto.set_flags(event.flags);
    outProto.set_policy_flags(event.policyFlags);

    for (uint32_t i = 0; i < event.pointerProperties.size(); i++) {
        auto* pointer = outProto.add_pointer();

        const auto& props = event.pointerProperties[i];
        pointer->set_pointer_id(props.id);
        pointer->set_tool_type(static_cast<int32_t>(props.toolType));

        const auto& coords = event.pointerCoords[i];
        auto bits = BitSet64(coords.bits);
        for (int32_t axisIndex = 0; !bits.isEmpty(); axisIndex++) {
            const auto axis = bits.clearFirstMarkedBit();
            auto axisEntry = pointer->add_axis_value();
            axisEntry->set_axis(axis);
            axisEntry->set_value(coords.values[axisIndex]);
        }
    }
}

void AndroidInputEventProtoConverter::toProtoKeyEvent(const TracedKeyEvent& event,
                                                      proto::AndroidKeyEvent& outProto) {
    outProto.set_event_id(event.id);
    outProto.set_event_time_nanos(event.eventTime);
    outProto.set_down_time_nanos(event.downTime);
    outProto.set_source(event.source);
    outProto.set_action(event.action);
    outProto.set_device_id(event.deviceId);
    outProto.set_display_id(event.displayId);
    outProto.set_key_code(event.keyCode);
    outProto.set_scan_code(event.scanCode);
    outProto.set_meta_state(event.metaState);
    outProto.set_repeat_count(event.repeatCount);
    outProto.set_flags(event.flags);
    outProto.set_policy_flags(event.policyFlags);
}

} // namespace android::inputdispatcher::trace
Loading