Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
| 20 | |
Greg Hackmann | 86efcc0 | 2014-03-07 12:44:02 -0800 | [diff] [blame] | 21 | #include <inttypes.h> |
| 22 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 23 | #include <android-base/stringprintf.h> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 24 | #include <android/log.h> |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 25 | |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 26 | #include <ui/FrameStats.h> |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 27 | |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 28 | #include "FrameTracker.h" |
| 29 | |
| 30 | namespace android { |
| 31 | |
Ben Widawsky | 3113546 | 2024-10-08 11:31:20 -0700 | [diff] [blame^] | 32 | FrameTracker::FrameTracker() : mOffset(0), mNumFences(0), mDisplayPeriod(0) {} |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 33 | |
| 34 | void FrameTracker::setDesiredPresentTime(nsecs_t presentTime) { |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 35 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 36 | mFrameRecords[mOffset].desiredPresentTime = presentTime; |
| 37 | } |
| 38 | |
| 39 | void FrameTracker::setFrameReadyTime(nsecs_t readyTime) { |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 40 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 41 | mFrameRecords[mOffset].frameReadyTime = readyTime; |
| 42 | } |
| 43 | |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 44 | void FrameTracker::setFrameReadyFence( |
| 45 | std::shared_ptr<FenceTime>&& readyFence) { |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 46 | Mutex::Autolock lock(mMutex); |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 47 | mFrameRecords[mOffset].frameReadyFence = std::move(readyFence); |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 48 | mNumFences++; |
| 49 | } |
| 50 | |
| 51 | void FrameTracker::setActualPresentTime(nsecs_t presentTime) { |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 52 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 53 | mFrameRecords[mOffset].actualPresentTime = presentTime; |
| 54 | } |
| 55 | |
Ady Abraham | 6c1b7ac | 2021-03-31 16:56:03 -0700 | [diff] [blame] | 56 | void FrameTracker::setActualPresentFence(const std::shared_ptr<FenceTime>& readyFence) { |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 57 | Mutex::Autolock lock(mMutex); |
Ady Abraham | 6c1b7ac | 2021-03-31 16:56:03 -0700 | [diff] [blame] | 58 | mFrameRecords[mOffset].actualPresentFence = readyFence; |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 59 | mNumFences++; |
| 60 | } |
| 61 | |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 62 | void FrameTracker::setDisplayRefreshPeriod(nsecs_t displayPeriod) { |
| 63 | Mutex::Autolock lock(mMutex); |
| 64 | mDisplayPeriod = displayPeriod; |
| 65 | } |
| 66 | |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 67 | void FrameTracker::advanceFrame() { |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 68 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 69 | |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 70 | // Advance to the next frame. |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 71 | mOffset = (mOffset+1) % NUM_FRAME_RECORDS; |
| 72 | mFrameRecords[mOffset].desiredPresentTime = INT64_MAX; |
| 73 | mFrameRecords[mOffset].frameReadyTime = INT64_MAX; |
| 74 | mFrameRecords[mOffset].actualPresentTime = INT64_MAX; |
| 75 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 76 | if (mFrameRecords[mOffset].frameReadyFence != nullptr) { |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 77 | // We're clobbering an unsignaled fence, so we need to decrement the |
| 78 | // fence count. |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 79 | mFrameRecords[mOffset].frameReadyFence = nullptr; |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 80 | mNumFences--; |
| 81 | } |
| 82 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 83 | if (mFrameRecords[mOffset].actualPresentFence != nullptr) { |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 84 | // We're clobbering an unsignaled fence, so we need to decrement the |
| 85 | // fence count. |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 86 | mFrameRecords[mOffset].actualPresentFence = nullptr; |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 87 | mNumFences--; |
| 88 | } |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 91 | void FrameTracker::clearStats() { |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 92 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 93 | for (size_t i = 0; i < NUM_FRAME_RECORDS; i++) { |
| 94 | mFrameRecords[i].desiredPresentTime = 0; |
| 95 | mFrameRecords[i].frameReadyTime = 0; |
| 96 | mFrameRecords[i].actualPresentTime = 0; |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 97 | mFrameRecords[i].frameReadyFence.reset(); |
| 98 | mFrameRecords[i].actualPresentFence.reset(); |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 99 | } |
| 100 | mNumFences = 0; |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 101 | mFrameRecords[mOffset].desiredPresentTime = INT64_MAX; |
| 102 | mFrameRecords[mOffset].frameReadyTime = INT64_MAX; |
| 103 | mFrameRecords[mOffset].actualPresentTime = INT64_MAX; |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 106 | void FrameTracker::getStats(FrameStats* outStats) const { |
| 107 | Mutex::Autolock lock(mMutex); |
| 108 | processFencesLocked(); |
| 109 | |
| 110 | outStats->refreshPeriodNano = mDisplayPeriod; |
| 111 | |
| 112 | const size_t offset = mOffset; |
| 113 | for (size_t i = 1; i < NUM_FRAME_RECORDS; i++) { |
| 114 | const size_t index = (offset + i) % NUM_FRAME_RECORDS; |
| 115 | |
| 116 | // Skip frame records with no data (if buffer not yet full). |
| 117 | if (mFrameRecords[index].desiredPresentTime == 0) { |
| 118 | continue; |
| 119 | } |
| 120 | |
| 121 | nsecs_t desiredPresentTimeNano = mFrameRecords[index].desiredPresentTime; |
| 122 | outStats->desiredPresentTimesNano.push_back(desiredPresentTimeNano); |
| 123 | |
| 124 | nsecs_t actualPresentTimeNano = mFrameRecords[index].actualPresentTime; |
| 125 | outStats->actualPresentTimesNano.push_back(actualPresentTimeNano); |
| 126 | |
| 127 | nsecs_t frameReadyTimeNano = mFrameRecords[index].frameReadyTime; |
| 128 | outStats->frameReadyTimesNano.push_back(frameReadyTimeNano); |
| 129 | } |
| 130 | } |
| 131 | |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 132 | void FrameTracker::processFencesLocked() const { |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 133 | FrameRecord* records = const_cast<FrameRecord*>(mFrameRecords); |
| 134 | int& numFences = const_cast<int&>(mNumFences); |
| 135 | |
| 136 | for (int i = 1; i < NUM_FRAME_RECORDS && numFences > 0; i++) { |
Ben Widawsky | 3113546 | 2024-10-08 11:31:20 -0700 | [diff] [blame^] | 137 | size_t idx = (mOffset + NUM_FRAME_RECORDS - i) % NUM_FRAME_RECORDS; |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 138 | |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 139 | const std::shared_ptr<FenceTime>& rfence = records[idx].frameReadyFence; |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 140 | if (rfence != nullptr) { |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 141 | records[idx].frameReadyTime = rfence->getSignalTime(); |
| 142 | if (records[idx].frameReadyTime < INT64_MAX) { |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 143 | records[idx].frameReadyFence = nullptr; |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 144 | numFences--; |
| 145 | } |
| 146 | } |
| 147 | |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 148 | const std::shared_ptr<FenceTime>& pfence = |
| 149 | records[idx].actualPresentFence; |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 150 | if (pfence != nullptr) { |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 151 | records[idx].actualPresentTime = pfence->getSignalTime(); |
| 152 | if (records[idx].actualPresentTime < INT64_MAX) { |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 153 | records[idx].actualPresentFence = nullptr; |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 154 | numFences--; |
| 155 | } |
| 156 | } |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | bool FrameTracker::isFrameValidLocked(size_t idx) const { |
| 161 | return mFrameRecords[idx].actualPresentTime > 0 && |
| 162 | mFrameRecords[idx].actualPresentTime < INT64_MAX; |
| 163 | } |
| 164 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 165 | void FrameTracker::dumpStats(std::string& result) const { |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 166 | Mutex::Autolock lock(mMutex); |
| 167 | processFencesLocked(); |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 168 | |
| 169 | const size_t o = mOffset; |
| 170 | for (size_t i = 1; i < NUM_FRAME_RECORDS; i++) { |
| 171 | const size_t index = (o+i) % NUM_FRAME_RECORDS; |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 172 | base::StringAppendF(&result, "%" PRId64 "\t%" PRId64 "\t%" PRId64 "\n", |
| 173 | mFrameRecords[index].desiredPresentTime, |
| 174 | mFrameRecords[index].actualPresentTime, |
| 175 | mFrameRecords[index].frameReadyTime); |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 176 | } |
| 177 | result.append("\n"); |
| 178 | } |
| 179 | |
| 180 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 181 | |
| 182 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 183 | #pragma clang diagnostic pop // ignored "-Wconversion" |