1*38e8c45fSAndroid Build Coastguard Worker /* 2*38e8c45fSAndroid Build Coastguard Worker * Copyright (C) 2012 The Android Open Source Project 3*38e8c45fSAndroid Build Coastguard Worker * 4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*38e8c45fSAndroid Build Coastguard Worker * 8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*38e8c45fSAndroid Build Coastguard Worker * 10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License. 15*38e8c45fSAndroid Build Coastguard Worker */ 16*38e8c45fSAndroid Build Coastguard Worker 17*38e8c45fSAndroid Build Coastguard Worker #pragma once 18*38e8c45fSAndroid Build Coastguard Worker 19*38e8c45fSAndroid Build Coastguard Worker #include <ui/FenceTime.h> 20*38e8c45fSAndroid Build Coastguard Worker #include <utils/Mutex.h> 21*38e8c45fSAndroid Build Coastguard Worker #include <utils/RefBase.h> 22*38e8c45fSAndroid Build Coastguard Worker #include <utils/Timers.h> 23*38e8c45fSAndroid Build Coastguard Worker 24*38e8c45fSAndroid Build Coastguard Worker #include <cstddef> 25*38e8c45fSAndroid Build Coastguard Worker #include <string_view> 26*38e8c45fSAndroid Build Coastguard Worker 27*38e8c45fSAndroid Build Coastguard Worker namespace android { 28*38e8c45fSAndroid Build Coastguard Worker 29*38e8c45fSAndroid Build Coastguard Worker // FrameTracker tracks information about the most recently rendered frames. It 30*38e8c45fSAndroid Build Coastguard Worker // uses a circular buffer of frame records, and is *NOT* thread-safe - 31*38e8c45fSAndroid Build Coastguard Worker // mutexing must be done at a higher level if multi-threaded access is 32*38e8c45fSAndroid Build Coastguard Worker // possible. 33*38e8c45fSAndroid Build Coastguard Worker // 34*38e8c45fSAndroid Build Coastguard Worker // Some of the time values tracked may be set either as a specific timestamp 35*38e8c45fSAndroid Build Coastguard Worker // or a fence. When a non-nullptr fence is set for a given time value, the 36*38e8c45fSAndroid Build Coastguard Worker // signal time of that fence is used instead of the timestamp. 37*38e8c45fSAndroid Build Coastguard Worker class FrameTracker { 38*38e8c45fSAndroid Build Coastguard Worker 39*38e8c45fSAndroid Build Coastguard Worker public: 40*38e8c45fSAndroid Build Coastguard Worker // NUM_FRAME_RECORDS is the size of the circular buffer used to track the 41*38e8c45fSAndroid Build Coastguard Worker // frame time history. 42*38e8c45fSAndroid Build Coastguard Worker enum { NUM_FRAME_RECORDS = 128 }; 43*38e8c45fSAndroid Build Coastguard Worker 44*38e8c45fSAndroid Build Coastguard Worker FrameTracker(); 45*38e8c45fSAndroid Build Coastguard Worker 46*38e8c45fSAndroid Build Coastguard Worker // setDesiredPresentTime sets the time at which the current frame 47*38e8c45fSAndroid Build Coastguard Worker // should be presented to the user under ideal (i.e. zero latency) 48*38e8c45fSAndroid Build Coastguard Worker // conditions. 49*38e8c45fSAndroid Build Coastguard Worker void setDesiredPresentTime(nsecs_t desiredPresentTime); 50*38e8c45fSAndroid Build Coastguard Worker 51*38e8c45fSAndroid Build Coastguard Worker // setFrameReadyTime sets the time at which the current frame became ready 52*38e8c45fSAndroid Build Coastguard Worker // to be presented to the user. For example, if the frame contents is 53*38e8c45fSAndroid Build Coastguard Worker // being written to memory by some asynchronous hardware, this would be 54*38e8c45fSAndroid Build Coastguard Worker // the time at which those writes completed. 55*38e8c45fSAndroid Build Coastguard Worker void setFrameReadyTime(nsecs_t readyTime); 56*38e8c45fSAndroid Build Coastguard Worker 57*38e8c45fSAndroid Build Coastguard Worker // setFrameReadyFence sets the fence that is used to get the time at which 58*38e8c45fSAndroid Build Coastguard Worker // the current frame became ready to be presented to the user. 59*38e8c45fSAndroid Build Coastguard Worker void setFrameReadyFence(std::shared_ptr<FenceTime>&& readyFence); 60*38e8c45fSAndroid Build Coastguard Worker 61*38e8c45fSAndroid Build Coastguard Worker // setActualPresentTime sets the timestamp at which the current frame became 62*38e8c45fSAndroid Build Coastguard Worker // visible to the user. 63*38e8c45fSAndroid Build Coastguard Worker void setActualPresentTime(nsecs_t displayTime); 64*38e8c45fSAndroid Build Coastguard Worker 65*38e8c45fSAndroid Build Coastguard Worker // setActualPresentFence sets the fence that is used to get the time 66*38e8c45fSAndroid Build Coastguard Worker // at which the current frame became visible to the user. 67*38e8c45fSAndroid Build Coastguard Worker void setActualPresentFence(const std::shared_ptr<FenceTime>& fence); 68*38e8c45fSAndroid Build Coastguard Worker 69*38e8c45fSAndroid Build Coastguard Worker // setDisplayRefreshPeriod sets the display refresh period in nanoseconds. 70*38e8c45fSAndroid Build Coastguard Worker // This is used to compute frame presentation duration statistics relative 71*38e8c45fSAndroid Build Coastguard Worker // to this period. 72*38e8c45fSAndroid Build Coastguard Worker void setDisplayRefreshPeriod(nsecs_t displayPeriod); 73*38e8c45fSAndroid Build Coastguard Worker 74*38e8c45fSAndroid Build Coastguard Worker // advanceFrame advances the frame tracker to the next frame. 75*38e8c45fSAndroid Build Coastguard Worker void advanceFrame(); 76*38e8c45fSAndroid Build Coastguard Worker 77*38e8c45fSAndroid Build Coastguard Worker // clearStats clears the tracked frame stats. 78*38e8c45fSAndroid Build Coastguard Worker void clearStats(); 79*38e8c45fSAndroid Build Coastguard Worker 80*38e8c45fSAndroid Build Coastguard Worker // getStats gets the tracked frame stats. 81*38e8c45fSAndroid Build Coastguard Worker void getStats(FrameStats* outStats) const; 82*38e8c45fSAndroid Build Coastguard Worker 83*38e8c45fSAndroid Build Coastguard Worker // logAndResetStats dumps the current statistics to the binary event log 84*38e8c45fSAndroid Build Coastguard Worker // and then resets the accumulated statistics to their initial values. 85*38e8c45fSAndroid Build Coastguard Worker void logAndResetStats(const std::string_view& name); 86*38e8c45fSAndroid Build Coastguard Worker 87*38e8c45fSAndroid Build Coastguard Worker // dumpStats dump appends the current frame display time history to the result string. 88*38e8c45fSAndroid Build Coastguard Worker void dumpStats(std::string& result) const; 89*38e8c45fSAndroid Build Coastguard Worker 90*38e8c45fSAndroid Build Coastguard Worker private: 91*38e8c45fSAndroid Build Coastguard Worker struct FrameRecord { FrameRecordFrameRecord92*38e8c45fSAndroid Build Coastguard Worker FrameRecord() : 93*38e8c45fSAndroid Build Coastguard Worker desiredPresentTime(0), 94*38e8c45fSAndroid Build Coastguard Worker frameReadyTime(0), 95*38e8c45fSAndroid Build Coastguard Worker actualPresentTime(0) {} 96*38e8c45fSAndroid Build Coastguard Worker nsecs_t desiredPresentTime; 97*38e8c45fSAndroid Build Coastguard Worker nsecs_t frameReadyTime; 98*38e8c45fSAndroid Build Coastguard Worker nsecs_t actualPresentTime; 99*38e8c45fSAndroid Build Coastguard Worker std::shared_ptr<FenceTime> frameReadyFence; 100*38e8c45fSAndroid Build Coastguard Worker std::shared_ptr<FenceTime> actualPresentFence; 101*38e8c45fSAndroid Build Coastguard Worker }; 102*38e8c45fSAndroid Build Coastguard Worker 103*38e8c45fSAndroid Build Coastguard Worker // processFences iterates over all the frame records that have a fence set 104*38e8c45fSAndroid Build Coastguard Worker // and replaces that fence with a timestamp if the fence has signaled. If 105*38e8c45fSAndroid Build Coastguard Worker // the fence is not signaled the record's displayTime is set to INT64_MAX. 106*38e8c45fSAndroid Build Coastguard Worker // 107*38e8c45fSAndroid Build Coastguard Worker // This method is const because although it modifies the frame records it 108*38e8c45fSAndroid Build Coastguard Worker // does so in such a way that the information represented should not 109*38e8c45fSAndroid Build Coastguard Worker // change. This allows it to be called from the dump method. 110*38e8c45fSAndroid Build Coastguard Worker void processFencesLocked() const; 111*38e8c45fSAndroid Build Coastguard Worker 112*38e8c45fSAndroid Build Coastguard Worker // updateStatsLocked updates the running statistics that are gathered 113*38e8c45fSAndroid Build Coastguard Worker // about the frame times. 114*38e8c45fSAndroid Build Coastguard Worker void updateStatsLocked(size_t newFrameIdx) const; 115*38e8c45fSAndroid Build Coastguard Worker 116*38e8c45fSAndroid Build Coastguard Worker // resetFrameCounteresLocked sets all elements of the mNumFrames array to 117*38e8c45fSAndroid Build Coastguard Worker // 0. 118*38e8c45fSAndroid Build Coastguard Worker void resetFrameCountersLocked(); 119*38e8c45fSAndroid Build Coastguard Worker 120*38e8c45fSAndroid Build Coastguard Worker // logStatsLocked dumps the current statistics to the binary event log. 121*38e8c45fSAndroid Build Coastguard Worker void logStatsLocked(const std::string_view& name) const; 122*38e8c45fSAndroid Build Coastguard Worker 123*38e8c45fSAndroid Build Coastguard Worker // isFrameValidLocked returns true if the data for the given frame index is 124*38e8c45fSAndroid Build Coastguard Worker // valid and has all arrived (i.e. there are no oustanding fences). 125*38e8c45fSAndroid Build Coastguard Worker bool isFrameValidLocked(size_t idx) const; 126*38e8c45fSAndroid Build Coastguard Worker 127*38e8c45fSAndroid Build Coastguard Worker // mFrameRecords is the circular buffer storing the tracked data for each 128*38e8c45fSAndroid Build Coastguard Worker // frame. 129*38e8c45fSAndroid Build Coastguard Worker FrameRecord mFrameRecords[NUM_FRAME_RECORDS]; 130*38e8c45fSAndroid Build Coastguard Worker 131*38e8c45fSAndroid Build Coastguard Worker // mOffset is the offset into mFrameRecords of the current frame. 132*38e8c45fSAndroid Build Coastguard Worker size_t mOffset; 133*38e8c45fSAndroid Build Coastguard Worker 134*38e8c45fSAndroid Build Coastguard Worker // mNumFences is the total number of fences set in the frame records. It 135*38e8c45fSAndroid Build Coastguard Worker // is incremented each time a fence is added and decremented each time a 136*38e8c45fSAndroid Build Coastguard Worker // signaled fence is removed in processFences or if advanceFrame clobbers 137*38e8c45fSAndroid Build Coastguard Worker // a fence. 138*38e8c45fSAndroid Build Coastguard Worker // 139*38e8c45fSAndroid Build Coastguard Worker // The number of fences is tracked so that the run time of processFences 140*38e8c45fSAndroid Build Coastguard Worker // doesn't grow with NUM_FRAME_RECORDS. 141*38e8c45fSAndroid Build Coastguard Worker int mNumFences; 142*38e8c45fSAndroid Build Coastguard Worker 143*38e8c45fSAndroid Build Coastguard Worker // mDisplayPeriod is the display refresh period of the display for which 144*38e8c45fSAndroid Build Coastguard Worker // this FrameTracker is gathering information. 145*38e8c45fSAndroid Build Coastguard Worker nsecs_t mDisplayPeriod; 146*38e8c45fSAndroid Build Coastguard Worker 147*38e8c45fSAndroid Build Coastguard Worker // mMutex is used to protect access to all member variables. 148*38e8c45fSAndroid Build Coastguard Worker mutable Mutex mMutex; 149*38e8c45fSAndroid Build Coastguard Worker }; 150*38e8c45fSAndroid Build Coastguard Worker 151*38e8c45fSAndroid Build Coastguard Worker } // namespace android 152