blob: e8fbcab9c06436cf1fe775f31bb314a43dd62191 [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 {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -070039 struct FrameTime {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070040 uint64_t frameNumber = 0;
41 nsecs_t postTime = 0;
42 nsecs_t latchTime = 0;
43 nsecs_t acquireTime = 0;
44 nsecs_t desiredTime = 0;
45 nsecs_t presentTime = 0;
Yiwei Zhangcf50ab92018-06-14 10:50:12 -070046 };
47
48 struct TimeRecord {
49 bool ready = false;
50 FrameTime frameTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070051 std::shared_ptr<FenceTime> acquireFence;
52 std::shared_ptr<FenceTime> presentFence;
53 };
54
55 struct LayerRecord {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -070056 std::string layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070057 // This is the index in timeRecords, at which the timestamps for that
58 // specific frame are still not fully received. This is not waiting for
59 // fences to signal, but rather waiting to receive those fences/timestamps.
60 int32_t waitData = -1;
Yiwei Zhangeaeea062018-06-28 14:46:51 -070061 uint32_t droppedFrames = 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070062 TimeRecord prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -070063 std::deque<TimeRecord> timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070064 };
65
Yiwei Zhang3a226d22018-10-16 09:23:03 -070066 struct PowerTime {
67 int32_t powerMode = HWC_POWER_MODE_OFF;
68 nsecs_t prevTime = 0;
69 };
70
Yiwei Zhangce6ebc02018-10-20 12:42:38 -070071 struct GlobalRecord {
72 nsecs_t prevPresentTime = 0;
73 std::deque<std::shared_ptr<FenceTime>> presentFences;
74 };
75
Yiwei Zhang0102ad22018-05-02 17:37:17 -070076public:
Yiwei Zhang7e666a52018-11-15 13:33:42 -080077 TimeStats() = default;
78 ~TimeStats() = default;
79
Dominik Laskowskic2867142019-01-21 11:33:38 -080080 void parseArgs(bool asProto, const Vector<String16>& args, std::string& result);
Yiwei Zhangaf8ee942018-11-22 00:15:23 -080081 bool isEnabled();
Yiwei Zhang7e666a52018-11-15 13:33:42 -080082
Yiwei Zhang0102ad22018-05-02 17:37:17 -070083 void incrementTotalFrames();
Yiwei Zhang621f9d42018-05-07 10:40:55 -070084 void incrementMissedFrames();
Yiwei Zhang0102ad22018-05-02 17:37:17 -070085 void incrementClientCompositionFrames();
86
Yiwei Zhang8e8fe522018-11-02 18:34:07 -070087 void setPostTime(int32_t layerID, uint64_t frameNumber, const std::string& layerName,
88 nsecs_t postTime);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -070089 void setLatchTime(int32_t layerID, uint64_t frameNumber, nsecs_t latchTime);
90 void setDesiredTime(int32_t layerID, uint64_t frameNumber, nsecs_t desiredTime);
91 void setAcquireTime(int32_t layerID, uint64_t frameNumber, nsecs_t acquireTime);
92 void setAcquireFence(int32_t layerID, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -070093 const std::shared_ptr<FenceTime>& acquireFence);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -070094 void setPresentTime(int32_t layerID, uint64_t frameNumber, nsecs_t presentTime);
95 void setPresentFence(int32_t layerID, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -070096 const std::shared_ptr<FenceTime>& presentFence);
Yiwei Zhangaf8ee942018-11-22 00:15:23 -080097 // Clean up the layer record
Yiwei Zhang9689e2f2018-05-11 12:33:23 -070098 void onDestroy(int32_t layerID);
Yiwei Zhangeaeea062018-06-28 14:46:51 -070099 // If SF skips or rejects a buffer, remove the corresponding TimeRecord.
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700100 void removeTimeRecord(int32_t layerID, uint64_t frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700101
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700102 void setPowerMode(int32_t powerMode);
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700103 void setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700104
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800105 // TODO(zzyiwei): Bound the timeStatsTracker with weighted LRU
106 // static const size_t MAX_NUM_LAYER_RECORDS = 200;
107 static const size_t MAX_NUM_TIME_RECORDS = 64;
108
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700109private:
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700110 bool recordReadyLocked(int32_t layerID, TimeRecord* timeRecord);
111 void flushAvailableRecordsToStatsLocked(int32_t layerID);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700112 void flushPowerTimeLocked();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700113 void flushAvailableGlobalRecordsToStatsLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700114
115 void enable();
116 void disable();
117 void clear();
Yiwei Zhang5434a782018-12-05 18:06:32 -0800118 void dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700119
120 std::atomic<bool> mEnabled = false;
121 std::mutex mMutex;
Yiwei Zhangdc224042018-10-18 15:34:00 -0700122 TimeStatsHelper::TimeStatsGlobal mTimeStats;
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700123 // Hashmap for LayerRecord with layerID as the hash key
124 std::unordered_map<int32_t, LayerRecord> mTimeStatsTracker;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700125 PowerTime mPowerTime;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700126 GlobalRecord mGlobalRecord;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700127};
128
129} // namespace android