-
Notifications
You must be signed in to change notification settings - Fork 3
/
db.c
149 lines (105 loc) · 2.97 KB
/
db.c
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "test.h"
static const int bar_width = 40;
static const int bar_speed = 8;
static struct modeset_out *modeset_list = NULL;
struct flip_data {
int bar_xpos;
unsigned num_frames_drawn;
struct timespec flip_time;
struct timespec draw_start_time;
uint64_t draw_total_time;
uint64_t min_flip_time, max_flip_time;
};
static void page_flip_event(void *data)
{
struct modeset_out *out = data;
struct timespec now;
struct flip_data *priv = out->data;
get_time_now(&now);
/* initialize values on first flip */
if (priv->num_frames_drawn == 0) {
priv->min_flip_time = UINT64_MAX;
priv->max_flip_time = 0;
priv->draw_start_time = now;
priv->flip_time = now;
priv->draw_total_time = 0;
}
/* measure min/max flip time */
if (priv->num_frames_drawn > 0) {
uint64_t us;
us = get_time_elapsed_us(&priv->flip_time, &now);
priv->flip_time = now;
if (us < priv->min_flip_time)
priv->min_flip_time = us;
if (us > priv->max_flip_time)
priv->max_flip_time = us;
}
const int measure_interval = 100;
if (priv->num_frames_drawn > 0 &&
priv->num_frames_drawn % measure_interval == 0) {
uint64_t us;
float flip_avg, draw_avg;
us = get_time_elapsed_us(&priv->draw_start_time, &now);
flip_avg = (float)us / measure_interval / 1000;
draw_avg = (float)priv->draw_total_time / measure_interval / 1000;
printf("Output %u: draw %f ms, flip avg/min/max %f/%f/%f\n",
out->output_id,
draw_avg,
flip_avg,
priv->min_flip_time / 1000.0,
priv->max_flip_time / 1000.0);
priv->draw_start_time = now;
priv->draw_total_time = 0;
priv->min_flip_time = UINT64_MAX;
priv->max_flip_time = 0;
}
/* draw */
{
/* back buffer */
struct framebuffer *buf = &out->bufs[(out->front_buf + 1) % out->num_buffers];
struct timespec ts1, ts2;
get_time_now(&ts1);
int old_xpos = (priv->bar_xpos + (buf->width - bar_width - bar_speed)) %
(buf->width - bar_width);
priv->bar_xpos = (priv->bar_xpos + bar_speed) % (buf->width - bar_width);
drm_draw_color_bar(buf, old_xpos, priv->bar_xpos, bar_width);
get_time_now(&ts2);
priv->draw_total_time += get_time_elapsed_us(&ts1, &ts2);
}
priv->num_frames_drawn += 1;
modeset_start_flip(out);
}
int main(int argc, char **argv)
{
int fd;
int opt;
const char *card = "/dev/dri/card0";
while ((opt = getopt(argc, argv, "c:")) != -1) {
switch (opt) {
case 'c':
card = optarg;
break;
}
}
// open the DRM device
fd = drm_open_dev_dumb(card);
// Prepare all connectors and CRTCs
modeset_prepare(fd, &modeset_list);
// Allocate buffers
modeset_alloc_fbs(modeset_list, 2);
// Allocate private data
for_each_output(out, modeset_list)
out->data = calloc(1, sizeof(struct flip_data));
// Set modes
modeset_set_modes(modeset_list);
// Draw color bar
modeset_main_loop(modeset_list, &page_flip_event);
// Free private data
for_each_output(out, modeset_list)
free(out->data);
// Free modeset data
modeset_cleanup(modeset_list);
close(fd);
fprintf(stderr, "exiting\n");
return 0;
}