-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain_FpsMonitor.cpp
More file actions
executable file
·129 lines (106 loc) · 4 KB
/
main_FpsMonitor.cpp
File metadata and controls
executable file
·129 lines (106 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright (C) 二的次方
*/
#define LOG_TAG "FpsMonitor"
#include <android-base/logging.h>
#include <android/gui/BnFpsListener.h>
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <gui/ISurfaceComposer.h>
#include <gui/SurfaceComposerClient.h>
#include <ui/DynamicDisplayInfo.h>
#include <utils/Log.h>
#include <chrono>
#include <sstream>
#include <iomanip>
#include <ctime>
#include "RefreshRateOverlay.h"
using namespace android;
int mTaskId = 0;
int mMode = 0;
std::string getTimeAsString() {
auto now = std::chrono::system_clock::now();
std::time_t now_time = std::chrono::system_clock::to_time_t(now);
std::tm* now_tm = std::localtime(&now_time);
std::ostringstream oss;
oss << std::put_time(now_tm, "%Y-%m-%d %H:%M:%S");
auto duration = now.time_since_epoch();
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration) % 1000;
oss << '.' << std::setfill('0') << std::setw(3) << millis.count();
return oss.str();
}
struct TaskFpsCallback : public gui::BnFpsListener {
binder::Status onFpsReported(float fps) override {
if(mMode == 0 || mMode == 1)
fprintf(stderr, "%s FPS=\033[31m%f\033[m\n",getTimeAsString().c_str(), fps);
if(mMode == 0 || mMode == 2)
mRefreshRateOverlay->changeRefreshRate(mDisplayInfo.renderFrameRate, fps);
return binder::Status::ok();
}
TaskFpsCallback() {
const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
CHECK(!ids.empty());
uint64_t displayId = ids.front().value;
status_t err = SurfaceComposerClient::getDynamicDisplayInfoFromId(displayId, &mDisplayInfo);
if (err != OK)
mDisplayInfo.getActiveDisplayMode()->resolution.set(1920, 1080);
ui::Size resolution = mDisplayInfo.getActiveDisplayMode()->resolution;
ftl::Flags<RefreshRateOverlay::Features> features;
//features |= RefreshRateOverlay::Features::ShowInMiddle;
features |= RefreshRateOverlay::Features::RenderRate;
//features |= RefreshRateOverlay::Features::Spinner;
mRefreshRateOverlay = std::make_unique<RefreshRateOverlay>(features);
mRefreshRateOverlay->setLayerStack(ui::LayerStack::fromValue(0));
mRefreshRateOverlay->setViewport(resolution);
}
std::unique_ptr<RefreshRateOverlay> mRefreshRateOverlay;
ui::DynamicDisplayInfo mDisplayInfo;
};
static void usage(const char *me)
{
fprintf(stderr, "\nusage: \t%s [options]\n"
"\t--------------------------------------- options ------------------------------------------------\n"
"\t[-h] help\n"
"\t[-t] task id of app that you want to observe \n"
"\t[-m] 1: only print log, 2: only show ui, default: print log and show ui \n"
"\t------------------------------------------------------------------------------------------------\n",
me);
exit(1);
}
void parseOptions(int argc, char **argv) {
const char *me = argv[0];
if(argc < 2) {
usage(me);
exit(0);
}
int res;
while((res = getopt(argc, argv, "t:m:h")) >= 0) {
switch(res) {
case 't':
mTaskId = atoi(optarg);
break;
case 'm':
mMode = atoi(optarg);
break;
case 'h':
default:
{
usage(me);
}
}
}
}
int main(int argc, char ** argv) {
parseOptions(argc, argv);
android::ProcessState::self()->setThreadPoolMaxThreadCount(1);
android::ProcessState::self()->startThreadPool();
sp<TaskFpsCallback> callback = new TaskFpsCallback();
if (SurfaceComposerClient::addFpsListener(mTaskId, callback) != OK) {
ALOGD("addFpsListener error!");
return -1;
}
fprintf(stderr, "-- Monitor Task#%d's FPS -- \n", mTaskId);
while(getchar() != 'q');
return 0;
}