FrameTracker.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #ifndef ANDROID_FRAMETRACKER_H
  17. #define ANDROID_FRAMETRACKER_H
  18. #include <stddef.h>
  19. #include <utils/Mutex.h>
  20. #include <utils/Timers.h>
  21. #include <utils/RefBase.h>
  22. namespace android {
  23. class String8;
  24. class Fence;
  25. // FrameTracker tracks information about the most recently rendered frames. It
  26. // uses a circular buffer of frame records, and is *NOT* thread-safe -
  27. // mutexing must be done at a higher level if multi-threaded access is
  28. // possible.
  29. //
  30. // Some of the time values tracked may be set either as a specific timestamp
  31. // or a fence. When a non-NULL fence is set for a given time value, the
  32. // signal time of that fence is used instead of the timestamp.
  33. class FrameTracker {
  34. public:
  35. // NUM_FRAME_RECORDS is the size of the circular buffer used to track the
  36. // frame time history.
  37. enum { NUM_FRAME_RECORDS = 128 };
  38. enum { NUM_FRAME_BUCKETS = 7 };
  39. FrameTracker();
  40. // setDesiredPresentTime sets the time at which the current frame
  41. // should be presented to the user under ideal (i.e. zero latency)
  42. // conditions.
  43. void setDesiredPresentTime(nsecs_t desiredPresentTime);
  44. // setFrameReadyTime sets the time at which the current frame became ready
  45. // to be presented to the user. For example, if the frame contents is
  46. // being written to memory by some asynchronous hardware, this would be
  47. // the time at which those writes completed.
  48. void setFrameReadyTime(nsecs_t readyTime);
  49. // setFrameReadyFence sets the fence that is used to get the time at which
  50. // the current frame became ready to be presented to the user.
  51. void setFrameReadyFence(const sp<Fence>& readyFence);
  52. // setActualPresentTime sets the timestamp at which the current frame became
  53. // visible to the user.
  54. void setActualPresentTime(nsecs_t displayTime);
  55. // setActualPresentFence sets the fence that is used to get the time
  56. // at which the current frame became visible to the user.
  57. void setActualPresentFence(const sp<Fence>& fence);
  58. // setDisplayRefreshPeriod sets the display refresh period in nanoseconds.
  59. // This is used to compute frame presentation duration statistics relative
  60. // to this period.
  61. void setDisplayRefreshPeriod(nsecs_t displayPeriod);
  62. // advanceFrame advances the frame tracker to the next frame.
  63. void advanceFrame();
  64. // clearStats clears the tracked frame stats.
  65. void clearStats();
  66. // getStats gets the tracked frame stats.
  67. void getStats(FrameStats* outStats) const;
  68. // logAndResetStats dumps the current statistics to the binary event log
  69. // and then resets the accumulated statistics to their initial values.
  70. void logAndResetStats(const String8& name);
  71. // dumpStats dump appends the current frame display time history to the result string.
  72. void dumpStats(String8& result) const;
  73. private:
  74. struct FrameRecord {
  75. FrameRecord() :
  76. desiredPresentTime(0),
  77. frameReadyTime(0),
  78. actualPresentTime(0) {}
  79. nsecs_t desiredPresentTime;
  80. nsecs_t frameReadyTime;
  81. nsecs_t actualPresentTime;
  82. sp<Fence> frameReadyFence;
  83. sp<Fence> actualPresentFence;
  84. };
  85. // processFences iterates over all the frame records that have a fence set
  86. // and replaces that fence with a timestamp if the fence has signaled. If
  87. // the fence is not signaled the record's displayTime is set to INT64_MAX.
  88. //
  89. // This method is const because although it modifies the frame records it
  90. // does so in such a way that the information represented should not
  91. // change. This allows it to be called from the dump method.
  92. void processFencesLocked() const;
  93. // updateStatsLocked updates the running statistics that are gathered
  94. // about the frame times.
  95. void updateStatsLocked(size_t newFrameIdx) const;
  96. // resetFrameCounteresLocked sets all elements of the mNumFrames array to
  97. // 0.
  98. void resetFrameCountersLocked();
  99. // logStatsLocked dumps the current statistics to the binary event log.
  100. void logStatsLocked(const String8& name) const;
  101. // isFrameValidLocked returns true if the data for the given frame index is
  102. // valid and has all arrived (i.e. there are no oustanding fences).
  103. bool isFrameValidLocked(size_t idx) const;
  104. // mFrameRecords is the circular buffer storing the tracked data for each
  105. // frame.
  106. FrameRecord mFrameRecords[NUM_FRAME_RECORDS];
  107. // mOffset is the offset into mFrameRecords of the current frame.
  108. size_t mOffset;
  109. // mNumFences is the total number of fences set in the frame records. It
  110. // is incremented each time a fence is added and decremented each time a
  111. // signaled fence is removed in processFences or if advanceFrame clobbers
  112. // a fence.
  113. //
  114. // The number of fences is tracked so that the run time of processFences
  115. // doesn't grow with NUM_FRAME_RECORDS.
  116. int mNumFences;
  117. // mNumFrames keeps a count of the number of frames with a duration in a
  118. // particular range of vsync periods. Element n of the array stores the
  119. // number of frames with duration in the half-inclusive range
  120. // [2^n, 2^(n+1)). The last element of the array contains the count for
  121. // all frames with duration greater than 2^(NUM_FRAME_BUCKETS-1).
  122. int32_t mNumFrames[NUM_FRAME_BUCKETS];
  123. // mDisplayPeriod is the display refresh period of the display for which
  124. // this FrameTracker is gathering information.
  125. nsecs_t mDisplayPeriod;
  126. // mMutex is used to protect access to all member variables.
  127. mutable Mutex mMutex;
  128. };
  129. }
  130. #endif // ANDROID_FRAMETRACKER_H