blob: adcdfb528d9d44d9153880c884add4baccbefdd0 [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
17#ifndef ANDROID_FRAMETRACKER_H
18#define ANDROID_FRAMETRACKER_H
19
Brian Anderson3d4039d2016-09-23 16:31:30 -070020#include <ui/FenceTime.h>
21
Jamie Gennis82dbc742012-11-08 19:23:28 -080022#include <stddef.h>
23
Jamie Gennis4b0eba92013-02-05 13:30:24 -080024#include <utils/Mutex.h>
Jamie Gennis82dbc742012-11-08 19:23:28 -080025#include <utils/Timers.h>
26#include <utils/RefBase.h>
27
28namespace android {
29
30class String8;
Jamie Gennis82dbc742012-11-08 19:23:28 -080031
32// FrameTracker tracks information about the most recently rendered frames. It
33// uses a circular buffer of frame records, and is *NOT* thread-safe -
34// mutexing must be done at a higher level if multi-threaded access is
35// possible.
36//
37// Some of the time values tracked may be set either as a specific timestamp
38// or a fence. When a non-NULL fence is set for a given time value, the
39// signal time of that fence is used instead of the timestamp.
40class FrameTracker {
41
42public:
43 // NUM_FRAME_RECORDS is the size of the circular buffer used to track the
44 // frame time history.
45 enum { NUM_FRAME_RECORDS = 128 };
46
Jamie Gennis6547ff42013-07-16 20:12:42 -070047 enum { NUM_FRAME_BUCKETS = 7 };
48
Jamie Gennis82dbc742012-11-08 19:23:28 -080049 FrameTracker();
50
51 // setDesiredPresentTime sets the time at which the current frame
52 // should be presented to the user under ideal (i.e. zero latency)
53 // conditions.
54 void setDesiredPresentTime(nsecs_t desiredPresentTime);
55
56 // setFrameReadyTime sets the time at which the current frame became ready
57 // to be presented to the user. For example, if the frame contents is
58 // being written to memory by some asynchronous hardware, this would be
59 // the time at which those writes completed.
60 void setFrameReadyTime(nsecs_t readyTime);
61
62 // setFrameReadyFence sets the fence that is used to get the time at which
63 // the current frame became ready to be presented to the user.
Brian Anderson3d4039d2016-09-23 16:31:30 -070064 void setFrameReadyFence(std::shared_ptr<FenceTime>&& readyFence);
Jamie Gennis82dbc742012-11-08 19:23:28 -080065
66 // setActualPresentTime sets the timestamp at which the current frame became
67 // visible to the user.
68 void setActualPresentTime(nsecs_t displayTime);
69
70 // setActualPresentFence sets the fence that is used to get the time
71 // at which the current frame became visible to the user.
Brian Anderson3d4039d2016-09-23 16:31:30 -070072 void setActualPresentFence(std::shared_ptr<FenceTime>&& fence);
Jamie Gennis82dbc742012-11-08 19:23:28 -080073
Jamie Gennis6547ff42013-07-16 20:12:42 -070074 // setDisplayRefreshPeriod sets the display refresh period in nanoseconds.
75 // This is used to compute frame presentation duration statistics relative
76 // to this period.
77 void setDisplayRefreshPeriod(nsecs_t displayPeriod);
78
Jamie Gennis82dbc742012-11-08 19:23:28 -080079 // advanceFrame advances the frame tracker to the next frame.
80 void advanceFrame();
81
Svetoslavd85084b2014-03-20 10:28:31 -070082 // clearStats clears the tracked frame stats.
83 void clearStats();
84
85 // getStats gets the tracked frame stats.
86 void getStats(FrameStats* outStats) const;
Jamie Gennis82dbc742012-11-08 19:23:28 -080087
Jamie Gennis6547ff42013-07-16 20:12:42 -070088 // logAndResetStats dumps the current statistics to the binary event log
89 // and then resets the accumulated statistics to their initial values.
90 void logAndResetStats(const String8& name);
91
Svetoslavd85084b2014-03-20 10:28:31 -070092 // dumpStats dump appends the current frame display time history to the result string.
93 void dumpStats(String8& result) const;
Jamie Gennis82dbc742012-11-08 19:23:28 -080094
95private:
96 struct FrameRecord {
97 FrameRecord() :
98 desiredPresentTime(0),
99 frameReadyTime(0),
100 actualPresentTime(0) {}
101 nsecs_t desiredPresentTime;
102 nsecs_t frameReadyTime;
103 nsecs_t actualPresentTime;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700104 std::shared_ptr<FenceTime> frameReadyFence;
105 std::shared_ptr<FenceTime> actualPresentFence;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800106 };
107
108 // processFences iterates over all the frame records that have a fence set
109 // and replaces that fence with a timestamp if the fence has signaled. If
110 // the fence is not signaled the record's displayTime is set to INT64_MAX.
111 //
112 // This method is const because although it modifies the frame records it
113 // does so in such a way that the information represented should not
114 // change. This allows it to be called from the dump method.
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800115 void processFencesLocked() const;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800116
Jamie Gennis6547ff42013-07-16 20:12:42 -0700117 // updateStatsLocked updates the running statistics that are gathered
118 // about the frame times.
119 void updateStatsLocked(size_t newFrameIdx) const;
120
121 // resetFrameCounteresLocked sets all elements of the mNumFrames array to
122 // 0.
123 void resetFrameCountersLocked();
124
125 // logStatsLocked dumps the current statistics to the binary event log.
126 void logStatsLocked(const String8& name) const;
127
128 // isFrameValidLocked returns true if the data for the given frame index is
129 // valid and has all arrived (i.e. there are no oustanding fences).
130 bool isFrameValidLocked(size_t idx) const;
131
Jamie Gennis82dbc742012-11-08 19:23:28 -0800132 // mFrameRecords is the circular buffer storing the tracked data for each
133 // frame.
134 FrameRecord mFrameRecords[NUM_FRAME_RECORDS];
135
136 // mOffset is the offset into mFrameRecords of the current frame.
137 size_t mOffset;
138
139 // mNumFences is the total number of fences set in the frame records. It
140 // is incremented each time a fence is added and decremented each time a
141 // signaled fence is removed in processFences or if advanceFrame clobbers
142 // a fence.
143 //
144 // The number of fences is tracked so that the run time of processFences
145 // doesn't grow with NUM_FRAME_RECORDS.
146 int mNumFences;
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800147
Jamie Gennis6547ff42013-07-16 20:12:42 -0700148 // mNumFrames keeps a count of the number of frames with a duration in a
149 // particular range of vsync periods. Element n of the array stores the
150 // number of frames with duration in the half-inclusive range
151 // [2^n, 2^(n+1)). The last element of the array contains the count for
152 // all frames with duration greater than 2^(NUM_FRAME_BUCKETS-1).
153 int32_t mNumFrames[NUM_FRAME_BUCKETS];
154
155 // mDisplayPeriod is the display refresh period of the display for which
156 // this FrameTracker is gathering information.
157 nsecs_t mDisplayPeriod;
158
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800159 // mMutex is used to protect access to all member variables.
160 mutable Mutex mMutex;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800161};
162
163}
164
165#endif // ANDROID_FRAMETRACKER_H