SF: Cleanup EventThread Part 2
De-refbase EventThread and a bunch of related classes. Convert from
usign StrongPointer to std::unique_ptr to hold owned references, or bare
pointers for unowned references.
I did not see any need for using std::shared_ptr, or anything else, as
SurfaceFlinger appeared to own all the objects, and they were created
once and not really destroyed afterwards.
Test: Things seem to still work on a Pixel XL
Bug: None
Change-Id: Ifff32118d31bc1bb51e38df695ebe5cf86d2bb6d
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 3774ab1..1054c32 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -424,7 +424,7 @@
postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
}
-class DispSyncSource : public VSyncSource, private DispSync::Callback {
+class DispSyncSource final : public VSyncSource, private DispSync::Callback {
public:
DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
const char* name) :
@@ -435,14 +435,13 @@
mVsyncEventLabel(String8::format("VSYNC-%s", name)),
mDispSync(dispSync),
mCallbackMutex(),
- mCallback(),
mVsyncMutex(),
mPhaseOffset(phaseOffset),
mEnabled(false) {}
- virtual ~DispSyncSource() {}
+ ~DispSyncSource() override = default;
- virtual void setVSyncEnabled(bool enable) {
+ void setVSyncEnabled(bool enable) override {
Mutex::Autolock lock(mVsyncMutex);
if (enable) {
status_t err = mDispSync->addEventListener(mName, mPhaseOffset,
@@ -464,12 +463,12 @@
mEnabled = enable;
}
- virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
+ void setCallback(VSyncSource::Callback* callback) override{
Mutex::Autolock lock(mCallbackMutex);
mCallback = callback;
}
- virtual void setPhaseOffset(nsecs_t phaseOffset) {
+ void setPhaseOffset(nsecs_t phaseOffset) override {
Mutex::Autolock lock(mVsyncMutex);
// Normalize phaseOffset to [0, period)
@@ -506,7 +505,7 @@
private:
virtual void onDispSyncEvent(nsecs_t when) {
- sp<VSyncSource::Callback> callback;
+ VSyncSource::Callback* callback;
{
Mutex::Autolock lock(mCallbackMutex);
callback = mCallback;
@@ -533,37 +532,36 @@
DispSync* mDispSync;
Mutex mCallbackMutex; // Protects the following
- sp<VSyncSource::Callback> mCallback;
+ VSyncSource::Callback* mCallback = nullptr;
Mutex mVsyncMutex; // Protects the following
nsecs_t mPhaseOffset;
bool mEnabled;
};
-class InjectVSyncSource : public VSyncSource {
+class InjectVSyncSource final : public VSyncSource {
public:
- InjectVSyncSource() {}
+ InjectVSyncSource() = default;
+ ~InjectVSyncSource() override = default;
- virtual ~InjectVSyncSource() {}
-
- virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
+ void setCallback(VSyncSource::Callback* callback) override {
std::lock_guard<std::mutex> lock(mCallbackMutex);
mCallback = callback;
}
- virtual void onInjectSyncEvent(nsecs_t when) {
+ void onInjectSyncEvent(nsecs_t when) {
std::lock_guard<std::mutex> lock(mCallbackMutex);
if (mCallback) {
mCallback->onVSyncEvent(when);
}
}
- virtual void setVSyncEnabled(bool) {}
- virtual void setPhaseOffset(nsecs_t) {}
+ void setVSyncEnabled(bool) override {}
+ void setPhaseOffset(nsecs_t) override {}
private:
std::mutex mCallbackMutex; // Protects the following
- sp<VSyncSource::Callback> mCallback;
+ VSyncSource::Callback* mCallback = nullptr;
};
// Do not call property_set on main thread which will be blocked by init
@@ -577,13 +575,16 @@
Mutex::Autolock _l(mStateLock);
// start the EventThread
- sp<VSyncSource> vsyncSrc =
- new DispSyncSource(&mPrimaryDispSync, SurfaceFlinger::vsyncPhaseOffsetNs, true, "app");
- mEventThread = new EventThread(vsyncSrc, *this, false, "appEventThread");
- sp<VSyncSource> sfVsyncSrc =
- new DispSyncSource(&mPrimaryDispSync, SurfaceFlinger::sfVsyncPhaseOffsetNs, true, "sf");
- mSFEventThread = new EventThread(sfVsyncSrc, *this, true, "sfEventThread");
- mEventQueue.setEventThread(mSFEventThread);
+
+ mEventThreadSource = std::make_unique<DispSyncSource>(
+ &mPrimaryDispSync, SurfaceFlinger::vsyncPhaseOffsetNs, true, "app");
+ mEventThread = std::make_unique<EventThread>(
+ mEventThreadSource.get(), *this, false, "sfEventThread");
+ mSfEventThreadSource = std::make_unique<DispSyncSource>(
+ &mPrimaryDispSync, SurfaceFlinger::sfVsyncPhaseOffsetNs, true, "sf");
+ mSFEventThread = std::make_unique<EventThread>(
+ mSfEventThreadSource.get(), *this, true, "appEventThread");
+ mEventQueue.setEventThread(mSFEventThread.get());
// Get a RenderEngine for the given display / config (can't fail)
getBE().mRenderEngine = RenderEngine::create(HAL_PIXEL_FORMAT_RGBA_8888,
@@ -1063,14 +1064,14 @@
if (enable) {
ALOGV("VSync Injections enabled");
if (mVSyncInjector.get() == nullptr) {
- mVSyncInjector = new InjectVSyncSource();
- mInjectorEventThread = new EventThread(mVSyncInjector, *this, false,
- "injEventThread");
+ mVSyncInjector = std::make_unique<InjectVSyncSource>();
+ mInjectorEventThread = std::make_unique<EventThread>(
+ mVSyncInjector.get(), *this, false, "injEvThread");
}
- mEventQueue.setEventThread(mInjectorEventThread);
+ mEventQueue.setEventThread(mInjectorEventThread.get());
} else {
ALOGV("VSync Injections disabled");
- mEventQueue.setEventThread(mSFEventThread);
+ mEventQueue.setEventThread(mSFEventThread.get());
}
mInjectVSyncs = enable;