blob: 50ff373e579050118c84d497d0382ae16ac78068 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004 * Copyright © 2012-2015 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04006 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050015 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040016 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Kristian Høgsberga9410222011-01-14 17:22:35 -050025#include "config.h"
26
Daniel Stoneb7452fe2012-06-01 12:14:06 +010027#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040028#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010032#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050033#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020034#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040035#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010036#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040037#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020038#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020039#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020040#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040041#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050042#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040043#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040044#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040045#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050046#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040047#include <sys/time.h>
48#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030049#include <errno.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050050
Marcin Slusarz554a0da2013-02-18 13:27:22 -050051#ifdef HAVE_LIBUNWIND
52#define UNW_LOCAL_ONLY
53#include <libunwind.h>
54#endif
55
Pekka Paalanenb5026542014-11-12 15:09:24 +020056#include "timeline.h"
57
Kristian Høgsberg82863022010-06-04 21:52:02 -040058#include "compositor.h"
Jonny Lamb8ae35902013-11-26 18:19:45 +010059#include "scaler-server-protocol.h"
Pekka Paalanen31f7d782014-09-23 22:08:43 -040060#include "presentation_timing-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030061#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040062#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050063#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050064
Kristian Høgsberg27da5382011-06-21 17:32:25 -040065static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040066static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040067
68static int
69sigchld_handler(int signal_number, void *data)
70{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050071 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040072 int status;
73 pid_t pid;
74
Pekka Paalanen23ade622014-08-27 13:31:26 +030075 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
76 wl_list_for_each(p, &child_process_list, link) {
77 if (p->pid == pid)
78 break;
79 }
Bill Spitzak027b9622012-03-17 15:22:03 -070080
Pekka Paalanen23ade622014-08-27 13:31:26 +030081 if (&p->link == &child_process_list) {
82 weston_log("unknown child process exited\n");
83 continue;
84 }
85
86 wl_list_remove(&p->link);
87 p->cleanup(p, status);
Kristian Høgsberg27da5382011-06-21 17:32:25 -040088 }
89
Pekka Paalanen23ade622014-08-27 13:31:26 +030090 if (pid < 0 && errno != ECHILD)
91 weston_log("waitpid error %m\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040092
93 return 1;
94}
95
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020096static void
Alexander Larsson0b135062013-05-28 16:23:36 +020097weston_output_transform_scale_init(struct weston_output *output,
98 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020099
Rob Bradford27b17932013-06-26 18:08:46 +0100100static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500101weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +0100102
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600103static void weston_mode_switch_finish(struct weston_output *output,
104 int mode_changed,
105 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800106{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200107 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +0200108 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200109 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -0600110 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +0200111
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200112 pixman_region32_init(&old_output_region);
113 pixman_region32_copy(&old_output_region, &output->region);
114
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200115 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200116 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200117
118 pixman_region32_init(&output->previous_damage);
119 pixman_region32_init_rect(&output->region, output->x, output->y,
120 output->width, output->height);
121
122 weston_output_update_matrix(output);
123
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200124 /* If a pointer falls outside the outputs new geometry, move it to its
125 * lower-right corner */
126 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400127 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200128 int32_t x, y;
129
130 if (!pointer)
131 continue;
132
133 x = wl_fixed_to_int(pointer->x);
134 y = wl_fixed_to_int(pointer->y);
135
136 if (!pixman_region32_contains_point(&old_output_region,
137 x, y, NULL) ||
138 pixman_region32_contains_point(&output->region,
139 x, y, NULL))
140 continue;
141
142 if (x >= output->x + output->width)
143 x = output->x + output->width - 1;
144 if (y >= output->y + output->height)
145 y = output->y + output->height - 1;
146
147 pointer->x = wl_fixed_from_int(x);
148 pointer->y = wl_fixed_from_int(y);
149 }
150
151 pixman_region32_fini(&old_output_region);
152
Derek Foremandd4cd332014-11-10 10:29:59 -0600153 if (!mode_changed && !scale_changed)
154 return;
155
Hardening57388e42013-09-18 23:56:36 +0200156 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600157 wl_resource_for_each(resource, &output->resource_list) {
158 if (mode_changed) {
159 wl_output_send_mode(resource,
160 output->current_mode->flags,
161 output->current_mode->width,
162 output->current_mode->height,
163 output->current_mode->refresh);
164 }
Hardening57388e42013-09-18 23:56:36 +0200165
Derek Foreman41bdc272014-11-05 13:26:57 -0600166 version = wl_resource_get_version(resource);
167 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600168 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200169
Derek Foreman41bdc272014-11-05 13:26:57 -0600170 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
171 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600172 }
173}
174
175WL_EXPORT int
176weston_output_mode_set_native(struct weston_output *output,
177 struct weston_mode *mode,
178 int32_t scale)
179{
180 int ret;
181 int mode_changed = 0, scale_changed = 0;
182
183 if (!output->switch_mode)
184 return -1;
185
186 if (!output->original_mode) {
187 mode_changed = 1;
188 ret = output->switch_mode(output, mode);
189 if (ret < 0)
190 return ret;
191 if (output->current_scale != scale) {
192 scale_changed = 1;
193 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200194 }
195 }
196
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600197 output->native_mode = mode;
198 output->native_scale = scale;
199
200 weston_mode_switch_finish(output, mode_changed, scale_changed);
201
202 return 0;
203}
204
205WL_EXPORT int
206weston_output_mode_switch_to_native(struct weston_output *output)
207{
208 int ret;
209 int mode_changed = 0, scale_changed = 0;
210
211 if (!output->switch_mode)
212 return -1;
213
214 if (!output->original_mode) {
215 weston_log("already in the native mode\n");
216 return -1;
217 }
218 /* the non fullscreen clients haven't seen a mode set since we
219 * switched into a temporary, so we need to notify them if the
220 * mode at that time is different from the native mode now.
221 */
222 mode_changed = (output->original_mode != output->native_mode);
223 scale_changed = (output->original_scale != output->native_scale);
224
225 ret = output->switch_mode(output, output->native_mode);
226 if (ret < 0)
227 return ret;
228
229 output->current_scale = output->native_scale;
230
231 output->original_mode = NULL;
232 output->original_scale = 0;
233
234 weston_mode_switch_finish(output, mode_changed, scale_changed);
235
236 return 0;
237}
238
239WL_EXPORT int
240weston_output_mode_switch_to_temporary(struct weston_output *output,
241 struct weston_mode *mode,
242 int32_t scale)
243{
244 int ret;
245
246 if (!output->switch_mode)
247 return -1;
248
249 /* original_mode is the last mode non full screen clients have seen,
250 * so we shouldn't change it if we already have one set.
251 */
252 if (!output->original_mode) {
253 output->original_mode = output->native_mode;
254 output->original_scale = output->native_scale;
255 }
256 ret = output->switch_mode(output, mode);
257 if (ret < 0)
258 return ret;
259
260 output->current_scale = scale;
261
262 weston_mode_switch_finish(output, 0, 0);
263
264 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800265}
266
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400267WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500268weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400269{
270 wl_list_insert(&child_process_list, &process->link);
271}
272
Benjamin Franzke06286262011-05-06 19:12:33 +0200273static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200274child_client_exec(int sockfd, const char *path)
275{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500276 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200277 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200278 sigset_t allsigs;
279
280 /* do not give our signal mask to the new process */
281 sigfillset(&allsigs);
282 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200283
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100284 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
285 if (seteuid(getuid()) == -1) {
286 weston_log("compositor: failed seteuid\n");
287 return;
288 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500289
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500290 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
291 * non-CLOEXEC fd to pass through exec. */
292 clientfd = dup(sockfd);
293 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200294 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500295 return;
296 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200297
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500298 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200299 setenv("WAYLAND_SOCKET", s, 1);
300
301 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200302 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200303 path);
304}
305
306WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500307weston_client_launch(struct weston_compositor *compositor,
308 struct weston_process *proc,
309 const char *path,
310 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200311{
312 int sv[2];
313 pid_t pid;
314 struct wl_client *client;
315
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300316 weston_log("launching '%s'\n", path);
317
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300318 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200319 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200320 "socketpair failed while launching '%s': %m\n",
321 path);
322 return NULL;
323 }
324
325 pid = fork();
326 if (pid == -1) {
327 close(sv[0]);
328 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200329 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200330 "fork failed while launching '%s': %m\n", path);
331 return NULL;
332 }
333
334 if (pid == 0) {
335 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700336 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200337 }
338
339 close(sv[1]);
340
341 client = wl_client_create(compositor->wl_display, sv[0]);
342 if (!client) {
343 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200344 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200345 "wl_client_create failed while launching '%s'.\n",
346 path);
347 return NULL;
348 }
349
350 proc->pid = pid;
351 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500352 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200353
354 return client;
355}
356
Pekka Paalanen9c1ac7b2014-08-27 12:03:38 +0300357struct process_info {
358 struct weston_process proc;
359 char *path;
360};
361
362static void
363process_handle_sigchld(struct weston_process *process, int status)
364{
365 struct process_info *pinfo =
366 container_of(process, struct process_info, proc);
367
368 /*
369 * There are no guarantees whether this runs before or after
370 * the wl_client destructor.
371 */
372
373 if (WIFEXITED(status)) {
374 weston_log("%s exited with status %d\n", pinfo->path,
375 WEXITSTATUS(status));
376 } else if (WIFSIGNALED(status)) {
377 weston_log("%s died on signal %d\n", pinfo->path,
378 WTERMSIG(status));
379 } else {
380 weston_log("%s disappeared\n", pinfo->path);
381 }
382
383 free(pinfo->path);
384 free(pinfo);
385}
386
387WL_EXPORT struct wl_client *
388weston_client_start(struct weston_compositor *compositor, const char *path)
389{
390 struct process_info *pinfo;
391 struct wl_client *client;
392
393 pinfo = zalloc(sizeof *pinfo);
394 if (!pinfo)
395 return NULL;
396
397 pinfo->path = strdup(path);
398 if (!pinfo->path)
399 goto out_free;
400
401 client = weston_client_launch(compositor, &pinfo->proc, path,
402 process_handle_sigchld);
403 if (!client)
404 goto out_str;
405
406 return client;
407
408out_str:
409 free(pinfo->path);
410
411out_free:
412 free(pinfo);
413
414 return NULL;
415}
416
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200417static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300418region_init_infinite(pixman_region32_t *region)
419{
420 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
421 UINT32_MAX, UINT32_MAX);
422}
423
Pekka Paalanene67858b2013-04-25 13:57:42 +0300424static struct weston_subsurface *
425weston_surface_to_subsurface(struct weston_surface *surface);
426
Jason Ekstranda7af7042013-10-12 22:38:11 -0500427WL_EXPORT struct weston_view *
428weston_view_create(struct weston_surface *surface)
429{
430 struct weston_view *view;
431
Bryce Harringtonde16d892014-11-20 22:21:57 -0800432 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500433 if (view == NULL)
434 return NULL;
435
436 view->surface = surface;
437
Jason Ekstranda7af7042013-10-12 22:38:11 -0500438 /* Assign to surface */
439 wl_list_insert(&surface->views, &view->surface_link);
440
441 wl_signal_init(&view->destroy_signal);
442 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300443 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500444
Jason Ekstranda7af7042013-10-12 22:38:11 -0500445 pixman_region32_init(&view->clip);
446
447 view->alpha = 1.0;
448 pixman_region32_init(&view->transform.opaque);
449
450 wl_list_init(&view->geometry.transformation_list);
451 wl_list_insert(&view->geometry.transformation_list,
452 &view->transform.position.link);
453 weston_matrix_init(&view->transform.position.matrix);
454 wl_list_init(&view->geometry.child_list);
455 pixman_region32_init(&view->transform.boundingbox);
456 view->transform.dirty = 1;
457
Jason Ekstranda7af7042013-10-12 22:38:11 -0500458 return view;
459}
460
Jason Ekstrand108865d2014-06-26 10:04:49 -0700461struct weston_frame_callback {
462 struct wl_resource *resource;
463 struct wl_list link;
464};
465
Pekka Paalanen133e4392014-09-23 22:08:46 -0400466struct weston_presentation_feedback {
467 struct wl_resource *resource;
468
469 /* XXX: could use just wl_resource_get_link() instead */
470 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200471
472 /* The per-surface feedback flags */
473 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400474};
475
476static void
477weston_presentation_feedback_discard(
478 struct weston_presentation_feedback *feedback)
479{
480 presentation_feedback_send_discarded(feedback->resource);
481 wl_resource_destroy(feedback->resource);
482}
483
484static void
485weston_presentation_feedback_discard_list(struct wl_list *list)
486{
487 struct weston_presentation_feedback *feedback, *tmp;
488
489 wl_list_for_each_safe(feedback, tmp, list, link)
490 weston_presentation_feedback_discard(feedback);
491}
492
493static void
494weston_presentation_feedback_present(
495 struct weston_presentation_feedback *feedback,
496 struct weston_output *output,
497 uint32_t refresh_nsec,
498 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200499 uint64_t seq,
500 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400501{
502 struct wl_client *client = wl_resource_get_client(feedback->resource);
503 struct wl_resource *o;
504 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400505
506 wl_resource_for_each(o, &output->resource_list) {
507 if (wl_resource_get_client(o) != client)
508 continue;
509
510 presentation_feedback_send_sync_output(feedback->resource, o);
511 }
512
513 secs = ts->tv_sec;
514 presentation_feedback_send_presented(feedback->resource,
515 secs >> 32, secs & 0xffffffff,
516 ts->tv_nsec,
517 refresh_nsec,
518 seq >> 32, seq & 0xffffffff,
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200519 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400520 wl_resource_destroy(feedback->resource);
521}
522
523static void
524weston_presentation_feedback_present_list(struct wl_list *list,
525 struct weston_output *output,
526 uint32_t refresh_nsec,
527 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200528 uint64_t seq,
529 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400530{
531 struct weston_presentation_feedback *feedback, *tmp;
532
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200533 assert(!(flags & PRESENTATION_FEEDBACK_INVALID) ||
534 wl_list_empty(list));
535
Pekka Paalanen133e4392014-09-23 22:08:46 -0400536 wl_list_for_each_safe(feedback, tmp, list, link)
537 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200538 refresh_nsec, ts, seq,
539 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400540}
541
Jason Ekstrand7b982072014-05-20 14:33:03 -0500542static void
543surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
544{
545 struct weston_surface_state *state =
546 container_of(listener, struct weston_surface_state,
547 buffer_destroy_listener);
548
549 state->buffer = NULL;
550}
551
552static void
553weston_surface_state_init(struct weston_surface_state *state)
554{
555 state->newly_attached = 0;
556 state->buffer = NULL;
557 state->buffer_destroy_listener.notify =
558 surface_state_handle_buffer_destroy;
559 state->sx = 0;
560 state->sy = 0;
561
562 pixman_region32_init(&state->damage);
563 pixman_region32_init(&state->opaque);
564 region_init_infinite(&state->input);
565
566 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400567 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500568
569 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
570 state->buffer_viewport.buffer.scale = 1;
571 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
572 state->buffer_viewport.surface.width = -1;
573 state->buffer_viewport.changed = 0;
574}
575
576static void
577weston_surface_state_fini(struct weston_surface_state *state)
578{
579 struct weston_frame_callback *cb, *next;
580
581 wl_list_for_each_safe(cb, next,
582 &state->frame_callback_list, link)
583 wl_resource_destroy(cb->resource);
584
Pekka Paalanen133e4392014-09-23 22:08:46 -0400585 weston_presentation_feedback_discard_list(&state->feedback_list);
586
Jason Ekstrand7b982072014-05-20 14:33:03 -0500587 pixman_region32_fini(&state->input);
588 pixman_region32_fini(&state->opaque);
589 pixman_region32_fini(&state->damage);
590
591 if (state->buffer)
592 wl_list_remove(&state->buffer_destroy_listener.link);
593 state->buffer = NULL;
594}
595
596static void
597weston_surface_state_set_buffer(struct weston_surface_state *state,
598 struct weston_buffer *buffer)
599{
600 if (state->buffer == buffer)
601 return;
602
603 if (state->buffer)
604 wl_list_remove(&state->buffer_destroy_listener.link);
605 state->buffer = buffer;
606 if (state->buffer)
607 wl_signal_add(&state->buffer->destroy_signal,
608 &state->buffer_destroy_listener);
609}
610
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500611WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500612weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500613{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500614 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400615
Bryce Harringtonde16d892014-11-20 22:21:57 -0800616 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400617 if (surface == NULL)
618 return NULL;
619
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500620 wl_signal_init(&surface->destroy_signal);
621
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500622 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200623 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400624
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200625 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
626 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200627 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
628 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500629
630 weston_surface_state_init(&surface->pending);
631
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400632 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500633 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300634 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400635
Jason Ekstranda7af7042013-10-12 22:38:11 -0500636 wl_list_init(&surface->views);
637
638 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400639 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500640
Pekka Paalanene67858b2013-04-25 13:57:42 +0300641 wl_list_init(&surface->subsurface_list);
642 wl_list_init(&surface->subsurface_list_pending);
643
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400644 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500645}
646
Alex Wu8811bf92012-02-28 18:07:54 +0800647WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500648weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200649 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500650{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100651 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500652}
653
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400654WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500655weston_view_to_global_float(struct weston_view *view,
656 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200657{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500658 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200659 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
660
Jason Ekstranda7af7042013-10-12 22:38:11 -0500661 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200662
663 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200664 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700665 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200666 v.f[3]);
667 *x = 0;
668 *y = 0;
669 return;
670 }
671
672 *x = v.f[0] / v.f[3];
673 *y = v.f[1] / v.f[3];
674 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500675 *x = sx + view->geometry.x;
676 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200677 }
678}
679
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500680WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200681weston_transformed_coord(int width, int height,
682 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200683 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200684 float sx, float sy, float *bx, float *by)
685{
686 switch (transform) {
687 case WL_OUTPUT_TRANSFORM_NORMAL:
688 default:
689 *bx = sx;
690 *by = sy;
691 break;
692 case WL_OUTPUT_TRANSFORM_FLIPPED:
693 *bx = width - sx;
694 *by = sy;
695 break;
696 case WL_OUTPUT_TRANSFORM_90:
697 *bx = height - sy;
698 *by = sx;
699 break;
700 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
701 *bx = height - sy;
702 *by = width - sx;
703 break;
704 case WL_OUTPUT_TRANSFORM_180:
705 *bx = width - sx;
706 *by = height - sy;
707 break;
708 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
709 *bx = sx;
710 *by = height - sy;
711 break;
712 case WL_OUTPUT_TRANSFORM_270:
713 *bx = sy;
714 *by = width - sx;
715 break;
716 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
717 *bx = sy;
718 *by = sx;
719 break;
720 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200721
722 *bx *= scale;
723 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200724}
725
726WL_EXPORT pixman_box32_t
727weston_transformed_rect(int width, int height,
728 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200729 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200730 pixman_box32_t rect)
731{
732 float x1, x2, y1, y2;
733
734 pixman_box32_t ret;
735
Alexander Larsson4ea95522013-05-22 14:41:37 +0200736 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200737 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200738 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200739 rect.x2, rect.y2, &x2, &y2);
740
741 if (x1 <= x2) {
742 ret.x1 = x1;
743 ret.x2 = x2;
744 } else {
745 ret.x1 = x2;
746 ret.x2 = x1;
747 }
748
749 if (y1 <= y2) {
750 ret.y1 = y1;
751 ret.y2 = y2;
752 } else {
753 ret.y1 = y2;
754 ret.y2 = y1;
755 }
756
757 return ret;
758}
759
760WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500761weston_transformed_region(int width, int height,
762 enum wl_output_transform transform,
763 int32_t scale,
764 pixman_region32_t *src, pixman_region32_t *dest)
765{
766 pixman_box32_t *src_rects, *dest_rects;
767 int nrects, i;
768
769 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
770 if (src != dest)
771 pixman_region32_copy(dest, src);
772 return;
773 }
774
775 src_rects = pixman_region32_rectangles(src, &nrects);
776 dest_rects = malloc(nrects * sizeof(*dest_rects));
777 if (!dest_rects)
778 return;
779
780 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
781 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
782 } else {
783 for (i = 0; i < nrects; i++) {
784 switch (transform) {
785 default:
786 case WL_OUTPUT_TRANSFORM_NORMAL:
787 dest_rects[i].x1 = src_rects[i].x1;
788 dest_rects[i].y1 = src_rects[i].y1;
789 dest_rects[i].x2 = src_rects[i].x2;
790 dest_rects[i].y2 = src_rects[i].y2;
791 break;
792 case WL_OUTPUT_TRANSFORM_90:
793 dest_rects[i].x1 = height - src_rects[i].y2;
794 dest_rects[i].y1 = src_rects[i].x1;
795 dest_rects[i].x2 = height - src_rects[i].y1;
796 dest_rects[i].y2 = src_rects[i].x2;
797 break;
798 case WL_OUTPUT_TRANSFORM_180:
799 dest_rects[i].x1 = width - src_rects[i].x2;
800 dest_rects[i].y1 = height - src_rects[i].y2;
801 dest_rects[i].x2 = width - src_rects[i].x1;
802 dest_rects[i].y2 = height - src_rects[i].y1;
803 break;
804 case WL_OUTPUT_TRANSFORM_270:
805 dest_rects[i].x1 = src_rects[i].y1;
806 dest_rects[i].y1 = width - src_rects[i].x2;
807 dest_rects[i].x2 = src_rects[i].y2;
808 dest_rects[i].y2 = width - src_rects[i].x1;
809 break;
810 case WL_OUTPUT_TRANSFORM_FLIPPED:
811 dest_rects[i].x1 = width - src_rects[i].x2;
812 dest_rects[i].y1 = src_rects[i].y1;
813 dest_rects[i].x2 = width - src_rects[i].x1;
814 dest_rects[i].y2 = src_rects[i].y2;
815 break;
816 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
817 dest_rects[i].x1 = height - src_rects[i].y2;
818 dest_rects[i].y1 = width - src_rects[i].x2;
819 dest_rects[i].x2 = height - src_rects[i].y1;
820 dest_rects[i].y2 = width - src_rects[i].x1;
821 break;
822 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
823 dest_rects[i].x1 = src_rects[i].x1;
824 dest_rects[i].y1 = height - src_rects[i].y2;
825 dest_rects[i].x2 = src_rects[i].x2;
826 dest_rects[i].y2 = height - src_rects[i].y1;
827 break;
828 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
829 dest_rects[i].x1 = src_rects[i].y1;
830 dest_rects[i].y1 = src_rects[i].x1;
831 dest_rects[i].x2 = src_rects[i].y2;
832 dest_rects[i].y2 = src_rects[i].x2;
833 break;
834 }
835 }
836 }
837
838 if (scale != 1) {
839 for (i = 0; i < nrects; i++) {
840 dest_rects[i].x1 *= scale;
841 dest_rects[i].x2 *= scale;
842 dest_rects[i].y1 *= scale;
843 dest_rects[i].y2 *= scale;
844 }
845 }
846
847 pixman_region32_clear(dest);
848 pixman_region32_init_rects(dest, dest_rects, nrects);
849 free(dest_rects);
850}
851
Jonny Lamb74130762013-11-26 18:19:46 +0100852static void
853scaler_surface_to_buffer(struct weston_surface *surface,
854 float sx, float sy, float *bx, float *by)
855{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200856 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200857 double src_width, src_height;
858 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200859
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200860 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
861 if (vp->surface.width == -1) {
862 *bx = sx;
863 *by = sy;
864 return;
865 }
Jonny Lamb74130762013-11-26 18:19:46 +0100866
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200867 src_x = 0.0;
868 src_y = 0.0;
869 src_width = surface->width_from_buffer;
870 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100871 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200872 src_x = wl_fixed_to_double(vp->buffer.src_x);
873 src_y = wl_fixed_to_double(vp->buffer.src_y);
874 src_width = wl_fixed_to_double(vp->buffer.src_width);
875 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100876 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200877
878 *bx = sx * src_width / surface->width + src_x;
879 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100880}
881
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500882WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200883weston_surface_to_buffer_float(struct weston_surface *surface,
884 float sx, float sy, float *bx, float *by)
885{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200886 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
887
Jonny Lamb74130762013-11-26 18:19:46 +0100888 /* first transform coordinates if the scaler is set */
889 scaler_surface_to_buffer(surface, sx, sy, bx, by);
890
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500891 weston_transformed_coord(surface->width_from_buffer,
892 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200893 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100894 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200895}
896
Alexander Larsson4ea95522013-05-22 14:41:37 +0200897WL_EXPORT void
898weston_surface_to_buffer(struct weston_surface *surface,
899 int sx, int sy, int *bx, int *by)
900{
901 float bxf, byf;
902
Jonny Lamb74130762013-11-26 18:19:46 +0100903 weston_surface_to_buffer_float(surface,
904 sx, sy, &bxf, &byf);
905
Alexander Larsson4ea95522013-05-22 14:41:37 +0200906 *bx = floorf(bxf);
907 *by = floorf(byf);
908}
909
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200910WL_EXPORT pixman_box32_t
911weston_surface_to_buffer_rect(struct weston_surface *surface,
912 pixman_box32_t rect)
913{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200914 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100915 float xf, yf;
916
917 /* first transform box coordinates if the scaler is set */
918 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
919 rect.x1 = floorf(xf);
920 rect.y1 = floorf(yf);
921
922 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
923 rect.x2 = floorf(xf);
924 rect.y2 = floorf(yf);
925
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500926 return weston_transformed_rect(surface->width_from_buffer,
927 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200928 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200929 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200930}
931
932WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500933weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400934 struct weston_plane *plane)
935{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500936 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400937 return;
938
Jason Ekstranda7af7042013-10-12 22:38:11 -0500939 weston_view_damage_below(view);
940 view->plane = plane;
941 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400942}
943
Pekka Paalanen51723d52015-02-17 13:10:01 +0200944/** Inflict damage on the plane where the view is visible.
945 *
946 * \param view The view that causes the damage.
947 *
948 * If the view is currently on a plane (including the primary plane),
949 * take the view's boundingbox, subtract all the opaque views that cover it,
950 * and add the remaining region as damage to the plane. This corresponds
951 * to the damage inflicted to the plane if this view disappeared.
952 *
953 * A repaint is scheduled for this view.
954 *
955 * The region of all opaque views covering this view is stored in
956 * weston_view::clip and updated by view_accumulate_damage() during
957 * weston_output_repaint(). Specifically, that region matches the
958 * scenegraph as it was last painted.
959 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400960WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500961weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200962{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500963 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200964
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500965 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200966 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500967 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800968 if (view->plane)
969 pixman_region32_union(&view->plane->damage,
970 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500971 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800972 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200973}
974
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400975static void
976weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
977{
978 uint32_t different = es->output_mask ^ mask;
979 uint32_t entered = mask & different;
980 uint32_t left = es->output_mask & different;
981 struct weston_output *output;
982 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500983 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400984
985 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500986 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400987 return;
988 if (different == 0)
989 return;
990
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500991 client = wl_resource_get_client(es->resource);
992
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400993 wl_list_for_each(output, &es->compositor->output_list, link) {
994 if (1 << output->id & different)
995 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500996 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400997 client);
998 if (resource == NULL)
999 continue;
1000 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001001 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001002 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001003 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001004 }
1005}
1006
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001007
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001008static void
1009weston_surface_assign_output(struct weston_surface *es)
1010{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001011 struct weston_output *new_output;
1012 struct weston_view *view;
1013 pixman_region32_t region;
1014 uint32_t max, area, mask;
1015 pixman_box32_t *e;
1016
1017 new_output = NULL;
1018 max = 0;
1019 mask = 0;
1020 pixman_region32_init(&region);
1021 wl_list_for_each(view, &es->views, surface_link) {
1022 if (!view->output)
1023 continue;
1024
1025 pixman_region32_intersect(&region, &view->transform.boundingbox,
1026 &view->output->region);
1027
1028 e = pixman_region32_extents(&region);
1029 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1030
1031 mask |= view->output_mask;
1032
1033 if (area >= max) {
1034 new_output = view->output;
1035 max = area;
1036 }
1037 }
1038 pixman_region32_fini(&region);
1039
1040 es->output = new_output;
1041 weston_surface_update_output_mask(es, mask);
1042}
1043
1044static void
1045weston_view_assign_output(struct weston_view *ev)
1046{
1047 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001048 struct weston_output *output, *new_output;
1049 pixman_region32_t region;
1050 uint32_t max, area, mask;
1051 pixman_box32_t *e;
1052
1053 new_output = NULL;
1054 max = 0;
1055 mask = 0;
1056 pixman_region32_init(&region);
1057 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001058 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001059 &output->region);
1060
1061 e = pixman_region32_extents(&region);
1062 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1063
1064 if (area > 0)
1065 mask |= 1 << output->id;
1066
1067 if (area >= max) {
1068 new_output = output;
1069 max = area;
1070 }
1071 }
1072 pixman_region32_fini(&region);
1073
Jason Ekstranda7af7042013-10-12 22:38:11 -05001074 ev->output = new_output;
1075 ev->output_mask = mask;
1076
1077 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001078}
1079
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001080static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001081view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001082 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001083{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001084 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1085 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001086 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001087 { inbox->x1, inbox->y1 },
1088 { inbox->x1, inbox->y2 },
1089 { inbox->x2, inbox->y1 },
1090 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001091 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001092 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001093 int i;
1094
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001095 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001096 /* avoid rounding empty bbox to 1x1 */
1097 pixman_region32_init(bbox);
1098 return;
1099 }
1100
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001101 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001102 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001103 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001104 if (x < min_x)
1105 min_x = x;
1106 if (x > max_x)
1107 max_x = x;
1108 if (y < min_y)
1109 min_y = y;
1110 if (y > max_y)
1111 max_y = y;
1112 }
1113
Pekka Paalanen219b9822012-02-08 15:38:37 +02001114 int_x = floorf(min_x);
1115 int_y = floorf(min_y);
1116 pixman_region32_init_rect(bbox, int_x, int_y,
1117 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001118}
1119
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001120static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001121weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001122{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001123 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001124
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001125 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001126 view->geometry.x = roundf(view->geometry.x);
1127 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001128
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001129 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001130 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1131 view->transform.position.matrix.d[12] = view->geometry.x;
1132 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001133
Jason Ekstranda7af7042013-10-12 22:38:11 -05001134 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001135
Jason Ekstranda7af7042013-10-12 22:38:11 -05001136 view->transform.inverse = view->transform.position.matrix;
1137 view->transform.inverse.d[12] = -view->geometry.x;
1138 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001139
Jason Ekstranda7af7042013-10-12 22:38:11 -05001140 pixman_region32_init_rect(&view->transform.boundingbox,
1141 view->geometry.x,
1142 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001143 view->surface->width,
1144 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001145
Jason Ekstranda7af7042013-10-12 22:38:11 -05001146 if (view->alpha == 1.0) {
1147 pixman_region32_copy(&view->transform.opaque,
1148 &view->surface->opaque);
1149 pixman_region32_translate(&view->transform.opaque,
1150 view->geometry.x,
1151 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001152 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001153}
1154
1155static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001156weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001157{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001158 struct weston_view *parent = view->geometry.parent;
1159 struct weston_matrix *matrix = &view->transform.matrix;
1160 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001161 struct weston_transform *tform;
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001162 pixman_box32_t surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001163
Jason Ekstranda7af7042013-10-12 22:38:11 -05001164 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001165
1166 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001167 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1168 view->transform.position.matrix.d[12] = view->geometry.x;
1169 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001170
1171 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001172 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001173 weston_matrix_multiply(matrix, &tform->matrix);
1174
Pekka Paalanen483243f2013-03-08 14:56:50 +02001175 if (parent)
1176 weston_matrix_multiply(matrix, &parent->transform.matrix);
1177
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001178 if (weston_matrix_invert(inverse, matrix) < 0) {
1179 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001180 weston_log("error: weston_view %p"
1181 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001182 return -1;
1183 }
1184
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001185 surfbox.x1 = 0;
1186 surfbox.y1 = 0;
1187 surfbox.x2 = view->surface->width;
1188 surfbox.y2 = view->surface->height;
1189 view_compute_bbox(view, &surfbox, &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001190
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001191 return 0;
1192}
1193
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001194static struct weston_layer *
1195get_view_layer(struct weston_view *view)
1196{
1197 if (view->parent_view)
1198 return get_view_layer(view->parent_view);
1199 return view->layer_link.layer;
1200}
1201
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001202WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001204{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001205 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001206 struct weston_layer *layer;
1207 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001208
Jason Ekstranda7af7042013-10-12 22:38:11 -05001209 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001210 return;
1211
Pekka Paalanen483243f2013-03-08 14:56:50 +02001212 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001213 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001214
Jason Ekstranda7af7042013-10-12 22:38:11 -05001215 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001216
Jason Ekstranda7af7042013-10-12 22:38:11 -05001217 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001218
Jason Ekstranda7af7042013-10-12 22:38:11 -05001219 pixman_region32_fini(&view->transform.boundingbox);
1220 pixman_region32_fini(&view->transform.opaque);
1221 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001222
Pekka Paalanencd403622012-01-25 13:37:39 +02001223 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001224 if (view->geometry.transformation_list.next ==
1225 &view->transform.position.link &&
1226 view->geometry.transformation_list.prev ==
1227 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001228 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001229 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001230 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001231 if (weston_view_update_transform_enable(view) < 0)
1232 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001233 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001234
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001235 layer = get_view_layer(view);
1236 if (layer) {
1237 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001238 pixman_region32_intersect(&view->transform.boundingbox,
1239 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001240 pixman_region32_intersect(&view->transform.opaque,
1241 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001242 pixman_region32_fini(&mask);
1243 }
1244
Jason Ekstranda7af7042013-10-12 22:38:11 -05001245 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001246
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001248
Jason Ekstranda7af7042013-10-12 22:38:11 -05001249 wl_signal_emit(&view->surface->compositor->transform_signal,
1250 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001251}
1252
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001253WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001254weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001255{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001257
1258 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001259 * The invariant: if view->geometry.dirty, then all views
1260 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001261 * Corollary: if not parent->geometry.dirty, then all ancestors
1262 * are not dirty.
1263 */
1264
Jason Ekstranda7af7042013-10-12 22:38:11 -05001265 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001266 return;
1267
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001269
Jason Ekstranda7af7042013-10-12 22:38:11 -05001270 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001271 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001273}
1274
1275WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276weston_view_to_global_fixed(struct weston_view *view,
1277 wl_fixed_t vx, wl_fixed_t vy,
1278 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001279{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001280 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001281
Jason Ekstranda7af7042013-10-12 22:38:11 -05001282 weston_view_to_global_float(view,
1283 wl_fixed_to_double(vx),
1284 wl_fixed_to_double(vy),
1285 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001286 *x = wl_fixed_from_double(xf);
1287 *y = wl_fixed_from_double(yf);
1288}
1289
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001290WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001291weston_view_from_global_float(struct weston_view *view,
1292 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001293{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001294 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001295 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1296
Jason Ekstranda7af7042013-10-12 22:38:11 -05001297 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001298
1299 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001300 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001302 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303 *vx = 0;
1304 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001305 return;
1306 }
1307
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308 *vx = v.f[0] / v.f[3];
1309 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001310 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311 *vx = x - view->geometry.x;
1312 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001313 }
1314}
1315
1316WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001317weston_view_from_global_fixed(struct weston_view *view,
1318 wl_fixed_t x, wl_fixed_t y,
1319 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001320{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001322
Jason Ekstranda7af7042013-10-12 22:38:11 -05001323 weston_view_from_global_float(view,
1324 wl_fixed_to_double(x),
1325 wl_fixed_to_double(y),
1326 &vxf, &vyf);
1327 *vx = wl_fixed_from_double(vxf);
1328 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001329}
1330
1331WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001332weston_view_from_global(struct weston_view *view,
1333 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001334{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001336
Jason Ekstranda7af7042013-10-12 22:38:11 -05001337 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1338 *vx = floorf(vxf);
1339 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001340}
1341
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001342WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001343weston_surface_schedule_repaint(struct weston_surface *surface)
1344{
1345 struct weston_output *output;
1346
1347 wl_list_for_each(output, &surface->compositor->output_list, link)
1348 if (surface->output_mask & (1 << output->id))
1349 weston_output_schedule_repaint(output);
1350}
1351
1352WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001353weston_view_schedule_repaint(struct weston_view *view)
1354{
1355 struct weston_output *output;
1356
1357 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1358 if (view->output_mask & (1 << output->id))
1359 weston_output_schedule_repaint(output);
1360}
1361
Pekka Paalanene508ce62015-02-19 13:59:55 +02001362/**
1363 * XXX: This function does it the wrong way.
1364 * surface->damage is the damage from the client, and causes
1365 * surface_flush_damage() to copy pixels. No window management action can
1366 * cause damage to the client-provided content, warranting re-upload!
1367 *
1368 * Instead of surface->damage, this function should record the damage
1369 * with all the views for this surface to avoid extraneous texture
1370 * uploads.
1371 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001372WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001373weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001374{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001375 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001376 0, 0, surface->width,
1377 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001378
Kristian Høgsberg98238702012-08-03 16:29:12 -04001379 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001380}
1381
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001382WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001383weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001384{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001385 if (view->geometry.x == x && view->geometry.y == y)
1386 return;
1387
Jason Ekstranda7af7042013-10-12 22:38:11 -05001388 view->geometry.x = x;
1389 view->geometry.y = y;
1390 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001391}
1392
Pekka Paalanen483243f2013-03-08 14:56:50 +02001393static void
1394transform_parent_handle_parent_destroy(struct wl_listener *listener,
1395 void *data)
1396{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001397 struct weston_view *view =
1398 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001399 geometry.parent_destroy_listener);
1400
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001402}
1403
1404WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001405weston_view_set_transform_parent(struct weston_view *view,
1406 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001407{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001408 if (view->geometry.parent) {
1409 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1410 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001411 }
1412
Jason Ekstranda7af7042013-10-12 22:38:11 -05001413 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001414
Jason Ekstranda7af7042013-10-12 22:38:11 -05001415 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001416 transform_parent_handle_parent_destroy;
1417 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001418 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001419 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001420 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001421 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001422 }
1423
Jason Ekstranda7af7042013-10-12 22:38:11 -05001424 weston_view_geometry_dirty(view);
1425}
1426
Derek Foreman280e7dd2014-10-03 13:13:42 -05001427WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428weston_view_is_mapped(struct weston_view *view)
1429{
1430 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001431 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001433 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001434}
1435
Derek Foreman280e7dd2014-10-03 13:13:42 -05001436WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001437weston_surface_is_mapped(struct weston_surface *surface)
1438{
1439 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001440 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001441 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001442 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001443}
1444
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001445static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001446surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001447{
1448 struct weston_view *view;
1449
1450 if (surface->width == width && surface->height == height)
1451 return;
1452
1453 surface->width = width;
1454 surface->height = height;
1455
1456 wl_list_for_each(view, &surface->views, surface_link)
1457 weston_view_geometry_dirty(view);
1458}
1459
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001460WL_EXPORT void
1461weston_surface_set_size(struct weston_surface *surface,
1462 int32_t width, int32_t height)
1463{
1464 assert(!surface->resource);
1465 surface_set_size(surface, width, height);
1466}
1467
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001468static int
1469fixed_round_up_to_int(wl_fixed_t f)
1470{
1471 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1472}
1473
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001474static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001475weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001476{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001477 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001478 int32_t width, height;
1479
1480 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001481 surface->width_from_buffer = 0;
1482 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001483 return;
1484 }
1485
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001486 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001487 case WL_OUTPUT_TRANSFORM_90:
1488 case WL_OUTPUT_TRANSFORM_270:
1489 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1490 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001491 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1492 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001493 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001494 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001495 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1496 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001497 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001498 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001499
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001500 surface->width_from_buffer = width;
1501 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001502}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001503
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001504static void
1505weston_surface_update_size(struct weston_surface *surface)
1506{
1507 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1508 int32_t width, height;
1509
1510 width = surface->width_from_buffer;
1511 height = surface->height_from_buffer;
1512
1513 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001514 surface_set_size(surface,
1515 vp->surface.width, vp->surface.height);
1516 return;
1517 }
1518
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001519 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001520 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1521 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1522
1523 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001524 return;
1525 }
1526
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001527 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001528}
1529
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001530WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001531weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001532{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001533 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001534
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001535 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001536
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001537 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001538}
1539
Jason Ekstranda7af7042013-10-12 22:38:11 -05001540WL_EXPORT struct weston_view *
1541weston_compositor_pick_view(struct weston_compositor *compositor,
1542 wl_fixed_t x, wl_fixed_t y,
1543 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001544{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001545 struct weston_view *view;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001546 int ix = wl_fixed_to_int(x);
1547 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001548
Jason Ekstranda7af7042013-10-12 22:38:11 -05001549 wl_list_for_each(view, &compositor->view_list, link) {
1550 weston_view_from_global_fixed(view, x, y, vx, vy);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001551 if (pixman_region32_contains_point(
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001552 &view->transform.boundingbox, ix, iy, NULL) &&
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001553 pixman_region32_contains_point(&view->surface->input,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001554 wl_fixed_to_int(*vx),
1555 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001556 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001557 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001558 }
1559
1560 return NULL;
1561}
1562
1563static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001564weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001565{
Daniel Stone37816df2012-05-16 18:45:18 +01001566 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001567
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001568 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001569 return;
1570
Daniel Stone37816df2012-05-16 18:45:18 +01001571 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001572 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001573}
1574
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001575WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001576weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001577{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001578 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001579
Jason Ekstranda7af7042013-10-12 22:38:11 -05001580 if (!weston_view_is_mapped(view))
1581 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001582
Jason Ekstranda7af7042013-10-12 22:38:11 -05001583 weston_view_damage_below(view);
1584 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001585 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001586 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001587 wl_list_remove(&view->link);
1588 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001589 view->output_mask = 0;
1590 weston_surface_assign_output(view->surface);
1591
1592 if (weston_surface_is_mapped(view->surface))
1593 return;
1594
1595 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1596 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001597 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001598 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001599 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001600 NULL,
1601 wl_fixed_from_int(0),
1602 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001603 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001604 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001605 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001606}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001607
Jason Ekstranda7af7042013-10-12 22:38:11 -05001608WL_EXPORT void
1609weston_surface_unmap(struct weston_surface *surface)
1610{
1611 struct weston_view *view;
1612
1613 wl_list_for_each(view, &surface->views, surface_link)
1614 weston_view_unmap(view);
1615 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001616}
1617
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001618static void
1619weston_surface_reset_pending_buffer(struct weston_surface *surface)
1620{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001621 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001622 surface->pending.sx = 0;
1623 surface->pending.sy = 0;
1624 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001625 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001626}
1627
Jason Ekstranda7af7042013-10-12 22:38:11 -05001628WL_EXPORT void
1629weston_view_destroy(struct weston_view *view)
1630{
1631 wl_signal_emit(&view->destroy_signal, view);
1632
1633 assert(wl_list_empty(&view->geometry.child_list));
1634
1635 if (weston_view_is_mapped(view)) {
1636 weston_view_unmap(view);
1637 weston_compositor_build_view_list(view->surface->compositor);
1638 }
1639
1640 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001641 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001642
1643 pixman_region32_fini(&view->clip);
1644 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001645 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001646
1647 weston_view_set_transform_parent(view, NULL);
1648
Jason Ekstranda7af7042013-10-12 22:38:11 -05001649 wl_list_remove(&view->surface_link);
1650
1651 free(view);
1652}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001653
1654WL_EXPORT void
1655weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001656{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001657 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001658 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001659
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001660 if (--surface->ref_count > 0)
1661 return;
1662
1663 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1664
Pekka Paalanene67858b2013-04-25 13:57:42 +03001665 assert(wl_list_empty(&surface->subsurface_list_pending));
1666 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001667
Jason Ekstranda7af7042013-10-12 22:38:11 -05001668 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1669 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001670
Jason Ekstrand7b982072014-05-20 14:33:03 -05001671 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001672
Pekka Paalanende685b82012-12-04 15:58:12 +02001673 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001674
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001675 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001676 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001677 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001678
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001679 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001680 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001681
Pekka Paalanen133e4392014-09-23 22:08:46 -04001682 weston_presentation_feedback_discard_list(&surface->feedback_list);
1683
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001684 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001685}
1686
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001687static void
1688destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001689{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001690 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001691
Giulio Camuffo0d379742013-11-15 22:06:15 +01001692 /* Set the resource to NULL, since we don't want to leave a
1693 * dangling pointer if the surface was refcounted and survives
1694 * the weston_surface_destroy() call. */
1695 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001696 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001697}
1698
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001699static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001700weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1701{
1702 struct weston_buffer *buffer =
1703 container_of(listener, struct weston_buffer, destroy_listener);
1704
1705 wl_signal_emit(&buffer->destroy_signal, buffer);
1706 free(buffer);
1707}
1708
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001709WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001710weston_buffer_from_resource(struct wl_resource *resource)
1711{
1712 struct weston_buffer *buffer;
1713 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001714
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001715 listener = wl_resource_get_destroy_listener(resource,
1716 weston_buffer_destroy_handler);
1717
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001718 if (listener)
1719 return container_of(listener, struct weston_buffer,
1720 destroy_listener);
1721
1722 buffer = zalloc(sizeof *buffer);
1723 if (buffer == NULL)
1724 return NULL;
1725
1726 buffer->resource = resource;
1727 wl_signal_init(&buffer->destroy_signal);
1728 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001729 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001730 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001731
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001732 return buffer;
1733}
1734
1735static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001736weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1737 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001738{
Pekka Paalanende685b82012-12-04 15:58:12 +02001739 struct weston_buffer_reference *ref =
1740 container_of(listener, struct weston_buffer_reference,
1741 destroy_listener);
1742
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001743 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001744 ref->buffer = NULL;
1745}
1746
1747WL_EXPORT void
1748weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001749 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001750{
1751 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001752 ref->buffer->busy_count--;
1753 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001754 assert(wl_resource_get_client(ref->buffer->resource));
1755 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001756 WL_BUFFER_RELEASE);
1757 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001758 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001759 }
1760
Pekka Paalanende685b82012-12-04 15:58:12 +02001761 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001762 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001763 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001764 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001765 }
1766
Pekka Paalanende685b82012-12-04 15:58:12 +02001767 ref->buffer = buffer;
1768 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1769}
1770
1771static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001772weston_surface_attach(struct weston_surface *surface,
1773 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001774{
1775 weston_buffer_reference(&surface->buffer_ref, buffer);
1776
Pekka Paalanena6421c42012-12-04 15:58:10 +02001777 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001778 if (weston_surface_is_mapped(surface))
1779 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001780 }
1781
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001782 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001783
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001784 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001785 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001786}
1787
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001788WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001789weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001790{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001791 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001792
1793 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001794 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001795}
1796
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001797WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001798weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001799{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001800 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001801
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001802 pixman_region32_union(&compositor->primary_plane.damage,
1803 &compositor->primary_plane.damage,
1804 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001805 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001806}
1807
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001808static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001809surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001810{
Pekka Paalanende685b82012-12-04 15:58:12 +02001811 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001812 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001813 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001814
Pekka Paalanenb5026542014-11-12 15:09:24 +02001815 if (weston_timeline_enabled_ &&
1816 pixman_region32_not_empty(&surface->damage))
1817 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
1818 TLP_OUTPUT(surface->output), TLP_END);
1819
Jason Ekstrandef540082014-06-26 10:37:36 -07001820 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821}
1822
1823static void
1824view_accumulate_damage(struct weston_view *view,
1825 pixman_region32_t *opaque)
1826{
1827 pixman_region32_t damage;
1828
1829 pixman_region32_init(&damage);
1830 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001831 pixman_box32_t *extents;
1832
Jason Ekstranda7af7042013-10-12 22:38:11 -05001833 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001834 view_compute_bbox(view, extents, &damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001835 pixman_region32_translate(&damage,
1836 -view->plane->x,
1837 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001838 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001839 pixman_region32_copy(&damage, &view->surface->damage);
1840 pixman_region32_translate(&damage,
1841 view->geometry.x - view->plane->x,
1842 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001843 }
1844
Jason Ekstranda7af7042013-10-12 22:38:11 -05001845 pixman_region32_subtract(&damage, &damage, opaque);
1846 pixman_region32_union(&view->plane->damage,
1847 &view->plane->damage, &damage);
1848 pixman_region32_fini(&damage);
1849 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001850 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001851}
1852
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001853static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001854compositor_accumulate_damage(struct weston_compositor *ec)
1855{
1856 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001857 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001858 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001859
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001860 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001861
1862 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001863 pixman_region32_copy(&plane->clip, &clip);
1864
1865 pixman_region32_init(&opaque);
1866
Jason Ekstranda7af7042013-10-12 22:38:11 -05001867 wl_list_for_each(ev, &ec->view_list, link) {
1868 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001869 continue;
1870
Jason Ekstranda7af7042013-10-12 22:38:11 -05001871 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001872 }
1873
1874 pixman_region32_union(&clip, &clip, &opaque);
1875 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001876 }
1877
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001878 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001879
Jason Ekstranda7af7042013-10-12 22:38:11 -05001880 wl_list_for_each(ev, &ec->view_list, link)
1881 ev->surface->touched = 0;
1882
1883 wl_list_for_each(ev, &ec->view_list, link) {
1884 if (ev->surface->touched)
1885 continue;
1886 ev->surface->touched = 1;
1887
1888 surface_flush_damage(ev->surface);
1889
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001890 /* Both the renderer and the backend have seen the buffer
1891 * by now. If renderer needs the buffer, it has its own
1892 * reference set. If the backend wants to keep the buffer
1893 * around for migrating the surface into a non-primary plane
1894 * later, keep_buffer is true. Otherwise, drop the core
1895 * reference now, and allow early buffer release. This enables
1896 * clients to use single-buffering.
1897 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001898 if (!ev->surface->keep_buffer)
1899 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001900 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001901}
1902
1903static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001904surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001905{
1906 struct weston_subsurface *sub;
1907
Pekka Paalanene67858b2013-04-25 13:57:42 +03001908 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001909 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001910 continue;
1911
Jason Ekstranda7af7042013-10-12 22:38:11 -05001912 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1913 wl_list_init(&sub->surface->views);
1914
1915 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001916 }
1917}
1918
1919static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001920surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001921{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001922 struct weston_subsurface *sub;
1923 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001924
Jason Ekstranda7af7042013-10-12 22:38:11 -05001925 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1926 if (sub->surface == surface)
1927 continue;
1928
George Kiagiadakised04d382014-06-13 18:10:26 +02001929 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
1930 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001931 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02001932 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001933
1934 surface_free_unused_subsurface_views(sub->surface);
1935 }
1936}
1937
1938static void
1939view_list_add_subsurface_view(struct weston_compositor *compositor,
1940 struct weston_subsurface *sub,
1941 struct weston_view *parent)
1942{
1943 struct weston_subsurface *child;
1944 struct weston_view *view = NULL, *iv;
1945
Pekka Paalanen661de3a2014-07-28 12:49:24 +03001946 if (!weston_surface_is_mapped(sub->surface))
1947 return;
1948
Jason Ekstranda7af7042013-10-12 22:38:11 -05001949 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1950 if (iv->geometry.parent == parent) {
1951 view = iv;
1952 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001953 }
1954 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001955
1956 if (view) {
1957 /* Put it back in the surface's list of views */
1958 wl_list_remove(&view->surface_link);
1959 wl_list_insert(&sub->surface->views, &view->surface_link);
1960 } else {
1961 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001962 weston_view_set_position(view,
1963 sub->position.x,
1964 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001965 weston_view_set_transform_parent(view, parent);
1966 }
1967
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001968 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001969 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001970
Pekka Paalanenb188e912013-11-19 14:03:35 +02001971 if (wl_list_empty(&sub->surface->subsurface_list)) {
1972 wl_list_insert(compositor->view_list.prev, &view->link);
1973 return;
1974 }
1975
1976 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1977 if (child->surface == sub->surface)
1978 wl_list_insert(compositor->view_list.prev, &view->link);
1979 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001980 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001981 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001982}
1983
1984static void
1985view_list_add(struct weston_compositor *compositor,
1986 struct weston_view *view)
1987{
1988 struct weston_subsurface *sub;
1989
1990 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001991
Pekka Paalanenb188e912013-11-19 14:03:35 +02001992 if (wl_list_empty(&view->surface->subsurface_list)) {
1993 wl_list_insert(compositor->view_list.prev, &view->link);
1994 return;
1995 }
1996
1997 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1998 if (sub->surface == view->surface)
1999 wl_list_insert(compositor->view_list.prev, &view->link);
2000 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002001 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002002 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002003}
2004
2005static void
2006weston_compositor_build_view_list(struct weston_compositor *compositor)
2007{
2008 struct weston_view *view;
2009 struct weston_layer *layer;
2010
2011 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002012 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002013 surface_stash_subsurface_views(view->surface);
2014
2015 wl_list_init(&compositor->view_list);
2016 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002017 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002018 view_list_add(compositor, view);
2019 }
2020 }
2021
2022 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002023 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002024 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002025}
2026
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002027static void
2028weston_output_take_feedback_list(struct weston_output *output,
2029 struct weston_surface *surface)
2030{
2031 struct weston_view *view;
2032 struct weston_presentation_feedback *feedback;
2033 uint32_t flags = 0xffffffff;
2034
2035 if (wl_list_empty(&surface->feedback_list))
2036 return;
2037
2038 /* All views must have the flag for the flag to survive. */
2039 wl_list_for_each(view, &surface->views, surface_link) {
2040 /* ignore views that are not on this output at all */
2041 if (view->output_mask & (1u << output->id))
2042 flags &= view->psf_flags;
2043 }
2044
2045 wl_list_for_each(feedback, &surface->feedback_list, link)
2046 feedback->psf_flags = flags;
2047
2048 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2049 wl_list_init(&surface->feedback_list);
2050}
2051
David Herrmann1edf44c2013-10-22 17:11:26 +02002052static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002053weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002054{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002055 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002056 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002057 struct weston_animation *animation, *next;
2058 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002059 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002060 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002061 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002062
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002063 if (output->destroying)
2064 return 0;
2065
Pekka Paalanenb5026542014-11-12 15:09:24 +02002066 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2067
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002068 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002069 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002070
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002071 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002072 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002073 } else {
2074 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002075 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002076 ev->psf_flags = 0;
2077 }
2078 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002079
Pekka Paalanene67858b2013-04-25 13:57:42 +03002080 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002081 wl_list_for_each(ev, &ec->view_list, link) {
2082 /* Note: This operation is safe to do multiple times on the
2083 * same surface.
2084 */
2085 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002086 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002087 &ev->surface->frame_callback_list);
2088 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002089
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002090 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002091 }
2092 }
2093
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002094 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002095
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002096 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002097 pixman_region32_intersect(&output_damage,
2098 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002099 pixman_region32_subtract(&output_damage,
2100 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002101
Scott Moreauccbf29d2012-02-22 14:21:41 -07002102 if (output->dirty)
2103 weston_output_update_matrix(output);
2104
David Herrmann1edf44c2013-10-22 17:11:26 +02002105 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002106
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002107 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002108
Kristian Høgsbergef044142011-06-21 15:02:12 -04002109 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002110
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002111 weston_compositor_repick(ec);
2112 wl_event_loop_dispatch(ec->input_loop, 0);
2113
Jonas Ådahldb773762012-06-13 00:01:21 +02002114 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002115 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002116 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002117 }
2118
Scott Moreaud64cf212012-06-08 19:40:54 -06002119 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002120 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002121 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002122 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002123
Pekka Paalanenb5026542014-11-12 15:09:24 +02002124 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2125
David Herrmann1edf44c2013-10-22 17:11:26 +02002126 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002127}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002128
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002129static int
2130weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002131{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002132 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002133
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002134 wl_event_loop_dispatch(compositor->input_loop, 0);
2135
2136 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002137}
2138
Pekka Paalanen82919792014-05-21 13:51:49 +03002139static void
2140weston_output_schedule_repaint_reset(struct weston_output *output)
2141{
2142 struct weston_compositor *compositor = output->compositor;
2143 struct wl_event_loop *loop;
2144 int fd;
2145
2146 output->repaint_scheduled = 0;
2147 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2148
2149 if (compositor->input_loop_source)
2150 return;
2151
2152 loop = wl_display_get_event_loop(compositor->wl_display);
2153 fd = wl_event_loop_get_fd(compositor->input_loop);
2154 compositor->input_loop_source =
2155 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2156 weston_compositor_read_input, compositor);
2157}
2158
Kristian Høgsbergef044142011-06-21 15:02:12 -04002159WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002160weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002161 const struct timespec *stamp,
2162 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002163{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002164 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen82919792014-05-21 13:51:49 +03002165 int r;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002166 uint32_t refresh_nsec;
2167
Pekka Paalanenb5026542014-11-12 15:09:24 +02002168 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2169 TLP_VBLANK(stamp), TLP_END);
2170
Pekka Paalanen133e4392014-09-23 22:08:46 -04002171 refresh_nsec = 1000000000000UL / output->current_mode->refresh;
2172 weston_presentation_feedback_present_list(&output->feedback_list,
2173 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002174 output->msc,
2175 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002176
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002177 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002178
2179 if (output->repaint_needed &&
2180 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2181 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002182 r = weston_output_repaint(output);
David Herrmann1edf44c2013-10-22 17:11:26 +02002183 if (!r)
2184 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002185 }
2186
Pekka Paalanen82919792014-05-21 13:51:49 +03002187 weston_output_schedule_repaint_reset(output);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002188}
2189
2190static void
2191idle_repaint(void *data)
2192{
2193 struct weston_output *output = data;
2194
Jonas Ådahle5a12252013-04-05 23:07:11 +02002195 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002196}
2197
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002198WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002199weston_layer_entry_insert(struct weston_layer_entry *list,
2200 struct weston_layer_entry *entry)
2201{
2202 wl_list_insert(&list->link, &entry->link);
2203 entry->layer = list->layer;
2204}
2205
2206WL_EXPORT void
2207weston_layer_entry_remove(struct weston_layer_entry *entry)
2208{
2209 wl_list_remove(&entry->link);
2210 wl_list_init(&entry->link);
2211 entry->layer = NULL;
2212}
2213
2214WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002215weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2216{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002217 wl_list_init(&layer->view_list.link);
2218 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002219 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002220 if (below != NULL)
2221 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002222}
2223
2224WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002225weston_layer_set_mask(struct weston_layer *layer,
2226 int x, int y, int width, int height)
2227{
2228 struct weston_view *view;
2229
2230 layer->mask.x1 = x;
2231 layer->mask.x2 = x + width;
2232 layer->mask.y1 = y;
2233 layer->mask.y2 = y + height;
2234
2235 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2236 weston_view_geometry_dirty(view);
2237 }
2238}
2239
2240WL_EXPORT void
2241weston_layer_set_mask_infinite(struct weston_layer *layer)
2242{
2243 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2244 UINT32_MAX, UINT32_MAX);
2245}
2246
2247WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002248weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002249{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002250 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002251 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002252
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002253 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2254 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002255 return;
2256
Pekka Paalanenb5026542014-11-12 15:09:24 +02002257 if (!output->repaint_needed)
2258 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2259
Kristian Høgsbergef044142011-06-21 15:02:12 -04002260 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002261 output->repaint_needed = 1;
2262 if (output->repaint_scheduled)
2263 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002264
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002265 wl_event_loop_add_idle(loop, idle_repaint, output);
2266 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002267 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2268
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002269
2270 if (compositor->input_loop_source) {
2271 wl_event_source_remove(compositor->input_loop_source);
2272 compositor->input_loop_source = NULL;
2273 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002274}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002275
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002276WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002277weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2278{
2279 struct weston_output *output;
2280
2281 wl_list_for_each(output, &compositor->output_list, link)
2282 weston_output_schedule_repaint(output);
2283}
2284
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002285static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002286surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002287{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002288 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002289}
2290
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002291static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002292surface_attach(struct wl_client *client,
2293 struct wl_resource *resource,
2294 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2295{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002296 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002297 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002298
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002299 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002300 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002301 if (buffer == NULL) {
2302 wl_client_post_no_memory(client);
2303 return;
2304 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002305 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002306
Pekka Paalanende685b82012-12-04 15:58:12 +02002307 /* Attach, attach, without commit in between does not send
2308 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002309 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002310
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002311 surface->pending.sx = sx;
2312 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002313 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002314}
2315
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002316static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002317surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002318 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002319 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002320{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002321 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002322
Pekka Paalanen8e159182012-10-10 12:49:25 +03002323 pixman_region32_union_rect(&surface->pending.damage,
2324 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002325 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002326}
2327
Kristian Høgsberg33418202011-08-16 23:01:28 -04002328static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002329destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002330{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002331 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002332
2333 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002334 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002335}
2336
2337static void
2338surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002339 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002340{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002341 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002342 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002343
2344 cb = malloc(sizeof *cb);
2345 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002346 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002347 return;
2348 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002349
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002350 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2351 callback);
2352 if (cb->resource == NULL) {
2353 free(cb);
2354 wl_resource_post_no_memory(resource);
2355 return;
2356 }
2357
Jason Ekstranda85118c2013-06-27 20:17:02 -05002358 wl_resource_set_implementation(cb->resource, NULL, cb,
2359 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002360
Pekka Paalanenbc106382012-10-10 12:49:31 +03002361 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002362}
2363
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002364static void
2365surface_set_opaque_region(struct wl_client *client,
2366 struct wl_resource *resource,
2367 struct wl_resource *region_resource)
2368{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002369 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002370 struct weston_region *region;
2371
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002372 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002373 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002374 pixman_region32_copy(&surface->pending.opaque,
2375 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002376 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002377 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002378 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002379}
2380
2381static void
2382surface_set_input_region(struct wl_client *client,
2383 struct wl_resource *resource,
2384 struct wl_resource *region_resource)
2385{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002386 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002387 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002388
2389 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002390 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002391 pixman_region32_copy(&surface->pending.input,
2392 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002393 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002394 pixman_region32_fini(&surface->pending.input);
2395 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002396 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002397}
2398
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002399static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002400weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002401{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002402 struct weston_subsurface *sub;
2403
2404 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2405 parent_link_pending) {
2406 wl_list_remove(&sub->parent_link);
2407 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2408 }
2409}
2410
2411static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002412weston_surface_commit_state(struct weston_surface *surface,
2413 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002414{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002415 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002416 pixman_region32_t opaque;
2417
Alexander Larsson4ea95522013-05-22 14:41:37 +02002418 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002419 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002420 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002421 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002422
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002423 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002424 if (state->newly_attached)
2425 weston_surface_attach(surface, state->buffer);
2426 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002427
Jason Ekstrand7b982072014-05-20 14:33:03 -05002428 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002429 weston_surface_update_size(surface);
2430 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002431 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002432 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002433
Jason Ekstrand7b982072014-05-20 14:33:03 -05002434 state->sx = 0;
2435 state->sy = 0;
2436 state->newly_attached = 0;
2437 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002438
2439 /* wl_surface.damage */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002440 if (weston_timeline_enabled_ &&
2441 pixman_region32_not_empty(&state->damage))
2442 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002443 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002444 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002445 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002446 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002447 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002448
2449 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002450 pixman_region32_init(&opaque);
2451 pixman_region32_intersect_rect(&opaque, &state->opaque,
2452 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002453
2454 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2455 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002456 wl_list_for_each(view, &surface->views, surface_link)
2457 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002458 }
2459
2460 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002461
2462 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002463 pixman_region32_intersect_rect(&surface->input, &state->input,
2464 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002465
Pekka Paalanenbc106382012-10-10 12:49:31 +03002466 /* wl_surface.frame */
2467 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002468 &state->frame_callback_list);
2469 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002470
2471 /* XXX:
2472 * What should happen with a feedback request, if there
2473 * is no wl_buffer attached for this commit?
2474 */
2475
2476 /* presentation.feedback */
2477 wl_list_insert_list(&surface->feedback_list,
2478 &state->feedback_list);
2479 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002480}
2481
2482static void
2483weston_surface_commit(struct weston_surface *surface)
2484{
2485 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002486
Pekka Paalanene67858b2013-04-25 13:57:42 +03002487 weston_surface_commit_subsurface_order(surface);
2488
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002489 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002490}
2491
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002492static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002493weston_subsurface_commit(struct weston_subsurface *sub);
2494
2495static void
2496weston_subsurface_parent_commit(struct weston_subsurface *sub,
2497 int parent_is_synchronized);
2498
2499static void
2500surface_commit(struct wl_client *client, struct wl_resource *resource)
2501{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002502 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002503 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2504
2505 if (sub) {
2506 weston_subsurface_commit(sub);
2507 return;
2508 }
2509
2510 weston_surface_commit(surface);
2511
2512 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2513 if (sub->surface != surface)
2514 weston_subsurface_parent_commit(sub, 0);
2515 }
2516}
2517
2518static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002519surface_set_buffer_transform(struct wl_client *client,
2520 struct wl_resource *resource, int transform)
2521{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002522 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002523
Jonny Lamba55f1392014-05-30 12:07:15 +02002524 /* if wl_output.transform grows more members this will need to be updated. */
2525 if (transform < 0 ||
2526 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2527 wl_resource_post_error(resource,
2528 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2529 "buffer transform must be a valid transform "
2530 "('%d' specified)", transform);
2531 return;
2532 }
2533
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002534 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002535 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002536}
2537
Alexander Larsson4ea95522013-05-22 14:41:37 +02002538static void
2539surface_set_buffer_scale(struct wl_client *client,
2540 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002541 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002542{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002543 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002544
Jonny Lamba55f1392014-05-30 12:07:15 +02002545 if (scale < 1) {
2546 wl_resource_post_error(resource,
2547 WL_SURFACE_ERROR_INVALID_SCALE,
2548 "buffer scale must be at least one "
2549 "('%d' specified)", scale);
2550 return;
2551 }
2552
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002553 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002554 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002555}
2556
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002557static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002558 surface_destroy,
2559 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002560 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002561 surface_frame,
2562 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002563 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002564 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002565 surface_set_buffer_transform,
2566 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002567};
2568
2569static void
2570compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002571 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002572{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002573 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002574 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002575
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002576 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002577 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002578 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002579 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002580 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002581
Jason Ekstranda85118c2013-06-27 20:17:02 -05002582 surface->resource =
2583 wl_resource_create(client, &wl_surface_interface,
2584 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002585 if (surface->resource == NULL) {
2586 weston_surface_destroy(surface);
2587 wl_resource_post_no_memory(resource);
2588 return;
2589 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002590 wl_resource_set_implementation(surface->resource, &surface_interface,
2591 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002592
2593 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002594}
2595
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002596static void
2597destroy_region(struct wl_resource *resource)
2598{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002599 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002600
2601 pixman_region32_fini(&region->region);
2602 free(region);
2603}
2604
2605static void
2606region_destroy(struct wl_client *client, struct wl_resource *resource)
2607{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002608 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002609}
2610
2611static void
2612region_add(struct wl_client *client, struct wl_resource *resource,
2613 int32_t x, int32_t y, int32_t width, int32_t height)
2614{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002615 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002616
2617 pixman_region32_union_rect(&region->region, &region->region,
2618 x, y, width, height);
2619}
2620
2621static void
2622region_subtract(struct wl_client *client, struct wl_resource *resource,
2623 int32_t x, int32_t y, int32_t width, int32_t height)
2624{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002625 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002626 pixman_region32_t rect;
2627
2628 pixman_region32_init_rect(&rect, x, y, width, height);
2629 pixman_region32_subtract(&region->region, &region->region, &rect);
2630 pixman_region32_fini(&rect);
2631}
2632
2633static const struct wl_region_interface region_interface = {
2634 region_destroy,
2635 region_add,
2636 region_subtract
2637};
2638
2639static void
2640compositor_create_region(struct wl_client *client,
2641 struct wl_resource *resource, uint32_t id)
2642{
2643 struct weston_region *region;
2644
2645 region = malloc(sizeof *region);
2646 if (region == NULL) {
2647 wl_resource_post_no_memory(resource);
2648 return;
2649 }
2650
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002651 pixman_region32_init(&region->region);
2652
Jason Ekstranda85118c2013-06-27 20:17:02 -05002653 region->resource =
2654 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002655 if (region->resource == NULL) {
2656 free(region);
2657 wl_resource_post_no_memory(resource);
2658 return;
2659 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002660 wl_resource_set_implementation(region->resource, &region_interface,
2661 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002662}
2663
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002664static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002665 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002666 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002667};
2668
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002669static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002670weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2671{
2672 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002673
Jason Ekstrand7b982072014-05-20 14:33:03 -05002674 weston_surface_commit_state(surface, &sub->cached);
2675 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002676
2677 weston_surface_commit_subsurface_order(surface);
2678
2679 weston_surface_schedule_repaint(surface);
2680
Jason Ekstrand7b982072014-05-20 14:33:03 -05002681 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002682}
2683
2684static void
2685weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2686{
2687 struct weston_surface *surface = sub->surface;
2688
2689 /*
2690 * If this commit would cause the surface to move by the
2691 * attach(dx, dy) parameters, the old damage region must be
2692 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002693 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002694 */
2695 pixman_region32_translate(&sub->cached.damage,
2696 -surface->pending.sx, -surface->pending.sy);
2697 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2698 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002699 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002700
2701 if (surface->pending.newly_attached) {
2702 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002703 weston_surface_state_set_buffer(&sub->cached,
2704 surface->pending.buffer);
2705 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002706 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002707 weston_presentation_feedback_discard_list(
2708 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002709 }
2710 sub->cached.sx += surface->pending.sx;
2711 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002712
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002713 sub->cached.buffer_viewport.changed |=
2714 surface->pending.buffer_viewport.changed;
2715 sub->cached.buffer_viewport.buffer =
2716 surface->pending.buffer_viewport.buffer;
2717 sub->cached.buffer_viewport.surface =
2718 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002719
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002720 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002721
2722 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2723
2724 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2725
2726 wl_list_insert_list(&sub->cached.frame_callback_list,
2727 &surface->pending.frame_callback_list);
2728 wl_list_init(&surface->pending.frame_callback_list);
2729
Pekka Paalanen133e4392014-09-23 22:08:46 -04002730 wl_list_insert_list(&sub->cached.feedback_list,
2731 &surface->pending.feedback_list);
2732 wl_list_init(&surface->pending.feedback_list);
2733
Jason Ekstrand7b982072014-05-20 14:33:03 -05002734 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002735}
2736
Derek Foreman280e7dd2014-10-03 13:13:42 -05002737static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03002738weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2739{
2740 while (sub) {
2741 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002742 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002743
2744 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002745 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002746
2747 sub = weston_surface_to_subsurface(sub->parent);
2748 }
2749
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01002750 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002751}
2752
2753static void
2754weston_subsurface_commit(struct weston_subsurface *sub)
2755{
2756 struct weston_surface *surface = sub->surface;
2757 struct weston_subsurface *tmp;
2758
2759 /* Recursive check for effectively synchronized. */
2760 if (weston_subsurface_is_synchronized(sub)) {
2761 weston_subsurface_commit_to_cache(sub);
2762 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05002763 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002764 /* flush accumulated state from cache */
2765 weston_subsurface_commit_to_cache(sub);
2766 weston_subsurface_commit_from_cache(sub);
2767 } else {
2768 weston_surface_commit(surface);
2769 }
2770
2771 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2772 if (tmp->surface != surface)
2773 weston_subsurface_parent_commit(tmp, 0);
2774 }
2775 }
2776}
2777
2778static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002779weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002780{
2781 struct weston_surface *surface = sub->surface;
2782 struct weston_subsurface *tmp;
2783
Pekka Paalanene67858b2013-04-25 13:57:42 +03002784 /* From now on, commit_from_cache the whole sub-tree, regardless of
2785 * the synchronized mode of each child. This sub-surface or some
2786 * of its ancestors were synchronized, so we are synchronized
2787 * all the way down.
2788 */
2789
Jason Ekstrand7b982072014-05-20 14:33:03 -05002790 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002791 weston_subsurface_commit_from_cache(sub);
2792
2793 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2794 if (tmp->surface != surface)
2795 weston_subsurface_parent_commit(tmp, 1);
2796 }
2797}
2798
2799static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002800weston_subsurface_parent_commit(struct weston_subsurface *sub,
2801 int parent_is_synchronized)
2802{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002803 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002804 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002805 wl_list_for_each(view, &sub->surface->views, surface_link)
2806 weston_view_set_position(view,
2807 sub->position.x,
2808 sub->position.y);
2809
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002810 sub->position.set = 0;
2811 }
2812
2813 if (parent_is_synchronized || sub->synchronized)
2814 weston_subsurface_synchronized_commit(sub);
2815}
2816
Pekka Paalanen8274d902014-08-06 19:36:51 +03002817static int
2818subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
2819{
2820 return snprintf(buf, len, "sub-surface");
2821}
2822
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002823static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002824subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002825{
2826 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002827 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002828
Jason Ekstranda7af7042013-10-12 22:38:11 -05002829 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002830 weston_view_set_position(view,
2831 view->geometry.x + dx,
2832 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002833
2834 /* No need to check parent mappedness, because if parent is not
2835 * mapped, parent is not in a visible layer, so this sub-surface
2836 * will not be drawn either.
2837 */
2838 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002839 struct weston_output *output;
2840
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002841 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03002842 * because that would call it also for the parent surface,
2843 * which might not be mapped yet. That would lead to
2844 * inconsistent state, where the window could never be
2845 * mapped.
2846 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002847 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03002848 * weston_surface_is_mapped() return true, so that when the
2849 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002850 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03002851 */
2852 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002853 output = container_of(compositor->output_list.next,
2854 struct weston_output, link);
2855
2856 surface->output = output;
2857 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002858 }
2859}
2860
2861static struct weston_subsurface *
2862weston_surface_to_subsurface(struct weston_surface *surface)
2863{
2864 if (surface->configure == subsurface_configure)
2865 return surface->configure_private;
2866
2867 return NULL;
2868}
2869
Pekka Paalanen01388e22013-04-25 13:57:44 +03002870WL_EXPORT struct weston_surface *
2871weston_surface_get_main_surface(struct weston_surface *surface)
2872{
2873 struct weston_subsurface *sub;
2874
2875 while (surface && (sub = weston_surface_to_subsurface(surface)))
2876 surface = sub->parent;
2877
2878 return surface;
2879}
2880
Pekka Paalanen50b67472014-10-01 15:02:41 +03002881WL_EXPORT int
2882weston_surface_set_role(struct weston_surface *surface,
2883 const char *role_name,
2884 struct wl_resource *error_resource,
2885 uint32_t error_code)
2886{
2887 assert(role_name);
2888
2889 if (surface->role_name == NULL ||
2890 surface->role_name == role_name ||
2891 strcmp(surface->role_name, role_name) == 0) {
2892 surface->role_name = role_name;
2893
2894 return 0;
2895 }
2896
2897 wl_resource_post_error(error_resource, error_code,
2898 "Cannot assign role %s to wl_surface@%d,"
2899 " already has role %s\n",
2900 role_name,
2901 wl_resource_get_id(surface->resource),
2902 surface->role_name);
2903 return -1;
2904}
2905
Pekka Paalanen8274d902014-08-06 19:36:51 +03002906WL_EXPORT void
2907weston_surface_set_label_func(struct weston_surface *surface,
2908 int (*desc)(struct weston_surface *,
2909 char *, size_t))
2910{
2911 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002912 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002913}
2914
Pekka Paalanenc647ed72015-02-09 13:16:57 +02002915/** Get the size of surface contents
2916 *
2917 * \param surface The surface to query.
2918 * \param width Returns the width of raw contents.
2919 * \param height Returns the height of raw contents.
2920 *
2921 * Retrieves the raw surface content size in pixels for the given surface.
2922 * This is the whole content size in buffer pixels. If the surface
2923 * has no content or the renderer does not implement this feature,
2924 * zeroes are returned.
2925 *
2926 * This function is used to determine the buffer size needed for
2927 * a weston_surface_copy_content() call.
2928 */
2929WL_EXPORT void
2930weston_surface_get_content_size(struct weston_surface *surface,
2931 int *width, int *height)
2932{
2933 struct weston_renderer *rer = surface->compositor->renderer;
2934
2935 if (!rer->surface_get_content_size) {
2936 *width = 0;
2937 *height = 0;
2938 return;
2939 }
2940
2941 rer->surface_get_content_size(surface, width, height);
2942}
2943
2944/** Copy surface contents to system memory.
2945 *
2946 * \param surface The surface to copy from.
2947 * \param target Pointer to the target memory buffer.
2948 * \param size Size of the target buffer in bytes.
2949 * \param src_x X location on contents to copy from.
2950 * \param src_y Y location on contents to copy from.
2951 * \param width Width in pixels of the area to copy.
2952 * \param height Height in pixels of the area to copy.
2953 * \return 0 for success, -1 for failure.
2954 *
2955 * Surface contents are maintained by the renderer. They can be in a
2956 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
2957 * else.
2958 *
2959 * Surface contents are copied into memory pointed to by target,
2960 * which has size bytes of space available. The target memory
2961 * may be larger than needed, but being smaller returns an error.
2962 * The extra bytes in target may or may not be written; their content is
2963 * unspecified. Size must be large enough to hold the image.
2964 *
2965 * The image in the target memory will be arranged in rows from
2966 * top to bottom, and pixels on a row from left to right. The pixel
2967 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
2968 * width * 4.
2969 *
2970 * Parameters src_x and src_y define the upper-left corner in buffer
2971 * coordinates (pixels) to copy from. Parameters width and height
2972 * define the size of the area to copy in pixels.
2973 *
2974 * The rectangle defined by src_x, src_y, width, height must fit in
2975 * the surface contents. Otherwise an error is returned.
2976 *
2977 * Use surface_get_data_size to determine the content size; the
2978 * needed target buffer size and rectangle limits.
2979 *
2980 * CURRENT IMPLEMENTATION RESTRICTIONS:
2981 * - the machine must be little-endian due to Pixman formats.
2982 *
2983 * NOTE: Pixman formats are premultiplied.
2984 */
2985WL_EXPORT int
2986weston_surface_copy_content(struct weston_surface *surface,
2987 void *target, size_t size,
2988 int src_x, int src_y,
2989 int width, int height)
2990{
2991 struct weston_renderer *rer = surface->compositor->renderer;
2992 int cw, ch;
2993 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
2994
2995 if (!rer->surface_copy_content)
2996 return -1;
2997
2998 weston_surface_get_content_size(surface, &cw, &ch);
2999
3000 if (src_x < 0 || src_y < 0)
3001 return -1;
3002
3003 if (width <= 0 || height <= 0)
3004 return -1;
3005
3006 if (src_x + width > cw || src_y + height > ch)
3007 return -1;
3008
3009 if (width * bytespp * height > size)
3010 return -1;
3011
3012 return rer->surface_copy_content(surface, target, size,
3013 src_x, src_y, width, height);
3014}
3015
Pekka Paalanene67858b2013-04-25 13:57:42 +03003016static void
3017subsurface_set_position(struct wl_client *client,
3018 struct wl_resource *resource, int32_t x, int32_t y)
3019{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003020 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003021
3022 if (!sub)
3023 return;
3024
3025 sub->position.x = x;
3026 sub->position.y = y;
3027 sub->position.set = 1;
3028}
3029
3030static struct weston_subsurface *
3031subsurface_from_surface(struct weston_surface *surface)
3032{
3033 struct weston_subsurface *sub;
3034
3035 sub = weston_surface_to_subsurface(surface);
3036 if (sub)
3037 return sub;
3038
3039 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3040 if (sub->surface == surface)
3041 return sub;
3042
3043 return NULL;
3044}
3045
3046static struct weston_subsurface *
3047subsurface_sibling_check(struct weston_subsurface *sub,
3048 struct weston_surface *surface,
3049 const char *request)
3050{
3051 struct weston_subsurface *sibling;
3052
3053 sibling = subsurface_from_surface(surface);
3054
3055 if (!sibling) {
3056 wl_resource_post_error(sub->resource,
3057 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3058 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003059 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003060 return NULL;
3061 }
3062
3063 if (sibling->parent != sub->parent) {
3064 wl_resource_post_error(sub->resource,
3065 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3066 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003067 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003068 return NULL;
3069 }
3070
3071 return sibling;
3072}
3073
3074static void
3075subsurface_place_above(struct wl_client *client,
3076 struct wl_resource *resource,
3077 struct wl_resource *sibling_resource)
3078{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003079 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003080 struct weston_surface *surface =
3081 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003082 struct weston_subsurface *sibling;
3083
3084 if (!sub)
3085 return;
3086
3087 sibling = subsurface_sibling_check(sub, surface, "place_above");
3088 if (!sibling)
3089 return;
3090
3091 wl_list_remove(&sub->parent_link_pending);
3092 wl_list_insert(sibling->parent_link_pending.prev,
3093 &sub->parent_link_pending);
3094}
3095
3096static void
3097subsurface_place_below(struct wl_client *client,
3098 struct wl_resource *resource,
3099 struct wl_resource *sibling_resource)
3100{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003101 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003102 struct weston_surface *surface =
3103 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003104 struct weston_subsurface *sibling;
3105
3106 if (!sub)
3107 return;
3108
3109 sibling = subsurface_sibling_check(sub, surface, "place_below");
3110 if (!sibling)
3111 return;
3112
3113 wl_list_remove(&sub->parent_link_pending);
3114 wl_list_insert(&sibling->parent_link_pending,
3115 &sub->parent_link_pending);
3116}
3117
3118static void
3119subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3120{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003121 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003122
3123 if (sub)
3124 sub->synchronized = 1;
3125}
3126
3127static void
3128subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3129{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003130 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003131
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003132 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003133 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003134
3135 /* If sub became effectively desynchronized, flush. */
3136 if (!weston_subsurface_is_synchronized(sub))
3137 weston_subsurface_synchronized_commit(sub);
3138 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003139}
3140
3141static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003142weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3143{
3144 wl_list_remove(&sub->parent_link);
3145 wl_list_remove(&sub->parent_link_pending);
3146 wl_list_remove(&sub->parent_destroy_listener.link);
3147 sub->parent = NULL;
3148}
3149
3150static void
3151weston_subsurface_destroy(struct weston_subsurface *sub);
3152
3153static void
3154subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3155{
3156 struct weston_subsurface *sub =
3157 container_of(listener, struct weston_subsurface,
3158 surface_destroy_listener);
3159 assert(data == &sub->surface->resource);
3160
3161 /* The protocol object (wl_resource) is left inert. */
3162 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003163 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003164
3165 weston_subsurface_destroy(sub);
3166}
3167
3168static void
3169subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3170{
3171 struct weston_subsurface *sub =
3172 container_of(listener, struct weston_subsurface,
3173 parent_destroy_listener);
3174 assert(data == &sub->parent->resource);
3175 assert(sub->surface != sub->parent);
3176
3177 if (weston_surface_is_mapped(sub->surface))
3178 weston_surface_unmap(sub->surface);
3179
3180 weston_subsurface_unlink_parent(sub);
3181}
3182
3183static void
3184subsurface_resource_destroy(struct wl_resource *resource)
3185{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003186 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003187
3188 if (sub)
3189 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003190}
3191
3192static void
3193subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3194{
3195 wl_resource_destroy(resource);
3196}
3197
3198static void
3199weston_subsurface_link_parent(struct weston_subsurface *sub,
3200 struct weston_surface *parent)
3201{
3202 sub->parent = parent;
3203 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003204 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003205 &sub->parent_destroy_listener);
3206
3207 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3208 wl_list_insert(&parent->subsurface_list_pending,
3209 &sub->parent_link_pending);
3210}
3211
3212static void
3213weston_subsurface_link_surface(struct weston_subsurface *sub,
3214 struct weston_surface *surface)
3215{
3216 sub->surface = surface;
3217 sub->surface_destroy_listener.notify =
3218 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003219 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003220 &sub->surface_destroy_listener);
3221}
3222
3223static void
3224weston_subsurface_destroy(struct weston_subsurface *sub)
3225{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003226 struct weston_view *view, *next;
3227
Pekka Paalanene67858b2013-04-25 13:57:42 +03003228 assert(sub->surface);
3229
3230 if (sub->resource) {
3231 assert(weston_surface_to_subsurface(sub->surface) == sub);
3232 assert(sub->parent_destroy_listener.notify ==
3233 subsurface_handle_parent_destroy);
3234
George Kiagiadakised04d382014-06-13 18:10:26 +02003235 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3236 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003237 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003238 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003239
Pekka Paalanene67858b2013-04-25 13:57:42 +03003240 if (sub->parent)
3241 weston_subsurface_unlink_parent(sub);
3242
Jason Ekstrand7b982072014-05-20 14:33:03 -05003243 weston_surface_state_fini(&sub->cached);
3244 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003245
3246 sub->surface->configure = NULL;
3247 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003248 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003249 } else {
3250 /* the dummy weston_subsurface for the parent itself */
3251 assert(sub->parent_destroy_listener.notify == NULL);
3252 wl_list_remove(&sub->parent_link);
3253 wl_list_remove(&sub->parent_link_pending);
3254 }
3255
3256 wl_list_remove(&sub->surface_destroy_listener.link);
3257 free(sub);
3258}
3259
3260static const struct wl_subsurface_interface subsurface_implementation = {
3261 subsurface_destroy,
3262 subsurface_set_position,
3263 subsurface_place_above,
3264 subsurface_place_below,
3265 subsurface_set_sync,
3266 subsurface_set_desync
3267};
3268
3269static struct weston_subsurface *
3270weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3271 struct weston_surface *parent)
3272{
3273 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003274 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003275
Bryce Harringtonde16d892014-11-20 22:21:57 -08003276 sub = zalloc(sizeof *sub);
3277 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003278 return NULL;
3279
Jason Ekstranda7af7042013-10-12 22:38:11 -05003280 wl_list_init(&sub->unused_views);
3281
Jason Ekstranda85118c2013-06-27 20:17:02 -05003282 sub->resource =
3283 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003284 if (!sub->resource) {
3285 free(sub);
3286 return NULL;
3287 }
3288
Jason Ekstranda85118c2013-06-27 20:17:02 -05003289 wl_resource_set_implementation(sub->resource,
3290 &subsurface_implementation,
3291 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003292 weston_subsurface_link_surface(sub, surface);
3293 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003294 weston_surface_state_init(&sub->cached);
3295 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003296 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003297
3298 return sub;
3299}
3300
3301/* Create a dummy subsurface for having the parent itself in its
3302 * sub-surface lists. Makes stacking order manipulation easy.
3303 */
3304static struct weston_subsurface *
3305weston_subsurface_create_for_parent(struct weston_surface *parent)
3306{
3307 struct weston_subsurface *sub;
3308
Bryce Harringtonde16d892014-11-20 22:21:57 -08003309 sub = zalloc(sizeof *sub);
3310 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003311 return NULL;
3312
3313 weston_subsurface_link_surface(sub, parent);
3314 sub->parent = parent;
3315 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3316 wl_list_insert(&parent->subsurface_list_pending,
3317 &sub->parent_link_pending);
3318
3319 return sub;
3320}
3321
3322static void
3323subcompositor_get_subsurface(struct wl_client *client,
3324 struct wl_resource *resource,
3325 uint32_t id,
3326 struct wl_resource *surface_resource,
3327 struct wl_resource *parent_resource)
3328{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003329 struct weston_surface *surface =
3330 wl_resource_get_user_data(surface_resource);
3331 struct weston_surface *parent =
3332 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003333 struct weston_subsurface *sub;
3334 static const char where[] = "get_subsurface: wl_subsurface@";
3335
3336 if (surface == parent) {
3337 wl_resource_post_error(resource,
3338 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3339 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003340 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003341 return;
3342 }
3343
3344 if (weston_surface_to_subsurface(surface)) {
3345 wl_resource_post_error(resource,
3346 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3347 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003348 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003349 return;
3350 }
3351
Pekka Paalanen50b67472014-10-01 15:02:41 +03003352 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3353 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003354 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003355
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003356 if (weston_surface_get_main_surface(parent) == surface) {
3357 wl_resource_post_error(resource,
3358 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3359 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003360 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003361 return;
3362 }
3363
Pekka Paalanene67858b2013-04-25 13:57:42 +03003364 /* make sure the parent is in its own list */
3365 if (wl_list_empty(&parent->subsurface_list)) {
3366 if (!weston_subsurface_create_for_parent(parent)) {
3367 wl_resource_post_no_memory(resource);
3368 return;
3369 }
3370 }
3371
3372 sub = weston_subsurface_create(id, surface, parent);
3373 if (!sub) {
3374 wl_resource_post_no_memory(resource);
3375 return;
3376 }
3377
3378 surface->configure = subsurface_configure;
3379 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003380 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003381}
3382
3383static void
3384subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3385{
3386 wl_resource_destroy(resource);
3387}
3388
3389static const struct wl_subcompositor_interface subcompositor_interface = {
3390 subcompositor_destroy,
3391 subcompositor_get_subsurface
3392};
3393
3394static void
3395bind_subcompositor(struct wl_client *client,
3396 void *data, uint32_t version, uint32_t id)
3397{
3398 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003399 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003400
Jason Ekstranda85118c2013-06-27 20:17:02 -05003401 resource =
3402 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003403 if (resource == NULL) {
3404 wl_client_post_no_memory(client);
3405 return;
3406 }
3407 wl_resource_set_implementation(resource, &subcompositor_interface,
3408 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003409}
3410
3411static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003412weston_compositor_dpms(struct weston_compositor *compositor,
3413 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003414{
3415 struct weston_output *output;
3416
3417 wl_list_for_each(output, &compositor->output_list, link)
3418 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003419 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003420}
3421
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003422WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003423weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003424{
Neil Roberts8b62e202013-09-30 13:14:47 +01003425 uint32_t old_state = compositor->state;
3426
3427 /* The state needs to be changed before emitting the wake
3428 * signal because that may try to schedule a repaint which
3429 * will not work if the compositor is still sleeping */
3430 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3431
3432 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003433 case WESTON_COMPOSITOR_SLEEPING:
3434 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3435 /* fall through */
3436 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003437 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003438 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003439 /* fall through */
3440 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003441 wl_event_source_timer_update(compositor->idle_source,
3442 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003443 }
3444}
3445
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003446WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003447weston_compositor_offscreen(struct weston_compositor *compositor)
3448{
3449 switch (compositor->state) {
3450 case WESTON_COMPOSITOR_OFFSCREEN:
3451 return;
3452 case WESTON_COMPOSITOR_SLEEPING:
3453 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3454 /* fall through */
3455 default:
3456 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3457 wl_event_source_timer_update(compositor->idle_source, 0);
3458 }
3459}
3460
3461WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003462weston_compositor_sleep(struct weston_compositor *compositor)
3463{
3464 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3465 return;
3466
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003467 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003468 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3469 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3470}
3471
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003472static int
3473idle_handler(void *data)
3474{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003475 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003476
3477 if (compositor->idle_inhibit)
3478 return 1;
3479
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003480 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003481 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003482
3483 return 1;
3484}
3485
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003486WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003487weston_plane_init(struct weston_plane *plane,
3488 struct weston_compositor *ec,
3489 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003490{
3491 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003492 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003493 plane->x = x;
3494 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003495 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003496
3497 /* Init the link so that the call to wl_list_remove() when releasing
3498 * the plane without ever stacking doesn't lead to a crash */
3499 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003500}
3501
3502WL_EXPORT void
3503weston_plane_release(struct weston_plane *plane)
3504{
Xiong Zhang97116532013-10-23 13:58:31 +08003505 struct weston_view *view;
3506
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003507 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003508 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003509
Xiong Zhang97116532013-10-23 13:58:31 +08003510 wl_list_for_each(view, &plane->compositor->view_list, link) {
3511 if (view->plane == plane)
3512 view->plane = NULL;
3513 }
3514
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003515 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003516}
3517
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003518WL_EXPORT void
3519weston_compositor_stack_plane(struct weston_compositor *ec,
3520 struct weston_plane *plane,
3521 struct weston_plane *above)
3522{
3523 if (above)
3524 wl_list_insert(above->link.prev, &plane->link);
3525 else
3526 wl_list_insert(&ec->plane_list, &plane->link);
3527}
3528
Casey Dahlin9074db52012-04-19 22:50:09 -04003529static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003530{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003531 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003532}
3533
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003534static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003535bind_output(struct wl_client *client,
3536 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003537{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003538 struct weston_output *output = data;
3539 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003540 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003541
Jason Ekstranda85118c2013-06-27 20:17:02 -05003542 resource = wl_resource_create(client, &wl_output_interface,
3543 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003544 if (resource == NULL) {
3545 wl_client_post_no_memory(client);
3546 return;
3547 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003548
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003549 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003550 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003551
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003552 wl_output_send_geometry(resource,
3553 output->x,
3554 output->y,
3555 output->mm_width,
3556 output->mm_height,
3557 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003558 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003559 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003560 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003561 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003562 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003563
3564 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003565 wl_output_send_mode(resource,
3566 mode->flags,
3567 mode->width,
3568 mode->height,
3569 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003570 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003571
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003572 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003573 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003574}
3575
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003576/* Move other outputs when one is removed so the space remains contiguos. */
3577static void
3578weston_compositor_remove_output(struct weston_compositor *compositor,
3579 struct weston_output *remove_output)
3580{
3581 struct weston_output *output;
3582 int offset = 0;
3583
3584 wl_list_for_each(output, &compositor->output_list, link) {
3585 if (output == remove_output) {
3586 offset = output->width;
3587 continue;
3588 }
3589
3590 if (offset > 0) {
3591 weston_output_move(output,
3592 output->x - offset, output->y);
3593 output->dirty = 1;
3594 }
3595 }
3596}
3597
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003598WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003599weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003600{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003601 struct wl_resource *resource;
3602
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003603 output->destroying = 1;
3604
Pekka Paalanen133e4392014-09-23 22:08:46 -04003605 weston_presentation_feedback_discard_list(&output->feedback_list);
3606
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003607 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003608 wl_list_remove(&output->link);
3609
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003610 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003611 wl_signal_emit(&output->destroy_signal, output);
3612
Richard Hughesafe690c2013-05-02 10:10:04 +01003613 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003614 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003615 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003616 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003617
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003618 wl_resource_for_each(resource, &output->resource_list) {
3619 wl_resource_set_destructor(resource, NULL);
3620 }
3621
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003622 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003623}
3624
Scott Moreau1bad5db2012-08-18 01:04:05 -06003625static void
3626weston_output_compute_transform(struct weston_output *output)
3627{
3628 struct weston_matrix transform;
3629 int flip;
3630
3631 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003632 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003633
3634 switch(output->transform) {
3635 case WL_OUTPUT_TRANSFORM_FLIPPED:
3636 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3637 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3638 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003639 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003640 flip = -1;
3641 break;
3642 default:
3643 flip = 1;
3644 break;
3645 }
3646
3647 switch(output->transform) {
3648 case WL_OUTPUT_TRANSFORM_NORMAL:
3649 case WL_OUTPUT_TRANSFORM_FLIPPED:
3650 transform.d[0] = flip;
3651 transform.d[1] = 0;
3652 transform.d[4] = 0;
3653 transform.d[5] = 1;
3654 break;
3655 case WL_OUTPUT_TRANSFORM_90:
3656 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3657 transform.d[0] = 0;
3658 transform.d[1] = -flip;
3659 transform.d[4] = 1;
3660 transform.d[5] = 0;
3661 break;
3662 case WL_OUTPUT_TRANSFORM_180:
3663 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3664 transform.d[0] = -flip;
3665 transform.d[1] = 0;
3666 transform.d[4] = 0;
3667 transform.d[5] = -1;
3668 break;
3669 case WL_OUTPUT_TRANSFORM_270:
3670 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3671 transform.d[0] = 0;
3672 transform.d[1] = flip;
3673 transform.d[4] = -1;
3674 transform.d[5] = 0;
3675 break;
3676 default:
3677 break;
3678 }
3679
3680 weston_matrix_multiply(&output->matrix, &transform);
3681}
3682
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003683WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003684weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003685{
Scott Moreau850ca422012-05-21 15:21:25 -06003686 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003687
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003688 weston_matrix_init(&output->matrix);
3689 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003690 -(output->x + output->width / 2.0),
3691 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003692
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003693 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003694 2.0 / output->width,
3695 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003696
Scott Moreauccbf29d2012-02-22 14:21:41 -07003697 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003698 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003699 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003700 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3701 output->zoom.trans_y, 0);
3702 weston_matrix_scale(&output->matrix, magnification,
3703 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003704 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003705
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003706 weston_output_compute_transform(output);
3707
Scott Moreauccbf29d2012-02-22 14:21:41 -07003708 output->dirty = 0;
3709}
3710
Scott Moreau1bad5db2012-08-18 01:04:05 -06003711static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003712weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003713{
3714 output->transform = transform;
3715
3716 switch (transform) {
3717 case WL_OUTPUT_TRANSFORM_90:
3718 case WL_OUTPUT_TRANSFORM_270:
3719 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3720 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3721 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003722 output->width = output->current_mode->height;
3723 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003724 break;
3725 case WL_OUTPUT_TRANSFORM_NORMAL:
3726 case WL_OUTPUT_TRANSFORM_180:
3727 case WL_OUTPUT_TRANSFORM_FLIPPED:
3728 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003729 output->width = output->current_mode->width;
3730 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003731 break;
3732 default:
3733 break;
3734 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003735
Hardening57388e42013-09-18 23:56:36 +02003736 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003737 output->width /= scale;
3738 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003739}
3740
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003741static void
3742weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003743{
3744 output->x = x;
3745 output->y = y;
3746
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003747 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003748 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003749 output->width,
3750 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003751}
3752
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003753WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003754weston_output_move(struct weston_output *output, int x, int y)
3755{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003756 struct wl_resource *resource;
3757
3758 output->move_x = x - output->x;
3759 output->move_y = y - output->y;
3760
3761 if (output->move_x == 0 && output->move_y == 0)
3762 return;
3763
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003764 weston_output_init_geometry(output, x, y);
3765
3766 output->dirty = 1;
3767
3768 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003769 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003770
3771 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003772 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003773 wl_output_send_geometry(resource,
3774 output->x,
3775 output->y,
3776 output->mm_width,
3777 output->mm_height,
3778 output->subpixel,
3779 output->make,
3780 output->model,
3781 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003782
3783 if (wl_resource_get_version(resource) >= 2)
3784 wl_output_send_done(resource);
3785 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003786}
3787
3788WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003789weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003790 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003791 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003792{
3793 output->compositor = c;
3794 output->x = x;
3795 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003796 output->mm_width = mm_width;
3797 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003798 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003799 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003800
Alexander Larsson0b135062013-05-28 16:23:36 +02003801 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003802 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003803
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003804 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003805 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003806
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003807 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003808 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003809 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003810 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003811 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003812
Casey Dahlin58ba1372012-04-19 22:50:08 -04003813 output->id = ffs(~output->compositor->output_id_pool) - 1;
3814 output->compositor->output_id_pool |= 1 << output->id;
3815
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003816 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003817 wl_global_create(c->wl_display, &wl_output_interface, 2,
3818 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003819 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003820}
3821
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003822WL_EXPORT void
3823weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003824 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003825 wl_fixed_t *x, wl_fixed_t *y)
3826{
3827 wl_fixed_t tx, ty;
3828 wl_fixed_t width, height;
Neil Robertseb5a2002014-05-01 16:13:55 +01003829 float zoom_scale, zx, zy;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003830
Hardeningff39efa2013-09-18 23:56:35 +02003831 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3832 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003833
3834 switch(output->transform) {
3835 case WL_OUTPUT_TRANSFORM_NORMAL:
3836 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003837 tx = device_x;
3838 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003839 break;
3840 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003841 tx = device_y;
3842 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003843 break;
3844 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003845 tx = width - device_x;
3846 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003847 break;
3848 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003849 tx = width - device_y;
3850 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003851 break;
3852 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003853 tx = width - device_x;
3854 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003855 break;
3856 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003857 tx = width - device_y;
3858 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003859 break;
3860 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003861 tx = device_x;
3862 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003863 break;
3864 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003865 tx = device_y;
3866 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003867 break;
3868 }
3869
Neil Robertseb5a2002014-05-01 16:13:55 +01003870 tx /= output->current_scale;
3871 ty /= output->current_scale;
3872
3873 if (output->zoom.active) {
3874 zoom_scale = output->zoom.spring_z.current;
3875 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3876 output->width / 2.0f *
3877 (zoom_scale + output->zoom.trans_x));
3878 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3879 output->height / 2.0f *
3880 (zoom_scale + output->zoom.trans_y));
3881 tx = wl_fixed_from_double(zx);
3882 ty = wl_fixed_from_double(zy);
3883 }
3884
3885 *x = tx + wl_fixed_from_int(output->x);
3886 *y = ty + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003887}
3888
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003889static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003890destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003891{
Jonny Lamb74130762013-11-26 18:19:46 +01003892 struct weston_surface *surface =
3893 wl_resource_get_user_data(resource);
3894
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003895 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003896 surface->pending.buffer_viewport.buffer.src_width =
3897 wl_fixed_from_int(-1);
3898 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003899 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003900}
3901
3902static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003903viewport_destroy(struct wl_client *client,
3904 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003905{
3906 wl_resource_destroy(resource);
3907}
3908
3909static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003910viewport_set(struct wl_client *client,
3911 struct wl_resource *resource,
3912 wl_fixed_t src_x,
3913 wl_fixed_t src_y,
3914 wl_fixed_t src_width,
3915 wl_fixed_t src_height,
3916 int32_t dst_width,
3917 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003918{
Jonny Lamb74130762013-11-26 18:19:46 +01003919 struct weston_surface *surface =
3920 wl_resource_get_user_data(resource);
3921
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003922 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003923
Jonny Lamb8ae35902013-11-26 18:19:45 +01003924 if (wl_fixed_to_double(src_width) < 0 ||
3925 wl_fixed_to_double(src_height) < 0) {
3926 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003927 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003928 "source dimensions must be non-negative (%fx%f)",
3929 wl_fixed_to_double(src_width),
3930 wl_fixed_to_double(src_height));
3931 return;
3932 }
3933
3934 if (dst_width <= 0 || dst_height <= 0) {
3935 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003936 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003937 "destination dimensions must be positive (%dx%d)",
3938 dst_width, dst_height);
3939 return;
3940 }
3941
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003942 surface->pending.buffer_viewport.buffer.src_x = src_x;
3943 surface->pending.buffer_viewport.buffer.src_y = src_y;
3944 surface->pending.buffer_viewport.buffer.src_width = src_width;
3945 surface->pending.buffer_viewport.buffer.src_height = src_height;
3946 surface->pending.buffer_viewport.surface.width = dst_width;
3947 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003948 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003949}
3950
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003951static void
3952viewport_set_source(struct wl_client *client,
3953 struct wl_resource *resource,
3954 wl_fixed_t src_x,
3955 wl_fixed_t src_y,
3956 wl_fixed_t src_width,
3957 wl_fixed_t src_height)
3958{
3959 struct weston_surface *surface =
3960 wl_resource_get_user_data(resource);
3961
3962 assert(surface->viewport_resource != NULL);
3963
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003964 if (src_width == wl_fixed_from_int(-1) &&
3965 src_height == wl_fixed_from_int(-1)) {
3966 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003967 surface->pending.buffer_viewport.buffer.src_width =
3968 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003969 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003970 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003971 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003972
3973 if (src_width <= 0 || src_height <= 0) {
3974 wl_resource_post_error(resource,
3975 WL_VIEWPORT_ERROR_BAD_VALUE,
3976 "source size must be positive (%fx%f)",
3977 wl_fixed_to_double(src_width),
3978 wl_fixed_to_double(src_height));
3979 return;
3980 }
3981
3982 surface->pending.buffer_viewport.buffer.src_x = src_x;
3983 surface->pending.buffer_viewport.buffer.src_y = src_y;
3984 surface->pending.buffer_viewport.buffer.src_width = src_width;
3985 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003986 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003987}
3988
3989static void
3990viewport_set_destination(struct wl_client *client,
3991 struct wl_resource *resource,
3992 int32_t dst_width,
3993 int32_t dst_height)
3994{
3995 struct weston_surface *surface =
3996 wl_resource_get_user_data(resource);
3997
3998 assert(surface->viewport_resource != NULL);
3999
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004000 if (dst_width == -1 && dst_height == -1) {
4001 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004002 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004003 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004004 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004005 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004006
4007 if (dst_width <= 0 || dst_height <= 0) {
4008 wl_resource_post_error(resource,
4009 WL_VIEWPORT_ERROR_BAD_VALUE,
4010 "destination size must be positive (%dx%d)",
4011 dst_width, dst_height);
4012 return;
4013 }
4014
4015 surface->pending.buffer_viewport.surface.width = dst_width;
4016 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004017 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004018}
4019
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004020static const struct wl_viewport_interface viewport_interface = {
4021 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004022 viewport_set,
4023 viewport_set_source,
4024 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004025};
4026
4027static void
4028scaler_destroy(struct wl_client *client,
4029 struct wl_resource *resource)
4030{
4031 wl_resource_destroy(resource);
4032}
4033
4034static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004035scaler_get_viewport(struct wl_client *client,
4036 struct wl_resource *scaler,
4037 uint32_t id,
4038 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004039{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004040 int version = wl_resource_get_version(scaler);
4041 struct weston_surface *surface =
4042 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004043 struct wl_resource *resource;
4044
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004045 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01004046 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004047 WL_SCALER_ERROR_VIEWPORT_EXISTS,
4048 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004049 return;
4050 }
4051
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004052 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004053 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004054 if (resource == NULL) {
4055 wl_client_post_no_memory(client);
4056 return;
4057 }
4058
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004059 wl_resource_set_implementation(resource, &viewport_interface,
4060 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004061
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004062 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004063}
4064
4065static const struct wl_scaler_interface scaler_interface = {
4066 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004067 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004068};
4069
4070static void
4071bind_scaler(struct wl_client *client,
4072 void *data, uint32_t version, uint32_t id)
4073{
4074 struct wl_resource *resource;
4075
4076 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004077 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004078 if (resource == NULL) {
4079 wl_client_post_no_memory(client);
4080 return;
4081 }
4082
4083 wl_resource_set_implementation(resource, &scaler_interface,
4084 NULL, NULL);
4085}
4086
4087static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004088destroy_presentation_feedback(struct wl_resource *feedback_resource)
4089{
4090 struct weston_presentation_feedback *feedback;
4091
4092 feedback = wl_resource_get_user_data(feedback_resource);
4093
4094 wl_list_remove(&feedback->link);
4095 free(feedback);
4096}
4097
4098static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004099presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4100{
4101 wl_resource_destroy(resource);
4102}
4103
4104static void
4105presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004106 struct wl_resource *presentation_resource,
4107 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004108 uint32_t callback)
4109{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004110 struct weston_surface *surface;
4111 struct weston_presentation_feedback *feedback;
4112
4113 surface = wl_resource_get_user_data(surface_resource);
4114
Bryce Harringtonde16d892014-11-20 22:21:57 -08004115 feedback = zalloc(sizeof *feedback);
4116 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004117 goto err_calloc;
4118
4119 feedback->resource = wl_resource_create(client,
4120 &presentation_feedback_interface,
4121 1, callback);
4122 if (!feedback->resource)
4123 goto err_create;
4124
4125 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4126 destroy_presentation_feedback);
4127
4128 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4129
4130 return;
4131
4132err_create:
4133 free(feedback);
4134
4135err_calloc:
4136 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004137}
4138
4139static const struct presentation_interface presentation_implementation = {
4140 presentation_destroy,
4141 presentation_feedback
4142};
4143
4144static void
4145bind_presentation(struct wl_client *client,
4146 void *data, uint32_t version, uint32_t id)
4147{
4148 struct weston_compositor *compositor = data;
4149 struct wl_resource *resource;
4150
4151 resource = wl_resource_create(client, &presentation_interface,
4152 MIN(version, 1), id);
4153 if (resource == NULL) {
4154 wl_client_post_no_memory(client);
4155 return;
4156 }
4157
4158 wl_resource_set_implementation(resource, &presentation_implementation,
4159 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004160 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004161}
4162
4163static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004164compositor_bind(struct wl_client *client,
4165 void *data, uint32_t version, uint32_t id)
4166{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004167 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004168 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004169
Jason Ekstranda85118c2013-06-27 20:17:02 -05004170 resource = wl_resource_create(client, &wl_compositor_interface,
4171 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004172 if (resource == NULL) {
4173 wl_client_post_no_memory(client);
4174 return;
4175 }
4176
4177 wl_resource_set_implementation(resource, &compositor_interface,
4178 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004179}
4180
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04004181static void
Martin Minarikf12c2872012-06-11 00:57:39 +02004182log_uname(void)
4183{
4184 struct utsname usys;
4185
4186 uname(&usys);
4187
4188 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
4189 usys.version, usys.machine);
4190}
4191
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004192WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004193weston_environment_get_fd(const char *env)
4194{
4195 char *e, *end;
4196 int fd, flags;
4197
4198 e = getenv(env);
4199 if (!e)
4200 return -1;
4201 fd = strtol(e, &end, 0);
4202 if (*end != '\0')
4203 return -1;
4204
4205 flags = fcntl(fd, F_GETFD);
4206 if (flags == -1)
4207 return -1;
4208
4209 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4210 unsetenv(env);
4211
4212 return fd;
4213}
4214
Pekka Paalanenb5026542014-11-12 15:09:24 +02004215static void
4216timeline_key_binding_handler(struct weston_seat *seat, uint32_t time,
4217 uint32_t key, void *data)
4218{
4219 struct weston_compositor *compositor = data;
4220
4221 if (weston_timeline_enabled_)
4222 weston_timeline_close();
4223 else
4224 weston_timeline_open(compositor);
4225}
4226
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004227WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04004228weston_compositor_init(struct weston_compositor *ec,
4229 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004230 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004231 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004232{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004233 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01004234 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004235 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004236
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004237 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04004238 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004239 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004240 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004241 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004242 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004243 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004244 wl_signal_init(&ec->idle_signal);
4245 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004246 wl_signal_init(&ec->show_input_panel_signal);
4247 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004248 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004249 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004250 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004251 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004252 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004253 wl_signal_init(&ec->session_signal);
4254 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004255
Casey Dahlin58ba1372012-04-19 22:50:08 -04004256 ec->output_id_pool = 0;
4257
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004258 if (!wl_global_create(display, &wl_compositor_interface, 3,
4259 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05004260 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004261
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004262 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
4263 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03004264 return -1;
4265
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004266 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004267 ec, bind_scaler))
4268 return -1;
4269
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004270 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4271 ec, bind_presentation))
4272 return -1;
4273
Jason Ekstranda7af7042013-10-12 22:38:11 -05004274 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004275 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004276 wl_list_init(&ec->layer_list);
4277 wl_list_init(&ec->seat_list);
4278 wl_list_init(&ec->output_list);
4279 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004280 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004281 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004282 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004283 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004284 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004285
Xiong Zhang97116532013-10-23 13:58:31 +08004286 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004287 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004288
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004289 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
4290 weston_config_section_get_string(s, "keymap_rules",
4291 (char **) &xkb_names.rules, NULL);
4292 weston_config_section_get_string(s, "keymap_model",
4293 (char **) &xkb_names.model, NULL);
4294 weston_config_section_get_string(s, "keymap_layout",
4295 (char **) &xkb_names.layout, NULL);
4296 weston_config_section_get_string(s, "keymap_variant",
4297 (char **) &xkb_names.variant, NULL);
4298 weston_config_section_get_string(s, "keymap_options",
4299 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01004300
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05004301 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
4302 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01004303
Jonny Lamb66a41a02014-08-12 14:58:25 +02004304 weston_config_section_get_int(s, "repeat-rate",
4305 &ec->kb_repeat_rate, 40);
4306 weston_config_section_get_int(s, "repeat-delay",
4307 &ec->kb_repeat_delay, 400);
4308
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004309 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01004310
4311 wl_data_device_manager_init(ec->wl_display);
4312
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04004313 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004314
Daniel Stone725c2c32012-06-22 14:04:36 +01004315 loop = wl_display_get_event_loop(ec->wl_display);
4316 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4317 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4318
4319 ec->input_loop = wl_event_loop_create();
4320
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004321 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4322 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4323
Pekka Paalanenb5026542014-11-12 15:09:24 +02004324 weston_compositor_add_debug_binding(ec, KEY_T,
4325 timeline_key_binding_handler, ec);
4326
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004327 weston_compositor_schedule_repaint(ec);
4328
Daniel Stone725c2c32012-06-22 14:04:36 +01004329 return 0;
4330}
4331
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004332WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004333weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004334{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004335 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004336
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004337 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004338 if (ec->input_loop_source)
4339 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004340
Matt Roper361d2ad2011-08-29 13:52:23 -07004341 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004342 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004343 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004344
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004345 if (ec->renderer)
4346 ec->renderer->destroy(ec);
4347
Daniel Stone325fc2d2012-05-30 16:31:58 +01004348 weston_binding_list_destroy_all(&ec->key_binding_list);
4349 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004350 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004351 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004352 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004353
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004354 weston_plane_release(&ec->primary_plane);
4355
Jonas Ådahlc97af922012-03-28 22:36:09 +02004356 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07004357
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004358 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07004359}
4360
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004361WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004362weston_compositor_exit_with_code(struct weston_compositor *compositor,
4363 int exit_code)
4364{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004365 if (compositor->exit_code == EXIT_SUCCESS)
4366 compositor->exit_code = exit_code;
4367
Frederic Plourdec336f062014-10-29 14:44:33 -04004368 wl_display_terminate(compositor->wl_display);
4369}
4370
4371WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004372weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4373 const struct weston_pointer_grab_interface *interface)
4374{
4375 struct weston_seat *seat;
4376
4377 ec->default_pointer_grab = interface;
4378 wl_list_for_each(seat, &ec->seat_list, link) {
4379 if (seat->pointer) {
4380 weston_pointer_set_default_grab(seat->pointer,
4381 interface);
4382 }
4383 }
4384}
4385
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004386WL_EXPORT int
4387weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4388 clockid_t clk_id)
4389{
4390 struct timespec ts;
4391
4392 if (clock_gettime(clk_id, &ts) < 0)
4393 return -1;
4394
4395 compositor->presentation_clock = clk_id;
4396
4397 return 0;
4398}
4399
4400/*
4401 * For choosing the software clock, when the display hardware or API
4402 * does not expose a compatible presentation timestamp.
4403 */
4404WL_EXPORT int
4405weston_compositor_set_presentation_clock_software(
4406 struct weston_compositor *compositor)
4407{
4408 /* In order of preference */
4409 static const clockid_t clocks[] = {
4410 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4411 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4412 CLOCK_MONOTONIC, /* no jumps, may crawl */
4413 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4414 CLOCK_REALTIME /* may jump and crawl */
4415 };
4416 unsigned i;
4417
4418 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4419 if (weston_compositor_set_presentation_clock(compositor,
4420 clocks[i]) == 0)
4421 return 0;
4422
4423 weston_log("Error: no suitable presentation clock available.\n");
4424
4425 return -1;
4426}
4427
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004428WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004429weston_version(int *major, int *minor, int *micro)
4430{
4431 *major = WESTON_VERSION_MAJOR;
4432 *minor = WESTON_VERSION_MINOR;
4433 *micro = WESTON_VERSION_MICRO;
4434}
4435
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004436static const char *
4437clock_name(clockid_t clk_id)
4438{
4439 static const char *names[] = {
4440 [CLOCK_REALTIME] = "CLOCK_REALTIME",
4441 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
4442 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
4443 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
4444 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
4445 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
4446 };
4447
4448 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
4449 return "unknown";
4450
4451 return names[clk_id];
4452}
4453
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004454static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004455 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004456 const char *desc;
4457} capability_strings[] = {
4458 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004459 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004460};
4461
4462static void
4463weston_compositor_log_capabilities(struct weston_compositor *compositor)
4464{
4465 unsigned i;
4466 int yes;
4467
4468 weston_log("Compositor capabilities:\n");
4469 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
4470 yes = compositor->capabilities & capability_strings[i].bit;
4471 weston_log_continue(STAMP_SPACE "%s %s\n",
4472 capability_strings[i].desc,
4473 yes ? "yes" : "no");
4474 }
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004475
4476 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
4477 clock_name(compositor->presentation_clock),
4478 compositor->presentation_clock);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004479}
4480
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004481static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004482{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004483 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004484
Martin Minarik6d118362012-06-07 18:01:59 +02004485 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004486 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004487
4488 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004489}
4490
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004491#ifdef HAVE_LIBUNWIND
4492
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004493static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004494print_backtrace(void)
4495{
4496 unw_cursor_t cursor;
4497 unw_context_t context;
4498 unw_word_t off;
4499 unw_proc_info_t pip;
4500 int ret, i = 0;
4501 char procname[256];
4502 const char *filename;
4503 Dl_info dlinfo;
4504
4505 pip.unwind_info = NULL;
4506 ret = unw_getcontext(&context);
4507 if (ret) {
4508 weston_log("unw_getcontext: %d\n", ret);
4509 return;
4510 }
4511
4512 ret = unw_init_local(&cursor, &context);
4513 if (ret) {
4514 weston_log("unw_init_local: %d\n", ret);
4515 return;
4516 }
4517
4518 ret = unw_step(&cursor);
4519 while (ret > 0) {
4520 ret = unw_get_proc_info(&cursor, &pip);
4521 if (ret) {
4522 weston_log("unw_get_proc_info: %d\n", ret);
4523 break;
4524 }
4525
4526 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4527 if (ret && ret != -UNW_ENOMEM) {
4528 if (ret != -UNW_EUNSPEC)
4529 weston_log("unw_get_proc_name: %d\n", ret);
4530 procname[0] = '?';
4531 procname[1] = 0;
4532 }
4533
4534 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4535 *dlinfo.dli_fname)
4536 filename = dlinfo.dli_fname;
4537 else
4538 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004539
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004540 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4541 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4542
4543 ret = unw_step(&cursor);
4544 if (ret < 0)
4545 weston_log("unw_step: %d\n", ret);
4546 }
4547}
4548
4549#else
4550
4551static void
4552print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004553{
4554 void *buffer[32];
4555 int i, count;
4556 Dl_info info;
4557
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004558 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4559 for (i = 0; i < count; i++) {
4560 dladdr(buffer[i], &info);
4561 weston_log(" [%016lx] %s (%s)\n",
4562 (long) buffer[i],
4563 info.dli_sname ? info.dli_sname : "--",
4564 info.dli_fname);
4565 }
4566}
4567
4568#endif
4569
4570static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004571on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004572{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004573 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004574 * then call the backend restore function, which will switch
4575 * back to the vt we launched from or ungrab X etc and then
4576 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004577 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004578 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004579 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004580
Peter Maatmane5b42e42013-03-27 22:38:53 +01004581 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004582
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004583 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004584
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004585 segv_compositor->restore(segv_compositor);
4586
4587 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004588}
4589
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004590WL_EXPORT void *
4591weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004592{
4593 char path[PATH_MAX];
4594 void *module, *init;
4595
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004596 if (name == NULL)
4597 return NULL;
4598
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004599 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004600 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004601 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004602 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004603
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004604 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4605 if (module) {
4606 weston_log("Module '%s' already loaded\n", path);
4607 dlclose(module);
4608 return NULL;
4609 }
4610
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004611 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004612 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004613 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004614 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004615 return NULL;
4616 }
4617
4618 init = dlsym(module, entrypoint);
4619 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004620 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004621 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004622 return NULL;
4623 }
4624
4625 return init;
4626}
4627
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004628static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004629load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004630 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004631{
4632 const char *p, *end;
4633 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004634 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004635 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004636
4637 if (modules == NULL)
4638 return 0;
4639
4640 p = modules;
4641 while (*p) {
4642 end = strchrnul(p, ',');
4643 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004644 module_init = weston_load_module(buffer, "module_init");
Ondřej Majerech01e98b62014-12-06 02:49:17 +01004645 if (!module_init)
4646 return -1;
4647 if (module_init(ec, argc, argv) < 0)
4648 return -1;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004649 p = end;
4650 while (*p == ',')
4651 p++;
4652
4653 }
4654
4655 return 0;
4656}
4657
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004658static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004659 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4660
4661static const char xdg_wrong_message[] =
4662 "fatal: environment variable XDG_RUNTIME_DIR\n"
4663 "is set to \"%s\", which is not a directory.\n";
4664
4665static const char xdg_wrong_mode_message[] =
4666 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004667 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4668 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004669
4670static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004671 "Refer to your distribution on how to get it, or\n"
4672 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4673 "on how to implement it.\n";
4674
Martin Minarik37032f82012-06-18 20:15:18 +02004675static void
4676verify_xdg_runtime_dir(void)
4677{
4678 char *dir = getenv("XDG_RUNTIME_DIR");
4679 struct stat s;
4680
4681 if (!dir) {
4682 weston_log(xdg_error_message);
4683 weston_log_continue(xdg_detail_message);
4684 exit(EXIT_FAILURE);
4685 }
4686
4687 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4688 weston_log(xdg_wrong_message, dir);
4689 weston_log_continue(xdg_detail_message);
4690 exit(EXIT_FAILURE);
4691 }
4692
4693 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4694 weston_log(xdg_wrong_mode_message,
4695 dir, s.st_mode & 0777, s.st_uid);
4696 weston_log_continue(xdg_detail_message);
4697 }
4698}
4699
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004700static int
4701usage(int error_code)
4702{
4703 fprintf(stderr,
4704 "Usage: weston [OPTIONS]\n\n"
4705 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4706 "Weston supports multiple backends, and depending on which backend is in use\n"
4707 "different options will be accepted.\n\n"
4708
4709
4710 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004711 " --version\t\tPrint weston version\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004712 " -B, --backend=MODULE\tBackend module, one of\n"
4713#if defined(BUILD_DRM_COMPOSITOR)
4714 "\t\t\t\tdrm-backend.so\n"
4715#endif
4716#if defined(BUILD_FBDEV_COMPOSITOR)
Pekka Paalanen86b70e12014-11-11 14:10:46 +02004717 "\t\t\t\tfbdev-backend.so\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004718#endif
4719#if defined(BUILD_X11_COMPOSITOR)
4720 "\t\t\t\tx11-backend.so\n"
4721#endif
4722#if defined(BUILD_WAYLAND_COMPOSITOR)
4723 "\t\t\t\twayland-backend.so\n"
4724#endif
4725#if defined(BUILD_RDP_COMPOSITOR)
4726 "\t\t\t\trdp-backend.so\n"
4727#endif
4728#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4729 "\t\t\t\trpi-backend.so\n"
4730#endif
Jason Ekstranda985da42013-08-22 17:28:03 -05004731 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004732 " -S, --socket=NAME\tName of socket to listen on\n"
4733 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004734 " --modules\t\tLoad the comma-separated list of modules\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004735 " --log=FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004736 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004737 " -h, --help\t\tThis help message\n\n");
4738
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004739#if defined(BUILD_DRM_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004740 fprintf(stderr,
4741 "Options for drm-backend.so:\n\n"
4742 " --connector=ID\tBring up only this connector\n"
4743 " --seat=SEAT\t\tThe seat that weston should run on\n"
4744 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004745 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004746 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004747#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004748
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004749#if defined(BUILD_FBDEV_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004750 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004751 "Options for fbdev-backend.so:\n\n"
4752 " --tty=TTY\t\tThe tty to use\n"
4753 " --device=DEVICE\tThe framebuffer device to use\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004754#endif
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004755
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004756#if defined(BUILD_X11_COMPOSITOR)
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004757 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004758 "Options for x11-backend.so:\n\n"
4759 " --width=WIDTH\t\tWidth of X window\n"
4760 " --height=HEIGHT\tHeight of X window\n"
4761 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004762 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004763 " --output-count=COUNT\tCreate multiple outputs\n"
4764 " --no-input\t\tDont create input devices\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004765#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004766
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004767#if defined(BUILD_WAYLAND_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004768 fprintf(stderr,
4769 "Options for wayland-backend.so:\n\n"
4770 " --width=WIDTH\t\tWidth of Wayland surface\n"
4771 " --height=HEIGHT\tHeight of Wayland surface\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004772 " --scale=SCALE\t\tScale factor of output\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004773 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004774 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004775 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004776 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004777 " --display=DISPLAY\tWayland display to connect to\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004778#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004779
Pekka Paalanene31e0532013-05-22 18:03:07 +03004780#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4781 fprintf(stderr,
4782 "Options for rpi-backend.so:\n\n"
4783 " --tty=TTY\t\tThe tty to use\n"
4784 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4785 " --transform=TR\tThe output transformation, TR is one of:\n"
4786 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004787 " --opaque-regions\tEnable support for opaque regions, can be "
4788 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004789 "\n");
4790#endif
4791
Hardeningc077a842013-07-08 00:51:35 +02004792#if defined(BUILD_RDP_COMPOSITOR)
Bryce Harrington59fe4232014-10-23 14:44:34 -07004793 fprintf(stderr,
4794 "Options for rdp-backend.so:\n\n"
4795 " --width=WIDTH\t\tWidth of desktop\n"
4796 " --height=HEIGHT\tHeight of desktop\n"
4797 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4798 " --address=ADDR\tThe address to bind\n"
Bryce Harringtonf5b34ae2014-10-23 14:44:35 -07004799 " --port=PORT\t\tThe port to listen on\n"
Bryce Harrington59fe4232014-10-23 14:44:34 -07004800 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
4801 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4802 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4803 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4804 "\n");
Hardeningc077a842013-07-08 00:51:35 +02004805#endif
4806
Bryce Harrington3e3b59e2014-11-19 15:06:19 -08004807#if defined(BUILD_HEADLESS_COMPOSITOR)
4808 fprintf(stderr,
4809 "Options for headless-backend.so:\n\n"
4810 " --width=WIDTH\t\tWidth of memory surface\n"
4811 " --height=HEIGHT\tHeight of memory surface\n"
4812 " --transform=TR\tThe output transformation, TR is one of:\n"
4813 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
4814 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
4815#endif
4816
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004817 exit(error_code);
4818}
4819
Peter Maatmane5b42e42013-03-27 22:38:53 +01004820static void
4821catch_signals(void)
4822{
4823 struct sigaction action;
4824
4825 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4826 action.sa_sigaction = on_caught_signal;
4827 sigemptyset(&action.sa_mask);
4828 sigaction(SIGSEGV, &action, NULL);
4829 sigaction(SIGABRT, &action, NULL);
4830}
4831
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004832static void
4833handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4834{
4835 struct wl_client *client = data;
4836
4837 weston_log("Primary client died. Closing...\n");
4838
4839 wl_display_terminate(wl_client_get_display(client));
4840}
4841
Ryo Munakatad8deff62014-09-06 07:32:05 +09004842static char *
4843weston_choose_default_backend(void)
4844{
4845 char *backend = NULL;
4846
4847 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
4848 backend = strdup("wayland-backend.so");
4849 else if (getenv("DISPLAY"))
4850 backend = strdup("x11-backend.so");
4851 else
4852 backend = strdup(WESTON_NATIVE_BACKEND);
4853
4854 return backend;
4855}
4856
4857static int
4858weston_create_listening_socket(struct wl_display *display, const char *socket_name)
4859{
4860 if (socket_name) {
4861 if (wl_display_add_socket(display, socket_name)) {
4862 weston_log("fatal: failed to add socket: %m\n");
4863 return -1;
4864 }
4865 } else {
4866 socket_name = wl_display_add_socket_auto(display);
4867 if (!socket_name) {
4868 weston_log("fatal: failed to add socket: %m\n");
4869 return -1;
4870 }
4871 }
4872
4873 setenv("WAYLAND_DISPLAY", socket_name, 1);
4874
4875 return 0;
4876}
4877
Derek Foreman64a3df02014-10-23 12:24:18 -05004878static const struct { const char *name; uint32_t token; } transforms[] = {
4879 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
4880 { "90", WL_OUTPUT_TRANSFORM_90 },
4881 { "180", WL_OUTPUT_TRANSFORM_180 },
4882 { "270", WL_OUTPUT_TRANSFORM_270 },
4883 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
4884 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
4885 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
4886 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
4887};
4888
4889WL_EXPORT int
4890weston_parse_transform(const char *transform, uint32_t *out)
4891{
4892 unsigned int i;
4893
4894 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
4895 if (strcmp(transforms[i].name, transform) == 0) {
4896 *out = transforms[i].token;
4897 return 0;
4898 }
4899
4900 *out = WL_OUTPUT_TRANSFORM_NORMAL;
4901 return -1;
4902}
4903
4904WL_EXPORT const char *
4905weston_transform_to_string(uint32_t output_transform)
4906{
4907 unsigned int i;
4908
4909 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
4910 if (transforms[i].token == output_transform)
4911 return transforms[i].name;
4912
4913 return "<illegal value>";
4914}
4915
4916
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004917int main(int argc, char *argv[])
4918{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004919 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004920 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004921 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004922 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004923 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004924 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004925 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004926 int *argc, char *argv[],
4927 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004928 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004929 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004930 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02004931 char *modules = NULL;
4932 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004933 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004934 char *server_socket = NULL, *end;
Frederic Plourde4a84c832014-10-30 15:06:34 -04004935 int32_t idle_time = -1;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004936 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09004937 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06004938 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004939 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004940 int32_t numlock_on;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004941 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004942 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004943 struct wl_client *primary_client;
4944 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004945 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004946
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004947 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09004948 { WESTON_OPTION_STRING, "backend", 'B', &backend },
4949 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004950 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4951 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004952 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004953 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004954 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004955 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03004956 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004957 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004958
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004959 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004960
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004961 if (help)
4962 usage(EXIT_SUCCESS);
4963
Scott Moreau12245142012-08-29 15:15:58 -06004964 if (version) {
4965 printf(PACKAGE_STRING "\n");
4966 return EXIT_SUCCESS;
4967 }
4968
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004969 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004970
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004971 weston_log("%s\n"
4972 STAMP_SPACE "%s\n"
4973 STAMP_SPACE "Bug reports to: %s\n"
4974 STAMP_SPACE "Build: %s\n",
4975 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004976 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004977 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004978
Martin Minarik37032f82012-06-18 20:15:18 +02004979 verify_xdg_runtime_dir();
4980
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004981 display = wl_display_create();
4982
Tiago Vignatti2116b892011-08-08 05:52:59 -07004983 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004984 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4985 display);
4986 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4987 display);
4988 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4989 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004990
4991 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004992 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4993 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004994
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304995 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
4996 ret = EXIT_FAILURE;
4997 goto out_signals;
4998 }
4999
Pekka Paalanen588bee12014-05-07 16:26:25 +03005000 if (noconfig == 0)
5001 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01005002 if (config != NULL) {
5003 weston_log("Using config file '%s'\n",
5004 weston_config_get_full_path(config));
5005 } else {
5006 weston_log("Starting with no config file.\n");
5007 }
5008 section = weston_config_get_section(config, "core", NULL, NULL);
5009
Ryo Munakata03faed22014-09-06 07:32:51 +09005010 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005011 weston_config_section_get_string(section, "backend", &backend,
5012 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09005013 if (!backend)
5014 backend = weston_choose_default_backend();
5015 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005016
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03005017 backend_init = weston_load_module(backend, "backend_init");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305018 if (!backend_init) {
5019 ret = EXIT_FAILURE;
5020 goto out_signals;
5021 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05005022
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005023 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05005024 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03005025 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305026 ret = EXIT_FAILURE;
5027 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05005028 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04005029
Peter Maatmane5b42e42013-03-27 22:38:53 +01005030 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04005031 segv_compositor = ec;
5032
Frederic Plourde4a84c832014-10-30 15:06:34 -04005033 if (idle_time < 0)
5034 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
5035 if (idle_time < 0)
5036 idle_time = 300; /* default idle timeout, in seconds */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005037 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005038 ec->default_pointer_grab = NULL;
Frederic Plourdec336f062014-10-29 14:44:33 -04005039 ec->exit_code = EXIT_SUCCESS;
Pekka Paalanen7296e792011-12-07 16:22:00 +02005040
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005041 for (i = 1; i < argc; i++)
5042 weston_log("fatal: unhandled option: %s\n", argv[i]);
5043 if (argc > 1) {
5044 ret = EXIT_FAILURE;
5045 goto out;
5046 }
5047
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005048 weston_compositor_log_capabilities(ec);
5049
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005050 server_socket = getenv("WAYLAND_SERVER_SOCKET");
5051 if (server_socket) {
5052 weston_log("Running with single client\n");
5053 fd = strtol(server_socket, &end, 0);
5054 if (*end != '\0')
5055 fd = -1;
5056 } else {
5057 fd = -1;
5058 }
5059
5060 if (fd != -1) {
5061 primary_client = wl_client_create(display, fd);
5062 if (!primary_client) {
5063 weston_log("fatal: failed to add client: %m\n");
5064 ret = EXIT_FAILURE;
5065 goto out;
5066 }
5067 primary_client_destroyed.notify =
5068 handle_primary_client_destroyed;
5069 wl_client_add_destroy_listener(primary_client,
5070 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09005071 } else if (weston_create_listening_socket(display, socket_name)) {
5072 ret = EXIT_FAILURE;
5073 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005074 }
5075
Ryo Munakata03faed22014-09-06 07:32:51 +09005076 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005077 weston_config_section_get_string(section, "shell", &shell,
5078 "desktop-shell.so");
5079
Ryo Munakata03faed22014-09-06 07:32:51 +09005080 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005081 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005082
5083 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09005084 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005085 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005086
5087 if (load_modules(ec, option_modules, &argc, argv) < 0)
5088 goto out;
5089
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005090 section = weston_config_get_section(config, "keyboard", NULL, NULL);
5091 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
5092 if (numlock_on) {
5093 wl_list_for_each(seat, &ec->seat_list, link) {
5094 if (seat->keyboard)
5095 weston_keyboard_set_locks(seat->keyboard,
5096 WESTON_NUM_LOCK,
5097 WESTON_NUM_LOCK);
5098 }
5099 }
5100
Pekka Paalanenc0444e32012-01-05 16:28:21 +02005101 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005102
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04005103 wl_display_run(display);
5104
Frederic Plourdec336f062014-10-29 14:44:33 -04005105 /* Allow for setting return exit code after
5106 * wl_display_run returns normally. This is
5107 * useful for devs/testers and automated tests
5108 * that want to indicate failure status to
5109 * testing infrastructure above
5110 */
5111 ret = ec->exit_code;
5112
Ryo Munakata03faed22014-09-06 07:32:51 +09005113out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005114 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005115 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005116
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005117 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005118
Daniel Stone855028f2012-05-01 20:37:10 +01005119 weston_compositor_xkb_destroy(ec);
5120
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005121 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305122
5123out_signals:
5124 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
5125 if (signals[i])
5126 wl_event_source_remove(signals[i]);
5127
Tiago Vignatti9e2be082011-12-19 00:04:46 +02005128 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005129
Martin Minarik19e6f262012-06-07 13:08:46 +02005130 weston_log_file_close();
5131
Ryo Munakata03faed22014-09-06 07:32:51 +09005132 free(backend);
5133 free(shell);
5134 free(socket_name);
5135 free(option_modules);
5136 free(log);
5137 free(modules);
5138
Pekka Paalanen3ab72692012-06-08 17:27:28 +03005139 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04005140}