SF: add partial implementation of VSyncReactor
Adds an adaptor for the VSyncDispatch/VSyncTracker system
that will implemente the DispSync interface. Implementation
is only partial, will build out rest in subsequent MPs.
Bug: 140303479
Test: 6 new unit tests (VSyncReactor)
Change-Id: I11441f0f5bac9095d14122f487ab99bbdf44a7e3
diff --git a/services/surfaceflinger/Scheduler/VSyncReactor.cpp b/services/surfaceflinger/Scheduler/VSyncReactor.cpp
new file mode 100644
index 0000000..1398362
--- /dev/null
+++ b/services/surfaceflinger/Scheduler/VSyncReactor.cpp
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2019 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.
+ */
+
+#include "VSyncReactor.h"
+#include "VSyncDispatch.h"
+#include "VSyncTracker.h"
+
+namespace android::scheduler {
+
+VSyncReactor::VSyncReactor(std::unique_ptr<VSyncDispatch> dispatch,
+ std::unique_ptr<VSyncTracker> tracker, size_t pendingFenceLimit)
+ : mDispatch(std::move(dispatch)),
+ mTracker(std::move(tracker)),
+ mPendingLimit(pendingFenceLimit) {}
+
+bool VSyncReactor::addPresentFence(const std::shared_ptr<FenceTime>& fence) {
+ if (!fence) {
+ return false;
+ }
+
+ nsecs_t const signalTime = fence->getCachedSignalTime();
+ if (signalTime == Fence::SIGNAL_TIME_INVALID) {
+ return true;
+ }
+
+ std::lock_guard<std::mutex> lk(mMutex);
+ if (mIgnorePresentFences) {
+ return true;
+ }
+
+ for (auto it = mUnfiredFences.begin(); it != mUnfiredFences.end();) {
+ auto const time = (*it)->getCachedSignalTime();
+ if (time == Fence::SIGNAL_TIME_PENDING) {
+ it++;
+ } else if (time == Fence::SIGNAL_TIME_INVALID) {
+ it = mUnfiredFences.erase(it);
+ } else {
+ mTracker->addVsyncTimestamp(time);
+ it = mUnfiredFences.erase(it);
+ }
+ }
+
+ if (signalTime == Fence::SIGNAL_TIME_PENDING) {
+ if (mPendingLimit == mUnfiredFences.size()) {
+ mUnfiredFences.erase(mUnfiredFences.begin());
+ }
+ mUnfiredFences.push_back(fence);
+ } else {
+ mTracker->addVsyncTimestamp(signalTime);
+ }
+
+ return false; // TODO(b/144707443): add policy for turning on HWVsync.
+}
+
+void VSyncReactor::setIgnorePresentFences(bool ignoration) {
+ std::lock_guard<std::mutex> lk(mMutex);
+ mIgnorePresentFences = ignoration;
+ if (mIgnorePresentFences == true) {
+ mUnfiredFences.clear();
+ }
+}
+
+} // namespace android::scheduler
diff --git a/services/surfaceflinger/Scheduler/VSyncReactor.h b/services/surfaceflinger/Scheduler/VSyncReactor.h
new file mode 100644
index 0000000..05fd0fd
--- /dev/null
+++ b/services/surfaceflinger/Scheduler/VSyncReactor.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2019 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.
+ */
+
+#pragma once
+
+#include <android-base/thread_annotations.h>
+#include <ui/FenceTime.h>
+#include <memory>
+#include <mutex>
+#include <vector>
+
+namespace android::scheduler {
+
+class VSyncDispatch;
+class VSyncTracker;
+
+// TODO (b/145217110): consider renaming.
+class VSyncReactor /* TODO (b/140201379): : public android::DispSync */ {
+public:
+ VSyncReactor(std::unique_ptr<VSyncDispatch> dispatch, std::unique_ptr<VSyncTracker> tracker,
+ size_t pendingFenceLimit);
+
+ bool addPresentFence(const std::shared_ptr<FenceTime>& fence);
+ void setIgnorePresentFences(bool ignoration);
+
+private:
+ std::unique_ptr<VSyncDispatch> const mDispatch;
+ std::unique_ptr<VSyncTracker> const mTracker;
+ size_t const mPendingLimit;
+
+ std::mutex mMutex;
+ bool mIgnorePresentFences GUARDED_BY(mMutex) = false;
+ std::vector<std::shared_ptr<FenceTime>> mUnfiredFences GUARDED_BY(mMutex);
+};
+
+} // namespace android::scheduler