compositor.c 141 KB
Newer Older
1
/*
2
3
 * Copyright © 2010-2011 Intel Corporation
 * Copyright © 2008-2011 Kristian Høgsberg
4
 * Copyright © 2012-2015 Collabora, Ltd.
5
 *
6
7
8
9
10
11
12
13
14
 * Permission to use, copy, modify, distribute, and sell this software and
 * its documentation for any purpose is hereby granted without fee, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of the copyright holders not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.  The copyright holders make
 * no representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
15
 *
16
17
18
19
20
21
22
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23
24
 */

25
26
#include "config.h"

27
#include <fcntl.h>
28
29
30
31
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
32
#include <limits.h>
33
#include <stdarg.h>
34
#include <assert.h>
35
#include <sys/ioctl.h>
36
#include <sys/mman.h>
37
#include <sys/wait.h>
38
#include <sys/socket.h>
39
#include <sys/utsname.h>
40
#include <sys/stat.h>
41
#include <unistd.h>
42
#include <math.h>
43
#include <linux/input.h>
44
#include <dlfcn.h>
45
#include <signal.h>
46
#include <setjmp.h>
47
48
#include <sys/time.h>
#include <time.h>
49
#include <errno.h>
50

51
52
53
54
55
#ifdef HAVE_LIBUNWIND
#define UNW_LOCAL_ONLY
#include <libunwind.h>
#endif

56
57
#include "timeline.h"

58
#include "compositor.h"
59
#include "scaler-server-protocol.h"
60
#include "presentation_timing-server-protocol.h"
61
#include "../shared/os-compatibility.h"
62
#include "git-version.h"
63
#include "version.h"
64

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */

#define NSEC_PER_SEC 1000000000

static void
timespec_sub(struct timespec *r,
	     const struct timespec *a, const struct timespec *b)
{
	r->tv_sec = a->tv_sec - b->tv_sec;
	r->tv_nsec = a->tv_nsec - b->tv_nsec;
	if (r->tv_nsec < 0) {
		r->tv_sec--;
		r->tv_nsec += NSEC_PER_SEC;
	}
}

static int64_t
timespec_to_nsec(const struct timespec *a)
{
	return (int64_t)a->tv_sec * NSEC_PER_SEC + a->tv_nsec;
}

87
static struct wl_list child_process_list;
88
static struct weston_compositor *segv_compositor;
89
90
91
92

static int
sigchld_handler(int signal_number, void *data)
{
93
	struct weston_process *p;
94
95
96
	int status;
	pid_t pid;

97
98
99
100
101
	while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
		wl_list_for_each(p, &child_process_list, link) {
			if (p->pid == pid)
				break;
		}
Bill Spitzak's avatar
Bill Spitzak committed
102

103
104
105
106
		if (&p->link == &child_process_list) {
			weston_log("unknown child process exited\n");
			continue;
		}
107

108
109
		wl_list_remove(&p->link);
		p->cleanup(p, status);
110
111
	}

112
113
	if (pid < 0 && errno != ECHILD)
		weston_log("waitpid error %m\n");
114
115
116
117

	return 1;
}

118
static void
119
120
weston_output_transform_scale_init(struct weston_output *output,
				   uint32_t transform, uint32_t scale);
121

122
static void
123
weston_compositor_build_view_list(struct weston_compositor *compositor);
124

125
126
127
static void weston_mode_switch_finish(struct weston_output *output,
				      int mode_changed,
				      int scale_changed)
128
{
129
	struct weston_seat *seat;
David Fort's avatar
David Fort committed
130
	struct wl_resource *resource;
131
	pixman_region32_t old_output_region;
132
	int version;
133

134
135
136
	pixman_region32_init(&old_output_region);
	pixman_region32_copy(&old_output_region, &output->region);

137
	/* Update output region and transformation matrix */
138
	weston_output_transform_scale_init(output, output->transform, output->current_scale);
139
140
141
142
143
144
145

	pixman_region32_init(&output->previous_damage);
	pixman_region32_init_rect(&output->region, output->x, output->y,
				  output->width, output->height);

	weston_output_update_matrix(output);

146
147
148
	/* If a pointer falls outside the outputs new geometry, move it to its
	 * lower-right corner */
	wl_list_for_each(seat, &output->compositor->seat_list, link) {
149
		struct weston_pointer *pointer = seat->pointer;
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
		int32_t x, y;

		if (!pointer)
			continue;

		x = wl_fixed_to_int(pointer->x);
		y = wl_fixed_to_int(pointer->y);

		if (!pixman_region32_contains_point(&old_output_region,
						    x, y, NULL) ||
		    pixman_region32_contains_point(&output->region,
						   x, y, NULL))
			continue;

		if (x >= output->x + output->width)
			x = output->x + output->width - 1;
		if (y >= output->y + output->height)
			y = output->y + output->height - 1;

		pointer->x = wl_fixed_from_int(x);
		pointer->y = wl_fixed_from_int(y);
	}

	pixman_region32_fini(&old_output_region);

175
176
177
	if (!mode_changed && !scale_changed)
		return;

David Fort's avatar
David Fort committed
178
	/* notify clients of the changes */
179
180
181
182
183
184
185
186
187
	wl_resource_for_each(resource, &output->resource_list) {
		if (mode_changed) {
			wl_output_send_mode(resource,
					    output->current_mode->flags,
					    output->current_mode->width,
					    output->current_mode->height,
					    output->current_mode->refresh);
		}

188
189
		version = wl_resource_get_version(resource);
		if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
190
191
			wl_output_send_scale(resource, output->current_scale);

192
193
		if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
			wl_output_send_done(resource);
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
	}
}

WL_EXPORT int
weston_output_mode_set_native(struct weston_output *output,
			      struct weston_mode *mode,
			      int32_t scale)
{
	int ret;
	int mode_changed = 0, scale_changed = 0;

	if (!output->switch_mode)
		return -1;

	if (!output->original_mode) {
		mode_changed = 1;
		ret = output->switch_mode(output, mode);
		if (ret < 0)
			return ret;
		if (output->current_scale != scale) {
			scale_changed = 1;
			output->current_scale = scale;
David Fort's avatar
David Fort committed
216
217
218
		}
	}

219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
	output->native_mode = mode;
	output->native_scale = scale;

	weston_mode_switch_finish(output, mode_changed, scale_changed);

	return 0;
}

WL_EXPORT int
weston_output_mode_switch_to_native(struct weston_output *output)
{
	int ret;
	int mode_changed = 0, scale_changed = 0;

	if (!output->switch_mode)
		return -1;

	if (!output->original_mode) {
		weston_log("already in the native mode\n");
		return -1;
	}
	/* the non fullscreen clients haven't seen a mode set since we
	 * switched into a temporary, so we need to notify them if the
	 * mode at that time is different from the native mode now.
	 */
	mode_changed = (output->original_mode != output->native_mode);
	scale_changed = (output->original_scale != output->native_scale);

	ret = output->switch_mode(output, output->native_mode);
	if (ret < 0)
		return ret;

	output->current_scale = output->native_scale;

	output->original_mode = NULL;
	output->original_scale = 0;

	weston_mode_switch_finish(output, mode_changed, scale_changed);

	return 0;
}

WL_EXPORT int
weston_output_mode_switch_to_temporary(struct weston_output *output,
				       struct weston_mode *mode,
				       int32_t scale)
{
	int ret;

	if (!output->switch_mode)
		return -1;

	/* original_mode is the last mode non full screen clients have seen,
	 * so we shouldn't change it if we already have one set.
	 */
	if (!output->original_mode) {
		output->original_mode = output->native_mode;
		output->original_scale = output->native_scale;
	}
	ret = output->switch_mode(output, mode);
	if (ret < 0)
		return ret;

	output->current_scale = scale;

	weston_mode_switch_finish(output, 0, 0);

	return 0;
287
288
}

289
WL_EXPORT void
290
weston_watch_process(struct weston_process *process)
291
292
293
294
{
	wl_list_insert(&child_process_list, &process->link);
}

295
296
297
static void
child_client_exec(int sockfd, const char *path)
{
298
	int clientfd;
299
	char s[32];
300
301
302
303
304
	sigset_t allsigs;

	/* do not give our signal mask to the new process */
	sigfillset(&allsigs);
	sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
305

306
307
308
309
310
	/* Launch clients as the user. Do not lauch clients with wrong euid.*/
	if (seteuid(getuid()) == -1) {
		weston_log("compositor: failed seteuid\n");
		return;
	}
311

312
313
314
315
	/* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
	 * non-CLOEXEC fd to pass through exec. */
	clientfd = dup(sockfd);
	if (clientfd == -1) {
316
		weston_log("compositor: dup failed: %m\n");
317
318
		return;
	}
319

320
	snprintf(s, sizeof s, "%d", clientfd);
321
322
323
	setenv("WAYLAND_SOCKET", s, 1);

	if (execl(path, path, NULL) < 0)
324
		weston_log("compositor: executing '%s' failed: %m\n",
325
326
327
328
			path);
}

WL_EXPORT struct wl_client *
329
330
331
332
weston_client_launch(struct weston_compositor *compositor,
		     struct weston_process *proc,
		     const char *path,
		     weston_process_cleanup_func_t cleanup)
333
334
335
336
337
{
	int sv[2];
	pid_t pid;
	struct wl_client *client;

338
339
	weston_log("launching '%s'\n", path);

340
	if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
341
		weston_log("weston_client_launch: "
342
343
344
345
346
347
348
349
350
			"socketpair failed while launching '%s': %m\n",
			path);
		return NULL;
	}

	pid = fork();
	if (pid == -1) {
		close(sv[0]);
		close(sv[1]);
351
		weston_log("weston_client_launch: "
352
353
354
355
356
357
			"fork failed while launching '%s': %m\n", path);
		return NULL;
	}

	if (pid == 0) {
		child_client_exec(sv[1], path);
358
		_exit(-1);
359
360
361
362
363
364
365
	}

	close(sv[1]);

	client = wl_client_create(compositor->wl_display, sv[0]);
	if (!client) {
		close(sv[0]);
366
		weston_log("weston_client_launch: "
367
368
369
370
371
372
373
			"wl_client_create failed while launching '%s'.\n",
			path);
		return NULL;
	}

	proc->pid = pid;
	proc->cleanup = cleanup;
374
	weston_watch_process(proc);
375
376
377
378

	return client;
}

379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
struct process_info {
	struct weston_process proc;
	char *path;
};

static void
process_handle_sigchld(struct weston_process *process, int status)
{
	struct process_info *pinfo =
		container_of(process, struct process_info, proc);

	/*
	 * There are no guarantees whether this runs before or after
	 * the wl_client destructor.
	 */

	if (WIFEXITED(status)) {
		weston_log("%s exited with status %d\n", pinfo->path,
			   WEXITSTATUS(status));
	} else if (WIFSIGNALED(status)) {
		weston_log("%s died on signal %d\n", pinfo->path,
			   WTERMSIG(status));
	} else {
		weston_log("%s disappeared\n", pinfo->path);
	}

	free(pinfo->path);
	free(pinfo);
}

WL_EXPORT struct wl_client *
weston_client_start(struct weston_compositor *compositor, const char *path)
{
	struct process_info *pinfo;
	struct wl_client *client;

	pinfo = zalloc(sizeof *pinfo);
	if (!pinfo)
		return NULL;

	pinfo->path = strdup(path);
	if (!pinfo->path)
		goto out_free;

	client = weston_client_launch(compositor, &pinfo->proc, path,
				      process_handle_sigchld);
	if (!client)
		goto out_str;

	return client;

out_str:
	free(pinfo->path);

out_free:
	free(pinfo);

	return NULL;
}

439
static void
440
region_init_infinite(pixman_region32_t *region)
441
{
442
443
	pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
				  UINT32_MAX, UINT32_MAX);
444
445
}

446
447
448
static struct weston_subsurface *
weston_surface_to_subsurface(struct weston_surface *surface);

449
450
451
452
453
WL_EXPORT struct weston_view *
weston_view_create(struct weston_surface *surface)
{
	struct weston_view *view;

454
	view = zalloc(sizeof *view);
455
456
457
458
459
460
461
462
463
464
	if (view == NULL)
		return NULL;

	view->surface = surface;

	/* Assign to surface */
	wl_list_insert(&surface->views, &view->surface_link);

	wl_signal_init(&view->destroy_signal);
	wl_list_init(&view->link);
465
	wl_list_init(&view->layer_link.link);
466
467
468
469
470
471
472
473
474
475
476

	pixman_region32_init(&view->clip);

	view->alpha = 1.0;
	pixman_region32_init(&view->transform.opaque);

	wl_list_init(&view->geometry.transformation_list);
	wl_list_insert(&view->geometry.transformation_list,
		       &view->transform.position.link);
	weston_matrix_init(&view->transform.position.matrix);
	wl_list_init(&view->geometry.child_list);
477
	pixman_region32_init(&view->geometry.scissor);
478
479
480
481
482
483
	pixman_region32_init(&view->transform.boundingbox);
	view->transform.dirty = 1;

	return view;
}

484
485
486
487
488
struct weston_frame_callback {
	struct wl_resource *resource;
	struct wl_list link;
};

489
490
491
492
493
struct weston_presentation_feedback {
	struct wl_resource *resource;

	/* XXX: could use just wl_resource_get_link() instead */
	struct wl_list link;
494
495
496

	/* The per-surface feedback flags */
	uint32_t psf_flags;
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
};

static void
weston_presentation_feedback_discard(
		struct weston_presentation_feedback *feedback)
{
	presentation_feedback_send_discarded(feedback->resource);
	wl_resource_destroy(feedback->resource);
}

static void
weston_presentation_feedback_discard_list(struct wl_list *list)
{
	struct weston_presentation_feedback *feedback, *tmp;

	wl_list_for_each_safe(feedback, tmp, list, link)
		weston_presentation_feedback_discard(feedback);
}

static void
weston_presentation_feedback_present(
		struct weston_presentation_feedback *feedback,
		struct weston_output *output,
		uint32_t refresh_nsec,
		const struct timespec *ts,
522
523
		uint64_t seq,
		uint32_t flags)
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
{
	struct wl_client *client = wl_resource_get_client(feedback->resource);
	struct wl_resource *o;
	uint64_t secs;

	wl_resource_for_each(o, &output->resource_list) {
		if (wl_resource_get_client(o) != client)
			continue;

		presentation_feedback_send_sync_output(feedback->resource, o);
	}

	secs = ts->tv_sec;
	presentation_feedback_send_presented(feedback->resource,
					     secs >> 32, secs & 0xffffffff,
					     ts->tv_nsec,
					     refresh_nsec,
					     seq >> 32, seq & 0xffffffff,
542
					     flags | feedback->psf_flags);
543
544
545
546
547
548
549
550
	wl_resource_destroy(feedback->resource);
}

static void
weston_presentation_feedback_present_list(struct wl_list *list,
					  struct weston_output *output,
					  uint32_t refresh_nsec,
					  const struct timespec *ts,
551
552
					  uint64_t seq,
					  uint32_t flags)
553
554
555
{
	struct weston_presentation_feedback *feedback, *tmp;

556
557
558
	assert(!(flags & PRESENTATION_FEEDBACK_INVALID) ||
	       wl_list_empty(list));

559
560
	wl_list_for_each_safe(feedback, tmp, list, link)
		weston_presentation_feedback_present(feedback, output,
561
562
						     refresh_nsec, ts, seq,
						     flags);
563
564
}

565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
static void
surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
{
	struct weston_surface_state *state =
		container_of(listener, struct weston_surface_state,
			     buffer_destroy_listener);

	state->buffer = NULL;
}

static void
weston_surface_state_init(struct weston_surface_state *state)
{
	state->newly_attached = 0;
	state->buffer = NULL;
	state->buffer_destroy_listener.notify =
		surface_state_handle_buffer_destroy;
	state->sx = 0;
	state->sy = 0;

	pixman_region32_init(&state->damage);
	pixman_region32_init(&state->opaque);
	region_init_infinite(&state->input);

	wl_list_init(&state->frame_callback_list);
590
	wl_list_init(&state->feedback_list);
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607

	state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
	state->buffer_viewport.buffer.scale = 1;
	state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
	state->buffer_viewport.surface.width = -1;
	state->buffer_viewport.changed = 0;
}

static void
weston_surface_state_fini(struct weston_surface_state *state)
{
	struct weston_frame_callback *cb, *next;

	wl_list_for_each_safe(cb, next,
			      &state->frame_callback_list, link)
		wl_resource_destroy(cb->resource);

608
609
	weston_presentation_feedback_discard_list(&state->feedback_list);

610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
	pixman_region32_fini(&state->input);
	pixman_region32_fini(&state->opaque);
	pixman_region32_fini(&state->damage);

	if (state->buffer)
		wl_list_remove(&state->buffer_destroy_listener.link);
	state->buffer = NULL;
}

static void
weston_surface_state_set_buffer(struct weston_surface_state *state,
				struct weston_buffer *buffer)
{
	if (state->buffer == buffer)
		return;

	if (state->buffer)
		wl_list_remove(&state->buffer_destroy_listener.link);
	state->buffer = buffer;
	if (state->buffer)
		wl_signal_add(&state->buffer->destroy_signal,
			      &state->buffer_destroy_listener);
}

634
WL_EXPORT struct weston_surface *
635
weston_surface_create(struct weston_compositor *compositor)
636
{
637
	struct weston_surface *surface;
638

639
	surface = zalloc(sizeof *surface);
640
641
642
	if (surface == NULL)
		return NULL;

643
644
	wl_signal_init(&surface->destroy_signal);

645
	surface->compositor = compositor;
646
	surface->ref_count = 1;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
647

648
649
	surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
	surface->buffer_viewport.buffer.scale = 1;
650
651
	surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
	surface->buffer_viewport.surface.width = -1;
652
653
654

	weston_surface_state_init(&surface->pending);

655
	pixman_region32_init(&surface->damage);
656
	pixman_region32_init(&surface->opaque);
Pekka Paalanen's avatar
Pekka Paalanen committed
657
	region_init_infinite(&surface->input);
658

659
660
661
	wl_list_init(&surface->views);

	wl_list_init(&surface->frame_callback_list);
662
	wl_list_init(&surface->feedback_list);
663

664
665
666
	wl_list_init(&surface->subsurface_list);
	wl_list_init(&surface->subsurface_list_pending);

667
668
669
	weston_matrix_init(&surface->buffer_to_surface_matrix);
	weston_matrix_init(&surface->surface_to_buffer_matrix);

670
	return surface;
671
672
}

673
WL_EXPORT void
674
weston_surface_set_color(struct weston_surface *surface,
675
		 float red, float green, float blue, float alpha)
676
{
677
	surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
678
679
}

680
WL_EXPORT void
681
682
weston_view_to_global_float(struct weston_view *view,
			    float sx, float sy, float *x, float *y)
683
{
684
	if (view->transform.enabled) {
685
686
		struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };

687
		weston_matrix_transform(&view->transform.matrix, &v);
688
689

		if (fabsf(v.f[3]) < 1e-6) {
690
			weston_log("warning: numerical instability in "
691
				"%s(), divisor = %g\n", __func__,
692
693
694
695
696
697
698
699
700
				v.f[3]);
			*x = 0;
			*y = 0;
			return;
		}

		*x = v.f[0] / v.f[3];
		*y = v.f[1] / v.f[3];
	} else {
701
702
		*x = sx + view->geometry.x;
		*y = sy + view->geometry.y;
703
704
705
	}
}

706
707
708
WL_EXPORT void
weston_transformed_coord(int width, int height,
			 enum wl_output_transform transform,
709
			 int32_t scale,
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
			 float sx, float sy, float *bx, float *by)
{
	switch (transform) {
	case WL_OUTPUT_TRANSFORM_NORMAL:
	default:
		*bx = sx;
		*by = sy;
		break;
	case WL_OUTPUT_TRANSFORM_FLIPPED:
		*bx = width - sx;
		*by = sy;
		break;
	case WL_OUTPUT_TRANSFORM_90:
		*bx = height - sy;
		*by = sx;
		break;
	case WL_OUTPUT_TRANSFORM_FLIPPED_90:
		*bx = height - sy;
		*by = width - sx;
		break;
	case WL_OUTPUT_TRANSFORM_180:
		*bx = width - sx;
		*by = height - sy;
		break;
	case WL_OUTPUT_TRANSFORM_FLIPPED_180:
		*bx = sx;
		*by = height - sy;
		break;
	case WL_OUTPUT_TRANSFORM_270:
		*bx = sy;
		*by = width - sx;
		break;
	case WL_OUTPUT_TRANSFORM_FLIPPED_270:
		*bx = sy;
		*by = sx;
		break;
	}
747
748
749

	*bx *= scale;
	*by *= scale;
750
751
752
753
754
}

WL_EXPORT pixman_box32_t
weston_transformed_rect(int width, int height,
			enum wl_output_transform transform,
755
			int32_t scale,
756
757
758
759
760
761
			pixman_box32_t rect)
{
	float x1, x2, y1, y2;

	pixman_box32_t ret;

762
	weston_transformed_coord(width, height, transform, scale,
763
				 rect.x1, rect.y1, &x1, &y1);
764
	weston_transformed_coord(width, height, transform, scale,
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
				 rect.x2, rect.y2, &x2, &y2);

	if (x1 <= x2) {
		ret.x1 = x1;
		ret.x2 = x2;
	} else {
		ret.x1 = x2;
		ret.x2 = x1;
	}

	if (y1 <= y2) {
		ret.y1 = y1;
		ret.y2 = y2;
	} else {
		ret.y1 = y2;
		ret.y2 = y1;
	}

	return ret;
}

786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
WL_EXPORT void
weston_transformed_region(int width, int height,
			  enum wl_output_transform transform,
			  int32_t scale,
			  pixman_region32_t *src, pixman_region32_t *dest)
{
	pixman_box32_t *src_rects, *dest_rects;
	int nrects, i;

	if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
		if (src != dest)
			pixman_region32_copy(dest, src);
		return;
	}

	src_rects = pixman_region32_rectangles(src, &nrects);
	dest_rects = malloc(nrects * sizeof(*dest_rects));
	if (!dest_rects)
		return;

	if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
		memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
	} else {
		for (i = 0; i < nrects; i++) {
			switch (transform) {
			default:
			case WL_OUTPUT_TRANSFORM_NORMAL:
				dest_rects[i].x1 = src_rects[i].x1;
				dest_rects[i].y1 = src_rects[i].y1;
				dest_rects[i].x2 = src_rects[i].x2;
				dest_rects[i].y2 = src_rects[i].y2;
				break;
			case WL_OUTPUT_TRANSFORM_90:
				dest_rects[i].x1 = height - src_rects[i].y2;
				dest_rects[i].y1 = src_rects[i].x1;
				dest_rects[i].x2 = height - src_rects[i].y1;
				dest_rects[i].y2 = src_rects[i].x2;
				break;
			case WL_OUTPUT_TRANSFORM_180:
				dest_rects[i].x1 = width - src_rects[i].x2;
				dest_rects[i].y1 = height - src_rects[i].y2;
				dest_rects[i].x2 = width - src_rects[i].x1;
				dest_rects[i].y2 = height - src_rects[i].y1;
				break;
			case WL_OUTPUT_TRANSFORM_270:
				dest_rects[i].x1 = src_rects[i].y1;
				dest_rects[i].y1 = width - src_rects[i].x2;
				dest_rects[i].x2 = src_rects[i].y2;
				dest_rects[i].y2 = width - src_rects[i].x1;
				break;
			case WL_OUTPUT_TRANSFORM_FLIPPED:
				dest_rects[i].x1 = width - src_rects[i].x2;
				dest_rects[i].y1 = src_rects[i].y1;
				dest_rects[i].x2 = width - src_rects[i].x1;
				dest_rects[i].y2 = src_rects[i].y2;
				break;
			case WL_OUTPUT_TRANSFORM_FLIPPED_90:
				dest_rects[i].x1 = height - src_rects[i].y2;
				dest_rects[i].y1 = width - src_rects[i].x2;
				dest_rects[i].x2 = height - src_rects[i].y1;
				dest_rects[i].y2 = width - src_rects[i].x1;
				break;
			case WL_OUTPUT_TRANSFORM_FLIPPED_180:
				dest_rects[i].x1 = src_rects[i].x1;
				dest_rects[i].y1 = height - src_rects[i].y2;
				dest_rects[i].x2 = src_rects[i].x2;
				dest_rects[i].y2 = height - src_rects[i].y1;
				break;
			case WL_OUTPUT_TRANSFORM_FLIPPED_270:
				dest_rects[i].x1 = src_rects[i].y1;
				dest_rects[i].y1 = src_rects[i].x1;
				dest_rects[i].x2 = src_rects[i].y2;
				dest_rects[i].y2 = src_rects[i].x2;
				break;
			}
		}
	}

	if (scale != 1) {
		for (i = 0; i < nrects; i++) {
			dest_rects[i].x1 *= scale;
			dest_rects[i].x2 *= scale;
			dest_rects[i].y1 *= scale;
			dest_rects[i].y2 *= scale;
		}
	}

	pixman_region32_clear(dest);
	pixman_region32_init_rects(dest, dest_rects, nrects);
	free(dest_rects);
}

878
879
880
881
static void
scaler_surface_to_buffer(struct weston_surface *surface,
			 float sx, float sy, float *bx, float *by)
{
882
	struct weston_buffer_viewport *vp = &surface->buffer_viewport;
883
884
	double src_width, src_height;
	double src_x, src_y;
885

886
887
888
889
890
891
	if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
		if (vp->surface.width == -1) {
			*bx = sx;
			*by = sy;
			return;
		}
892

893
894
895
896
		src_x = 0.0;
		src_y = 0.0;
		src_width = surface->width_from_buffer;
		src_height = surface->height_from_buffer;
897
	} else {
898
899
900
901
		src_x = wl_fixed_to_double(vp->buffer.src_x);
		src_y = wl_fixed_to_double(vp->buffer.src_y);
		src_width = wl_fixed_to_double(vp->buffer.src_width);
		src_height = wl_fixed_to_double(vp->buffer.src_height);
902
	}
903
904
905

	*bx = sx * src_width / surface->width + src_x;
	*by = sy * src_height / surface->height + src_y;
906
907
}

908
909
910
911
WL_EXPORT void
weston_surface_to_buffer_float(struct weston_surface *surface,
			       float sx, float sy, float *bx, float *by)
{
912
913
	struct weston_buffer_viewport *vp = &surface->buffer_viewport;

914
915
916
	/* first transform coordinates if the scaler is set */
	scaler_surface_to_buffer(surface, sx, sy, bx, by);

917
918
	weston_transformed_coord(surface->width_from_buffer,
				 surface->height_from_buffer,
919
				 vp->buffer.transform, vp->buffer.scale,
920
				 *bx, *by, bx, by);
921
922
}

923
924
925
926
927
928
WL_EXPORT void
weston_surface_to_buffer(struct weston_surface *surface,
			 int sx, int sy, int *bx, int *by)
{
	float bxf, byf;

929
930
931
	weston_surface_to_buffer_float(surface,
				       sx, sy, &bxf, &byf);

932
933
934
935
	*bx = floorf(bxf);
	*by = floorf(byf);
}

936
937
938
939
WL_EXPORT pixman_box32_t
weston_surface_to_buffer_rect(struct weston_surface *surface,
			      pixman_box32_t rect)
{
940
	struct weston_buffer_viewport *vp = &surface->buffer_viewport;
941
942
943
944
945
946
947
948
949
950
951
	float xf, yf;

	/* first transform box coordinates if the scaler is set */
	scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
	rect.x1 = floorf(xf);
	rect.y1 = floorf(yf);

	scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
	rect.x2 = floorf(xf);
	rect.y2 = floorf(yf);

952
953
	return weston_transformed_rect(surface->width_from_buffer,
				       surface->height_from_buffer,
954
				       vp->buffer.transform, vp->buffer.scale,
955
				       rect);
956
957
}

958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
/** Transform a region from surface coordinates to buffer coordinates
 *
 * \param surface The surface to fetch wl_viewport and buffer transformation
 * from.
 * \param surface_region[in] The region in surface coordinates.
 * \param buffer_region[out] The region converted to buffer coordinates.
 *
 * Buffer_region must be init'd, but will be completely overwritten.
 *
 * Viewport and buffer transformations can only do translation, scaling,
 * and rotations in 90-degree steps. Therefore the only loss in the
 * conversion is coordinate flooring (rounding).
 */
WL_EXPORT void
weston_surface_to_buffer_region(struct weston_surface *surface,
				pixman_region32_t *surface_region,
				pixman_region32_t *buffer_region)
{
	pixman_box32_t *src_rects, *dest_rects;
	int nrects, i;

	src_rects = pixman_region32_rectangles(surface_region, &nrects);
	dest_rects = malloc(nrects * sizeof(*dest_rects));
	if (!dest_rects)
		return;

	for (i = 0; i < nrects; i++) {
		dest_rects[i] = weston_surface_to_buffer_rect(surface,
							      src_rects[i]);
	}

	pixman_region32_fini(buffer_region);
	pixman_region32_init_rects(buffer_region, dest_rects, nrects);
	free(dest_rects);
}

994
WL_EXPORT void
995
weston_view_move_to_plane(struct weston_view *view,
996
997
			     struct weston_plane *plane)
{
998
	if (view->plane == plane)
999
1000
		return;

1001
1002
1003
	weston_view_damage_below(view);
	view->plane = plane;
	weston_surface_damage(view->surface);
1004
1005
}

1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
/** Inflict damage on the plane where the view is visible.
 *
 * \param view The view that causes the damage.
 *
 * If the view is currently on a plane (including the primary plane),
 * take the view's boundingbox, subtract all the opaque views that cover it,
 * and add the remaining region as damage to the plane. This corresponds
 * to the damage inflicted to the plane if this view disappeared.
 *
 * A repaint is scheduled for this view.
 *
 * The region of all opaque views covering this view is stored in
 * weston_view::clip and updated by view_accumulate_damage() during
 * weston_output_repaint(). Specifically, that region matches the
 * scenegraph as it was last painted.
 */
1022
WL_EXPORT void
1023
weston_view_damage_below(struct weston_view *view)
1024
{
1025
1026
1027
	pixman_region32_t damage;

	pixman_region32_init(&damage);
1028
	pixman_region32_subtract(&damage, &view->transform.boundingbox,
1029
				 &view->clip);
1030
1031
1032
	if (view->plane)
		pixman_region32_union(&view->plane->damage,
				      &view->plane->damage, &damage);
1033
	pixman_region32_fini(&damage);
1034
	weston_view_schedule_repaint(view);
1035
1036
}

1037
1038
1039
1040
1041
1042
1043
1044
static void
weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
{
	uint32_t different = es->output_mask ^ mask;
	uint32_t entered = mask & different;
	uint32_t left = es->output_mask & different;
	struct weston_output *output;
	struct wl_resource *resource = NULL;
1045
	struct wl_client *client;
1046
1047

	es->output_mask = mask;
1048
	if (es->resource == NULL)
1049
1050
1051
1052
		return;
	if (different == 0)
		return;

1053
1054
	client = wl_resource_get_client(es->resource);

1055
1056
1057
	wl_list_for_each(output, &es->compositor->output_list, link) {
		if (1 << output->id & different)
			resource =
1058
				wl_resource_find_for_client(&output->resource_list,
1059
1060
1061
1062
							 client);
		if (resource == NULL)
			continue;
		if (1 << output->id & entered)
1063
			wl_surface_send_enter(es->resource, resource);
1064
		if (1 << output->id & left)
1065
			wl_surface_send_leave(es->resource, resource);
1066
1067
1068
	}
}

1069

1070
1071
1072
static void
weston_surface_assign_output(struct weston_surface *es)
{
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
	struct weston_output *new_output;
	struct weston_view *view;
	pixman_region32_t region;
	uint32_t max, area, mask;
	pixman_box32_t *e;

	new_output = NULL;
	max = 0;
	mask = 0;
	pixman_region32_init(&region);
	wl_list_for_each(view, &es->views, surface_link) {
		if (!view->output)
			continue;

		pixman_region32_intersect(&region, &view->transform.boundingbox,
					  &view->output->region);

		e = pixman_region32_extents(&region);
		area = (e->x2 - e->x1) * (e->y2 - e->y1);

		mask |= view->output_mask;

		if (area >= max) {
			new_output = view->output;
			max = area;
		}
	}
	pixman_region32_fini(&region);

	es->output = new_output;
	weston_surface_update_output_mask(es, mask);
}

static void
weston_view_assign_output(struct weston_view *ev)
{
	struct weston_compositor *ec = ev->surface->compositor;
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
	struct weston_output *output, *new_output;
	pixman_region32_t region;
	uint32_t max, area, mask;
	pixman_box32_t *e;

	new_output = NULL;
	max = 0;
	mask = 0;
	pixman_region32_init(&region);
	wl_list_for_each(output, &ec->output_list, link) {
1120
		pixman_region32_intersect(&region, &ev->transform.boundingbox,
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
					  &output->region);

		e = pixman_region32_extents(&region);
		area = (e->x2 - e->x1) * (e->y2 - e->y1);

		if (area > 0)
			mask |= 1 << output->id;

		if (area >= max) {
			new_output = output;
			max = area;
		}
	}
	pixman_region32_fini(&region);

1136
1137
1138
1139
	ev->output = new_output;
	ev->output_mask = mask;

	weston_surface_assign_output(ev->surface);
1140
1141
}

1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
static void
weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
			int from_x, int from_y, int *to_x, int *to_y)
{
	float x, y;

	weston_view_to_global_float(from, from_x, from_y, &x, &y);
	weston_view_from_global_float(to, x, y, &x, &y);

	*to_x = round(x);
	*to_y = round(y);
}

static void
weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
{
	pixman_box32_t *a;
	pixman_box32_t b;

	a = pixman_region32_extents(&from->geometry.scissor);

	weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
	weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);

	pixman_region32_fini(&to->geometry.scissor);
	pixman_region32_init_with_extents(&to->geometry.scissor, &b);
}

1170
static void
1171
view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
1172
		  pixman_region32_t *bbox)
1173
{
1174
1175
	float min_x = HUGE_VALF,  min_y = HUGE_VALF;
	float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
1176
	int32_t s[4][2] = {
1177
1178
1179
1180
		{ inbox->x1, inbox->y1 },
		{ inbox->x1, inbox->y2 },
		{ inbox->x2, inbox->y1 },
		{ inbox->x2, inbox->y2 },
1181
	};
1182
	float int_x, int_y;
1183
1184
	int i;

1185
	if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
1186
1187
1188
1189
1190
		/* avoid rounding empty bbox to 1x1 */
		pixman_region32_init(bbox);
		return;
	}

1191
	for (i = 0; i < 4; ++i) {
1192
		float x, y;
1193
		weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
		if (x < min_x)
			min_x = x;
		if (x > max_x)
			max_x = x;
		if (y < min_y)
			min_y = y;
		if (y > max_y)
			max_y = y;
	}

1204
1205
1206
1207
	int_x = floorf(min_x);
	int_y = floorf(min_y);
	pixman_region32_init_rect(bbox, int_x, int_y,
				  ceilf(max_x) - int_x, ceilf(max_y) - int_y);
1208
1209
}

1210
static void
1211
weston_view_update_transform_disable(struct weston_view *view)
1212
{
1213
	view->transform.enabled = 0;
1214

1215
	/* round off fractions when not transformed */
1216
1217
	view->geometry.x = roundf(view->geometry.x);
	view->geometry.y = roundf(view->geometry.y);
1218

1219
	/* Otherwise identity matrix, but with x and y translation. */
1220
1221
1222
	view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
	view->transform.position.matrix.d[12] = view->geometry.x;
	view->transform.position.matrix.d[13] = view->geometry.y;
1223

1224
	view->transform.matrix = view->transform.position.matrix;
1225

1226
1227
1228
	view->transform.inverse = view->transform.position.matrix;
	view->transform.inverse.d[12] = -view->geometry.x;
	view->transform.inverse.d[13] = -view->geometry.y;
1229

1230
	pixman_region32_init_rect(&view->transform.boundingbox,
1231
				  0, 0,
1232
1233
				  view->surface->width,
				  view->surface->height);
1234
1235
1236
1237
1238
1239
1240
	if (view->geometry.scissor_enabled)
		pixman_region32_intersect(&view->transform.boundingbox,
					  &view->transform.boundingbox,
					  &view->geometry.scissor);

	pixman_region32_translate(&view->transform.boundingbox,
				  view->geometry.x, view->geometry.y);
1241

1242
1243
1244
1245
1246
1247
	if (view->alpha == 1.0) {
		pixman_region32_copy(&view->transform.opaque,
				     &view->surface->opaque);
		pixman_region32_translate(&view->transform.opaque,
					  view->geometry.x,
					  view->geometry.y);
1248
	}
1249
1250
1251
}

static int
1252
weston_view_update_transform_enable(struct weston_view *view)
1253
{
1254
1255
1256
	struct weston_view *parent = view->geometry.parent;
	struct weston_matrix *matrix = &view->transform.matrix;
	struct weston_matrix *inverse = &view->transform.inverse;
1257
	struct weston_transform *tform;
1258
1259
	pixman_region32_t surfregion;
	const pixman_box32_t *surfbox;
1260

1261
	view->transform.enabled = 1;
1262

1263
	/* Otherwise identity matrix, but with x and y translation. */
1264
1265
1266
	view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
	view->transform.position.matrix.d[12] = view->geometry.x;
	view->transform.position.matrix.d[13] = view->geometry.y;
1267

1268
	weston_matrix_init(matrix);
1269
	wl_list_for_each(tform, &view->geometry.transformation_list, link)
1270
1271
		weston_matrix_multiply(matrix, &tform->matrix);

1272
1273
1274
	if (parent)
		weston_matrix_multiply(matrix, &parent->transform.matrix);

1275
1276
	if (weston_matrix_invert(inverse, matrix) < 0) {
		/* Oops, bad total transformation, not invertible */
1277
1278
		weston_log("error: weston_view %p"
			" transformation not invertible.\n", view);
1279
		return -1;
1280
	}
1281

1282
1283
1284
1285
1286
1287
1288
1289
1290
	pixman_region32_init_rect(&surfregion, 0, 0,
				  view->surface->width, view->surface->height);
	if (view->geometry.scissor_enabled)
		pixman_region32_intersect(&surfregion, &surfregion,
					  &view->geometry.scissor);
	surfbox = pixman_region32_extents(&surfregion);

	view_compute_bbox(view, surfbox, &view->transform.boundingbox);
	pixman_region32_fini(&surfregion);
1291

1292
1293
1294
	return 0;
}

1295
1296
1297
1298
1299
1300
1301
1302
static struct weston_layer *
get_view_layer(struct weston_view *view)
{
	if (view->parent_view)
		return get_view_layer(view->parent_view);
	return view->layer_link.layer;
}

1303
WL_EXPORT void
1304
weston_view_update_transform(struct weston_view *view)
1305
{
1306
	struct weston_view *parent = view->geometry.parent;
1307
1308
	struct weston_layer *layer;
	pixman_region32_t mask;
1309

1310
	if (!view->transform.dirty)
1311
1312
		return;

1313
	if (parent)
1314
		weston_view_update_transform(parent);
1315

1316
	view->transform.dirty = 0;
1317

1318
	weston_view_damage_below(view);
1319

1320
1321
1322
	pixman_region32_fini(&view->transform.boundingbox);
	pixman_region32_fini(&view->transform.opaque);
	pixman_region32_init(&view->transform.opaque);
1323

1324
	/* transform.position is always in transformation_list */
1325
1326
1327
1328
	if (view->geometry.transformation_list.next ==
	    &view->transform.position.link &&
	    view->geometry.transformation_list.prev ==
	    &view->transform.position.link &&
1329
	    !parent) {
1330
		weston_view_update_transform_disable(view);
1331
	} else {
1332
1333
		if (weston_view_update_transform_enable(view) < 0)
			weston_view_update_transform_disable(view);
1334
	}
1335

1336
1337
1338
	layer = get_view_layer(view);
	if (layer) {
		pixman_region32_init_with_extents(&mask, &layer->mask);
1339
1340
		pixman_region32_intersect(&view->transform.boundingbox,
					  &view->transform.boundingbox, &mask);
1341
1342
		pixman_region32_intersect(&view->transform.opaque,
					  &view->transform.opaque, &mask);
1343
1344
1345
		pixman_region32_fini(&mask);
	}

1346
1347
1348
1349
1350
1351
1352
1353
1354
	if (parent) {
		if (parent->geometry.scissor_enabled) {
			view->geometry.scissor_enabled = true;
			weston_view_transfer_scissor(parent, view);
		} else {
			view->geometry.scissor_enabled = false;
		}
	}