| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 22 | #include <hardware/hwcomposer_defs.h> | 
|  | 23 |  | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 24 | #include <ui/FenceTime.h> | 
|  | 25 |  | 
|  | 26 | #include <utils/String16.h> | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 27 | #include <utils/Vector.h> | 
|  | 28 |  | 
| Yiwei Zhang | c5f2c45 | 2018-05-08 16:31:56 -0700 | [diff] [blame] | 29 | #include <deque> | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 30 | #include <mutex> | 
| Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 31 | #include <optional> | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 32 | #include <unordered_map> | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 33 |  | 
|  | 34 | using namespace android::surfaceflinger; | 
|  | 35 |  | 
|  | 36 | namespace android { | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 37 |  | 
|  | 38 | class TimeStats { | 
| Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 39 | public: | 
|  | 40 | virtual ~TimeStats() = default; | 
|  | 41 |  | 
|  | 42 | virtual void parseArgs(bool asProto, const Vector<String16>& args, std::string& result) = 0; | 
|  | 43 | virtual bool isEnabled() = 0; | 
|  | 44 |  | 
|  | 45 | virtual void incrementTotalFrames() = 0; | 
|  | 46 | virtual void incrementMissedFrames() = 0; | 
|  | 47 | virtual void incrementClientCompositionFrames() = 0; | 
|  | 48 |  | 
|  | 49 | virtual void setPostTime(int32_t layerID, uint64_t frameNumber, const std::string& layerName, | 
|  | 50 | nsecs_t postTime) = 0; | 
|  | 51 | virtual void setLatchTime(int32_t layerID, uint64_t frameNumber, nsecs_t latchTime) = 0; | 
|  | 52 | virtual void setDesiredTime(int32_t layerID, uint64_t frameNumber, nsecs_t desiredTime) = 0; | 
|  | 53 | virtual void setAcquireTime(int32_t layerID, uint64_t frameNumber, nsecs_t acquireTime) = 0; | 
|  | 54 | virtual void setAcquireFence(int32_t layerID, uint64_t frameNumber, | 
|  | 55 | const std::shared_ptr<FenceTime>& acquireFence) = 0; | 
|  | 56 | virtual void setPresentTime(int32_t layerID, uint64_t frameNumber, nsecs_t presentTime) = 0; | 
|  | 57 | virtual void setPresentFence(int32_t layerID, uint64_t frameNumber, | 
|  | 58 | const std::shared_ptr<FenceTime>& presentFence) = 0; | 
|  | 59 | // Clean up the layer record | 
|  | 60 | virtual void onDestroy(int32_t layerID) = 0; | 
|  | 61 | // If SF skips or rejects a buffer, remove the corresponding TimeRecord. | 
|  | 62 | virtual void removeTimeRecord(int32_t layerID, uint64_t frameNumber) = 0; | 
|  | 63 |  | 
|  | 64 | virtual void setPowerMode(int32_t powerMode) = 0; | 
|  | 65 | // Source of truth is RefrehRateStats. | 
|  | 66 | virtual void recordRefreshRate(uint32_t fps, nsecs_t duration) = 0; | 
|  | 67 | virtual void setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) = 0; | 
|  | 68 | }; | 
|  | 69 |  | 
|  | 70 | namespace impl { | 
|  | 71 |  | 
|  | 72 | class TimeStats : public android::TimeStats { | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 73 | struct FrameTime { | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 74 | uint64_t frameNumber = 0; | 
|  | 75 | nsecs_t postTime = 0; | 
|  | 76 | nsecs_t latchTime = 0; | 
|  | 77 | nsecs_t acquireTime = 0; | 
|  | 78 | nsecs_t desiredTime = 0; | 
|  | 79 | nsecs_t presentTime = 0; | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 80 | }; | 
|  | 81 |  | 
|  | 82 | struct TimeRecord { | 
|  | 83 | bool ready = false; | 
|  | 84 | FrameTime frameTime; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 85 | std::shared_ptr<FenceTime> acquireFence; | 
|  | 86 | std::shared_ptr<FenceTime> presentFence; | 
|  | 87 | }; | 
|  | 88 |  | 
|  | 89 | struct LayerRecord { | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 90 | std::string layerName; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 91 | // This is the index in timeRecords, at which the timestamps for that | 
|  | 92 | // specific frame are still not fully received. This is not waiting for | 
|  | 93 | // fences to signal, but rather waiting to receive those fences/timestamps. | 
|  | 94 | int32_t waitData = -1; | 
| Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 95 | uint32_t droppedFrames = 0; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 96 | TimeRecord prevTimeRecord; | 
| Yiwei Zhang | c5f2c45 | 2018-05-08 16:31:56 -0700 | [diff] [blame] | 97 | std::deque<TimeRecord> timeRecords; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 98 | }; | 
|  | 99 |  | 
| Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 100 | struct PowerTime { | 
|  | 101 | int32_t powerMode = HWC_POWER_MODE_OFF; | 
|  | 102 | nsecs_t prevTime = 0; | 
|  | 103 | }; | 
|  | 104 |  | 
| Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 105 | struct GlobalRecord { | 
|  | 106 | nsecs_t prevPresentTime = 0; | 
|  | 107 | std::deque<std::shared_ptr<FenceTime>> presentFences; | 
|  | 108 | }; | 
|  | 109 |  | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 110 | public: | 
| Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 111 | TimeStats() = default; | 
| Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 112 |  | 
| Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 113 | void parseArgs(bool asProto, const Vector<String16>& args, std::string& result) override; | 
|  | 114 | bool isEnabled() override; | 
| Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 115 |  | 
| Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 116 | void incrementTotalFrames() override; | 
|  | 117 | void incrementMissedFrames() override; | 
|  | 118 | void incrementClientCompositionFrames() override; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 119 |  | 
| Yiwei Zhang | 8e8fe52 | 2018-11-02 18:34:07 -0700 | [diff] [blame] | 120 | void setPostTime(int32_t layerID, uint64_t frameNumber, const std::string& layerName, | 
| Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 121 | nsecs_t postTime) override; | 
|  | 122 | void setLatchTime(int32_t layerID, uint64_t frameNumber, nsecs_t latchTime) override; | 
|  | 123 | void setDesiredTime(int32_t layerID, uint64_t frameNumber, nsecs_t desiredTime) override; | 
|  | 124 | void setAcquireTime(int32_t layerID, uint64_t frameNumber, nsecs_t acquireTime) override; | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 125 | void setAcquireFence(int32_t layerID, uint64_t frameNumber, | 
| Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 126 | const std::shared_ptr<FenceTime>& acquireFence) override; | 
|  | 127 | void setPresentTime(int32_t layerID, uint64_t frameNumber, nsecs_t presentTime) override; | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 128 | void setPresentFence(int32_t layerID, uint64_t frameNumber, | 
| Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 129 | const std::shared_ptr<FenceTime>& presentFence) override; | 
| Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 130 | // Clean up the layer record | 
| Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 131 | void onDestroy(int32_t layerID) override; | 
| Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 132 | // If SF skips or rejects a buffer, remove the corresponding TimeRecord. | 
| Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 133 | void removeTimeRecord(int32_t layerID, uint64_t frameNumber) override; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 134 |  | 
| Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 135 | void setPowerMode(int32_t powerMode) override; | 
|  | 136 | // Source of truth is RefrehRateStats. | 
|  | 137 | void recordRefreshRate(uint32_t fps, nsecs_t duration) override; | 
|  | 138 | void setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) override; | 
| Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 139 |  | 
| Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 140 | // TODO(zzyiwei): Bound the timeStatsTracker with weighted LRU | 
|  | 141 | // static const size_t MAX_NUM_LAYER_RECORDS = 200; | 
|  | 142 | static const size_t MAX_NUM_TIME_RECORDS = 64; | 
|  | 143 |  | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 144 | private: | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 145 | bool recordReadyLocked(int32_t layerID, TimeRecord* timeRecord); | 
|  | 146 | void flushAvailableRecordsToStatsLocked(int32_t layerID); | 
| Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 147 | void flushPowerTimeLocked(); | 
| Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 148 | void flushAvailableGlobalRecordsToStatsLocked(); | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 149 |  | 
|  | 150 | void enable(); | 
|  | 151 | void disable(); | 
|  | 152 | void clear(); | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 153 | void dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result); | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 154 |  | 
|  | 155 | std::atomic<bool> mEnabled = false; | 
|  | 156 | std::mutex mMutex; | 
| Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 157 | TimeStatsHelper::TimeStatsGlobal mTimeStats; | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 158 | // Hashmap for LayerRecord with layerID as the hash key | 
|  | 159 | std::unordered_map<int32_t, LayerRecord> mTimeStatsTracker; | 
| Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 160 | PowerTime mPowerTime; | 
| Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 161 | GlobalRecord mGlobalRecord; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 162 | }; | 
|  | 163 |  | 
| Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 164 | } // namespace impl | 
|  | 165 |  | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 166 | } // namespace android |