• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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 #pragma once
18 
19 #include "EGL.h"
20 #include "Thread.h"
21 
22 #include <array>
23 #include <map>
24 #include <vector>
25 
26 #include <swappy/swappy_extra.h>
27 
28 using TimePoint = std::chrono::steady_clock::time_point;
29 using namespace std::chrono_literals;
30 
31 namespace swappy {
32 
33 
34 class FrameStatistics {
35 public:
FrameStatistics(std::shared_ptr<EGL> egl,std::chrono::nanoseconds refreshPeriod)36     explicit FrameStatistics(std::shared_ptr<EGL> egl,
37                              std::chrono::nanoseconds refreshPeriod) :
38             mEgl(egl), mRefreshPeriod(refreshPeriod) {};
39     ~FrameStatistics() = default;
40 
41     void capture(EGLDisplay dpy, EGLSurface surface);
42 
43     Swappy_Stats getStats();
44 
45 private:
46     static constexpr int MAX_FRAME_LAG = 10;
47     static constexpr std::chrono::nanoseconds LOG_EVERY_N_NS = 1s;
48 
49     void updateFrames(EGLnsecsANDROID start, EGLnsecsANDROID end, uint64_t stat[]);
50     void updateIdleFrames(EGL::FrameTimestamps& frameStats) REQUIRES(mMutex);
51     void updateLateFrames(EGL::FrameTimestamps& frameStats) REQUIRES(mMutex);
52     void updateOffsetFromPreviousFrame(EGL::FrameTimestamps& frameStats) REQUIRES(mMutex);
53     void updateLatencyFrames(EGL::FrameTimestamps& frameStats,
54                              TimePoint frameStartTime) REQUIRES(mMutex);
55     void logFrames() REQUIRES(mMutex);
56 
57     std::shared_ptr<EGL> mEgl;
58     const std::chrono::nanoseconds mRefreshPeriod;
59 
60     struct EGLFrame {
61         EGLDisplay dpy;
62         EGLSurface surface;
63         EGLuint64KHR id;
64         TimePoint startFrameTime;
65     };
66     std::vector<EGLFrame> mPendingFrames;
67     EGLnsecsANDROID mPrevFrameTime = 0;
68 
69     std::mutex mMutex;
70     Swappy_Stats mStats GUARDED_BY(mMutex)= {};
71 };
72 
73 } //namespace swappy
74