blob: d4d25fa2519ebc8769711da5e1b6338c54972cc4 [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"
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020033#include "presentation_timing-server-protocol.h"
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020034
35struct headless_compositor {
36 struct weston_compositor base;
37 struct weston_seat fake_seat;
Derek Foremana04e9132014-11-19 15:06:17 -080038 bool use_pixman;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020039};
40
41struct headless_output {
42 struct weston_output base;
43 struct weston_mode mode;
44 struct wl_event_source *finish_frame_timer;
Derek Foremana04e9132014-11-19 15:06:17 -080045 uint32_t *image_buf;
46 pixman_image_t *image;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020047};
48
Derek Foremana04e9132014-11-19 15:06:17 -080049struct headless_parameters {
50 int width;
51 int height;
52 int use_pixman;
Derek Foreman4703d682014-11-19 15:06:18 -080053 uint32_t transform;
Derek Foremana04e9132014-11-19 15:06:17 -080054};
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020055
Jonas Ådahle5a12252013-04-05 23:07:11 +020056static void
57headless_output_start_repaint_loop(struct weston_output *output)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020058{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -040059 struct timespec ts;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020060
Pekka Paalanenb5eedad2014-09-23 22:08:45 -040061 clock_gettime(output->compositor->presentation_clock, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020062 weston_output_finish_frame(output, &ts, PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +020063}
64
65static int
66finish_frame_handler(void *data)
67{
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020068 struct headless_output *output = data;
69 struct timespec ts;
70
71 clock_gettime(output->base.compositor->presentation_clock, &ts);
72 weston_output_finish_frame(&output->base, &ts, 0);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020073
74 return 1;
75}
76
David Herrmann1edf44c2013-10-22 17:11:26 +020077static int
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020078headless_output_repaint(struct weston_output *output_base,
79 pixman_region32_t *damage)
80{
81 struct headless_output *output = (struct headless_output *) output_base;
82 struct weston_compositor *ec = output->base.compositor;
83
84 ec->renderer->repaint_output(&output->base, damage);
85
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +020086 pixman_region32_subtract(&ec->primary_plane.damage,
87 &ec->primary_plane.damage, damage);
88
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020089 wl_event_source_timer_update(output->finish_frame_timer, 16);
90
David Herrmann1edf44c2013-10-22 17:11:26 +020091 return 0;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020092}
93
94static void
95headless_output_destroy(struct weston_output *output_base)
96{
97 struct headless_output *output = (struct headless_output *) output_base;
Derek Foremana04e9132014-11-19 15:06:17 -080098 struct headless_compositor *c =
99 (struct headless_compositor *) output->base.compositor;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200100
101 wl_event_source_remove(output->finish_frame_timer);
Derek Foremana04e9132014-11-19 15:06:17 -0800102
103 if (c->use_pixman) {
104 pixman_renderer_output_destroy(&output->base);
105 pixman_image_unref(output->image);
106 free(output->image_buf);
107 }
108
109 weston_output_destroy(&output->base);
110
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200111 free(output);
112
113 return;
114}
115
116static int
117headless_compositor_create_output(struct headless_compositor *c,
Derek Foreman4703d682014-11-19 15:06:18 -0800118 struct headless_parameters *param)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200119{
120 struct headless_output *output;
121 struct wl_event_loop *loop;
122
Peter Huttererf3d62272013-08-08 11:57:05 +1000123 output = zalloc(sizeof *output);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200124 if (output == NULL)
125 return -1;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200126
127 output->mode.flags =
128 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Derek Foreman4703d682014-11-19 15:06:18 -0800129 output->mode.width = param->width;
130 output->mode.height = param->height;
Pekka Paalanen1879f212014-05-23 12:48:45 +0300131 output->mode.refresh = 60000;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200132 wl_list_init(&output->base.mode_list);
133 wl_list_insert(&output->base.mode_list, &output->mode.link);
134
Hardeningff39efa2013-09-18 23:56:35 +0200135 output->base.current_mode = &output->mode;
Derek Foreman4703d682014-11-19 15:06:18 -0800136 weston_output_init(&output->base, &c->base, 0, 0, param->width,
137 param->height, param->transform, 1);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200138
139 output->base.make = "weston";
140 output->base.model = "headless";
141
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200142 loop = wl_display_get_event_loop(c->base.wl_display);
143 output->finish_frame_timer =
144 wl_event_loop_add_timer(loop, finish_frame_handler, output);
145
Jonas Ådahle5a12252013-04-05 23:07:11 +0200146 output->base.start_repaint_loop = headless_output_start_repaint_loop;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200147 output->base.repaint = headless_output_repaint;
148 output->base.destroy = headless_output_destroy;
149 output->base.assign_planes = NULL;
150 output->base.set_backlight = NULL;
151 output->base.set_dpms = NULL;
152 output->base.switch_mode = NULL;
153
Derek Foremana04e9132014-11-19 15:06:17 -0800154 if (c->use_pixman) {
Derek Foreman4703d682014-11-19 15:06:18 -0800155 output->image_buf = malloc(param->width * param->height * 4);
Derek Foremana04e9132014-11-19 15:06:17 -0800156 if (!output->image_buf)
157 return -1;
158
159 output->image = pixman_image_create_bits(PIXMAN_x8r8g8b8,
Derek Foreman4703d682014-11-19 15:06:18 -0800160 param->width,
161 param->height,
Derek Foremana04e9132014-11-19 15:06:17 -0800162 output->image_buf,
Derek Foreman4703d682014-11-19 15:06:18 -0800163 param->width * 4);
Derek Foremana04e9132014-11-19 15:06:17 -0800164
165 if (pixman_renderer_output_create(&output->base) < 0)
166 return -1;
167
168 pixman_renderer_output_set_buffer(&output->base,
169 output->image);
170 }
171
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200172 wl_list_insert(c->base.output_list.prev, &output->base.link);
173
174 return 0;
175}
176
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100177static int
178headless_input_create(struct headless_compositor *c)
179{
180 weston_seat_init(&c->fake_seat, &c->base, "default");
181
182 weston_seat_init_pointer(&c->fake_seat);
183
184 if (weston_seat_init_keyboard(&c->fake_seat, NULL) < 0)
185 return -1;
186
187 return 0;
188}
189
190static void
191headless_input_destroy(struct headless_compositor *c)
192{
193 weston_seat_release(&c->fake_seat);
194}
195
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200196static void
197headless_restore(struct weston_compositor *ec)
198{
199}
200
201static void
202headless_destroy(struct weston_compositor *ec)
203{
204 struct headless_compositor *c = (struct headless_compositor *) ec;
205
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100206 headless_input_destroy(c);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200207 weston_compositor_shutdown(ec);
208
209 free(ec);
210}
211
212static struct weston_compositor *
213headless_compositor_create(struct wl_display *display,
Derek Foremana04e9132014-11-19 15:06:17 -0800214 struct headless_parameters *param,
215 const char *display_name,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400216 int *argc, char *argv[],
217 struct weston_config *config)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200218{
219 struct headless_compositor *c;
220
Peter Huttererf3d62272013-08-08 11:57:05 +1000221 c = zalloc(sizeof *c);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200222 if (c == NULL)
223 return NULL;
224
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400225 if (weston_compositor_init(&c->base, display, argc, argv, config) < 0)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200226 goto err_free;
227
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400228 if (weston_compositor_set_presentation_clock_software(&c->base) < 0)
229 goto err_compositor;
230
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100231 if (headless_input_create(c) < 0)
232 goto err_compositor;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200233
234 c->base.destroy = headless_destroy;
235 c->base.restore = headless_restore;
236
Derek Foremana04e9132014-11-19 15:06:17 -0800237 c->use_pixman = param->use_pixman;
238 if (c->use_pixman) {
239 pixman_renderer_init(&c->base);
240 }
Derek Foreman4703d682014-11-19 15:06:18 -0800241 if (headless_compositor_create_output(c, param) < 0)
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100242 goto err_input;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200243
Derek Foremana04e9132014-11-19 15:06:17 -0800244 if (!c->use_pixman && noop_renderer_init(&c->base) < 0)
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100245 goto err_input;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200246
247 return &c->base;
248
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100249err_input:
250 headless_input_destroy(c);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200251err_compositor:
252 weston_compositor_shutdown(&c->base);
253err_free:
254 free(c);
255 return NULL;
256}
257
258WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500259backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400260 struct weston_config *config)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200261{
262 int width = 1024, height = 640;
263 char *display_name = NULL;
Derek Foremana04e9132014-11-19 15:06:17 -0800264 struct headless_parameters param = { 0, };
Derek Foreman4703d682014-11-19 15:06:18 -0800265 const char *transform = "normal";
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200266
267 const struct weston_option headless_options[] = {
268 { WESTON_OPTION_INTEGER, "width", 0, &width },
269 { WESTON_OPTION_INTEGER, "height", 0, &height },
Derek Foremana04e9132014-11-19 15:06:17 -0800270 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &param.use_pixman },
Derek Foreman4703d682014-11-19 15:06:18 -0800271 { WESTON_OPTION_STRING, "transform", 0, &transform },
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200272 };
273
274 parse_options(headless_options,
275 ARRAY_LENGTH(headless_options), argc, argv);
276
Derek Foremana04e9132014-11-19 15:06:17 -0800277 param.width = width;
278 param.height = height;
279
Derek Foreman4703d682014-11-19 15:06:18 -0800280 if (weston_parse_transform(transform, &param.transform) < 0)
281 weston_log("Invalid transform \"%s\"\n", transform);
282
Derek Foremana04e9132014-11-19 15:06:17 -0800283 return headless_compositor_create(display, &param, display_name,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400284 argc, argv, config);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200285}