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

Commit 39f2a2ee authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Automerger Merge Worker
Browse files

Merge "SF: Modernize task scheduling API" into rvc-dev am: c50b6044

Change-Id: I8c511ab53361d763e5c6f4675d698f5d7075f525
parents 7705d5e6 c50b6044
Loading
Loading
Loading
Loading

services/surfaceflinger/Barrier.h

deleted100644 → 0
+0 −59
Original line number Diff line number Diff line
/*
 * Copyright (C) 2007 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.
 */

#ifndef ANDROID_BARRIER_H
#define ANDROID_BARRIER_H

#include <stdint.h>
#include <condition_variable>
#include <mutex>

namespace android {

class Barrier
{
public:
    // Release any threads waiting at the Barrier.
    // Provides release semantics: preceding loads and stores will be visible
    // to other threads before they wake up.
    void open() {
        std::lock_guard<std::mutex> lock(mMutex);
        mIsOpen = true;
        mCondition.notify_all();
    }

    // Reset the Barrier, so wait() will block until open() has been called.
    void close() {
        std::lock_guard<std::mutex> lock(mMutex);
        mIsOpen = false;
    }

    // Wait until the Barrier is OPEN.
    // Provides acquire semantics: no subsequent loads or stores will occur
    // until wait() returns.
    void wait() const {
        std::unique_lock<std::mutex> lock(mMutex);
        mCondition.wait(lock, [this]() NO_THREAD_SAFETY_ANALYSIS { return mIsOpen; });
    }
private:
    mutable std::mutex mMutex;
    mutable std::condition_variable mCondition;
    int mIsOpen GUARDED_BY(mMutex){false};
};

}; // namespace android

#endif // ANDROID_BARRIER_H
+1 −7
Original line number Diff line number Diff line
@@ -38,13 +38,7 @@ MonitoredProducer::~MonitoredProducer() {
    // because we don't know where this destructor is called from. It could be
    // called with the mStateLock held, leading to a dead-lock (it actually
    // happens).
    sp<LambdaMessage> cleanUpListMessage =
            new LambdaMessage([flinger = mFlinger, asBinder = wp<IBinder>(onAsBinder())]() {
                Mutex::Autolock lock(flinger->mStateLock);
                flinger->mGraphicBufferProducerList.erase(asBinder);
            });

    mFlinger->postMessageAsync(cleanUpListMessage);
    mFlinger->removeGraphicBufferProducerAsync(onAsBinder());
}

status_t MonitoredProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
+4 −36
Original line number Diff line number Diff line
@@ -18,10 +18,6 @@
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wconversion"

#include <errno.h>
#include <stdint.h>
#include <sys/types.h>

#include <binder/IPCThreadState.h>

#include <utils/Log.h>
@@ -35,26 +31,7 @@
#include "MessageQueue.h"
#include "SurfaceFlinger.h"

namespace android {

// ---------------------------------------------------------------------------

MessageBase::MessageBase() : MessageHandler() {}

MessageBase::~MessageBase() {}

void MessageBase::handleMessage(const Message&) {
    this->handler();
    barrier.open();
};

// ---------------------------------------------------------------------------

MessageQueue::~MessageQueue() = default;

// ---------------------------------------------------------------------------

namespace impl {
namespace android::impl {

void MessageQueue::Handler::dispatchRefresh() {
    if ((android_atomic_or(eventMaskRefresh, &mEventMask) & eventMaskRefresh) == 0) {
@@ -123,14 +100,8 @@ void MessageQueue::waitMessage() {
    } while (true);
}

status_t MessageQueue::postMessage(const sp<MessageBase>& messageHandler, nsecs_t relTime) {
    const Message dummyMessage;
    if (relTime > 0) {
        mLooper->sendMessageDelayed(relTime, messageHandler, dummyMessage);
    } else {
        mLooper->sendMessage(messageHandler, dummyMessage);
    }
    return NO_ERROR;
void MessageQueue::postMessage(sp<MessageHandler>&& handler) {
    mLooper->sendMessage(handler, Message());
}

void MessageQueue::invalidate() {
@@ -160,10 +131,7 @@ int MessageQueue::eventReceiver(int /*fd*/, int /*events*/) {
    return 1;
}

// ---------------------------------------------------------------------------

} // namespace impl
} // namespace android
} // namespace android::impl

// TODO(b/129481165): remove the #pragma below and fix conversion issues
#pragma clang diagnostic pop // ignored "-Wconversion"
+21 −47
Original line number Diff line number Diff line
@@ -14,12 +14,12 @@
 * limitations under the License.
 */

#ifndef ANDROID_MESSAGE_QUEUE_H
#define ANDROID_MESSAGE_QUEUE_H
#pragma once

#include <errno.h>
#include <stdint.h>
#include <sys/types.h>
#include <cstdint>
#include <future>
#include <type_traits>
#include <utility>

#include <utils/Looper.h>
#include <utils/Timers.h>
@@ -28,53 +28,31 @@
#include <gui/IDisplayEventConnection.h>
#include <private/gui/BitTube.h>

#include "Barrier.h"
#include "EventThread.h"

#include <functional>

namespace android {

class SurfaceFlinger;

// ---------------------------------------------------------------------------

class MessageBase : public MessageHandler {
public:
    MessageBase();

    // return true if message has a handler
    virtual bool handler() = 0;

    // waits for the handler to be processed
    void wait() const { barrier.wait(); }
template <typename F>
class Task : public MessageHandler {
    template <typename G>
    friend auto makeTask(G&&);

protected:
    virtual ~MessageBase();
    explicit Task(F&& f) : mTask(std::move(f)) {}

private:
    virtual void handleMessage(const Message& message);
    void handleMessage(const Message&) override { mTask(); }

    mutable Barrier barrier;
    using T = std::invoke_result_t<F>;
    std::packaged_task<T()> mTask;
};

class LambdaMessage : public MessageBase {
public:
    explicit LambdaMessage(std::function<void()> handler)
          : MessageBase(), mHandler(std::move(handler)) {}

    bool handler() override {
        mHandler();
        // This return value is no longer checked, so it's always safe to return true
        return true;
template <typename F>
inline auto makeTask(F&& f) {
    sp<Task<F>> task = new Task<F>(std::move(f));
    return std::make_pair(task, task->mTask.get_future());
}

private:
    const std::function<void()> mHandler;
};

// ---------------------------------------------------------------------------

class MessageQueue {
public:
    enum {
@@ -82,12 +60,12 @@ public:
        REFRESH = 1,
    };

    virtual ~MessageQueue();
    virtual ~MessageQueue() = default;

    virtual void init(const sp<SurfaceFlinger>& flinger) = 0;
    virtual void setEventConnection(const sp<EventThreadConnection>& connection) = 0;
    virtual void waitMessage() = 0;
    virtual status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime = 0) = 0;
    virtual void postMessage(sp<MessageHandler>&&) = 0;
    virtual void invalidate() = 0;
    virtual void refresh() = 0;
};
@@ -127,7 +105,7 @@ public:
    void setEventConnection(const sp<EventThreadConnection>& connection) override;

    void waitMessage() override;
    status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime = 0) override;
    void postMessage(sp<MessageHandler>&&) override;

    // sends INVALIDATE message at next VSYNC
    void invalidate() override;
@@ -136,9 +114,5 @@ public:
    void refresh() override;
};

// ---------------------------------------------------------------------------

} // namespace impl
} // namespace android

#endif /* ANDROID_MESSAGE_QUEUE_H */
+135 −179

File changed.

Preview size limit exceeded, changes collapsed.

Loading