blob: bc412aee2f7a3b4ecdb48d67c1e125cfc05df305 [file] [log] [blame]
Jamie Gennis82dbc742012-11-08 19:23:28 -08001/*
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
Dominik Laskowski87a07e42019-10-10 20:38:02 -070017#pragma once
Jamie Gennis82dbc742012-11-08 19:23:28 -080018
Brian Anderson3d4039d2016-09-23 16:31:30 -070019#include <ui/FenceTime.h>
Jamie Gennis4b0eba92013-02-05 13:30:24 -080020#include <utils/Mutex.h>
Jamie Gennis82dbc742012-11-08 19:23:28 -080021#include <utils/RefBase.h>
Dominik Laskowski87a07e42019-10-10 20:38:02 -070022#include <utils/Timers.h>
23
24#include <cstddef>
25#include <string_view>
Jamie Gennis82dbc742012-11-08 19:23:28 -080026
27namespace android {
28
Jamie Gennis82dbc742012-11-08 19:23:28 -080029// FrameTracker tracks information about the most recently rendered frames. It
30// uses a circular buffer of frame records, and is *NOT* thread-safe -
31// mutexing must be done at a higher level if multi-threaded access is
32// possible.
33//
34// Some of the time values tracked may be set either as a specific timestamp
Peiyong Lin566a3b42018-01-09 18:22:43 -080035// or a fence. When a non-nullptr fence is set for a given time value, the
Jamie Gennis82dbc742012-11-08 19:23:28 -080036// signal time of that fence is used instead of the timestamp.
37class FrameTracker {
38
39public:
40 // NUM_FRAME_RECORDS is the size of the circular buffer used to track the
41 // frame time history.
42 enum { NUM_FRAME_RECORDS = 128 };
43
Jamie Gennis6547ff42013-07-16 20:12:42 -070044 enum { NUM_FRAME_BUCKETS = 7 };
45
Jamie Gennis82dbc742012-11-08 19:23:28 -080046 FrameTracker();
47
48 // setDesiredPresentTime sets the time at which the current frame
49 // should be presented to the user under ideal (i.e. zero latency)
50 // conditions.
51 void setDesiredPresentTime(nsecs_t desiredPresentTime);
52
53 // setFrameReadyTime sets the time at which the current frame became ready
54 // to be presented to the user. For example, if the frame contents is
55 // being written to memory by some asynchronous hardware, this would be
56 // the time at which those writes completed.
57 void setFrameReadyTime(nsecs_t readyTime);
58
59 // setFrameReadyFence sets the fence that is used to get the time at which
60 // the current frame became ready to be presented to the user.
Brian Anderson3d4039d2016-09-23 16:31:30 -070061 void setFrameReadyFence(std::shared_ptr<FenceTime>&& readyFence);
Jamie Gennis82dbc742012-11-08 19:23:28 -080062
63 // setActualPresentTime sets the timestamp at which the current frame became
64 // visible to the user.
65 void setActualPresentTime(nsecs_t displayTime);
66
67 // setActualPresentFence sets the fence that is used to get the time
68 // at which the current frame became visible to the user.
Ady Abraham6c1b7ac2021-03-31 16:56:03 -070069 void setActualPresentFence(const std::shared_ptr<FenceTime>& fence);
Jamie Gennis82dbc742012-11-08 19:23:28 -080070
Jamie Gennis6547ff42013-07-16 20:12:42 -070071 // setDisplayRefreshPeriod sets the display refresh period in nanoseconds.
72 // This is used to compute frame presentation duration statistics relative
73 // to this period.
74 void setDisplayRefreshPeriod(nsecs_t displayPeriod);
75
Jamie Gennis82dbc742012-11-08 19:23:28 -080076 // advanceFrame advances the frame tracker to the next frame.
77 void advanceFrame();
78
Svetoslavd85084b2014-03-20 10:28:31 -070079 // clearStats clears the tracked frame stats.
80 void clearStats();
81
82 // getStats gets the tracked frame stats.
83 void getStats(FrameStats* outStats) const;
Jamie Gennis82dbc742012-11-08 19:23:28 -080084
Jamie Gennis6547ff42013-07-16 20:12:42 -070085 // logAndResetStats dumps the current statistics to the binary event log
86 // and then resets the accumulated statistics to their initial values.
Dominik Laskowski87a07e42019-10-10 20:38:02 -070087 void logAndResetStats(const std::string_view& name);
Jamie Gennis6547ff42013-07-16 20:12:42 -070088
Svetoslavd85084b2014-03-20 10:28:31 -070089 // dumpStats dump appends the current frame display time history to the result string.
Yiwei Zhang5434a782018-12-05 18:06:32 -080090 void dumpStats(std::string& result) const;
Jamie Gennis82dbc742012-11-08 19:23:28 -080091
92private:
93 struct FrameRecord {
94 FrameRecord() :
95 desiredPresentTime(0),
96 frameReadyTime(0),
97 actualPresentTime(0) {}
98 nsecs_t desiredPresentTime;
99 nsecs_t frameReadyTime;
100 nsecs_t actualPresentTime;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700101 std::shared_ptr<FenceTime> frameReadyFence;
102 std::shared_ptr<FenceTime> actualPresentFence;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800103 };
104
105 // processFences iterates over all the frame records that have a fence set
106 // and replaces that fence with a timestamp if the fence has signaled. If
107 // the fence is not signaled the record's displayTime is set to INT64_MAX.
108 //
109 // This method is const because although it modifies the frame records it
110 // does so in such a way that the information represented should not
111 // change. This allows it to be called from the dump method.
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800112 void processFencesLocked() const;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800113
Jamie Gennis6547ff42013-07-16 20:12:42 -0700114 // updateStatsLocked updates the running statistics that are gathered
115 // about the frame times.
116 void updateStatsLocked(size_t newFrameIdx) const;
117
118 // resetFrameCounteresLocked sets all elements of the mNumFrames array to
119 // 0.
120 void resetFrameCountersLocked();
121
122 // logStatsLocked dumps the current statistics to the binary event log.
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700123 void logStatsLocked(const std::string_view& name) const;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700124
125 // isFrameValidLocked returns true if the data for the given frame index is
126 // valid and has all arrived (i.e. there are no oustanding fences).
127 bool isFrameValidLocked(size_t idx) const;
128
Jamie Gennis82dbc742012-11-08 19:23:28 -0800129 // mFrameRecords is the circular buffer storing the tracked data for each
130 // frame.
131 FrameRecord mFrameRecords[NUM_FRAME_RECORDS];
132
133 // mOffset is the offset into mFrameRecords of the current frame.
134 size_t mOffset;
135
136 // mNumFences is the total number of fences set in the frame records. It
137 // is incremented each time a fence is added and decremented each time a
138 // signaled fence is removed in processFences or if advanceFrame clobbers
139 // a fence.
140 //
141 // The number of fences is tracked so that the run time of processFences
142 // doesn't grow with NUM_FRAME_RECORDS.
143 int mNumFences;
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800144
Jamie Gennis6547ff42013-07-16 20:12:42 -0700145 // mNumFrames keeps a count of the number of frames with a duration in a
146 // particular range of vsync periods. Element n of the array stores the
147 // number of frames with duration in the half-inclusive range
148 // [2^n, 2^(n+1)). The last element of the array contains the count for
149 // all frames with duration greater than 2^(NUM_FRAME_BUCKETS-1).
150 int32_t mNumFrames[NUM_FRAME_BUCKETS];
151
152 // mDisplayPeriod is the display refresh period of the display for which
153 // this FrameTracker is gathering information.
154 nsecs_t mDisplayPeriod;
155
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800156 // mMutex is used to protect access to all member variables.
157 mutable Mutex mMutex;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800158};
159
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700160} // namespace android