blob: ede94946186c2fd0364ed0cdbbbacc8ffa89d0f1 [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 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020025
26#include <stdlib.h>
27#include <string.h>
28#include <sys/time.h>
Derek Foremana04e9132014-11-19 15:06:17 -080029#include <stdbool.h>
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020030
31#include "compositor.h"
Derek Foremana04e9132014-11-19 15:06:17 -080032#include "pixman-renderer.h"
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020033
34struct headless_compositor {
35 struct weston_compositor base;
36 struct weston_seat fake_seat;
Derek Foremana04e9132014-11-19 15:06:17 -080037 bool use_pixman;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020038};
39
40struct headless_output {
41 struct weston_output base;
42 struct weston_mode mode;
43 struct wl_event_source *finish_frame_timer;
Derek Foremana04e9132014-11-19 15:06:17 -080044 uint32_t *image_buf;
45 pixman_image_t *image;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020046};
47
Derek Foremana04e9132014-11-19 15:06:17 -080048struct headless_parameters {
49 int width;
50 int height;
51 int use_pixman;
52};
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020053
Jonas Ådahle5a12252013-04-05 23:07:11 +020054static void
55headless_output_start_repaint_loop(struct weston_output *output)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020056{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -040057 struct timespec ts;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020058
Pekka Paalanenb5eedad2014-09-23 22:08:45 -040059 clock_gettime(output->compositor->presentation_clock, &ts);
60 weston_output_finish_frame(output, &ts);
Jonas Ådahle5a12252013-04-05 23:07:11 +020061}
62
63static int
64finish_frame_handler(void *data)
65{
66 headless_output_start_repaint_loop(data);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020067
68 return 1;
69}
70
David Herrmann1edf44c2013-10-22 17:11:26 +020071static int
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020072headless_output_repaint(struct weston_output *output_base,
73 pixman_region32_t *damage)
74{
75 struct headless_output *output = (struct headless_output *) output_base;
76 struct weston_compositor *ec = output->base.compositor;
77
78 ec->renderer->repaint_output(&output->base, damage);
79
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +020080 pixman_region32_subtract(&ec->primary_plane.damage,
81 &ec->primary_plane.damage, damage);
82
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020083 wl_event_source_timer_update(output->finish_frame_timer, 16);
84
David Herrmann1edf44c2013-10-22 17:11:26 +020085 return 0;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020086}
87
88static void
89headless_output_destroy(struct weston_output *output_base)
90{
91 struct headless_output *output = (struct headless_output *) output_base;
Derek Foremana04e9132014-11-19 15:06:17 -080092 struct headless_compositor *c =
93 (struct headless_compositor *) output->base.compositor;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020094
95 wl_event_source_remove(output->finish_frame_timer);
Derek Foremana04e9132014-11-19 15:06:17 -080096
97 if (c->use_pixman) {
98 pixman_renderer_output_destroy(&output->base);
99 pixman_image_unref(output->image);
100 free(output->image_buf);
101 }
102
103 weston_output_destroy(&output->base);
104
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200105 free(output);
106
107 return;
108}
109
110static int
111headless_compositor_create_output(struct headless_compositor *c,
112 int width, int height)
113{
114 struct headless_output *output;
115 struct wl_event_loop *loop;
116
Peter Huttererf3d62272013-08-08 11:57:05 +1000117 output = zalloc(sizeof *output);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200118 if (output == NULL)
119 return -1;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200120
121 output->mode.flags =
122 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
123 output->mode.width = width;
124 output->mode.height = height;
125 output->mode.refresh = 60;
126 wl_list_init(&output->base.mode_list);
127 wl_list_insert(&output->base.mode_list, &output->mode.link);
128
Hardeningff39efa2013-09-18 23:56:35 +0200129 output->base.current_mode = &output->mode;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200130 weston_output_init(&output->base, &c->base, 0, 0, width, height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200131 WL_OUTPUT_TRANSFORM_NORMAL, 1);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200132
133 output->base.make = "weston";
134 output->base.model = "headless";
135
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200136 loop = wl_display_get_event_loop(c->base.wl_display);
137 output->finish_frame_timer =
138 wl_event_loop_add_timer(loop, finish_frame_handler, output);
139
Jonas Ådahle5a12252013-04-05 23:07:11 +0200140 output->base.start_repaint_loop = headless_output_start_repaint_loop;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200141 output->base.repaint = headless_output_repaint;
142 output->base.destroy = headless_output_destroy;
143 output->base.assign_planes = NULL;
144 output->base.set_backlight = NULL;
145 output->base.set_dpms = NULL;
146 output->base.switch_mode = NULL;
147
Derek Foremana04e9132014-11-19 15:06:17 -0800148 if (c->use_pixman) {
149 output->image_buf = malloc(width * height * 4);
150 if (!output->image_buf)
151 return -1;
152
153 output->image = pixman_image_create_bits(PIXMAN_x8r8g8b8,
154 width,
155 height,
156 output->image_buf,
157 width * 4);
158
159 if (pixman_renderer_output_create(&output->base) < 0)
160 return -1;
161
162 pixman_renderer_output_set_buffer(&output->base,
163 output->image);
164 }
165
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200166 wl_list_insert(c->base.output_list.prev, &output->base.link);
167
168 return 0;
169}
170
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100171static int
172headless_input_create(struct headless_compositor *c)
173{
174 weston_seat_init(&c->fake_seat, &c->base, "default");
175
176 weston_seat_init_pointer(&c->fake_seat);
177
178 if (weston_seat_init_keyboard(&c->fake_seat, NULL) < 0)
179 return -1;
180
181 return 0;
182}
183
184static void
185headless_input_destroy(struct headless_compositor *c)
186{
187 weston_seat_release(&c->fake_seat);
188}
189
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200190static void
191headless_restore(struct weston_compositor *ec)
192{
193}
194
195static void
196headless_destroy(struct weston_compositor *ec)
197{
198 struct headless_compositor *c = (struct headless_compositor *) ec;
199
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100200 headless_input_destroy(c);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200201 weston_compositor_shutdown(ec);
202
203 free(ec);
204}
205
206static struct weston_compositor *
207headless_compositor_create(struct wl_display *display,
Derek Foremana04e9132014-11-19 15:06:17 -0800208 struct headless_parameters *param,
209 const char *display_name,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400210 int *argc, char *argv[],
211 struct weston_config *config)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200212{
213 struct headless_compositor *c;
214
Peter Huttererf3d62272013-08-08 11:57:05 +1000215 c = zalloc(sizeof *c);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200216 if (c == NULL)
217 return NULL;
218
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400219 if (weston_compositor_init(&c->base, display, argc, argv, config) < 0)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200220 goto err_free;
221
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400222 if (weston_compositor_set_presentation_clock_software(&c->base) < 0)
223 goto err_compositor;
224
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100225 if (headless_input_create(c) < 0)
226 goto err_compositor;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200227
228 c->base.destroy = headless_destroy;
229 c->base.restore = headless_restore;
230
Derek Foremana04e9132014-11-19 15:06:17 -0800231 c->use_pixman = param->use_pixman;
232 if (c->use_pixman) {
233 pixman_renderer_init(&c->base);
234 }
235 if (headless_compositor_create_output(c, param->width, param->height) < 0)
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100236 goto err_input;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200237
Derek Foremana04e9132014-11-19 15:06:17 -0800238 if (!c->use_pixman && noop_renderer_init(&c->base) < 0)
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100239 goto err_input;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200240
241 return &c->base;
242
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100243err_input:
244 headless_input_destroy(c);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200245err_compositor:
246 weston_compositor_shutdown(&c->base);
247err_free:
248 free(c);
249 return NULL;
250}
251
252WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500253backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400254 struct weston_config *config)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200255{
256 int width = 1024, height = 640;
257 char *display_name = NULL;
Derek Foremana04e9132014-11-19 15:06:17 -0800258 struct headless_parameters param = { 0, };
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200259
260 const struct weston_option headless_options[] = {
261 { WESTON_OPTION_INTEGER, "width", 0, &width },
262 { WESTON_OPTION_INTEGER, "height", 0, &height },
Derek Foremana04e9132014-11-19 15:06:17 -0800263 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &param.use_pixman },
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200264 };
265
266 parse_options(headless_options,
267 ARRAY_LENGTH(headless_options), argc, argv);
268
Derek Foremana04e9132014-11-19 15:06:17 -0800269 param.width = width;
270 param.height = height;
271
272 return headless_compositor_create(display, &param, display_name,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400273 argc, argv, config);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200274}