blob: 945c84b7e9bc2ac2cf6c3d14622510cd426073de [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;
Derek Foreman4703d682014-11-19 15:06:18 -080052 uint32_t transform;
Derek Foremana04e9132014-11-19 15:06:17 -080053};
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020054
Jonas Ådahle5a12252013-04-05 23:07:11 +020055static void
56headless_output_start_repaint_loop(struct weston_output *output)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020057{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -040058 struct timespec ts;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020059
Pekka Paalanenb5eedad2014-09-23 22:08:45 -040060 clock_gettime(output->compositor->presentation_clock, &ts);
61 weston_output_finish_frame(output, &ts);
Jonas Ådahle5a12252013-04-05 23:07:11 +020062}
63
64static int
65finish_frame_handler(void *data)
66{
67 headless_output_start_repaint_loop(data);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020068
69 return 1;
70}
71
David Herrmann1edf44c2013-10-22 17:11:26 +020072static int
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020073headless_output_repaint(struct weston_output *output_base,
74 pixman_region32_t *damage)
75{
76 struct headless_output *output = (struct headless_output *) output_base;
77 struct weston_compositor *ec = output->base.compositor;
78
79 ec->renderer->repaint_output(&output->base, damage);
80
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +020081 pixman_region32_subtract(&ec->primary_plane.damage,
82 &ec->primary_plane.damage, damage);
83
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020084 wl_event_source_timer_update(output->finish_frame_timer, 16);
85
David Herrmann1edf44c2013-10-22 17:11:26 +020086 return 0;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020087}
88
89static void
90headless_output_destroy(struct weston_output *output_base)
91{
92 struct headless_output *output = (struct headless_output *) output_base;
Derek Foremana04e9132014-11-19 15:06:17 -080093 struct headless_compositor *c =
94 (struct headless_compositor *) output->base.compositor;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020095
96 wl_event_source_remove(output->finish_frame_timer);
Derek Foremana04e9132014-11-19 15:06:17 -080097
98 if (c->use_pixman) {
99 pixman_renderer_output_destroy(&output->base);
100 pixman_image_unref(output->image);
101 free(output->image_buf);
102 }
103
104 weston_output_destroy(&output->base);
105
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200106 free(output);
107
108 return;
109}
110
111static int
112headless_compositor_create_output(struct headless_compositor *c,
Derek Foreman4703d682014-11-19 15:06:18 -0800113 struct headless_parameters *param)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200114{
115 struct headless_output *output;
116 struct wl_event_loop *loop;
117
Peter Huttererf3d62272013-08-08 11:57:05 +1000118 output = zalloc(sizeof *output);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200119 if (output == NULL)
120 return -1;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200121
122 output->mode.flags =
123 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Derek Foreman4703d682014-11-19 15:06:18 -0800124 output->mode.width = param->width;
125 output->mode.height = param->height;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200126 output->mode.refresh = 60;
127 wl_list_init(&output->base.mode_list);
128 wl_list_insert(&output->base.mode_list, &output->mode.link);
129
Hardeningff39efa2013-09-18 23:56:35 +0200130 output->base.current_mode = &output->mode;
Derek Foreman4703d682014-11-19 15:06:18 -0800131 weston_output_init(&output->base, &c->base, 0, 0, param->width,
132 param->height, param->transform, 1);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200133
134 output->base.make = "weston";
135 output->base.model = "headless";
136
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200137 loop = wl_display_get_event_loop(c->base.wl_display);
138 output->finish_frame_timer =
139 wl_event_loop_add_timer(loop, finish_frame_handler, output);
140
Jonas Ådahle5a12252013-04-05 23:07:11 +0200141 output->base.start_repaint_loop = headless_output_start_repaint_loop;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200142 output->base.repaint = headless_output_repaint;
143 output->base.destroy = headless_output_destroy;
144 output->base.assign_planes = NULL;
145 output->base.set_backlight = NULL;
146 output->base.set_dpms = NULL;
147 output->base.switch_mode = NULL;
148
Derek Foremana04e9132014-11-19 15:06:17 -0800149 if (c->use_pixman) {
Derek Foreman4703d682014-11-19 15:06:18 -0800150 output->image_buf = malloc(param->width * param->height * 4);
Derek Foremana04e9132014-11-19 15:06:17 -0800151 if (!output->image_buf)
152 return -1;
153
154 output->image = pixman_image_create_bits(PIXMAN_x8r8g8b8,
Derek Foreman4703d682014-11-19 15:06:18 -0800155 param->width,
156 param->height,
Derek Foremana04e9132014-11-19 15:06:17 -0800157 output->image_buf,
Derek Foreman4703d682014-11-19 15:06:18 -0800158 param->width * 4);
Derek Foremana04e9132014-11-19 15:06:17 -0800159
160 if (pixman_renderer_output_create(&output->base) < 0)
161 return -1;
162
163 pixman_renderer_output_set_buffer(&output->base,
164 output->image);
165 }
166
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200167 wl_list_insert(c->base.output_list.prev, &output->base.link);
168
169 return 0;
170}
171
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100172static int
173headless_input_create(struct headless_compositor *c)
174{
175 weston_seat_init(&c->fake_seat, &c->base, "default");
176
177 weston_seat_init_pointer(&c->fake_seat);
178
179 if (weston_seat_init_keyboard(&c->fake_seat, NULL) < 0)
180 return -1;
181
182 return 0;
183}
184
185static void
186headless_input_destroy(struct headless_compositor *c)
187{
188 weston_seat_release(&c->fake_seat);
189}
190
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200191static void
192headless_restore(struct weston_compositor *ec)
193{
194}
195
196static void
197headless_destroy(struct weston_compositor *ec)
198{
199 struct headless_compositor *c = (struct headless_compositor *) ec;
200
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100201 headless_input_destroy(c);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200202 weston_compositor_shutdown(ec);
203
204 free(ec);
205}
206
207static struct weston_compositor *
208headless_compositor_create(struct wl_display *display,
Derek Foremana04e9132014-11-19 15:06:17 -0800209 struct headless_parameters *param,
210 const char *display_name,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400211 int *argc, char *argv[],
212 struct weston_config *config)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200213{
214 struct headless_compositor *c;
215
Peter Huttererf3d62272013-08-08 11:57:05 +1000216 c = zalloc(sizeof *c);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200217 if (c == NULL)
218 return NULL;
219
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400220 if (weston_compositor_init(&c->base, display, argc, argv, config) < 0)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200221 goto err_free;
222
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400223 if (weston_compositor_set_presentation_clock_software(&c->base) < 0)
224 goto err_compositor;
225
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100226 if (headless_input_create(c) < 0)
227 goto err_compositor;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200228
229 c->base.destroy = headless_destroy;
230 c->base.restore = headless_restore;
231
Derek Foremana04e9132014-11-19 15:06:17 -0800232 c->use_pixman = param->use_pixman;
233 if (c->use_pixman) {
234 pixman_renderer_init(&c->base);
235 }
Derek Foreman4703d682014-11-19 15:06:18 -0800236 if (headless_compositor_create_output(c, param) < 0)
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100237 goto err_input;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200238
Derek Foremana04e9132014-11-19 15:06:17 -0800239 if (!c->use_pixman && noop_renderer_init(&c->base) < 0)
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100240 goto err_input;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200241
242 return &c->base;
243
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100244err_input:
245 headless_input_destroy(c);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200246err_compositor:
247 weston_compositor_shutdown(&c->base);
248err_free:
249 free(c);
250 return NULL;
251}
252
253WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500254backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400255 struct weston_config *config)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200256{
257 int width = 1024, height = 640;
258 char *display_name = NULL;
Derek Foremana04e9132014-11-19 15:06:17 -0800259 struct headless_parameters param = { 0, };
Derek Foreman4703d682014-11-19 15:06:18 -0800260 const char *transform = "normal";
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200261
262 const struct weston_option headless_options[] = {
263 { WESTON_OPTION_INTEGER, "width", 0, &width },
264 { WESTON_OPTION_INTEGER, "height", 0, &height },
Derek Foremana04e9132014-11-19 15:06:17 -0800265 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &param.use_pixman },
Derek Foreman4703d682014-11-19 15:06:18 -0800266 { WESTON_OPTION_STRING, "transform", 0, &transform },
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200267 };
268
269 parse_options(headless_options,
270 ARRAY_LENGTH(headless_options), argc, argv);
271
Derek Foremana04e9132014-11-19 15:06:17 -0800272 param.width = width;
273 param.height = height;
274
Derek Foreman4703d682014-11-19 15:06:18 -0800275 if (weston_parse_transform(transform, &param.transform) < 0)
276 weston_log("Invalid transform \"%s\"\n", transform);
277
Derek Foremana04e9132014-11-19 15:06:17 -0800278 return headless_compositor_create(display, &param, display_name,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400279 argc, argv, config);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200280}