Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame^] | 1 | #ifndef ANDROID_DVR_FRAME_HISTORY_H_ |
| 2 | #define ANDROID_DVR_FRAME_HISTORY_H_ |
| 3 | |
| 4 | #include <dvr/graphics.h> |
| 5 | #include <pdx/file_handle.h> |
| 6 | #include <private/dvr/ring_buffer.h> |
| 7 | |
| 8 | namespace android { |
| 9 | namespace dvr { |
| 10 | |
| 11 | // FrameHistory tracks frame times from the start of rendering commands to when |
| 12 | // the buffer is ready. |
| 13 | class FrameHistory { |
| 14 | public: |
| 15 | FrameHistory(); |
| 16 | explicit FrameHistory(int pending_frame_buffer_size); |
| 17 | |
| 18 | void Reset(int pending_frame_buffer_size); |
| 19 | |
| 20 | // Call when starting rendering commands (i.e. dvrBeginRenderFrame). |
| 21 | void OnFrameStart(uint32_t scheduled_vsync, int64_t scheduled_finish_ns); |
| 22 | |
| 23 | // Call when rendering commands are finished (i.e. dvrPresent). |
| 24 | void OnFrameSubmit(android::pdx::LocalHandle&& fence); |
| 25 | |
| 26 | // Call once per frame to see if any pending frames have finished. |
| 27 | void CheckForFinishedFrames(); |
| 28 | |
| 29 | // Uses the recently completed frame render times to predict how long the next |
| 30 | // frame will take, in vsync intervals. For example if the predicted frame |
| 31 | // time is 10ms and the vsync interval is 11ms, this will return 1. If the |
| 32 | // predicted frame time is 12ms and the vsync interval is 11ms, this will |
| 33 | // return 2. |
| 34 | int PredictNextFrameVsyncInterval(int64_t vsync_period_ns) const; |
| 35 | |
| 36 | // Returns results for recently completed frames. Each frame's result is |
| 37 | // returned only once. |
| 38 | int GetPreviousFrameResults(DvrFrameScheduleResult* results, |
| 39 | int result_count); |
| 40 | |
| 41 | // Gets the vsync count for the most recently started frame. If there are no |
| 42 | // started frames this will return UINT32_MAX. |
| 43 | uint32_t GetCurrentFrameVsync() const; |
| 44 | |
| 45 | private: |
| 46 | struct PendingFrame { |
| 47 | int64_t start_ns; |
| 48 | uint32_t scheduled_vsync; |
| 49 | int64_t scheduled_finish_ns; |
| 50 | android::pdx::LocalHandle fence; |
| 51 | |
| 52 | PendingFrame(); |
| 53 | PendingFrame(int64_t start_ns, uint32_t scheduled_vsync, |
| 54 | int64_t scheduled_finish_ns, |
| 55 | android::pdx::LocalHandle&& fence); |
| 56 | |
| 57 | PendingFrame(PendingFrame&&) = default; |
| 58 | PendingFrame& operator=(PendingFrame&&) = default; |
| 59 | PendingFrame(const PendingFrame&) = delete; |
| 60 | PendingFrame& operator=(const PendingFrame&) = delete; |
| 61 | }; |
| 62 | |
| 63 | RingBuffer<PendingFrame> pending_frames_; |
| 64 | RingBuffer<DvrFrameScheduleResult> finished_frames_; |
| 65 | RingBuffer<int64_t> frame_duration_history_; |
| 66 | }; |
| 67 | |
| 68 | } // namespace dvr |
| 69 | } // namespace android |
| 70 | |
| 71 | #endif // ANDROID_DVR_FRAME_HISTORY_H_ |