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

Commit bfae3b4a 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 am: 39f2a2ee am: ae6ee19c

Change-Id: I3a1c901f34d55bcbc891d40d5ed468d5d7adf3ca
parents 5021cdb0 ae6ee19c
Loading
Loading
Loading
Loading

services/surfaceflinger/Barrier.h

deleted100644 → 0
+0 −59
Original line number Original line 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 Original line 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
    // 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
    // called with the mStateLock held, leading to a dead-lock (it actually
    // happens).
    // happens).
    sp<LambdaMessage> cleanUpListMessage =
    mFlinger->removeGraphicBufferProducerAsync(onAsBinder());
            new LambdaMessage([flinger = mFlinger, asBinder = wp<IBinder>(onAsBinder())]() {
                Mutex::Autolock lock(flinger->mStateLock);
                flinger->mGraphicBufferProducerList.erase(asBinder);
            });

    mFlinger->postMessageAsync(cleanUpListMessage);
}
}


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


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

#include <binder/IPCThreadState.h>
#include <binder/IPCThreadState.h>


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


namespace android {
namespace android::impl {

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

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

MessageBase::~MessageBase() {}

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

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

MessageQueue::~MessageQueue() = default;

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

namespace impl {


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


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


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


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

} // namespace impl
} // namespace android


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


#ifndef ANDROID_MESSAGE_QUEUE_H
#pragma once
#define ANDROID_MESSAGE_QUEUE_H


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


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


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


#include <functional>

namespace android {
namespace android {


class SurfaceFlinger;
class SurfaceFlinger;


// ---------------------------------------------------------------------------
template <typename F>

class Task : public MessageHandler {
class MessageBase : public MessageHandler {
    template <typename G>
public:
    friend auto makeTask(G&&);
    MessageBase();

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

    // waits for the handler to be processed
    void wait() const { barrier.wait(); }


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


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


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


class LambdaMessage : public MessageBase {
template <typename F>
public:
inline auto makeTask(F&& f) {
    explicit LambdaMessage(std::function<void()> handler)
    sp<Task<F>> task = new Task<F>(std::move(f));
          : MessageBase(), mHandler(std::move(handler)) {}
    return std::make_pair(task, task->mTask.get_future());

    bool handler() override {
        mHandler();
        // This return value is no longer checked, so it's always safe to return true
        return true;
}
}


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

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

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


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


    virtual void init(const sp<SurfaceFlinger>& flinger) = 0;
    virtual void init(const sp<SurfaceFlinger>& flinger) = 0;
    virtual void setEventConnection(const sp<EventThreadConnection>& connection) = 0;
    virtual void setEventConnection(const sp<EventThreadConnection>& connection) = 0;
    virtual void waitMessage() = 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 invalidate() = 0;
    virtual void refresh() = 0;
    virtual void refresh() = 0;
};
};
@@ -127,7 +105,7 @@ public:
    void setEventConnection(const sp<EventThreadConnection>& connection) override;
    void setEventConnection(const sp<EventThreadConnection>& connection) override;


    void waitMessage() 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
    // sends INVALIDATE message at next VSYNC
    void invalidate() override;
    void invalidate() override;
@@ -136,9 +114,5 @@ public:
    void refresh() override;
    void refresh() override;
};
};


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

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

#endif /* ANDROID_MESSAGE_QUEUE_H */
+135 −179

File changed.

Preview size limit exceeded, changes collapsed.

Loading