blob: 6c22ee303be6fda96892d4ee852923792b2912bd [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
34#include "compositor.h"
Benoit Gschwind3c530942016-04-15 20:28:32 -070035#include "compositor-headless.h"
36#include "shared/helpers.h"
Derek Foremana04e9132014-11-19 15:06:17 -080037#include "pixman-renderer.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020038#include "presentation-time-server-protocol.h"
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020039
Giulio Camuffo954f1832014-10-11 18:27:30 +030040struct headless_backend {
41 struct weston_backend base;
42 struct weston_compositor *compositor;
Benoit Gschwind3c530942016-04-15 20:28:32 -070043
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020044 struct weston_seat fake_seat;
Derek Foremana04e9132014-11-19 15:06:17 -080045 bool use_pixman;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020046};
47
48struct headless_output {
49 struct weston_output base;
Benoit Gschwind3c530942016-04-15 20:28:32 -070050
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020051 struct weston_mode mode;
52 struct wl_event_source *finish_frame_timer;
Derek Foremana04e9132014-11-19 15:06:17 -080053 uint32_t *image_buf;
54 pixman_image_t *image;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020055};
56
Jonas Ådahle5a12252013-04-05 23:07:11 +020057static void
58headless_output_start_repaint_loop(struct weston_output *output)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020059{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -040060 struct timespec ts;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020061
Pekka Paalanen662f3842015-03-18 12:17:26 +020062 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020063 weston_output_finish_frame(output, &ts, WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +020064}
65
66static int
67finish_frame_handler(void *data)
68{
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020069 struct headless_output *output = data;
70 struct timespec ts;
71
Pekka Paalanen662f3842015-03-18 12:17:26 +020072 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020073 weston_output_finish_frame(&output->base, &ts, 0);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020074
75 return 1;
76}
77
David Herrmann1edf44c2013-10-22 17:11:26 +020078static int
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020079headless_output_repaint(struct weston_output *output_base,
80 pixman_region32_t *damage)
81{
82 struct headless_output *output = (struct headless_output *) output_base;
83 struct weston_compositor *ec = output->base.compositor;
84
85 ec->renderer->repaint_output(&output->base, damage);
86
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +020087 pixman_region32_subtract(&ec->primary_plane.damage,
88 &ec->primary_plane.damage, damage);
89
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020090 wl_event_source_timer_update(output->finish_frame_timer, 16);
91
David Herrmann1edf44c2013-10-22 17:11:26 +020092 return 0;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +020093}
94
95static void
96headless_output_destroy(struct weston_output *output_base)
97{
98 struct headless_output *output = (struct headless_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +030099 struct headless_backend *b =
100 (struct headless_backend *) output->base.compositor->backend;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200101
102 wl_event_source_remove(output->finish_frame_timer);
Derek Foremana04e9132014-11-19 15:06:17 -0800103
Giulio Camuffo954f1832014-10-11 18:27:30 +0300104 if (b->use_pixman) {
Derek Foremana04e9132014-11-19 15:06:17 -0800105 pixman_renderer_output_destroy(&output->base);
106 pixman_image_unref(output->image);
107 free(output->image_buf);
108 }
109
110 weston_output_destroy(&output->base);
111
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200112 free(output);
113
114 return;
115}
116
117static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300118headless_backend_create_output(struct headless_backend *b,
Benoit Gschwind3c530942016-04-15 20:28:32 -0700119 struct weston_headless_backend_config *config)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200120{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300121 struct weston_compositor *c = b->compositor;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200122 struct headless_output *output;
123 struct wl_event_loop *loop;
124
Peter Huttererf3d62272013-08-08 11:57:05 +1000125 output = zalloc(sizeof *output);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200126 if (output == NULL)
127 return -1;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200128
129 output->mode.flags =
130 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Benoit Gschwind3c530942016-04-15 20:28:32 -0700131 output->mode.width = config->width;
132 output->mode.height = config->height;
Pekka Paalanen1879f212014-05-23 12:48:45 +0300133 output->mode.refresh = 60000;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200134 wl_list_init(&output->base.mode_list);
135 wl_list_insert(&output->base.mode_list, &output->mode.link);
136
Hardeningff39efa2013-09-18 23:56:35 +0200137 output->base.current_mode = &output->mode;
Benoit Gschwind3c530942016-04-15 20:28:32 -0700138 weston_output_init(&output->base, c, 0, 0, config->width,
139 config->height, config->transform, 1);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200140
141 output->base.make = "weston";
142 output->base.model = "headless";
143
Giulio Camuffo954f1832014-10-11 18:27:30 +0300144 loop = wl_display_get_event_loop(c->wl_display);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200145 output->finish_frame_timer =
146 wl_event_loop_add_timer(loop, finish_frame_handler, output);
147
Jonas Ådahle5a12252013-04-05 23:07:11 +0200148 output->base.start_repaint_loop = headless_output_start_repaint_loop;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200149 output->base.repaint = headless_output_repaint;
150 output->base.destroy = headless_output_destroy;
151 output->base.assign_planes = NULL;
152 output->base.set_backlight = NULL;
153 output->base.set_dpms = NULL;
154 output->base.switch_mode = NULL;
155
Giulio Camuffo954f1832014-10-11 18:27:30 +0300156 if (b->use_pixman) {
Benoit Gschwind3c530942016-04-15 20:28:32 -0700157 output->image_buf = malloc(config->width * config->height * 4);
Derek Foremana04e9132014-11-19 15:06:17 -0800158 if (!output->image_buf)
159 return -1;
160
161 output->image = pixman_image_create_bits(PIXMAN_x8r8g8b8,
Benoit Gschwind3c530942016-04-15 20:28:32 -0700162 config->width,
163 config->height,
Derek Foremana04e9132014-11-19 15:06:17 -0800164 output->image_buf,
Benoit Gschwind3c530942016-04-15 20:28:32 -0700165 config->width * 4);
Derek Foremana04e9132014-11-19 15:06:17 -0800166
167 if (pixman_renderer_output_create(&output->base) < 0)
168 return -1;
169
170 pixman_renderer_output_set_buffer(&output->base,
171 output->image);
172 }
173
Giulio Camuffo954f1832014-10-11 18:27:30 +0300174 weston_compositor_add_output(c, &output->base);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200175
176 return 0;
177}
178
179static void
180headless_restore(struct weston_compositor *ec)
181{
182}
183
184static void
185headless_destroy(struct weston_compositor *ec)
186{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300187 struct headless_backend *b = (struct headless_backend *) ec->backend;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200188
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200189 weston_compositor_shutdown(ec);
190
Giulio Camuffo954f1832014-10-11 18:27:30 +0300191 free(b);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200192}
193
Giulio Camuffo954f1832014-10-11 18:27:30 +0300194static struct headless_backend *
195headless_backend_create(struct weston_compositor *compositor,
Benoit Gschwind3c530942016-04-15 20:28:32 -0700196 struct weston_headless_backend_config *config)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200197{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300198 struct headless_backend *b;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200199
Giulio Camuffo954f1832014-10-11 18:27:30 +0300200 b = zalloc(sizeof *b);
201 if (b == NULL)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200202 return NULL;
203
Giulio Camuffo954f1832014-10-11 18:27:30 +0300204 b->compositor = compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300205 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
206 goto err_free;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400207
Giulio Camuffo954f1832014-10-11 18:27:30 +0300208 b->base.destroy = headless_destroy;
209 b->base.restore = headless_restore;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200210
Benoit Gschwind3c530942016-04-15 20:28:32 -0700211 b->use_pixman = config->use_pixman;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300212 if (b->use_pixman) {
213 pixman_renderer_init(compositor);
Derek Foremana04e9132014-11-19 15:06:17 -0800214 }
Armin Krezovićd84deeb2016-06-23 11:59:29 +0200215
216 if (!config->no_outputs) {
217 if (headless_backend_create_output(b, config) < 0)
218 goto err_input;
219 }
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200220
Giulio Camuffo954f1832014-10-11 18:27:30 +0300221 if (!b->use_pixman && noop_renderer_init(compositor) < 0)
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100222 goto err_input;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200223
Giulio Camuffo954f1832014-10-11 18:27:30 +0300224 compositor->backend = &b->base;
225 return b;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200226
Emilio Pozuelo Monfortdd9f6bc2014-02-07 09:34:43 +0100227err_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300228 weston_compositor_shutdown(compositor);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200229err_free:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300230 free(b);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200231 return NULL;
232}
233
Benoit Gschwind3c530942016-04-15 20:28:32 -0700234static void
235config_init_to_defaults(struct weston_headless_backend_config *config)
236{
237}
238
Giulio Camuffo954f1832014-10-11 18:27:30 +0300239WL_EXPORT int
240backend_init(struct weston_compositor *compositor,
Giulio Camuffo93daabb2015-10-17 19:24:14 +0300241 struct weston_backend_config *config_base)
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200242{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300243 struct headless_backend *b;
Benoit Gschwind3c530942016-04-15 20:28:32 -0700244 struct weston_headless_backend_config config = {{ 0, }};
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200245
Benoit Gschwind3c530942016-04-15 20:28:32 -0700246 if (config_base == NULL ||
247 config_base->struct_version != WESTON_HEADLESS_BACKEND_CONFIG_VERSION ||
248 config_base->struct_size > sizeof(struct weston_headless_backend_config)) {
249 weston_log("headless backend config structure is invalid\n");
250 return -1;
251 }
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200252
Benoit Gschwind3c530942016-04-15 20:28:32 -0700253 config_init_to_defaults(&config);
254 memcpy(&config, config_base, config_base->struct_size);
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200255
Benoit Gschwind3c530942016-04-15 20:28:32 -0700256 b = headless_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300257 if (b == NULL)
258 return -1;
Benoit Gschwind3c530942016-04-15 20:28:32 -0700259
Giulio Camuffo954f1832014-10-11 18:27:30 +0300260 return 0;
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +0200261}