blob: 41d5ef6a9ff580e8575327f10781115ce6bc112f [file]
/*
* Copyright (C) 2025 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.
*/
#pragma once
#include <android-base/thread_annotations.h>
#include <gui/WindowInfosListener.h>
#include <input/Input.h>
#include <utils/RefBase.h>
#include <utils/StrongPointer.h>
#include <flat_set>
#include <memory>
#include <mutex>
#include <vector>
#include "InputTracingBackendInterface.h"
#include "RawEvent.h"
namespace android {
/**
* Tracer implementation for InputReader and associated components.
*/
class InputReaderTracer {
public:
explicit InputReaderTracer(std::shared_ptr<input_trace::InputTracingBackendInterface> backend);
InputReaderTracer(const InputReaderTracer&) = delete;
InputReaderTracer& operator=(const InputReaderTracer&) = delete;
void traceRawEvent(const RawEvent& event);
void traceDeviceAddition(nsecs_t timestamp, const input_trace::TracedEvdevDevice& device);
void traceDeviceRemoval(nsecs_t timestamp, RawDeviceId deviceId);
void traceDevicesSnapshot(
nsecs_t timestamp,
std::unique_ptr<const std::vector<input_trace::TracedEvdevDevice>> devices);
void onWindowInfosChanged(const gui::WindowInfosUpdate& update);
/** Informs the tracer of a change in IME connection state, to be recorded in trace metadata. */
void setImeConnectionActive(bool active);
using IncrementalStateClearedCallback =
input_trace::InputTracingBackendInterface::IncrementalStateClearedCallback;
/**
* Registers a callback to be called whenever incremental state is cleared.
*
* When this happens, any data that's needed to correctly interpret subsequent traced events
* (such as descriptions of evdev devices, which are important context for raw events) should be
* retraced.
*
* The callbacks are run on a Perfetto background thread while holding an internal lock in the
* backend. To avoid deadlocks:
* - The callback must not call registerIncrementalStateClearedCallback or
* deregisterIncrementalStateClearedCallback.
* - The callback must not acquire any locks that are held by callers of
* deregisterIncrementalStateClearedCallback.
*
* @param owner A pointer to the object owning the callback, used to identify it for
* deregistration.
*/
void registerIncrementalStateClearedCallback(void* owner,
IncrementalStateClearedCallback callback);
/** Deregisters a previously registered callback. */
void deregisterIncrementalStateClearedCallback(void* owner);
private:
input_trace::TracedEventMetadata makeMetadata(nsecs_t timestamp);
std::shared_ptr<input_trace::InputTracingBackendInterface> mBackend;
std::mutex mLock;
bool mSecureWindowVisible GUARDED_BY(mLock) = false;
bool mImeConnectionActive GUARDED_BY(mLock) = false;
std::flat_set<gui::Uid> mInputWindowUids GUARDED_BY(mLock);
};
} // namespace android