1 /*
2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <aidl/android/hardware/power/WorkDuration.h>
20 
21 #include <deque>
22 #include <optional>
23 #include <vector>
24 
25 #include "SessionMetrics.h"
26 
27 namespace aidl {
28 namespace google {
29 namespace hardware {
30 namespace power {
31 namespace impl {
32 namespace pixel {
33 
34 using aidl::android::hardware::power::WorkDuration;
35 class SessionRecords {
36   public:
37     struct CycleRecord {
38         int32_t startIntervalUs{0};
39         int32_t totalDurationUs{0};
40         bool isMissedCycle{false};
41         bool isFPSJitter{false};
42     };
43 
44   public:
45     SessionRecords(const int32_t maxNumOfRecords, const double jankCheckTimeFactor);
46     ~SessionRecords() = default;
47 
48     void addReportedDurations(const std::vector<WorkDuration> &actualDurationsNs,
49                               int64_t targetDurationNs, FrameBuckets &newFramesInBuckets,
50                               bool computeFPSJitters = false);
51     std::optional<int32_t> getMaxDuration();
52     std::optional<int32_t> getAvgDuration();
53     int32_t getNumOfRecords();
54     int32_t getNumOfMissedCycles();
55     bool isLowFrameRate(int32_t fpsLowRateThreshold);
56     void resetRecords();
57     // It will only return valid value when the computeFPSJitters is enabled while
58     // calling addReportedDurations. It's mainly for game mode FPS monitoring.
59     int32_t getLatestFPS() const;
60     int32_t getNumOfFPSJitters() const;
61 
62   private:
63     void updateFrameBuckets(int32_t frameDurationUs, bool isJankFrame,
64                             FrameBuckets &framesInBuckets);
65 
66     const int32_t kMaxNumOfRecords;
67     const double kJankCheckTimeFactor;
68     std::vector<CycleRecord> mRecords;
69     // A descending order queue to store the records' indexes.
70     // It is for detecting the maximum duration.
71     std::deque<int32_t> mRecordsIndQueue;
72     int32_t mAvgDurationUs{0};
73     int64_t mLastStartTimeNs{0};
74     int32_t mLatestRecordIndex{-1};
75     int32_t mNumOfMissedCycles{0};
76     int32_t mNumOfFrames{0};
77     int64_t mSumOfDurationsUs{0};
78 
79     // Compute the sum of start interval for the last few frames.
80     // It can be beneficial for computing the FPS jitters.
81     int32_t mLatestStartIntervalSumUs{0};
82     int32_t mNumOfFrameFPSJitters{0};
83     int32_t mAddedFramesForFPSCheck{0};
84 };
85 
86 }  // namespace pixel
87 }  // namespace impl
88 }  // namespace power
89 }  // namespace hardware
90 }  // namespace google
91 }  // namespace aidl
92