-
Notifications
You must be signed in to change notification settings - Fork 12
/
drm-atomic.c
295 lines (243 loc) · 7.98 KB
/
drm-atomic.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* Copyright (c) 2017 Rob Clark <[email protected]>
* Copyright (c) 2019 NVIDIA Corporation
* Copyright (c) 2020 Antonin Stefanutti <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sub license,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <errno.h>
#include <poll.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "common.h"
#include "drm-common.h"
static struct drm drm;
static int add_connector_property(drmModeAtomicReq *req, uint32_t obj_id,
const char *name, uint64_t value)
{
struct connector *obj = drm.connector;
unsigned int i;
int prop_id = 0;
for (i = 0; i < obj->props->count_props; i++) {
if (strcmp(obj->props_info[i]->name, name) == 0) {
prop_id = obj->props_info[i]->prop_id;
break;
}
}
if (prop_id < 0) {
printf("No connector property: %s\n", name);
return -EINVAL;
}
return drmModeAtomicAddProperty(req, obj_id, prop_id, value);
}
static int add_crtc_property(drmModeAtomicReq *req, uint32_t obj_id,
const char *name, uint64_t value)
{
struct crtc *obj = drm.crtc;
unsigned int i;
int prop_id = -1;
for (i = 0; i < obj->props->count_props; i++) {
if (strcmp(obj->props_info[i]->name, name) == 0) {
prop_id = obj->props_info[i]->prop_id;
break;
}
}
if (prop_id < 0) {
printf("No CRTC property: %s\n", name);
return -EINVAL;
}
return drmModeAtomicAddProperty(req, obj_id, prop_id, value);
}
static int add_plane_property(drmModeAtomicReq *req, uint32_t obj_id,
const char *name, uint64_t value)
{
struct plane *obj = drm.plane;
unsigned int prop_idx;
int res = find_plane_prop(&drm, name, &prop_idx);
const drmModePropertyRes *prop_info;
if (res) return res;
prop_info = obj->props_info[prop_idx];
return drmModeAtomicAddProperty(req, obj_id, prop_info->prop_id, value);
}
static int drm_atomic_commit(uint32_t fb_id, uint32_t flags)
{
drmModeAtomicReq *req;
uint32_t plane_id = drm.plane->plane->plane_id;
uint32_t blob_id;
int ret;
req = drmModeAtomicAlloc();
if (flags & DRM_MODE_ATOMIC_ALLOW_MODESET) {
if (add_connector_property(req, drm.connector_id, "CRTC_ID",
drm.crtc_id) < 0)
return -1;
if (drmModeCreatePropertyBlob(drm.fd, drm.mode, sizeof(*drm.mode),
&blob_id) != 0)
return -1;
if (add_crtc_property(req, drm.crtc_id, "MODE_ID", blob_id) < 0)
return -1;
if (add_crtc_property(req, drm.crtc_id, "ACTIVE", 1) < 0)
return -1;
}
add_plane_property(req, plane_id, "FB_ID", fb_id);
add_plane_property(req, plane_id, "CRTC_ID", drm.crtc_id);
add_plane_property(req, plane_id, "SRC_X", 0);
add_plane_property(req, plane_id, "SRC_Y", 0);
add_plane_property(req, plane_id, "SRC_W", drm.mode->hdisplay << 16);
add_plane_property(req, plane_id, "SRC_H", drm.mode->vdisplay << 16);
add_plane_property(req, plane_id, "CRTC_X", 0);
add_plane_property(req, plane_id, "CRTC_Y", 0);
add_plane_property(req, plane_id, "CRTC_W", drm.mode->hdisplay);
add_plane_property(req, plane_id, "CRTC_H", drm.mode->vdisplay);
ret = drmModeAtomicCommit(drm.fd, req, flags, NULL);
drmModeAtomicFree(req);
return ret;
}
static void page_flip_handler(int fd, unsigned int frame,
unsigned int sec, unsigned int usec, void *data)
{
/* suppress 'unused parameter' warnings */
(void) fd, (void) frame, (void) sec, (void) usec, (void) data;
// printf("page flip event occurred: %12.6f\n", sec + (usec / 1000000.0));
}
static int atomic_run(const struct gbm *gbm, const struct egl *egl)
{
struct gbm_bo *bo = NULL;
struct drm_fb *fb;
uint32_t i = 0;
uint64_t start_time, report_time, cur_time;
int ret;
uint32_t flags = DRM_MODE_ATOMIC_NONBLOCK;
if (drm.async_page_flip) {
flags |= DRM_MODE_PAGE_FLIP_ASYNC;
} else {
flags |= DRM_MODE_PAGE_FLIP_EVENT;
}
drmEventContext evctx = {
.version = 4,
.page_flip_handler = page_flip_handler
};
/* Allow a modeset change for the first commit only. */
flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
start_time = report_time = get_time_ns();
while (drm.frames == 0 || i < drm.frames) {
unsigned frame = i;
struct gbm_bo *next_bo;
/* Start fps measuring on second frame, to remove the time spent
* compiling shader, etc, from the fps:
*/
if (i == 1) {
start_time = report_time = get_time_ns();
}
if (!gbm->surface) {
glBindFramebuffer(GL_FRAMEBUFFER, egl->fbs[frame % NUM_BUFFERS].fb);
}
egl->draw(start_time, i++);
/* Block until all the buffered GL operations are completed.
* This is required on NVIDIA GPUs, for which the DRM drivers
* do not wait for the rendering to complete, upon executing
* page flipping operations.
*/
glFinish();
if (gbm->surface) {
eglSwapBuffers(egl->display, egl->surface);
}
if (gbm->surface) {
next_bo = gbm_surface_lock_front_buffer(gbm->surface);
} else {
next_bo = gbm->bos[frame % NUM_BUFFERS];
}
if (!next_bo) {
printf("Failed to lock front buffer\n");
return -1;
}
fb = drm_fb_get_from_bo(next_bo);
if (!fb) {
printf("Failed to get a new framebuffer BO\n");
return -1;
}
cur_time = get_time_ns();
if (cur_time > (report_time + 2 * NSEC_PER_SEC)) {
double elapsed_time = cur_time - start_time;
double secs = elapsed_time / (double) NSEC_PER_SEC;
unsigned frames = i - 1; /* first frame ignored */
printf("Rendered %u frames in %f sec (%f fps)\n",
frames, secs, (double) frames / secs);
report_time = cur_time;
}
/* Check for user input: */
struct pollfd fdset[] = {
{
.fd = STDIN_FILENO,
.events = POLLIN,
}
};
ret = poll(fdset, ARRAY_SIZE(fdset), 0);
if (ret > 0) {
printf("user interrupted!\n");
return 0;
}
/*
* Here you could also update drm plane layers if you want
* hw composition
*/
ret = drm_atomic_commit(fb->fb_id, flags);
if (ret) {
printf("failed to commit: %s\n", strerror(errno));
return -1;
}
if (!drm.async_page_flip) {
ret = drmHandleEvent(drm.fd, &evctx);
if (ret) {
printf("failed to wait for page flip completion\n");
return -1;
}
}
/* release last buffer to render on again: */
if (bo && gbm->surface)
gbm_surface_release_buffer(gbm->surface, bo);
bo = next_bo;
/* Allow a modeset change for the first commit only. */
flags &= ~(DRM_MODE_ATOMIC_ALLOW_MODESET);
}
finish_perfcntrs();
cur_time = get_time_ns();
double elapsed_time = cur_time - start_time;
double secs = elapsed_time / (double) NSEC_PER_SEC;
unsigned frames = i - 1; /* first frame ignored */
printf("Rendered %u frames in %f sec (%f fps)\n",
frames, secs, (double) frames / secs);
dump_perfcntrs(frames, elapsed_time);
return ret;
}
const struct drm * init_drm_atomic(int fd, const struct options *options)
{
int ret;
ret = drmSetClientCap(fd, DRM_CLIENT_CAP_ATOMIC, 1);
if (ret) {
printf("No atomic mode setting support: %s\n", strerror(errno));
return NULL;
}
ret = init_drm(&drm, fd, options);
if (ret)
return NULL;
drm.run = atomic_run;
return &drm;
}