blob: dd332422181c5bf88ab88faabbfa720d82dd2b72 [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>
29
30#include "compositor.h"
31
32struct headless_compositor {
33 struct weston_compositor base;
34 struct weston_seat fake_seat;
35};
36
37struct headless_output {
38 struct weston_output base;
39 struct weston_mode mode;
40 struct wl_event_source *finish_frame_timer;
41};
42
43
Jonas Ådahle5a12252013-04-05 23:07:11 +020044static void
45headless_output_start_repaint_loop(struct weston_output *output)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020046{
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020047 uint32_t msec;
48 struct timeval tv;
49
50 gettimeofday(&tv, NULL);
51 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
52 weston_output_finish_frame(output, msec);
Jonas Ådahle5a12252013-04-05 23:07:11 +020053}
54
55static int
56finish_frame_handler(void *data)
57{
58 headless_output_start_repaint_loop(data);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020059
60 return 1;
61}
62
63static void
64headless_output_repaint(struct weston_output *output_base,
65 pixman_region32_t *damage)
66{
67 struct headless_output *output = (struct headless_output *) output_base;
68 struct weston_compositor *ec = output->base.compositor;
69
70 ec->renderer->repaint_output(&output->base, damage);
71
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +020072 pixman_region32_subtract(&ec->primary_plane.damage,
73 &ec->primary_plane.damage, damage);
74
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020075 wl_event_source_timer_update(output->finish_frame_timer, 16);
76
77 return;
78}
79
80static void
81headless_output_destroy(struct weston_output *output_base)
82{
83 struct headless_output *output = (struct headless_output *) output_base;
84
85 wl_event_source_remove(output->finish_frame_timer);
86 free(output);
87
88 return;
89}
90
91static int
92headless_compositor_create_output(struct headless_compositor *c,
93 int width, int height)
94{
95 struct headless_output *output;
96 struct wl_event_loop *loop;
97
98 output = malloc(sizeof *output);
99 if (output == NULL)
100 return -1;
101 memset(output, 0, sizeof *output);
102
103 output->mode.flags =
104 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
105 output->mode.width = width;
106 output->mode.height = height;
107 output->mode.refresh = 60;
108 wl_list_init(&output->base.mode_list);
109 wl_list_insert(&output->base.mode_list, &output->mode.link);
110
111 output->base.current = &output->mode;
112 weston_output_init(&output->base, &c->base, 0, 0, width, height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200113 WL_OUTPUT_TRANSFORM_NORMAL, 1);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200114
115 output->base.make = "weston";
116 output->base.model = "headless";
117
118 weston_output_move(&output->base, 0, 0);
119
120 loop = wl_display_get_event_loop(c->base.wl_display);
121 output->finish_frame_timer =
122 wl_event_loop_add_timer(loop, finish_frame_handler, output);
123
124 output->base.origin = output->base.current;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200125 output->base.start_repaint_loop = headless_output_start_repaint_loop;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200126 output->base.repaint = headless_output_repaint;
127 output->base.destroy = headless_output_destroy;
128 output->base.assign_planes = NULL;
129 output->base.set_backlight = NULL;
130 output->base.set_dpms = NULL;
131 output->base.switch_mode = NULL;
132
133 wl_list_insert(c->base.output_list.prev, &output->base.link);
134
135 return 0;
136}
137
138static void
139headless_restore(struct weston_compositor *ec)
140{
141}
142
143static void
144headless_destroy(struct weston_compositor *ec)
145{
146 struct headless_compositor *c = (struct headless_compositor *) ec;
147
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +0300148 ec->renderer->destroy(ec);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200149
150 weston_seat_release(&c->fake_seat);
151 weston_compositor_shutdown(ec);
152
153 free(ec);
154}
155
156static struct weston_compositor *
157headless_compositor_create(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400158 int width, int height, const char *display_name,
159 int *argc, char *argv[],
160 struct weston_config *config)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200161{
162 struct headless_compositor *c;
163
164 c = calloc(1, sizeof *c);
165 if (c == NULL)
166 return NULL;
167
168 memset(c, 0, sizeof *c);
169
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400170 if (weston_compositor_init(&c->base, display, argc, argv, config) < 0)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200171 goto err_free;
172
Rob Bradford9af5f9e2013-05-31 18:09:50 +0100173 weston_seat_init(&c->fake_seat, &c->base, "default");
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200174
175 c->base.destroy = headless_destroy;
176 c->base.restore = headless_restore;
177
178 if (headless_compositor_create_output(c, width, height) < 0)
179 goto err_compositor;
180
181 if (noop_renderer_init(&c->base) < 0)
182 goto err_compositor;
183
184 return &c->base;
185
186err_compositor:
187 weston_compositor_shutdown(&c->base);
188err_free:
189 free(c);
190 return NULL;
191}
192
193WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500194backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400195 struct weston_config *config)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200196{
197 int width = 1024, height = 640;
198 char *display_name = NULL;
199
200 const struct weston_option headless_options[] = {
201 { WESTON_OPTION_INTEGER, "width", 0, &width },
202 { WESTON_OPTION_INTEGER, "height", 0, &height },
203 };
204
205 parse_options(headless_options,
206 ARRAY_LENGTH(headless_options), argc, argv);
207
208 return headless_compositor_create(display, width, height, display_name,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400209 argc, argv, config);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200210}