blob: 16b5c39d48e742078d7dc67a47cd783747bcc561 [file] [log] [blame]
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +02001/*
2 * Copyright © 2010-2011 Benjamin Franzke
3 * Copyright © 2012 Intel Corporation
4 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07005 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020012 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070013 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020025 */
26
Daniel Stonec228e232013-05-22 18:03:19 +030027#include "config.h"
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020028
29#include <stdlib.h>
30#include <string.h>
31#include <sys/time.h>
Derek Foremana04e9132014-11-19 15:06:17 -080032#include <stdbool.h>
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020033
Jon Cruz35b2eaa2015-06-15 15:37:08 -070034#include "shared/helpers.h"
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020035#include "compositor.h"
Derek Foremana04e9132014-11-19 15:06:17 -080036#include "pixman-renderer.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020037#include "presentation-time-server-protocol.h"
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020038
Giulio Camuffo954f1832014-10-11 18:27:30 +030039struct headless_backend {
40 struct weston_backend base;
41 struct weston_compositor *compositor;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020042 struct weston_seat fake_seat;
Derek Foremana04e9132014-11-19 15:06:17 -080043 bool use_pixman;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020044};
45
46struct headless_output {
47 struct weston_output base;
48 struct weston_mode mode;
49 struct wl_event_source *finish_frame_timer;
Derek Foremana04e9132014-11-19 15:06:17 -080050 uint32_t *image_buf;
51 pixman_image_t *image;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020052};
53
Derek Foremana04e9132014-11-19 15:06:17 -080054struct headless_parameters {
55 int width;
56 int height;
57 int use_pixman;
Derek Foreman4703d682014-11-19 15:06:18 -080058 uint32_t transform;
Derek Foremana04e9132014-11-19 15:06:17 -080059};
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020060
Jonas Ådahle5a12252013-04-05 23:07:11 +020061static void
62headless_output_start_repaint_loop(struct weston_output *output)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020063{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -040064 struct timespec ts;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020065
Pekka Paalanen662f3842015-03-18 12:17:26 +020066 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020067 weston_output_finish_frame(output, &ts, WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +020068}
69
70static int
71finish_frame_handler(void *data)
72{
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020073 struct headless_output *output = data;
74 struct timespec ts;
75
Pekka Paalanen662f3842015-03-18 12:17:26 +020076 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020077 weston_output_finish_frame(&output->base, &ts, 0);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020078
79 return 1;
80}
81
David Herrmann1edf44c2013-10-22 17:11:26 +020082static int
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020083headless_output_repaint(struct weston_output *output_base,
84 pixman_region32_t *damage)
85{
86 struct headless_output *output = (struct headless_output *) output_base;
87 struct weston_compositor *ec = output->base.compositor;
88
89 ec->renderer->repaint_output(&output->base, damage);
90
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +020091 pixman_region32_subtract(&ec->primary_plane.damage,
92 &ec->primary_plane.damage, damage);
93
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020094 wl_event_source_timer_update(output->finish_frame_timer, 16);
95
David Herrmann1edf44c2013-10-22 17:11:26 +020096 return 0;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020097}
98
99static void
100headless_output_destroy(struct weston_output *output_base)
101{
102 struct headless_output *output = (struct headless_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300103 struct headless_backend *b =
104 (struct headless_backend *) output->base.compositor->backend;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200105
106 wl_event_source_remove(output->finish_frame_timer);
Derek Foremana04e9132014-11-19 15:06:17 -0800107
Giulio Camuffo954f1832014-10-11 18:27:30 +0300108 if (b->use_pixman) {
Derek Foremana04e9132014-11-19 15:06:17 -0800109 pixman_renderer_output_destroy(&output->base);
110 pixman_image_unref(output->image);
111 free(output->image_buf);
112 }
113
114 weston_output_destroy(&output->base);
115
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200116 free(output);
117
118 return;
119}
120
121static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300122headless_backend_create_output(struct headless_backend *b,
123 struct headless_parameters *param)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200124{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300125 struct weston_compositor *c = b->compositor;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200126 struct headless_output *output;
127 struct wl_event_loop *loop;
128
Peter Huttererf3d62272013-08-08 11:57:05 +1000129 output = zalloc(sizeof *output);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200130 if (output == NULL)
131 return -1;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200132
133 output->mode.flags =
134 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Derek Foreman4703d682014-11-19 15:06:18 -0800135 output->mode.width = param->width;
136 output->mode.height = param->height;
Pekka Paalanen1879f212014-05-23 12:48:45 +0300137 output->mode.refresh = 60000;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200138 wl_list_init(&output->base.mode_list);
139 wl_list_insert(&output->base.mode_list, &output->mode.link);
140
Hardeningff39efa2013-09-18 23:56:35 +0200141 output->base.current_mode = &output->mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300142 weston_output_init(&output->base, c, 0, 0, param->width,
Derek Foreman4703d682014-11-19 15:06:18 -0800143 param->height, param->transform, 1);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200144
145 output->base.make = "weston";
146 output->base.model = "headless";
147
Giulio Camuffo954f1832014-10-11 18:27:30 +0300148 loop = wl_display_get_event_loop(c->wl_display);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200149 output->finish_frame_timer =
150 wl_event_loop_add_timer(loop, finish_frame_handler, output);
151
Jonas Ådahle5a12252013-04-05 23:07:11 +0200152 output->base.start_repaint_loop = headless_output_start_repaint_loop;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200153 output->base.repaint = headless_output_repaint;
154 output->base.destroy = headless_output_destroy;
155 output->base.assign_planes = NULL;
156 output->base.set_backlight = NULL;
157 output->base.set_dpms = NULL;
158 output->base.switch_mode = NULL;
159
Giulio Camuffo954f1832014-10-11 18:27:30 +0300160 if (b->use_pixman) {
Derek Foreman4703d682014-11-19 15:06:18 -0800161 output->image_buf = malloc(param->width * param->height * 4);
Derek Foremana04e9132014-11-19 15:06:17 -0800162 if (!output->image_buf)
163 return -1;
164
165 output->image = pixman_image_create_bits(PIXMAN_x8r8g8b8,
Derek Foreman4703d682014-11-19 15:06:18 -0800166 param->width,
167 param->height,
Derek Foremana04e9132014-11-19 15:06:17 -0800168 output->image_buf,
Derek Foreman4703d682014-11-19 15:06:18 -0800169 param->width * 4);
Derek Foremana04e9132014-11-19 15:06:17 -0800170
171 if (pixman_renderer_output_create(&output->base) < 0)
172 return -1;
173
174 pixman_renderer_output_set_buffer(&output->base,
175 output->image);
176 }
177
Giulio Camuffo954f1832014-10-11 18:27:30 +0300178 weston_compositor_add_output(c, &output->base);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200179
180 return 0;
181}
182
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100183static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300184headless_input_create(struct headless_backend *b)
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100185{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300186 weston_seat_init(&b->fake_seat, b->compositor, "default");
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100187
Giulio Camuffo954f1832014-10-11 18:27:30 +0300188 weston_seat_init_pointer(&b->fake_seat);
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100189
Giulio Camuffo954f1832014-10-11 18:27:30 +0300190 if (weston_seat_init_keyboard(&b->fake_seat, NULL) < 0)
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100191 return -1;
192
193 return 0;
194}
195
196static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300197headless_input_destroy(struct headless_backend *b)
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100198{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300199 weston_seat_release(&b->fake_seat);
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100200}
201
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200202static void
203headless_restore(struct weston_compositor *ec)
204{
205}
206
207static void
208headless_destroy(struct weston_compositor *ec)
209{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300210 struct headless_backend *b = (struct headless_backend *) ec->backend;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200211
Giulio Camuffo954f1832014-10-11 18:27:30 +0300212 headless_input_destroy(b);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200213 weston_compositor_shutdown(ec);
214
Giulio Camuffo954f1832014-10-11 18:27:30 +0300215 free(b);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200216}
217
Giulio Camuffo954f1832014-10-11 18:27:30 +0300218static struct headless_backend *
219headless_backend_create(struct weston_compositor *compositor,
220 struct headless_parameters *param,
Giulio Camuffo459137b2014-10-11 23:56:24 +0300221 const char *display_name)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200222{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300223 struct headless_backend *b;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200224
Giulio Camuffo954f1832014-10-11 18:27:30 +0300225 b = zalloc(sizeof *b);
226 if (b == NULL)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200227 return NULL;
228
Giulio Camuffo954f1832014-10-11 18:27:30 +0300229 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300230 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
231 goto err_free;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400232
Giulio Camuffo954f1832014-10-11 18:27:30 +0300233 if (headless_input_create(b) < 0)
234 goto err_free;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200235
Giulio Camuffo954f1832014-10-11 18:27:30 +0300236 b->base.destroy = headless_destroy;
237 b->base.restore = headless_restore;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200238
Giulio Camuffo954f1832014-10-11 18:27:30 +0300239 b->use_pixman = param->use_pixman;
240 if (b->use_pixman) {
241 pixman_renderer_init(compositor);
Derek Foremana04e9132014-11-19 15:06:17 -0800242 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300243 if (headless_backend_create_output(b, param) < 0)
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100244 goto err_input;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200245
Giulio Camuffo954f1832014-10-11 18:27:30 +0300246 if (!b->use_pixman && noop_renderer_init(compositor) < 0)
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100247 goto err_input;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200248
Giulio Camuffo954f1832014-10-11 18:27:30 +0300249 compositor->backend = &b->base;
250 return b;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200251
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100252err_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300253 weston_compositor_shutdown(compositor);
254 headless_input_destroy(b);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200255err_free:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300256 free(b);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200257 return NULL;
258}
259
Giulio Camuffo954f1832014-10-11 18:27:30 +0300260WL_EXPORT int
261backend_init(struct weston_compositor *compositor,
262 int *argc, char *argv[],
Giulio Camuffo93daabb2015-10-17 19:24:14 +0300263 struct weston_config *config,
264 struct weston_backend_config *config_base)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200265{
266 int width = 1024, height = 640;
267 char *display_name = NULL;
Derek Foremana04e9132014-11-19 15:06:17 -0800268 struct headless_parameters param = { 0, };
Derek Foreman4703d682014-11-19 15:06:18 -0800269 const char *transform = "normal";
Giulio Camuffo954f1832014-10-11 18:27:30 +0300270 struct headless_backend *b;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200271
272 const struct weston_option headless_options[] = {
273 { WESTON_OPTION_INTEGER, "width", 0, &width },
274 { WESTON_OPTION_INTEGER, "height", 0, &height },
Derek Foremana04e9132014-11-19 15:06:17 -0800275 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &param.use_pixman },
Derek Foreman4703d682014-11-19 15:06:18 -0800276 { WESTON_OPTION_STRING, "transform", 0, &transform },
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200277 };
278
279 parse_options(headless_options,
280 ARRAY_LENGTH(headless_options), argc, argv);
281
Derek Foremana04e9132014-11-19 15:06:17 -0800282 param.width = width;
283 param.height = height;
284
Derek Foreman4703d682014-11-19 15:06:18 -0800285 if (weston_parse_transform(transform, &param.transform) < 0)
286 weston_log("Invalid transform \"%s\"\n", transform);
287
Giulio Camuffo459137b2014-10-11 23:56:24 +0300288 b = headless_backend_create(compositor, &param, display_name);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300289 if (b == NULL)
290 return -1;
291 return 0;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200292}