blob: c94f00ff1efa6803f9628045668f624cbacbc250 [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
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001877 assert(surface->resource == NULL);
1878
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001879 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1880
Pekka Paalanene67858b2013-04-25 13:57:42 +03001881 assert(wl_list_empty(&surface->subsurface_list_pending));
1882 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001883
Jason Ekstranda7af7042013-10-12 22:38:11 -05001884 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1885 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001886
Jason Ekstrand7b982072014-05-20 14:33:03 -05001887 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001888
Pekka Paalanende685b82012-12-04 15:58:12 +02001889 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001890
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001891 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001892 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001893 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001894
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001895 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001896 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001897
Pekka Paalanen133e4392014-09-23 22:08:46 -04001898 weston_presentation_feedback_discard_list(&surface->feedback_list);
1899
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001900 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001901}
1902
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001903static void
1904destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001905{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001906 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001907
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001908 assert(surface);
1909
Giulio Camuffo0d379742013-11-15 22:06:15 +01001910 /* Set the resource to NULL, since we don't want to leave a
1911 * dangling pointer if the surface was refcounted and survives
1912 * the weston_surface_destroy() call. */
1913 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001914 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001915}
1916
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001917static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001918weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1919{
1920 struct weston_buffer *buffer =
1921 container_of(listener, struct weston_buffer, destroy_listener);
1922
1923 wl_signal_emit(&buffer->destroy_signal, buffer);
1924 free(buffer);
1925}
1926
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001927WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001928weston_buffer_from_resource(struct wl_resource *resource)
1929{
1930 struct weston_buffer *buffer;
1931 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001932
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001933 listener = wl_resource_get_destroy_listener(resource,
1934 weston_buffer_destroy_handler);
1935
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001936 if (listener)
1937 return container_of(listener, struct weston_buffer,
1938 destroy_listener);
1939
1940 buffer = zalloc(sizeof *buffer);
1941 if (buffer == NULL)
1942 return NULL;
1943
1944 buffer->resource = resource;
1945 wl_signal_init(&buffer->destroy_signal);
1946 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001947 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001948 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001949
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001950 return buffer;
1951}
1952
1953static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001954weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1955 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001956{
Pekka Paalanende685b82012-12-04 15:58:12 +02001957 struct weston_buffer_reference *ref =
1958 container_of(listener, struct weston_buffer_reference,
1959 destroy_listener);
1960
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001961 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001962 ref->buffer = NULL;
1963}
1964
1965WL_EXPORT void
1966weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001967 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001968{
1969 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001970 ref->buffer->busy_count--;
1971 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001972 assert(wl_resource_get_client(ref->buffer->resource));
1973 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001974 WL_BUFFER_RELEASE);
1975 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001976 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001977 }
1978
Pekka Paalanende685b82012-12-04 15:58:12 +02001979 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001980 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001981 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001982 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001983 }
1984
Pekka Paalanende685b82012-12-04 15:58:12 +02001985 ref->buffer = buffer;
1986 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1987}
1988
1989static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001990weston_surface_attach(struct weston_surface *surface,
1991 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001992{
1993 weston_buffer_reference(&surface->buffer_ref, buffer);
1994
Pekka Paalanena6421c42012-12-04 15:58:10 +02001995 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001996 if (weston_surface_is_mapped(surface))
1997 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001998 }
1999
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002000 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002001
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002002 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002003 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002004}
2005
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002006WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002007weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002008{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002009 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002010
2011 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002012 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002013}
2014
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002015WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002016weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002017{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002018 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002019
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002020 pixman_region32_union(&compositor->primary_plane.damage,
2021 &compositor->primary_plane.damage,
2022 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002023 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002024}
2025
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002026static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002027surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002028{
Pekka Paalanende685b82012-12-04 15:58:12 +02002029 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002030 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002031 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002032
Pekka Paalanenb5026542014-11-12 15:09:24 +02002033 if (weston_timeline_enabled_ &&
2034 pixman_region32_not_empty(&surface->damage))
2035 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2036 TLP_OUTPUT(surface->output), TLP_END);
2037
Jason Ekstrandef540082014-06-26 10:37:36 -07002038 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002039}
2040
2041static void
2042view_accumulate_damage(struct weston_view *view,
2043 pixman_region32_t *opaque)
2044{
2045 pixman_region32_t damage;
2046
2047 pixman_region32_init(&damage);
2048 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002049 pixman_box32_t *extents;
2050
Jason Ekstranda7af7042013-10-12 22:38:11 -05002051 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002052 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002053 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002054 pixman_region32_copy(&damage, &view->surface->damage);
2055 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002056 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002057 }
2058
Pekka Paalanen380adf52015-02-16 14:39:11 +02002059 pixman_region32_intersect(&damage, &damage,
2060 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002061 pixman_region32_subtract(&damage, &damage, opaque);
2062 pixman_region32_union(&view->plane->damage,
2063 &view->plane->damage, &damage);
2064 pixman_region32_fini(&damage);
2065 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002066 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002067}
2068
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002069static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002070compositor_accumulate_damage(struct weston_compositor *ec)
2071{
2072 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002073 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002074 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002075
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002076 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002077
2078 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002079 pixman_region32_copy(&plane->clip, &clip);
2080
2081 pixman_region32_init(&opaque);
2082
Jason Ekstranda7af7042013-10-12 22:38:11 -05002083 wl_list_for_each(ev, &ec->view_list, link) {
2084 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002085 continue;
2086
Jason Ekstranda7af7042013-10-12 22:38:11 -05002087 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002088 }
2089
2090 pixman_region32_union(&clip, &clip, &opaque);
2091 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002092 }
2093
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002094 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002095
Jason Ekstranda7af7042013-10-12 22:38:11 -05002096 wl_list_for_each(ev, &ec->view_list, link)
2097 ev->surface->touched = 0;
2098
2099 wl_list_for_each(ev, &ec->view_list, link) {
2100 if (ev->surface->touched)
2101 continue;
2102 ev->surface->touched = 1;
2103
2104 surface_flush_damage(ev->surface);
2105
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002106 /* Both the renderer and the backend have seen the buffer
2107 * by now. If renderer needs the buffer, it has its own
2108 * reference set. If the backend wants to keep the buffer
2109 * around for migrating the surface into a non-primary plane
2110 * later, keep_buffer is true. Otherwise, drop the core
2111 * reference now, and allow early buffer release. This enables
2112 * clients to use single-buffering.
2113 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002114 if (!ev->surface->keep_buffer)
2115 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002116 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002117}
2118
2119static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002120surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002121{
2122 struct weston_subsurface *sub;
2123
Pekka Paalanene67858b2013-04-25 13:57:42 +03002124 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002125 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002126 continue;
2127
Jason Ekstranda7af7042013-10-12 22:38:11 -05002128 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2129 wl_list_init(&sub->surface->views);
2130
2131 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002132 }
2133}
2134
2135static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002136surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002137{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002138 struct weston_subsurface *sub;
2139 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002140
Jason Ekstranda7af7042013-10-12 22:38:11 -05002141 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2142 if (sub->surface == surface)
2143 continue;
2144
George Kiagiadakised04d382014-06-13 18:10:26 +02002145 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2146 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002147 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002148 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002149
2150 surface_free_unused_subsurface_views(sub->surface);
2151 }
2152}
2153
2154static void
2155view_list_add_subsurface_view(struct weston_compositor *compositor,
2156 struct weston_subsurface *sub,
2157 struct weston_view *parent)
2158{
2159 struct weston_subsurface *child;
2160 struct weston_view *view = NULL, *iv;
2161
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002162 if (!weston_surface_is_mapped(sub->surface))
2163 return;
2164
Jason Ekstranda7af7042013-10-12 22:38:11 -05002165 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2166 if (iv->geometry.parent == parent) {
2167 view = iv;
2168 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002169 }
2170 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002171
2172 if (view) {
2173 /* Put it back in the surface's list of views */
2174 wl_list_remove(&view->surface_link);
2175 wl_list_insert(&sub->surface->views, &view->surface_link);
2176 } else {
2177 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002178 weston_view_set_position(view,
2179 sub->position.x,
2180 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002181 weston_view_set_transform_parent(view, parent);
2182 }
2183
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002184 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002185 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002186
Pekka Paalanenb188e912013-11-19 14:03:35 +02002187 if (wl_list_empty(&sub->surface->subsurface_list)) {
2188 wl_list_insert(compositor->view_list.prev, &view->link);
2189 return;
2190 }
2191
2192 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2193 if (child->surface == sub->surface)
2194 wl_list_insert(compositor->view_list.prev, &view->link);
2195 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002196 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002197 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002198}
2199
2200static void
2201view_list_add(struct weston_compositor *compositor,
2202 struct weston_view *view)
2203{
2204 struct weston_subsurface *sub;
2205
2206 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002207
Pekka Paalanenb188e912013-11-19 14:03:35 +02002208 if (wl_list_empty(&view->surface->subsurface_list)) {
2209 wl_list_insert(compositor->view_list.prev, &view->link);
2210 return;
2211 }
2212
2213 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2214 if (sub->surface == view->surface)
2215 wl_list_insert(compositor->view_list.prev, &view->link);
2216 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002217 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002218 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002219}
2220
2221static void
2222weston_compositor_build_view_list(struct weston_compositor *compositor)
2223{
2224 struct weston_view *view;
2225 struct weston_layer *layer;
2226
2227 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002228 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002229 surface_stash_subsurface_views(view->surface);
2230
2231 wl_list_init(&compositor->view_list);
2232 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002233 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002234 view_list_add(compositor, view);
2235 }
2236 }
2237
2238 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002239 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002240 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002241}
2242
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002243static void
2244weston_output_take_feedback_list(struct weston_output *output,
2245 struct weston_surface *surface)
2246{
2247 struct weston_view *view;
2248 struct weston_presentation_feedback *feedback;
2249 uint32_t flags = 0xffffffff;
2250
2251 if (wl_list_empty(&surface->feedback_list))
2252 return;
2253
2254 /* All views must have the flag for the flag to survive. */
2255 wl_list_for_each(view, &surface->views, surface_link) {
2256 /* ignore views that are not on this output at all */
2257 if (view->output_mask & (1u << output->id))
2258 flags &= view->psf_flags;
2259 }
2260
2261 wl_list_for_each(feedback, &surface->feedback_list, link)
2262 feedback->psf_flags = flags;
2263
2264 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2265 wl_list_init(&surface->feedback_list);
2266}
2267
David Herrmann1edf44c2013-10-22 17:11:26 +02002268static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002269weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002270{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002271 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002272 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002273 struct weston_animation *animation, *next;
2274 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002275 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002276 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002277 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002278
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002279 if (output->destroying)
2280 return 0;
2281
Pekka Paalanenb5026542014-11-12 15:09:24 +02002282 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2283
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002284 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002285 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002286
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002287 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002288 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002289 } else {
2290 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002291 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002292 ev->psf_flags = 0;
2293 }
2294 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002295
Pekka Paalanene67858b2013-04-25 13:57:42 +03002296 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002297 wl_list_for_each(ev, &ec->view_list, link) {
2298 /* Note: This operation is safe to do multiple times on the
2299 * same surface.
2300 */
2301 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002302 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002303 &ev->surface->frame_callback_list);
2304 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002305
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002306 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002307 }
2308 }
2309
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002310 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002311
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002312 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002313 pixman_region32_intersect(&output_damage,
2314 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002315 pixman_region32_subtract(&output_damage,
2316 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002317
Scott Moreauccbf29d2012-02-22 14:21:41 -07002318 if (output->dirty)
2319 weston_output_update_matrix(output);
2320
David Herrmann1edf44c2013-10-22 17:11:26 +02002321 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002322
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002323 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002324
Kristian Høgsbergef044142011-06-21 15:02:12 -04002325 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002326
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002327 weston_compositor_repick(ec);
2328 wl_event_loop_dispatch(ec->input_loop, 0);
2329
Jonas Ådahldb773762012-06-13 00:01:21 +02002330 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002331 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002332 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002333 }
2334
Scott Moreaud64cf212012-06-08 19:40:54 -06002335 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002336 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002337 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002338 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002339
Pekka Paalanenb5026542014-11-12 15:09:24 +02002340 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2341
David Herrmann1edf44c2013-10-22 17:11:26 +02002342 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002343}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002344
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002345static int
2346weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002347{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002348 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002349
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002350 wl_event_loop_dispatch(compositor->input_loop, 0);
2351
2352 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002353}
2354
Pekka Paalanen82919792014-05-21 13:51:49 +03002355static void
2356weston_output_schedule_repaint_reset(struct weston_output *output)
2357{
2358 struct weston_compositor *compositor = output->compositor;
2359 struct wl_event_loop *loop;
2360 int fd;
2361
2362 output->repaint_scheduled = 0;
2363 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2364
2365 if (compositor->input_loop_source)
2366 return;
2367
2368 loop = wl_display_get_event_loop(compositor->wl_display);
2369 fd = wl_event_loop_get_fd(compositor->input_loop);
2370 compositor->input_loop_source =
2371 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2372 weston_compositor_read_input, compositor);
2373}
2374
Pekka Paalanen0513a952014-05-21 16:17:27 +03002375static int
2376output_repaint_timer_handler(void *data)
2377{
2378 struct weston_output *output = data;
2379 struct weston_compositor *compositor = output->compositor;
2380
2381 if (output->repaint_needed &&
2382 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2383 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2384 weston_output_repaint(output) == 0)
2385 return 0;
2386
2387 weston_output_schedule_repaint_reset(output);
2388
2389 return 0;
2390}
2391
Kristian Høgsbergef044142011-06-21 15:02:12 -04002392WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002393weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002394 const struct timespec *stamp,
2395 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002396{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002397 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002398 int32_t refresh_nsec;
2399 struct timespec now;
2400 struct timespec gone;
2401 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002402
Pekka Paalanenb5026542014-11-12 15:09:24 +02002403 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2404 TLP_VBLANK(stamp), TLP_END);
2405
Pekka Paalanen0513a952014-05-21 16:17:27 +03002406 refresh_nsec = 1000000000000LL / output->current_mode->refresh;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002407 weston_presentation_feedback_present_list(&output->feedback_list,
2408 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002409 output->msc,
2410 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002411
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002412 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002413
Pekka Paalanen0513a952014-05-21 16:17:27 +03002414 weston_compositor_read_presentation_clock(compositor, &now);
2415 timespec_sub(&gone, &now, stamp);
2416 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2417 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002418
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002419 if (msec < -1000 || msec > 1000) {
2420 static bool warned;
2421
2422 if (!warned)
2423 weston_log("Warning: computed repaint delay is "
2424 "insane: %d msec\n", msec);
2425 warned = true;
2426
2427 msec = 0;
2428 }
2429
2430 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002431 output_repaint_timer_handler(output);
2432 else
2433 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002434}
2435
2436static void
2437idle_repaint(void *data)
2438{
2439 struct weston_output *output = data;
2440
Jonas Ådahle5a12252013-04-05 23:07:11 +02002441 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002442}
2443
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002444WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002445weston_layer_entry_insert(struct weston_layer_entry *list,
2446 struct weston_layer_entry *entry)
2447{
2448 wl_list_insert(&list->link, &entry->link);
2449 entry->layer = list->layer;
2450}
2451
2452WL_EXPORT void
2453weston_layer_entry_remove(struct weston_layer_entry *entry)
2454{
2455 wl_list_remove(&entry->link);
2456 wl_list_init(&entry->link);
2457 entry->layer = NULL;
2458}
2459
2460WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002461weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2462{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002463 wl_list_init(&layer->view_list.link);
2464 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002465 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002466 if (below != NULL)
2467 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002468}
2469
2470WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002471weston_layer_set_mask(struct weston_layer *layer,
2472 int x, int y, int width, int height)
2473{
2474 struct weston_view *view;
2475
2476 layer->mask.x1 = x;
2477 layer->mask.x2 = x + width;
2478 layer->mask.y1 = y;
2479 layer->mask.y2 = y + height;
2480
2481 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2482 weston_view_geometry_dirty(view);
2483 }
2484}
2485
2486WL_EXPORT void
2487weston_layer_set_mask_infinite(struct weston_layer *layer)
2488{
2489 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2490 UINT32_MAX, UINT32_MAX);
2491}
2492
2493WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002494weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002495{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002496 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002497 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002498
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002499 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2500 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002501 return;
2502
Pekka Paalanenb5026542014-11-12 15:09:24 +02002503 if (!output->repaint_needed)
2504 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2505
Kristian Høgsbergef044142011-06-21 15:02:12 -04002506 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002507 output->repaint_needed = 1;
2508 if (output->repaint_scheduled)
2509 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002510
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002511 wl_event_loop_add_idle(loop, idle_repaint, output);
2512 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002513 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2514
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002515
2516 if (compositor->input_loop_source) {
2517 wl_event_source_remove(compositor->input_loop_source);
2518 compositor->input_loop_source = NULL;
2519 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002520}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002521
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002522WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002523weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2524{
2525 struct weston_output *output;
2526
2527 wl_list_for_each(output, &compositor->output_list, link)
2528 weston_output_schedule_repaint(output);
2529}
2530
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002531static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002532surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002533{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002534 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002535}
2536
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002537static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002538surface_attach(struct wl_client *client,
2539 struct wl_resource *resource,
2540 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2541{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002542 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002543 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002544
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002545 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002546 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002547 if (buffer == NULL) {
2548 wl_client_post_no_memory(client);
2549 return;
2550 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002551 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002552
Pekka Paalanende685b82012-12-04 15:58:12 +02002553 /* Attach, attach, without commit in between does not send
2554 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002555 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002556
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002557 surface->pending.sx = sx;
2558 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002559 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002560}
2561
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002562static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002563surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002564 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002565 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002566{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002567 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002568
Pekka Paalanen8e159182012-10-10 12:49:25 +03002569 pixman_region32_union_rect(&surface->pending.damage,
2570 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002571 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002572}
2573
Kristian Høgsberg33418202011-08-16 23:01:28 -04002574static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002575destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002576{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002577 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002578
2579 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002580 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002581}
2582
2583static void
2584surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002585 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002586{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002587 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002588 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002589
2590 cb = malloc(sizeof *cb);
2591 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002592 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002593 return;
2594 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002595
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002596 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2597 callback);
2598 if (cb->resource == NULL) {
2599 free(cb);
2600 wl_resource_post_no_memory(resource);
2601 return;
2602 }
2603
Jason Ekstranda85118c2013-06-27 20:17:02 -05002604 wl_resource_set_implementation(cb->resource, NULL, cb,
2605 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002606
Pekka Paalanenbc106382012-10-10 12:49:31 +03002607 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002608}
2609
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002610static void
2611surface_set_opaque_region(struct wl_client *client,
2612 struct wl_resource *resource,
2613 struct wl_resource *region_resource)
2614{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002615 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002616 struct weston_region *region;
2617
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002618 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002619 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002620 pixman_region32_copy(&surface->pending.opaque,
2621 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002622 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002623 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002624 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002625}
2626
2627static void
2628surface_set_input_region(struct wl_client *client,
2629 struct wl_resource *resource,
2630 struct wl_resource *region_resource)
2631{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002632 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002633 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002634
2635 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002636 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002637 pixman_region32_copy(&surface->pending.input,
2638 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002639 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002640 pixman_region32_fini(&surface->pending.input);
2641 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002642 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002643}
2644
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002645static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002646weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002647{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002648 struct weston_subsurface *sub;
2649
2650 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2651 parent_link_pending) {
2652 wl_list_remove(&sub->parent_link);
2653 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2654 }
2655}
2656
2657static void
Jason Ekstrand1e059042014-10-16 10:55:19 -05002658weston_surface_build_buffer_matrix(struct weston_surface *surface,
2659 struct weston_matrix *matrix)
2660{
2661 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2662 double src_width, src_height, dest_width, dest_height;
2663
2664 weston_matrix_init(matrix);
2665
2666 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2667 src_width = surface->width_from_buffer;
2668 src_height = surface->height_from_buffer;
2669 } else {
2670 src_width = wl_fixed_to_double(vp->buffer.src_width);
2671 src_height = wl_fixed_to_double(vp->buffer.src_height);
2672 }
2673
2674 if (vp->surface.width == -1) {
2675 dest_width = src_width;
2676 dest_height = src_height;
2677 } else {
2678 dest_width = vp->surface.width;
2679 dest_height = vp->surface.height;
2680 }
2681
2682 if (src_width != dest_width || src_height != dest_height)
2683 weston_matrix_scale(matrix,
2684 src_width / dest_width,
2685 src_height / dest_height, 1);
2686
2687 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2688 weston_matrix_translate(matrix,
2689 wl_fixed_to_double(vp->buffer.src_x),
2690 wl_fixed_to_double(vp->buffer.src_y),
2691 0);
2692
2693 switch (vp->buffer.transform) {
2694 case WL_OUTPUT_TRANSFORM_FLIPPED:
2695 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2696 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2697 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2698 weston_matrix_scale(matrix, -1, 1, 1);
2699 weston_matrix_translate(matrix,
2700 surface->width_from_buffer, 0, 0);
2701 break;
2702 }
2703
2704 switch (vp->buffer.transform) {
2705 default:
2706 case WL_OUTPUT_TRANSFORM_NORMAL:
2707 case WL_OUTPUT_TRANSFORM_FLIPPED:
2708 break;
2709 case WL_OUTPUT_TRANSFORM_90:
2710 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2711 weston_matrix_rotate_xy(matrix, 0, 1);
2712 weston_matrix_translate(matrix,
2713 surface->height_from_buffer, 0, 0);
2714 break;
2715 case WL_OUTPUT_TRANSFORM_180:
2716 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2717 weston_matrix_rotate_xy(matrix, -1, 0);
2718 weston_matrix_translate(matrix,
2719 surface->width_from_buffer,
2720 surface->height_from_buffer, 0);
2721 break;
2722 case WL_OUTPUT_TRANSFORM_270:
2723 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2724 weston_matrix_rotate_xy(matrix, 0, -1);
2725 weston_matrix_translate(matrix,
2726 0, surface->width_from_buffer, 0);
2727 break;
2728 }
2729
2730 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2731}
2732
2733static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002734weston_surface_commit_state(struct weston_surface *surface,
2735 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002736{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002737 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002738 pixman_region32_t opaque;
2739
Alexander Larsson4ea95522013-05-22 14:41:37 +02002740 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002741 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002742 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002743 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002744
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002745 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002746 if (state->newly_attached)
2747 weston_surface_attach(surface, state->buffer);
2748 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002749
Jason Ekstrand1e059042014-10-16 10:55:19 -05002750 weston_surface_build_buffer_matrix(surface,
2751 &surface->surface_to_buffer_matrix);
2752 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2753 &surface->surface_to_buffer_matrix);
2754
Jason Ekstrand7b982072014-05-20 14:33:03 -05002755 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002756 weston_surface_update_size(surface);
2757 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002758 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002759 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002760
Jason Ekstrand7b982072014-05-20 14:33:03 -05002761 state->sx = 0;
2762 state->sy = 0;
2763 state->newly_attached = 0;
2764 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002765
2766 /* wl_surface.damage */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002767 if (weston_timeline_enabled_ &&
2768 pixman_region32_not_empty(&state->damage))
2769 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002770 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002771 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002772 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002773 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002774 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002775
2776 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002777 pixman_region32_init(&opaque);
2778 pixman_region32_intersect_rect(&opaque, &state->opaque,
2779 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002780
2781 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2782 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002783 wl_list_for_each(view, &surface->views, surface_link)
2784 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002785 }
2786
2787 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002788
2789 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002790 pixman_region32_intersect_rect(&surface->input, &state->input,
2791 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002792
Pekka Paalanenbc106382012-10-10 12:49:31 +03002793 /* wl_surface.frame */
2794 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002795 &state->frame_callback_list);
2796 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002797
2798 /* XXX:
2799 * What should happen with a feedback request, if there
2800 * is no wl_buffer attached for this commit?
2801 */
2802
2803 /* presentation.feedback */
2804 wl_list_insert_list(&surface->feedback_list,
2805 &state->feedback_list);
2806 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002807}
2808
2809static void
2810weston_surface_commit(struct weston_surface *surface)
2811{
2812 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002813
Pekka Paalanene67858b2013-04-25 13:57:42 +03002814 weston_surface_commit_subsurface_order(surface);
2815
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002816 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002817}
2818
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002819static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002820weston_subsurface_commit(struct weston_subsurface *sub);
2821
2822static void
2823weston_subsurface_parent_commit(struct weston_subsurface *sub,
2824 int parent_is_synchronized);
2825
2826static void
2827surface_commit(struct wl_client *client, struct wl_resource *resource)
2828{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002829 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002830 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2831
2832 if (sub) {
2833 weston_subsurface_commit(sub);
2834 return;
2835 }
2836
2837 weston_surface_commit(surface);
2838
2839 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2840 if (sub->surface != surface)
2841 weston_subsurface_parent_commit(sub, 0);
2842 }
2843}
2844
2845static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002846surface_set_buffer_transform(struct wl_client *client,
2847 struct wl_resource *resource, int transform)
2848{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002849 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002850
Jonny Lamba55f1392014-05-30 12:07:15 +02002851 /* if wl_output.transform grows more members this will need to be updated. */
2852 if (transform < 0 ||
2853 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2854 wl_resource_post_error(resource,
2855 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2856 "buffer transform must be a valid transform "
2857 "('%d' specified)", transform);
2858 return;
2859 }
2860
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002861 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002862 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002863}
2864
Alexander Larsson4ea95522013-05-22 14:41:37 +02002865static void
2866surface_set_buffer_scale(struct wl_client *client,
2867 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002868 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002869{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002870 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002871
Jonny Lamba55f1392014-05-30 12:07:15 +02002872 if (scale < 1) {
2873 wl_resource_post_error(resource,
2874 WL_SURFACE_ERROR_INVALID_SCALE,
2875 "buffer scale must be at least one "
2876 "('%d' specified)", scale);
2877 return;
2878 }
2879
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002880 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002881 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002882}
2883
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002884static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002885 surface_destroy,
2886 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002887 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002888 surface_frame,
2889 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002890 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002891 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002892 surface_set_buffer_transform,
2893 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002894};
2895
2896static void
2897compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002898 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002899{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002900 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002901 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002902
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002903 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002904 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002905 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002906 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002907 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002908
Jason Ekstranda85118c2013-06-27 20:17:02 -05002909 surface->resource =
2910 wl_resource_create(client, &wl_surface_interface,
2911 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002912 if (surface->resource == NULL) {
2913 weston_surface_destroy(surface);
2914 wl_resource_post_no_memory(resource);
2915 return;
2916 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002917 wl_resource_set_implementation(surface->resource, &surface_interface,
2918 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002919
2920 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002921}
2922
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002923static void
2924destroy_region(struct wl_resource *resource)
2925{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002926 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002927
2928 pixman_region32_fini(&region->region);
2929 free(region);
2930}
2931
2932static void
2933region_destroy(struct wl_client *client, struct wl_resource *resource)
2934{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002935 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002936}
2937
2938static void
2939region_add(struct wl_client *client, struct wl_resource *resource,
2940 int32_t x, int32_t y, int32_t width, int32_t height)
2941{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002942 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002943
2944 pixman_region32_union_rect(&region->region, &region->region,
2945 x, y, width, height);
2946}
2947
2948static void
2949region_subtract(struct wl_client *client, struct wl_resource *resource,
2950 int32_t x, int32_t y, int32_t width, int32_t height)
2951{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002952 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002953 pixman_region32_t rect;
2954
2955 pixman_region32_init_rect(&rect, x, y, width, height);
2956 pixman_region32_subtract(&region->region, &region->region, &rect);
2957 pixman_region32_fini(&rect);
2958}
2959
2960static const struct wl_region_interface region_interface = {
2961 region_destroy,
2962 region_add,
2963 region_subtract
2964};
2965
2966static void
2967compositor_create_region(struct wl_client *client,
2968 struct wl_resource *resource, uint32_t id)
2969{
2970 struct weston_region *region;
2971
2972 region = malloc(sizeof *region);
2973 if (region == NULL) {
2974 wl_resource_post_no_memory(resource);
2975 return;
2976 }
2977
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002978 pixman_region32_init(&region->region);
2979
Jason Ekstranda85118c2013-06-27 20:17:02 -05002980 region->resource =
2981 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002982 if (region->resource == NULL) {
2983 free(region);
2984 wl_resource_post_no_memory(resource);
2985 return;
2986 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002987 wl_resource_set_implementation(region->resource, &region_interface,
2988 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002989}
2990
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002991static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002992 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002993 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002994};
2995
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002996static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002997weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2998{
2999 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003000
Jason Ekstrand7b982072014-05-20 14:33:03 -05003001 weston_surface_commit_state(surface, &sub->cached);
3002 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003003
3004 weston_surface_commit_subsurface_order(surface);
3005
3006 weston_surface_schedule_repaint(surface);
3007
Jason Ekstrand7b982072014-05-20 14:33:03 -05003008 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003009}
3010
3011static void
3012weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3013{
3014 struct weston_surface *surface = sub->surface;
3015
3016 /*
3017 * If this commit would cause the surface to move by the
3018 * attach(dx, dy) parameters, the old damage region must be
3019 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02003020 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003021 */
3022 pixman_region32_translate(&sub->cached.damage,
3023 -surface->pending.sx, -surface->pending.sy);
3024 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
3025 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07003026 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003027
3028 if (surface->pending.newly_attached) {
3029 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003030 weston_surface_state_set_buffer(&sub->cached,
3031 surface->pending.buffer);
3032 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003033 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003034 weston_presentation_feedback_discard_list(
3035 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003036 }
3037 sub->cached.sx += surface->pending.sx;
3038 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003039
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003040 sub->cached.buffer_viewport.changed |=
3041 surface->pending.buffer_viewport.changed;
3042 sub->cached.buffer_viewport.buffer =
3043 surface->pending.buffer_viewport.buffer;
3044 sub->cached.buffer_viewport.surface =
3045 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003046
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003047 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003048
3049 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3050
3051 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3052
3053 wl_list_insert_list(&sub->cached.frame_callback_list,
3054 &surface->pending.frame_callback_list);
3055 wl_list_init(&surface->pending.frame_callback_list);
3056
Pekka Paalanen133e4392014-09-23 22:08:46 -04003057 wl_list_insert_list(&sub->cached.feedback_list,
3058 &surface->pending.feedback_list);
3059 wl_list_init(&surface->pending.feedback_list);
3060
Jason Ekstrand7b982072014-05-20 14:33:03 -05003061 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003062}
3063
Derek Foreman280e7dd2014-10-03 13:13:42 -05003064static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003065weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3066{
3067 while (sub) {
3068 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003069 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003070
3071 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003072 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003073
3074 sub = weston_surface_to_subsurface(sub->parent);
3075 }
3076
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003077 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003078}
3079
3080static void
3081weston_subsurface_commit(struct weston_subsurface *sub)
3082{
3083 struct weston_surface *surface = sub->surface;
3084 struct weston_subsurface *tmp;
3085
3086 /* Recursive check for effectively synchronized. */
3087 if (weston_subsurface_is_synchronized(sub)) {
3088 weston_subsurface_commit_to_cache(sub);
3089 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003090 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003091 /* flush accumulated state from cache */
3092 weston_subsurface_commit_to_cache(sub);
3093 weston_subsurface_commit_from_cache(sub);
3094 } else {
3095 weston_surface_commit(surface);
3096 }
3097
3098 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3099 if (tmp->surface != surface)
3100 weston_subsurface_parent_commit(tmp, 0);
3101 }
3102 }
3103}
3104
3105static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003106weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003107{
3108 struct weston_surface *surface = sub->surface;
3109 struct weston_subsurface *tmp;
3110
Pekka Paalanene67858b2013-04-25 13:57:42 +03003111 /* From now on, commit_from_cache the whole sub-tree, regardless of
3112 * the synchronized mode of each child. This sub-surface or some
3113 * of its ancestors were synchronized, so we are synchronized
3114 * all the way down.
3115 */
3116
Jason Ekstrand7b982072014-05-20 14:33:03 -05003117 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003118 weston_subsurface_commit_from_cache(sub);
3119
3120 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3121 if (tmp->surface != surface)
3122 weston_subsurface_parent_commit(tmp, 1);
3123 }
3124}
3125
3126static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003127weston_subsurface_parent_commit(struct weston_subsurface *sub,
3128 int parent_is_synchronized)
3129{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003130 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003131 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003132 wl_list_for_each(view, &sub->surface->views, surface_link)
3133 weston_view_set_position(view,
3134 sub->position.x,
3135 sub->position.y);
3136
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003137 sub->position.set = 0;
3138 }
3139
3140 if (parent_is_synchronized || sub->synchronized)
3141 weston_subsurface_synchronized_commit(sub);
3142}
3143
Pekka Paalanen8274d902014-08-06 19:36:51 +03003144static int
3145subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3146{
3147 return snprintf(buf, len, "sub-surface");
3148}
3149
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003150static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003151subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003152{
3153 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003154 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003155
Jason Ekstranda7af7042013-10-12 22:38:11 -05003156 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003157 weston_view_set_position(view,
3158 view->geometry.x + dx,
3159 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003160
3161 /* No need to check parent mappedness, because if parent is not
3162 * mapped, parent is not in a visible layer, so this sub-surface
3163 * will not be drawn either.
3164 */
3165 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003166 struct weston_output *output;
3167
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003168 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003169 * because that would call it also for the parent surface,
3170 * which might not be mapped yet. That would lead to
3171 * inconsistent state, where the window could never be
3172 * mapped.
3173 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003174 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003175 * weston_surface_is_mapped() return true, so that when the
3176 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003177 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003178 */
3179 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003180 output = container_of(compositor->output_list.next,
3181 struct weston_output, link);
3182
3183 surface->output = output;
3184 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003185 }
3186}
3187
3188static struct weston_subsurface *
3189weston_surface_to_subsurface(struct weston_surface *surface)
3190{
3191 if (surface->configure == subsurface_configure)
3192 return surface->configure_private;
3193
3194 return NULL;
3195}
3196
Pekka Paalanen01388e22013-04-25 13:57:44 +03003197WL_EXPORT struct weston_surface *
3198weston_surface_get_main_surface(struct weston_surface *surface)
3199{
3200 struct weston_subsurface *sub;
3201
3202 while (surface && (sub = weston_surface_to_subsurface(surface)))
3203 surface = sub->parent;
3204
3205 return surface;
3206}
3207
Pekka Paalanen50b67472014-10-01 15:02:41 +03003208WL_EXPORT int
3209weston_surface_set_role(struct weston_surface *surface,
3210 const char *role_name,
3211 struct wl_resource *error_resource,
3212 uint32_t error_code)
3213{
3214 assert(role_name);
3215
3216 if (surface->role_name == NULL ||
3217 surface->role_name == role_name ||
3218 strcmp(surface->role_name, role_name) == 0) {
3219 surface->role_name = role_name;
3220
3221 return 0;
3222 }
3223
3224 wl_resource_post_error(error_resource, error_code,
3225 "Cannot assign role %s to wl_surface@%d,"
3226 " already has role %s\n",
3227 role_name,
3228 wl_resource_get_id(surface->resource),
3229 surface->role_name);
3230 return -1;
3231}
3232
Pekka Paalanen8274d902014-08-06 19:36:51 +03003233WL_EXPORT void
3234weston_surface_set_label_func(struct weston_surface *surface,
3235 int (*desc)(struct weston_surface *,
3236 char *, size_t))
3237{
3238 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003239 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003240}
3241
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003242/** Get the size of surface contents
3243 *
3244 * \param surface The surface to query.
3245 * \param width Returns the width of raw contents.
3246 * \param height Returns the height of raw contents.
3247 *
3248 * Retrieves the raw surface content size in pixels for the given surface.
3249 * This is the whole content size in buffer pixels. If the surface
3250 * has no content or the renderer does not implement this feature,
3251 * zeroes are returned.
3252 *
3253 * This function is used to determine the buffer size needed for
3254 * a weston_surface_copy_content() call.
3255 */
3256WL_EXPORT void
3257weston_surface_get_content_size(struct weston_surface *surface,
3258 int *width, int *height)
3259{
3260 struct weston_renderer *rer = surface->compositor->renderer;
3261
3262 if (!rer->surface_get_content_size) {
3263 *width = 0;
3264 *height = 0;
3265 return;
3266 }
3267
3268 rer->surface_get_content_size(surface, width, height);
3269}
3270
3271/** Copy surface contents to system memory.
3272 *
3273 * \param surface The surface to copy from.
3274 * \param target Pointer to the target memory buffer.
3275 * \param size Size of the target buffer in bytes.
3276 * \param src_x X location on contents to copy from.
3277 * \param src_y Y location on contents to copy from.
3278 * \param width Width in pixels of the area to copy.
3279 * \param height Height in pixels of the area to copy.
3280 * \return 0 for success, -1 for failure.
3281 *
3282 * Surface contents are maintained by the renderer. They can be in a
3283 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3284 * else.
3285 *
3286 * Surface contents are copied into memory pointed to by target,
3287 * which has size bytes of space available. The target memory
3288 * may be larger than needed, but being smaller returns an error.
3289 * The extra bytes in target may or may not be written; their content is
3290 * unspecified. Size must be large enough to hold the image.
3291 *
3292 * The image in the target memory will be arranged in rows from
3293 * top to bottom, and pixels on a row from left to right. The pixel
3294 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3295 * width * 4.
3296 *
3297 * Parameters src_x and src_y define the upper-left corner in buffer
3298 * coordinates (pixels) to copy from. Parameters width and height
3299 * define the size of the area to copy in pixels.
3300 *
3301 * The rectangle defined by src_x, src_y, width, height must fit in
3302 * the surface contents. Otherwise an error is returned.
3303 *
3304 * Use surface_get_data_size to determine the content size; the
3305 * needed target buffer size and rectangle limits.
3306 *
3307 * CURRENT IMPLEMENTATION RESTRICTIONS:
3308 * - the machine must be little-endian due to Pixman formats.
3309 *
3310 * NOTE: Pixman formats are premultiplied.
3311 */
3312WL_EXPORT int
3313weston_surface_copy_content(struct weston_surface *surface,
3314 void *target, size_t size,
3315 int src_x, int src_y,
3316 int width, int height)
3317{
3318 struct weston_renderer *rer = surface->compositor->renderer;
3319 int cw, ch;
3320 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3321
3322 if (!rer->surface_copy_content)
3323 return -1;
3324
3325 weston_surface_get_content_size(surface, &cw, &ch);
3326
3327 if (src_x < 0 || src_y < 0)
3328 return -1;
3329
3330 if (width <= 0 || height <= 0)
3331 return -1;
3332
3333 if (src_x + width > cw || src_y + height > ch)
3334 return -1;
3335
3336 if (width * bytespp * height > size)
3337 return -1;
3338
3339 return rer->surface_copy_content(surface, target, size,
3340 src_x, src_y, width, height);
3341}
3342
Pekka Paalanene67858b2013-04-25 13:57:42 +03003343static void
3344subsurface_set_position(struct wl_client *client,
3345 struct wl_resource *resource, int32_t x, int32_t y)
3346{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003347 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003348
3349 if (!sub)
3350 return;
3351
3352 sub->position.x = x;
3353 sub->position.y = y;
3354 sub->position.set = 1;
3355}
3356
3357static struct weston_subsurface *
3358subsurface_from_surface(struct weston_surface *surface)
3359{
3360 struct weston_subsurface *sub;
3361
3362 sub = weston_surface_to_subsurface(surface);
3363 if (sub)
3364 return sub;
3365
3366 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3367 if (sub->surface == surface)
3368 return sub;
3369
3370 return NULL;
3371}
3372
3373static struct weston_subsurface *
3374subsurface_sibling_check(struct weston_subsurface *sub,
3375 struct weston_surface *surface,
3376 const char *request)
3377{
3378 struct weston_subsurface *sibling;
3379
3380 sibling = subsurface_from_surface(surface);
3381
3382 if (!sibling) {
3383 wl_resource_post_error(sub->resource,
3384 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3385 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003386 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003387 return NULL;
3388 }
3389
3390 if (sibling->parent != sub->parent) {
3391 wl_resource_post_error(sub->resource,
3392 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3393 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003394 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003395 return NULL;
3396 }
3397
3398 return sibling;
3399}
3400
3401static void
3402subsurface_place_above(struct wl_client *client,
3403 struct wl_resource *resource,
3404 struct wl_resource *sibling_resource)
3405{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003406 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003407 struct weston_surface *surface =
3408 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003409 struct weston_subsurface *sibling;
3410
3411 if (!sub)
3412 return;
3413
3414 sibling = subsurface_sibling_check(sub, surface, "place_above");
3415 if (!sibling)
3416 return;
3417
3418 wl_list_remove(&sub->parent_link_pending);
3419 wl_list_insert(sibling->parent_link_pending.prev,
3420 &sub->parent_link_pending);
3421}
3422
3423static void
3424subsurface_place_below(struct wl_client *client,
3425 struct wl_resource *resource,
3426 struct wl_resource *sibling_resource)
3427{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003428 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003429 struct weston_surface *surface =
3430 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003431 struct weston_subsurface *sibling;
3432
3433 if (!sub)
3434 return;
3435
3436 sibling = subsurface_sibling_check(sub, surface, "place_below");
3437 if (!sibling)
3438 return;
3439
3440 wl_list_remove(&sub->parent_link_pending);
3441 wl_list_insert(&sibling->parent_link_pending,
3442 &sub->parent_link_pending);
3443}
3444
3445static void
3446subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3447{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003448 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003449
3450 if (sub)
3451 sub->synchronized = 1;
3452}
3453
3454static void
3455subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3456{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003457 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003458
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003459 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003460 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003461
3462 /* If sub became effectively desynchronized, flush. */
3463 if (!weston_subsurface_is_synchronized(sub))
3464 weston_subsurface_synchronized_commit(sub);
3465 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003466}
3467
3468static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003469weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3470{
3471 wl_list_remove(&sub->parent_link);
3472 wl_list_remove(&sub->parent_link_pending);
3473 wl_list_remove(&sub->parent_destroy_listener.link);
3474 sub->parent = NULL;
3475}
3476
3477static void
3478weston_subsurface_destroy(struct weston_subsurface *sub);
3479
3480static void
3481subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3482{
3483 struct weston_subsurface *sub =
3484 container_of(listener, struct weston_subsurface,
3485 surface_destroy_listener);
3486 assert(data == &sub->surface->resource);
3487
3488 /* The protocol object (wl_resource) is left inert. */
3489 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003490 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003491
3492 weston_subsurface_destroy(sub);
3493}
3494
3495static void
3496subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3497{
3498 struct weston_subsurface *sub =
3499 container_of(listener, struct weston_subsurface,
3500 parent_destroy_listener);
3501 assert(data == &sub->parent->resource);
3502 assert(sub->surface != sub->parent);
3503
3504 if (weston_surface_is_mapped(sub->surface))
3505 weston_surface_unmap(sub->surface);
3506
3507 weston_subsurface_unlink_parent(sub);
3508}
3509
3510static void
3511subsurface_resource_destroy(struct wl_resource *resource)
3512{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003513 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003514
3515 if (sub)
3516 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003517}
3518
3519static void
3520subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3521{
3522 wl_resource_destroy(resource);
3523}
3524
3525static void
3526weston_subsurface_link_parent(struct weston_subsurface *sub,
3527 struct weston_surface *parent)
3528{
3529 sub->parent = parent;
3530 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003531 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003532 &sub->parent_destroy_listener);
3533
3534 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3535 wl_list_insert(&parent->subsurface_list_pending,
3536 &sub->parent_link_pending);
3537}
3538
3539static void
3540weston_subsurface_link_surface(struct weston_subsurface *sub,
3541 struct weston_surface *surface)
3542{
3543 sub->surface = surface;
3544 sub->surface_destroy_listener.notify =
3545 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003546 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003547 &sub->surface_destroy_listener);
3548}
3549
3550static void
3551weston_subsurface_destroy(struct weston_subsurface *sub)
3552{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003553 struct weston_view *view, *next;
3554
Pekka Paalanene67858b2013-04-25 13:57:42 +03003555 assert(sub->surface);
3556
3557 if (sub->resource) {
3558 assert(weston_surface_to_subsurface(sub->surface) == sub);
3559 assert(sub->parent_destroy_listener.notify ==
3560 subsurface_handle_parent_destroy);
3561
George Kiagiadakised04d382014-06-13 18:10:26 +02003562 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3563 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003564 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003565 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003566
Pekka Paalanene67858b2013-04-25 13:57:42 +03003567 if (sub->parent)
3568 weston_subsurface_unlink_parent(sub);
3569
Jason Ekstrand7b982072014-05-20 14:33:03 -05003570 weston_surface_state_fini(&sub->cached);
3571 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003572
3573 sub->surface->configure = NULL;
3574 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003575 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003576 } else {
3577 /* the dummy weston_subsurface for the parent itself */
3578 assert(sub->parent_destroy_listener.notify == NULL);
3579 wl_list_remove(&sub->parent_link);
3580 wl_list_remove(&sub->parent_link_pending);
3581 }
3582
3583 wl_list_remove(&sub->surface_destroy_listener.link);
3584 free(sub);
3585}
3586
3587static const struct wl_subsurface_interface subsurface_implementation = {
3588 subsurface_destroy,
3589 subsurface_set_position,
3590 subsurface_place_above,
3591 subsurface_place_below,
3592 subsurface_set_sync,
3593 subsurface_set_desync
3594};
3595
3596static struct weston_subsurface *
3597weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3598 struct weston_surface *parent)
3599{
3600 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003601 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003602
Bryce Harringtonde16d892014-11-20 22:21:57 -08003603 sub = zalloc(sizeof *sub);
3604 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003605 return NULL;
3606
Jason Ekstranda7af7042013-10-12 22:38:11 -05003607 wl_list_init(&sub->unused_views);
3608
Jason Ekstranda85118c2013-06-27 20:17:02 -05003609 sub->resource =
3610 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003611 if (!sub->resource) {
3612 free(sub);
3613 return NULL;
3614 }
3615
Jason Ekstranda85118c2013-06-27 20:17:02 -05003616 wl_resource_set_implementation(sub->resource,
3617 &subsurface_implementation,
3618 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003619 weston_subsurface_link_surface(sub, surface);
3620 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003621 weston_surface_state_init(&sub->cached);
3622 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003623 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003624
3625 return sub;
3626}
3627
3628/* Create a dummy subsurface for having the parent itself in its
3629 * sub-surface lists. Makes stacking order manipulation easy.
3630 */
3631static struct weston_subsurface *
3632weston_subsurface_create_for_parent(struct weston_surface *parent)
3633{
3634 struct weston_subsurface *sub;
3635
Bryce Harringtonde16d892014-11-20 22:21:57 -08003636 sub = zalloc(sizeof *sub);
3637 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003638 return NULL;
3639
3640 weston_subsurface_link_surface(sub, parent);
3641 sub->parent = parent;
3642 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3643 wl_list_insert(&parent->subsurface_list_pending,
3644 &sub->parent_link_pending);
3645
3646 return sub;
3647}
3648
3649static void
3650subcompositor_get_subsurface(struct wl_client *client,
3651 struct wl_resource *resource,
3652 uint32_t id,
3653 struct wl_resource *surface_resource,
3654 struct wl_resource *parent_resource)
3655{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003656 struct weston_surface *surface =
3657 wl_resource_get_user_data(surface_resource);
3658 struct weston_surface *parent =
3659 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003660 struct weston_subsurface *sub;
3661 static const char where[] = "get_subsurface: wl_subsurface@";
3662
3663 if (surface == parent) {
3664 wl_resource_post_error(resource,
3665 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3666 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003667 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003668 return;
3669 }
3670
3671 if (weston_surface_to_subsurface(surface)) {
3672 wl_resource_post_error(resource,
3673 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3674 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003675 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003676 return;
3677 }
3678
Pekka Paalanen50b67472014-10-01 15:02:41 +03003679 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3680 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003681 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003682
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003683 if (weston_surface_get_main_surface(parent) == surface) {
3684 wl_resource_post_error(resource,
3685 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3686 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003687 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003688 return;
3689 }
3690
Pekka Paalanene67858b2013-04-25 13:57:42 +03003691 /* make sure the parent is in its own list */
3692 if (wl_list_empty(&parent->subsurface_list)) {
3693 if (!weston_subsurface_create_for_parent(parent)) {
3694 wl_resource_post_no_memory(resource);
3695 return;
3696 }
3697 }
3698
3699 sub = weston_subsurface_create(id, surface, parent);
3700 if (!sub) {
3701 wl_resource_post_no_memory(resource);
3702 return;
3703 }
3704
3705 surface->configure = subsurface_configure;
3706 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003707 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003708}
3709
3710static void
3711subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3712{
3713 wl_resource_destroy(resource);
3714}
3715
3716static const struct wl_subcompositor_interface subcompositor_interface = {
3717 subcompositor_destroy,
3718 subcompositor_get_subsurface
3719};
3720
3721static void
3722bind_subcompositor(struct wl_client *client,
3723 void *data, uint32_t version, uint32_t id)
3724{
3725 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003726 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003727
Jason Ekstranda85118c2013-06-27 20:17:02 -05003728 resource =
3729 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003730 if (resource == NULL) {
3731 wl_client_post_no_memory(client);
3732 return;
3733 }
3734 wl_resource_set_implementation(resource, &subcompositor_interface,
3735 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003736}
3737
3738static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003739weston_compositor_dpms(struct weston_compositor *compositor,
3740 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003741{
3742 struct weston_output *output;
3743
3744 wl_list_for_each(output, &compositor->output_list, link)
3745 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003746 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003747}
3748
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003749WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003750weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003751{
Neil Roberts8b62e202013-09-30 13:14:47 +01003752 uint32_t old_state = compositor->state;
3753
3754 /* The state needs to be changed before emitting the wake
3755 * signal because that may try to schedule a repaint which
3756 * will not work if the compositor is still sleeping */
3757 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3758
3759 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003760 case WESTON_COMPOSITOR_SLEEPING:
3761 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3762 /* fall through */
3763 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003764 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003765 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003766 /* fall through */
3767 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003768 wl_event_source_timer_update(compositor->idle_source,
3769 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003770 }
3771}
3772
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003773WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003774weston_compositor_offscreen(struct weston_compositor *compositor)
3775{
3776 switch (compositor->state) {
3777 case WESTON_COMPOSITOR_OFFSCREEN:
3778 return;
3779 case WESTON_COMPOSITOR_SLEEPING:
3780 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3781 /* fall through */
3782 default:
3783 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3784 wl_event_source_timer_update(compositor->idle_source, 0);
3785 }
3786}
3787
3788WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003789weston_compositor_sleep(struct weston_compositor *compositor)
3790{
3791 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3792 return;
3793
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003794 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003795 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3796 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3797}
3798
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003799static int
3800idle_handler(void *data)
3801{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003802 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003803
3804 if (compositor->idle_inhibit)
3805 return 1;
3806
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003807 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003808 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003809
3810 return 1;
3811}
3812
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003813WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003814weston_plane_init(struct weston_plane *plane,
3815 struct weston_compositor *ec,
3816 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003817{
3818 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003819 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003820 plane->x = x;
3821 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003822 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003823
3824 /* Init the link so that the call to wl_list_remove() when releasing
3825 * the plane without ever stacking doesn't lead to a crash */
3826 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003827}
3828
3829WL_EXPORT void
3830weston_plane_release(struct weston_plane *plane)
3831{
Xiong Zhang97116532013-10-23 13:58:31 +08003832 struct weston_view *view;
3833
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003834 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003835 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003836
Xiong Zhang97116532013-10-23 13:58:31 +08003837 wl_list_for_each(view, &plane->compositor->view_list, link) {
3838 if (view->plane == plane)
3839 view->plane = NULL;
3840 }
3841
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003842 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003843}
3844
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003845WL_EXPORT void
3846weston_compositor_stack_plane(struct weston_compositor *ec,
3847 struct weston_plane *plane,
3848 struct weston_plane *above)
3849{
3850 if (above)
3851 wl_list_insert(above->link.prev, &plane->link);
3852 else
3853 wl_list_insert(&ec->plane_list, &plane->link);
3854}
3855
Casey Dahlin9074db52012-04-19 22:50:09 -04003856static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003857{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003858 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003859}
3860
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003861static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003862bind_output(struct wl_client *client,
3863 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003864{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003865 struct weston_output *output = data;
3866 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003867 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003868
Jason Ekstranda85118c2013-06-27 20:17:02 -05003869 resource = wl_resource_create(client, &wl_output_interface,
3870 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003871 if (resource == NULL) {
3872 wl_client_post_no_memory(client);
3873 return;
3874 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003875
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003876 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003877 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003878
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003879 wl_output_send_geometry(resource,
3880 output->x,
3881 output->y,
3882 output->mm_width,
3883 output->mm_height,
3884 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003885 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003886 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003887 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003888 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003889 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003890
3891 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003892 wl_output_send_mode(resource,
3893 mode->flags,
3894 mode->width,
3895 mode->height,
3896 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003897 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003898
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003899 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003900 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003901}
3902
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003903/* Move other outputs when one is removed so the space remains contiguos. */
3904static void
3905weston_compositor_remove_output(struct weston_compositor *compositor,
3906 struct weston_output *remove_output)
3907{
3908 struct weston_output *output;
3909 int offset = 0;
3910
3911 wl_list_for_each(output, &compositor->output_list, link) {
3912 if (output == remove_output) {
3913 offset = output->width;
3914 continue;
3915 }
3916
3917 if (offset > 0) {
3918 weston_output_move(output,
3919 output->x - offset, output->y);
3920 output->dirty = 1;
3921 }
3922 }
3923}
3924
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003925WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003926weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003927{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003928 struct wl_resource *resource;
3929
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003930 output->destroying = 1;
3931
Pekka Paalanen0513a952014-05-21 16:17:27 +03003932 wl_event_source_remove(output->repaint_timer);
3933
Pekka Paalanen133e4392014-09-23 22:08:46 -04003934 weston_presentation_feedback_discard_list(&output->feedback_list);
3935
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003936 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003937 wl_list_remove(&output->link);
3938
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003939 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003940 wl_signal_emit(&output->destroy_signal, output);
3941
Richard Hughesafe690c2013-05-02 10:10:04 +01003942 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003943 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003944 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003945 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003946
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003947 wl_resource_for_each(resource, &output->resource_list) {
3948 wl_resource_set_destructor(resource, NULL);
3949 }
3950
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003951 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003952}
3953
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003954WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003955weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003956{
Scott Moreau850ca422012-05-21 15:21:25 -06003957 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003958
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003959 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003960 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003961
Scott Moreauccbf29d2012-02-22 14:21:41 -07003962 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003963 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003964 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003965 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003966 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003967 weston_matrix_scale(&output->matrix, magnification,
3968 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003969 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003970
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003971 switch (output->transform) {
3972 case WL_OUTPUT_TRANSFORM_FLIPPED:
3973 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3974 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3975 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3976 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3977 weston_matrix_scale(&output->matrix, -1, 1, 1);
3978 break;
3979 }
3980
3981 switch (output->transform) {
3982 default:
3983 case WL_OUTPUT_TRANSFORM_NORMAL:
3984 case WL_OUTPUT_TRANSFORM_FLIPPED:
3985 break;
3986 case WL_OUTPUT_TRANSFORM_90:
3987 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3988 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
3989 weston_matrix_rotate_xy(&output->matrix, 0, 1);
3990 break;
3991 case WL_OUTPUT_TRANSFORM_180:
3992 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3993 weston_matrix_translate(&output->matrix,
3994 -output->width, -output->height, 0);
3995 weston_matrix_rotate_xy(&output->matrix, -1, 0);
3996 break;
3997 case WL_OUTPUT_TRANSFORM_270:
3998 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3999 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4000 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4001 break;
4002 }
4003
4004 if (output->current_scale != 1)
4005 weston_matrix_scale(&output->matrix,
4006 output->current_scale,
4007 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004008
Scott Moreauccbf29d2012-02-22 14:21:41 -07004009 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004010
4011 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004012}
4013
Scott Moreau1bad5db2012-08-18 01:04:05 -06004014static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004015weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004016{
4017 output->transform = transform;
4018
4019 switch (transform) {
4020 case WL_OUTPUT_TRANSFORM_90:
4021 case WL_OUTPUT_TRANSFORM_270:
4022 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4023 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4024 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02004025 output->width = output->current_mode->height;
4026 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004027 break;
4028 case WL_OUTPUT_TRANSFORM_NORMAL:
4029 case WL_OUTPUT_TRANSFORM_180:
4030 case WL_OUTPUT_TRANSFORM_FLIPPED:
4031 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02004032 output->width = output->current_mode->width;
4033 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004034 break;
4035 default:
4036 break;
4037 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06004038
Hardening57388e42013-09-18 23:56:36 +02004039 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02004040 output->width /= scale;
4041 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02004042}
4043
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004044static void
4045weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004046{
4047 output->x = x;
4048 output->y = y;
4049
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004050 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004051 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004052 output->width,
4053 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004054}
4055
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004056WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004057weston_output_move(struct weston_output *output, int x, int y)
4058{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004059 struct wl_resource *resource;
4060
4061 output->move_x = x - output->x;
4062 output->move_y = y - output->y;
4063
4064 if (output->move_x == 0 && output->move_y == 0)
4065 return;
4066
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004067 weston_output_init_geometry(output, x, y);
4068
4069 output->dirty = 1;
4070
4071 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004072 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004073
4074 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004075 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004076 wl_output_send_geometry(resource,
4077 output->x,
4078 output->y,
4079 output->mm_width,
4080 output->mm_height,
4081 output->subpixel,
4082 output->make,
4083 output->model,
4084 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004085
4086 if (wl_resource_get_version(resource) >= 2)
4087 wl_output_send_done(resource);
4088 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004089}
4090
4091WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004092weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004093 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004094 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004095{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004096 struct wl_event_loop *loop;
4097
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004098 output->compositor = c;
4099 output->x = x;
4100 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004101 output->mm_width = mm_width;
4102 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004103 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004104 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004105
Alexander Larsson0b135062013-05-28 16:23:36 +02004106 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004107 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004108
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004109 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004110 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004111
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004112 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004113 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004114 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004115 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004116 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02004117
Pekka Paalanen0513a952014-05-21 16:17:27 +03004118 loop = wl_display_get_event_loop(c->wl_display);
4119 output->repaint_timer = wl_event_loop_add_timer(loop,
4120 output_repaint_timer_handler, output);
4121
Casey Dahlin58ba1372012-04-19 22:50:08 -04004122 output->id = ffs(~output->compositor->output_id_pool) - 1;
4123 output->compositor->output_id_pool |= 1 << output->id;
4124
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004125 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004126 wl_global_create(c->wl_display, &wl_output_interface, 2,
4127 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01004128 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004129}
4130
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004131WL_EXPORT void
4132weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05004133 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004134 wl_fixed_t *x, wl_fixed_t *y)
4135{
Derek Foreman0f679412014-10-02 13:41:17 -05004136 struct weston_vector p = { {
4137 wl_fixed_to_double(device_x),
4138 wl_fixed_to_double(device_y),
4139 0.0,
4140 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004141
Derek Foreman67a18b92015-03-24 11:36:14 -05004142 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004143
Derek Foreman0f679412014-10-02 13:41:17 -05004144 *x = wl_fixed_from_double(p.f[0] / p.f[3]);
4145 *y = wl_fixed_from_double(p.f[1] / p.f[3]);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004146}
4147
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004148static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004149destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004150{
Jonny Lamb74130762013-11-26 18:19:46 +01004151 struct weston_surface *surface =
4152 wl_resource_get_user_data(resource);
4153
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004154 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004155 surface->pending.buffer_viewport.buffer.src_width =
4156 wl_fixed_from_int(-1);
4157 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004158 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004159}
4160
4161static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004162viewport_destroy(struct wl_client *client,
4163 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004164{
4165 wl_resource_destroy(resource);
4166}
4167
4168static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004169viewport_set(struct wl_client *client,
4170 struct wl_resource *resource,
4171 wl_fixed_t src_x,
4172 wl_fixed_t src_y,
4173 wl_fixed_t src_width,
4174 wl_fixed_t src_height,
4175 int32_t dst_width,
4176 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004177{
Jonny Lamb74130762013-11-26 18:19:46 +01004178 struct weston_surface *surface =
4179 wl_resource_get_user_data(resource);
4180
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004181 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01004182
Jonny Lamb8ae35902013-11-26 18:19:45 +01004183 if (wl_fixed_to_double(src_width) < 0 ||
4184 wl_fixed_to_double(src_height) < 0) {
4185 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004186 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004187 "source dimensions must be non-negative (%fx%f)",
4188 wl_fixed_to_double(src_width),
4189 wl_fixed_to_double(src_height));
4190 return;
4191 }
4192
4193 if (dst_width <= 0 || dst_height <= 0) {
4194 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004195 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004196 "destination dimensions must be positive (%dx%d)",
4197 dst_width, dst_height);
4198 return;
4199 }
4200
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004201 surface->pending.buffer_viewport.buffer.src_x = src_x;
4202 surface->pending.buffer_viewport.buffer.src_y = src_y;
4203 surface->pending.buffer_viewport.buffer.src_width = src_width;
4204 surface->pending.buffer_viewport.buffer.src_height = src_height;
4205 surface->pending.buffer_viewport.surface.width = dst_width;
4206 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004207 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004208}
4209
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004210static void
4211viewport_set_source(struct wl_client *client,
4212 struct wl_resource *resource,
4213 wl_fixed_t src_x,
4214 wl_fixed_t src_y,
4215 wl_fixed_t src_width,
4216 wl_fixed_t src_height)
4217{
4218 struct weston_surface *surface =
4219 wl_resource_get_user_data(resource);
4220
4221 assert(surface->viewport_resource != NULL);
4222
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004223 if (src_width == wl_fixed_from_int(-1) &&
4224 src_height == wl_fixed_from_int(-1)) {
4225 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004226 surface->pending.buffer_viewport.buffer.src_width =
4227 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004228 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004229 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004230 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004231
4232 if (src_width <= 0 || src_height <= 0) {
4233 wl_resource_post_error(resource,
4234 WL_VIEWPORT_ERROR_BAD_VALUE,
4235 "source size must be positive (%fx%f)",
4236 wl_fixed_to_double(src_width),
4237 wl_fixed_to_double(src_height));
4238 return;
4239 }
4240
4241 surface->pending.buffer_viewport.buffer.src_x = src_x;
4242 surface->pending.buffer_viewport.buffer.src_y = src_y;
4243 surface->pending.buffer_viewport.buffer.src_width = src_width;
4244 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004245 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004246}
4247
4248static void
4249viewport_set_destination(struct wl_client *client,
4250 struct wl_resource *resource,
4251 int32_t dst_width,
4252 int32_t dst_height)
4253{
4254 struct weston_surface *surface =
4255 wl_resource_get_user_data(resource);
4256
4257 assert(surface->viewport_resource != NULL);
4258
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004259 if (dst_width == -1 && dst_height == -1) {
4260 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004261 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004262 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004263 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004264 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004265
4266 if (dst_width <= 0 || dst_height <= 0) {
4267 wl_resource_post_error(resource,
4268 WL_VIEWPORT_ERROR_BAD_VALUE,
4269 "destination size must be positive (%dx%d)",
4270 dst_width, dst_height);
4271 return;
4272 }
4273
4274 surface->pending.buffer_viewport.surface.width = dst_width;
4275 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004276 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004277}
4278
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004279static const struct wl_viewport_interface viewport_interface = {
4280 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004281 viewport_set,
4282 viewport_set_source,
4283 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004284};
4285
4286static void
4287scaler_destroy(struct wl_client *client,
4288 struct wl_resource *resource)
4289{
4290 wl_resource_destroy(resource);
4291}
4292
4293static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004294scaler_get_viewport(struct wl_client *client,
4295 struct wl_resource *scaler,
4296 uint32_t id,
4297 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004298{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004299 int version = wl_resource_get_version(scaler);
4300 struct weston_surface *surface =
4301 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004302 struct wl_resource *resource;
4303
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004304 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01004305 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004306 WL_SCALER_ERROR_VIEWPORT_EXISTS,
4307 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004308 return;
4309 }
4310
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004311 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004312 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004313 if (resource == NULL) {
4314 wl_client_post_no_memory(client);
4315 return;
4316 }
4317
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004318 wl_resource_set_implementation(resource, &viewport_interface,
4319 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004320
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004321 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004322}
4323
4324static const struct wl_scaler_interface scaler_interface = {
4325 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004326 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004327};
4328
4329static void
4330bind_scaler(struct wl_client *client,
4331 void *data, uint32_t version, uint32_t id)
4332{
4333 struct wl_resource *resource;
4334
4335 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004336 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004337 if (resource == NULL) {
4338 wl_client_post_no_memory(client);
4339 return;
4340 }
4341
4342 wl_resource_set_implementation(resource, &scaler_interface,
4343 NULL, NULL);
4344}
4345
4346static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004347destroy_presentation_feedback(struct wl_resource *feedback_resource)
4348{
4349 struct weston_presentation_feedback *feedback;
4350
4351 feedback = wl_resource_get_user_data(feedback_resource);
4352
4353 wl_list_remove(&feedback->link);
4354 free(feedback);
4355}
4356
4357static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004358presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4359{
4360 wl_resource_destroy(resource);
4361}
4362
4363static void
4364presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004365 struct wl_resource *presentation_resource,
4366 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004367 uint32_t callback)
4368{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004369 struct weston_surface *surface;
4370 struct weston_presentation_feedback *feedback;
4371
4372 surface = wl_resource_get_user_data(surface_resource);
4373
Bryce Harringtonde16d892014-11-20 22:21:57 -08004374 feedback = zalloc(sizeof *feedback);
4375 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004376 goto err_calloc;
4377
4378 feedback->resource = wl_resource_create(client,
4379 &presentation_feedback_interface,
4380 1, callback);
4381 if (!feedback->resource)
4382 goto err_create;
4383
4384 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4385 destroy_presentation_feedback);
4386
4387 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4388
4389 return;
4390
4391err_create:
4392 free(feedback);
4393
4394err_calloc:
4395 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004396}
4397
4398static const struct presentation_interface presentation_implementation = {
4399 presentation_destroy,
4400 presentation_feedback
4401};
4402
4403static void
4404bind_presentation(struct wl_client *client,
4405 void *data, uint32_t version, uint32_t id)
4406{
4407 struct weston_compositor *compositor = data;
4408 struct wl_resource *resource;
4409
4410 resource = wl_resource_create(client, &presentation_interface,
4411 MIN(version, 1), id);
4412 if (resource == NULL) {
4413 wl_client_post_no_memory(client);
4414 return;
4415 }
4416
4417 wl_resource_set_implementation(resource, &presentation_implementation,
4418 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004419 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004420}
4421
4422static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004423compositor_bind(struct wl_client *client,
4424 void *data, uint32_t version, uint32_t id)
4425{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004426 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004427 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004428
Jason Ekstranda85118c2013-06-27 20:17:02 -05004429 resource = wl_resource_create(client, &wl_compositor_interface,
4430 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004431 if (resource == NULL) {
4432 wl_client_post_no_memory(client);
4433 return;
4434 }
4435
4436 wl_resource_set_implementation(resource, &compositor_interface,
4437 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004438}
4439
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04004440static void
Martin Minarikf12c2872012-06-11 00:57:39 +02004441log_uname(void)
4442{
4443 struct utsname usys;
4444
4445 uname(&usys);
4446
4447 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
4448 usys.version, usys.machine);
4449}
4450
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004451WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004452weston_environment_get_fd(const char *env)
4453{
4454 char *e, *end;
4455 int fd, flags;
4456
4457 e = getenv(env);
4458 if (!e)
4459 return -1;
4460 fd = strtol(e, &end, 0);
4461 if (*end != '\0')
4462 return -1;
4463
4464 flags = fcntl(fd, F_GETFD);
4465 if (flags == -1)
4466 return -1;
4467
4468 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4469 unsetenv(env);
4470
4471 return fd;
4472}
4473
Pekka Paalanenb5026542014-11-12 15:09:24 +02004474static void
4475timeline_key_binding_handler(struct weston_seat *seat, uint32_t time,
4476 uint32_t key, void *data)
4477{
4478 struct weston_compositor *compositor = data;
4479
4480 if (weston_timeline_enabled_)
4481 weston_timeline_close();
4482 else
4483 weston_timeline_open(compositor);
4484}
4485
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004486WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04004487weston_compositor_init(struct weston_compositor *ec,
4488 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004489 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004490 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004491{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004492 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01004493 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004494 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004495
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004496 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04004497 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004498 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004499 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004500 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004501 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004502 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004503 wl_signal_init(&ec->idle_signal);
4504 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004505 wl_signal_init(&ec->show_input_panel_signal);
4506 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004507 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004508 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004509 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004510 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004511 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004512 wl_signal_init(&ec->session_signal);
4513 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004514
Casey Dahlin58ba1372012-04-19 22:50:08 -04004515 ec->output_id_pool = 0;
4516
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004517 if (!wl_global_create(display, &wl_compositor_interface, 3,
4518 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05004519 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004520
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004521 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
4522 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03004523 return -1;
4524
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004525 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004526 ec, bind_scaler))
4527 return -1;
4528
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004529 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4530 ec, bind_presentation))
4531 return -1;
4532
Jason Ekstranda7af7042013-10-12 22:38:11 -05004533 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004534 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004535 wl_list_init(&ec->layer_list);
4536 wl_list_init(&ec->seat_list);
4537 wl_list_init(&ec->output_list);
4538 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004539 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004540 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004541 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004542 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004543 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004544
Xiong Zhang97116532013-10-23 13:58:31 +08004545 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004546 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004547
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004548 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
4549 weston_config_section_get_string(s, "keymap_rules",
4550 (char **) &xkb_names.rules, NULL);
4551 weston_config_section_get_string(s, "keymap_model",
4552 (char **) &xkb_names.model, NULL);
4553 weston_config_section_get_string(s, "keymap_layout",
4554 (char **) &xkb_names.layout, NULL);
4555 weston_config_section_get_string(s, "keymap_variant",
4556 (char **) &xkb_names.variant, NULL);
4557 weston_config_section_get_string(s, "keymap_options",
4558 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01004559
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05004560 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
4561 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01004562
Jonny Lamb66a41a02014-08-12 14:58:25 +02004563 weston_config_section_get_int(s, "repeat-rate",
4564 &ec->kb_repeat_rate, 40);
4565 weston_config_section_get_int(s, "repeat-delay",
4566 &ec->kb_repeat_delay, 400);
4567
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004568 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01004569
4570 wl_data_device_manager_init(ec->wl_display);
4571
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04004572 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004573
Daniel Stone725c2c32012-06-22 14:04:36 +01004574 loop = wl_display_get_event_loop(ec->wl_display);
4575 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4576 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4577
4578 ec->input_loop = wl_event_loop_create();
4579
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004580 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4581 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4582
Pekka Paalanenb5026542014-11-12 15:09:24 +02004583 weston_compositor_add_debug_binding(ec, KEY_T,
4584 timeline_key_binding_handler, ec);
4585
Pekka Paalanen0513a952014-05-21 16:17:27 +03004586 s = weston_config_get_section(ec->config, "core", NULL, NULL);
4587 weston_config_section_get_int(s, "repaint-window", &ec->repaint_msec,
4588 DEFAULT_REPAINT_WINDOW);
4589 if (ec->repaint_msec < -10 || ec->repaint_msec > 1000) {
4590 weston_log("Invalid repaint_window value in config: %d\n",
4591 ec->repaint_msec);
4592 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
4593 }
4594 weston_log("Output repaint window is %d ms maximum.\n",
4595 ec->repaint_msec);
4596
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004597 weston_compositor_schedule_repaint(ec);
4598
Daniel Stone725c2c32012-06-22 14:04:36 +01004599 return 0;
4600}
4601
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004602WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004603weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004604{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004605 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004606
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004607 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004608 if (ec->input_loop_source)
4609 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004610
Matt Roper361d2ad2011-08-29 13:52:23 -07004611 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004612 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004613 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004614
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004615 if (ec->renderer)
4616 ec->renderer->destroy(ec);
4617
Daniel Stone325fc2d2012-05-30 16:31:58 +01004618 weston_binding_list_destroy_all(&ec->key_binding_list);
4619 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004620 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004621 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004622 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004623
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004624 weston_plane_release(&ec->primary_plane);
4625
Jonas Ådahlc97af922012-03-28 22:36:09 +02004626 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07004627
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004628 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07004629}
4630
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004631WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004632weston_compositor_exit_with_code(struct weston_compositor *compositor,
4633 int exit_code)
4634{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004635 if (compositor->exit_code == EXIT_SUCCESS)
4636 compositor->exit_code = exit_code;
4637
Frederic Plourdec336f062014-10-29 14:44:33 -04004638 wl_display_terminate(compositor->wl_display);
4639}
4640
4641WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004642weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4643 const struct weston_pointer_grab_interface *interface)
4644{
4645 struct weston_seat *seat;
4646
4647 ec->default_pointer_grab = interface;
4648 wl_list_for_each(seat, &ec->seat_list, link) {
4649 if (seat->pointer) {
4650 weston_pointer_set_default_grab(seat->pointer,
4651 interface);
4652 }
4653 }
4654}
4655
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004656WL_EXPORT int
4657weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4658 clockid_t clk_id)
4659{
4660 struct timespec ts;
4661
4662 if (clock_gettime(clk_id, &ts) < 0)
4663 return -1;
4664
4665 compositor->presentation_clock = clk_id;
4666
4667 return 0;
4668}
4669
4670/*
4671 * For choosing the software clock, when the display hardware or API
4672 * does not expose a compatible presentation timestamp.
4673 */
4674WL_EXPORT int
4675weston_compositor_set_presentation_clock_software(
4676 struct weston_compositor *compositor)
4677{
4678 /* In order of preference */
4679 static const clockid_t clocks[] = {
4680 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4681 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4682 CLOCK_MONOTONIC, /* no jumps, may crawl */
4683 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4684 CLOCK_REALTIME /* may jump and crawl */
4685 };
4686 unsigned i;
4687
4688 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4689 if (weston_compositor_set_presentation_clock(compositor,
4690 clocks[i]) == 0)
4691 return 0;
4692
4693 weston_log("Error: no suitable presentation clock available.\n");
4694
4695 return -1;
4696}
4697
Pekka Paalanen662f3842015-03-18 12:17:26 +02004698/** Read the current time from the Presentation clock
4699 *
4700 * \param compositor
4701 * \param ts[out] The current time.
4702 *
4703 * \note Reading the current time in user space is always imprecise to some
4704 * degree.
4705 *
4706 * This function is never meant to fail. If reading the clock does fail,
4707 * an error message is logged and a zero time is returned. Callers are not
4708 * supposed to detect or react to failures.
4709 */
4710WL_EXPORT void
4711weston_compositor_read_presentation_clock(
4712 const struct weston_compositor *compositor,
4713 struct timespec *ts)
4714{
4715 static bool warned;
4716 int ret;
4717
4718 ret = clock_gettime(compositor->presentation_clock, ts);
4719 if (ret < 0) {
4720 ts->tv_sec = 0;
4721 ts->tv_nsec = 0;
4722
4723 if (!warned)
4724 weston_log("Error: failure to read "
4725 "the presentation clock %#x: '%m' (%d)\n",
4726 compositor->presentation_clock, errno);
4727 warned = true;
4728 }
4729}
4730
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004731WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004732weston_version(int *major, int *minor, int *micro)
4733{
4734 *major = WESTON_VERSION_MAJOR;
4735 *minor = WESTON_VERSION_MINOR;
4736 *micro = WESTON_VERSION_MICRO;
4737}
4738
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004739static const char *
4740clock_name(clockid_t clk_id)
4741{
4742 static const char *names[] = {
4743 [CLOCK_REALTIME] = "CLOCK_REALTIME",
4744 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
4745 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
4746 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
4747 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
4748 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
4749 };
4750
4751 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
4752 return "unknown";
4753
4754 return names[clk_id];
4755}
4756
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004757static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004758 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004759 const char *desc;
4760} capability_strings[] = {
4761 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004762 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004763};
4764
4765static void
4766weston_compositor_log_capabilities(struct weston_compositor *compositor)
4767{
4768 unsigned i;
4769 int yes;
4770
4771 weston_log("Compositor capabilities:\n");
4772 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
4773 yes = compositor->capabilities & capability_strings[i].bit;
4774 weston_log_continue(STAMP_SPACE "%s %s\n",
4775 capability_strings[i].desc,
4776 yes ? "yes" : "no");
4777 }
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004778
4779 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
4780 clock_name(compositor->presentation_clock),
4781 compositor->presentation_clock);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004782}
4783
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004784static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004785{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004786 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004787
Martin Minarik6d118362012-06-07 18:01:59 +02004788 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004789 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004790
4791 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004792}
4793
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004794#ifdef HAVE_LIBUNWIND
4795
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004796static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004797print_backtrace(void)
4798{
4799 unw_cursor_t cursor;
4800 unw_context_t context;
4801 unw_word_t off;
4802 unw_proc_info_t pip;
4803 int ret, i = 0;
4804 char procname[256];
4805 const char *filename;
4806 Dl_info dlinfo;
4807
4808 pip.unwind_info = NULL;
4809 ret = unw_getcontext(&context);
4810 if (ret) {
4811 weston_log("unw_getcontext: %d\n", ret);
4812 return;
4813 }
4814
4815 ret = unw_init_local(&cursor, &context);
4816 if (ret) {
4817 weston_log("unw_init_local: %d\n", ret);
4818 return;
4819 }
4820
4821 ret = unw_step(&cursor);
4822 while (ret > 0) {
4823 ret = unw_get_proc_info(&cursor, &pip);
4824 if (ret) {
4825 weston_log("unw_get_proc_info: %d\n", ret);
4826 break;
4827 }
4828
4829 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4830 if (ret && ret != -UNW_ENOMEM) {
4831 if (ret != -UNW_EUNSPEC)
4832 weston_log("unw_get_proc_name: %d\n", ret);
4833 procname[0] = '?';
4834 procname[1] = 0;
4835 }
4836
4837 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4838 *dlinfo.dli_fname)
4839 filename = dlinfo.dli_fname;
4840 else
4841 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004842
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004843 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4844 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4845
4846 ret = unw_step(&cursor);
4847 if (ret < 0)
4848 weston_log("unw_step: %d\n", ret);
4849 }
4850}
4851
4852#else
4853
4854static void
4855print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004856{
4857 void *buffer[32];
4858 int i, count;
4859 Dl_info info;
4860
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004861 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4862 for (i = 0; i < count; i++) {
4863 dladdr(buffer[i], &info);
4864 weston_log(" [%016lx] %s (%s)\n",
4865 (long) buffer[i],
4866 info.dli_sname ? info.dli_sname : "--",
4867 info.dli_fname);
4868 }
4869}
4870
4871#endif
4872
4873static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004874on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004875{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004876 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004877 * then call the backend restore function, which will switch
4878 * back to the vt we launched from or ungrab X etc and then
4879 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004880 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004881 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004882 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004883
Peter Maatmane5b42e42013-03-27 22:38:53 +01004884 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004885
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004886 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004887
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004888 segv_compositor->restore(segv_compositor);
4889
4890 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004891}
4892
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004893WL_EXPORT void *
4894weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004895{
4896 char path[PATH_MAX];
4897 void *module, *init;
4898
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004899 if (name == NULL)
4900 return NULL;
4901
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004902 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004903 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004904 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004905 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004906
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004907 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4908 if (module) {
4909 weston_log("Module '%s' already loaded\n", path);
4910 dlclose(module);
4911 return NULL;
4912 }
4913
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004914 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004915 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004916 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004917 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004918 return NULL;
4919 }
4920
4921 init = dlsym(module, entrypoint);
4922 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004923 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004924 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004925 return NULL;
4926 }
4927
4928 return init;
4929}
4930
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004931static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004932load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004933 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004934{
4935 const char *p, *end;
4936 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004937 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004938 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004939
4940 if (modules == NULL)
4941 return 0;
4942
4943 p = modules;
4944 while (*p) {
4945 end = strchrnul(p, ',');
4946 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004947 module_init = weston_load_module(buffer, "module_init");
Ondřej Majerech01e98b62014-12-06 02:49:17 +01004948 if (!module_init)
4949 return -1;
4950 if (module_init(ec, argc, argv) < 0)
4951 return -1;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004952 p = end;
4953 while (*p == ',')
4954 p++;
4955
4956 }
4957
4958 return 0;
4959}
4960
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004961static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004962 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4963
4964static const char xdg_wrong_message[] =
4965 "fatal: environment variable XDG_RUNTIME_DIR\n"
4966 "is set to \"%s\", which is not a directory.\n";
4967
4968static const char xdg_wrong_mode_message[] =
4969 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004970 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4971 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004972
4973static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004974 "Refer to your distribution on how to get it, or\n"
4975 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4976 "on how to implement it.\n";
4977
Martin Minarik37032f82012-06-18 20:15:18 +02004978static void
4979verify_xdg_runtime_dir(void)
4980{
4981 char *dir = getenv("XDG_RUNTIME_DIR");
4982 struct stat s;
4983
4984 if (!dir) {
4985 weston_log(xdg_error_message);
4986 weston_log_continue(xdg_detail_message);
4987 exit(EXIT_FAILURE);
4988 }
4989
4990 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4991 weston_log(xdg_wrong_message, dir);
4992 weston_log_continue(xdg_detail_message);
4993 exit(EXIT_FAILURE);
4994 }
4995
4996 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4997 weston_log(xdg_wrong_mode_message,
4998 dir, s.st_mode & 0777, s.st_uid);
4999 weston_log_continue(xdg_detail_message);
5000 }
5001}
5002
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005003static int
5004usage(int error_code)
5005{
5006 fprintf(stderr,
5007 "Usage: weston [OPTIONS]\n\n"
5008 "This is weston version " VERSION ", the Wayland reference compositor.\n"
5009 "Weston supports multiple backends, and depending on which backend is in use\n"
5010 "different options will be accepted.\n\n"
5011
5012
5013 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06005014 " --version\t\tPrint weston version\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07005015 " -B, --backend=MODULE\tBackend module, one of\n"
5016#if defined(BUILD_DRM_COMPOSITOR)
5017 "\t\t\t\tdrm-backend.so\n"
5018#endif
5019#if defined(BUILD_FBDEV_COMPOSITOR)
Pekka Paalanen86b70e12014-11-11 14:10:46 +02005020 "\t\t\t\tfbdev-backend.so\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07005021#endif
5022#if defined(BUILD_X11_COMPOSITOR)
5023 "\t\t\t\tx11-backend.so\n"
5024#endif
5025#if defined(BUILD_WAYLAND_COMPOSITOR)
5026 "\t\t\t\twayland-backend.so\n"
5027#endif
5028#if defined(BUILD_RDP_COMPOSITOR)
5029 "\t\t\t\trdp-backend.so\n"
5030#endif
5031#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
5032 "\t\t\t\trpi-backend.so\n"
5033#endif
Jason Ekstranda985da42013-08-22 17:28:03 -05005034 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005035 " -S, --socket=NAME\tName of socket to listen on\n"
5036 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005037 " --modules\t\tLoad the comma-separated list of modules\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07005038 " --log=FILE\t\tLog to the given file\n"
Bryce Harrington06c42742015-04-02 19:16:50 -07005039 " -c, --config=FILE\tConfig file to load, defaults to weston.ini\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03005040 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005041 " -h, --help\t\tThis help message\n\n");
5042
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005043#if defined(BUILD_DRM_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005044 fprintf(stderr,
5045 "Options for drm-backend.so:\n\n"
5046 " --connector=ID\tBring up only this connector\n"
5047 " --seat=SEAT\t\tThe seat that weston should run on\n"
5048 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02005049 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005050 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005051#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005052
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005053#if defined(BUILD_FBDEV_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005054 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01005055 "Options for fbdev-backend.so:\n\n"
5056 " --tty=TTY\t\tThe tty to use\n"
5057 " --device=DEVICE\tThe framebuffer device to use\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005058#endif
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01005059
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005060#if defined(BUILD_X11_COMPOSITOR)
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01005061 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005062 "Options for x11-backend.so:\n\n"
5063 " --width=WIDTH\t\tWidth of X window\n"
5064 " --height=HEIGHT\tHeight of X window\n"
5065 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05005066 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005067 " --output-count=COUNT\tCreate multiple outputs\n"
5068 " --no-input\t\tDont create input devices\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005069#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005070
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005071#if defined(BUILD_WAYLAND_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005072 fprintf(stderr,
5073 "Options for wayland-backend.so:\n\n"
5074 " --width=WIDTH\t\tWidth of Wayland surface\n"
5075 " --height=HEIGHT\tHeight of Wayland surface\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07005076 " --scale=SCALE\t\tScale factor of output\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06005077 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05005078 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05005079 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05005080 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005081 " --display=DISPLAY\tWayland display to connect to\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005082#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005083
Pekka Paalanene31e0532013-05-22 18:03:07 +03005084#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
5085 fprintf(stderr,
5086 "Options for rpi-backend.so:\n\n"
5087 " --tty=TTY\t\tThe tty to use\n"
5088 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
5089 " --transform=TR\tThe output transformation, TR is one of:\n"
5090 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01005091 " --opaque-regions\tEnable support for opaque regions, can be "
5092 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03005093 "\n");
5094#endif
5095
Hardeningc077a842013-07-08 00:51:35 +02005096#if defined(BUILD_RDP_COMPOSITOR)
Bryce Harrington59fe4232014-10-23 14:44:34 -07005097 fprintf(stderr,
5098 "Options for rdp-backend.so:\n\n"
5099 " --width=WIDTH\t\tWidth of desktop\n"
5100 " --height=HEIGHT\tHeight of desktop\n"
5101 " --env-socket=SOCKET\tUse that socket as peer connection\n"
5102 " --address=ADDR\tThe address to bind\n"
Bryce Harringtonf5b34ae2014-10-23 14:44:35 -07005103 " --port=PORT\t\tThe port to listen on\n"
Bryce Harrington59fe4232014-10-23 14:44:34 -07005104 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
5105 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
5106 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
5107 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
5108 "\n");
Hardeningc077a842013-07-08 00:51:35 +02005109#endif
5110
Bryce Harrington3e3b59e2014-11-19 15:06:19 -08005111#if defined(BUILD_HEADLESS_COMPOSITOR)
5112 fprintf(stderr,
5113 "Options for headless-backend.so:\n\n"
5114 " --width=WIDTH\t\tWidth of memory surface\n"
5115 " --height=HEIGHT\tHeight of memory surface\n"
5116 " --transform=TR\tThe output transformation, TR is one of:\n"
5117 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
5118 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
5119#endif
5120
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005121 exit(error_code);
5122}
5123
Peter Maatmane5b42e42013-03-27 22:38:53 +01005124static void
5125catch_signals(void)
5126{
5127 struct sigaction action;
5128
5129 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
5130 action.sa_sigaction = on_caught_signal;
5131 sigemptyset(&action.sa_mask);
5132 sigaction(SIGSEGV, &action, NULL);
5133 sigaction(SIGABRT, &action, NULL);
5134}
5135
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005136static void
5137handle_primary_client_destroyed(struct wl_listener *listener, void *data)
5138{
5139 struct wl_client *client = data;
5140
5141 weston_log("Primary client died. Closing...\n");
5142
5143 wl_display_terminate(wl_client_get_display(client));
5144}
5145
Ryo Munakatad8deff62014-09-06 07:32:05 +09005146static char *
5147weston_choose_default_backend(void)
5148{
5149 char *backend = NULL;
5150
5151 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
5152 backend = strdup("wayland-backend.so");
5153 else if (getenv("DISPLAY"))
5154 backend = strdup("x11-backend.so");
5155 else
5156 backend = strdup(WESTON_NATIVE_BACKEND);
5157
5158 return backend;
5159}
5160
5161static int
5162weston_create_listening_socket(struct wl_display *display, const char *socket_name)
5163{
5164 if (socket_name) {
5165 if (wl_display_add_socket(display, socket_name)) {
5166 weston_log("fatal: failed to add socket: %m\n");
5167 return -1;
5168 }
5169 } else {
5170 socket_name = wl_display_add_socket_auto(display);
5171 if (!socket_name) {
5172 weston_log("fatal: failed to add socket: %m\n");
5173 return -1;
5174 }
5175 }
5176
5177 setenv("WAYLAND_DISPLAY", socket_name, 1);
5178
5179 return 0;
5180}
5181
Derek Foreman64a3df02014-10-23 12:24:18 -05005182static const struct { const char *name; uint32_t token; } transforms[] = {
5183 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
5184 { "90", WL_OUTPUT_TRANSFORM_90 },
5185 { "180", WL_OUTPUT_TRANSFORM_180 },
5186 { "270", WL_OUTPUT_TRANSFORM_270 },
5187 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
5188 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
5189 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
5190 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
5191};
5192
5193WL_EXPORT int
5194weston_parse_transform(const char *transform, uint32_t *out)
5195{
5196 unsigned int i;
5197
5198 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
5199 if (strcmp(transforms[i].name, transform) == 0) {
5200 *out = transforms[i].token;
5201 return 0;
5202 }
5203
5204 *out = WL_OUTPUT_TRANSFORM_NORMAL;
5205 return -1;
5206}
5207
5208WL_EXPORT const char *
5209weston_transform_to_string(uint32_t output_transform)
5210{
5211 unsigned int i;
5212
5213 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
5214 if (transforms[i].token == output_transform)
5215 return transforms[i].name;
5216
5217 return "<illegal value>";
5218}
5219
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005220static int
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005221load_configuration(struct weston_config **config, int32_t noconfig,
5222 const char *config_file)
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005223{
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005224 const char *file = "weston.ini";
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02005225 const char *full_path;
5226
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005227 *config = NULL;
5228
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005229 if (config_file)
5230 file = config_file;
5231
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005232 if (noconfig == 0)
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005233 *config = weston_config_parse(file);
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005234
5235 if (*config) {
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02005236 full_path = weston_config_get_full_path(*config);
5237
5238 weston_log("Using config file '%s'\n", full_path);
5239 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005240
5241 return 0;
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005242 }
5243
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005244 if (config_file && noconfig == 0) {
5245 weston_log("fatal: error opening or reading config file"
5246 " '%s'.\n", config_file);
5247
5248 return -1;
5249 }
5250
5251 weston_log("Starting with no config file.\n");
5252 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
5253
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005254 return 0;
5255}
Derek Foreman64a3df02014-10-23 12:24:18 -05005256
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005257int main(int argc, char *argv[])
5258{
Pekka Paalanen111c6f92015-03-20 14:35:58 +02005259 int ret = EXIT_FAILURE;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005260 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005261 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005262 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05005263 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005264 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005265 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005266 int *argc, char *argv[],
5267 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005268 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005269 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05005270 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02005271 char *modules = NULL;
5272 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02005273 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005274 char *server_socket = NULL, *end;
Frederic Plourde4a84c832014-10-30 15:06:34 -04005275 int32_t idle_time = -1;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005276 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09005277 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06005278 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03005279 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005280 int32_t numlock_on;
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005281 char *config_file = NULL;
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005282 struct weston_config *config;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005283 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005284 struct wl_client *primary_client;
5285 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005286 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04005287
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005288 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09005289 { WESTON_OPTION_STRING, "backend", 'B', &backend },
5290 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005291 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
5292 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005293 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02005294 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005295 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06005296 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03005297 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005298 { WESTON_OPTION_STRING, "config", 'c', &config_file },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04005299 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05005300
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005301 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005302
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005303 if (help)
5304 usage(EXIT_SUCCESS);
5305
Scott Moreau12245142012-08-29 15:15:58 -06005306 if (version) {
5307 printf(PACKAGE_STRING "\n");
5308 return EXIT_SUCCESS;
5309 }
5310
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02005311 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005312
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03005313 weston_log("%s\n"
5314 STAMP_SPACE "%s\n"
5315 STAMP_SPACE "Bug reports to: %s\n"
5316 STAMP_SPACE "Build: %s\n",
5317 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04005318 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03005319 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04005320
Martin Minarik37032f82012-06-18 20:15:18 +02005321 verify_xdg_runtime_dir();
5322
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005323 display = wl_display_create();
5324
Tiago Vignatti2116b892011-08-08 05:52:59 -07005325 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005326 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
5327 display);
5328 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
5329 display);
5330 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
5331 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07005332
5333 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005334 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
5335 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05005336
Pekka Paalanen111c6f92015-03-20 14:35:58 +02005337 if (!signals[0] || !signals[1] || !signals[2] || !signals[3])
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305338 goto out_signals;
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305339
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005340 if (load_configuration(&config, noconfig, config_file) < 0)
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005341 goto out_signals;
5342
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01005343 section = weston_config_get_section(config, "core", NULL, NULL);
5344
Ryo Munakata03faed22014-09-06 07:32:51 +09005345 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005346 weston_config_section_get_string(section, "backend", &backend,
5347 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09005348 if (!backend)
5349 backend = weston_choose_default_backend();
5350 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005351
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03005352 backend_init = weston_load_module(backend, "backend_init");
Pekka Paalanen111c6f92015-03-20 14:35:58 +02005353 if (!backend_init)
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305354 goto out_signals;
Kristian Høgsberga9410222011-01-14 17:22:35 -05005355
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005356 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05005357 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03005358 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305359 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05005360 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04005361
Peter Maatmane5b42e42013-03-27 22:38:53 +01005362 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04005363 segv_compositor = ec;
5364
Frederic Plourde4a84c832014-10-30 15:06:34 -04005365 if (idle_time < 0)
5366 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
5367 if (idle_time < 0)
5368 idle_time = 300; /* default idle timeout, in seconds */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005369 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005370 ec->default_pointer_grab = NULL;
Frederic Plourdec336f062014-10-29 14:44:33 -04005371 ec->exit_code = EXIT_SUCCESS;
Pekka Paalanen7296e792011-12-07 16:22:00 +02005372
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005373 weston_compositor_log_capabilities(ec);
5374
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005375 server_socket = getenv("WAYLAND_SERVER_SOCKET");
5376 if (server_socket) {
5377 weston_log("Running with single client\n");
5378 fd = strtol(server_socket, &end, 0);
5379 if (*end != '\0')
5380 fd = -1;
5381 } else {
5382 fd = -1;
5383 }
5384
5385 if (fd != -1) {
5386 primary_client = wl_client_create(display, fd);
5387 if (!primary_client) {
5388 weston_log("fatal: failed to add client: %m\n");
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005389 goto out;
5390 }
5391 primary_client_destroyed.notify =
5392 handle_primary_client_destroyed;
5393 wl_client_add_destroy_listener(primary_client,
5394 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09005395 } else if (weston_create_listening_socket(display, socket_name)) {
Ryo Munakatad8deff62014-09-06 07:32:05 +09005396 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005397 }
5398
Ryo Munakata03faed22014-09-06 07:32:51 +09005399 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005400 weston_config_section_get_string(section, "shell", &shell,
5401 "desktop-shell.so");
5402
Ryo Munakata03faed22014-09-06 07:32:51 +09005403 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005404 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005405
5406 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09005407 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005408 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005409
5410 if (load_modules(ec, option_modules, &argc, argv) < 0)
5411 goto out;
5412
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005413 section = weston_config_get_section(config, "keyboard", NULL, NULL);
5414 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
5415 if (numlock_on) {
5416 wl_list_for_each(seat, &ec->seat_list, link) {
5417 if (seat->keyboard)
5418 weston_keyboard_set_locks(seat->keyboard,
5419 WESTON_NUM_LOCK,
5420 WESTON_NUM_LOCK);
5421 }
5422 }
5423
Pekka Paalanen17a1a962015-03-24 15:56:15 +02005424 for (i = 1; i < argc; i++)
5425 weston_log("fatal: unhandled option: %s\n", argv[i]);
5426 if (argc > 1)
5427 goto out;
5428
Pekka Paalanenc0444e32012-01-05 16:28:21 +02005429 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005430
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04005431 wl_display_run(display);
5432
Frederic Plourdec336f062014-10-29 14:44:33 -04005433 /* Allow for setting return exit code after
5434 * wl_display_run returns normally. This is
5435 * useful for devs/testers and automated tests
5436 * that want to indicate failure status to
5437 * testing infrastructure above
5438 */
5439 ret = ec->exit_code;
5440
Ryo Munakata03faed22014-09-06 07:32:51 +09005441out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005442 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005443 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005444
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005445 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005446
Daniel Stone855028f2012-05-01 20:37:10 +01005447 weston_compositor_xkb_destroy(ec);
5448
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005449 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305450
5451out_signals:
5452 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
5453 if (signals[i])
5454 wl_event_source_remove(signals[i]);
5455
Tiago Vignatti9e2be082011-12-19 00:04:46 +02005456 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005457
Martin Minarik19e6f262012-06-07 13:08:46 +02005458 weston_log_file_close();
5459
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005460 free(config_file);
Ryo Munakata03faed22014-09-06 07:32:51 +09005461 free(backend);
5462 free(shell);
5463 free(socket_name);
5464 free(option_modules);
5465 free(log);
5466 free(modules);
5467
Pekka Paalanen3ab72692012-06-08 17:27:28 +03005468 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04005469}