-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprojector.c
More file actions
500 lines (409 loc) · 11.9 KB
/
Copy pathprojector.c
File metadata and controls
500 lines (409 loc) · 11.9 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
* Copyright (c) 2019 Luc Verhaegen <libv@skynet.be>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*
*
* This file has all the code supporting the projector, currently on HDMI,
* but in future we might also get VGA.
*
*/
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <drm_fourcc.h>
#include "juggler.h"
#include "kms.h"
#include "projector.h"
#include "capture.h"
static pthread_t kms_projector_thread[1];
struct kms_projector {
bool connected;
bool mode_ok;
uint32_t connector_id;
uint32_t encoder_id;
uint32_t crtc_id;
int crtc_width;
int crtc_height;
int crtc_index;
struct kms_plane *capture_scaling;
struct kms_plane *capture_yuv;
/*
* it could be that the primary plane is not used by us, and
* should be disabled
*/
struct kms_plane *plane_disable;
pthread_mutex_t capture_buffer_mutex[1];
/*
* This is the buffer that is currently being shown. It will be
* released as soon as the next buffer is shown using atomic modeset
* commit.
*/
struct capture_buffer *capture_buffer_current;
/*
* This is the buffer that our blocking atomic modeset is about to
* show.
*/
struct capture_buffer *capture_buffer_next;
/*
* This is the upcoming buffer that was last queued by capture.
*/
struct capture_buffer *capture_buffer_new;
/*
* Count the number of frames not updated, so we can implement
* a poor mans "No signal".
*/
uint32_t capture_stall_count;
bool capture_stalled;
struct kms_buffer *capture_stalled_buffer;
/* Flag the stream stopping, protect with capture_buffer_mutex */
bool capture_stopped;
uint32_t capture_stopped_count;
};
static struct kms_projector *kms_projector;
/*
* Get all the desired planes in one go.
*/
static int
kms_projector_planes_get(struct kms_projector *projector)
{
drmModePlaneRes *resources_plane = NULL;
int ret = 0, i;
/* Get plane resources so we can start sifting through the planes */
resources_plane = drmModeGetPlaneResources(kms_fd);
if (!resources_plane) {
fprintf(stderr, "%s: Failed to get KMS plane resources\n",
__func__);
ret = 0;
goto error;
}
/* now cycle through the planes to find one for our crtc */
for (i = 0; i < (int) resources_plane->count_planes; i++) {
drmModePlane *plane;
uint32_t plane_id = resources_plane->planes[i];
bool frontend = false, yuv = false, used = false;
int j;
plane = drmModeGetPlane(kms_fd, plane_id);
if (!plane) {
fprintf(stderr, "%s: failed to get Plane %u: %s\n",
__func__, plane_id, strerror(errno));
ret = 0;
goto error;
}
if (!(plane->possible_crtcs & (1 << projector->crtc_index)))
goto plane_next;
for (j = 0; j < (int) plane->count_formats; j++) {
switch (plane->formats[j]) {
/* only supported by frontend */
case DRM_FORMAT_NV12:
frontend = true;
break;
/* supported by the frontend and yuv layers */
case DRM_FORMAT_R8_G8_B8:
yuv = true;
break;
default:
break;
}
}
if (frontend) {
projector->capture_scaling =
kms_plane_create(plane->plane_id);
if (!projector->capture_scaling) {
ret = -1;
goto plane_error;
}
used = true;
} else if (yuv) {
projector->capture_yuv =
kms_plane_create(plane->plane_id);
if (!projector->capture_yuv) {
ret = -1;
goto plane_error;
}
used = true;
}
if (plane->fb_id && !used) {
if (!projector->plane_disable) {
projector->plane_disable =
kms_plane_create(plane->plane_id);
/* if this fails, continue */
} else
fprintf(stderr, "%s: multiple planes need to "
"be disabled (%d, %d)!\n", __func__,
projector->plane_disable->plane_id,
plane->plane_id);
}
plane_next:
drmModeFreePlane(plane);
continue;
plane_error:
drmModeFreePlane(plane);
break;
}
if (projector->plane_disable)
projector->plane_disable->active = true;
error:
drmModeFreePlaneResources(resources_plane);
return ret;
}
/*
* Show input buffer on projector, scaled, with borders.
*/
static void
kms_projector_capture_set(struct kms_projector *projector,
struct capture_buffer *buffer,
drmModeAtomicReqPtr request)
{
struct kms_plane *plane = projector->capture_scaling;
int width, height;
uint32_t fb_id;
bool stopped = false;
pthread_mutex_lock(projector->capture_buffer_mutex);
stopped = projector->capture_stopped;
pthread_mutex_unlock(projector->capture_buffer_mutex);
if (stopped || projector->capture_stalled) {
width = projector->capture_stalled_buffer->width;
height = projector->capture_stalled_buffer->height;
fb_id = projector->capture_stalled_buffer->fb_id;
plane->active = false;
} else if (buffer) {
width = buffer->width;
height = buffer->height;
fb_id = buffer->kms_fb_id;
} else
return;
if (!plane->active) {
int x, y, w, h;
drmModeAtomicAddProperty(request, plane->plane_id,
plane->property_crtc_id,
projector->crtc_id);
/* Scale, with borders, and center */
if ((width == projector->crtc_width) &&
(height == projector->crtc_height)) {
x = 0;
y = 0;
w = projector->crtc_width;
h = projector->crtc_height;
} else {
/* first, try to fit horizontally. */
w = projector->crtc_width;
h = height * projector->crtc_width /
width;
/* if height does not fit, inverse the logic */
if (h > projector->crtc_height) {
h = projector->crtc_height;
w = width * projector->crtc_height /
height;
}
/* center */
x = (projector->crtc_width - w) / 2;
y = (projector->crtc_height -h) / 2;
}
drmModeAtomicAddProperty(request, plane->plane_id,
plane->property_crtc_x, x);
drmModeAtomicAddProperty(request, plane->plane_id,
plane->property_crtc_y, y);
drmModeAtomicAddProperty(request, plane->plane_id,
plane->property_crtc_w, w);
drmModeAtomicAddProperty(request, plane->plane_id,
plane->property_crtc_h, h);
/* read in full size image */
drmModeAtomicAddProperty(request, plane->plane_id,
plane->property_src_x, 0);
drmModeAtomicAddProperty(request, plane->plane_id,
plane->property_src_y, 0);
drmModeAtomicAddProperty(request, plane->plane_id,
plane->property_src_w,
width << 16);
drmModeAtomicAddProperty(request, plane->plane_id,
plane->property_src_h,
height << 16);
plane->active = true;
}
/* actual flip. */
drmModeAtomicAddProperty(request, plane->plane_id,
plane->property_fb_id, fb_id);
}
static int
kms_projector_frame_update(struct kms_projector *projector,
struct capture_buffer *buffer, int frame)
{
drmModeAtomicReqPtr request;
int ret;
request = drmModeAtomicAlloc();
kms_projector_capture_set(projector, buffer, request);
if (projector->plane_disable && projector->plane_disable->active)
kms_plane_disable(projector->plane_disable, request);
ret = drmModeAtomicCommit(kms_fd, request,
DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
drmModeAtomicFree(request);
if (ret) {
fprintf(stderr, "%s: failed to show frame %d: %s\n",
__func__, frame, strerror(errno));
ret = -errno;
}
return ret;
}
static void *
kms_projector_thread_handler(void *arg)
{
struct kms_projector *projector = (struct kms_projector *) arg;
bool stopped = false;
int ret, i;
for (i = 0; true; i++) {
struct capture_buffer *new, *old = NULL;
pthread_mutex_lock(projector->capture_buffer_mutex);
new = projector->capture_buffer_new;
projector->capture_buffer_new = NULL;
stopped = projector->capture_stopped;
pthread_mutex_unlock(projector->capture_buffer_mutex);
if (new) {
ret = kms_projector_frame_update(projector, new, i);
if (ret)
return NULL;
old = projector->capture_buffer_current;
projector->capture_buffer_current = new;
if (old)
capture_buffer_display_release(old);
if (projector->capture_stall_count) {
if (projector->capture_stall_count > 2)
printf("Projector: Capture stalled for"
" %d frames.\n",
projector->capture_stall_count);
projector->capture_stall_count = 0;
projector->capture_stalled = false;
}
if (projector->capture_stopped_count) {
printf("Projector: Capture stopped for"
" %d frames.\n",
projector->capture_stopped_count);
projector->capture_stopped_count = 0;
}
} else if (stopped) {
projector->capture_stopped_count++;
if (projector->capture_buffer_current) {
printf("Projector: No input! (stopped)\n");
ret = kms_projector_frame_update(projector,
NULL, i);
if (ret)
return NULL;
old = projector->capture_buffer_current;
projector->capture_buffer_current = NULL;
capture_buffer_display_release(old);
}
usleep(16667);
} else {
projector->capture_stall_count++;
if (projector->capture_stall_count == 5) {
printf("Projector: No input! (stalled)\n");
projector->capture_stalled = true;
ret = kms_projector_frame_update(projector,
NULL, i);
if (ret)
return NULL;
old = projector->capture_buffer_current;
projector->capture_buffer_current = NULL;
if (old)
capture_buffer_display_release(old);
}
usleep(16667);
}
}
printf("%s: done!\n", __func__);
return NULL;
}
void
kms_projector_capture_stop(void)
{
struct kms_projector *projector = kms_projector;
struct capture_buffer *new;
pthread_mutex_lock(projector->capture_buffer_mutex);
new = projector->capture_buffer_new;
projector->capture_buffer_new = NULL;
projector->capture_stopped = true;
pthread_mutex_unlock(projector->capture_buffer_mutex);
if (new)
capture_buffer_display_release(new);
}
void
kms_projector_capture_display(struct capture_buffer *buffer)
{
struct kms_projector *projector = kms_projector;
struct capture_buffer *old;
if (!projector) {
capture_buffer_display_release(buffer);
return;
}
pthread_mutex_lock(projector->capture_buffer_mutex);
old = projector->capture_buffer_new;
projector->capture_buffer_new = buffer;
projector->capture_stopped = false;
pthread_mutex_unlock(projector->capture_buffer_mutex);
if (old)
capture_buffer_display_release(old);
}
int
kms_projector_init(void)
{
struct kms_projector *projector;
int ret;
projector = calloc(1, sizeof(struct kms_projector));
if (!projector)
return -ENOMEM;
kms_projector = projector;
pthread_mutex_init(projector->capture_buffer_mutex, NULL);
ret = kms_connector_id_get(DRM_MODE_CONNECTOR_HDMIA,
&projector->connector_id);
if (ret)
return ret;
ret = kms_connection_check(projector->connector_id,
&projector->connected,
&projector->encoder_id);
if (ret)
return ret;
ret = kms_crtc_id_get(projector->encoder_id,
&projector->crtc_id, &projector->mode_ok,
&projector->crtc_width, &projector->crtc_height);
if (ret)
return ret;
ret = kms_crtc_index_get(projector->crtc_id);
if (ret < 0)
return ret;
projector->crtc_index = ret;
printf("Projector is CRTC %d, %4dx%4d\n", projector->crtc_index,
projector->crtc_width, projector->crtc_height);
ret = kms_projector_planes_get(projector);
if (ret)
return ret;
projector->capture_stalled_buffer = kms_png_read("capture_stalled.png");
if (!projector->capture_stalled_buffer)
return -1;
ret = pthread_create(kms_projector_thread, NULL,
kms_projector_thread_handler,
(void *) kms_projector);
if (ret) {
fprintf(stderr, "%s() projector thread creation failed: %s\n",
__func__, strerror(ret));
return ret;
}
return 0;
}