blob: c0e35ce907f413398c64527af79b8fececa1b788 [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 Paalanen363aa7b2014-12-17 16:20:40 +020037#include "presentation_timing-server-protocol.h"
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020038
39struct headless_compositor {
40 struct weston_compositor base;
41 struct weston_seat fake_seat;
Derek Foremana04e9132014-11-19 15:06:17 -080042 bool use_pixman;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020043};
44
45struct headless_output {
46 struct weston_output base;
47 struct weston_mode mode;
48 struct wl_event_source *finish_frame_timer;
Derek Foremana04e9132014-11-19 15:06:17 -080049 uint32_t *image_buf;
50 pixman_image_t *image;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020051};
52
Derek Foremana04e9132014-11-19 15:06:17 -080053struct headless_parameters {
54 int width;
55 int height;
56 int use_pixman;
Derek Foreman4703d682014-11-19 15:06:18 -080057 uint32_t transform;
Derek Foremana04e9132014-11-19 15:06:17 -080058};
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020059
Jonas Ådahle5a12252013-04-05 23:07:11 +020060static void
61headless_output_start_repaint_loop(struct weston_output *output)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020062{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -040063 struct timespec ts;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020064
Pekka Paalanen662f3842015-03-18 12:17:26 +020065 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020066 weston_output_finish_frame(output, &ts, PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +020067}
68
69static int
70finish_frame_handler(void *data)
71{
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020072 struct headless_output *output = data;
73 struct timespec ts;
74
Pekka Paalanen662f3842015-03-18 12:17:26 +020075 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020076 weston_output_finish_frame(&output->base, &ts, 0);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020077
78 return 1;
79}
80
David Herrmann1edf44c2013-10-22 17:11:26 +020081static int
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020082headless_output_repaint(struct weston_output *output_base,
83 pixman_region32_t *damage)
84{
85 struct headless_output *output = (struct headless_output *) output_base;
86 struct weston_compositor *ec = output->base.compositor;
87
88 ec->renderer->repaint_output(&output->base, damage);
89
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +020090 pixman_region32_subtract(&ec->primary_plane.damage,
91 &ec->primary_plane.damage, damage);
92
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020093 wl_event_source_timer_update(output->finish_frame_timer, 16);
94
David Herrmann1edf44c2013-10-22 17:11:26 +020095 return 0;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020096}
97
98static void
99headless_output_destroy(struct weston_output *output_base)
100{
101 struct headless_output *output = (struct headless_output *) output_base;
Derek Foremana04e9132014-11-19 15:06:17 -0800102 struct headless_compositor *c =
103 (struct headless_compositor *) output->base.compositor;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200104
105 wl_event_source_remove(output->finish_frame_timer);
Derek Foremana04e9132014-11-19 15:06:17 -0800106
107 if (c->use_pixman) {
108 pixman_renderer_output_destroy(&output->base);
109 pixman_image_unref(output->image);
110 free(output->image_buf);
111 }
112
113 weston_output_destroy(&output->base);
114
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200115 free(output);
116
117 return;
118}
119
120static int
121headless_compositor_create_output(struct headless_compositor *c,
Derek Foreman4703d682014-11-19 15:06:18 -0800122 struct headless_parameters *param)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200123{
124 struct headless_output *output;
125 struct wl_event_loop *loop;
126
Peter Huttererf3d62272013-08-08 11:57:05 +1000127 output = zalloc(sizeof *output);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200128 if (output == NULL)
129 return -1;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200130
131 output->mode.flags =
132 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Derek Foreman4703d682014-11-19 15:06:18 -0800133 output->mode.width = param->width;
134 output->mode.height = param->height;
Pekka Paalanen1879f212014-05-23 12:48:45 +0300135 output->mode.refresh = 60000;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200136 wl_list_init(&output->base.mode_list);
137 wl_list_insert(&output->base.mode_list, &output->mode.link);
138
Hardeningff39efa2013-09-18 23:56:35 +0200139 output->base.current_mode = &output->mode;
Derek Foreman4703d682014-11-19 15:06:18 -0800140 weston_output_init(&output->base, &c->base, 0, 0, param->width,
141 param->height, param->transform, 1);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200142
143 output->base.make = "weston";
144 output->base.model = "headless";
145
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200146 loop = wl_display_get_event_loop(c->base.wl_display);
147 output->finish_frame_timer =
148 wl_event_loop_add_timer(loop, finish_frame_handler, output);
149
Jonas Ådahle5a12252013-04-05 23:07:11 +0200150 output->base.start_repaint_loop = headless_output_start_repaint_loop;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200151 output->base.repaint = headless_output_repaint;
152 output->base.destroy = headless_output_destroy;
153 output->base.assign_planes = NULL;
154 output->base.set_backlight = NULL;
155 output->base.set_dpms = NULL;
156 output->base.switch_mode = NULL;
157
Derek Foremana04e9132014-11-19 15:06:17 -0800158 if (c->use_pixman) {
Derek Foreman4703d682014-11-19 15:06:18 -0800159 output->image_buf = malloc(param->width * param->height * 4);
Derek Foremana04e9132014-11-19 15:06:17 -0800160 if (!output->image_buf)
161 return -1;
162
163 output->image = pixman_image_create_bits(PIXMAN_x8r8g8b8,
Derek Foreman4703d682014-11-19 15:06:18 -0800164 param->width,
165 param->height,
Derek Foremana04e9132014-11-19 15:06:17 -0800166 output->image_buf,
Derek Foreman4703d682014-11-19 15:06:18 -0800167 param->width * 4);
Derek Foremana04e9132014-11-19 15:06:17 -0800168
169 if (pixman_renderer_output_create(&output->base) < 0)
170 return -1;
171
172 pixman_renderer_output_set_buffer(&output->base,
173 output->image);
174 }
175
Giulio Camuffob1147152015-05-06 21:41:57 +0300176 weston_compositor_add_output(&c->base, &output->base);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200177
178 return 0;
179}
180
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100181static int
182headless_input_create(struct headless_compositor *c)
183{
184 weston_seat_init(&c->fake_seat, &c->base, "default");
185
186 weston_seat_init_pointer(&c->fake_seat);
187
188 if (weston_seat_init_keyboard(&c->fake_seat, NULL) < 0)
189 return -1;
190
191 return 0;
192}
193
194static void
195headless_input_destroy(struct headless_compositor *c)
196{
197 weston_seat_release(&c->fake_seat);
198}
199
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200200static void
201headless_restore(struct weston_compositor *ec)
202{
203}
204
205static void
206headless_destroy(struct weston_compositor *ec)
207{
208 struct headless_compositor *c = (struct headless_compositor *) ec;
209
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100210 headless_input_destroy(c);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200211 weston_compositor_shutdown(ec);
212
213 free(ec);
214}
215
216static struct weston_compositor *
217headless_compositor_create(struct wl_display *display,
Derek Foremana04e9132014-11-19 15:06:17 -0800218 struct headless_parameters *param,
219 const char *display_name,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400220 int *argc, char *argv[],
221 struct weston_config *config)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200222{
223 struct headless_compositor *c;
224
Peter Huttererf3d62272013-08-08 11:57:05 +1000225 c = zalloc(sizeof *c);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200226 if (c == NULL)
227 return NULL;
228
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400229 if (weston_compositor_init(&c->base, display, argc, argv, config) < 0)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200230 goto err_free;
231
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400232 if (weston_compositor_set_presentation_clock_software(&c->base) < 0)
233 goto err_compositor;
234
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100235 if (headless_input_create(c) < 0)
236 goto err_compositor;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200237
238 c->base.destroy = headless_destroy;
239 c->base.restore = headless_restore;
240
Derek Foremana04e9132014-11-19 15:06:17 -0800241 c->use_pixman = param->use_pixman;
242 if (c->use_pixman) {
243 pixman_renderer_init(&c->base);
244 }
Derek Foreman4703d682014-11-19 15:06:18 -0800245 if (headless_compositor_create_output(c, param) < 0)
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100246 goto err_input;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200247
Derek Foremana04e9132014-11-19 15:06:17 -0800248 if (!c->use_pixman && noop_renderer_init(&c->base) < 0)
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100249 goto err_input;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200250
251 return &c->base;
252
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100253err_input:
254 headless_input_destroy(c);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200255err_compositor:
256 weston_compositor_shutdown(&c->base);
257err_free:
258 free(c);
259 return NULL;
260}
261
262WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500263backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400264 struct weston_config *config)
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";
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200270
271 const struct weston_option headless_options[] = {
272 { WESTON_OPTION_INTEGER, "width", 0, &width },
273 { WESTON_OPTION_INTEGER, "height", 0, &height },
Derek Foremana04e9132014-11-19 15:06:17 -0800274 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &param.use_pixman },
Derek Foreman4703d682014-11-19 15:06:18 -0800275 { WESTON_OPTION_STRING, "transform", 0, &transform },
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200276 };
277
278 parse_options(headless_options,
279 ARRAY_LENGTH(headless_options), argc, argv);
280
Derek Foremana04e9132014-11-19 15:06:17 -0800281 param.width = width;
282 param.height = height;
283
Derek Foreman4703d682014-11-19 15:06:18 -0800284 if (weston_parse_transform(transform, &param.transform) < 0)
285 weston_log("Invalid transform \"%s\"\n", transform);
286
Derek Foremana04e9132014-11-19 15:06:17 -0800287 return headless_compositor_create(display, &param, display_name,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400288 argc, argv, config);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200289}