blob: 598eb33f370b20c48476f7da39310c0330b48c7a [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
34#include "compositor.h"
Derek Foremana04e9132014-11-19 15:06:17 -080035#include "pixman-renderer.h"
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020036#include "presentation_timing-server-protocol.h"
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020037
38struct headless_compositor {
39 struct weston_compositor base;
40 struct weston_seat fake_seat;
Derek Foremana04e9132014-11-19 15:06:17 -080041 bool use_pixman;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020042};
43
44struct headless_output {
45 struct weston_output base;
46 struct weston_mode mode;
47 struct wl_event_source *finish_frame_timer;
Derek Foremana04e9132014-11-19 15:06:17 -080048 uint32_t *image_buf;
49 pixman_image_t *image;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020050};
51
Derek Foremana04e9132014-11-19 15:06:17 -080052struct headless_parameters {
53 int width;
54 int height;
55 int use_pixman;
Derek Foreman4703d682014-11-19 15:06:18 -080056 uint32_t transform;
Derek Foremana04e9132014-11-19 15:06:17 -080057};
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020058
Jonas Ådahle5a12252013-04-05 23:07:11 +020059static void
60headless_output_start_repaint_loop(struct weston_output *output)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020061{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -040062 struct timespec ts;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020063
Pekka Paalanen662f3842015-03-18 12:17:26 +020064 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020065 weston_output_finish_frame(output, &ts, PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +020066}
67
68static int
69finish_frame_handler(void *data)
70{
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020071 struct headless_output *output = data;
72 struct timespec ts;
73
Pekka Paalanen662f3842015-03-18 12:17:26 +020074 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020075 weston_output_finish_frame(&output->base, &ts, 0);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020076
77 return 1;
78}
79
David Herrmann1edf44c2013-10-22 17:11:26 +020080static int
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020081headless_output_repaint(struct weston_output *output_base,
82 pixman_region32_t *damage)
83{
84 struct headless_output *output = (struct headless_output *) output_base;
85 struct weston_compositor *ec = output->base.compositor;
86
87 ec->renderer->repaint_output(&output->base, damage);
88
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +020089 pixman_region32_subtract(&ec->primary_plane.damage,
90 &ec->primary_plane.damage, damage);
91
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020092 wl_event_source_timer_update(output->finish_frame_timer, 16);
93
David Herrmann1edf44c2013-10-22 17:11:26 +020094 return 0;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020095}
96
97static void
98headless_output_destroy(struct weston_output *output_base)
99{
100 struct headless_output *output = (struct headless_output *) output_base;
Derek Foremana04e9132014-11-19 15:06:17 -0800101 struct headless_compositor *c =
102 (struct headless_compositor *) output->base.compositor;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200103
104 wl_event_source_remove(output->finish_frame_timer);
Derek Foremana04e9132014-11-19 15:06:17 -0800105
106 if (c->use_pixman) {
107 pixman_renderer_output_destroy(&output->base);
108 pixman_image_unref(output->image);
109 free(output->image_buf);
110 }
111
112 weston_output_destroy(&output->base);
113
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200114 free(output);
115
116 return;
117}
118
119static int
120headless_compositor_create_output(struct headless_compositor *c,
Derek Foreman4703d682014-11-19 15:06:18 -0800121 struct headless_parameters *param)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200122{
123 struct headless_output *output;
124 struct wl_event_loop *loop;
125
Peter Huttererf3d62272013-08-08 11:57:05 +1000126 output = zalloc(sizeof *output);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200127 if (output == NULL)
128 return -1;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200129
130 output->mode.flags =
131 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Derek Foreman4703d682014-11-19 15:06:18 -0800132 output->mode.width = param->width;
133 output->mode.height = param->height;
Pekka Paalanen1879f212014-05-23 12:48:45 +0300134 output->mode.refresh = 60000;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200135 wl_list_init(&output->base.mode_list);
136 wl_list_insert(&output->base.mode_list, &output->mode.link);
137
Hardeningff39efa2013-09-18 23:56:35 +0200138 output->base.current_mode = &output->mode;
Derek Foreman4703d682014-11-19 15:06:18 -0800139 weston_output_init(&output->base, &c->base, 0, 0, param->width,
140 param->height, param->transform, 1);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200141
142 output->base.make = "weston";
143 output->base.model = "headless";
144
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200145 loop = wl_display_get_event_loop(c->base.wl_display);
146 output->finish_frame_timer =
147 wl_event_loop_add_timer(loop, finish_frame_handler, output);
148
Jonas Ådahle5a12252013-04-05 23:07:11 +0200149 output->base.start_repaint_loop = headless_output_start_repaint_loop;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200150 output->base.repaint = headless_output_repaint;
151 output->base.destroy = headless_output_destroy;
152 output->base.assign_planes = NULL;
153 output->base.set_backlight = NULL;
154 output->base.set_dpms = NULL;
155 output->base.switch_mode = NULL;
156
Derek Foremana04e9132014-11-19 15:06:17 -0800157 if (c->use_pixman) {
Derek Foreman4703d682014-11-19 15:06:18 -0800158 output->image_buf = malloc(param->width * param->height * 4);
Derek Foremana04e9132014-11-19 15:06:17 -0800159 if (!output->image_buf)
160 return -1;
161
162 output->image = pixman_image_create_bits(PIXMAN_x8r8g8b8,
Derek Foreman4703d682014-11-19 15:06:18 -0800163 param->width,
164 param->height,
Derek Foremana04e9132014-11-19 15:06:17 -0800165 output->image_buf,
Derek Foreman4703d682014-11-19 15:06:18 -0800166 param->width * 4);
Derek Foremana04e9132014-11-19 15:06:17 -0800167
168 if (pixman_renderer_output_create(&output->base) < 0)
169 return -1;
170
171 pixman_renderer_output_set_buffer(&output->base,
172 output->image);
173 }
174
Giulio Camuffob1147152015-05-06 21:41:57 +0300175 weston_compositor_add_output(&c->base, &output->base);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200176
177 return 0;
178}
179
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100180static int
181headless_input_create(struct headless_compositor *c)
182{
183 weston_seat_init(&c->fake_seat, &c->base, "default");
184
185 weston_seat_init_pointer(&c->fake_seat);
186
187 if (weston_seat_init_keyboard(&c->fake_seat, NULL) < 0)
188 return -1;
189
190 return 0;
191}
192
193static void
194headless_input_destroy(struct headless_compositor *c)
195{
196 weston_seat_release(&c->fake_seat);
197}
198
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200199static void
200headless_restore(struct weston_compositor *ec)
201{
202}
203
204static void
205headless_destroy(struct weston_compositor *ec)
206{
207 struct headless_compositor *c = (struct headless_compositor *) ec;
208
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100209 headless_input_destroy(c);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200210 weston_compositor_shutdown(ec);
211
212 free(ec);
213}
214
215static struct weston_compositor *
216headless_compositor_create(struct wl_display *display,
Derek Foremana04e9132014-11-19 15:06:17 -0800217 struct headless_parameters *param,
218 const char *display_name,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400219 int *argc, char *argv[],
220 struct weston_config *config)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200221{
222 struct headless_compositor *c;
223
Peter Huttererf3d62272013-08-08 11:57:05 +1000224 c = zalloc(sizeof *c);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200225 if (c == NULL)
226 return NULL;
227
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400228 if (weston_compositor_init(&c->base, display, argc, argv, config) < 0)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200229 goto err_free;
230
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400231 if (weston_compositor_set_presentation_clock_software(&c->base) < 0)
232 goto err_compositor;
233
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100234 if (headless_input_create(c) < 0)
235 goto err_compositor;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200236
237 c->base.destroy = headless_destroy;
238 c->base.restore = headless_restore;
239
Derek Foremana04e9132014-11-19 15:06:17 -0800240 c->use_pixman = param->use_pixman;
241 if (c->use_pixman) {
242 pixman_renderer_init(&c->base);
243 }
Derek Foreman4703d682014-11-19 15:06:18 -0800244 if (headless_compositor_create_output(c, param) < 0)
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100245 goto err_input;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200246
Derek Foremana04e9132014-11-19 15:06:17 -0800247 if (!c->use_pixman && noop_renderer_init(&c->base) < 0)
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100248 goto err_input;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200249
250 return &c->base;
251
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100252err_input:
253 headless_input_destroy(c);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200254err_compositor:
255 weston_compositor_shutdown(&c->base);
256err_free:
257 free(c);
258 return NULL;
259}
260
261WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500262backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400263 struct weston_config *config)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200264{
265 int width = 1024, height = 640;
266 char *display_name = NULL;
Derek Foremana04e9132014-11-19 15:06:17 -0800267 struct headless_parameters param = { 0, };
Derek Foreman4703d682014-11-19 15:06:18 -0800268 const char *transform = "normal";
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200269
270 const struct weston_option headless_options[] = {
271 { WESTON_OPTION_INTEGER, "width", 0, &width },
272 { WESTON_OPTION_INTEGER, "height", 0, &height },
Derek Foremana04e9132014-11-19 15:06:17 -0800273 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &param.use_pixman },
Derek Foreman4703d682014-11-19 15:06:18 -0800274 { WESTON_OPTION_STRING, "transform", 0, &transform },
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200275 };
276
277 parse_options(headless_options,
278 ARRAY_LENGTH(headless_options), argc, argv);
279
Derek Foremana04e9132014-11-19 15:06:17 -0800280 param.width = width;
281 param.height = height;
282
Derek Foreman4703d682014-11-19 15:06:18 -0800283 if (weston_parse_transform(transform, &param.transform) < 0)
284 weston_log("Invalid transform \"%s\"\n", transform);
285
Derek Foremana04e9132014-11-19 15:06:17 -0800286 return headless_compositor_create(display, &param, display_name,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400287 argc, argv, config);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200288}