compositor-headless.c 6.72 KB
Newer Older
1 2 3 4
/*
 * Copyright © 2010-2011 Benjamin Franzke
 * Copyright © 2012 Intel Corporation
 *
5 6 7 8 9 10 11
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
12
 *
13 14 15 16 17 18 19 20 21 22 23 24
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
25 26
 */

27
#include "config.h"
28

29
#include <stdint.h>
30 31 32
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
33
#include <stdbool.h>
34 35

#include "compositor.h"
36 37
#include "compositor-headless.h"
#include "shared/helpers.h"
38
#include "pixman-renderer.h"
39
#include "presentation-time-server-protocol.h"
40

41 42 43
struct headless_backend {
	struct weston_backend base;
	struct weston_compositor *compositor;
44

45
	struct weston_seat fake_seat;
46
	bool use_pixman;
47 48 49 50
};

struct headless_output {
	struct weston_output base;
51

52 53
	struct weston_mode mode;
	struct wl_event_source *finish_frame_timer;
54 55
	uint32_t *image_buf;
	pixman_image_t *image;
56 57
};

58 59
static void
headless_output_start_repaint_loop(struct weston_output *output)
60
{
61
	struct timespec ts;
62

63
	weston_compositor_read_presentation_clock(output->compositor, &ts);
64
	weston_output_finish_frame(output, &ts, WP_PRESENTATION_FEEDBACK_INVALID);
65 66 67 68 69
}

static int
finish_frame_handler(void *data)
{
70 71 72
	struct headless_output *output = data;
	struct timespec ts;

73
	weston_compositor_read_presentation_clock(output->base.compositor, &ts);
74
	weston_output_finish_frame(&output->base, &ts, 0);
75 76 77 78

	return 1;
}

79
static int
80 81 82 83 84 85 86 87
headless_output_repaint(struct weston_output *output_base,
		       pixman_region32_t *damage)
{
	struct headless_output *output = (struct headless_output *) output_base;
	struct weston_compositor *ec = output->base.compositor;

	ec->renderer->repaint_output(&output->base, damage);

88 89 90
	pixman_region32_subtract(&ec->primary_plane.damage,
				 &ec->primary_plane.damage, damage);

91 92
	wl_event_source_timer_update(output->finish_frame_timer, 16);

93
	return 0;
94 95 96 97 98 99
}

static void
headless_output_destroy(struct weston_output *output_base)
{
	struct headless_output *output = (struct headless_output *) output_base;
100 101
	struct headless_backend *b =
			(struct headless_backend *) output->base.compositor->backend;
102 103

	wl_event_source_remove(output->finish_frame_timer);
104

105
	if (b->use_pixman) {
106 107 108 109 110 111 112
		pixman_renderer_output_destroy(&output->base);
		pixman_image_unref(output->image);
		free(output->image_buf);
	}

	weston_output_destroy(&output->base);

113 114 115 116 117 118
	free(output);

	return;
}

static int
119
headless_backend_create_output(struct headless_backend *b,
120
			       struct weston_headless_backend_config *config)
121
{
122
	struct weston_compositor *c = b->compositor;
123 124 125
	struct headless_output *output;
	struct wl_event_loop *loop;

Peter Hutterer's avatar
Peter Hutterer committed
126
	output = zalloc(sizeof *output);
127 128 129 130 131
	if (output == NULL)
		return -1;

	output->mode.flags =
		WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
132 133
	output->mode.width = config->width;
	output->mode.height = config->height;
134
	output->mode.refresh = 60000;
135 136 137
	wl_list_init(&output->base.mode_list);
	wl_list_insert(&output->base.mode_list, &output->mode.link);

138
	output->base.current_mode = &output->mode;
139 140
	weston_output_init(&output->base, c, 0, 0, config->width,
			   config->height, config->transform, 1);
141 142 143 144

	output->base.make = "weston";
	output->base.model = "headless";

145
	loop = wl_display_get_event_loop(c->wl_display);
146 147 148
	output->finish_frame_timer =
		wl_event_loop_add_timer(loop, finish_frame_handler, output);

149
	output->base.start_repaint_loop = headless_output_start_repaint_loop;
150 151 152 153 154 155 156
	output->base.repaint = headless_output_repaint;
	output->base.destroy = headless_output_destroy;
	output->base.assign_planes = NULL;
	output->base.set_backlight = NULL;
	output->base.set_dpms = NULL;
	output->base.switch_mode = NULL;

157
	if (b->use_pixman) {
158
		output->image_buf = malloc(config->width * config->height * 4);
159 160 161 162
		if (!output->image_buf)
			return -1;

		output->image = pixman_image_create_bits(PIXMAN_x8r8g8b8,
163 164
							 config->width,
							 config->height,
165
							 output->image_buf,
166
							 config->width * 4);
167 168 169 170 171 172 173 174

		if (pixman_renderer_output_create(&output->base) < 0)
			return -1;

		pixman_renderer_output_set_buffer(&output->base,
						  output->image);
	}

175
	weston_compositor_add_output(c, &output->base);
176 177 178 179 180 181 182 183 184 185 186 187

	return 0;
}

static void
headless_restore(struct weston_compositor *ec)
{
}

static void
headless_destroy(struct weston_compositor *ec)
{
188
	struct headless_backend *b = (struct headless_backend *) ec->backend;
189 190 191

	weston_compositor_shutdown(ec);

192
	free(b);
193 194
}

195 196
static struct headless_backend *
headless_backend_create(struct weston_compositor *compositor,
197
			struct weston_headless_backend_config *config)
198
{
199
	struct headless_backend *b;
200

201 202
	b = zalloc(sizeof *b);
	if (b == NULL)
203 204
		return NULL;

205 206 207
	b->compositor = compositor;
	if (weston_compositor_set_presentation_clock_software(compositor) < 0)
		goto err_free;
208

209 210
	b->base.destroy = headless_destroy;
	b->base.restore = headless_restore;
211

212
	b->use_pixman = config->use_pixman;
213 214
	if (b->use_pixman) {
		pixman_renderer_init(compositor);
215
	}
216 217 218 219 220

	if (!config->no_outputs) {
		if (headless_backend_create_output(b, config) < 0)
			goto err_input;
	}
221

222
	if (!b->use_pixman && noop_renderer_init(compositor) < 0)
223
		goto err_input;
224

225 226
	compositor->backend = &b->base;
	return b;
227

228
err_input:
229
	weston_compositor_shutdown(compositor);
230
err_free:
231
	free(b);
232 233 234
	return NULL;
}

235 236 237 238 239
static void
config_init_to_defaults(struct weston_headless_backend_config *config)
{
}

240 241
WL_EXPORT int
backend_init(struct weston_compositor *compositor,
242
	     struct weston_backend_config *config_base)
243
{
244
	struct headless_backend *b;
245
	struct weston_headless_backend_config config = {{ 0, }};
246

247 248 249 250 251 252
	if (config_base == NULL ||
	    config_base->struct_version != WESTON_HEADLESS_BACKEND_CONFIG_VERSION ||
	    config_base->struct_size > sizeof(struct weston_headless_backend_config)) {
		weston_log("headless backend config structure is invalid\n");
		return -1;
	}
253

254 255
	config_init_to_defaults(&config);
	memcpy(&config, config_base, config_base->struct_size);
256

257
	b = headless_backend_create(compositor, &config);
258 259
	if (b == NULL)
		return -1;
260

261
	return 0;
262
}