blob: 1313132920186cb685338e966d3b98b987804b1d [file] [log] [blame]
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001/*
2 * Copyright 2018 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
Mikael Pessa2e1608f2019-07-19 11:25:35 -070019#include <hardware/hwcomposer_defs.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070020#include <timestatsproto/TimeStatsHelper.h>
21#include <timestatsproto/TimeStatsProtoHeader.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070022#include <ui/FenceTime.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070023#include <utils/String16.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070024#include <utils/Vector.h>
25
Yiwei Zhangc5f2c452018-05-08 16:31:56 -070026#include <deque>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070027#include <mutex>
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070028#include <optional>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070029#include <unordered_map>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070030
31using namespace android::surfaceflinger;
32
33namespace android {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070034
35class TimeStats {
Alec Mourifb571ea2019-01-24 18:42:10 -080036public:
37 virtual ~TimeStats() = default;
38
39 virtual void parseArgs(bool asProto, const Vector<String16>& args, std::string& result) = 0;
40 virtual bool isEnabled() = 0;
Oliver Nguyenf3b7c9c2019-05-07 13:28:07 -070041 virtual std::string miniDump() = 0;
Alec Mourifb571ea2019-01-24 18:42:10 -080042
43 virtual void incrementTotalFrames() = 0;
44 virtual void incrementMissedFrames() = 0;
45 virtual void incrementClientCompositionFrames() = 0;
46
47 virtual void setPostTime(int32_t layerID, uint64_t frameNumber, const std::string& layerName,
48 nsecs_t postTime) = 0;
49 virtual void setLatchTime(int32_t layerID, uint64_t frameNumber, nsecs_t latchTime) = 0;
50 virtual void setDesiredTime(int32_t layerID, uint64_t frameNumber, nsecs_t desiredTime) = 0;
51 virtual void setAcquireTime(int32_t layerID, uint64_t frameNumber, nsecs_t acquireTime) = 0;
52 virtual void setAcquireFence(int32_t layerID, uint64_t frameNumber,
53 const std::shared_ptr<FenceTime>& acquireFence) = 0;
54 virtual void setPresentTime(int32_t layerID, uint64_t frameNumber, nsecs_t presentTime) = 0;
55 virtual void setPresentFence(int32_t layerID, uint64_t frameNumber,
56 const std::shared_ptr<FenceTime>& presentFence) = 0;
57 // Clean up the layer record
58 virtual void onDestroy(int32_t layerID) = 0;
59 // If SF skips or rejects a buffer, remove the corresponding TimeRecord.
60 virtual void removeTimeRecord(int32_t layerID, uint64_t frameNumber) = 0;
61
62 virtual void setPowerMode(int32_t powerMode) = 0;
63 // Source of truth is RefrehRateStats.
64 virtual void recordRefreshRate(uint32_t fps, nsecs_t duration) = 0;
65 virtual void setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) = 0;
66};
67
68namespace impl {
69
70class TimeStats : public android::TimeStats {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -070071 struct FrameTime {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070072 uint64_t frameNumber = 0;
73 nsecs_t postTime = 0;
74 nsecs_t latchTime = 0;
75 nsecs_t acquireTime = 0;
76 nsecs_t desiredTime = 0;
77 nsecs_t presentTime = 0;
Yiwei Zhangcf50ab92018-06-14 10:50:12 -070078 };
79
80 struct TimeRecord {
81 bool ready = false;
82 FrameTime frameTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070083 std::shared_ptr<FenceTime> acquireFence;
84 std::shared_ptr<FenceTime> presentFence;
85 };
86
87 struct LayerRecord {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -070088 std::string layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070089 // This is the index in timeRecords, at which the timestamps for that
90 // specific frame are still not fully received. This is not waiting for
91 // fences to signal, but rather waiting to receive those fences/timestamps.
92 int32_t waitData = -1;
Yiwei Zhangeaeea062018-06-28 14:46:51 -070093 uint32_t droppedFrames = 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070094 TimeRecord prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -070095 std::deque<TimeRecord> timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070096 };
97
Yiwei Zhang3a226d22018-10-16 09:23:03 -070098 struct PowerTime {
99 int32_t powerMode = HWC_POWER_MODE_OFF;
100 nsecs_t prevTime = 0;
101 };
102
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700103 struct GlobalRecord {
104 nsecs_t prevPresentTime = 0;
105 std::deque<std::shared_ptr<FenceTime>> presentFences;
106 };
107
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700108public:
Alec Mourib3885ad2019-09-06 17:08:55 -0700109 TimeStats();
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800110
Alec Mourifb571ea2019-01-24 18:42:10 -0800111 void parseArgs(bool asProto, const Vector<String16>& args, std::string& result) override;
112 bool isEnabled() override;
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700113 std::string miniDump() override;
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800114
Alec Mourifb571ea2019-01-24 18:42:10 -0800115 void incrementTotalFrames() override;
116 void incrementMissedFrames() override;
117 void incrementClientCompositionFrames() override;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700118
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700119 void setPostTime(int32_t layerID, uint64_t frameNumber, const std::string& layerName,
Alec Mourifb571ea2019-01-24 18:42:10 -0800120 nsecs_t postTime) override;
121 void setLatchTime(int32_t layerID, uint64_t frameNumber, nsecs_t latchTime) override;
122 void setDesiredTime(int32_t layerID, uint64_t frameNumber, nsecs_t desiredTime) override;
123 void setAcquireTime(int32_t layerID, uint64_t frameNumber, nsecs_t acquireTime) override;
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700124 void setAcquireFence(int32_t layerID, uint64_t frameNumber,
Alec Mourifb571ea2019-01-24 18:42:10 -0800125 const std::shared_ptr<FenceTime>& acquireFence) override;
126 void setPresentTime(int32_t layerID, uint64_t frameNumber, nsecs_t presentTime) override;
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700127 void setPresentFence(int32_t layerID, uint64_t frameNumber,
Alec Mourifb571ea2019-01-24 18:42:10 -0800128 const std::shared_ptr<FenceTime>& presentFence) override;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800129 // Clean up the layer record
Alec Mourifb571ea2019-01-24 18:42:10 -0800130 void onDestroy(int32_t layerID) override;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700131 // If SF skips or rejects a buffer, remove the corresponding TimeRecord.
Alec Mourifb571ea2019-01-24 18:42:10 -0800132 void removeTimeRecord(int32_t layerID, uint64_t frameNumber) override;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700133
Alec Mourifb571ea2019-01-24 18:42:10 -0800134 void setPowerMode(int32_t powerMode) override;
135 // Source of truth is RefrehRateStats.
136 void recordRefreshRate(uint32_t fps, nsecs_t duration) override;
137 void setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) override;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700138
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800139 static const size_t MAX_NUM_TIME_RECORDS = 64;
140
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700141private:
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700142 bool recordReadyLocked(int32_t layerID, TimeRecord* timeRecord);
143 void flushAvailableRecordsToStatsLocked(int32_t layerID);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700144 void flushPowerTimeLocked();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700145 void flushAvailableGlobalRecordsToStatsLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700146
147 void enable();
148 void disable();
149 void clear();
Yiwei Zhang5434a782018-12-05 18:06:32 -0800150 void dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700151
152 std::atomic<bool> mEnabled = false;
153 std::mutex mMutex;
Yiwei Zhangdc224042018-10-18 15:34:00 -0700154 TimeStatsHelper::TimeStatsGlobal mTimeStats;
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700155 // Hashmap for LayerRecord with layerID as the hash key
156 std::unordered_map<int32_t, LayerRecord> mTimeStatsTracker;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700157 PowerTime mPowerTime;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700158 GlobalRecord mGlobalRecord;
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700159
160 static const size_t MAX_NUM_LAYER_RECORDS = 200;
Yiwei Zhange926ab52019-08-14 15:16:00 -0700161 static const size_t MAX_NUM_LAYER_STATS = 200;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700162};
163
Alec Mourifb571ea2019-01-24 18:42:10 -0800164} // namespace impl
165
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700166} // namespace android