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

Commit 072233fa authored by Ana Krulec's avatar Ana Krulec
Browse files

SF: Move DispSyncSource to STL plus thread safety annotations

This change is part of go/surface-flinger-scheduler

Test: No added tests for now.

Change-Id: I74b53d27999150bad925ab6d2d9ba0cad78a838c
parent 0c6385a1
Loading
Loading
Loading
Loading
+12 −15
Original line number Diff line number Diff line
@@ -16,8 +16,9 @@

#include "DispSyncSource.h"

#include <utils/Mutex.h>
#include <android-base/stringprintf.h>
#include <utils/Trace.h>
#include <mutex>

#include "DispSync.h"
#include "EventThread.h"
@@ -27,42 +28,38 @@ namespace android {
DispSyncSource::DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
                               const char* name)
      : mName(name),
        mValue(0),
        mTraceVsync(traceVsync),
        mVsyncOnLabel(String8::format("VsyncOn-%s", name)),
        mVsyncEventLabel(String8::format("VSYNC-%s", name)),
        mVsyncOnLabel(base::StringPrintf("VsyncOn-%s", name)),
        mVsyncEventLabel(base::StringPrintf("VSYNC-%s", name)),
        mDispSync(dispSync),
        mCallbackMutex(),
        mVsyncMutex(),
        mPhaseOffset(phaseOffset),
        mEnabled(false) {}
        mPhaseOffset(phaseOffset) {}

void DispSyncSource::setVSyncEnabled(bool enable) {
    Mutex::Autolock lock(mVsyncMutex);
    std::lock_guard lock(mVsyncMutex);
    if (enable) {
        status_t err = mDispSync->addEventListener(mName, mPhaseOffset,
                                                   static_cast<DispSync::Callback*>(this));
        if (err != NO_ERROR) {
            ALOGE("error registering vsync callback: %s (%d)", strerror(-err), err);
        }
        // ATRACE_INT(mVsyncOnLabel.string(), 1);
        // ATRACE_INT(mVsyncOnLabel.c_str(), 1);
    } else {
        status_t err = mDispSync->removeEventListener(static_cast<DispSync::Callback*>(this));
        if (err != NO_ERROR) {
            ALOGE("error unregistering vsync callback: %s (%d)", strerror(-err), err);
        }
        // ATRACE_INT(mVsyncOnLabel.string(), 0);
        // ATRACE_INT(mVsyncOnLabel.c_str(), 0);
    }
    mEnabled = enable;
}

void DispSyncSource::setCallback(VSyncSource::Callback* callback) {
    Mutex::Autolock lock(mCallbackMutex);
    std::lock_guard lock(mCallbackMutex);
    mCallback = callback;
}

void DispSyncSource::setPhaseOffset(nsecs_t phaseOffset) {
    Mutex::Autolock lock(mVsyncMutex);
    std::lock_guard lock(mVsyncMutex);

    // Normalize phaseOffset to [0, period)
    auto period = mDispSync->getPeriod();
@@ -89,12 +86,12 @@ void DispSyncSource::setPhaseOffset(nsecs_t phaseOffset) {
void DispSyncSource::onDispSyncEvent(nsecs_t when) {
    VSyncSource::Callback* callback;
    {
        Mutex::Autolock lock(mCallbackMutex);
        std::lock_guard lock(mCallbackMutex);
        callback = mCallback;

        if (mTraceVsync) {
            mValue = (mValue + 1) % 2;
            ATRACE_INT(mVsyncEventLabel.string(), mValue);
            ATRACE_INT(mVsyncEventLabel.c_str(), mValue);
        }
    }

+10 −11
Original line number Diff line number Diff line
@@ -15,8 +15,8 @@
 */
#pragma once

#include <utils/Mutex.h>
#include <utils/String8.h>
#include <mutex>
#include <string>

#include "DispSync.h"
#include "EventThread.h"
@@ -39,21 +39,20 @@ private:
    virtual void onDispSyncEvent(nsecs_t when);

    const char* const mName;

    int mValue;
    int mValue = 0;

    const bool mTraceVsync;
    const String8 mVsyncOnLabel;
    const String8 mVsyncEventLabel;
    const std::string mVsyncOnLabel;
    const std::string mVsyncEventLabel;

    DispSync* mDispSync;

    Mutex mCallbackMutex; // Protects the following
    VSyncSource::Callback* mCallback = nullptr;
    std::mutex mCallbackMutex;
    VSyncSource::Callback* mCallback GUARDED_BY(mCallbackMutex) = nullptr;

    Mutex mVsyncMutex; // Protects the following
    nsecs_t mPhaseOffset;
    bool mEnabled;
    std::mutex mVsyncMutex;
    nsecs_t mPhaseOffset GUARDED_BY(mVsyncMutex);
    bool mEnabled GUARDED_BY(mVsyncMutex) = false;
};

} // namespace android
 No newline at end of file