Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008-2011 Kristian Høgsberg |
| 3 | * Copyright © 2011 Intel Corporation |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 4 | * Copyright © 2012-2013 Raspberry Pi Foundation |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 5 | * |
| 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. |
| 15 | * |
| 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. |
| 23 | */ |
| 24 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 25 | #include "config.h" |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 26 | |
| 27 | #include <errno.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <stdio.h> |
| 30 | #include <string.h> |
| 31 | #include <math.h> |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 32 | #include <sys/time.h> |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 33 | #include <sys/types.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <unistd.h> |
| 36 | |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 37 | #include <linux/input.h> |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 38 | #include <libudev.h> |
| 39 | |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 40 | #ifdef HAVE_BCM_HOST |
| 41 | # include <bcm_host.h> |
| 42 | #else |
| 43 | # include "rpi-bcm-stubs.h" |
| 44 | #endif |
| 45 | |
| 46 | #include "compositor.h" |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 47 | #include "rpi-renderer.h" |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 48 | #include "launcher-util.h" |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 49 | #include "udev-input.h" |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 50 | |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 51 | #if 0 |
| 52 | #define DBG(...) \ |
| 53 | weston_log(__VA_ARGS__) |
| 54 | #else |
| 55 | #define DBG(...) do {} while (0) |
| 56 | #endif |
| 57 | |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 58 | struct rpi_compositor; |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 59 | struct rpi_output; |
| 60 | |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 61 | struct rpi_flippipe { |
| 62 | int readfd; |
| 63 | int writefd; |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame^] | 64 | clockid_t clk_id; |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 65 | struct wl_event_source *source; |
| 66 | }; |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 67 | |
| 68 | struct rpi_output { |
| 69 | struct rpi_compositor *compositor; |
| 70 | struct weston_output base; |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 71 | int single_buffer; |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 72 | |
| 73 | struct weston_mode mode; |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 74 | struct rpi_flippipe flippipe; |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 75 | |
| 76 | DISPMANX_DISPLAY_HANDLE_T display; |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | struct rpi_seat { |
| 80 | struct weston_seat base; |
| 81 | struct wl_list devices_list; |
| 82 | |
| 83 | struct udev_monitor *udev_monitor; |
| 84 | struct wl_event_source *udev_monitor_source; |
| 85 | char *seat_id; |
| 86 | }; |
| 87 | |
| 88 | struct rpi_compositor { |
| 89 | struct weston_compositor base; |
| 90 | uint32_t prev_state; |
| 91 | |
| 92 | struct udev *udev; |
Jonas Ådahl | 37d38d9 | 2013-12-02 22:05:02 +0100 | [diff] [blame] | 93 | struct udev_input input; |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 94 | struct wl_listener session_listener; |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 95 | |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 96 | int single_buffer; |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | static inline struct rpi_output * |
| 100 | to_rpi_output(struct weston_output *base) |
| 101 | { |
| 102 | return container_of(base, struct rpi_output, base); |
| 103 | } |
| 104 | |
| 105 | static inline struct rpi_seat * |
| 106 | to_rpi_seat(struct weston_seat *base) |
| 107 | { |
| 108 | return container_of(base, struct rpi_seat, base); |
| 109 | } |
| 110 | |
| 111 | static inline struct rpi_compositor * |
| 112 | to_rpi_compositor(struct weston_compositor *base) |
| 113 | { |
| 114 | return container_of(base, struct rpi_compositor, base); |
| 115 | } |
| 116 | |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 117 | static void |
| 118 | rpi_flippipe_update_complete(DISPMANX_UPDATE_HANDLE_T update, void *data) |
| 119 | { |
| 120 | /* This function runs in a different thread. */ |
| 121 | struct rpi_flippipe *flippipe = data; |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame^] | 122 | struct timespec ts; |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 123 | ssize_t ret; |
| 124 | |
| 125 | /* manufacture flip completion timestamp */ |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame^] | 126 | clock_gettime(flippipe->clk_id, &ts); |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 127 | |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame^] | 128 | ret = write(flippipe->writefd, &ts, sizeof ts); |
| 129 | if (ret != sizeof ts) |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 130 | weston_log("ERROR: %s failed to write, ret %zd, errno %d\n", |
| 131 | __func__, ret, errno); |
| 132 | } |
| 133 | |
| 134 | static int |
| 135 | rpi_dispmanx_update_submit(DISPMANX_UPDATE_HANDLE_T update, |
| 136 | struct rpi_output *output) |
| 137 | { |
| 138 | /* |
| 139 | * The callback registered here will eventually be called |
| 140 | * in a different thread context. Therefore we cannot call |
| 141 | * the usual functions from rpi_flippipe_update_complete(). |
| 142 | * Instead, we have a pipe for passing the message from the |
| 143 | * thread, waking up the Weston main event loop, calling |
| 144 | * rpi_flippipe_handler(), and then ending up in |
| 145 | * rpi_output_update_complete() in the main thread context, |
| 146 | * where we can do the frame finishing work. |
| 147 | */ |
| 148 | return vc_dispmanx_update_submit(update, rpi_flippipe_update_complete, |
| 149 | &output->flippipe); |
| 150 | } |
| 151 | |
| 152 | static void |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame^] | 153 | rpi_output_update_complete(struct rpi_output *output, |
| 154 | const struct timespec *stamp); |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 155 | |
| 156 | static int |
| 157 | rpi_flippipe_handler(int fd, uint32_t mask, void *data) |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 158 | { |
| 159 | struct rpi_output *output = data; |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 160 | ssize_t ret; |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame^] | 161 | struct timespec ts; |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 162 | |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 163 | if (mask != WL_EVENT_READABLE) |
| 164 | weston_log("ERROR: unexpected mask 0x%x in %s\n", |
| 165 | mask, __func__); |
| 166 | |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame^] | 167 | ret = read(fd, &ts, sizeof ts); |
| 168 | if (ret != sizeof ts) { |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 169 | weston_log("ERROR: %s failed to read, ret %zd, errno %d\n", |
| 170 | __func__, ret, errno); |
| 171 | } |
| 172 | |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame^] | 173 | rpi_output_update_complete(output, &ts); |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 174 | |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 175 | return 1; |
| 176 | } |
| 177 | |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 178 | static int |
| 179 | rpi_flippipe_init(struct rpi_flippipe *flippipe, struct rpi_output *output) |
| 180 | { |
| 181 | struct wl_event_loop *loop; |
| 182 | int fd[2]; |
| 183 | |
| 184 | if (pipe2(fd, O_CLOEXEC) == -1) |
| 185 | return -1; |
| 186 | |
| 187 | flippipe->readfd = fd[0]; |
| 188 | flippipe->writefd = fd[1]; |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame^] | 189 | flippipe->clk_id = output->compositor->base.presentation_clock; |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 190 | |
| 191 | loop = wl_display_get_event_loop(output->compositor->base.wl_display); |
| 192 | flippipe->source = wl_event_loop_add_fd(loop, flippipe->readfd, |
| 193 | WL_EVENT_READABLE, |
| 194 | rpi_flippipe_handler, output); |
| 195 | |
| 196 | if (!flippipe->source) { |
| 197 | close(flippipe->readfd); |
| 198 | close(flippipe->writefd); |
| 199 | return -1; |
| 200 | } |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static void |
| 206 | rpi_flippipe_release(struct rpi_flippipe *flippipe) |
| 207 | { |
| 208 | wl_event_source_remove(flippipe->source); |
| 209 | close(flippipe->readfd); |
| 210 | close(flippipe->writefd); |
| 211 | } |
| 212 | |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 213 | static void |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 214 | rpi_output_start_repaint_loop(struct weston_output *output) |
| 215 | { |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame^] | 216 | struct timespec ts; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 217 | |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame^] | 218 | clock_gettime(output->compositor->presentation_clock, &ts); |
| 219 | weston_output_finish_frame(output, &ts); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 220 | } |
| 221 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 222 | static int |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 223 | rpi_output_repaint(struct weston_output *base, pixman_region32_t *damage) |
| 224 | { |
| 225 | struct rpi_output *output = to_rpi_output(base); |
| 226 | struct rpi_compositor *compositor = output->compositor; |
Ander Conselvan de Oliveira | 0a88772 | 2012-11-22 15:57:00 +0200 | [diff] [blame] | 227 | struct weston_plane *primary_plane = &compositor->base.primary_plane; |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 228 | DISPMANX_UPDATE_HANDLE_T update; |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 229 | |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 230 | DBG("frame update start\n"); |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 231 | |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 232 | /* Update priority higher than in rpi-renderer's |
| 233 | * output destroy function, see rpi_output_destroy(). |
| 234 | */ |
| 235 | update = vc_dispmanx_update_start(1); |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 236 | |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 237 | rpi_renderer_set_update_handle(&output->base, update); |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 238 | compositor->base.renderer->repaint_output(&output->base, damage); |
| 239 | |
Ander Conselvan de Oliveira | 0a88772 | 2012-11-22 15:57:00 +0200 | [diff] [blame] | 240 | pixman_region32_subtract(&primary_plane->damage, |
| 241 | &primary_plane->damage, damage); |
| 242 | |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 243 | /* schedule callback to rpi_output_update_complete() */ |
| 244 | rpi_dispmanx_update_submit(update, output); |
| 245 | DBG("frame update submitted\n"); |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 246 | return 0; |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static void |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame^] | 250 | rpi_output_update_complete(struct rpi_output *output, |
| 251 | const struct timespec *stamp) |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 252 | { |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame^] | 253 | DBG("frame update complete(%ld.%09ld)\n", |
| 254 | (long)stamp->tv_sec, (long)stamp->tv_nsec); |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 255 | rpi_renderer_finish_frame(&output->base); |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame^] | 256 | weston_output_finish_frame(&output->base, stamp); |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | static void |
| 260 | rpi_output_destroy(struct weston_output *base) |
| 261 | { |
| 262 | struct rpi_output *output = to_rpi_output(base); |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 263 | |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 264 | DBG("%s\n", __func__); |
| 265 | |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 266 | rpi_renderer_output_destroy(base); |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 267 | |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 268 | /* rpi_renderer_output_destroy() will schedule a removal of |
| 269 | * all Dispmanx Elements, and wait for the update to complete. |
| 270 | * Assuming updates are sequential, the wait should guarantee, |
| 271 | * that any pending rpi_flippipe_update_complete() callbacks |
| 272 | * have happened already. Therefore we can destroy the flippipe |
| 273 | * now. |
| 274 | */ |
| 275 | rpi_flippipe_release(&output->flippipe); |
| 276 | |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 277 | weston_output_destroy(&output->base); |
| 278 | |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 279 | vc_dispmanx_display_close(output->display); |
| 280 | |
| 281 | free(output); |
| 282 | } |
| 283 | |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 284 | static const char *transform_names[] = { |
| 285 | [WL_OUTPUT_TRANSFORM_NORMAL] = "normal", |
| 286 | [WL_OUTPUT_TRANSFORM_90] = "90", |
| 287 | [WL_OUTPUT_TRANSFORM_180] = "180", |
| 288 | [WL_OUTPUT_TRANSFORM_270] = "270", |
| 289 | [WL_OUTPUT_TRANSFORM_FLIPPED] = "flipped", |
| 290 | [WL_OUTPUT_TRANSFORM_FLIPPED_90] = "flipped-90", |
| 291 | [WL_OUTPUT_TRANSFORM_FLIPPED_180] = "flipped-180", |
| 292 | [WL_OUTPUT_TRANSFORM_FLIPPED_270] = "flipped-270", |
| 293 | }; |
| 294 | |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 295 | static int |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 296 | str2transform(const char *name) |
| 297 | { |
| 298 | unsigned i; |
| 299 | |
| 300 | for (i = 0; i < ARRAY_LENGTH(transform_names); i++) |
| 301 | if (strcmp(name, transform_names[i]) == 0) |
| 302 | return i; |
| 303 | |
| 304 | return -1; |
| 305 | } |
| 306 | |
| 307 | static const char * |
| 308 | transform2str(uint32_t output_transform) |
| 309 | { |
| 310 | if (output_transform >= ARRAY_LENGTH(transform_names)) |
| 311 | return "<illegal value>"; |
| 312 | |
| 313 | return transform_names[output_transform]; |
| 314 | } |
| 315 | |
| 316 | static int |
| 317 | rpi_output_create(struct rpi_compositor *compositor, uint32_t transform) |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 318 | { |
| 319 | struct rpi_output *output; |
| 320 | DISPMANX_MODEINFO_T modeinfo; |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 321 | int ret; |
| 322 | float mm_width, mm_height; |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 323 | |
| 324 | output = calloc(1, sizeof *output); |
| 325 | if (!output) |
| 326 | return -1; |
| 327 | |
| 328 | output->compositor = compositor; |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 329 | output->single_buffer = compositor->single_buffer; |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 330 | |
| 331 | if (rpi_flippipe_init(&output->flippipe, output) < 0) { |
| 332 | weston_log("Creating message pipe failed.\n"); |
| 333 | goto out_free; |
| 334 | } |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 335 | |
| 336 | output->display = vc_dispmanx_display_open(DISPMANX_ID_HDMI); |
| 337 | if (!output->display) { |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 338 | weston_log("Failed to open dispmanx HDMI display.\n"); |
| 339 | goto out_pipe; |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | ret = vc_dispmanx_display_get_info(output->display, &modeinfo); |
| 343 | if (ret < 0) { |
| 344 | weston_log("Failed to get display mode information.\n"); |
| 345 | goto out_dmx_close; |
| 346 | } |
| 347 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 348 | output->base.start_repaint_loop = rpi_output_start_repaint_loop; |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 349 | output->base.repaint = rpi_output_repaint; |
| 350 | output->base.destroy = rpi_output_destroy; |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 351 | output->base.assign_planes = NULL; |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 352 | output->base.set_backlight = NULL; |
| 353 | output->base.set_dpms = NULL; |
| 354 | output->base.switch_mode = NULL; |
| 355 | |
| 356 | /* XXX: use tvservice to get information from and control the |
| 357 | * HDMI and SDTV outputs. See: |
| 358 | * /opt/vc/include/interface/vmcs_host/vc_tvservice.h |
| 359 | */ |
| 360 | |
| 361 | /* only one static mode in list */ |
| 362 | output->mode.flags = |
| 363 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 364 | output->mode.width = modeinfo.width; |
| 365 | output->mode.height = modeinfo.height; |
| 366 | output->mode.refresh = 60000; |
| 367 | wl_list_init(&output->base.mode_list); |
| 368 | wl_list_insert(&output->base.mode_list, &output->mode.link); |
| 369 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 370 | output->base.current_mode = &output->mode; |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 371 | output->base.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN; |
| 372 | output->base.make = "unknown"; |
| 373 | output->base.model = "unknown"; |
| 374 | |
| 375 | /* guess 96 dpi */ |
| 376 | mm_width = modeinfo.width * (25.4f / 96.0f); |
| 377 | mm_height = modeinfo.height * (25.4f / 96.0f); |
| 378 | |
| 379 | weston_output_init(&output->base, &compositor->base, |
| 380 | 0, 0, round(mm_width), round(mm_height), |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 381 | transform, 1); |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 382 | |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 383 | if (rpi_renderer_output_create(&output->base, output->display) < 0) |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 384 | goto out_output; |
| 385 | |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 386 | wl_list_insert(compositor->base.output_list.prev, &output->base.link); |
| 387 | |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 388 | weston_log("Raspberry Pi HDMI output %dx%d px\n", |
| 389 | output->mode.width, output->mode.height); |
| 390 | weston_log_continue(STAMP_SPACE "guessing %d Hz and 96 dpi\n", |
| 391 | output->mode.refresh / 1000); |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 392 | weston_log_continue(STAMP_SPACE "orientation: %s\n", |
| 393 | transform2str(output->base.transform)); |
| 394 | |
| 395 | if (!strncmp(transform2str(output->base.transform), "flipped", 7)) |
| 396 | weston_log("warning: flipped output transforms may not work\n"); |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 397 | |
| 398 | return 0; |
| 399 | |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 400 | out_output: |
| 401 | weston_output_destroy(&output->base); |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 402 | |
| 403 | out_dmx_close: |
| 404 | vc_dispmanx_display_close(output->display); |
| 405 | |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 406 | out_pipe: |
| 407 | rpi_flippipe_release(&output->flippipe); |
| 408 | |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 409 | out_free: |
| 410 | free(output); |
| 411 | return -1; |
| 412 | } |
| 413 | |
| 414 | static void |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 415 | rpi_compositor_destroy(struct weston_compositor *base) |
| 416 | { |
| 417 | struct rpi_compositor *compositor = to_rpi_compositor(base); |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 418 | |
Jonas Ådahl | 37d38d9 | 2013-12-02 22:05:02 +0100 | [diff] [blame] | 419 | udev_input_destroy(&compositor->input); |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 420 | |
| 421 | /* destroys outputs, too */ |
| 422 | weston_compositor_shutdown(&compositor->base); |
| 423 | |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 424 | weston_launcher_destroy(compositor->base.launcher); |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 425 | |
| 426 | bcm_host_deinit(); |
| 427 | free(compositor); |
| 428 | } |
| 429 | |
| 430 | static void |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 431 | session_notify(struct wl_listener *listener, void *data) |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 432 | { |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 433 | struct rpi_compositor *compositor = data; |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 434 | struct weston_output *output; |
| 435 | |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 436 | if (compositor->base.session_active) { |
| 437 | weston_log("activating session\n"); |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 438 | compositor->base.state = compositor->prev_state; |
| 439 | weston_compositor_damage_all(&compositor->base); |
Jonas Ådahl | 0feb32e | 2014-03-12 22:08:41 +0100 | [diff] [blame] | 440 | udev_input_enable(&compositor->input); |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 441 | } else { |
| 442 | weston_log("deactivating session\n"); |
Jonas Ådahl | 37d38d9 | 2013-12-02 22:05:02 +0100 | [diff] [blame] | 443 | udev_input_disable(&compositor->input); |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 444 | |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 445 | compositor->prev_state = compositor->base.state; |
Philipp Brüschweiler | 57edf7f | 2013-03-29 13:01:56 +0100 | [diff] [blame] | 446 | weston_compositor_offscreen(&compositor->base); |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 447 | |
| 448 | /* If we have a repaint scheduled (either from a |
| 449 | * pending pageflip or the idle handler), make sure we |
| 450 | * cancel that so we don't try to pageflip when we're |
Philipp Brüschweiler | 57edf7f | 2013-03-29 13:01:56 +0100 | [diff] [blame] | 451 | * vt switched away. The OFFSCREEN state will prevent |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 452 | * further attemps at repainting. When we switch |
| 453 | * back, we schedule a repaint, which will process |
| 454 | * pending frame callbacks. */ |
| 455 | |
| 456 | wl_list_for_each(output, |
| 457 | &compositor->base.output_list, link) { |
| 458 | output->repaint_needed = 0; |
| 459 | } |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 460 | }; |
| 461 | } |
| 462 | |
| 463 | static void |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 464 | rpi_restore(struct weston_compositor *compositor) |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 465 | { |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 466 | weston_launcher_restore(compositor->launcher); |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | static void |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 470 | switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data) |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 471 | { |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 472 | struct weston_compositor *compositor = data; |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 473 | |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 474 | weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1); |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 475 | } |
| 476 | |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 477 | struct rpi_parameters { |
| 478 | int tty; |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 479 | struct rpi_renderer_parameters renderer; |
| 480 | uint32_t output_transform; |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 481 | }; |
| 482 | |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 483 | static struct weston_compositor * |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 484 | rpi_compositor_create(struct wl_display *display, int *argc, char *argv[], |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 485 | struct weston_config *config, |
| 486 | struct rpi_parameters *param) |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 487 | { |
| 488 | struct rpi_compositor *compositor; |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 489 | uint32_t key; |
| 490 | |
| 491 | weston_log("initializing Raspberry Pi backend\n"); |
| 492 | |
| 493 | compositor = calloc(1, sizeof *compositor); |
| 494 | if (compositor == NULL) |
| 495 | return NULL; |
| 496 | |
| 497 | if (weston_compositor_init(&compositor->base, display, argc, argv, |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 498 | config) < 0) |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 499 | goto out_free; |
| 500 | |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame^] | 501 | if (weston_compositor_set_presentation_clock_software( |
| 502 | &compositor->base) < 0) |
| 503 | goto out_compositor; |
| 504 | |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 505 | compositor->udev = udev_new(); |
| 506 | if (compositor->udev == NULL) { |
| 507 | weston_log("Failed to initialize udev context.\n"); |
| 508 | goto out_compositor; |
| 509 | } |
| 510 | |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 511 | compositor->session_listener.notify = session_notify; |
| 512 | wl_signal_add(&compositor->base.session_signal, |
| 513 | &compositor ->session_listener); |
Kristian Høgsberg | 6ff3ff5 | 2013-10-02 10:53:33 -0700 | [diff] [blame] | 514 | compositor->base.launcher = |
David Herrmann | cc5b2ed | 2013-10-22 00:28:09 +0200 | [diff] [blame] | 515 | weston_launcher_connect(&compositor->base, param->tty, "seat0"); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 516 | if (!compositor->base.launcher) { |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 517 | weston_log("Failed to initialize tty.\n"); |
Pekka Paalanen | 4773332 | 2013-12-04 15:34:48 +0200 | [diff] [blame] | 518 | goto out_udev; |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | compositor->base.destroy = rpi_compositor_destroy; |
| 522 | compositor->base.restore = rpi_restore; |
| 523 | |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 524 | compositor->prev_state = WESTON_COMPOSITOR_ACTIVE; |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 525 | compositor->single_buffer = param->renderer.single_buffer; |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 526 | |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 527 | weston_log("Dispmanx planes are %s buffered.\n", |
| 528 | compositor->single_buffer ? "single" : "double"); |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 529 | |
| 530 | for (key = KEY_F1; key < KEY_F9; key++) |
| 531 | weston_compositor_add_key_binding(&compositor->base, key, |
| 532 | MODIFIER_CTRL | MODIFIER_ALT, |
| 533 | switch_vt_binding, compositor); |
| 534 | |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 535 | /* |
| 536 | * bcm_host_init() creates threads. |
| 537 | * Therefore we must have all signal handlers set and signals blocked |
| 538 | * before calling it. Otherwise the signals may end in the bcm |
| 539 | * threads and cause the default behaviour there. For instance, |
| 540 | * SIGUSR1 used for VT switching caused Weston to terminate there. |
| 541 | */ |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 542 | bcm_host_init(); |
| 543 | |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 544 | if (rpi_renderer_create(&compositor->base, ¶m->renderer) < 0) |
Emilio Pozuelo Monfort | bf539e3 | 2014-02-03 16:57:27 +0100 | [diff] [blame] | 545 | goto out_launcher; |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 546 | |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 547 | if (rpi_output_create(compositor, param->output_transform) < 0) |
| 548 | goto out_renderer; |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 549 | |
Emilio Pozuelo Monfort | bf539e3 | 2014-02-03 16:57:27 +0100 | [diff] [blame] | 550 | if (udev_input_init(&compositor->input, |
| 551 | &compositor->base, |
| 552 | compositor->udev, "seat0") != 0) { |
| 553 | weston_log("Failed to initialize udev input.\n"); |
| 554 | goto out_renderer; |
| 555 | } |
| 556 | |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 557 | return &compositor->base; |
| 558 | |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 559 | out_renderer: |
Vasily Khoruzhick | 52cfd61 | 2013-01-08 19:09:01 +0300 | [diff] [blame] | 560 | compositor->base.renderer->destroy(&compositor->base); |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 561 | |
Pekka Paalanen | 4773332 | 2013-12-04 15:34:48 +0200 | [diff] [blame] | 562 | out_launcher: |
| 563 | weston_launcher_destroy(compositor->base.launcher); |
| 564 | |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 565 | out_udev: |
| 566 | udev_unref(compositor->udev); |
| 567 | |
| 568 | out_compositor: |
| 569 | weston_compositor_shutdown(&compositor->base); |
| 570 | |
| 571 | out_free: |
| 572 | bcm_host_deinit(); |
| 573 | free(compositor); |
| 574 | |
| 575 | return NULL; |
| 576 | } |
| 577 | |
| 578 | WL_EXPORT struct weston_compositor * |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 579 | backend_init(struct wl_display *display, int *argc, char *argv[], |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 580 | struct weston_config *config) |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 581 | { |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 582 | const char *transform = "normal"; |
| 583 | int ret; |
| 584 | |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 585 | struct rpi_parameters param = { |
| 586 | .tty = 0, /* default to current tty */ |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 587 | .renderer.single_buffer = 0, |
| 588 | .output_transform = WL_OUTPUT_TRANSFORM_NORMAL, |
Tomeu Vizoso | e4f7b92 | 2013-12-02 17:18:58 +0100 | [diff] [blame] | 589 | .renderer.opaque_regions = 0, |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 590 | }; |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 591 | |
| 592 | const struct weston_option rpi_options[] = { |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 593 | { WESTON_OPTION_INTEGER, "tty", 0, ¶m.tty }, |
Pekka Paalanen | 7fb46fb | 2012-11-07 12:25:15 +0200 | [diff] [blame] | 594 | { WESTON_OPTION_BOOLEAN, "single-buffer", 0, |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 595 | ¶m.renderer.single_buffer }, |
| 596 | { WESTON_OPTION_STRING, "transform", 0, &transform }, |
Tomeu Vizoso | e4f7b92 | 2013-12-02 17:18:58 +0100 | [diff] [blame] | 597 | { WESTON_OPTION_BOOLEAN, "opaque-regions", 0, |
| 598 | ¶m.renderer.opaque_regions }, |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 599 | }; |
| 600 | |
| 601 | parse_options(rpi_options, ARRAY_LENGTH(rpi_options), argc, argv); |
| 602 | |
Pekka Paalanen | e31e053 | 2013-05-22 18:03:07 +0300 | [diff] [blame] | 603 | ret = str2transform(transform); |
| 604 | if (ret < 0) |
| 605 | weston_log("invalid transform \"%s\"\n", transform); |
| 606 | else |
| 607 | param.output_transform = ret; |
| 608 | |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 609 | return rpi_compositor_create(display, argc, argv, config, ¶m); |
Pekka Paalanen | e8de35c | 2012-11-07 12:25:14 +0200 | [diff] [blame] | 610 | } |