blob: 2bcb5682b02bd361fc9cb200f2cd0d06cf0bbe83 [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
19#include <timestatsproto/TimeStatsHelper.h>
20#include <timestatsproto/TimeStatsProtoHeader.h>
21
Yiwei Zhang3a226d22018-10-16 09:23:03 -070022#include <hardware/hwcomposer_defs.h>
23
Yiwei Zhang0102ad22018-05-02 17:37:17 -070024#include <ui/FenceTime.h>
25
26#include <utils/String16.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070027#include <utils/Vector.h>
28
Yiwei Zhangc5f2c452018-05-08 16:31:56 -070029#include <deque>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070030#include <mutex>
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070031#include <optional>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070032#include <unordered_map>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070033
34using namespace android::surfaceflinger;
35
36namespace android {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070037
38class TimeStats {
Alec Mourifb571ea2019-01-24 18:42:10 -080039public:
40 virtual ~TimeStats() = default;
41
42 virtual void parseArgs(bool asProto, const Vector<String16>& args, std::string& result) = 0;
43 virtual bool isEnabled() = 0;
Oliver Nguyend0d943a2019-05-07 13:28:07 -070044 virtual std::string miniDump() = 0;
Alec Mourifb571ea2019-01-24 18:42:10 -080045
46 virtual void incrementTotalFrames() = 0;
47 virtual void incrementMissedFrames() = 0;
48 virtual void incrementClientCompositionFrames() = 0;
49
50 virtual void setPostTime(int32_t layerID, uint64_t frameNumber, const std::string& layerName,
51 nsecs_t postTime) = 0;
52 virtual void setLatchTime(int32_t layerID, uint64_t frameNumber, nsecs_t latchTime) = 0;
53 virtual void setDesiredTime(int32_t layerID, uint64_t frameNumber, nsecs_t desiredTime) = 0;
54 virtual void setAcquireTime(int32_t layerID, uint64_t frameNumber, nsecs_t acquireTime) = 0;
55 virtual void setAcquireFence(int32_t layerID, uint64_t frameNumber,
56 const std::shared_ptr<FenceTime>& acquireFence) = 0;
57 virtual void setPresentTime(int32_t layerID, uint64_t frameNumber, nsecs_t presentTime) = 0;
58 virtual void setPresentFence(int32_t layerID, uint64_t frameNumber,
59 const std::shared_ptr<FenceTime>& presentFence) = 0;
60 // Clean up the layer record
61 virtual void onDestroy(int32_t layerID) = 0;
62 // If SF skips or rejects a buffer, remove the corresponding TimeRecord.
63 virtual void removeTimeRecord(int32_t layerID, uint64_t frameNumber) = 0;
64
65 virtual void setPowerMode(int32_t powerMode) = 0;
66 // Source of truth is RefrehRateStats.
67 virtual void recordRefreshRate(uint32_t fps, nsecs_t duration) = 0;
68 virtual void setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) = 0;
69};
70
71namespace impl {
72
73class TimeStats : public android::TimeStats {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -070074 struct FrameTime {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070075 uint64_t frameNumber = 0;
76 nsecs_t postTime = 0;
77 nsecs_t latchTime = 0;
78 nsecs_t acquireTime = 0;
79 nsecs_t desiredTime = 0;
80 nsecs_t presentTime = 0;
Yiwei Zhangcf50ab92018-06-14 10:50:12 -070081 };
82
83 struct TimeRecord {
84 bool ready = false;
85 FrameTime frameTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070086 std::shared_ptr<FenceTime> acquireFence;
87 std::shared_ptr<FenceTime> presentFence;
88 };
89
90 struct LayerRecord {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -070091 std::string layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070092 // This is the index in timeRecords, at which the timestamps for that
93 // specific frame are still not fully received. This is not waiting for
94 // fences to signal, but rather waiting to receive those fences/timestamps.
95 int32_t waitData = -1;
Yiwei Zhangeaeea062018-06-28 14:46:51 -070096 uint32_t droppedFrames = 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070097 TimeRecord prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -070098 std::deque<TimeRecord> timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070099 };
100
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700101 struct PowerTime {
102 int32_t powerMode = HWC_POWER_MODE_OFF;
103 nsecs_t prevTime = 0;
104 };
105
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700106 struct GlobalRecord {
107 nsecs_t prevPresentTime = 0;
108 std::deque<std::shared_ptr<FenceTime>> presentFences;
109 };
110
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700111public:
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800112 TimeStats() = default;
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800113
Alec Mourifb571ea2019-01-24 18:42:10 -0800114 void parseArgs(bool asProto, const Vector<String16>& args, std::string& result) override;
115 bool isEnabled() override;
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700116 std::string miniDump() override;
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800117
Alec Mourifb571ea2019-01-24 18:42:10 -0800118 void incrementTotalFrames() override;
119 void incrementMissedFrames() override;
120 void incrementClientCompositionFrames() override;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700121
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700122 void setPostTime(int32_t layerID, uint64_t frameNumber, const std::string& layerName,
Alec Mourifb571ea2019-01-24 18:42:10 -0800123 nsecs_t postTime) override;
124 void setLatchTime(int32_t layerID, uint64_t frameNumber, nsecs_t latchTime) override;
125 void setDesiredTime(int32_t layerID, uint64_t frameNumber, nsecs_t desiredTime) override;
126 void setAcquireTime(int32_t layerID, uint64_t frameNumber, nsecs_t acquireTime) override;
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700127 void setAcquireFence(int32_t layerID, uint64_t frameNumber,
Alec Mourifb571ea2019-01-24 18:42:10 -0800128 const std::shared_ptr<FenceTime>& acquireFence) override;
129 void setPresentTime(int32_t layerID, uint64_t frameNumber, nsecs_t presentTime) override;
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700130 void setPresentFence(int32_t layerID, uint64_t frameNumber,
Alec Mourifb571ea2019-01-24 18:42:10 -0800131 const std::shared_ptr<FenceTime>& presentFence) override;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800132 // Clean up the layer record
Alec Mourifb571ea2019-01-24 18:42:10 -0800133 void onDestroy(int32_t layerID) override;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700134 // If SF skips or rejects a buffer, remove the corresponding TimeRecord.
Alec Mourifb571ea2019-01-24 18:42:10 -0800135 void removeTimeRecord(int32_t layerID, uint64_t frameNumber) override;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700136
Alec Mourifb571ea2019-01-24 18:42:10 -0800137 void setPowerMode(int32_t powerMode) override;
138 // Source of truth is RefrehRateStats.
139 void recordRefreshRate(uint32_t fps, nsecs_t duration) override;
140 void setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) override;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700141
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800142 static const size_t MAX_NUM_TIME_RECORDS = 64;
143
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700144private:
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700145 bool recordReadyLocked(int32_t layerID, TimeRecord* timeRecord);
146 void flushAvailableRecordsToStatsLocked(int32_t layerID);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700147 void flushPowerTimeLocked();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700148 void flushAvailableGlobalRecordsToStatsLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700149
150 void enable();
151 void disable();
152 void clear();
Yiwei Zhang5434a782018-12-05 18:06:32 -0800153 void dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700154
155 std::atomic<bool> mEnabled = false;
156 std::mutex mMutex;
Yiwei Zhangdc224042018-10-18 15:34:00 -0700157 TimeStatsHelper::TimeStatsGlobal mTimeStats;
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700158 // Hashmap for LayerRecord with layerID as the hash key
159 std::unordered_map<int32_t, LayerRecord> mTimeStatsTracker;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700160 PowerTime mPowerTime;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700161 GlobalRecord mGlobalRecord;
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700162
163 static const size_t MAX_NUM_LAYER_RECORDS = 200;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700164};
165
Alec Mourifb571ea2019-01-24 18:42:10 -0800166} // namespace impl
167
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700168} // namespace android