SF: Adding Layer History to SF.

Calculate average and median between the timestamp differences that come in for
individual layers, and store them to systrace.

See go/surface-flinger-scheduler for more details and systrace links.

Test: SF tests updated.
Bug: 113612090
Change-Id: I934bbe36ab57d16a5b219c7cb8e0580112238671
diff --git a/services/surfaceflinger/Scheduler/LayerHistory.cpp b/services/surfaceflinger/Scheduler/LayerHistory.cpp
index e944eb9..1f47949 100644
--- a/services/surfaceflinger/Scheduler/LayerHistory.cpp
+++ b/services/surfaceflinger/Scheduler/LayerHistory.cpp
@@ -39,11 +39,15 @@
 void LayerHistory::incrementCounter() {
     mCounter++;
     mCounter = mCounter % ARRAY_SIZE;
+    // Clear all the previous data from the history. This is a ring buffer, so we are
+    // reusing memory.
     mElements[mCounter].clear();
 }
 
 const std::unordered_map<std::string, nsecs_t>& LayerHistory::get(size_t index) const {
-    return mElements.at(index);
+    // For the purposes of the layer history, the index = 0 always needs to start at the
+    // current counter, and then decrement to access the layers in correct historical order.
+    return mElements.at((ARRAY_SIZE + (mCounter - (index % ARRAY_SIZE))) % ARRAY_SIZE);
 }
 
 } // namespace android
\ No newline at end of file