blob: 7c229b696abee3cca8904ba3f0231af0b254dd7e [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
Pekka Paalanen0513a952014-05-21 16:17:27 +030065#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
66
67#define NSEC_PER_SEC 1000000000
68
69static void
70timespec_sub(struct timespec *r,
71 const struct timespec *a, const struct timespec *b)
72{
73 r->tv_sec = a->tv_sec - b->tv_sec;
74 r->tv_nsec = a->tv_nsec - b->tv_nsec;
75 if (r->tv_nsec < 0) {
76 r->tv_sec--;
77 r->tv_nsec += NSEC_PER_SEC;
78 }
79}
80
81static int64_t
82timespec_to_nsec(const struct timespec *a)
83{
84 return (int64_t)a->tv_sec * NSEC_PER_SEC + a->tv_nsec;
85}
86
Kristian Høgsberg27da5382011-06-21 17:32:25 -040087static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040088static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040089
90static int
91sigchld_handler(int signal_number, void *data)
92{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050093 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040094 int status;
95 pid_t pid;
96
Pekka Paalanen23ade622014-08-27 13:31:26 +030097 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
98 wl_list_for_each(p, &child_process_list, link) {
99 if (p->pid == pid)
100 break;
101 }
Bill Spitzak027b9622012-03-17 15:22:03 -0700102
Pekka Paalanen23ade622014-08-27 13:31:26 +0300103 if (&p->link == &child_process_list) {
104 weston_log("unknown child process exited\n");
105 continue;
106 }
107
108 wl_list_remove(&p->link);
109 p->cleanup(p, status);
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400110 }
111
Pekka Paalanen23ade622014-08-27 13:31:26 +0300112 if (pid < 0 && errno != ECHILD)
113 weston_log("waitpid error %m\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400114
115 return 1;
116}
117
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200118static void
Alexander Larsson0b135062013-05-28 16:23:36 +0200119weston_output_transform_scale_init(struct weston_output *output,
120 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200121
Rob Bradford27b17932013-06-26 18:08:46 +0100122static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500123weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +0100124
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600125static void weston_mode_switch_finish(struct weston_output *output,
126 int mode_changed,
127 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800128{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200129 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +0200130 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200131 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -0600132 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +0200133
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200134 pixman_region32_init(&old_output_region);
135 pixman_region32_copy(&old_output_region, &output->region);
136
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200137 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200138 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200139
140 pixman_region32_init(&output->previous_damage);
141 pixman_region32_init_rect(&output->region, output->x, output->y,
142 output->width, output->height);
143
144 weston_output_update_matrix(output);
145
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200146 /* If a pointer falls outside the outputs new geometry, move it to its
147 * lower-right corner */
148 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400149 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200150 int32_t x, y;
151
152 if (!pointer)
153 continue;
154
155 x = wl_fixed_to_int(pointer->x);
156 y = wl_fixed_to_int(pointer->y);
157
158 if (!pixman_region32_contains_point(&old_output_region,
159 x, y, NULL) ||
160 pixman_region32_contains_point(&output->region,
161 x, y, NULL))
162 continue;
163
164 if (x >= output->x + output->width)
165 x = output->x + output->width - 1;
166 if (y >= output->y + output->height)
167 y = output->y + output->height - 1;
168
169 pointer->x = wl_fixed_from_int(x);
170 pointer->y = wl_fixed_from_int(y);
171 }
172
173 pixman_region32_fini(&old_output_region);
174
Derek Foremandd4cd332014-11-10 10:29:59 -0600175 if (!mode_changed && !scale_changed)
176 return;
177
Hardening57388e42013-09-18 23:56:36 +0200178 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600179 wl_resource_for_each(resource, &output->resource_list) {
180 if (mode_changed) {
181 wl_output_send_mode(resource,
182 output->current_mode->flags,
183 output->current_mode->width,
184 output->current_mode->height,
185 output->current_mode->refresh);
186 }
Hardening57388e42013-09-18 23:56:36 +0200187
Derek Foreman41bdc272014-11-05 13:26:57 -0600188 version = wl_resource_get_version(resource);
189 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600190 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200191
Derek Foreman41bdc272014-11-05 13:26:57 -0600192 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
193 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600194 }
195}
196
197WL_EXPORT int
198weston_output_mode_set_native(struct weston_output *output,
199 struct weston_mode *mode,
200 int32_t scale)
201{
202 int ret;
203 int mode_changed = 0, scale_changed = 0;
204
205 if (!output->switch_mode)
206 return -1;
207
208 if (!output->original_mode) {
209 mode_changed = 1;
210 ret = output->switch_mode(output, mode);
211 if (ret < 0)
212 return ret;
213 if (output->current_scale != scale) {
214 scale_changed = 1;
215 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200216 }
217 }
218
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600219 output->native_mode = mode;
220 output->native_scale = scale;
221
222 weston_mode_switch_finish(output, mode_changed, scale_changed);
223
224 return 0;
225}
226
227WL_EXPORT int
228weston_output_mode_switch_to_native(struct weston_output *output)
229{
230 int ret;
231 int mode_changed = 0, scale_changed = 0;
232
233 if (!output->switch_mode)
234 return -1;
235
236 if (!output->original_mode) {
237 weston_log("already in the native mode\n");
238 return -1;
239 }
240 /* the non fullscreen clients haven't seen a mode set since we
241 * switched into a temporary, so we need to notify them if the
242 * mode at that time is different from the native mode now.
243 */
244 mode_changed = (output->original_mode != output->native_mode);
245 scale_changed = (output->original_scale != output->native_scale);
246
247 ret = output->switch_mode(output, output->native_mode);
248 if (ret < 0)
249 return ret;
250
251 output->current_scale = output->native_scale;
252
253 output->original_mode = NULL;
254 output->original_scale = 0;
255
256 weston_mode_switch_finish(output, mode_changed, scale_changed);
257
258 return 0;
259}
260
261WL_EXPORT int
262weston_output_mode_switch_to_temporary(struct weston_output *output,
263 struct weston_mode *mode,
264 int32_t scale)
265{
266 int ret;
267
268 if (!output->switch_mode)
269 return -1;
270
271 /* original_mode is the last mode non full screen clients have seen,
272 * so we shouldn't change it if we already have one set.
273 */
274 if (!output->original_mode) {
275 output->original_mode = output->native_mode;
276 output->original_scale = output->native_scale;
277 }
278 ret = output->switch_mode(output, mode);
279 if (ret < 0)
280 return ret;
281
282 output->current_scale = scale;
283
284 weston_mode_switch_finish(output, 0, 0);
285
286 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800287}
288
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400289WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500290weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400291{
292 wl_list_insert(&child_process_list, &process->link);
293}
294
Benjamin Franzke06286262011-05-06 19:12:33 +0200295static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200296child_client_exec(int sockfd, const char *path)
297{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500298 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200299 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200300 sigset_t allsigs;
301
302 /* do not give our signal mask to the new process */
303 sigfillset(&allsigs);
304 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200305
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100306 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
307 if (seteuid(getuid()) == -1) {
308 weston_log("compositor: failed seteuid\n");
309 return;
310 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500311
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500312 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
313 * non-CLOEXEC fd to pass through exec. */
314 clientfd = dup(sockfd);
315 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200316 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500317 return;
318 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200319
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500320 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200321 setenv("WAYLAND_SOCKET", s, 1);
322
323 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200324 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200325 path);
326}
327
328WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500329weston_client_launch(struct weston_compositor *compositor,
330 struct weston_process *proc,
331 const char *path,
332 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200333{
334 int sv[2];
335 pid_t pid;
336 struct wl_client *client;
337
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300338 weston_log("launching '%s'\n", path);
339
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300340 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200341 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200342 "socketpair failed while launching '%s': %m\n",
343 path);
344 return NULL;
345 }
346
347 pid = fork();
348 if (pid == -1) {
349 close(sv[0]);
350 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200351 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200352 "fork failed while launching '%s': %m\n", path);
353 return NULL;
354 }
355
356 if (pid == 0) {
357 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700358 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200359 }
360
361 close(sv[1]);
362
363 client = wl_client_create(compositor->wl_display, sv[0]);
364 if (!client) {
365 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200366 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200367 "wl_client_create failed while launching '%s'.\n",
368 path);
369 return NULL;
370 }
371
372 proc->pid = pid;
373 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500374 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200375
376 return client;
377}
378
Pekka Paalanen9c1ac7b2014-08-27 12:03:38 +0300379struct process_info {
380 struct weston_process proc;
381 char *path;
382};
383
384static void
385process_handle_sigchld(struct weston_process *process, int status)
386{
387 struct process_info *pinfo =
388 container_of(process, struct process_info, proc);
389
390 /*
391 * There are no guarantees whether this runs before or after
392 * the wl_client destructor.
393 */
394
395 if (WIFEXITED(status)) {
396 weston_log("%s exited with status %d\n", pinfo->path,
397 WEXITSTATUS(status));
398 } else if (WIFSIGNALED(status)) {
399 weston_log("%s died on signal %d\n", pinfo->path,
400 WTERMSIG(status));
401 } else {
402 weston_log("%s disappeared\n", pinfo->path);
403 }
404
405 free(pinfo->path);
406 free(pinfo);
407}
408
409WL_EXPORT struct wl_client *
410weston_client_start(struct weston_compositor *compositor, const char *path)
411{
412 struct process_info *pinfo;
413 struct wl_client *client;
414
415 pinfo = zalloc(sizeof *pinfo);
416 if (!pinfo)
417 return NULL;
418
419 pinfo->path = strdup(path);
420 if (!pinfo->path)
421 goto out_free;
422
423 client = weston_client_launch(compositor, &pinfo->proc, path,
424 process_handle_sigchld);
425 if (!client)
426 goto out_str;
427
428 return client;
429
430out_str:
431 free(pinfo->path);
432
433out_free:
434 free(pinfo);
435
436 return NULL;
437}
438
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200439static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300440region_init_infinite(pixman_region32_t *region)
441{
442 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
443 UINT32_MAX, UINT32_MAX);
444}
445
Pekka Paalanene67858b2013-04-25 13:57:42 +0300446static struct weston_subsurface *
447weston_surface_to_subsurface(struct weston_surface *surface);
448
Jason Ekstranda7af7042013-10-12 22:38:11 -0500449WL_EXPORT struct weston_view *
450weston_view_create(struct weston_surface *surface)
451{
452 struct weston_view *view;
453
Bryce Harringtonde16d892014-11-20 22:21:57 -0800454 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500455 if (view == NULL)
456 return NULL;
457
458 view->surface = surface;
459
Jason Ekstranda7af7042013-10-12 22:38:11 -0500460 /* Assign to surface */
461 wl_list_insert(&surface->views, &view->surface_link);
462
463 wl_signal_init(&view->destroy_signal);
464 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300465 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500466
Jason Ekstranda7af7042013-10-12 22:38:11 -0500467 pixman_region32_init(&view->clip);
468
469 view->alpha = 1.0;
470 pixman_region32_init(&view->transform.opaque);
471
472 wl_list_init(&view->geometry.transformation_list);
473 wl_list_insert(&view->geometry.transformation_list,
474 &view->transform.position.link);
475 weston_matrix_init(&view->transform.position.matrix);
476 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200477 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500478 pixman_region32_init(&view->transform.boundingbox);
479 view->transform.dirty = 1;
480
Jason Ekstranda7af7042013-10-12 22:38:11 -0500481 return view;
482}
483
Jason Ekstrand108865d2014-06-26 10:04:49 -0700484struct weston_frame_callback {
485 struct wl_resource *resource;
486 struct wl_list link;
487};
488
Pekka Paalanen133e4392014-09-23 22:08:46 -0400489struct weston_presentation_feedback {
490 struct wl_resource *resource;
491
492 /* XXX: could use just wl_resource_get_link() instead */
493 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200494
495 /* The per-surface feedback flags */
496 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400497};
498
499static void
500weston_presentation_feedback_discard(
501 struct weston_presentation_feedback *feedback)
502{
503 presentation_feedback_send_discarded(feedback->resource);
504 wl_resource_destroy(feedback->resource);
505}
506
507static void
508weston_presentation_feedback_discard_list(struct wl_list *list)
509{
510 struct weston_presentation_feedback *feedback, *tmp;
511
512 wl_list_for_each_safe(feedback, tmp, list, link)
513 weston_presentation_feedback_discard(feedback);
514}
515
516static void
517weston_presentation_feedback_present(
518 struct weston_presentation_feedback *feedback,
519 struct weston_output *output,
520 uint32_t refresh_nsec,
521 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200522 uint64_t seq,
523 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400524{
525 struct wl_client *client = wl_resource_get_client(feedback->resource);
526 struct wl_resource *o;
527 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400528
529 wl_resource_for_each(o, &output->resource_list) {
530 if (wl_resource_get_client(o) != client)
531 continue;
532
533 presentation_feedback_send_sync_output(feedback->resource, o);
534 }
535
536 secs = ts->tv_sec;
537 presentation_feedback_send_presented(feedback->resource,
538 secs >> 32, secs & 0xffffffff,
539 ts->tv_nsec,
540 refresh_nsec,
541 seq >> 32, seq & 0xffffffff,
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200542 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400543 wl_resource_destroy(feedback->resource);
544}
545
546static void
547weston_presentation_feedback_present_list(struct wl_list *list,
548 struct weston_output *output,
549 uint32_t refresh_nsec,
550 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200551 uint64_t seq,
552 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400553{
554 struct weston_presentation_feedback *feedback, *tmp;
555
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200556 assert(!(flags & PRESENTATION_FEEDBACK_INVALID) ||
557 wl_list_empty(list));
558
Pekka Paalanen133e4392014-09-23 22:08:46 -0400559 wl_list_for_each_safe(feedback, tmp, list, link)
560 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200561 refresh_nsec, ts, seq,
562 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400563}
564
Jason Ekstrand7b982072014-05-20 14:33:03 -0500565static void
566surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
567{
568 struct weston_surface_state *state =
569 container_of(listener, struct weston_surface_state,
570 buffer_destroy_listener);
571
572 state->buffer = NULL;
573}
574
575static void
576weston_surface_state_init(struct weston_surface_state *state)
577{
578 state->newly_attached = 0;
579 state->buffer = NULL;
580 state->buffer_destroy_listener.notify =
581 surface_state_handle_buffer_destroy;
582 state->sx = 0;
583 state->sy = 0;
584
585 pixman_region32_init(&state->damage);
586 pixman_region32_init(&state->opaque);
587 region_init_infinite(&state->input);
588
589 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400590 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500591
592 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
593 state->buffer_viewport.buffer.scale = 1;
594 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
595 state->buffer_viewport.surface.width = -1;
596 state->buffer_viewport.changed = 0;
597}
598
599static void
600weston_surface_state_fini(struct weston_surface_state *state)
601{
602 struct weston_frame_callback *cb, *next;
603
604 wl_list_for_each_safe(cb, next,
605 &state->frame_callback_list, link)
606 wl_resource_destroy(cb->resource);
607
Pekka Paalanen133e4392014-09-23 22:08:46 -0400608 weston_presentation_feedback_discard_list(&state->feedback_list);
609
Jason Ekstrand7b982072014-05-20 14:33:03 -0500610 pixman_region32_fini(&state->input);
611 pixman_region32_fini(&state->opaque);
612 pixman_region32_fini(&state->damage);
613
614 if (state->buffer)
615 wl_list_remove(&state->buffer_destroy_listener.link);
616 state->buffer = NULL;
617}
618
619static void
620weston_surface_state_set_buffer(struct weston_surface_state *state,
621 struct weston_buffer *buffer)
622{
623 if (state->buffer == buffer)
624 return;
625
626 if (state->buffer)
627 wl_list_remove(&state->buffer_destroy_listener.link);
628 state->buffer = buffer;
629 if (state->buffer)
630 wl_signal_add(&state->buffer->destroy_signal,
631 &state->buffer_destroy_listener);
632}
633
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500634WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500635weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500636{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500637 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400638
Bryce Harringtonde16d892014-11-20 22:21:57 -0800639 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400640 if (surface == NULL)
641 return NULL;
642
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500643 wl_signal_init(&surface->destroy_signal);
644
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500645 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200646 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400647
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200648 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
649 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200650 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
651 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500652
653 weston_surface_state_init(&surface->pending);
654
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400655 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500656 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300657 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400658
Jason Ekstranda7af7042013-10-12 22:38:11 -0500659 wl_list_init(&surface->views);
660
661 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400662 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500663
Pekka Paalanene67858b2013-04-25 13:57:42 +0300664 wl_list_init(&surface->subsurface_list);
665 wl_list_init(&surface->subsurface_list_pending);
666
Jason Ekstrand1e059042014-10-16 10:55:19 -0500667 weston_matrix_init(&surface->buffer_to_surface_matrix);
668 weston_matrix_init(&surface->surface_to_buffer_matrix);
669
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400670 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500671}
672
Alex Wu8811bf92012-02-28 18:07:54 +0800673WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500674weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200675 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500676{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100677 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500678}
679
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400680WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500681weston_view_to_global_float(struct weston_view *view,
682 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200683{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500684 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200685 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
686
Jason Ekstranda7af7042013-10-12 22:38:11 -0500687 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200688
689 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200690 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700691 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200692 v.f[3]);
693 *x = 0;
694 *y = 0;
695 return;
696 }
697
698 *x = v.f[0] / v.f[3];
699 *y = v.f[1] / v.f[3];
700 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500701 *x = sx + view->geometry.x;
702 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200703 }
704}
705
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500706WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200707weston_transformed_coord(int width, int height,
708 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200709 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200710 float sx, float sy, float *bx, float *by)
711{
712 switch (transform) {
713 case WL_OUTPUT_TRANSFORM_NORMAL:
714 default:
715 *bx = sx;
716 *by = sy;
717 break;
718 case WL_OUTPUT_TRANSFORM_FLIPPED:
719 *bx = width - sx;
720 *by = sy;
721 break;
722 case WL_OUTPUT_TRANSFORM_90:
723 *bx = height - sy;
724 *by = sx;
725 break;
726 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
727 *bx = height - sy;
728 *by = width - sx;
729 break;
730 case WL_OUTPUT_TRANSFORM_180:
731 *bx = width - sx;
732 *by = height - sy;
733 break;
734 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
735 *bx = sx;
736 *by = height - sy;
737 break;
738 case WL_OUTPUT_TRANSFORM_270:
739 *bx = sy;
740 *by = width - sx;
741 break;
742 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
743 *bx = sy;
744 *by = sx;
745 break;
746 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200747
748 *bx *= scale;
749 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200750}
751
752WL_EXPORT pixman_box32_t
753weston_transformed_rect(int width, int height,
754 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200755 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200756 pixman_box32_t rect)
757{
758 float x1, x2, y1, y2;
759
760 pixman_box32_t ret;
761
Alexander Larsson4ea95522013-05-22 14:41:37 +0200762 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200763 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200764 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200765 rect.x2, rect.y2, &x2, &y2);
766
767 if (x1 <= x2) {
768 ret.x1 = x1;
769 ret.x2 = x2;
770 } else {
771 ret.x1 = x2;
772 ret.x2 = x1;
773 }
774
775 if (y1 <= y2) {
776 ret.y1 = y1;
777 ret.y2 = y2;
778 } else {
779 ret.y1 = y2;
780 ret.y2 = y1;
781 }
782
783 return ret;
784}
785
786WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500787weston_transformed_region(int width, int height,
788 enum wl_output_transform transform,
789 int32_t scale,
790 pixman_region32_t *src, pixman_region32_t *dest)
791{
792 pixman_box32_t *src_rects, *dest_rects;
793 int nrects, i;
794
795 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
796 if (src != dest)
797 pixman_region32_copy(dest, src);
798 return;
799 }
800
801 src_rects = pixman_region32_rectangles(src, &nrects);
802 dest_rects = malloc(nrects * sizeof(*dest_rects));
803 if (!dest_rects)
804 return;
805
806 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
807 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
808 } else {
809 for (i = 0; i < nrects; i++) {
810 switch (transform) {
811 default:
812 case WL_OUTPUT_TRANSFORM_NORMAL:
813 dest_rects[i].x1 = src_rects[i].x1;
814 dest_rects[i].y1 = src_rects[i].y1;
815 dest_rects[i].x2 = src_rects[i].x2;
816 dest_rects[i].y2 = src_rects[i].y2;
817 break;
818 case WL_OUTPUT_TRANSFORM_90:
819 dest_rects[i].x1 = height - src_rects[i].y2;
820 dest_rects[i].y1 = src_rects[i].x1;
821 dest_rects[i].x2 = height - src_rects[i].y1;
822 dest_rects[i].y2 = src_rects[i].x2;
823 break;
824 case WL_OUTPUT_TRANSFORM_180:
825 dest_rects[i].x1 = width - src_rects[i].x2;
826 dest_rects[i].y1 = height - src_rects[i].y2;
827 dest_rects[i].x2 = width - src_rects[i].x1;
828 dest_rects[i].y2 = height - src_rects[i].y1;
829 break;
830 case WL_OUTPUT_TRANSFORM_270:
831 dest_rects[i].x1 = src_rects[i].y1;
832 dest_rects[i].y1 = width - src_rects[i].x2;
833 dest_rects[i].x2 = src_rects[i].y2;
834 dest_rects[i].y2 = width - src_rects[i].x1;
835 break;
836 case WL_OUTPUT_TRANSFORM_FLIPPED:
837 dest_rects[i].x1 = width - src_rects[i].x2;
838 dest_rects[i].y1 = src_rects[i].y1;
839 dest_rects[i].x2 = width - src_rects[i].x1;
840 dest_rects[i].y2 = src_rects[i].y2;
841 break;
842 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
843 dest_rects[i].x1 = height - src_rects[i].y2;
844 dest_rects[i].y1 = width - src_rects[i].x2;
845 dest_rects[i].x2 = height - src_rects[i].y1;
846 dest_rects[i].y2 = width - src_rects[i].x1;
847 break;
848 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
849 dest_rects[i].x1 = src_rects[i].x1;
850 dest_rects[i].y1 = height - src_rects[i].y2;
851 dest_rects[i].x2 = src_rects[i].x2;
852 dest_rects[i].y2 = height - src_rects[i].y1;
853 break;
854 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
855 dest_rects[i].x1 = src_rects[i].y1;
856 dest_rects[i].y1 = src_rects[i].x1;
857 dest_rects[i].x2 = src_rects[i].y2;
858 dest_rects[i].y2 = src_rects[i].x2;
859 break;
860 }
861 }
862 }
863
864 if (scale != 1) {
865 for (i = 0; i < nrects; i++) {
866 dest_rects[i].x1 *= scale;
867 dest_rects[i].x2 *= scale;
868 dest_rects[i].y1 *= scale;
869 dest_rects[i].y2 *= scale;
870 }
871 }
872
873 pixman_region32_clear(dest);
874 pixman_region32_init_rects(dest, dest_rects, nrects);
875 free(dest_rects);
876}
877
Jonny Lamb74130762013-11-26 18:19:46 +0100878static void
879scaler_surface_to_buffer(struct weston_surface *surface,
880 float sx, float sy, float *bx, float *by)
881{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200882 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200883 double src_width, src_height;
884 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200885
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200886 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
887 if (vp->surface.width == -1) {
888 *bx = sx;
889 *by = sy;
890 return;
891 }
Jonny Lamb74130762013-11-26 18:19:46 +0100892
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200893 src_x = 0.0;
894 src_y = 0.0;
895 src_width = surface->width_from_buffer;
896 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100897 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200898 src_x = wl_fixed_to_double(vp->buffer.src_x);
899 src_y = wl_fixed_to_double(vp->buffer.src_y);
900 src_width = wl_fixed_to_double(vp->buffer.src_width);
901 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100902 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200903
904 *bx = sx * src_width / surface->width + src_x;
905 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100906}
907
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500908WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200909weston_surface_to_buffer_float(struct weston_surface *surface,
910 float sx, float sy, float *bx, float *by)
911{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200912 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
913
Jonny Lamb74130762013-11-26 18:19:46 +0100914 /* first transform coordinates if the scaler is set */
915 scaler_surface_to_buffer(surface, sx, sy, bx, by);
916
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500917 weston_transformed_coord(surface->width_from_buffer,
918 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200919 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100920 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200921}
922
Alexander Larsson4ea95522013-05-22 14:41:37 +0200923WL_EXPORT void
924weston_surface_to_buffer(struct weston_surface *surface,
925 int sx, int sy, int *bx, int *by)
926{
927 float bxf, byf;
928
Jonny Lamb74130762013-11-26 18:19:46 +0100929 weston_surface_to_buffer_float(surface,
930 sx, sy, &bxf, &byf);
931
Alexander Larsson4ea95522013-05-22 14:41:37 +0200932 *bx = floorf(bxf);
933 *by = floorf(byf);
934}
935
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200936WL_EXPORT pixman_box32_t
937weston_surface_to_buffer_rect(struct weston_surface *surface,
938 pixman_box32_t rect)
939{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200940 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100941 float xf, yf;
942
943 /* first transform box coordinates if the scaler is set */
944 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
945 rect.x1 = floorf(xf);
946 rect.y1 = floorf(yf);
947
948 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
949 rect.x2 = floorf(xf);
950 rect.y2 = floorf(yf);
951
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500952 return weston_transformed_rect(surface->width_from_buffer,
953 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200954 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200955 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200956}
957
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200958/** Transform a region from surface coordinates to buffer coordinates
959 *
960 * \param surface The surface to fetch wl_viewport and buffer transformation
961 * from.
962 * \param surface_region[in] The region in surface coordinates.
963 * \param buffer_region[out] The region converted to buffer coordinates.
964 *
965 * Buffer_region must be init'd, but will be completely overwritten.
966 *
967 * Viewport and buffer transformations can only do translation, scaling,
968 * and rotations in 90-degree steps. Therefore the only loss in the
969 * conversion is coordinate flooring (rounding).
970 */
971WL_EXPORT void
972weston_surface_to_buffer_region(struct weston_surface *surface,
973 pixman_region32_t *surface_region,
974 pixman_region32_t *buffer_region)
975{
976 pixman_box32_t *src_rects, *dest_rects;
977 int nrects, i;
978
979 src_rects = pixman_region32_rectangles(surface_region, &nrects);
980 dest_rects = malloc(nrects * sizeof(*dest_rects));
981 if (!dest_rects)
982 return;
983
984 for (i = 0; i < nrects; i++) {
985 dest_rects[i] = weston_surface_to_buffer_rect(surface,
986 src_rects[i]);
987 }
988
989 pixman_region32_fini(buffer_region);
990 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
991 free(dest_rects);
992}
993
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200994WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500995weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400996 struct weston_plane *plane)
997{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500998 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400999 return;
1000
Jason Ekstranda7af7042013-10-12 22:38:11 -05001001 weston_view_damage_below(view);
1002 view->plane = plane;
1003 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001004}
1005
Pekka Paalanen51723d52015-02-17 13:10:01 +02001006/** Inflict damage on the plane where the view is visible.
1007 *
1008 * \param view The view that causes the damage.
1009 *
1010 * If the view is currently on a plane (including the primary plane),
1011 * take the view's boundingbox, subtract all the opaque views that cover it,
1012 * and add the remaining region as damage to the plane. This corresponds
1013 * to the damage inflicted to the plane if this view disappeared.
1014 *
1015 * A repaint is scheduled for this view.
1016 *
1017 * The region of all opaque views covering this view is stored in
1018 * weston_view::clip and updated by view_accumulate_damage() during
1019 * weston_output_repaint(). Specifically, that region matches the
1020 * scenegraph as it was last painted.
1021 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001022WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001023weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001024{
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001025 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001026
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001027 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001028 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001029 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +08001030 if (view->plane)
1031 pixman_region32_union(&view->plane->damage,
1032 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001033 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001034 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001035}
1036
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001037static void
1038weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1039{
1040 uint32_t different = es->output_mask ^ mask;
1041 uint32_t entered = mask & different;
1042 uint32_t left = es->output_mask & different;
1043 struct weston_output *output;
1044 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001045 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001046
1047 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001048 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001049 return;
1050 if (different == 0)
1051 return;
1052
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001053 client = wl_resource_get_client(es->resource);
1054
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001055 wl_list_for_each(output, &es->compositor->output_list, link) {
1056 if (1 << output->id & different)
1057 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001058 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001059 client);
1060 if (resource == NULL)
1061 continue;
1062 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001063 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001064 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001065 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001066 }
1067}
1068
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001069
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001070static void
1071weston_surface_assign_output(struct weston_surface *es)
1072{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001073 struct weston_output *new_output;
1074 struct weston_view *view;
1075 pixman_region32_t region;
1076 uint32_t max, area, mask;
1077 pixman_box32_t *e;
1078
1079 new_output = NULL;
1080 max = 0;
1081 mask = 0;
1082 pixman_region32_init(&region);
1083 wl_list_for_each(view, &es->views, surface_link) {
1084 if (!view->output)
1085 continue;
1086
1087 pixman_region32_intersect(&region, &view->transform.boundingbox,
1088 &view->output->region);
1089
1090 e = pixman_region32_extents(&region);
1091 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1092
1093 mask |= view->output_mask;
1094
1095 if (area >= max) {
1096 new_output = view->output;
1097 max = area;
1098 }
1099 }
1100 pixman_region32_fini(&region);
1101
1102 es->output = new_output;
1103 weston_surface_update_output_mask(es, mask);
1104}
1105
1106static void
1107weston_view_assign_output(struct weston_view *ev)
1108{
1109 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001110 struct weston_output *output, *new_output;
1111 pixman_region32_t region;
1112 uint32_t max, area, mask;
1113 pixman_box32_t *e;
1114
1115 new_output = NULL;
1116 max = 0;
1117 mask = 0;
1118 pixman_region32_init(&region);
1119 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001120 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001121 &output->region);
1122
1123 e = pixman_region32_extents(&region);
1124 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1125
1126 if (area > 0)
1127 mask |= 1 << output->id;
1128
1129 if (area >= max) {
1130 new_output = output;
1131 max = area;
1132 }
1133 }
1134 pixman_region32_fini(&region);
1135
Jason Ekstranda7af7042013-10-12 22:38:11 -05001136 ev->output = new_output;
1137 ev->output_mask = mask;
1138
1139 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001140}
1141
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001142static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001143weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1144 int from_x, int from_y, int *to_x, int *to_y)
1145{
1146 float x, y;
1147
1148 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1149 weston_view_from_global_float(to, x, y, &x, &y);
1150
1151 *to_x = round(x);
1152 *to_y = round(y);
1153}
1154
1155static void
1156weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1157{
1158 pixman_box32_t *a;
1159 pixman_box32_t b;
1160
1161 a = pixman_region32_extents(&from->geometry.scissor);
1162
1163 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1164 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1165
1166 pixman_region32_fini(&to->geometry.scissor);
1167 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1168}
1169
1170static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001171view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001172 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001173{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001174 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1175 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001176 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001177 { inbox->x1, inbox->y1 },
1178 { inbox->x1, inbox->y2 },
1179 { inbox->x2, inbox->y1 },
1180 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001181 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001182 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001183 int i;
1184
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001185 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001186 /* avoid rounding empty bbox to 1x1 */
1187 pixman_region32_init(bbox);
1188 return;
1189 }
1190
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001191 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001192 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001193 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001194 if (x < min_x)
1195 min_x = x;
1196 if (x > max_x)
1197 max_x = x;
1198 if (y < min_y)
1199 min_y = y;
1200 if (y > max_y)
1201 max_y = y;
1202 }
1203
Pekka Paalanen219b9822012-02-08 15:38:37 +02001204 int_x = floorf(min_x);
1205 int_y = floorf(min_y);
1206 pixman_region32_init_rect(bbox, int_x, int_y,
1207 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001208}
1209
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001210static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001211weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001212{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001213 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001214
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001215 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001216 view->geometry.x = roundf(view->geometry.x);
1217 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001218
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001219 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001220 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1221 view->transform.position.matrix.d[12] = view->geometry.x;
1222 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001223
Jason Ekstranda7af7042013-10-12 22:38:11 -05001224 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001225
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226 view->transform.inverse = view->transform.position.matrix;
1227 view->transform.inverse.d[12] = -view->geometry.x;
1228 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001229
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001231 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001232 view->surface->width,
1233 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001234 if (view->geometry.scissor_enabled)
1235 pixman_region32_intersect(&view->transform.boundingbox,
1236 &view->transform.boundingbox,
1237 &view->geometry.scissor);
1238
1239 pixman_region32_translate(&view->transform.boundingbox,
1240 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001241
Jason Ekstranda7af7042013-10-12 22:38:11 -05001242 if (view->alpha == 1.0) {
1243 pixman_region32_copy(&view->transform.opaque,
1244 &view->surface->opaque);
1245 pixman_region32_translate(&view->transform.opaque,
1246 view->geometry.x,
1247 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001248 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001249}
1250
1251static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001252weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001253{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001254 struct weston_view *parent = view->geometry.parent;
1255 struct weston_matrix *matrix = &view->transform.matrix;
1256 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001257 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001258 pixman_region32_t surfregion;
1259 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001260
Jason Ekstranda7af7042013-10-12 22:38:11 -05001261 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001262
1263 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001264 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1265 view->transform.position.matrix.d[12] = view->geometry.x;
1266 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001267
1268 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001269 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001270 weston_matrix_multiply(matrix, &tform->matrix);
1271
Pekka Paalanen483243f2013-03-08 14:56:50 +02001272 if (parent)
1273 weston_matrix_multiply(matrix, &parent->transform.matrix);
1274
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001275 if (weston_matrix_invert(inverse, matrix) < 0) {
1276 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277 weston_log("error: weston_view %p"
1278 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001279 return -1;
1280 }
1281
Pekka Paalanen380adf52015-02-16 14:39:11 +02001282 pixman_region32_init_rect(&surfregion, 0, 0,
1283 view->surface->width, view->surface->height);
1284 if (view->geometry.scissor_enabled)
1285 pixman_region32_intersect(&surfregion, &surfregion,
1286 &view->geometry.scissor);
1287 surfbox = pixman_region32_extents(&surfregion);
1288
1289 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1290 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001291
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001292 return 0;
1293}
1294
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001295static struct weston_layer *
1296get_view_layer(struct weston_view *view)
1297{
1298 if (view->parent_view)
1299 return get_view_layer(view->parent_view);
1300 return view->layer_link.layer;
1301}
1302
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001303WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001304weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001305{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001306 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001307 struct weston_layer *layer;
1308 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001309
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001311 return;
1312
Pekka Paalanen483243f2013-03-08 14:56:50 +02001313 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001315
Jason Ekstranda7af7042013-10-12 22:38:11 -05001316 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001317
Jason Ekstranda7af7042013-10-12 22:38:11 -05001318 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001319
Jason Ekstranda7af7042013-10-12 22:38:11 -05001320 pixman_region32_fini(&view->transform.boundingbox);
1321 pixman_region32_fini(&view->transform.opaque);
1322 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001323
Pekka Paalanencd403622012-01-25 13:37:39 +02001324 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001325 if (view->geometry.transformation_list.next ==
1326 &view->transform.position.link &&
1327 view->geometry.transformation_list.prev ==
1328 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001329 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001330 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001331 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001332 if (weston_view_update_transform_enable(view) < 0)
1333 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001334 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001335
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001336 layer = get_view_layer(view);
1337 if (layer) {
1338 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001339 pixman_region32_intersect(&view->transform.boundingbox,
1340 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001341 pixman_region32_intersect(&view->transform.opaque,
1342 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001343 pixman_region32_fini(&mask);
1344 }
1345
Pekka Paalanen380adf52015-02-16 14:39:11 +02001346 if (parent) {
1347 if (parent->geometry.scissor_enabled) {
1348 view->geometry.scissor_enabled = true;
1349 weston_view_transfer_scissor(parent, view);
1350 } else {
1351 view->geometry.scissor_enabled = false;
1352 }
1353 }
1354
Jason Ekstranda7af7042013-10-12 22:38:11 -05001355 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001356
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001358
Jason Ekstranda7af7042013-10-12 22:38:11 -05001359 wl_signal_emit(&view->surface->compositor->transform_signal,
1360 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001361}
1362
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001363WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001364weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001365{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001366 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001367
1368 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001369 * The invariant: if view->geometry.dirty, then all views
1370 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001371 * Corollary: if not parent->geometry.dirty, then all ancestors
1372 * are not dirty.
1373 */
1374
Jason Ekstranda7af7042013-10-12 22:38:11 -05001375 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001376 return;
1377
Jason Ekstranda7af7042013-10-12 22:38:11 -05001378 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001379
Jason Ekstranda7af7042013-10-12 22:38:11 -05001380 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001381 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001382 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001383}
1384
1385WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001386weston_view_to_global_fixed(struct weston_view *view,
1387 wl_fixed_t vx, wl_fixed_t vy,
1388 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001389{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001390 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001391
Jason Ekstranda7af7042013-10-12 22:38:11 -05001392 weston_view_to_global_float(view,
1393 wl_fixed_to_double(vx),
1394 wl_fixed_to_double(vy),
1395 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001396 *x = wl_fixed_from_double(xf);
1397 *y = wl_fixed_from_double(yf);
1398}
1399
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001400WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401weston_view_from_global_float(struct weston_view *view,
1402 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001403{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001404 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001405 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1406
Jason Ekstranda7af7042013-10-12 22:38:11 -05001407 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001408
1409 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001410 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001411 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001412 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001413 *vx = 0;
1414 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001415 return;
1416 }
1417
Jason Ekstranda7af7042013-10-12 22:38:11 -05001418 *vx = v.f[0] / v.f[3];
1419 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001420 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001421 *vx = x - view->geometry.x;
1422 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001423 }
1424}
1425
1426WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001427weston_view_from_global_fixed(struct weston_view *view,
1428 wl_fixed_t x, wl_fixed_t y,
1429 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001430{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001431 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001432
Jason Ekstranda7af7042013-10-12 22:38:11 -05001433 weston_view_from_global_float(view,
1434 wl_fixed_to_double(x),
1435 wl_fixed_to_double(y),
1436 &vxf, &vyf);
1437 *vx = wl_fixed_from_double(vxf);
1438 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001439}
1440
1441WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001442weston_view_from_global(struct weston_view *view,
1443 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001444{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001445 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001446
Jason Ekstranda7af7042013-10-12 22:38:11 -05001447 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1448 *vx = floorf(vxf);
1449 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001450}
1451
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001452WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001453weston_surface_schedule_repaint(struct weston_surface *surface)
1454{
1455 struct weston_output *output;
1456
1457 wl_list_for_each(output, &surface->compositor->output_list, link)
1458 if (surface->output_mask & (1 << output->id))
1459 weston_output_schedule_repaint(output);
1460}
1461
1462WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001463weston_view_schedule_repaint(struct weston_view *view)
1464{
1465 struct weston_output *output;
1466
1467 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1468 if (view->output_mask & (1 << output->id))
1469 weston_output_schedule_repaint(output);
1470}
1471
Pekka Paalanene508ce62015-02-19 13:59:55 +02001472/**
1473 * XXX: This function does it the wrong way.
1474 * surface->damage is the damage from the client, and causes
1475 * surface_flush_damage() to copy pixels. No window management action can
1476 * cause damage to the client-provided content, warranting re-upload!
1477 *
1478 * Instead of surface->damage, this function should record the damage
1479 * with all the views for this surface to avoid extraneous texture
1480 * uploads.
1481 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001482WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001483weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001484{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001485 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001486 0, 0, surface->width,
1487 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001488
Kristian Høgsberg98238702012-08-03 16:29:12 -04001489 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001490}
1491
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001492WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001493weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001494{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001495 if (view->geometry.x == x && view->geometry.y == y)
1496 return;
1497
Jason Ekstranda7af7042013-10-12 22:38:11 -05001498 view->geometry.x = x;
1499 view->geometry.y = y;
1500 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001501}
1502
Pekka Paalanen483243f2013-03-08 14:56:50 +02001503static void
1504transform_parent_handle_parent_destroy(struct wl_listener *listener,
1505 void *data)
1506{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001507 struct weston_view *view =
1508 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001509 geometry.parent_destroy_listener);
1510
Jason Ekstranda7af7042013-10-12 22:38:11 -05001511 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001512}
1513
1514WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001515weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001516 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001517{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001518 if (view->geometry.parent) {
1519 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1520 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001521
1522 if (!parent)
1523 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001524 }
1525
Jason Ekstranda7af7042013-10-12 22:38:11 -05001526 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001527
Jason Ekstranda7af7042013-10-12 22:38:11 -05001528 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001529 transform_parent_handle_parent_destroy;
1530 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001531 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001532 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001533 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001534 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001535 }
1536
Jason Ekstranda7af7042013-10-12 22:38:11 -05001537 weston_view_geometry_dirty(view);
1538}
1539
Pekka Paalanen380adf52015-02-16 14:39:11 +02001540/** Set a clip mask rectangle on a view
1541 *
1542 * \param view The view to set the clip mask on.
1543 * \param x Top-left corner X coordinate of the clip rectangle.
1544 * \param y Top-left corner Y coordinate of the clip rectangle.
1545 * \param width Width of the clip rectangle, non-negative.
1546 * \param height Height of the clip rectangle, non-negative.
1547 *
1548 * A shell may set a clip mask rectangle on a view. Everything outside
1549 * the rectangle is cut away for input and output purposes: it is
1550 * not drawn and cannot be hit by hit-test based input like pointer
1551 * motion or touch-downs. Everything inside the rectangle will behave
1552 * normally. Clients are unaware of clipping.
1553 *
1554 * The rectangle is set in the surface local coordinates. Setting a clip
1555 * mask rectangle does not affect the view position, the view is positioned
1556 * as it would be without a clip. The clip also does not change
1557 * weston_surface::width,height.
1558 *
1559 * The clip mask rectangle is part of transformation inheritance
1560 * (weston_view_set_transform_parent()). A clip set in the root of the
1561 * transformation inheritance tree will affect all views in the tree.
1562 * A clip can be set only on the root view. Attempting to set a clip
1563 * on view that has a transformation parent will fail. Assigning a parent
1564 * to a view that has a clip set will cause the clip to be forgotten.
1565 *
1566 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1567 * on the additional transformations in the child views. These transformations
1568 * may not rotate the coordinate axes, i.e., only translation and scaling
1569 * are allowed. Violating this restriction causes the clipping to malfunction.
1570 * Furthermore, using scaling may cause rounding errors in child clipping.
1571 *
1572 * The clip mask rectangle is not automatically adjusted based on
1573 * wl_surface.attach dx and dy arguments.
1574 *
1575 * A clip mask rectangle can be set only if the compositor capability
1576 * WESTON_CAP_VIEW_CLIP_MASK is present.
1577 *
1578 * This function sets the clip mask rectangle and schedules a repaint for
1579 * the view.
1580 */
1581WL_EXPORT void
1582weston_view_set_mask(struct weston_view *view,
1583 int x, int y, int width, int height)
1584{
1585 struct weston_compositor *compositor = view->surface->compositor;
1586
1587 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1588 weston_log("%s not allowed without capability!\n", __func__);
1589 return;
1590 }
1591
1592 if (view->geometry.parent) {
1593 weston_log("view %p has a parent, clip forbidden!\n", view);
1594 return;
1595 }
1596
1597 if (width < 0 || height < 0) {
1598 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1599 x, y, width, height);
1600 return;
1601 }
1602
1603 pixman_region32_fini(&view->geometry.scissor);
1604 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1605 view->geometry.scissor_enabled = true;
1606 weston_view_geometry_dirty(view);
1607 weston_view_schedule_repaint(view);
1608}
1609
1610/** Remove the clip mask from a view
1611 *
1612 * \param view The view to remove the clip mask from.
1613 *
1614 * Removed the clip mask rectangle and schedules a repaint.
1615 *
1616 * \sa weston_view_set_mask
1617 */
1618WL_EXPORT void
1619weston_view_set_mask_infinite(struct weston_view *view)
1620{
1621 view->geometry.scissor_enabled = false;
1622 weston_view_geometry_dirty(view);
1623 weston_view_schedule_repaint(view);
1624}
1625
Derek Foreman280e7dd2014-10-03 13:13:42 -05001626WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001627weston_view_is_mapped(struct weston_view *view)
1628{
1629 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001630 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001631 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001632 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001633}
1634
Derek Foreman280e7dd2014-10-03 13:13:42 -05001635WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001636weston_surface_is_mapped(struct weston_surface *surface)
1637{
1638 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001639 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001640 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001641 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001642}
1643
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001644static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001645surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001646{
1647 struct weston_view *view;
1648
1649 if (surface->width == width && surface->height == height)
1650 return;
1651
1652 surface->width = width;
1653 surface->height = height;
1654
1655 wl_list_for_each(view, &surface->views, surface_link)
1656 weston_view_geometry_dirty(view);
1657}
1658
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001659WL_EXPORT void
1660weston_surface_set_size(struct weston_surface *surface,
1661 int32_t width, int32_t height)
1662{
1663 assert(!surface->resource);
1664 surface_set_size(surface, width, height);
1665}
1666
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001667static int
1668fixed_round_up_to_int(wl_fixed_t f)
1669{
1670 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1671}
1672
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001673static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001674weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001675{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001676 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001677 int32_t width, height;
1678
1679 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001680 surface->width_from_buffer = 0;
1681 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001682 return;
1683 }
1684
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001685 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001686 case WL_OUTPUT_TRANSFORM_90:
1687 case WL_OUTPUT_TRANSFORM_270:
1688 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1689 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001690 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1691 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001692 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001693 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001694 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1695 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001696 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001697 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001698
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001699 surface->width_from_buffer = width;
1700 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001701}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001702
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001703static void
1704weston_surface_update_size(struct weston_surface *surface)
1705{
1706 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1707 int32_t width, height;
1708
1709 width = surface->width_from_buffer;
1710 height = surface->height_from_buffer;
1711
1712 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001713 surface_set_size(surface,
1714 vp->surface.width, vp->surface.height);
1715 return;
1716 }
1717
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001718 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001719 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1720 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1721
1722 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001723 return;
1724 }
1725
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001726 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001727}
1728
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001729WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001730weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001731{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001732 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001733
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001734 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001735
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001736 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001737}
1738
Jason Ekstranda7af7042013-10-12 22:38:11 -05001739WL_EXPORT struct weston_view *
1740weston_compositor_pick_view(struct weston_compositor *compositor,
1741 wl_fixed_t x, wl_fixed_t y,
1742 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001743{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001744 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001745 wl_fixed_t view_x, view_y;
1746 int view_ix, view_iy;
1747 int ix = wl_fixed_to_int(x);
1748 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001749
Jason Ekstranda7af7042013-10-12 22:38:11 -05001750 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001751 if (!pixman_region32_contains_point(
1752 &view->transform.boundingbox, ix, iy, NULL))
1753 continue;
1754
1755 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1756 view_ix = wl_fixed_to_int(view_x);
1757 view_iy = wl_fixed_to_int(view_y);
1758
1759 if (!pixman_region32_contains_point(&view->surface->input,
1760 view_ix, view_iy, NULL))
1761 continue;
1762
Pekka Paalanen380adf52015-02-16 14:39:11 +02001763 if (view->geometry.scissor_enabled &&
1764 !pixman_region32_contains_point(&view->geometry.scissor,
1765 view_ix, view_iy, NULL))
1766 continue;
1767
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001768 *vx = view_x;
1769 *vy = view_y;
1770 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001771 }
1772
1773 return NULL;
1774}
1775
1776static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001777weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001778{
Daniel Stone37816df2012-05-16 18:45:18 +01001779 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001780
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001781 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001782 return;
1783
Daniel Stone37816df2012-05-16 18:45:18 +01001784 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001785 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001786}
1787
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001788WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001789weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001790{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001791 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001792
Jason Ekstranda7af7042013-10-12 22:38:11 -05001793 if (!weston_view_is_mapped(view))
1794 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001795
Jason Ekstranda7af7042013-10-12 22:38:11 -05001796 weston_view_damage_below(view);
1797 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001798 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001799 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001800 wl_list_remove(&view->link);
1801 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001802 view->output_mask = 0;
1803 weston_surface_assign_output(view->surface);
1804
1805 if (weston_surface_is_mapped(view->surface))
1806 return;
1807
1808 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1809 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001810 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001811 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001812 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001813 NULL,
1814 wl_fixed_from_int(0),
1815 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001816 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001817 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001818 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001819}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001820
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821WL_EXPORT void
1822weston_surface_unmap(struct weston_surface *surface)
1823{
1824 struct weston_view *view;
1825
1826 wl_list_for_each(view, &surface->views, surface_link)
1827 weston_view_unmap(view);
1828 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001829}
1830
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001831static void
1832weston_surface_reset_pending_buffer(struct weston_surface *surface)
1833{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001834 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001835 surface->pending.sx = 0;
1836 surface->pending.sy = 0;
1837 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001838 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001839}
1840
Jason Ekstranda7af7042013-10-12 22:38:11 -05001841WL_EXPORT void
1842weston_view_destroy(struct weston_view *view)
1843{
1844 wl_signal_emit(&view->destroy_signal, view);
1845
1846 assert(wl_list_empty(&view->geometry.child_list));
1847
1848 if (weston_view_is_mapped(view)) {
1849 weston_view_unmap(view);
1850 weston_compositor_build_view_list(view->surface->compositor);
1851 }
1852
1853 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001854 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001855
1856 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001857 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001858 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001859 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001860
1861 weston_view_set_transform_parent(view, NULL);
1862
Jason Ekstranda7af7042013-10-12 22:38:11 -05001863 wl_list_remove(&view->surface_link);
1864
1865 free(view);
1866}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001867
1868WL_EXPORT void
1869weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001870{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001871 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001872 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001873
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001874 if (--surface->ref_count > 0)
1875 return;
1876
1877 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1878
Pekka Paalanene67858b2013-04-25 13:57:42 +03001879 assert(wl_list_empty(&surface->subsurface_list_pending));
1880 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001881
Jason Ekstranda7af7042013-10-12 22:38:11 -05001882 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1883 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001884
Jason Ekstrand7b982072014-05-20 14:33:03 -05001885 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001886
Pekka Paalanende685b82012-12-04 15:58:12 +02001887 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001888
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001889 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001890 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001891 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001892
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001893 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001894 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001895
Pekka Paalanen133e4392014-09-23 22:08:46 -04001896 weston_presentation_feedback_discard_list(&surface->feedback_list);
1897
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001898 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001899}
1900
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001901static void
1902destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001903{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001904 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001905
Giulio Camuffo0d379742013-11-15 22:06:15 +01001906 /* Set the resource to NULL, since we don't want to leave a
1907 * dangling pointer if the surface was refcounted and survives
1908 * the weston_surface_destroy() call. */
1909 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001910 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001911}
1912
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001913static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001914weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1915{
1916 struct weston_buffer *buffer =
1917 container_of(listener, struct weston_buffer, destroy_listener);
1918
1919 wl_signal_emit(&buffer->destroy_signal, buffer);
1920 free(buffer);
1921}
1922
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001923WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001924weston_buffer_from_resource(struct wl_resource *resource)
1925{
1926 struct weston_buffer *buffer;
1927 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001928
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001929 listener = wl_resource_get_destroy_listener(resource,
1930 weston_buffer_destroy_handler);
1931
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001932 if (listener)
1933 return container_of(listener, struct weston_buffer,
1934 destroy_listener);
1935
1936 buffer = zalloc(sizeof *buffer);
1937 if (buffer == NULL)
1938 return NULL;
1939
1940 buffer->resource = resource;
1941 wl_signal_init(&buffer->destroy_signal);
1942 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001943 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001944 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001945
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001946 return buffer;
1947}
1948
1949static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001950weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1951 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001952{
Pekka Paalanende685b82012-12-04 15:58:12 +02001953 struct weston_buffer_reference *ref =
1954 container_of(listener, struct weston_buffer_reference,
1955 destroy_listener);
1956
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001957 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001958 ref->buffer = NULL;
1959}
1960
1961WL_EXPORT void
1962weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001963 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001964{
1965 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001966 ref->buffer->busy_count--;
1967 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001968 assert(wl_resource_get_client(ref->buffer->resource));
1969 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001970 WL_BUFFER_RELEASE);
1971 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001972 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001973 }
1974
Pekka Paalanende685b82012-12-04 15:58:12 +02001975 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001976 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001977 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001978 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001979 }
1980
Pekka Paalanende685b82012-12-04 15:58:12 +02001981 ref->buffer = buffer;
1982 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1983}
1984
1985static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001986weston_surface_attach(struct weston_surface *surface,
1987 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001988{
1989 weston_buffer_reference(&surface->buffer_ref, buffer);
1990
Pekka Paalanena6421c42012-12-04 15:58:10 +02001991 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001992 if (weston_surface_is_mapped(surface))
1993 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001994 }
1995
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001996 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001997
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001998 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001999 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002000}
2001
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002002WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002003weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002004{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002005 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002006
2007 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002008 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002009}
2010
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002011WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002012weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002013{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002014 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002015
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002016 pixman_region32_union(&compositor->primary_plane.damage,
2017 &compositor->primary_plane.damage,
2018 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002019 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002020}
2021
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002022static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002023surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002024{
Pekka Paalanende685b82012-12-04 15:58:12 +02002025 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002026 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002027 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002028
Pekka Paalanenb5026542014-11-12 15:09:24 +02002029 if (weston_timeline_enabled_ &&
2030 pixman_region32_not_empty(&surface->damage))
2031 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2032 TLP_OUTPUT(surface->output), TLP_END);
2033
Jason Ekstrandef540082014-06-26 10:37:36 -07002034 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002035}
2036
2037static void
2038view_accumulate_damage(struct weston_view *view,
2039 pixman_region32_t *opaque)
2040{
2041 pixman_region32_t damage;
2042
2043 pixman_region32_init(&damage);
2044 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002045 pixman_box32_t *extents;
2046
Jason Ekstranda7af7042013-10-12 22:38:11 -05002047 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002048 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002049 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002050 pixman_region32_copy(&damage, &view->surface->damage);
2051 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002052 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002053 }
2054
Pekka Paalanen380adf52015-02-16 14:39:11 +02002055 pixman_region32_intersect(&damage, &damage,
2056 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002057 pixman_region32_subtract(&damage, &damage, opaque);
2058 pixman_region32_union(&view->plane->damage,
2059 &view->plane->damage, &damage);
2060 pixman_region32_fini(&damage);
2061 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002062 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002063}
2064
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002065static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002066compositor_accumulate_damage(struct weston_compositor *ec)
2067{
2068 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002069 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002070 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002071
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002072 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002073
2074 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002075 pixman_region32_copy(&plane->clip, &clip);
2076
2077 pixman_region32_init(&opaque);
2078
Jason Ekstranda7af7042013-10-12 22:38:11 -05002079 wl_list_for_each(ev, &ec->view_list, link) {
2080 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002081 continue;
2082
Jason Ekstranda7af7042013-10-12 22:38:11 -05002083 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002084 }
2085
2086 pixman_region32_union(&clip, &clip, &opaque);
2087 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002088 }
2089
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002090 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002091
Jason Ekstranda7af7042013-10-12 22:38:11 -05002092 wl_list_for_each(ev, &ec->view_list, link)
2093 ev->surface->touched = 0;
2094
2095 wl_list_for_each(ev, &ec->view_list, link) {
2096 if (ev->surface->touched)
2097 continue;
2098 ev->surface->touched = 1;
2099
2100 surface_flush_damage(ev->surface);
2101
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002102 /* Both the renderer and the backend have seen the buffer
2103 * by now. If renderer needs the buffer, it has its own
2104 * reference set. If the backend wants to keep the buffer
2105 * around for migrating the surface into a non-primary plane
2106 * later, keep_buffer is true. Otherwise, drop the core
2107 * reference now, and allow early buffer release. This enables
2108 * clients to use single-buffering.
2109 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002110 if (!ev->surface->keep_buffer)
2111 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002112 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002113}
2114
2115static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002116surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002117{
2118 struct weston_subsurface *sub;
2119
Pekka Paalanene67858b2013-04-25 13:57:42 +03002120 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002121 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002122 continue;
2123
Jason Ekstranda7af7042013-10-12 22:38:11 -05002124 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2125 wl_list_init(&sub->surface->views);
2126
2127 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002128 }
2129}
2130
2131static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002132surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002133{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002134 struct weston_subsurface *sub;
2135 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002136
Jason Ekstranda7af7042013-10-12 22:38:11 -05002137 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2138 if (sub->surface == surface)
2139 continue;
2140
George Kiagiadakised04d382014-06-13 18:10:26 +02002141 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2142 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002143 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002144 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002145
2146 surface_free_unused_subsurface_views(sub->surface);
2147 }
2148}
2149
2150static void
2151view_list_add_subsurface_view(struct weston_compositor *compositor,
2152 struct weston_subsurface *sub,
2153 struct weston_view *parent)
2154{
2155 struct weston_subsurface *child;
2156 struct weston_view *view = NULL, *iv;
2157
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002158 if (!weston_surface_is_mapped(sub->surface))
2159 return;
2160
Jason Ekstranda7af7042013-10-12 22:38:11 -05002161 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2162 if (iv->geometry.parent == parent) {
2163 view = iv;
2164 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002165 }
2166 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002167
2168 if (view) {
2169 /* Put it back in the surface's list of views */
2170 wl_list_remove(&view->surface_link);
2171 wl_list_insert(&sub->surface->views, &view->surface_link);
2172 } else {
2173 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002174 weston_view_set_position(view,
2175 sub->position.x,
2176 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002177 weston_view_set_transform_parent(view, parent);
2178 }
2179
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002180 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002181 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002182
Pekka Paalanenb188e912013-11-19 14:03:35 +02002183 if (wl_list_empty(&sub->surface->subsurface_list)) {
2184 wl_list_insert(compositor->view_list.prev, &view->link);
2185 return;
2186 }
2187
2188 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2189 if (child->surface == sub->surface)
2190 wl_list_insert(compositor->view_list.prev, &view->link);
2191 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002192 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002193 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002194}
2195
2196static void
2197view_list_add(struct weston_compositor *compositor,
2198 struct weston_view *view)
2199{
2200 struct weston_subsurface *sub;
2201
2202 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002203
Pekka Paalanenb188e912013-11-19 14:03:35 +02002204 if (wl_list_empty(&view->surface->subsurface_list)) {
2205 wl_list_insert(compositor->view_list.prev, &view->link);
2206 return;
2207 }
2208
2209 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2210 if (sub->surface == view->surface)
2211 wl_list_insert(compositor->view_list.prev, &view->link);
2212 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002213 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002214 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002215}
2216
2217static void
2218weston_compositor_build_view_list(struct weston_compositor *compositor)
2219{
2220 struct weston_view *view;
2221 struct weston_layer *layer;
2222
2223 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002224 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002225 surface_stash_subsurface_views(view->surface);
2226
2227 wl_list_init(&compositor->view_list);
2228 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002229 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002230 view_list_add(compositor, view);
2231 }
2232 }
2233
2234 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002235 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002236 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002237}
2238
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002239static void
2240weston_output_take_feedback_list(struct weston_output *output,
2241 struct weston_surface *surface)
2242{
2243 struct weston_view *view;
2244 struct weston_presentation_feedback *feedback;
2245 uint32_t flags = 0xffffffff;
2246
2247 if (wl_list_empty(&surface->feedback_list))
2248 return;
2249
2250 /* All views must have the flag for the flag to survive. */
2251 wl_list_for_each(view, &surface->views, surface_link) {
2252 /* ignore views that are not on this output at all */
2253 if (view->output_mask & (1u << output->id))
2254 flags &= view->psf_flags;
2255 }
2256
2257 wl_list_for_each(feedback, &surface->feedback_list, link)
2258 feedback->psf_flags = flags;
2259
2260 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2261 wl_list_init(&surface->feedback_list);
2262}
2263
David Herrmann1edf44c2013-10-22 17:11:26 +02002264static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002265weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002266{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002267 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002268 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002269 struct weston_animation *animation, *next;
2270 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002271 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002272 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002273 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002274
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002275 if (output->destroying)
2276 return 0;
2277
Pekka Paalanenb5026542014-11-12 15:09:24 +02002278 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2279
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002280 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002281 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002282
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002283 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002284 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002285 } else {
2286 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002287 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002288 ev->psf_flags = 0;
2289 }
2290 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002291
Pekka Paalanene67858b2013-04-25 13:57:42 +03002292 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002293 wl_list_for_each(ev, &ec->view_list, link) {
2294 /* Note: This operation is safe to do multiple times on the
2295 * same surface.
2296 */
2297 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002298 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002299 &ev->surface->frame_callback_list);
2300 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002301
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002302 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002303 }
2304 }
2305
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002306 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002307
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002308 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002309 pixman_region32_intersect(&output_damage,
2310 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002311 pixman_region32_subtract(&output_damage,
2312 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002313
Scott Moreauccbf29d2012-02-22 14:21:41 -07002314 if (output->dirty)
2315 weston_output_update_matrix(output);
2316
David Herrmann1edf44c2013-10-22 17:11:26 +02002317 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002318
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002319 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002320
Kristian Høgsbergef044142011-06-21 15:02:12 -04002321 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002322
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002323 weston_compositor_repick(ec);
2324 wl_event_loop_dispatch(ec->input_loop, 0);
2325
Jonas Ådahldb773762012-06-13 00:01:21 +02002326 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002327 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002328 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002329 }
2330
Scott Moreaud64cf212012-06-08 19:40:54 -06002331 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002332 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002333 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002334 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002335
Pekka Paalanenb5026542014-11-12 15:09:24 +02002336 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2337
David Herrmann1edf44c2013-10-22 17:11:26 +02002338 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002339}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002340
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002341static int
2342weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002343{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002344 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002345
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002346 wl_event_loop_dispatch(compositor->input_loop, 0);
2347
2348 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002349}
2350
Pekka Paalanen82919792014-05-21 13:51:49 +03002351static void
2352weston_output_schedule_repaint_reset(struct weston_output *output)
2353{
2354 struct weston_compositor *compositor = output->compositor;
2355 struct wl_event_loop *loop;
2356 int fd;
2357
2358 output->repaint_scheduled = 0;
2359 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2360
2361 if (compositor->input_loop_source)
2362 return;
2363
2364 loop = wl_display_get_event_loop(compositor->wl_display);
2365 fd = wl_event_loop_get_fd(compositor->input_loop);
2366 compositor->input_loop_source =
2367 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2368 weston_compositor_read_input, compositor);
2369}
2370
Pekka Paalanen0513a952014-05-21 16:17:27 +03002371static int
2372output_repaint_timer_handler(void *data)
2373{
2374 struct weston_output *output = data;
2375 struct weston_compositor *compositor = output->compositor;
2376
2377 if (output->repaint_needed &&
2378 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2379 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2380 weston_output_repaint(output) == 0)
2381 return 0;
2382
2383 weston_output_schedule_repaint_reset(output);
2384
2385 return 0;
2386}
2387
Kristian Høgsbergef044142011-06-21 15:02:12 -04002388WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002389weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002390 const struct timespec *stamp,
2391 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002392{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002393 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002394 int32_t refresh_nsec;
2395 struct timespec now;
2396 struct timespec gone;
2397 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002398
Pekka Paalanenb5026542014-11-12 15:09:24 +02002399 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2400 TLP_VBLANK(stamp), TLP_END);
2401
Pekka Paalanen0513a952014-05-21 16:17:27 +03002402 refresh_nsec = 1000000000000LL / output->current_mode->refresh;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002403 weston_presentation_feedback_present_list(&output->feedback_list,
2404 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002405 output->msc,
2406 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002407
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002408 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002409
Pekka Paalanen0513a952014-05-21 16:17:27 +03002410 weston_compositor_read_presentation_clock(compositor, &now);
2411 timespec_sub(&gone, &now, stamp);
2412 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2413 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002414
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002415 if (msec < -1000 || msec > 1000) {
2416 static bool warned;
2417
2418 if (!warned)
2419 weston_log("Warning: computed repaint delay is "
2420 "insane: %d msec\n", msec);
2421 warned = true;
2422
2423 msec = 0;
2424 }
2425
2426 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002427 output_repaint_timer_handler(output);
2428 else
2429 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002430}
2431
2432static void
2433idle_repaint(void *data)
2434{
2435 struct weston_output *output = data;
2436
Jonas Ådahle5a12252013-04-05 23:07:11 +02002437 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002438}
2439
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002440WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002441weston_layer_entry_insert(struct weston_layer_entry *list,
2442 struct weston_layer_entry *entry)
2443{
2444 wl_list_insert(&list->link, &entry->link);
2445 entry->layer = list->layer;
2446}
2447
2448WL_EXPORT void
2449weston_layer_entry_remove(struct weston_layer_entry *entry)
2450{
2451 wl_list_remove(&entry->link);
2452 wl_list_init(&entry->link);
2453 entry->layer = NULL;
2454}
2455
2456WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002457weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2458{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002459 wl_list_init(&layer->view_list.link);
2460 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002461 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002462 if (below != NULL)
2463 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002464}
2465
2466WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002467weston_layer_set_mask(struct weston_layer *layer,
2468 int x, int y, int width, int height)
2469{
2470 struct weston_view *view;
2471
2472 layer->mask.x1 = x;
2473 layer->mask.x2 = x + width;
2474 layer->mask.y1 = y;
2475 layer->mask.y2 = y + height;
2476
2477 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2478 weston_view_geometry_dirty(view);
2479 }
2480}
2481
2482WL_EXPORT void
2483weston_layer_set_mask_infinite(struct weston_layer *layer)
2484{
2485 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2486 UINT32_MAX, UINT32_MAX);
2487}
2488
2489WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002490weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002491{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002492 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002493 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002494
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002495 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2496 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002497 return;
2498
Pekka Paalanenb5026542014-11-12 15:09:24 +02002499 if (!output->repaint_needed)
2500 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2501
Kristian Høgsbergef044142011-06-21 15:02:12 -04002502 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002503 output->repaint_needed = 1;
2504 if (output->repaint_scheduled)
2505 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002506
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002507 wl_event_loop_add_idle(loop, idle_repaint, output);
2508 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002509 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2510
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002511
2512 if (compositor->input_loop_source) {
2513 wl_event_source_remove(compositor->input_loop_source);
2514 compositor->input_loop_source = NULL;
2515 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002516}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002517
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002518WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002519weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2520{
2521 struct weston_output *output;
2522
2523 wl_list_for_each(output, &compositor->output_list, link)
2524 weston_output_schedule_repaint(output);
2525}
2526
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002527static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002528surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002529{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002530 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002531}
2532
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002533static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002534surface_attach(struct wl_client *client,
2535 struct wl_resource *resource,
2536 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2537{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002538 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002539 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002540
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002541 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002542 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002543 if (buffer == NULL) {
2544 wl_client_post_no_memory(client);
2545 return;
2546 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002547 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002548
Pekka Paalanende685b82012-12-04 15:58:12 +02002549 /* Attach, attach, without commit in between does not send
2550 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002551 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002552
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002553 surface->pending.sx = sx;
2554 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002555 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002556}
2557
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002558static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002559surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002560 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002561 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002562{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002563 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002564
Pekka Paalanen8e159182012-10-10 12:49:25 +03002565 pixman_region32_union_rect(&surface->pending.damage,
2566 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002567 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002568}
2569
Kristian Høgsberg33418202011-08-16 23:01:28 -04002570static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002571destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002572{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002573 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002574
2575 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002576 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002577}
2578
2579static void
2580surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002581 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002582{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002583 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002584 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002585
2586 cb = malloc(sizeof *cb);
2587 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002588 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002589 return;
2590 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002591
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002592 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2593 callback);
2594 if (cb->resource == NULL) {
2595 free(cb);
2596 wl_resource_post_no_memory(resource);
2597 return;
2598 }
2599
Jason Ekstranda85118c2013-06-27 20:17:02 -05002600 wl_resource_set_implementation(cb->resource, NULL, cb,
2601 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002602
Pekka Paalanenbc106382012-10-10 12:49:31 +03002603 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002604}
2605
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002606static void
2607surface_set_opaque_region(struct wl_client *client,
2608 struct wl_resource *resource,
2609 struct wl_resource *region_resource)
2610{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002611 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002612 struct weston_region *region;
2613
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002614 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002615 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002616 pixman_region32_copy(&surface->pending.opaque,
2617 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002618 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002619 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002620 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002621}
2622
2623static void
2624surface_set_input_region(struct wl_client *client,
2625 struct wl_resource *resource,
2626 struct wl_resource *region_resource)
2627{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002628 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002629 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002630
2631 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002632 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002633 pixman_region32_copy(&surface->pending.input,
2634 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002635 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002636 pixman_region32_fini(&surface->pending.input);
2637 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002638 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002639}
2640
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002641static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002642weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002643{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002644 struct weston_subsurface *sub;
2645
2646 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2647 parent_link_pending) {
2648 wl_list_remove(&sub->parent_link);
2649 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2650 }
2651}
2652
2653static void
Jason Ekstrand1e059042014-10-16 10:55:19 -05002654weston_surface_build_buffer_matrix(struct weston_surface *surface,
2655 struct weston_matrix *matrix)
2656{
2657 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2658 double src_width, src_height, dest_width, dest_height;
2659
2660 weston_matrix_init(matrix);
2661
2662 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2663 src_width = surface->width_from_buffer;
2664 src_height = surface->height_from_buffer;
2665 } else {
2666 src_width = wl_fixed_to_double(vp->buffer.src_width);
2667 src_height = wl_fixed_to_double(vp->buffer.src_height);
2668 }
2669
2670 if (vp->surface.width == -1) {
2671 dest_width = src_width;
2672 dest_height = src_height;
2673 } else {
2674 dest_width = vp->surface.width;
2675 dest_height = vp->surface.height;
2676 }
2677
2678 if (src_width != dest_width || src_height != dest_height)
2679 weston_matrix_scale(matrix,
2680 src_width / dest_width,
2681 src_height / dest_height, 1);
2682
2683 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2684 weston_matrix_translate(matrix,
2685 wl_fixed_to_double(vp->buffer.src_x),
2686 wl_fixed_to_double(vp->buffer.src_y),
2687 0);
2688
2689 switch (vp->buffer.transform) {
2690 case WL_OUTPUT_TRANSFORM_FLIPPED:
2691 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2692 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2693 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2694 weston_matrix_scale(matrix, -1, 1, 1);
2695 weston_matrix_translate(matrix,
2696 surface->width_from_buffer, 0, 0);
2697 break;
2698 }
2699
2700 switch (vp->buffer.transform) {
2701 default:
2702 case WL_OUTPUT_TRANSFORM_NORMAL:
2703 case WL_OUTPUT_TRANSFORM_FLIPPED:
2704 break;
2705 case WL_OUTPUT_TRANSFORM_90:
2706 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2707 weston_matrix_rotate_xy(matrix, 0, 1);
2708 weston_matrix_translate(matrix,
2709 surface->height_from_buffer, 0, 0);
2710 break;
2711 case WL_OUTPUT_TRANSFORM_180:
2712 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2713 weston_matrix_rotate_xy(matrix, -1, 0);
2714 weston_matrix_translate(matrix,
2715 surface->width_from_buffer,
2716 surface->height_from_buffer, 0);
2717 break;
2718 case WL_OUTPUT_TRANSFORM_270:
2719 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2720 weston_matrix_rotate_xy(matrix, 0, -1);
2721 weston_matrix_translate(matrix,
2722 0, surface->width_from_buffer, 0);
2723 break;
2724 }
2725
2726 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2727}
2728
2729static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002730weston_surface_commit_state(struct weston_surface *surface,
2731 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002732{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002733 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002734 pixman_region32_t opaque;
2735
Alexander Larsson4ea95522013-05-22 14:41:37 +02002736 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002737 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002738 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002739 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002740
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002741 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002742 if (state->newly_attached)
2743 weston_surface_attach(surface, state->buffer);
2744 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002745
Jason Ekstrand1e059042014-10-16 10:55:19 -05002746 weston_surface_build_buffer_matrix(surface,
2747 &surface->surface_to_buffer_matrix);
2748 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2749 &surface->surface_to_buffer_matrix);
2750
Jason Ekstrand7b982072014-05-20 14:33:03 -05002751 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002752 weston_surface_update_size(surface);
2753 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002754 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002755 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002756
Jason Ekstrand7b982072014-05-20 14:33:03 -05002757 state->sx = 0;
2758 state->sy = 0;
2759 state->newly_attached = 0;
2760 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002761
2762 /* wl_surface.damage */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002763 if (weston_timeline_enabled_ &&
2764 pixman_region32_not_empty(&state->damage))
2765 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002766 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002767 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002768 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002769 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002770 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002771
2772 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002773 pixman_region32_init(&opaque);
2774 pixman_region32_intersect_rect(&opaque, &state->opaque,
2775 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002776
2777 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2778 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002779 wl_list_for_each(view, &surface->views, surface_link)
2780 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002781 }
2782
2783 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002784
2785 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002786 pixman_region32_intersect_rect(&surface->input, &state->input,
2787 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002788
Pekka Paalanenbc106382012-10-10 12:49:31 +03002789 /* wl_surface.frame */
2790 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002791 &state->frame_callback_list);
2792 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002793
2794 /* XXX:
2795 * What should happen with a feedback request, if there
2796 * is no wl_buffer attached for this commit?
2797 */
2798
2799 /* presentation.feedback */
2800 wl_list_insert_list(&surface->feedback_list,
2801 &state->feedback_list);
2802 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002803}
2804
2805static void
2806weston_surface_commit(struct weston_surface *surface)
2807{
2808 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002809
Pekka Paalanene67858b2013-04-25 13:57:42 +03002810 weston_surface_commit_subsurface_order(surface);
2811
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002812 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002813}
2814
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002815static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002816weston_subsurface_commit(struct weston_subsurface *sub);
2817
2818static void
2819weston_subsurface_parent_commit(struct weston_subsurface *sub,
2820 int parent_is_synchronized);
2821
2822static void
2823surface_commit(struct wl_client *client, struct wl_resource *resource)
2824{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002825 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002826 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2827
2828 if (sub) {
2829 weston_subsurface_commit(sub);
2830 return;
2831 }
2832
2833 weston_surface_commit(surface);
2834
2835 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2836 if (sub->surface != surface)
2837 weston_subsurface_parent_commit(sub, 0);
2838 }
2839}
2840
2841static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002842surface_set_buffer_transform(struct wl_client *client,
2843 struct wl_resource *resource, int transform)
2844{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002845 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002846
Jonny Lamba55f1392014-05-30 12:07:15 +02002847 /* if wl_output.transform grows more members this will need to be updated. */
2848 if (transform < 0 ||
2849 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2850 wl_resource_post_error(resource,
2851 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2852 "buffer transform must be a valid transform "
2853 "('%d' specified)", transform);
2854 return;
2855 }
2856
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002857 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002858 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002859}
2860
Alexander Larsson4ea95522013-05-22 14:41:37 +02002861static void
2862surface_set_buffer_scale(struct wl_client *client,
2863 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002864 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002865{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002866 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002867
Jonny Lamba55f1392014-05-30 12:07:15 +02002868 if (scale < 1) {
2869 wl_resource_post_error(resource,
2870 WL_SURFACE_ERROR_INVALID_SCALE,
2871 "buffer scale must be at least one "
2872 "('%d' specified)", scale);
2873 return;
2874 }
2875
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002876 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002877 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002878}
2879
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002880static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002881 surface_destroy,
2882 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002883 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002884 surface_frame,
2885 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002886 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002887 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002888 surface_set_buffer_transform,
2889 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002890};
2891
2892static void
2893compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002894 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002895{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002896 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002897 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002898
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002899 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002900 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002901 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002902 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002903 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002904
Jason Ekstranda85118c2013-06-27 20:17:02 -05002905 surface->resource =
2906 wl_resource_create(client, &wl_surface_interface,
2907 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002908 if (surface->resource == NULL) {
2909 weston_surface_destroy(surface);
2910 wl_resource_post_no_memory(resource);
2911 return;
2912 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002913 wl_resource_set_implementation(surface->resource, &surface_interface,
2914 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002915
2916 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002917}
2918
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002919static void
2920destroy_region(struct wl_resource *resource)
2921{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002922 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002923
2924 pixman_region32_fini(&region->region);
2925 free(region);
2926}
2927
2928static void
2929region_destroy(struct wl_client *client, struct wl_resource *resource)
2930{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002931 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002932}
2933
2934static void
2935region_add(struct wl_client *client, struct wl_resource *resource,
2936 int32_t x, int32_t y, int32_t width, int32_t height)
2937{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002938 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002939
2940 pixman_region32_union_rect(&region->region, &region->region,
2941 x, y, width, height);
2942}
2943
2944static void
2945region_subtract(struct wl_client *client, struct wl_resource *resource,
2946 int32_t x, int32_t y, int32_t width, int32_t height)
2947{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002948 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002949 pixman_region32_t rect;
2950
2951 pixman_region32_init_rect(&rect, x, y, width, height);
2952 pixman_region32_subtract(&region->region, &region->region, &rect);
2953 pixman_region32_fini(&rect);
2954}
2955
2956static const struct wl_region_interface region_interface = {
2957 region_destroy,
2958 region_add,
2959 region_subtract
2960};
2961
2962static void
2963compositor_create_region(struct wl_client *client,
2964 struct wl_resource *resource, uint32_t id)
2965{
2966 struct weston_region *region;
2967
2968 region = malloc(sizeof *region);
2969 if (region == NULL) {
2970 wl_resource_post_no_memory(resource);
2971 return;
2972 }
2973
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002974 pixman_region32_init(&region->region);
2975
Jason Ekstranda85118c2013-06-27 20:17:02 -05002976 region->resource =
2977 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002978 if (region->resource == NULL) {
2979 free(region);
2980 wl_resource_post_no_memory(resource);
2981 return;
2982 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002983 wl_resource_set_implementation(region->resource, &region_interface,
2984 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002985}
2986
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002987static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002988 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002989 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002990};
2991
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002992static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002993weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2994{
2995 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002996
Jason Ekstrand7b982072014-05-20 14:33:03 -05002997 weston_surface_commit_state(surface, &sub->cached);
2998 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002999
3000 weston_surface_commit_subsurface_order(surface);
3001
3002 weston_surface_schedule_repaint(surface);
3003
Jason Ekstrand7b982072014-05-20 14:33:03 -05003004 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003005}
3006
3007static void
3008weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3009{
3010 struct weston_surface *surface = sub->surface;
3011
3012 /*
3013 * If this commit would cause the surface to move by the
3014 * attach(dx, dy) parameters, the old damage region must be
3015 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02003016 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003017 */
3018 pixman_region32_translate(&sub->cached.damage,
3019 -surface->pending.sx, -surface->pending.sy);
3020 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
3021 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07003022 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003023
3024 if (surface->pending.newly_attached) {
3025 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003026 weston_surface_state_set_buffer(&sub->cached,
3027 surface->pending.buffer);
3028 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003029 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003030 weston_presentation_feedback_discard_list(
3031 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003032 }
3033 sub->cached.sx += surface->pending.sx;
3034 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003035
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003036 sub->cached.buffer_viewport.changed |=
3037 surface->pending.buffer_viewport.changed;
3038 sub->cached.buffer_viewport.buffer =
3039 surface->pending.buffer_viewport.buffer;
3040 sub->cached.buffer_viewport.surface =
3041 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003042
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003043 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003044
3045 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3046
3047 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3048
3049 wl_list_insert_list(&sub->cached.frame_callback_list,
3050 &surface->pending.frame_callback_list);
3051 wl_list_init(&surface->pending.frame_callback_list);
3052
Pekka Paalanen133e4392014-09-23 22:08:46 -04003053 wl_list_insert_list(&sub->cached.feedback_list,
3054 &surface->pending.feedback_list);
3055 wl_list_init(&surface->pending.feedback_list);
3056
Jason Ekstrand7b982072014-05-20 14:33:03 -05003057 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003058}
3059
Derek Foreman280e7dd2014-10-03 13:13:42 -05003060static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003061weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3062{
3063 while (sub) {
3064 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003065 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003066
3067 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003068 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003069
3070 sub = weston_surface_to_subsurface(sub->parent);
3071 }
3072
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003073 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003074}
3075
3076static void
3077weston_subsurface_commit(struct weston_subsurface *sub)
3078{
3079 struct weston_surface *surface = sub->surface;
3080 struct weston_subsurface *tmp;
3081
3082 /* Recursive check for effectively synchronized. */
3083 if (weston_subsurface_is_synchronized(sub)) {
3084 weston_subsurface_commit_to_cache(sub);
3085 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003086 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003087 /* flush accumulated state from cache */
3088 weston_subsurface_commit_to_cache(sub);
3089 weston_subsurface_commit_from_cache(sub);
3090 } else {
3091 weston_surface_commit(surface);
3092 }
3093
3094 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3095 if (tmp->surface != surface)
3096 weston_subsurface_parent_commit(tmp, 0);
3097 }
3098 }
3099}
3100
3101static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003102weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003103{
3104 struct weston_surface *surface = sub->surface;
3105 struct weston_subsurface *tmp;
3106
Pekka Paalanene67858b2013-04-25 13:57:42 +03003107 /* From now on, commit_from_cache the whole sub-tree, regardless of
3108 * the synchronized mode of each child. This sub-surface or some
3109 * of its ancestors were synchronized, so we are synchronized
3110 * all the way down.
3111 */
3112
Jason Ekstrand7b982072014-05-20 14:33:03 -05003113 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003114 weston_subsurface_commit_from_cache(sub);
3115
3116 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3117 if (tmp->surface != surface)
3118 weston_subsurface_parent_commit(tmp, 1);
3119 }
3120}
3121
3122static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003123weston_subsurface_parent_commit(struct weston_subsurface *sub,
3124 int parent_is_synchronized)
3125{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003126 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003127 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003128 wl_list_for_each(view, &sub->surface->views, surface_link)
3129 weston_view_set_position(view,
3130 sub->position.x,
3131 sub->position.y);
3132
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003133 sub->position.set = 0;
3134 }
3135
3136 if (parent_is_synchronized || sub->synchronized)
3137 weston_subsurface_synchronized_commit(sub);
3138}
3139
Pekka Paalanen8274d902014-08-06 19:36:51 +03003140static int
3141subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3142{
3143 return snprintf(buf, len, "sub-surface");
3144}
3145
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003146static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003147subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003148{
3149 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003150 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003151
Jason Ekstranda7af7042013-10-12 22:38:11 -05003152 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003153 weston_view_set_position(view,
3154 view->geometry.x + dx,
3155 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003156
3157 /* No need to check parent mappedness, because if parent is not
3158 * mapped, parent is not in a visible layer, so this sub-surface
3159 * will not be drawn either.
3160 */
3161 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003162 struct weston_output *output;
3163
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003164 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003165 * because that would call it also for the parent surface,
3166 * which might not be mapped yet. That would lead to
3167 * inconsistent state, where the window could never be
3168 * mapped.
3169 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003170 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003171 * weston_surface_is_mapped() return true, so that when the
3172 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003173 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003174 */
3175 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003176 output = container_of(compositor->output_list.next,
3177 struct weston_output, link);
3178
3179 surface->output = output;
3180 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003181 }
3182}
3183
3184static struct weston_subsurface *
3185weston_surface_to_subsurface(struct weston_surface *surface)
3186{
3187 if (surface->configure == subsurface_configure)
3188 return surface->configure_private;
3189
3190 return NULL;
3191}
3192
Pekka Paalanen01388e22013-04-25 13:57:44 +03003193WL_EXPORT struct weston_surface *
3194weston_surface_get_main_surface(struct weston_surface *surface)
3195{
3196 struct weston_subsurface *sub;
3197
3198 while (surface && (sub = weston_surface_to_subsurface(surface)))
3199 surface = sub->parent;
3200
3201 return surface;
3202}
3203
Pekka Paalanen50b67472014-10-01 15:02:41 +03003204WL_EXPORT int
3205weston_surface_set_role(struct weston_surface *surface,
3206 const char *role_name,
3207 struct wl_resource *error_resource,
3208 uint32_t error_code)
3209{
3210 assert(role_name);
3211
3212 if (surface->role_name == NULL ||
3213 surface->role_name == role_name ||
3214 strcmp(surface->role_name, role_name) == 0) {
3215 surface->role_name = role_name;
3216
3217 return 0;
3218 }
3219
3220 wl_resource_post_error(error_resource, error_code,
3221 "Cannot assign role %s to wl_surface@%d,"
3222 " already has role %s\n",
3223 role_name,
3224 wl_resource_get_id(surface->resource),
3225 surface->role_name);
3226 return -1;
3227}
3228
Pekka Paalanen8274d902014-08-06 19:36:51 +03003229WL_EXPORT void
3230weston_surface_set_label_func(struct weston_surface *surface,
3231 int (*desc)(struct weston_surface *,
3232 char *, size_t))
3233{
3234 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003235 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003236}
3237
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003238/** Get the size of surface contents
3239 *
3240 * \param surface The surface to query.
3241 * \param width Returns the width of raw contents.
3242 * \param height Returns the height of raw contents.
3243 *
3244 * Retrieves the raw surface content size in pixels for the given surface.
3245 * This is the whole content size in buffer pixels. If the surface
3246 * has no content or the renderer does not implement this feature,
3247 * zeroes are returned.
3248 *
3249 * This function is used to determine the buffer size needed for
3250 * a weston_surface_copy_content() call.
3251 */
3252WL_EXPORT void
3253weston_surface_get_content_size(struct weston_surface *surface,
3254 int *width, int *height)
3255{
3256 struct weston_renderer *rer = surface->compositor->renderer;
3257
3258 if (!rer->surface_get_content_size) {
3259 *width = 0;
3260 *height = 0;
3261 return;
3262 }
3263
3264 rer->surface_get_content_size(surface, width, height);
3265}
3266
3267/** Copy surface contents to system memory.
3268 *
3269 * \param surface The surface to copy from.
3270 * \param target Pointer to the target memory buffer.
3271 * \param size Size of the target buffer in bytes.
3272 * \param src_x X location on contents to copy from.
3273 * \param src_y Y location on contents to copy from.
3274 * \param width Width in pixels of the area to copy.
3275 * \param height Height in pixels of the area to copy.
3276 * \return 0 for success, -1 for failure.
3277 *
3278 * Surface contents are maintained by the renderer. They can be in a
3279 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3280 * else.
3281 *
3282 * Surface contents are copied into memory pointed to by target,
3283 * which has size bytes of space available. The target memory
3284 * may be larger than needed, but being smaller returns an error.
3285 * The extra bytes in target may or may not be written; their content is
3286 * unspecified. Size must be large enough to hold the image.
3287 *
3288 * The image in the target memory will be arranged in rows from
3289 * top to bottom, and pixels on a row from left to right. The pixel
3290 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3291 * width * 4.
3292 *
3293 * Parameters src_x and src_y define the upper-left corner in buffer
3294 * coordinates (pixels) to copy from. Parameters width and height
3295 * define the size of the area to copy in pixels.
3296 *
3297 * The rectangle defined by src_x, src_y, width, height must fit in
3298 * the surface contents. Otherwise an error is returned.
3299 *
3300 * Use surface_get_data_size to determine the content size; the
3301 * needed target buffer size and rectangle limits.
3302 *
3303 * CURRENT IMPLEMENTATION RESTRICTIONS:
3304 * - the machine must be little-endian due to Pixman formats.
3305 *
3306 * NOTE: Pixman formats are premultiplied.
3307 */
3308WL_EXPORT int
3309weston_surface_copy_content(struct weston_surface *surface,
3310 void *target, size_t size,
3311 int src_x, int src_y,
3312 int width, int height)
3313{
3314 struct weston_renderer *rer = surface->compositor->renderer;
3315 int cw, ch;
3316 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3317
3318 if (!rer->surface_copy_content)
3319 return -1;
3320
3321 weston_surface_get_content_size(surface, &cw, &ch);
3322
3323 if (src_x < 0 || src_y < 0)
3324 return -1;
3325
3326 if (width <= 0 || height <= 0)
3327 return -1;
3328
3329 if (src_x + width > cw || src_y + height > ch)
3330 return -1;
3331
3332 if (width * bytespp * height > size)
3333 return -1;
3334
3335 return rer->surface_copy_content(surface, target, size,
3336 src_x, src_y, width, height);
3337}
3338
Pekka Paalanene67858b2013-04-25 13:57:42 +03003339static void
3340subsurface_set_position(struct wl_client *client,
3341 struct wl_resource *resource, int32_t x, int32_t y)
3342{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003343 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003344
3345 if (!sub)
3346 return;
3347
3348 sub->position.x = x;
3349 sub->position.y = y;
3350 sub->position.set = 1;
3351}
3352
3353static struct weston_subsurface *
3354subsurface_from_surface(struct weston_surface *surface)
3355{
3356 struct weston_subsurface *sub;
3357
3358 sub = weston_surface_to_subsurface(surface);
3359 if (sub)
3360 return sub;
3361
3362 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3363 if (sub->surface == surface)
3364 return sub;
3365
3366 return NULL;
3367}
3368
3369static struct weston_subsurface *
3370subsurface_sibling_check(struct weston_subsurface *sub,
3371 struct weston_surface *surface,
3372 const char *request)
3373{
3374 struct weston_subsurface *sibling;
3375
3376 sibling = subsurface_from_surface(surface);
3377
3378 if (!sibling) {
3379 wl_resource_post_error(sub->resource,
3380 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3381 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003382 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003383 return NULL;
3384 }
3385
3386 if (sibling->parent != sub->parent) {
3387 wl_resource_post_error(sub->resource,
3388 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3389 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003390 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003391 return NULL;
3392 }
3393
3394 return sibling;
3395}
3396
3397static void
3398subsurface_place_above(struct wl_client *client,
3399 struct wl_resource *resource,
3400 struct wl_resource *sibling_resource)
3401{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003402 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003403 struct weston_surface *surface =
3404 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003405 struct weston_subsurface *sibling;
3406
3407 if (!sub)
3408 return;
3409
3410 sibling = subsurface_sibling_check(sub, surface, "place_above");
3411 if (!sibling)
3412 return;
3413
3414 wl_list_remove(&sub->parent_link_pending);
3415 wl_list_insert(sibling->parent_link_pending.prev,
3416 &sub->parent_link_pending);
3417}
3418
3419static void
3420subsurface_place_below(struct wl_client *client,
3421 struct wl_resource *resource,
3422 struct wl_resource *sibling_resource)
3423{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003424 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003425 struct weston_surface *surface =
3426 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003427 struct weston_subsurface *sibling;
3428
3429 if (!sub)
3430 return;
3431
3432 sibling = subsurface_sibling_check(sub, surface, "place_below");
3433 if (!sibling)
3434 return;
3435
3436 wl_list_remove(&sub->parent_link_pending);
3437 wl_list_insert(&sibling->parent_link_pending,
3438 &sub->parent_link_pending);
3439}
3440
3441static void
3442subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3443{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003444 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003445
3446 if (sub)
3447 sub->synchronized = 1;
3448}
3449
3450static void
3451subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3452{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003453 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003454
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003455 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003456 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003457
3458 /* If sub became effectively desynchronized, flush. */
3459 if (!weston_subsurface_is_synchronized(sub))
3460 weston_subsurface_synchronized_commit(sub);
3461 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003462}
3463
3464static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003465weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3466{
3467 wl_list_remove(&sub->parent_link);
3468 wl_list_remove(&sub->parent_link_pending);
3469 wl_list_remove(&sub->parent_destroy_listener.link);
3470 sub->parent = NULL;
3471}
3472
3473static void
3474weston_subsurface_destroy(struct weston_subsurface *sub);
3475
3476static void
3477subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3478{
3479 struct weston_subsurface *sub =
3480 container_of(listener, struct weston_subsurface,
3481 surface_destroy_listener);
3482 assert(data == &sub->surface->resource);
3483
3484 /* The protocol object (wl_resource) is left inert. */
3485 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003486 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003487
3488 weston_subsurface_destroy(sub);
3489}
3490
3491static void
3492subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3493{
3494 struct weston_subsurface *sub =
3495 container_of(listener, struct weston_subsurface,
3496 parent_destroy_listener);
3497 assert(data == &sub->parent->resource);
3498 assert(sub->surface != sub->parent);
3499
3500 if (weston_surface_is_mapped(sub->surface))
3501 weston_surface_unmap(sub->surface);
3502
3503 weston_subsurface_unlink_parent(sub);
3504}
3505
3506static void
3507subsurface_resource_destroy(struct wl_resource *resource)
3508{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003509 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003510
3511 if (sub)
3512 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003513}
3514
3515static void
3516subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3517{
3518 wl_resource_destroy(resource);
3519}
3520
3521static void
3522weston_subsurface_link_parent(struct weston_subsurface *sub,
3523 struct weston_surface *parent)
3524{
3525 sub->parent = parent;
3526 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003527 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003528 &sub->parent_destroy_listener);
3529
3530 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3531 wl_list_insert(&parent->subsurface_list_pending,
3532 &sub->parent_link_pending);
3533}
3534
3535static void
3536weston_subsurface_link_surface(struct weston_subsurface *sub,
3537 struct weston_surface *surface)
3538{
3539 sub->surface = surface;
3540 sub->surface_destroy_listener.notify =
3541 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003542 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003543 &sub->surface_destroy_listener);
3544}
3545
3546static void
3547weston_subsurface_destroy(struct weston_subsurface *sub)
3548{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003549 struct weston_view *view, *next;
3550
Pekka Paalanene67858b2013-04-25 13:57:42 +03003551 assert(sub->surface);
3552
3553 if (sub->resource) {
3554 assert(weston_surface_to_subsurface(sub->surface) == sub);
3555 assert(sub->parent_destroy_listener.notify ==
3556 subsurface_handle_parent_destroy);
3557
George Kiagiadakised04d382014-06-13 18:10:26 +02003558 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3559 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003560 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003561 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003562
Pekka Paalanene67858b2013-04-25 13:57:42 +03003563 if (sub->parent)
3564 weston_subsurface_unlink_parent(sub);
3565
Jason Ekstrand7b982072014-05-20 14:33:03 -05003566 weston_surface_state_fini(&sub->cached);
3567 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003568
3569 sub->surface->configure = NULL;
3570 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003571 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003572 } else {
3573 /* the dummy weston_subsurface for the parent itself */
3574 assert(sub->parent_destroy_listener.notify == NULL);
3575 wl_list_remove(&sub->parent_link);
3576 wl_list_remove(&sub->parent_link_pending);
3577 }
3578
3579 wl_list_remove(&sub->surface_destroy_listener.link);
3580 free(sub);
3581}
3582
3583static const struct wl_subsurface_interface subsurface_implementation = {
3584 subsurface_destroy,
3585 subsurface_set_position,
3586 subsurface_place_above,
3587 subsurface_place_below,
3588 subsurface_set_sync,
3589 subsurface_set_desync
3590};
3591
3592static struct weston_subsurface *
3593weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3594 struct weston_surface *parent)
3595{
3596 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003597 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003598
Bryce Harringtonde16d892014-11-20 22:21:57 -08003599 sub = zalloc(sizeof *sub);
3600 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003601 return NULL;
3602
Jason Ekstranda7af7042013-10-12 22:38:11 -05003603 wl_list_init(&sub->unused_views);
3604
Jason Ekstranda85118c2013-06-27 20:17:02 -05003605 sub->resource =
3606 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003607 if (!sub->resource) {
3608 free(sub);
3609 return NULL;
3610 }
3611
Jason Ekstranda85118c2013-06-27 20:17:02 -05003612 wl_resource_set_implementation(sub->resource,
3613 &subsurface_implementation,
3614 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003615 weston_subsurface_link_surface(sub, surface);
3616 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003617 weston_surface_state_init(&sub->cached);
3618 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003619 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003620
3621 return sub;
3622}
3623
3624/* Create a dummy subsurface for having the parent itself in its
3625 * sub-surface lists. Makes stacking order manipulation easy.
3626 */
3627static struct weston_subsurface *
3628weston_subsurface_create_for_parent(struct weston_surface *parent)
3629{
3630 struct weston_subsurface *sub;
3631
Bryce Harringtonde16d892014-11-20 22:21:57 -08003632 sub = zalloc(sizeof *sub);
3633 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003634 return NULL;
3635
3636 weston_subsurface_link_surface(sub, parent);
3637 sub->parent = parent;
3638 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3639 wl_list_insert(&parent->subsurface_list_pending,
3640 &sub->parent_link_pending);
3641
3642 return sub;
3643}
3644
3645static void
3646subcompositor_get_subsurface(struct wl_client *client,
3647 struct wl_resource *resource,
3648 uint32_t id,
3649 struct wl_resource *surface_resource,
3650 struct wl_resource *parent_resource)
3651{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003652 struct weston_surface *surface =
3653 wl_resource_get_user_data(surface_resource);
3654 struct weston_surface *parent =
3655 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003656 struct weston_subsurface *sub;
3657 static const char where[] = "get_subsurface: wl_subsurface@";
3658
3659 if (surface == parent) {
3660 wl_resource_post_error(resource,
3661 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3662 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003663 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003664 return;
3665 }
3666
3667 if (weston_surface_to_subsurface(surface)) {
3668 wl_resource_post_error(resource,
3669 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3670 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003671 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003672 return;
3673 }
3674
Pekka Paalanen50b67472014-10-01 15:02:41 +03003675 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3676 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003677 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003678
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003679 if (weston_surface_get_main_surface(parent) == surface) {
3680 wl_resource_post_error(resource,
3681 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3682 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003683 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003684 return;
3685 }
3686
Pekka Paalanene67858b2013-04-25 13:57:42 +03003687 /* make sure the parent is in its own list */
3688 if (wl_list_empty(&parent->subsurface_list)) {
3689 if (!weston_subsurface_create_for_parent(parent)) {
3690 wl_resource_post_no_memory(resource);
3691 return;
3692 }
3693 }
3694
3695 sub = weston_subsurface_create(id, surface, parent);
3696 if (!sub) {
3697 wl_resource_post_no_memory(resource);
3698 return;
3699 }
3700
3701 surface->configure = subsurface_configure;
3702 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003703 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003704}
3705
3706static void
3707subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3708{
3709 wl_resource_destroy(resource);
3710}
3711
3712static const struct wl_subcompositor_interface subcompositor_interface = {
3713 subcompositor_destroy,
3714 subcompositor_get_subsurface
3715};
3716
3717static void
3718bind_subcompositor(struct wl_client *client,
3719 void *data, uint32_t version, uint32_t id)
3720{
3721 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003722 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003723
Jason Ekstranda85118c2013-06-27 20:17:02 -05003724 resource =
3725 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003726 if (resource == NULL) {
3727 wl_client_post_no_memory(client);
3728 return;
3729 }
3730 wl_resource_set_implementation(resource, &subcompositor_interface,
3731 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003732}
3733
3734static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003735weston_compositor_dpms(struct weston_compositor *compositor,
3736 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003737{
3738 struct weston_output *output;
3739
3740 wl_list_for_each(output, &compositor->output_list, link)
3741 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003742 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003743}
3744
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003745WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003746weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003747{
Neil Roberts8b62e202013-09-30 13:14:47 +01003748 uint32_t old_state = compositor->state;
3749
3750 /* The state needs to be changed before emitting the wake
3751 * signal because that may try to schedule a repaint which
3752 * will not work if the compositor is still sleeping */
3753 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3754
3755 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003756 case WESTON_COMPOSITOR_SLEEPING:
3757 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3758 /* fall through */
3759 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003760 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003761 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003762 /* fall through */
3763 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003764 wl_event_source_timer_update(compositor->idle_source,
3765 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003766 }
3767}
3768
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003769WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003770weston_compositor_offscreen(struct weston_compositor *compositor)
3771{
3772 switch (compositor->state) {
3773 case WESTON_COMPOSITOR_OFFSCREEN:
3774 return;
3775 case WESTON_COMPOSITOR_SLEEPING:
3776 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3777 /* fall through */
3778 default:
3779 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3780 wl_event_source_timer_update(compositor->idle_source, 0);
3781 }
3782}
3783
3784WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003785weston_compositor_sleep(struct weston_compositor *compositor)
3786{
3787 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3788 return;
3789
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003790 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003791 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3792 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3793}
3794
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003795static int
3796idle_handler(void *data)
3797{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003798 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003799
3800 if (compositor->idle_inhibit)
3801 return 1;
3802
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003803 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003804 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003805
3806 return 1;
3807}
3808
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003809WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003810weston_plane_init(struct weston_plane *plane,
3811 struct weston_compositor *ec,
3812 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003813{
3814 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003815 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003816 plane->x = x;
3817 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003818 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003819
3820 /* Init the link so that the call to wl_list_remove() when releasing
3821 * the plane without ever stacking doesn't lead to a crash */
3822 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003823}
3824
3825WL_EXPORT void
3826weston_plane_release(struct weston_plane *plane)
3827{
Xiong Zhang97116532013-10-23 13:58:31 +08003828 struct weston_view *view;
3829
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003830 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003831 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003832
Xiong Zhang97116532013-10-23 13:58:31 +08003833 wl_list_for_each(view, &plane->compositor->view_list, link) {
3834 if (view->plane == plane)
3835 view->plane = NULL;
3836 }
3837
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003838 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003839}
3840
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003841WL_EXPORT void
3842weston_compositor_stack_plane(struct weston_compositor *ec,
3843 struct weston_plane *plane,
3844 struct weston_plane *above)
3845{
3846 if (above)
3847 wl_list_insert(above->link.prev, &plane->link);
3848 else
3849 wl_list_insert(&ec->plane_list, &plane->link);
3850}
3851
Casey Dahlin9074db52012-04-19 22:50:09 -04003852static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003853{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003854 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003855}
3856
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003857static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003858bind_output(struct wl_client *client,
3859 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003860{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003861 struct weston_output *output = data;
3862 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003863 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003864
Jason Ekstranda85118c2013-06-27 20:17:02 -05003865 resource = wl_resource_create(client, &wl_output_interface,
3866 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003867 if (resource == NULL) {
3868 wl_client_post_no_memory(client);
3869 return;
3870 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003871
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003872 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003873 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003874
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003875 wl_output_send_geometry(resource,
3876 output->x,
3877 output->y,
3878 output->mm_width,
3879 output->mm_height,
3880 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003881 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003882 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003883 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003884 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003885 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003886
3887 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003888 wl_output_send_mode(resource,
3889 mode->flags,
3890 mode->width,
3891 mode->height,
3892 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003893 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003894
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003895 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003896 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003897}
3898
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003899/* Move other outputs when one is removed so the space remains contiguos. */
3900static void
3901weston_compositor_remove_output(struct weston_compositor *compositor,
3902 struct weston_output *remove_output)
3903{
3904 struct weston_output *output;
3905 int offset = 0;
3906
3907 wl_list_for_each(output, &compositor->output_list, link) {
3908 if (output == remove_output) {
3909 offset = output->width;
3910 continue;
3911 }
3912
3913 if (offset > 0) {
3914 weston_output_move(output,
3915 output->x - offset, output->y);
3916 output->dirty = 1;
3917 }
3918 }
3919}
3920
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003921WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003922weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003923{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003924 struct wl_resource *resource;
3925
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003926 output->destroying = 1;
3927
Pekka Paalanen0513a952014-05-21 16:17:27 +03003928 wl_event_source_remove(output->repaint_timer);
3929
Pekka Paalanen133e4392014-09-23 22:08:46 -04003930 weston_presentation_feedback_discard_list(&output->feedback_list);
3931
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003932 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003933 wl_list_remove(&output->link);
3934
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003935 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003936 wl_signal_emit(&output->destroy_signal, output);
3937
Richard Hughesafe690c2013-05-02 10:10:04 +01003938 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003939 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003940 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003941 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003942
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003943 wl_resource_for_each(resource, &output->resource_list) {
3944 wl_resource_set_destructor(resource, NULL);
3945 }
3946
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003947 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003948}
3949
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003950WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003951weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003952{
Scott Moreau850ca422012-05-21 15:21:25 -06003953 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003954
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003955 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003956 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003957
Scott Moreauccbf29d2012-02-22 14:21:41 -07003958 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003959 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003960 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003961 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003962 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003963 weston_matrix_scale(&output->matrix, magnification,
3964 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003965 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003966
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003967 switch (output->transform) {
3968 case WL_OUTPUT_TRANSFORM_FLIPPED:
3969 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3970 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3971 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3972 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3973 weston_matrix_scale(&output->matrix, -1, 1, 1);
3974 break;
3975 }
3976
3977 switch (output->transform) {
3978 default:
3979 case WL_OUTPUT_TRANSFORM_NORMAL:
3980 case WL_OUTPUT_TRANSFORM_FLIPPED:
3981 break;
3982 case WL_OUTPUT_TRANSFORM_90:
3983 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3984 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
3985 weston_matrix_rotate_xy(&output->matrix, 0, 1);
3986 break;
3987 case WL_OUTPUT_TRANSFORM_180:
3988 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3989 weston_matrix_translate(&output->matrix,
3990 -output->width, -output->height, 0);
3991 weston_matrix_rotate_xy(&output->matrix, -1, 0);
3992 break;
3993 case WL_OUTPUT_TRANSFORM_270:
3994 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3995 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3996 weston_matrix_rotate_xy(&output->matrix, 0, -1);
3997 break;
3998 }
3999
4000 if (output->current_scale != 1)
4001 weston_matrix_scale(&output->matrix,
4002 output->current_scale,
4003 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004004
Scott Moreauccbf29d2012-02-22 14:21:41 -07004005 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004006
4007 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004008}
4009
Scott Moreau1bad5db2012-08-18 01:04:05 -06004010static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004011weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004012{
4013 output->transform = transform;
4014
4015 switch (transform) {
4016 case WL_OUTPUT_TRANSFORM_90:
4017 case WL_OUTPUT_TRANSFORM_270:
4018 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4019 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4020 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02004021 output->width = output->current_mode->height;
4022 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004023 break;
4024 case WL_OUTPUT_TRANSFORM_NORMAL:
4025 case WL_OUTPUT_TRANSFORM_180:
4026 case WL_OUTPUT_TRANSFORM_FLIPPED:
4027 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02004028 output->width = output->current_mode->width;
4029 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004030 break;
4031 default:
4032 break;
4033 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06004034
Hardening57388e42013-09-18 23:56:36 +02004035 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02004036 output->width /= scale;
4037 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02004038}
4039
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004040static void
4041weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004042{
4043 output->x = x;
4044 output->y = y;
4045
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004046 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004047 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004048 output->width,
4049 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004050}
4051
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004052WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004053weston_output_move(struct weston_output *output, int x, int y)
4054{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004055 struct wl_resource *resource;
4056
4057 output->move_x = x - output->x;
4058 output->move_y = y - output->y;
4059
4060 if (output->move_x == 0 && output->move_y == 0)
4061 return;
4062
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004063 weston_output_init_geometry(output, x, y);
4064
4065 output->dirty = 1;
4066
4067 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004068 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004069
4070 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004071 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004072 wl_output_send_geometry(resource,
4073 output->x,
4074 output->y,
4075 output->mm_width,
4076 output->mm_height,
4077 output->subpixel,
4078 output->make,
4079 output->model,
4080 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004081
4082 if (wl_resource_get_version(resource) >= 2)
4083 wl_output_send_done(resource);
4084 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004085}
4086
4087WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004088weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004089 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004090 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004091{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004092 struct wl_event_loop *loop;
4093
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004094 output->compositor = c;
4095 output->x = x;
4096 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004097 output->mm_width = mm_width;
4098 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004099 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004100 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004101
Alexander Larsson0b135062013-05-28 16:23:36 +02004102 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004103 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004104
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004105 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004106 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004107
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004108 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004109 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004110 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004111 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004112 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02004113
Pekka Paalanen0513a952014-05-21 16:17:27 +03004114 loop = wl_display_get_event_loop(c->wl_display);
4115 output->repaint_timer = wl_event_loop_add_timer(loop,
4116 output_repaint_timer_handler, output);
4117
Casey Dahlin58ba1372012-04-19 22:50:08 -04004118 output->id = ffs(~output->compositor->output_id_pool) - 1;
4119 output->compositor->output_id_pool |= 1 << output->id;
4120
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004121 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004122 wl_global_create(c->wl_display, &wl_output_interface, 2,
4123 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01004124 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004125}
4126
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004127WL_EXPORT void
4128weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05004129 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004130 wl_fixed_t *x, wl_fixed_t *y)
4131{
Derek Foreman0f679412014-10-02 13:41:17 -05004132 struct weston_vector p = { {
4133 wl_fixed_to_double(device_x),
4134 wl_fixed_to_double(device_y),
4135 0.0,
4136 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004137
Derek Foreman67a18b92015-03-24 11:36:14 -05004138 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004139
Derek Foreman0f679412014-10-02 13:41:17 -05004140 *x = wl_fixed_from_double(p.f[0] / p.f[3]);
4141 *y = wl_fixed_from_double(p.f[1] / p.f[3]);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004142}
4143
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004144static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004145destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004146{
Jonny Lamb74130762013-11-26 18:19:46 +01004147 struct weston_surface *surface =
4148 wl_resource_get_user_data(resource);
4149
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004150 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004151 surface->pending.buffer_viewport.buffer.src_width =
4152 wl_fixed_from_int(-1);
4153 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004154 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004155}
4156
4157static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004158viewport_destroy(struct wl_client *client,
4159 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004160{
4161 wl_resource_destroy(resource);
4162}
4163
4164static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004165viewport_set(struct wl_client *client,
4166 struct wl_resource *resource,
4167 wl_fixed_t src_x,
4168 wl_fixed_t src_y,
4169 wl_fixed_t src_width,
4170 wl_fixed_t src_height,
4171 int32_t dst_width,
4172 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004173{
Jonny Lamb74130762013-11-26 18:19:46 +01004174 struct weston_surface *surface =
4175 wl_resource_get_user_data(resource);
4176
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004177 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01004178
Jonny Lamb8ae35902013-11-26 18:19:45 +01004179 if (wl_fixed_to_double(src_width) < 0 ||
4180 wl_fixed_to_double(src_height) < 0) {
4181 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004182 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004183 "source dimensions must be non-negative (%fx%f)",
4184 wl_fixed_to_double(src_width),
4185 wl_fixed_to_double(src_height));
4186 return;
4187 }
4188
4189 if (dst_width <= 0 || dst_height <= 0) {
4190 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004191 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004192 "destination dimensions must be positive (%dx%d)",
4193 dst_width, dst_height);
4194 return;
4195 }
4196
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004197 surface->pending.buffer_viewport.buffer.src_x = src_x;
4198 surface->pending.buffer_viewport.buffer.src_y = src_y;
4199 surface->pending.buffer_viewport.buffer.src_width = src_width;
4200 surface->pending.buffer_viewport.buffer.src_height = src_height;
4201 surface->pending.buffer_viewport.surface.width = dst_width;
4202 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004203 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004204}
4205
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004206static void
4207viewport_set_source(struct wl_client *client,
4208 struct wl_resource *resource,
4209 wl_fixed_t src_x,
4210 wl_fixed_t src_y,
4211 wl_fixed_t src_width,
4212 wl_fixed_t src_height)
4213{
4214 struct weston_surface *surface =
4215 wl_resource_get_user_data(resource);
4216
4217 assert(surface->viewport_resource != NULL);
4218
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004219 if (src_width == wl_fixed_from_int(-1) &&
4220 src_height == wl_fixed_from_int(-1)) {
4221 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004222 surface->pending.buffer_viewport.buffer.src_width =
4223 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004224 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004225 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004226 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004227
4228 if (src_width <= 0 || src_height <= 0) {
4229 wl_resource_post_error(resource,
4230 WL_VIEWPORT_ERROR_BAD_VALUE,
4231 "source size must be positive (%fx%f)",
4232 wl_fixed_to_double(src_width),
4233 wl_fixed_to_double(src_height));
4234 return;
4235 }
4236
4237 surface->pending.buffer_viewport.buffer.src_x = src_x;
4238 surface->pending.buffer_viewport.buffer.src_y = src_y;
4239 surface->pending.buffer_viewport.buffer.src_width = src_width;
4240 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004241 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004242}
4243
4244static void
4245viewport_set_destination(struct wl_client *client,
4246 struct wl_resource *resource,
4247 int32_t dst_width,
4248 int32_t dst_height)
4249{
4250 struct weston_surface *surface =
4251 wl_resource_get_user_data(resource);
4252
4253 assert(surface->viewport_resource != NULL);
4254
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004255 if (dst_width == -1 && dst_height == -1) {
4256 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004257 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004258 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004259 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004260 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004261
4262 if (dst_width <= 0 || dst_height <= 0) {
4263 wl_resource_post_error(resource,
4264 WL_VIEWPORT_ERROR_BAD_VALUE,
4265 "destination size must be positive (%dx%d)",
4266 dst_width, dst_height);
4267 return;
4268 }
4269
4270 surface->pending.buffer_viewport.surface.width = dst_width;
4271 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004272 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004273}
4274
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004275static const struct wl_viewport_interface viewport_interface = {
4276 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004277 viewport_set,
4278 viewport_set_source,
4279 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004280};
4281
4282static void
4283scaler_destroy(struct wl_client *client,
4284 struct wl_resource *resource)
4285{
4286 wl_resource_destroy(resource);
4287}
4288
4289static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004290scaler_get_viewport(struct wl_client *client,
4291 struct wl_resource *scaler,
4292 uint32_t id,
4293 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004294{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004295 int version = wl_resource_get_version(scaler);
4296 struct weston_surface *surface =
4297 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004298 struct wl_resource *resource;
4299
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004300 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01004301 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004302 WL_SCALER_ERROR_VIEWPORT_EXISTS,
4303 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004304 return;
4305 }
4306
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004307 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004308 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004309 if (resource == NULL) {
4310 wl_client_post_no_memory(client);
4311 return;
4312 }
4313
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004314 wl_resource_set_implementation(resource, &viewport_interface,
4315 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004316
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004317 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004318}
4319
4320static const struct wl_scaler_interface scaler_interface = {
4321 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004322 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004323};
4324
4325static void
4326bind_scaler(struct wl_client *client,
4327 void *data, uint32_t version, uint32_t id)
4328{
4329 struct wl_resource *resource;
4330
4331 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004332 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004333 if (resource == NULL) {
4334 wl_client_post_no_memory(client);
4335 return;
4336 }
4337
4338 wl_resource_set_implementation(resource, &scaler_interface,
4339 NULL, NULL);
4340}
4341
4342static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004343destroy_presentation_feedback(struct wl_resource *feedback_resource)
4344{
4345 struct weston_presentation_feedback *feedback;
4346
4347 feedback = wl_resource_get_user_data(feedback_resource);
4348
4349 wl_list_remove(&feedback->link);
4350 free(feedback);
4351}
4352
4353static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004354presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4355{
4356 wl_resource_destroy(resource);
4357}
4358
4359static void
4360presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004361 struct wl_resource *presentation_resource,
4362 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004363 uint32_t callback)
4364{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004365 struct weston_surface *surface;
4366 struct weston_presentation_feedback *feedback;
4367
4368 surface = wl_resource_get_user_data(surface_resource);
4369
Bryce Harringtonde16d892014-11-20 22:21:57 -08004370 feedback = zalloc(sizeof *feedback);
4371 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004372 goto err_calloc;
4373
4374 feedback->resource = wl_resource_create(client,
4375 &presentation_feedback_interface,
4376 1, callback);
4377 if (!feedback->resource)
4378 goto err_create;
4379
4380 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4381 destroy_presentation_feedback);
4382
4383 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4384
4385 return;
4386
4387err_create:
4388 free(feedback);
4389
4390err_calloc:
4391 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004392}
4393
4394static const struct presentation_interface presentation_implementation = {
4395 presentation_destroy,
4396 presentation_feedback
4397};
4398
4399static void
4400bind_presentation(struct wl_client *client,
4401 void *data, uint32_t version, uint32_t id)
4402{
4403 struct weston_compositor *compositor = data;
4404 struct wl_resource *resource;
4405
4406 resource = wl_resource_create(client, &presentation_interface,
4407 MIN(version, 1), id);
4408 if (resource == NULL) {
4409 wl_client_post_no_memory(client);
4410 return;
4411 }
4412
4413 wl_resource_set_implementation(resource, &presentation_implementation,
4414 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004415 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004416}
4417
4418static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004419compositor_bind(struct wl_client *client,
4420 void *data, uint32_t version, uint32_t id)
4421{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004422 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004423 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004424
Jason Ekstranda85118c2013-06-27 20:17:02 -05004425 resource = wl_resource_create(client, &wl_compositor_interface,
4426 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004427 if (resource == NULL) {
4428 wl_client_post_no_memory(client);
4429 return;
4430 }
4431
4432 wl_resource_set_implementation(resource, &compositor_interface,
4433 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004434}
4435
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04004436static void
Martin Minarikf12c2872012-06-11 00:57:39 +02004437log_uname(void)
4438{
4439 struct utsname usys;
4440
4441 uname(&usys);
4442
4443 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
4444 usys.version, usys.machine);
4445}
4446
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004447WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004448weston_environment_get_fd(const char *env)
4449{
4450 char *e, *end;
4451 int fd, flags;
4452
4453 e = getenv(env);
4454 if (!e)
4455 return -1;
4456 fd = strtol(e, &end, 0);
4457 if (*end != '\0')
4458 return -1;
4459
4460 flags = fcntl(fd, F_GETFD);
4461 if (flags == -1)
4462 return -1;
4463
4464 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4465 unsetenv(env);
4466
4467 return fd;
4468}
4469
Pekka Paalanenb5026542014-11-12 15:09:24 +02004470static void
4471timeline_key_binding_handler(struct weston_seat *seat, uint32_t time,
4472 uint32_t key, void *data)
4473{
4474 struct weston_compositor *compositor = data;
4475
4476 if (weston_timeline_enabled_)
4477 weston_timeline_close();
4478 else
4479 weston_timeline_open(compositor);
4480}
4481
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004482WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04004483weston_compositor_init(struct weston_compositor *ec,
4484 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004485 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004486 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004487{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004488 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01004489 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004490 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004491
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004492 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04004493 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004494 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004495 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004496 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004497 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004498 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004499 wl_signal_init(&ec->idle_signal);
4500 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004501 wl_signal_init(&ec->show_input_panel_signal);
4502 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004503 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004504 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004505 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004506 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004507 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004508 wl_signal_init(&ec->session_signal);
4509 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004510
Casey Dahlin58ba1372012-04-19 22:50:08 -04004511 ec->output_id_pool = 0;
4512
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004513 if (!wl_global_create(display, &wl_compositor_interface, 3,
4514 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05004515 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004516
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004517 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
4518 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03004519 return -1;
4520
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004521 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004522 ec, bind_scaler))
4523 return -1;
4524
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004525 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4526 ec, bind_presentation))
4527 return -1;
4528
Jason Ekstranda7af7042013-10-12 22:38:11 -05004529 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004530 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004531 wl_list_init(&ec->layer_list);
4532 wl_list_init(&ec->seat_list);
4533 wl_list_init(&ec->output_list);
4534 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004535 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004536 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004537 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004538 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004539 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004540
Xiong Zhang97116532013-10-23 13:58:31 +08004541 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004542 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004543
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004544 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
4545 weston_config_section_get_string(s, "keymap_rules",
4546 (char **) &xkb_names.rules, NULL);
4547 weston_config_section_get_string(s, "keymap_model",
4548 (char **) &xkb_names.model, NULL);
4549 weston_config_section_get_string(s, "keymap_layout",
4550 (char **) &xkb_names.layout, NULL);
4551 weston_config_section_get_string(s, "keymap_variant",
4552 (char **) &xkb_names.variant, NULL);
4553 weston_config_section_get_string(s, "keymap_options",
4554 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01004555
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05004556 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
4557 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01004558
Jonny Lamb66a41a02014-08-12 14:58:25 +02004559 weston_config_section_get_int(s, "repeat-rate",
4560 &ec->kb_repeat_rate, 40);
4561 weston_config_section_get_int(s, "repeat-delay",
4562 &ec->kb_repeat_delay, 400);
4563
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004564 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01004565
4566 wl_data_device_manager_init(ec->wl_display);
4567
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04004568 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004569
Daniel Stone725c2c32012-06-22 14:04:36 +01004570 loop = wl_display_get_event_loop(ec->wl_display);
4571 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4572 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4573
4574 ec->input_loop = wl_event_loop_create();
4575
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004576 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4577 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4578
Pekka Paalanenb5026542014-11-12 15:09:24 +02004579 weston_compositor_add_debug_binding(ec, KEY_T,
4580 timeline_key_binding_handler, ec);
4581
Pekka Paalanen0513a952014-05-21 16:17:27 +03004582 s = weston_config_get_section(ec->config, "core", NULL, NULL);
4583 weston_config_section_get_int(s, "repaint-window", &ec->repaint_msec,
4584 DEFAULT_REPAINT_WINDOW);
4585 if (ec->repaint_msec < -10 || ec->repaint_msec > 1000) {
4586 weston_log("Invalid repaint_window value in config: %d\n",
4587 ec->repaint_msec);
4588 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
4589 }
4590 weston_log("Output repaint window is %d ms maximum.\n",
4591 ec->repaint_msec);
4592
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004593 weston_compositor_schedule_repaint(ec);
4594
Daniel Stone725c2c32012-06-22 14:04:36 +01004595 return 0;
4596}
4597
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004598WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004599weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004600{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004601 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004602
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004603 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004604 if (ec->input_loop_source)
4605 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004606
Matt Roper361d2ad2011-08-29 13:52:23 -07004607 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004608 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004609 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004610
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004611 if (ec->renderer)
4612 ec->renderer->destroy(ec);
4613
Daniel Stone325fc2d2012-05-30 16:31:58 +01004614 weston_binding_list_destroy_all(&ec->key_binding_list);
4615 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004616 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004617 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004618 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004619
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004620 weston_plane_release(&ec->primary_plane);
4621
Jonas Ådahlc97af922012-03-28 22:36:09 +02004622 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07004623
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004624 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07004625}
4626
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004627WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004628weston_compositor_exit_with_code(struct weston_compositor *compositor,
4629 int exit_code)
4630{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004631 if (compositor->exit_code == EXIT_SUCCESS)
4632 compositor->exit_code = exit_code;
4633
Frederic Plourdec336f062014-10-29 14:44:33 -04004634 wl_display_terminate(compositor->wl_display);
4635}
4636
4637WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004638weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4639 const struct weston_pointer_grab_interface *interface)
4640{
4641 struct weston_seat *seat;
4642
4643 ec->default_pointer_grab = interface;
4644 wl_list_for_each(seat, &ec->seat_list, link) {
4645 if (seat->pointer) {
4646 weston_pointer_set_default_grab(seat->pointer,
4647 interface);
4648 }
4649 }
4650}
4651
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004652WL_EXPORT int
4653weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4654 clockid_t clk_id)
4655{
4656 struct timespec ts;
4657
4658 if (clock_gettime(clk_id, &ts) < 0)
4659 return -1;
4660
4661 compositor->presentation_clock = clk_id;
4662
4663 return 0;
4664}
4665
4666/*
4667 * For choosing the software clock, when the display hardware or API
4668 * does not expose a compatible presentation timestamp.
4669 */
4670WL_EXPORT int
4671weston_compositor_set_presentation_clock_software(
4672 struct weston_compositor *compositor)
4673{
4674 /* In order of preference */
4675 static const clockid_t clocks[] = {
4676 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4677 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4678 CLOCK_MONOTONIC, /* no jumps, may crawl */
4679 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4680 CLOCK_REALTIME /* may jump and crawl */
4681 };
4682 unsigned i;
4683
4684 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4685 if (weston_compositor_set_presentation_clock(compositor,
4686 clocks[i]) == 0)
4687 return 0;
4688
4689 weston_log("Error: no suitable presentation clock available.\n");
4690
4691 return -1;
4692}
4693
Pekka Paalanen662f3842015-03-18 12:17:26 +02004694/** Read the current time from the Presentation clock
4695 *
4696 * \param compositor
4697 * \param ts[out] The current time.
4698 *
4699 * \note Reading the current time in user space is always imprecise to some
4700 * degree.
4701 *
4702 * This function is never meant to fail. If reading the clock does fail,
4703 * an error message is logged and a zero time is returned. Callers are not
4704 * supposed to detect or react to failures.
4705 */
4706WL_EXPORT void
4707weston_compositor_read_presentation_clock(
4708 const struct weston_compositor *compositor,
4709 struct timespec *ts)
4710{
4711 static bool warned;
4712 int ret;
4713
4714 ret = clock_gettime(compositor->presentation_clock, ts);
4715 if (ret < 0) {
4716 ts->tv_sec = 0;
4717 ts->tv_nsec = 0;
4718
4719 if (!warned)
4720 weston_log("Error: failure to read "
4721 "the presentation clock %#x: '%m' (%d)\n",
4722 compositor->presentation_clock, errno);
4723 warned = true;
4724 }
4725}
4726
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004727WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004728weston_version(int *major, int *minor, int *micro)
4729{
4730 *major = WESTON_VERSION_MAJOR;
4731 *minor = WESTON_VERSION_MINOR;
4732 *micro = WESTON_VERSION_MICRO;
4733}
4734
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004735static const char *
4736clock_name(clockid_t clk_id)
4737{
4738 static const char *names[] = {
4739 [CLOCK_REALTIME] = "CLOCK_REALTIME",
4740 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
4741 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
4742 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
4743 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
4744 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
4745 };
4746
4747 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
4748 return "unknown";
4749
4750 return names[clk_id];
4751}
4752
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004753static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004754 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004755 const char *desc;
4756} capability_strings[] = {
4757 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004758 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004759};
4760
4761static void
4762weston_compositor_log_capabilities(struct weston_compositor *compositor)
4763{
4764 unsigned i;
4765 int yes;
4766
4767 weston_log("Compositor capabilities:\n");
4768 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
4769 yes = compositor->capabilities & capability_strings[i].bit;
4770 weston_log_continue(STAMP_SPACE "%s %s\n",
4771 capability_strings[i].desc,
4772 yes ? "yes" : "no");
4773 }
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004774
4775 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
4776 clock_name(compositor->presentation_clock),
4777 compositor->presentation_clock);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004778}
4779
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004780static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004781{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004782 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004783
Martin Minarik6d118362012-06-07 18:01:59 +02004784 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004785 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004786
4787 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004788}
4789
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004790#ifdef HAVE_LIBUNWIND
4791
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004792static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004793print_backtrace(void)
4794{
4795 unw_cursor_t cursor;
4796 unw_context_t context;
4797 unw_word_t off;
4798 unw_proc_info_t pip;
4799 int ret, i = 0;
4800 char procname[256];
4801 const char *filename;
4802 Dl_info dlinfo;
4803
4804 pip.unwind_info = NULL;
4805 ret = unw_getcontext(&context);
4806 if (ret) {
4807 weston_log("unw_getcontext: %d\n", ret);
4808 return;
4809 }
4810
4811 ret = unw_init_local(&cursor, &context);
4812 if (ret) {
4813 weston_log("unw_init_local: %d\n", ret);
4814 return;
4815 }
4816
4817 ret = unw_step(&cursor);
4818 while (ret > 0) {
4819 ret = unw_get_proc_info(&cursor, &pip);
4820 if (ret) {
4821 weston_log("unw_get_proc_info: %d\n", ret);
4822 break;
4823 }
4824
4825 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4826 if (ret && ret != -UNW_ENOMEM) {
4827 if (ret != -UNW_EUNSPEC)
4828 weston_log("unw_get_proc_name: %d\n", ret);
4829 procname[0] = '?';
4830 procname[1] = 0;
4831 }
4832
4833 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4834 *dlinfo.dli_fname)
4835 filename = dlinfo.dli_fname;
4836 else
4837 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004838
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004839 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4840 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4841
4842 ret = unw_step(&cursor);
4843 if (ret < 0)
4844 weston_log("unw_step: %d\n", ret);
4845 }
4846}
4847
4848#else
4849
4850static void
4851print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004852{
4853 void *buffer[32];
4854 int i, count;
4855 Dl_info info;
4856
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004857 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4858 for (i = 0; i < count; i++) {
4859 dladdr(buffer[i], &info);
4860 weston_log(" [%016lx] %s (%s)\n",
4861 (long) buffer[i],
4862 info.dli_sname ? info.dli_sname : "--",
4863 info.dli_fname);
4864 }
4865}
4866
4867#endif
4868
4869static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004870on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004871{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004872 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004873 * then call the backend restore function, which will switch
4874 * back to the vt we launched from or ungrab X etc and then
4875 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004876 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004877 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004878 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004879
Peter Maatmane5b42e42013-03-27 22:38:53 +01004880 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004881
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004882 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004883
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004884 segv_compositor->restore(segv_compositor);
4885
4886 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004887}
4888
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004889WL_EXPORT void *
4890weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004891{
4892 char path[PATH_MAX];
4893 void *module, *init;
4894
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004895 if (name == NULL)
4896 return NULL;
4897
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004898 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004899 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004900 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004901 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004902
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004903 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4904 if (module) {
4905 weston_log("Module '%s' already loaded\n", path);
4906 dlclose(module);
4907 return NULL;
4908 }
4909
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004910 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004911 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004912 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004913 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004914 return NULL;
4915 }
4916
4917 init = dlsym(module, entrypoint);
4918 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004919 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004920 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004921 return NULL;
4922 }
4923
4924 return init;
4925}
4926
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004927static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004928load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004929 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004930{
4931 const char *p, *end;
4932 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004933 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004934 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004935
4936 if (modules == NULL)
4937 return 0;
4938
4939 p = modules;
4940 while (*p) {
4941 end = strchrnul(p, ',');
4942 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004943 module_init = weston_load_module(buffer, "module_init");
Ondřej Majerech01e98b62014-12-06 02:49:17 +01004944 if (!module_init)
4945 return -1;
4946 if (module_init(ec, argc, argv) < 0)
4947 return -1;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004948 p = end;
4949 while (*p == ',')
4950 p++;
4951
4952 }
4953
4954 return 0;
4955}
4956
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004957static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004958 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4959
4960static const char xdg_wrong_message[] =
4961 "fatal: environment variable XDG_RUNTIME_DIR\n"
4962 "is set to \"%s\", which is not a directory.\n";
4963
4964static const char xdg_wrong_mode_message[] =
4965 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004966 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4967 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004968
4969static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004970 "Refer to your distribution on how to get it, or\n"
4971 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4972 "on how to implement it.\n";
4973
Martin Minarik37032f82012-06-18 20:15:18 +02004974static void
4975verify_xdg_runtime_dir(void)
4976{
4977 char *dir = getenv("XDG_RUNTIME_DIR");
4978 struct stat s;
4979
4980 if (!dir) {
4981 weston_log(xdg_error_message);
4982 weston_log_continue(xdg_detail_message);
4983 exit(EXIT_FAILURE);
4984 }
4985
4986 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4987 weston_log(xdg_wrong_message, dir);
4988 weston_log_continue(xdg_detail_message);
4989 exit(EXIT_FAILURE);
4990 }
4991
4992 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4993 weston_log(xdg_wrong_mode_message,
4994 dir, s.st_mode & 0777, s.st_uid);
4995 weston_log_continue(xdg_detail_message);
4996 }
4997}
4998
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004999static int
5000usage(int error_code)
5001{
5002 fprintf(stderr,
5003 "Usage: weston [OPTIONS]\n\n"
5004 "This is weston version " VERSION ", the Wayland reference compositor.\n"
5005 "Weston supports multiple backends, and depending on which backend is in use\n"
5006 "different options will be accepted.\n\n"
5007
5008
5009 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06005010 " --version\t\tPrint weston version\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07005011 " -B, --backend=MODULE\tBackend module, one of\n"
5012#if defined(BUILD_DRM_COMPOSITOR)
5013 "\t\t\t\tdrm-backend.so\n"
5014#endif
5015#if defined(BUILD_FBDEV_COMPOSITOR)
Pekka Paalanen86b70e12014-11-11 14:10:46 +02005016 "\t\t\t\tfbdev-backend.so\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07005017#endif
5018#if defined(BUILD_X11_COMPOSITOR)
5019 "\t\t\t\tx11-backend.so\n"
5020#endif
5021#if defined(BUILD_WAYLAND_COMPOSITOR)
5022 "\t\t\t\twayland-backend.so\n"
5023#endif
5024#if defined(BUILD_RDP_COMPOSITOR)
5025 "\t\t\t\trdp-backend.so\n"
5026#endif
5027#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
5028 "\t\t\t\trpi-backend.so\n"
5029#endif
Jason Ekstranda985da42013-08-22 17:28:03 -05005030 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005031 " -S, --socket=NAME\tName of socket to listen on\n"
5032 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005033 " --modules\t\tLoad the comma-separated list of modules\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07005034 " --log=FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03005035 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005036 " -h, --help\t\tThis help message\n\n");
5037
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005038#if defined(BUILD_DRM_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005039 fprintf(stderr,
5040 "Options for drm-backend.so:\n\n"
5041 " --connector=ID\tBring up only this connector\n"
5042 " --seat=SEAT\t\tThe seat that weston should run on\n"
5043 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02005044 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005045 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005046#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005047
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005048#if defined(BUILD_FBDEV_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005049 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01005050 "Options for fbdev-backend.so:\n\n"
5051 " --tty=TTY\t\tThe tty to use\n"
5052 " --device=DEVICE\tThe framebuffer device to use\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005053#endif
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01005054
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005055#if defined(BUILD_X11_COMPOSITOR)
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01005056 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005057 "Options for x11-backend.so:\n\n"
5058 " --width=WIDTH\t\tWidth of X window\n"
5059 " --height=HEIGHT\tHeight of X window\n"
5060 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05005061 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005062 " --output-count=COUNT\tCreate multiple outputs\n"
5063 " --no-input\t\tDont create input devices\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005064#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005065
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005066#if defined(BUILD_WAYLAND_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005067 fprintf(stderr,
5068 "Options for wayland-backend.so:\n\n"
5069 " --width=WIDTH\t\tWidth of Wayland surface\n"
5070 " --height=HEIGHT\tHeight of Wayland surface\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07005071 " --scale=SCALE\t\tScale factor of output\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06005072 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05005073 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05005074 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05005075 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005076 " --display=DISPLAY\tWayland display to connect to\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005077#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005078
Pekka Paalanene31e0532013-05-22 18:03:07 +03005079#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
5080 fprintf(stderr,
5081 "Options for rpi-backend.so:\n\n"
5082 " --tty=TTY\t\tThe tty to use\n"
5083 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
5084 " --transform=TR\tThe output transformation, TR is one of:\n"
5085 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01005086 " --opaque-regions\tEnable support for opaque regions, can be "
5087 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03005088 "\n");
5089#endif
5090
Hardeningc077a842013-07-08 00:51:35 +02005091#if defined(BUILD_RDP_COMPOSITOR)
Bryce Harrington59fe4232014-10-23 14:44:34 -07005092 fprintf(stderr,
5093 "Options for rdp-backend.so:\n\n"
5094 " --width=WIDTH\t\tWidth of desktop\n"
5095 " --height=HEIGHT\tHeight of desktop\n"
5096 " --env-socket=SOCKET\tUse that socket as peer connection\n"
5097 " --address=ADDR\tThe address to bind\n"
Bryce Harringtonf5b34ae2014-10-23 14:44:35 -07005098 " --port=PORT\t\tThe port to listen on\n"
Bryce Harrington59fe4232014-10-23 14:44:34 -07005099 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
5100 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
5101 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
5102 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
5103 "\n");
Hardeningc077a842013-07-08 00:51:35 +02005104#endif
5105
Bryce Harrington3e3b59e2014-11-19 15:06:19 -08005106#if defined(BUILD_HEADLESS_COMPOSITOR)
5107 fprintf(stderr,
5108 "Options for headless-backend.so:\n\n"
5109 " --width=WIDTH\t\tWidth of memory surface\n"
5110 " --height=HEIGHT\tHeight of memory surface\n"
5111 " --transform=TR\tThe output transformation, TR is one of:\n"
5112 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
5113 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
5114#endif
5115
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005116 exit(error_code);
5117}
5118
Peter Maatmane5b42e42013-03-27 22:38:53 +01005119static void
5120catch_signals(void)
5121{
5122 struct sigaction action;
5123
5124 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
5125 action.sa_sigaction = on_caught_signal;
5126 sigemptyset(&action.sa_mask);
5127 sigaction(SIGSEGV, &action, NULL);
5128 sigaction(SIGABRT, &action, NULL);
5129}
5130
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005131static void
5132handle_primary_client_destroyed(struct wl_listener *listener, void *data)
5133{
5134 struct wl_client *client = data;
5135
5136 weston_log("Primary client died. Closing...\n");
5137
5138 wl_display_terminate(wl_client_get_display(client));
5139}
5140
Ryo Munakatad8deff62014-09-06 07:32:05 +09005141static char *
5142weston_choose_default_backend(void)
5143{
5144 char *backend = NULL;
5145
5146 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
5147 backend = strdup("wayland-backend.so");
5148 else if (getenv("DISPLAY"))
5149 backend = strdup("x11-backend.so");
5150 else
5151 backend = strdup(WESTON_NATIVE_BACKEND);
5152
5153 return backend;
5154}
5155
5156static int
5157weston_create_listening_socket(struct wl_display *display, const char *socket_name)
5158{
5159 if (socket_name) {
5160 if (wl_display_add_socket(display, socket_name)) {
5161 weston_log("fatal: failed to add socket: %m\n");
5162 return -1;
5163 }
5164 } else {
5165 socket_name = wl_display_add_socket_auto(display);
5166 if (!socket_name) {
5167 weston_log("fatal: failed to add socket: %m\n");
5168 return -1;
5169 }
5170 }
5171
5172 setenv("WAYLAND_DISPLAY", socket_name, 1);
5173
5174 return 0;
5175}
5176
Derek Foreman64a3df02014-10-23 12:24:18 -05005177static const struct { const char *name; uint32_t token; } transforms[] = {
5178 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
5179 { "90", WL_OUTPUT_TRANSFORM_90 },
5180 { "180", WL_OUTPUT_TRANSFORM_180 },
5181 { "270", WL_OUTPUT_TRANSFORM_270 },
5182 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
5183 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
5184 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
5185 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
5186};
5187
5188WL_EXPORT int
5189weston_parse_transform(const char *transform, uint32_t *out)
5190{
5191 unsigned int i;
5192
5193 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
5194 if (strcmp(transforms[i].name, transform) == 0) {
5195 *out = transforms[i].token;
5196 return 0;
5197 }
5198
5199 *out = WL_OUTPUT_TRANSFORM_NORMAL;
5200 return -1;
5201}
5202
5203WL_EXPORT const char *
5204weston_transform_to_string(uint32_t output_transform)
5205{
5206 unsigned int i;
5207
5208 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
5209 if (transforms[i].token == output_transform)
5210 return transforms[i].name;
5211
5212 return "<illegal value>";
5213}
5214
5215
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005216int main(int argc, char *argv[])
5217{
Pekka Paalanen111c6f92015-03-20 14:35:58 +02005218 int ret = EXIT_FAILURE;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005219 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005220 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005221 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05005222 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005223 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005224 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005225 int *argc, char *argv[],
5226 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005227 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005228 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05005229 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02005230 char *modules = NULL;
5231 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02005232 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005233 char *server_socket = NULL, *end;
Frederic Plourde4a84c832014-10-30 15:06:34 -04005234 int32_t idle_time = -1;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005235 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09005236 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06005237 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03005238 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005239 int32_t numlock_on;
Pekka Paalanen588bee12014-05-07 16:26:25 +03005240 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005241 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005242 struct wl_client *primary_client;
5243 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005244 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04005245
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005246 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09005247 { WESTON_OPTION_STRING, "backend", 'B', &backend },
5248 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005249 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
5250 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005251 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02005252 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005253 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06005254 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03005255 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04005256 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05005257
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005258 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005259
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005260 if (help)
5261 usage(EXIT_SUCCESS);
5262
Scott Moreau12245142012-08-29 15:15:58 -06005263 if (version) {
5264 printf(PACKAGE_STRING "\n");
5265 return EXIT_SUCCESS;
5266 }
5267
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02005268 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005269
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03005270 weston_log("%s\n"
5271 STAMP_SPACE "%s\n"
5272 STAMP_SPACE "Bug reports to: %s\n"
5273 STAMP_SPACE "Build: %s\n",
5274 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04005275 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03005276 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04005277
Martin Minarik37032f82012-06-18 20:15:18 +02005278 verify_xdg_runtime_dir();
5279
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005280 display = wl_display_create();
5281
Tiago Vignatti2116b892011-08-08 05:52:59 -07005282 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005283 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
5284 display);
5285 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
5286 display);
5287 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
5288 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07005289
5290 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005291 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
5292 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05005293
Pekka Paalanen111c6f92015-03-20 14:35:58 +02005294 if (!signals[0] || !signals[1] || !signals[2] || !signals[3])
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305295 goto out_signals;
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305296
Pekka Paalanen588bee12014-05-07 16:26:25 +03005297 if (noconfig == 0)
5298 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01005299 if (config != NULL) {
5300 weston_log("Using config file '%s'\n",
5301 weston_config_get_full_path(config));
5302 } else {
5303 weston_log("Starting with no config file.\n");
5304 }
5305 section = weston_config_get_section(config, "core", NULL, NULL);
5306
Ryo Munakata03faed22014-09-06 07:32:51 +09005307 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005308 weston_config_section_get_string(section, "backend", &backend,
5309 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09005310 if (!backend)
5311 backend = weston_choose_default_backend();
5312 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005313
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03005314 backend_init = weston_load_module(backend, "backend_init");
Pekka Paalanen111c6f92015-03-20 14:35:58 +02005315 if (!backend_init)
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305316 goto out_signals;
Kristian Høgsberga9410222011-01-14 17:22:35 -05005317
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005318 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05005319 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03005320 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305321 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05005322 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04005323
Peter Maatmane5b42e42013-03-27 22:38:53 +01005324 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04005325 segv_compositor = ec;
5326
Frederic Plourde4a84c832014-10-30 15:06:34 -04005327 if (idle_time < 0)
5328 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
5329 if (idle_time < 0)
5330 idle_time = 300; /* default idle timeout, in seconds */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005331 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005332 ec->default_pointer_grab = NULL;
Frederic Plourdec336f062014-10-29 14:44:33 -04005333 ec->exit_code = EXIT_SUCCESS;
Pekka Paalanen7296e792011-12-07 16:22:00 +02005334
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005335 for (i = 1; i < argc; i++)
5336 weston_log("fatal: unhandled option: %s\n", argv[i]);
Pekka Paalanen111c6f92015-03-20 14:35:58 +02005337 if (argc > 1)
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005338 goto out;
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005339
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005340 weston_compositor_log_capabilities(ec);
5341
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005342 server_socket = getenv("WAYLAND_SERVER_SOCKET");
5343 if (server_socket) {
5344 weston_log("Running with single client\n");
5345 fd = strtol(server_socket, &end, 0);
5346 if (*end != '\0')
5347 fd = -1;
5348 } else {
5349 fd = -1;
5350 }
5351
5352 if (fd != -1) {
5353 primary_client = wl_client_create(display, fd);
5354 if (!primary_client) {
5355 weston_log("fatal: failed to add client: %m\n");
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005356 goto out;
5357 }
5358 primary_client_destroyed.notify =
5359 handle_primary_client_destroyed;
5360 wl_client_add_destroy_listener(primary_client,
5361 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09005362 } else if (weston_create_listening_socket(display, socket_name)) {
Ryo Munakatad8deff62014-09-06 07:32:05 +09005363 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005364 }
5365
Ryo Munakata03faed22014-09-06 07:32:51 +09005366 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005367 weston_config_section_get_string(section, "shell", &shell,
5368 "desktop-shell.so");
5369
Ryo Munakata03faed22014-09-06 07:32:51 +09005370 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005371 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005372
5373 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09005374 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005375 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005376
5377 if (load_modules(ec, option_modules, &argc, argv) < 0)
5378 goto out;
5379
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005380 section = weston_config_get_section(config, "keyboard", NULL, NULL);
5381 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
5382 if (numlock_on) {
5383 wl_list_for_each(seat, &ec->seat_list, link) {
5384 if (seat->keyboard)
5385 weston_keyboard_set_locks(seat->keyboard,
5386 WESTON_NUM_LOCK,
5387 WESTON_NUM_LOCK);
5388 }
5389 }
5390
Pekka Paalanenc0444e32012-01-05 16:28:21 +02005391 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005392
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04005393 wl_display_run(display);
5394
Frederic Plourdec336f062014-10-29 14:44:33 -04005395 /* Allow for setting return exit code after
5396 * wl_display_run returns normally. This is
5397 * useful for devs/testers and automated tests
5398 * that want to indicate failure status to
5399 * testing infrastructure above
5400 */
5401 ret = ec->exit_code;
5402
Ryo Munakata03faed22014-09-06 07:32:51 +09005403out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005404 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005405 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005406
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005407 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005408
Daniel Stone855028f2012-05-01 20:37:10 +01005409 weston_compositor_xkb_destroy(ec);
5410
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005411 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305412
5413out_signals:
5414 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
5415 if (signals[i])
5416 wl_event_source_remove(signals[i]);
5417
Tiago Vignatti9e2be082011-12-19 00:04:46 +02005418 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005419
Martin Minarik19e6f262012-06-07 13:08:46 +02005420 weston_log_file_close();
5421
Ryo Munakata03faed22014-09-06 07:32:51 +09005422 free(backend);
5423 free(shell);
5424 free(socket_name);
5425 free(option_modules);
5426 free(log);
5427 free(modules);
5428
Pekka Paalanen3ab72692012-06-08 17:27:28 +03005429 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04005430}