2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
4 * Copyright © 2012-2013 Raspberry Pi Foundation
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32 #include <sys/types.h>
41 # include <bcm_host.h>
43 # include "rpi-bcm-stubs.h"
46 #include "compositor.h"
47 #include "rpi-renderer.h"
52 weston_log(__VA_ARGS__)
54 #define DBG(...) do {} while (0)
57 struct rpi_compositor;
63 struct wl_event_source *source;
67 struct rpi_compositor *compositor;
68 struct weston_output base;
71 struct weston_mode mode;
72 struct rpi_flippipe flippipe;
74 DISPMANX_DISPLAY_HANDLE_T display;
78 struct weston_seat base;
79 struct wl_list devices_list;
81 struct udev_monitor *udev_monitor;
82 struct wl_event_source *udev_monitor_source;
86 struct rpi_compositor {
87 struct weston_compositor base;
96 static inline struct rpi_output *
97 to_rpi_output(struct weston_output *base)
99 return container_of(base, struct rpi_output, base);
102 static inline struct rpi_seat *
103 to_rpi_seat(struct weston_seat *base)
105 return container_of(base, struct rpi_seat, base);
108 static inline struct rpi_compositor *
109 to_rpi_compositor(struct weston_compositor *base)
111 return container_of(base, struct rpi_compositor, base);
115 rpi_get_current_time(void)
119 /* XXX: use CLOCK_MONOTONIC instead? */
120 gettimeofday(&tv, NULL);
121 return (uint64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
125 rpi_flippipe_update_complete(DISPMANX_UPDATE_HANDLE_T update, void *data)
127 /* This function runs in a different thread. */
128 struct rpi_flippipe *flippipe = data;
132 /* manufacture flip completion timestamp */
133 time = rpi_get_current_time();
135 ret = write(flippipe->writefd, &time, sizeof time);
136 if (ret != sizeof time)
137 weston_log("ERROR: %s failed to write, ret %zd, errno %d\n",
138 __func__, ret, errno);
142 rpi_dispmanx_update_submit(DISPMANX_UPDATE_HANDLE_T update,
143 struct rpi_output *output)
146 * The callback registered here will eventually be called
147 * in a different thread context. Therefore we cannot call
148 * the usual functions from rpi_flippipe_update_complete().
149 * Instead, we have a pipe for passing the message from the
150 * thread, waking up the Weston main event loop, calling
151 * rpi_flippipe_handler(), and then ending up in
152 * rpi_output_update_complete() in the main thread context,
153 * where we can do the frame finishing work.
155 return vc_dispmanx_update_submit(update, rpi_flippipe_update_complete,
160 rpi_output_update_complete(struct rpi_output *output, uint64_t time);
163 rpi_flippipe_handler(int fd, uint32_t mask, void *data)
165 struct rpi_output *output = data;
169 if (mask != WL_EVENT_READABLE)
170 weston_log("ERROR: unexpected mask 0x%x in %s\n",
173 ret = read(fd, &time, sizeof time);
174 if (ret != sizeof time) {
175 weston_log("ERROR: %s failed to read, ret %zd, errno %d\n",
176 __func__, ret, errno);
179 rpi_output_update_complete(output, time);
185 rpi_flippipe_init(struct rpi_flippipe *flippipe, struct rpi_output *output)
187 struct wl_event_loop *loop;
190 if (pipe2(fd, O_CLOEXEC) == -1)
193 flippipe->readfd = fd[0];
194 flippipe->writefd = fd[1];
196 loop = wl_display_get_event_loop(output->compositor->base.wl_display);
197 flippipe->source = wl_event_loop_add_fd(loop, flippipe->readfd,
199 rpi_flippipe_handler, output);
201 if (!flippipe->source) {
202 close(flippipe->readfd);
203 close(flippipe->writefd);
211 rpi_flippipe_release(struct rpi_flippipe *flippipe)
213 wl_event_source_remove(flippipe->source);
214 close(flippipe->readfd);
215 close(flippipe->writefd);
219 rpi_output_start_repaint_loop(struct weston_output *output)
223 time = rpi_get_current_time();
224 weston_output_finish_frame(output, time);
228 rpi_output_repaint(struct weston_output *base, pixman_region32_t *damage)
230 struct rpi_output *output = to_rpi_output(base);
231 struct rpi_compositor *compositor = output->compositor;
232 struct weston_plane *primary_plane = &compositor->base.primary_plane;
233 DISPMANX_UPDATE_HANDLE_T update;
235 DBG("frame update start\n");
237 /* Update priority higher than in rpi-renderer's
238 * output destroy function, see rpi_output_destroy().
240 update = vc_dispmanx_update_start(1);
242 rpi_renderer_set_update_handle(&output->base, update);
243 compositor->base.renderer->repaint_output(&output->base, damage);
245 pixman_region32_subtract(&primary_plane->damage,
246 &primary_plane->damage, damage);
248 /* schedule callback to rpi_output_update_complete() */
249 rpi_dispmanx_update_submit(update, output);
250 DBG("frame update submitted\n");
254 rpi_output_update_complete(struct rpi_output *output, uint64_t time)
256 DBG("frame update complete(%" PRIu64 ")\n", time);
257 rpi_renderer_finish_frame(&output->base);
258 weston_output_finish_frame(&output->base, time);
262 rpi_output_destroy(struct weston_output *base)
264 struct rpi_output *output = to_rpi_output(base);
266 DBG("%s\n", __func__);
268 rpi_renderer_output_destroy(base);
270 /* rpi_renderer_output_destroy() will schedule a removal of
271 * all Dispmanx Elements, and wait for the update to complete.
272 * Assuming updates are sequential, the wait should guarantee,
273 * that any pending rpi_flippipe_update_complete() callbacks
274 * have happened already. Therefore we can destroy the flippipe
277 rpi_flippipe_release(&output->flippipe);
279 wl_list_remove(&output->base.link);
280 weston_output_destroy(&output->base);
282 vc_dispmanx_display_close(output->display);
287 static const char *transform_names[] = {
288 [WL_OUTPUT_TRANSFORM_NORMAL] = "normal",
289 [WL_OUTPUT_TRANSFORM_90] = "90",
290 [WL_OUTPUT_TRANSFORM_180] = "180",
291 [WL_OUTPUT_TRANSFORM_270] = "270",
292 [WL_OUTPUT_TRANSFORM_FLIPPED] = "flipped",
293 [WL_OUTPUT_TRANSFORM_FLIPPED_90] = "flipped-90",
294 [WL_OUTPUT_TRANSFORM_FLIPPED_180] = "flipped-180",
295 [WL_OUTPUT_TRANSFORM_FLIPPED_270] = "flipped-270",
299 str2transform(const char *name)
303 for (i = 0; i < ARRAY_LENGTH(transform_names); i++)
304 if (strcmp(name, transform_names[i]) == 0)
311 transform2str(uint32_t output_transform)
313 if (output_transform >= ARRAY_LENGTH(transform_names))
314 return "<illegal value>";
316 return transform_names[output_transform];
320 rpi_output_create(struct rpi_compositor *compositor, uint32_t transform)
322 struct rpi_output *output;
323 DISPMANX_MODEINFO_T modeinfo;
325 float mm_width, mm_height;
327 output = calloc(1, sizeof *output);
331 output->compositor = compositor;
332 output->single_buffer = compositor->single_buffer;
334 if (rpi_flippipe_init(&output->flippipe, output) < 0) {
335 weston_log("Creating message pipe failed.\n");
339 output->display = vc_dispmanx_display_open(DISPMANX_ID_HDMI);
340 if (!output->display) {
341 weston_log("Failed to open dispmanx HDMI display.\n");
345 ret = vc_dispmanx_display_get_info(output->display, &modeinfo);
347 weston_log("Failed to get display mode information.\n");
351 output->base.start_repaint_loop = rpi_output_start_repaint_loop;
352 output->base.repaint = rpi_output_repaint;
353 output->base.destroy = rpi_output_destroy;
354 output->base.assign_planes = NULL;
355 output->base.set_backlight = NULL;
356 output->base.set_dpms = NULL;
357 output->base.switch_mode = NULL;
359 /* XXX: use tvservice to get information from and control the
360 * HDMI and SDTV outputs. See:
361 * /opt/vc/include/interface/vmcs_host/vc_tvservice.h
364 /* only one static mode in list */
366 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
367 output->mode.width = modeinfo.width;
368 output->mode.height = modeinfo.height;
369 output->mode.refresh = 60000;
370 wl_list_init(&output->base.mode_list);
371 wl_list_insert(&output->base.mode_list, &output->mode.link);
373 output->base.current = &output->mode;
374 output->base.origin = &output->mode;
375 output->base.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
376 output->base.make = "unknown";
377 output->base.model = "unknown";
380 mm_width = modeinfo.width * (25.4f / 96.0f);
381 mm_height = modeinfo.height * (25.4f / 96.0f);
383 weston_output_init(&output->base, &compositor->base,
384 0, 0, round(mm_width), round(mm_height),
387 if (rpi_renderer_output_create(&output->base, output->display) < 0)
390 wl_list_insert(compositor->base.output_list.prev, &output->base.link);
392 weston_log("Raspberry Pi HDMI output %dx%d px\n",
393 output->mode.width, output->mode.height);
394 weston_log_continue(STAMP_SPACE "guessing %d Hz and 96 dpi\n",
395 output->mode.refresh / 1000);
396 weston_log_continue(STAMP_SPACE "orientation: %s\n",
397 transform2str(output->base.transform));
399 if (!strncmp(transform2str(output->base.transform), "flipped", 7))
400 weston_log("warning: flipped output transforms may not work\n");
405 weston_output_destroy(&output->base);
408 vc_dispmanx_display_close(output->display);
411 rpi_flippipe_release(&output->flippipe);
419 rpi_led_update(struct weston_seat *seat_base, enum weston_led leds)
421 struct rpi_seat *seat = to_rpi_seat(seat_base);
422 struct evdev_device *device;
424 wl_list_for_each(device, &seat->devices_list, link)
425 evdev_led_update(device, leds);
428 static const char default_seat[] = "seat0";
431 device_added(struct udev_device *udev_device, struct rpi_seat *master)
433 struct evdev_device *device;
435 const char *device_seat;
438 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
440 device_seat = default_seat;
442 if (strcmp(device_seat, master->seat_id))
445 devnode = udev_device_get_devnode(udev_device);
447 /* Use non-blocking mode so that we can loop on read on
448 * evdev_device_data() until all events on the fd are
449 * read. mtdev_get() also expects this. */
450 fd = open(devnode, O_RDWR | O_NONBLOCK | O_CLOEXEC);
452 weston_log("opening input device '%s' failed.\n", devnode);
456 device = evdev_device_create(&master->base, devnode, fd);
459 weston_log("not using input device '%s'.\n", devnode);
463 wl_list_insert(master->devices_list.prev, &device->link);
467 evdev_add_devices(struct udev *udev, struct weston_seat *seat_base)
469 struct rpi_seat *seat = to_rpi_seat(seat_base);
470 struct udev_enumerate *e;
471 struct udev_list_entry *entry;
472 struct udev_device *device;
473 const char *path, *sysname;
475 e = udev_enumerate_new(udev);
476 udev_enumerate_add_match_subsystem(e, "input");
477 udev_enumerate_scan_devices(e);
478 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
479 path = udev_list_entry_get_name(entry);
480 device = udev_device_new_from_syspath(udev, path);
482 sysname = udev_device_get_sysname(device);
483 if (strncmp("event", sysname, 5) != 0) {
484 udev_device_unref(device);
488 device_added(device, seat);
490 udev_device_unref(device);
492 udev_enumerate_unref(e);
494 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
496 if (wl_list_empty(&seat->devices_list)) {
498 "warning: no input devices on entering Weston. "
500 "\t- no permissions to read /dev/input/event*\n"
501 "\t- seats misconfigured "
502 "(Weston backend option 'seat', "
503 "udev device property ID_SEAT)\n");
508 evdev_udev_handler(int fd, uint32_t mask, void *data)
510 struct rpi_seat *seat = data;
511 struct udev_device *udev_device;
512 struct evdev_device *device, *next;
516 udev_device = udev_monitor_receive_device(seat->udev_monitor);
520 action = udev_device_get_action(udev_device);
524 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
527 if (!strcmp(action, "add")) {
528 device_added(udev_device, seat);
530 else if (!strcmp(action, "remove")) {
531 devnode = udev_device_get_devnode(udev_device);
532 wl_list_for_each_safe(device, next, &seat->devices_list, link)
533 if (!strcmp(device->devnode, devnode)) {
534 weston_log("input device %s, %s removed\n",
535 device->devname, device->devnode);
536 evdev_device_destroy(device);
542 udev_device_unref(udev_device);
548 evdev_enable_udev_monitor(struct udev *udev, struct weston_seat *seat_base)
550 struct rpi_seat *master = to_rpi_seat(seat_base);
551 struct wl_event_loop *loop;
552 struct weston_compositor *c = master->base.compositor;
555 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
556 if (!master->udev_monitor) {
557 weston_log("udev: failed to create the udev monitor\n");
561 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
564 if (udev_monitor_enable_receiving(master->udev_monitor)) {
565 weston_log("udev: failed to bind the udev monitor\n");
566 udev_monitor_unref(master->udev_monitor);
570 loop = wl_display_get_event_loop(c->wl_display);
571 fd = udev_monitor_get_fd(master->udev_monitor);
572 master->udev_monitor_source =
573 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
574 evdev_udev_handler, master);
575 if (!master->udev_monitor_source) {
576 udev_monitor_unref(master->udev_monitor);
584 evdev_disable_udev_monitor(struct weston_seat *seat_base)
586 struct rpi_seat *seat = to_rpi_seat(seat_base);
588 if (!seat->udev_monitor)
591 udev_monitor_unref(seat->udev_monitor);
592 seat->udev_monitor = NULL;
593 wl_event_source_remove(seat->udev_monitor_source);
594 seat->udev_monitor_source = NULL;
598 evdev_input_create(struct weston_compositor *c, struct udev *udev,
601 struct rpi_seat *seat;
603 seat = malloc(sizeof *seat);
607 memset(seat, 0, sizeof *seat);
608 weston_seat_init(&seat->base, c);
609 seat->base.led_update = rpi_led_update;
611 wl_list_init(&seat->devices_list);
612 seat->seat_id = strdup(seat_id);
613 if (!evdev_enable_udev_monitor(udev, &seat->base)) {
619 evdev_add_devices(udev, &seat->base);
623 evdev_remove_devices(struct weston_seat *seat_base)
625 struct rpi_seat *seat = to_rpi_seat(seat_base);
626 struct evdev_device *device, *next;
628 wl_list_for_each_safe(device, next, &seat->devices_list, link)
629 evdev_device_destroy(device);
631 if (seat->base.keyboard)
632 notify_keyboard_focus_out(&seat->base);
636 evdev_input_destroy(struct weston_seat *seat_base)
638 struct rpi_seat *seat = to_rpi_seat(seat_base);
640 evdev_remove_devices(seat_base);
641 evdev_disable_udev_monitor(&seat->base);
643 weston_seat_release(seat_base);
649 rpi_compositor_destroy(struct weston_compositor *base)
651 struct rpi_compositor *compositor = to_rpi_compositor(base);
652 struct weston_seat *seat, *next;
654 wl_list_for_each_safe(seat, next, &compositor->base.seat_list, link)
655 evdev_input_destroy(seat);
657 /* destroys outputs, too */
658 weston_compositor_shutdown(&compositor->base);
660 compositor->base.renderer->destroy(&compositor->base);
661 tty_destroy(compositor->tty);
668 vt_func(struct weston_compositor *base, int event)
670 struct rpi_compositor *compositor = to_rpi_compositor(base);
671 struct weston_seat *seat;
672 struct weston_output *output;
676 weston_log("entering VT\n");
677 compositor->base.focus = 1;
678 compositor->base.state = compositor->prev_state;
679 weston_compositor_damage_all(&compositor->base);
680 wl_list_for_each(seat, &compositor->base.seat_list, link) {
681 evdev_add_devices(compositor->udev, seat);
682 evdev_enable_udev_monitor(compositor->udev, seat);
686 weston_log("leaving VT\n");
687 wl_list_for_each(seat, &compositor->base.seat_list, link) {
688 evdev_disable_udev_monitor(seat);
689 evdev_remove_devices(seat);
692 compositor->base.focus = 0;
693 compositor->prev_state = compositor->base.state;
694 weston_compositor_offscreen(&compositor->base);
696 /* If we have a repaint scheduled (either from a
697 * pending pageflip or the idle handler), make sure we
698 * cancel that so we don't try to pageflip when we're
699 * vt switched away. The OFFSCREEN state will prevent
700 * further attemps at repainting. When we switch
701 * back, we schedule a repaint, which will process
702 * pending frame callbacks. */
704 wl_list_for_each(output,
705 &compositor->base.output_list, link) {
706 output->repaint_needed = 0;
714 rpi_restore(struct weston_compositor *base)
716 struct rpi_compositor *compositor = to_rpi_compositor(base);
718 tty_reset(compositor->tty);
722 switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
724 struct rpi_compositor *ec = data;
726 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
729 struct rpi_parameters {
731 struct rpi_renderer_parameters renderer;
732 uint32_t output_transform;
735 static struct weston_compositor *
736 rpi_compositor_create(struct wl_display *display, int *argc, char *argv[],
737 struct weston_config *config,
738 struct rpi_parameters *param)
740 struct rpi_compositor *compositor;
741 const char *seat = default_seat;
744 weston_log("initializing Raspberry Pi backend\n");
746 compositor = calloc(1, sizeof *compositor);
747 if (compositor == NULL)
750 if (weston_compositor_init(&compositor->base, display, argc, argv,
754 compositor->udev = udev_new();
755 if (compositor->udev == NULL) {
756 weston_log("Failed to initialize udev context.\n");
760 compositor->tty = tty_create(&compositor->base, vt_func, param->tty);
761 if (!compositor->tty) {
762 weston_log("Failed to initialize tty.\n");
766 compositor->base.destroy = rpi_compositor_destroy;
767 compositor->base.restore = rpi_restore;
769 compositor->base.focus = 1;
770 compositor->prev_state = WESTON_COMPOSITOR_ACTIVE;
771 compositor->single_buffer = param->renderer.single_buffer;
773 weston_log("Dispmanx planes are %s buffered.\n",
774 compositor->single_buffer ? "single" : "double");
776 for (key = KEY_F1; key < KEY_F9; key++)
777 weston_compositor_add_key_binding(&compositor->base, key,
778 MODIFIER_CTRL | MODIFIER_ALT,
779 switch_vt_binding, compositor);
782 * bcm_host_init() creates threads.
783 * Therefore we must have all signal handlers set and signals blocked
784 * before calling it. Otherwise the signals may end in the bcm
785 * threads and cause the default behaviour there. For instance,
786 * SIGUSR1 used for VT switching caused Weston to terminate there.
790 if (rpi_renderer_create(&compositor->base, ¶m->renderer) < 0)
793 if (rpi_output_create(compositor, param->output_transform) < 0)
796 evdev_input_create(&compositor->base, compositor->udev, seat);
798 return &compositor->base;
801 compositor->base.renderer->destroy(&compositor->base);
804 tty_destroy(compositor->tty);
807 udev_unref(compositor->udev);
810 weston_compositor_shutdown(&compositor->base);
819 WL_EXPORT struct weston_compositor *
820 backend_init(struct wl_display *display, int *argc, char *argv[],
821 struct weston_config *config)
823 const char *transform = "normal";
826 struct rpi_parameters param = {
827 .tty = 0, /* default to current tty */
828 .renderer.single_buffer = 0,
829 .output_transform = WL_OUTPUT_TRANSFORM_NORMAL,
832 const struct weston_option rpi_options[] = {
833 { WESTON_OPTION_INTEGER, "tty", 0, ¶m.tty },
834 { WESTON_OPTION_BOOLEAN, "single-buffer", 0,
835 ¶m.renderer.single_buffer },
836 { WESTON_OPTION_STRING, "transform", 0, &transform },
839 parse_options(rpi_options, ARRAY_LENGTH(rpi_options), argc, argv);
841 ret = str2transform(transform);
843 weston_log("invalid transform \"%s\"\n", transform);
845 param.output_transform = ret;
847 return rpi_compositor_create(display, argc, argv, config, ¶m);