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

Commit aed7ad07 authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

Use sp<>::make instead of new

For better refcount safety, use sp<>::make instead of new.

After this change, sp<T> t = new T(args..) will be prohibited at compile
time.

Bug: 241125940
Test: m checkinput
Change-Id: I27806ca8f41e8d67744f3569c87a64241318c20b
parent f9323e22
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -38,6 +38,7 @@ cc_defaults {
        "-Wshadow",
        "-Wshadow",
        "-Wshadow-field-in-constructor-modified",
        "-Wshadow-field-in-constructor-modified",
        "-Wshadow-uncaptured-local",
        "-Wshadow-uncaptured-local",
        "-DANDROID_UTILS_REF_BASE_DISABLE_IMPLICIT_CONSTRUCTION",
    ],
    ],
    sanitize: {
    sanitize: {
        misc_undefined: ["bounds"],
        misc_undefined: ["bounds"],
+1 −1
Original line number Original line Diff line number Diff line
@@ -41,7 +41,7 @@ private:


InputThread::InputThread(std::string name, std::function<void()> loop, std::function<void()> wake)
InputThread::InputThread(std::string name, std::function<void()> loop, std::function<void()> wake)
      : mName(name), mThreadWake(wake) {
      : mName(name), mThreadWake(wake) {
    mThread = new InputThreadImpl(loop);
    mThread = sp<InputThreadImpl>::make(loop);
    mThread->run(mName.c_str(), ANDROID_PRIORITY_URGENT_DISPLAY);
    mThread->run(mName.c_str(), ANDROID_PRIORITY_URGENT_DISPLAY);
}
}


+9 −6
Original line number Original line Diff line number Diff line
@@ -260,14 +260,15 @@ static NotifyMotionArgs generateMotionArgs() {


static void benchmarkNotifyMotion(benchmark::State& state) {
static void benchmarkNotifyMotion(benchmark::State& state) {
    // Create dispatcher
    // Create dispatcher
    sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
    sp<FakeInputDispatcherPolicy> fakePolicy = sp<FakeInputDispatcherPolicy>::make();
    InputDispatcher dispatcher(fakePolicy);
    InputDispatcher dispatcher(fakePolicy);
    dispatcher.setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
    dispatcher.setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
    dispatcher.start();
    dispatcher.start();


    // Create a window that will receive motion events
    // Create a window that will receive motion events
    std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
    std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
    sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
    sp<FakeWindowHandle> window =
            sp<FakeWindowHandle>::make(application, dispatcher, "Fake Window");


    dispatcher.setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
    dispatcher.setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});


@@ -294,14 +295,15 @@ static void benchmarkNotifyMotion(benchmark::State& state) {


static void benchmarkInjectMotion(benchmark::State& state) {
static void benchmarkInjectMotion(benchmark::State& state) {
    // Create dispatcher
    // Create dispatcher
    sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
    sp<FakeInputDispatcherPolicy> fakePolicy = sp<FakeInputDispatcherPolicy>::make();
    InputDispatcher dispatcher(fakePolicy);
    InputDispatcher dispatcher(fakePolicy);
    dispatcher.setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
    dispatcher.setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
    dispatcher.start();
    dispatcher.start();


    // Create a window that will receive motion events
    // Create a window that will receive motion events
    std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
    std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
    sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
    sp<FakeWindowHandle> window =
            sp<FakeWindowHandle>::make(application, dispatcher, "Fake Window");


    dispatcher.setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
    dispatcher.setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});


@@ -327,14 +329,15 @@ static void benchmarkInjectMotion(benchmark::State& state) {


static void benchmarkOnWindowInfosChanged(benchmark::State& state) {
static void benchmarkOnWindowInfosChanged(benchmark::State& state) {
    // Create dispatcher
    // Create dispatcher
    sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
    sp<FakeInputDispatcherPolicy> fakePolicy = sp<FakeInputDispatcherPolicy>::make();
    InputDispatcher dispatcher(fakePolicy);
    InputDispatcher dispatcher(fakePolicy);
    dispatcher.setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
    dispatcher.setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
    dispatcher.start();
    dispatcher.start();


    // Create a window
    // Create a window
    std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
    std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
    sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
    sp<FakeWindowHandle> window =
            sp<FakeWindowHandle>::make(application, dispatcher, "Fake Window");


    std::vector<gui::WindowInfo> windowInfos{*window->getInfo()};
    std::vector<gui::WindowInfo> windowInfos{*window->getInfo()};
    gui::DisplayInfo info;
    gui::DisplayInfo info;
+10 −7
Original line number Original line Diff line number Diff line
@@ -555,10 +555,10 @@ InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& polic
        mLatencyAggregator(),
        mLatencyAggregator(),
        mLatencyTracker(&mLatencyAggregator),
        mLatencyTracker(&mLatencyAggregator),
        kPerDisplayTouchModeEnabled(mPolicy->isPerDisplayTouchModeEnabled()) {
        kPerDisplayTouchModeEnabled(mPolicy->isPerDisplayTouchModeEnabled()) {
    mLooper = new Looper(false);
    mLooper = sp<Looper>::make(false);
    mReporter = createInputReporter();
    mReporter = createInputReporter();


    mWindowInfoListener = new DispatcherWindowListener(*this);
    mWindowInfoListener = sp<DispatcherWindowListener>::make(*this);
    SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener);
    SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener);


    mKeyRepeatState.lastKeyEntry = nullptr;
    mKeyRepeatState.lastKeyEntry = nullptr;
@@ -5510,7 +5510,7 @@ Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const
        const sp<IBinder>& token = serverChannel->getConnectionToken();
        const sp<IBinder>& token = serverChannel->getConnectionToken();
        int fd = serverChannel->getFd();
        int fd = serverChannel->getFd();
        sp<Connection> connection =
        sp<Connection> connection =
                new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator);
                sp<Connection>::make(std::move(serverChannel), false /*monitor*/, mIdGenerator);


        if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) {
        if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) {
            ALOGE("Created a new connection, but the token %p is already known", token.get());
            ALOGE("Created a new connection, but the token %p is already known", token.get());
@@ -5520,7 +5520,8 @@ Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const
        std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback,
        std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback,
                                                            this, std::placeholders::_1, token);
                                                            this, std::placeholders::_1, token);


        mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr);
        mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback),
                       nullptr);
    } // release lock
    } // release lock


    // Wake the looper because some connections have changed.
    // Wake the looper because some connections have changed.
@@ -5546,7 +5547,8 @@ Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_
                                          << " without a specified display.";
                                          << " without a specified display.";
        }
        }


        sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator);
        sp<Connection> connection =
                sp<Connection>::make(serverChannel, true /*monitor*/, mIdGenerator);
        const sp<IBinder>& token = serverChannel->getConnectionToken();
        const sp<IBinder>& token = serverChannel->getConnectionToken();
        const int fd = serverChannel->getFd();
        const int fd = serverChannel->getFd();


@@ -5559,7 +5561,8 @@ Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_


        mGlobalMonitorsByDisplay[displayId].emplace_back(serverChannel, pid);
        mGlobalMonitorsByDisplay[displayId].emplace_back(serverChannel, pid);


        mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr);
        mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback),
                       nullptr);
    }
    }


    // Wake the looper because some connections have changed.
    // Wake the looper because some connections have changed.
@@ -6367,7 +6370,7 @@ void InputDispatcher::onWindowInfosChanged(const std::vector<WindowInfo>& window
    std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay;
    std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay;
    for (const auto& info : windowInfos) {
    for (const auto& info : windowInfos) {
        handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>());
        handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>());
        handlesPerDisplay[info.displayId].push_back(new WindowInfoHandle(info));
        handlesPerDisplay[info.displayId].push_back(sp<WindowInfoHandle>::make(info));
    }
    }


    { // acquire lock
    { // acquire lock
+1 −1
Original line number Original line Diff line number Diff line
@@ -35,7 +35,7 @@ void InputReporter::reportDroppedKey(uint32_t sequenceNum) {
}
}


sp<InputReporterInterface> createInputReporter() {
sp<InputReporterInterface> createInputReporter() {
    return new InputReporter();
    return sp<InputReporter>::make();
}
}


} // namespace android
} // namespace android
Loading