FrameTracker.h revision 82dbc7429f5f9f2b303b31dc5b9f2bfd1bbe6add
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
17#ifndef ANDROID_FRAMETRACKER_H
18#define ANDROID_FRAMETRACKER_H
19
20#include <stddef.h>
21
22#include <utils/Timers.h>
23#include <utils/RefBase.h>
24
25namespace android {
26
27class String8;
28class Fence;
29
30// FrameTracker tracks information about the most recently rendered frames. It
31// uses a circular buffer of frame records, and is *NOT* thread-safe -
32// mutexing must be done at a higher level if multi-threaded access is
33// possible.
34//
35// Some of the time values tracked may be set either as a specific timestamp
36// or a fence.  When a non-NULL fence is set for a given time value, the
37// signal time of that fence is used instead of the timestamp.
38class FrameTracker {
39
40public:
41    // NUM_FRAME_RECORDS is the size of the circular buffer used to track the
42    // frame time history.
43    enum { NUM_FRAME_RECORDS = 128 };
44
45    FrameTracker();
46
47    // setDesiredPresentTime sets the time at which the current frame
48    // should be presented to the user under ideal (i.e. zero latency)
49    // conditions.
50    void setDesiredPresentTime(nsecs_t desiredPresentTime);
51
52    // setFrameReadyTime sets the time at which the current frame became ready
53    // to be presented to the user.  For example, if the frame contents is
54    // being written to memory by some asynchronous hardware, this would be
55    // the time at which those writes completed.
56    void setFrameReadyTime(nsecs_t readyTime);
57
58    // setFrameReadyFence sets the fence that is used to get the time at which
59    // the current frame became ready to be presented to the user.
60    void setFrameReadyFence(const sp<Fence>& readyFence);
61
62    // setActualPresentTime sets the timestamp at which the current frame became
63    // visible to the user.
64    void setActualPresentTime(nsecs_t displayTime);
65
66    // setActualPresentFence sets the fence that is used to get the time
67    // at which the current frame became visible to the user.
68    void setActualPresentFence(const sp<Fence>& fence);
69
70    // advanceFrame advances the frame tracker to the next frame.
71    void advanceFrame();
72
73    // clear resets all the tracked frame data to zero.
74    void clear();
75
76    // dump appends the current frame display time history to the result string.
77    void dump(String8& result) const;
78
79private:
80    struct FrameRecord {
81        FrameRecord() :
82            desiredPresentTime(0),
83            frameReadyTime(0),
84            actualPresentTime(0) {}
85        nsecs_t desiredPresentTime;
86        nsecs_t frameReadyTime;
87        nsecs_t actualPresentTime;
88        sp<Fence> frameReadyFence;
89        sp<Fence> actualPresentFence;
90    };
91
92    // processFences iterates over all the frame records that have a fence set
93    // and replaces that fence with a timestamp if the fence has signaled.  If
94    // the fence is not signaled the record's displayTime is set to INT64_MAX.
95    //
96    // This method is const because although it modifies the frame records it
97    // does so in such a way that the information represented should not
98    // change.  This allows it to be called from the dump method.
99    void processFences() const;
100
101    // mFrameRecords is the circular buffer storing the tracked data for each
102    // frame.
103    FrameRecord mFrameRecords[NUM_FRAME_RECORDS];
104
105    // mOffset is the offset into mFrameRecords of the current frame.
106    size_t mOffset;
107
108    // mNumFences is the total number of fences set in the frame records.  It
109    // is incremented each time a fence is added and decremented each time a
110    // signaled fence is removed in processFences or if advanceFrame clobbers
111    // a fence.
112    //
113    // The number of fences is tracked so that the run time of processFences
114    // doesn't grow with NUM_FRAME_RECORDS.
115    int mNumFences;
116};
117
118}
119
120#endif // ANDROID_FRAMETRACKER_H
121