blob: 670bc8ef0b026012f9c8c19b3f3782f300ccd852 [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
Alec Mouri9519bf12019-11-15 16:54:44 -080047 // Records the start and end times for a frame.
48 // The start time is the same as the beginning of a SurfaceFlinger
49 // invalidate message.
50 // The end time corresponds to when SurfaceFlinger finishes submitting the
51 // request to HWC to present a frame.
52 virtual void recordFrameDuration(nsecs_t startTime, nsecs_t endTime) = 0;
53
Yiwei Zhang1a88c402019-11-18 10:43:58 -080054 virtual void setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Alec Mourifb571ea2019-01-24 18:42:10 -080055 nsecs_t postTime) = 0;
Yiwei Zhang1a88c402019-11-18 10:43:58 -080056 virtual void setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) = 0;
57 virtual void setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) = 0;
58 virtual void setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) = 0;
59 virtual void setAcquireFence(int32_t layerId, uint64_t frameNumber,
Alec Mourifb571ea2019-01-24 18:42:10 -080060 const std::shared_ptr<FenceTime>& acquireFence) = 0;
Yiwei Zhang1a88c402019-11-18 10:43:58 -080061 virtual void setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) = 0;
62 virtual void setPresentFence(int32_t layerId, uint64_t frameNumber,
Alec Mourifb571ea2019-01-24 18:42:10 -080063 const std::shared_ptr<FenceTime>& presentFence) = 0;
64 // Clean up the layer record
Yiwei Zhang1a88c402019-11-18 10:43:58 -080065 virtual void onDestroy(int32_t layerId) = 0;
Alec Mourifb571ea2019-01-24 18:42:10 -080066 // If SF skips or rejects a buffer, remove the corresponding TimeRecord.
Yiwei Zhang1a88c402019-11-18 10:43:58 -080067 virtual void removeTimeRecord(int32_t layerId, uint64_t frameNumber) = 0;
Alec Mourifb571ea2019-01-24 18:42:10 -080068
69 virtual void setPowerMode(int32_t powerMode) = 0;
70 // Source of truth is RefrehRateStats.
71 virtual void recordRefreshRate(uint32_t fps, nsecs_t duration) = 0;
72 virtual void setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) = 0;
73};
74
75namespace impl {
76
77class TimeStats : public android::TimeStats {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -070078 struct FrameTime {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070079 uint64_t frameNumber = 0;
80 nsecs_t postTime = 0;
81 nsecs_t latchTime = 0;
82 nsecs_t acquireTime = 0;
83 nsecs_t desiredTime = 0;
84 nsecs_t presentTime = 0;
Yiwei Zhangcf50ab92018-06-14 10:50:12 -070085 };
86
87 struct TimeRecord {
88 bool ready = false;
89 FrameTime frameTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070090 std::shared_ptr<FenceTime> acquireFence;
91 std::shared_ptr<FenceTime> presentFence;
92 };
93
94 struct LayerRecord {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -070095 std::string layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070096 // This is the index in timeRecords, at which the timestamps for that
97 // specific frame are still not fully received. This is not waiting for
98 // fences to signal, but rather waiting to receive those fences/timestamps.
99 int32_t waitData = -1;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700100 uint32_t droppedFrames = 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700101 TimeRecord prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700102 std::deque<TimeRecord> timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700103 };
104
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700105 struct PowerTime {
106 int32_t powerMode = HWC_POWER_MODE_OFF;
107 nsecs_t prevTime = 0;
108 };
109
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700110 struct GlobalRecord {
111 nsecs_t prevPresentTime = 0;
112 std::deque<std::shared_ptr<FenceTime>> presentFences;
113 };
114
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700115public:
Alec Mourib3885ad2019-09-06 17:08:55 -0700116 TimeStats();
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800117
Alec Mourifb571ea2019-01-24 18:42:10 -0800118 void parseArgs(bool asProto, const Vector<String16>& args, std::string& result) override;
119 bool isEnabled() override;
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700120 std::string miniDump() override;
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800121
Alec Mourifb571ea2019-01-24 18:42:10 -0800122 void incrementTotalFrames() override;
123 void incrementMissedFrames() override;
124 void incrementClientCompositionFrames() override;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700125
Alec Mouri9519bf12019-11-15 16:54:44 -0800126 void recordFrameDuration(nsecs_t startTime, nsecs_t endTime) override;
127
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800128 void setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Alec Mourifb571ea2019-01-24 18:42:10 -0800129 nsecs_t postTime) override;
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800130 void setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) override;
131 void setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) override;
132 void setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) override;
133 void setAcquireFence(int32_t layerId, uint64_t frameNumber,
Alec Mourifb571ea2019-01-24 18:42:10 -0800134 const std::shared_ptr<FenceTime>& acquireFence) override;
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800135 void setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) override;
136 void setPresentFence(int32_t layerId, uint64_t frameNumber,
Alec Mourifb571ea2019-01-24 18:42:10 -0800137 const std::shared_ptr<FenceTime>& presentFence) override;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800138 // Clean up the layer record
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800139 void onDestroy(int32_t layerId) override;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700140 // If SF skips or rejects a buffer, remove the corresponding TimeRecord.
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800141 void removeTimeRecord(int32_t layerId, uint64_t frameNumber) override;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700142
Alec Mourifb571ea2019-01-24 18:42:10 -0800143 void setPowerMode(int32_t powerMode) override;
144 // Source of truth is RefrehRateStats.
145 void recordRefreshRate(uint32_t fps, nsecs_t duration) override;
146 void setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) override;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700147
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800148 static const size_t MAX_NUM_TIME_RECORDS = 64;
149
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700150private:
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800151 bool recordReadyLocked(int32_t layerId, TimeRecord* timeRecord);
152 void flushAvailableRecordsToStatsLocked(int32_t layerId);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700153 void flushPowerTimeLocked();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700154 void flushAvailableGlobalRecordsToStatsLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700155
156 void enable();
157 void disable();
158 void clear();
Yiwei Zhang5434a782018-12-05 18:06:32 -0800159 void dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700160
161 std::atomic<bool> mEnabled = false;
162 std::mutex mMutex;
Yiwei Zhangdc224042018-10-18 15:34:00 -0700163 TimeStatsHelper::TimeStatsGlobal mTimeStats;
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800164 // Hashmap for LayerRecord with layerId as the hash key
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700165 std::unordered_map<int32_t, LayerRecord> mTimeStatsTracker;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700166 PowerTime mPowerTime;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700167 GlobalRecord mGlobalRecord;
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700168
169 static const size_t MAX_NUM_LAYER_RECORDS = 200;
Yiwei Zhange926ab52019-08-14 15:16:00 -0700170 static const size_t MAX_NUM_LAYER_STATS = 200;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700171};
172
Alec Mourifb571ea2019-01-24 18:42:10 -0800173} // namespace impl
174
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700175} // namespace android