blob: c78d84e6f309c67ba3d720628a6b35099158a9d0 [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>
27#include <utils/String8.h>
28#include <utils/Vector.h>
29
Yiwei Zhangc5f2c452018-05-08 16:31:56 -070030#include <deque>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070031#include <mutex>
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070032#include <optional>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070033#include <unordered_map>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070034
35using namespace android::surfaceflinger;
36
37namespace android {
38class String8;
39
40class TimeStats {
41 // TODO(zzyiwei): Bound the timeStatsTracker with weighted LRU
42 // static const size_t MAX_NUM_LAYER_RECORDS = 200;
43 static const size_t MAX_NUM_TIME_RECORDS = 64;
44
Yiwei Zhangcf50ab92018-06-14 10:50:12 -070045 struct FrameTime {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070046 uint64_t frameNumber = 0;
47 nsecs_t postTime = 0;
48 nsecs_t latchTime = 0;
49 nsecs_t acquireTime = 0;
50 nsecs_t desiredTime = 0;
51 nsecs_t presentTime = 0;
Yiwei Zhangcf50ab92018-06-14 10:50:12 -070052 };
53
54 struct TimeRecord {
55 bool ready = false;
56 FrameTime frameTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070057 std::shared_ptr<FenceTime> acquireFence;
58 std::shared_ptr<FenceTime> presentFence;
59 };
60
61 struct LayerRecord {
62 // This is the index in timeRecords, at which the timestamps for that
63 // specific frame are still not fully received. This is not waiting for
64 // fences to signal, but rather waiting to receive those fences/timestamps.
65 int32_t waitData = -1;
Yiwei Zhangeaeea062018-06-28 14:46:51 -070066 uint32_t droppedFrames = 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070067 TimeRecord prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -070068 std::deque<TimeRecord> timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070069 };
70
Yiwei Zhang3a226d22018-10-16 09:23:03 -070071 struct PowerTime {
72 int32_t powerMode = HWC_POWER_MODE_OFF;
73 nsecs_t prevTime = 0;
74 };
75
Yiwei Zhangce6ebc02018-10-20 12:42:38 -070076 struct GlobalRecord {
77 nsecs_t prevPresentTime = 0;
78 std::deque<std::shared_ptr<FenceTime>> presentFences;
79 };
80
Yiwei Zhang0102ad22018-05-02 17:37:17 -070081public:
82 static TimeStats& getInstance();
83 void parseArgs(bool asProto, const Vector<String16>& args, size_t& index, String8& result);
84 void incrementTotalFrames();
Yiwei Zhang621f9d42018-05-07 10:40:55 -070085 void incrementMissedFrames();
Yiwei Zhang0102ad22018-05-02 17:37:17 -070086 void incrementClientCompositionFrames();
87
88 void setPostTime(const std::string& layerName, uint64_t frameNumber, nsecs_t postTime);
89 void setLatchTime(const std::string& layerName, uint64_t frameNumber, nsecs_t latchTime);
90 void setDesiredTime(const std::string& layerName, uint64_t frameNumber, nsecs_t desiredTime);
91 void setAcquireTime(const std::string& layerName, uint64_t frameNumber, nsecs_t acquireTime);
92 void setAcquireFence(const std::string& layerName, uint64_t frameNumber,
93 const std::shared_ptr<FenceTime>& acquireFence);
94 void setPresentTime(const std::string& layerName, uint64_t frameNumber, nsecs_t presentTime);
95 void setPresentFence(const std::string& layerName, uint64_t frameNumber,
96 const std::shared_ptr<FenceTime>& presentFence);
Yiwei Zhangeaeea062018-06-28 14:46:51 -070097 // On producer disconnect with BufferQueue.
Yiwei Zhang0102ad22018-05-02 17:37:17 -070098 void onDisconnect(const std::string& layerName);
Yiwei Zhangdc224042018-10-18 15:34:00 -070099 // On layer tear down.
100 void onDestroy(const std::string& layerName);
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700101 // When SF is cleaning up the queue, clear the LayerRecord as well.
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700102 void clearLayerRecord(const std::string& layerName);
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700103 // If SF skips or rejects a buffer, remove the corresponding TimeRecord.
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700104 void removeTimeRecord(const std::string& layerName, uint64_t frameNumber);
105
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700106 void setPowerMode(int32_t powerMode);
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700107 void setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700108
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700109private:
110 TimeStats() = default;
111
112 bool recordReadyLocked(const std::string& layerName, TimeRecord* timeRecord);
113 void flushAvailableRecordsToStatsLocked(const std::string& layerName);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700114 void flushPowerTimeLocked();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700115 void flushAvailableGlobalRecordsToStatsLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700116
117 void enable();
118 void disable();
119 void clear();
120 bool isEnabled();
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700121 void dump(bool asProto, std::optional<uint32_t> maxLayers, String8& result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700122
123 std::atomic<bool> mEnabled = false;
124 std::mutex mMutex;
Yiwei Zhangdc224042018-10-18 15:34:00 -0700125 TimeStatsHelper::TimeStatsGlobal mTimeStats;
126 std::unordered_map<std::string, LayerRecord> mTimeStatsTracker;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700127 PowerTime mPowerTime;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700128 GlobalRecord mGlobalRecord;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700129};
130
131} // namespace android