SF: BufferStateLayer cleanup
Clean up some of the logic now that
BufferLayer has been merged into BufferStateLayer.
Test: presubmit
Bug: 238781169
Change-Id: I5114dd8d457638f810fe02d29bfab3444e042d2d
diff --git a/services/surfaceflinger/HwcSlotGenerator.h b/services/surfaceflinger/HwcSlotGenerator.h
new file mode 100644
index 0000000..5a1b6d7
--- /dev/null
+++ b/services/surfaceflinger/HwcSlotGenerator.h
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2022 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 <functional>
+#include <mutex>
+#include <stack>
+#include <unordered_map>
+
+#include "ClientCache.h"
+
+namespace android {
+
+class HwcSlotGenerator : public ClientCache::ErasedRecipient {
+public:
+ HwcSlotGenerator();
+ void bufferErased(const client_cache_t& clientCacheId);
+ int getHwcCacheSlot(const client_cache_t& clientCacheId);
+
+private:
+ friend class SlotGenerationTest;
+ int addCachedBuffer(const client_cache_t& clientCacheId) REQUIRES(mMutex);
+ int getFreeHwcCacheSlot() REQUIRES(mMutex);
+ void evictLeastRecentlyUsed() REQUIRES(mMutex);
+ void eraseBufferLocked(const client_cache_t& clientCacheId) REQUIRES(mMutex);
+
+ struct CachedBufferHash {
+ std::size_t operator()(const client_cache_t& clientCacheId) const {
+ return std::hash<uint64_t>{}(clientCacheId.id);
+ }
+ };
+
+ std::mutex mMutex;
+
+ std::unordered_map<client_cache_t, std::pair<int /*HwcCacheSlot*/, uint64_t /*counter*/>,
+ CachedBufferHash>
+ mCachedBuffers GUARDED_BY(mMutex);
+ std::stack<int /*HwcCacheSlot*/> mFreeHwcCacheSlots GUARDED_BY(mMutex);
+
+ // The cache increments this counter value when a slot is updated or used.
+ // Used to track the least recently-used buffer
+ uint64_t mCounter = 0;
+};
+} // namespace android