blob: 8f02b4de4b7b3f9d2f6a0f248255221a4adf4166 [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)
Derek Foreman4c93c082015-04-30 16:45:41 -05001817 weston_touch_set_focus(seat->touch, 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
Pekka Paalanenca790762015-04-17 14:23:38 +03001879 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001880
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);
Pekka Paalanenca790762015-04-17 14:23:38 +03003486 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003487
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);
Pekka Paalanenca790762015-04-17 14:23:38 +03003501 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003502 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);
Giulio Camuffob1147152015-05-06 21:41:57 +03004128}
4129
4130/** Adds an output to the compositor's output list and
4131 * send the compositor's output_created signal.
4132 *
4133 * \param compositor The compositor instance.
4134 * \param output The output to be added.
4135 */
4136WL_EXPORT void
4137weston_compositor_add_output(struct weston_compositor *compositor,
4138 struct weston_output *output)
4139{
4140 wl_list_insert(compositor->output_list.prev, &output->link);
4141 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004142}
4143
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004144WL_EXPORT void
4145weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05004146 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004147 wl_fixed_t *x, wl_fixed_t *y)
4148{
Derek Foreman0f679412014-10-02 13:41:17 -05004149 struct weston_vector p = { {
4150 wl_fixed_to_double(device_x),
4151 wl_fixed_to_double(device_y),
4152 0.0,
4153 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004154
Derek Foreman67a18b92015-03-24 11:36:14 -05004155 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004156
Derek Foreman0f679412014-10-02 13:41:17 -05004157 *x = wl_fixed_from_double(p.f[0] / p.f[3]);
4158 *y = wl_fixed_from_double(p.f[1] / p.f[3]);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004159}
4160
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004161static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004162destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004163{
Jonny Lamb74130762013-11-26 18:19:46 +01004164 struct weston_surface *surface =
4165 wl_resource_get_user_data(resource);
4166
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004167 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004168 surface->pending.buffer_viewport.buffer.src_width =
4169 wl_fixed_from_int(-1);
4170 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004171 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004172}
4173
4174static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004175viewport_destroy(struct wl_client *client,
4176 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004177{
4178 wl_resource_destroy(resource);
4179}
4180
4181static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004182viewport_set(struct wl_client *client,
4183 struct wl_resource *resource,
4184 wl_fixed_t src_x,
4185 wl_fixed_t src_y,
4186 wl_fixed_t src_width,
4187 wl_fixed_t src_height,
4188 int32_t dst_width,
4189 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004190{
Jonny Lamb74130762013-11-26 18:19:46 +01004191 struct weston_surface *surface =
4192 wl_resource_get_user_data(resource);
4193
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004194 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01004195
Jonny Lamb8ae35902013-11-26 18:19:45 +01004196 if (wl_fixed_to_double(src_width) < 0 ||
4197 wl_fixed_to_double(src_height) < 0) {
4198 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004199 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004200 "source dimensions must be non-negative (%fx%f)",
4201 wl_fixed_to_double(src_width),
4202 wl_fixed_to_double(src_height));
4203 return;
4204 }
4205
4206 if (dst_width <= 0 || dst_height <= 0) {
4207 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004208 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004209 "destination dimensions must be positive (%dx%d)",
4210 dst_width, dst_height);
4211 return;
4212 }
4213
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004214 surface->pending.buffer_viewport.buffer.src_x = src_x;
4215 surface->pending.buffer_viewport.buffer.src_y = src_y;
4216 surface->pending.buffer_viewport.buffer.src_width = src_width;
4217 surface->pending.buffer_viewport.buffer.src_height = src_height;
4218 surface->pending.buffer_viewport.surface.width = dst_width;
4219 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004220 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004221}
4222
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004223static void
4224viewport_set_source(struct wl_client *client,
4225 struct wl_resource *resource,
4226 wl_fixed_t src_x,
4227 wl_fixed_t src_y,
4228 wl_fixed_t src_width,
4229 wl_fixed_t src_height)
4230{
4231 struct weston_surface *surface =
4232 wl_resource_get_user_data(resource);
4233
4234 assert(surface->viewport_resource != NULL);
4235
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004236 if (src_width == wl_fixed_from_int(-1) &&
4237 src_height == wl_fixed_from_int(-1)) {
4238 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004239 surface->pending.buffer_viewport.buffer.src_width =
4240 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004241 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004242 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004243 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004244
4245 if (src_width <= 0 || src_height <= 0) {
4246 wl_resource_post_error(resource,
4247 WL_VIEWPORT_ERROR_BAD_VALUE,
4248 "source size must be positive (%fx%f)",
4249 wl_fixed_to_double(src_width),
4250 wl_fixed_to_double(src_height));
4251 return;
4252 }
4253
4254 surface->pending.buffer_viewport.buffer.src_x = src_x;
4255 surface->pending.buffer_viewport.buffer.src_y = src_y;
4256 surface->pending.buffer_viewport.buffer.src_width = src_width;
4257 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004258 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004259}
4260
4261static void
4262viewport_set_destination(struct wl_client *client,
4263 struct wl_resource *resource,
4264 int32_t dst_width,
4265 int32_t dst_height)
4266{
4267 struct weston_surface *surface =
4268 wl_resource_get_user_data(resource);
4269
4270 assert(surface->viewport_resource != NULL);
4271
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004272 if (dst_width == -1 && dst_height == -1) {
4273 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004274 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004275 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004276 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004277 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004278
4279 if (dst_width <= 0 || dst_height <= 0) {
4280 wl_resource_post_error(resource,
4281 WL_VIEWPORT_ERROR_BAD_VALUE,
4282 "destination size must be positive (%dx%d)",
4283 dst_width, dst_height);
4284 return;
4285 }
4286
4287 surface->pending.buffer_viewport.surface.width = dst_width;
4288 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004289 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004290}
4291
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004292static const struct wl_viewport_interface viewport_interface = {
4293 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004294 viewport_set,
4295 viewport_set_source,
4296 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004297};
4298
4299static void
4300scaler_destroy(struct wl_client *client,
4301 struct wl_resource *resource)
4302{
4303 wl_resource_destroy(resource);
4304}
4305
4306static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004307scaler_get_viewport(struct wl_client *client,
4308 struct wl_resource *scaler,
4309 uint32_t id,
4310 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004311{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004312 int version = wl_resource_get_version(scaler);
4313 struct weston_surface *surface =
4314 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004315 struct wl_resource *resource;
4316
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004317 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01004318 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004319 WL_SCALER_ERROR_VIEWPORT_EXISTS,
4320 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004321 return;
4322 }
4323
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004324 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004325 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004326 if (resource == NULL) {
4327 wl_client_post_no_memory(client);
4328 return;
4329 }
4330
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004331 wl_resource_set_implementation(resource, &viewport_interface,
4332 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004333
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004334 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004335}
4336
4337static const struct wl_scaler_interface scaler_interface = {
4338 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004339 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004340};
4341
4342static void
4343bind_scaler(struct wl_client *client,
4344 void *data, uint32_t version, uint32_t id)
4345{
4346 struct wl_resource *resource;
4347
4348 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004349 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004350 if (resource == NULL) {
4351 wl_client_post_no_memory(client);
4352 return;
4353 }
4354
4355 wl_resource_set_implementation(resource, &scaler_interface,
4356 NULL, NULL);
4357}
4358
4359static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004360destroy_presentation_feedback(struct wl_resource *feedback_resource)
4361{
4362 struct weston_presentation_feedback *feedback;
4363
4364 feedback = wl_resource_get_user_data(feedback_resource);
4365
4366 wl_list_remove(&feedback->link);
4367 free(feedback);
4368}
4369
4370static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004371presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4372{
4373 wl_resource_destroy(resource);
4374}
4375
4376static void
4377presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004378 struct wl_resource *presentation_resource,
4379 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004380 uint32_t callback)
4381{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004382 struct weston_surface *surface;
4383 struct weston_presentation_feedback *feedback;
4384
4385 surface = wl_resource_get_user_data(surface_resource);
4386
Bryce Harringtonde16d892014-11-20 22:21:57 -08004387 feedback = zalloc(sizeof *feedback);
4388 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004389 goto err_calloc;
4390
4391 feedback->resource = wl_resource_create(client,
4392 &presentation_feedback_interface,
4393 1, callback);
4394 if (!feedback->resource)
4395 goto err_create;
4396
4397 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4398 destroy_presentation_feedback);
4399
4400 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4401
4402 return;
4403
4404err_create:
4405 free(feedback);
4406
4407err_calloc:
4408 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004409}
4410
4411static const struct presentation_interface presentation_implementation = {
4412 presentation_destroy,
4413 presentation_feedback
4414};
4415
4416static void
4417bind_presentation(struct wl_client *client,
4418 void *data, uint32_t version, uint32_t id)
4419{
4420 struct weston_compositor *compositor = data;
4421 struct wl_resource *resource;
4422
4423 resource = wl_resource_create(client, &presentation_interface,
4424 MIN(version, 1), id);
4425 if (resource == NULL) {
4426 wl_client_post_no_memory(client);
4427 return;
4428 }
4429
4430 wl_resource_set_implementation(resource, &presentation_implementation,
4431 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004432 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004433}
4434
4435static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004436compositor_bind(struct wl_client *client,
4437 void *data, uint32_t version, uint32_t id)
4438{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004439 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004440 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004441
Jason Ekstranda85118c2013-06-27 20:17:02 -05004442 resource = wl_resource_create(client, &wl_compositor_interface,
4443 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004444 if (resource == NULL) {
4445 wl_client_post_no_memory(client);
4446 return;
4447 }
4448
4449 wl_resource_set_implementation(resource, &compositor_interface,
4450 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004451}
4452
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04004453static void
Martin Minarikf12c2872012-06-11 00:57:39 +02004454log_uname(void)
4455{
4456 struct utsname usys;
4457
4458 uname(&usys);
4459
4460 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
4461 usys.version, usys.machine);
4462}
4463
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004464WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004465weston_environment_get_fd(const char *env)
4466{
4467 char *e, *end;
4468 int fd, flags;
4469
4470 e = getenv(env);
4471 if (!e)
4472 return -1;
4473 fd = strtol(e, &end, 0);
4474 if (*end != '\0')
4475 return -1;
4476
4477 flags = fcntl(fd, F_GETFD);
4478 if (flags == -1)
4479 return -1;
4480
4481 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4482 unsetenv(env);
4483
4484 return fd;
4485}
4486
Pekka Paalanenb5026542014-11-12 15:09:24 +02004487static void
4488timeline_key_binding_handler(struct weston_seat *seat, uint32_t time,
4489 uint32_t key, void *data)
4490{
4491 struct weston_compositor *compositor = data;
4492
4493 if (weston_timeline_enabled_)
4494 weston_timeline_close();
4495 else
4496 weston_timeline_open(compositor);
4497}
4498
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004499WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04004500weston_compositor_init(struct weston_compositor *ec,
4501 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004502 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004503 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004504{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004505 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01004506 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004507 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004508
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004509 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04004510 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004511 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004512 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004513 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004514 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004515 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004516 wl_signal_init(&ec->idle_signal);
4517 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004518 wl_signal_init(&ec->show_input_panel_signal);
4519 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004520 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004521 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004522 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004523 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004524 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004525 wl_signal_init(&ec->session_signal);
4526 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004527
Casey Dahlin58ba1372012-04-19 22:50:08 -04004528 ec->output_id_pool = 0;
4529
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004530 if (!wl_global_create(display, &wl_compositor_interface, 3,
4531 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05004532 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004533
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004534 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
4535 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03004536 return -1;
4537
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004538 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004539 ec, bind_scaler))
4540 return -1;
4541
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004542 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4543 ec, bind_presentation))
4544 return -1;
4545
Jason Ekstranda7af7042013-10-12 22:38:11 -05004546 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004547 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004548 wl_list_init(&ec->layer_list);
4549 wl_list_init(&ec->seat_list);
4550 wl_list_init(&ec->output_list);
4551 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004552 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004553 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004554 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004555 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004556 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004557
Xiong Zhang97116532013-10-23 13:58:31 +08004558 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004559 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004560
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004561 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
4562 weston_config_section_get_string(s, "keymap_rules",
4563 (char **) &xkb_names.rules, NULL);
4564 weston_config_section_get_string(s, "keymap_model",
4565 (char **) &xkb_names.model, NULL);
4566 weston_config_section_get_string(s, "keymap_layout",
4567 (char **) &xkb_names.layout, NULL);
4568 weston_config_section_get_string(s, "keymap_variant",
4569 (char **) &xkb_names.variant, NULL);
4570 weston_config_section_get_string(s, "keymap_options",
4571 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01004572
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05004573 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
4574 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01004575
Jonny Lamb66a41a02014-08-12 14:58:25 +02004576 weston_config_section_get_int(s, "repeat-rate",
4577 &ec->kb_repeat_rate, 40);
4578 weston_config_section_get_int(s, "repeat-delay",
4579 &ec->kb_repeat_delay, 400);
4580
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004581 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01004582
4583 wl_data_device_manager_init(ec->wl_display);
4584
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04004585 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004586
Daniel Stone725c2c32012-06-22 14:04:36 +01004587 loop = wl_display_get_event_loop(ec->wl_display);
4588 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4589 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4590
4591 ec->input_loop = wl_event_loop_create();
4592
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004593 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4594 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4595
Pekka Paalanenb5026542014-11-12 15:09:24 +02004596 weston_compositor_add_debug_binding(ec, KEY_T,
4597 timeline_key_binding_handler, ec);
4598
Pekka Paalanen0513a952014-05-21 16:17:27 +03004599 s = weston_config_get_section(ec->config, "core", NULL, NULL);
4600 weston_config_section_get_int(s, "repaint-window", &ec->repaint_msec,
4601 DEFAULT_REPAINT_WINDOW);
4602 if (ec->repaint_msec < -10 || ec->repaint_msec > 1000) {
4603 weston_log("Invalid repaint_window value in config: %d\n",
4604 ec->repaint_msec);
4605 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
4606 }
4607 weston_log("Output repaint window is %d ms maximum.\n",
4608 ec->repaint_msec);
4609
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004610 weston_compositor_schedule_repaint(ec);
4611
Daniel Stone725c2c32012-06-22 14:04:36 +01004612 return 0;
4613}
4614
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004615WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004616weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004617{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004618 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004619
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004620 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004621 if (ec->input_loop_source)
4622 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004623
Matt Roper361d2ad2011-08-29 13:52:23 -07004624 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004625 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004626 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004627
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004628 if (ec->renderer)
4629 ec->renderer->destroy(ec);
4630
Daniel Stone325fc2d2012-05-30 16:31:58 +01004631 weston_binding_list_destroy_all(&ec->key_binding_list);
4632 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004633 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004634 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004635 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004636
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004637 weston_plane_release(&ec->primary_plane);
4638
Jonas Ådahlc97af922012-03-28 22:36:09 +02004639 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07004640
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004641 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07004642}
4643
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004644WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004645weston_compositor_exit_with_code(struct weston_compositor *compositor,
4646 int exit_code)
4647{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004648 if (compositor->exit_code == EXIT_SUCCESS)
4649 compositor->exit_code = exit_code;
4650
Frederic Plourdec336f062014-10-29 14:44:33 -04004651 wl_display_terminate(compositor->wl_display);
4652}
4653
4654WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004655weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4656 const struct weston_pointer_grab_interface *interface)
4657{
4658 struct weston_seat *seat;
4659
4660 ec->default_pointer_grab = interface;
4661 wl_list_for_each(seat, &ec->seat_list, link) {
4662 if (seat->pointer) {
4663 weston_pointer_set_default_grab(seat->pointer,
4664 interface);
4665 }
4666 }
4667}
4668
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004669WL_EXPORT int
4670weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4671 clockid_t clk_id)
4672{
4673 struct timespec ts;
4674
4675 if (clock_gettime(clk_id, &ts) < 0)
4676 return -1;
4677
4678 compositor->presentation_clock = clk_id;
4679
4680 return 0;
4681}
4682
4683/*
4684 * For choosing the software clock, when the display hardware or API
4685 * does not expose a compatible presentation timestamp.
4686 */
4687WL_EXPORT int
4688weston_compositor_set_presentation_clock_software(
4689 struct weston_compositor *compositor)
4690{
4691 /* In order of preference */
4692 static const clockid_t clocks[] = {
4693 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4694 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4695 CLOCK_MONOTONIC, /* no jumps, may crawl */
4696 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4697 CLOCK_REALTIME /* may jump and crawl */
4698 };
4699 unsigned i;
4700
4701 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4702 if (weston_compositor_set_presentation_clock(compositor,
4703 clocks[i]) == 0)
4704 return 0;
4705
4706 weston_log("Error: no suitable presentation clock available.\n");
4707
4708 return -1;
4709}
4710
Pekka Paalanen662f3842015-03-18 12:17:26 +02004711/** Read the current time from the Presentation clock
4712 *
4713 * \param compositor
4714 * \param ts[out] The current time.
4715 *
4716 * \note Reading the current time in user space is always imprecise to some
4717 * degree.
4718 *
4719 * This function is never meant to fail. If reading the clock does fail,
4720 * an error message is logged and a zero time is returned. Callers are not
4721 * supposed to detect or react to failures.
4722 */
4723WL_EXPORT void
4724weston_compositor_read_presentation_clock(
4725 const struct weston_compositor *compositor,
4726 struct timespec *ts)
4727{
4728 static bool warned;
4729 int ret;
4730
4731 ret = clock_gettime(compositor->presentation_clock, ts);
4732 if (ret < 0) {
4733 ts->tv_sec = 0;
4734 ts->tv_nsec = 0;
4735
4736 if (!warned)
4737 weston_log("Error: failure to read "
4738 "the presentation clock %#x: '%m' (%d)\n",
4739 compositor->presentation_clock, errno);
4740 warned = true;
4741 }
4742}
4743
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004744WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004745weston_version(int *major, int *minor, int *micro)
4746{
4747 *major = WESTON_VERSION_MAJOR;
4748 *minor = WESTON_VERSION_MINOR;
4749 *micro = WESTON_VERSION_MICRO;
4750}
4751
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004752static const char *
4753clock_name(clockid_t clk_id)
4754{
4755 static const char *names[] = {
4756 [CLOCK_REALTIME] = "CLOCK_REALTIME",
4757 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
4758 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
4759 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
4760 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
4761 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
4762 };
4763
4764 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
4765 return "unknown";
4766
4767 return names[clk_id];
4768}
4769
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004770static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004771 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004772 const char *desc;
4773} capability_strings[] = {
4774 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004775 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004776};
4777
4778static void
4779weston_compositor_log_capabilities(struct weston_compositor *compositor)
4780{
4781 unsigned i;
4782 int yes;
4783
4784 weston_log("Compositor capabilities:\n");
4785 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
4786 yes = compositor->capabilities & capability_strings[i].bit;
4787 weston_log_continue(STAMP_SPACE "%s %s\n",
4788 capability_strings[i].desc,
4789 yes ? "yes" : "no");
4790 }
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004791
4792 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
4793 clock_name(compositor->presentation_clock),
4794 compositor->presentation_clock);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004795}
4796
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004797static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004798{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004799 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004800
Martin Minarik6d118362012-06-07 18:01:59 +02004801 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004802 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004803
4804 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004805}
4806
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004807#ifdef HAVE_LIBUNWIND
4808
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004809static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004810print_backtrace(void)
4811{
4812 unw_cursor_t cursor;
4813 unw_context_t context;
4814 unw_word_t off;
4815 unw_proc_info_t pip;
4816 int ret, i = 0;
4817 char procname[256];
4818 const char *filename;
4819 Dl_info dlinfo;
4820
4821 pip.unwind_info = NULL;
4822 ret = unw_getcontext(&context);
4823 if (ret) {
4824 weston_log("unw_getcontext: %d\n", ret);
4825 return;
4826 }
4827
4828 ret = unw_init_local(&cursor, &context);
4829 if (ret) {
4830 weston_log("unw_init_local: %d\n", ret);
4831 return;
4832 }
4833
4834 ret = unw_step(&cursor);
4835 while (ret > 0) {
4836 ret = unw_get_proc_info(&cursor, &pip);
4837 if (ret) {
4838 weston_log("unw_get_proc_info: %d\n", ret);
4839 break;
4840 }
4841
4842 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4843 if (ret && ret != -UNW_ENOMEM) {
4844 if (ret != -UNW_EUNSPEC)
4845 weston_log("unw_get_proc_name: %d\n", ret);
4846 procname[0] = '?';
4847 procname[1] = 0;
4848 }
4849
4850 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4851 *dlinfo.dli_fname)
4852 filename = dlinfo.dli_fname;
4853 else
4854 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004855
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004856 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4857 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4858
4859 ret = unw_step(&cursor);
4860 if (ret < 0)
4861 weston_log("unw_step: %d\n", ret);
4862 }
4863}
4864
4865#else
4866
4867static void
4868print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004869{
4870 void *buffer[32];
4871 int i, count;
4872 Dl_info info;
4873
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004874 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4875 for (i = 0; i < count; i++) {
4876 dladdr(buffer[i], &info);
4877 weston_log(" [%016lx] %s (%s)\n",
4878 (long) buffer[i],
4879 info.dli_sname ? info.dli_sname : "--",
4880 info.dli_fname);
4881 }
4882}
4883
4884#endif
4885
4886static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004887on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004888{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004889 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004890 * then call the backend restore function, which will switch
4891 * back to the vt we launched from or ungrab X etc and then
4892 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004893 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004894 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004895 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004896
Peter Maatmane5b42e42013-03-27 22:38:53 +01004897 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004898
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004899 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004900
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004901 segv_compositor->restore(segv_compositor);
4902
4903 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004904}
4905
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004906WL_EXPORT void *
4907weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004908{
4909 char path[PATH_MAX];
4910 void *module, *init;
4911
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004912 if (name == NULL)
4913 return NULL;
4914
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004915 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004916 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004917 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004918 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004919
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004920 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4921 if (module) {
4922 weston_log("Module '%s' already loaded\n", path);
4923 dlclose(module);
4924 return NULL;
4925 }
4926
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004927 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004928 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004929 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004930 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004931 return NULL;
4932 }
4933
4934 init = dlsym(module, entrypoint);
4935 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004936 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004937 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004938 return NULL;
4939 }
4940
4941 return init;
4942}
4943
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004944static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004945load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004946 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004947{
4948 const char *p, *end;
4949 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004950 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004951 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004952
4953 if (modules == NULL)
4954 return 0;
4955
4956 p = modules;
4957 while (*p) {
4958 end = strchrnul(p, ',');
4959 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004960 module_init = weston_load_module(buffer, "module_init");
Ondřej Majerech01e98b62014-12-06 02:49:17 +01004961 if (!module_init)
4962 return -1;
4963 if (module_init(ec, argc, argv) < 0)
4964 return -1;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004965 p = end;
4966 while (*p == ',')
4967 p++;
4968
4969 }
4970
4971 return 0;
4972}
4973
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004974static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004975 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4976
4977static const char xdg_wrong_message[] =
4978 "fatal: environment variable XDG_RUNTIME_DIR\n"
4979 "is set to \"%s\", which is not a directory.\n";
4980
4981static const char xdg_wrong_mode_message[] =
4982 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004983 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4984 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004985
4986static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004987 "Refer to your distribution on how to get it, or\n"
4988 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4989 "on how to implement it.\n";
4990
Martin Minarik37032f82012-06-18 20:15:18 +02004991static void
4992verify_xdg_runtime_dir(void)
4993{
4994 char *dir = getenv("XDG_RUNTIME_DIR");
4995 struct stat s;
4996
4997 if (!dir) {
4998 weston_log(xdg_error_message);
4999 weston_log_continue(xdg_detail_message);
5000 exit(EXIT_FAILURE);
5001 }
5002
5003 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
5004 weston_log(xdg_wrong_message, dir);
5005 weston_log_continue(xdg_detail_message);
5006 exit(EXIT_FAILURE);
5007 }
5008
5009 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
5010 weston_log(xdg_wrong_mode_message,
5011 dir, s.st_mode & 0777, s.st_uid);
5012 weston_log_continue(xdg_detail_message);
5013 }
5014}
5015
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005016static int
5017usage(int error_code)
5018{
5019 fprintf(stderr,
5020 "Usage: weston [OPTIONS]\n\n"
5021 "This is weston version " VERSION ", the Wayland reference compositor.\n"
5022 "Weston supports multiple backends, and depending on which backend is in use\n"
5023 "different options will be accepted.\n\n"
5024
5025
5026 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06005027 " --version\t\tPrint weston version\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07005028 " -B, --backend=MODULE\tBackend module, one of\n"
5029#if defined(BUILD_DRM_COMPOSITOR)
5030 "\t\t\t\tdrm-backend.so\n"
5031#endif
5032#if defined(BUILD_FBDEV_COMPOSITOR)
Pekka Paalanen86b70e12014-11-11 14:10:46 +02005033 "\t\t\t\tfbdev-backend.so\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07005034#endif
5035#if defined(BUILD_X11_COMPOSITOR)
5036 "\t\t\t\tx11-backend.so\n"
5037#endif
5038#if defined(BUILD_WAYLAND_COMPOSITOR)
5039 "\t\t\t\twayland-backend.so\n"
5040#endif
5041#if defined(BUILD_RDP_COMPOSITOR)
5042 "\t\t\t\trdp-backend.so\n"
5043#endif
5044#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
5045 "\t\t\t\trpi-backend.so\n"
5046#endif
Jason Ekstranda985da42013-08-22 17:28:03 -05005047 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005048 " -S, --socket=NAME\tName of socket to listen on\n"
5049 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005050 " --modules\t\tLoad the comma-separated list of modules\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07005051 " --log=FILE\t\tLog to the given file\n"
Bryce Harrington06c42742015-04-02 19:16:50 -07005052 " -c, --config=FILE\tConfig file to load, defaults to weston.ini\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03005053 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005054 " -h, --help\t\tThis help message\n\n");
5055
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005056#if defined(BUILD_DRM_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005057 fprintf(stderr,
5058 "Options for drm-backend.so:\n\n"
5059 " --connector=ID\tBring up only this connector\n"
5060 " --seat=SEAT\t\tThe seat that weston should run on\n"
5061 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02005062 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005063 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005064#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005065
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005066#if defined(BUILD_FBDEV_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005067 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01005068 "Options for fbdev-backend.so:\n\n"
5069 " --tty=TTY\t\tThe tty to use\n"
5070 " --device=DEVICE\tThe framebuffer device to use\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005071#endif
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01005072
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005073#if defined(BUILD_X11_COMPOSITOR)
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01005074 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005075 "Options for x11-backend.so:\n\n"
5076 " --width=WIDTH\t\tWidth of X window\n"
5077 " --height=HEIGHT\tHeight of X window\n"
5078 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05005079 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005080 " --output-count=COUNT\tCreate multiple outputs\n"
5081 " --no-input\t\tDont create input devices\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005082#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005083
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005084#if defined(BUILD_WAYLAND_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005085 fprintf(stderr,
5086 "Options for wayland-backend.so:\n\n"
5087 " --width=WIDTH\t\tWidth of Wayland surface\n"
5088 " --height=HEIGHT\tHeight of Wayland surface\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07005089 " --scale=SCALE\t\tScale factor of output\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06005090 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05005091 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05005092 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05005093 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005094 " --display=DISPLAY\tWayland display to connect to\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005095#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005096
Pekka Paalanene31e0532013-05-22 18:03:07 +03005097#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
5098 fprintf(stderr,
5099 "Options for rpi-backend.so:\n\n"
5100 " --tty=TTY\t\tThe tty to use\n"
5101 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
5102 " --transform=TR\tThe output transformation, TR is one of:\n"
5103 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01005104 " --opaque-regions\tEnable support for opaque regions, can be "
5105 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03005106 "\n");
5107#endif
5108
Hardeningc077a842013-07-08 00:51:35 +02005109#if defined(BUILD_RDP_COMPOSITOR)
Bryce Harrington59fe4232014-10-23 14:44:34 -07005110 fprintf(stderr,
5111 "Options for rdp-backend.so:\n\n"
5112 " --width=WIDTH\t\tWidth of desktop\n"
5113 " --height=HEIGHT\tHeight of desktop\n"
5114 " --env-socket=SOCKET\tUse that socket as peer connection\n"
5115 " --address=ADDR\tThe address to bind\n"
Bryce Harringtonf5b34ae2014-10-23 14:44:35 -07005116 " --port=PORT\t\tThe port to listen on\n"
Bryce Harrington59fe4232014-10-23 14:44:34 -07005117 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
5118 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
5119 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
5120 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
5121 "\n");
Hardeningc077a842013-07-08 00:51:35 +02005122#endif
5123
Bryce Harrington3e3b59e2014-11-19 15:06:19 -08005124#if defined(BUILD_HEADLESS_COMPOSITOR)
5125 fprintf(stderr,
5126 "Options for headless-backend.so:\n\n"
5127 " --width=WIDTH\t\tWidth of memory surface\n"
5128 " --height=HEIGHT\tHeight of memory surface\n"
5129 " --transform=TR\tThe output transformation, TR is one of:\n"
5130 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
5131 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
5132#endif
5133
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005134 exit(error_code);
5135}
5136
Peter Maatmane5b42e42013-03-27 22:38:53 +01005137static void
5138catch_signals(void)
5139{
5140 struct sigaction action;
5141
5142 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
5143 action.sa_sigaction = on_caught_signal;
5144 sigemptyset(&action.sa_mask);
5145 sigaction(SIGSEGV, &action, NULL);
5146 sigaction(SIGABRT, &action, NULL);
5147}
5148
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005149static void
5150handle_primary_client_destroyed(struct wl_listener *listener, void *data)
5151{
5152 struct wl_client *client = data;
5153
5154 weston_log("Primary client died. Closing...\n");
5155
5156 wl_display_terminate(wl_client_get_display(client));
5157}
5158
Ryo Munakatad8deff62014-09-06 07:32:05 +09005159static char *
5160weston_choose_default_backend(void)
5161{
5162 char *backend = NULL;
5163
5164 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
5165 backend = strdup("wayland-backend.so");
5166 else if (getenv("DISPLAY"))
5167 backend = strdup("x11-backend.so");
5168 else
5169 backend = strdup(WESTON_NATIVE_BACKEND);
5170
5171 return backend;
5172}
5173
5174static int
5175weston_create_listening_socket(struct wl_display *display, const char *socket_name)
5176{
5177 if (socket_name) {
5178 if (wl_display_add_socket(display, socket_name)) {
5179 weston_log("fatal: failed to add socket: %m\n");
5180 return -1;
5181 }
5182 } else {
5183 socket_name = wl_display_add_socket_auto(display);
5184 if (!socket_name) {
5185 weston_log("fatal: failed to add socket: %m\n");
5186 return -1;
5187 }
5188 }
5189
5190 setenv("WAYLAND_DISPLAY", socket_name, 1);
5191
5192 return 0;
5193}
5194
Derek Foreman64a3df02014-10-23 12:24:18 -05005195static const struct { const char *name; uint32_t token; } transforms[] = {
5196 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
5197 { "90", WL_OUTPUT_TRANSFORM_90 },
5198 { "180", WL_OUTPUT_TRANSFORM_180 },
5199 { "270", WL_OUTPUT_TRANSFORM_270 },
5200 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
5201 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
5202 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
5203 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
5204};
5205
5206WL_EXPORT int
5207weston_parse_transform(const char *transform, uint32_t *out)
5208{
5209 unsigned int i;
5210
5211 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
5212 if (strcmp(transforms[i].name, transform) == 0) {
5213 *out = transforms[i].token;
5214 return 0;
5215 }
5216
5217 *out = WL_OUTPUT_TRANSFORM_NORMAL;
5218 return -1;
5219}
5220
5221WL_EXPORT const char *
5222weston_transform_to_string(uint32_t output_transform)
5223{
5224 unsigned int i;
5225
5226 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
5227 if (transforms[i].token == output_transform)
5228 return transforms[i].name;
5229
5230 return "<illegal value>";
5231}
5232
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005233static int
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005234load_configuration(struct weston_config **config, int32_t noconfig,
5235 const char *config_file)
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005236{
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005237 const char *file = "weston.ini";
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02005238 const char *full_path;
5239
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005240 *config = NULL;
5241
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005242 if (config_file)
5243 file = config_file;
5244
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005245 if (noconfig == 0)
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005246 *config = weston_config_parse(file);
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005247
5248 if (*config) {
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02005249 full_path = weston_config_get_full_path(*config);
5250
5251 weston_log("Using config file '%s'\n", full_path);
5252 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005253
5254 return 0;
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005255 }
5256
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005257 if (config_file && noconfig == 0) {
5258 weston_log("fatal: error opening or reading config file"
5259 " '%s'.\n", config_file);
5260
5261 return -1;
5262 }
5263
5264 weston_log("Starting with no config file.\n");
5265 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
5266
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005267 return 0;
5268}
Derek Foreman64a3df02014-10-23 12:24:18 -05005269
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005270int main(int argc, char *argv[])
5271{
Pekka Paalanen111c6f92015-03-20 14:35:58 +02005272 int ret = EXIT_FAILURE;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005273 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005274 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005275 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05005276 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005277 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005278 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005279 int *argc, char *argv[],
5280 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005281 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005282 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05005283 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02005284 char *modules = NULL;
5285 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02005286 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005287 char *server_socket = NULL, *end;
Frederic Plourde4a84c832014-10-30 15:06:34 -04005288 int32_t idle_time = -1;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005289 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09005290 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06005291 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03005292 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005293 int32_t numlock_on;
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005294 char *config_file = NULL;
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005295 struct weston_config *config;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005296 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005297 struct wl_client *primary_client;
5298 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005299 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04005300
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005301 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09005302 { WESTON_OPTION_STRING, "backend", 'B', &backend },
5303 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005304 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
5305 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005306 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02005307 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005308 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06005309 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03005310 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005311 { WESTON_OPTION_STRING, "config", 'c', &config_file },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04005312 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05005313
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005314 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005315
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005316 if (help)
5317 usage(EXIT_SUCCESS);
5318
Scott Moreau12245142012-08-29 15:15:58 -06005319 if (version) {
5320 printf(PACKAGE_STRING "\n");
5321 return EXIT_SUCCESS;
5322 }
5323
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02005324 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005325
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03005326 weston_log("%s\n"
5327 STAMP_SPACE "%s\n"
5328 STAMP_SPACE "Bug reports to: %s\n"
5329 STAMP_SPACE "Build: %s\n",
5330 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04005331 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03005332 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04005333
Martin Minarik37032f82012-06-18 20:15:18 +02005334 verify_xdg_runtime_dir();
5335
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005336 display = wl_display_create();
5337
Tiago Vignatti2116b892011-08-08 05:52:59 -07005338 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005339 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
5340 display);
5341 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
5342 display);
5343 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
5344 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07005345
5346 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005347 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
5348 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05005349
Pekka Paalanen111c6f92015-03-20 14:35:58 +02005350 if (!signals[0] || !signals[1] || !signals[2] || !signals[3])
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305351 goto out_signals;
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305352
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005353 if (load_configuration(&config, noconfig, config_file) < 0)
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005354 goto out_signals;
5355
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01005356 section = weston_config_get_section(config, "core", NULL, NULL);
5357
Ryo Munakata03faed22014-09-06 07:32:51 +09005358 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005359 weston_config_section_get_string(section, "backend", &backend,
5360 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09005361 if (!backend)
5362 backend = weston_choose_default_backend();
5363 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005364
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03005365 backend_init = weston_load_module(backend, "backend_init");
Pekka Paalanen111c6f92015-03-20 14:35:58 +02005366 if (!backend_init)
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305367 goto out_signals;
Kristian Høgsberga9410222011-01-14 17:22:35 -05005368
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005369 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05005370 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03005371 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305372 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05005373 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04005374
Peter Maatmane5b42e42013-03-27 22:38:53 +01005375 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04005376 segv_compositor = ec;
5377
Frederic Plourde4a84c832014-10-30 15:06:34 -04005378 if (idle_time < 0)
5379 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
5380 if (idle_time < 0)
5381 idle_time = 300; /* default idle timeout, in seconds */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005382 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005383 ec->default_pointer_grab = NULL;
Frederic Plourdec336f062014-10-29 14:44:33 -04005384 ec->exit_code = EXIT_SUCCESS;
Pekka Paalanen7296e792011-12-07 16:22:00 +02005385
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005386 weston_compositor_log_capabilities(ec);
5387
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005388 server_socket = getenv("WAYLAND_SERVER_SOCKET");
5389 if (server_socket) {
5390 weston_log("Running with single client\n");
5391 fd = strtol(server_socket, &end, 0);
5392 if (*end != '\0')
5393 fd = -1;
5394 } else {
5395 fd = -1;
5396 }
5397
5398 if (fd != -1) {
5399 primary_client = wl_client_create(display, fd);
5400 if (!primary_client) {
5401 weston_log("fatal: failed to add client: %m\n");
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005402 goto out;
5403 }
5404 primary_client_destroyed.notify =
5405 handle_primary_client_destroyed;
5406 wl_client_add_destroy_listener(primary_client,
5407 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09005408 } else if (weston_create_listening_socket(display, socket_name)) {
Ryo Munakatad8deff62014-09-06 07:32:05 +09005409 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005410 }
5411
Ryo Munakata03faed22014-09-06 07:32:51 +09005412 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005413 weston_config_section_get_string(section, "shell", &shell,
5414 "desktop-shell.so");
5415
Ryo Munakata03faed22014-09-06 07:32:51 +09005416 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005417 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005418
5419 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09005420 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005421 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005422
5423 if (load_modules(ec, option_modules, &argc, argv) < 0)
5424 goto out;
5425
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005426 section = weston_config_get_section(config, "keyboard", NULL, NULL);
5427 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
5428 if (numlock_on) {
5429 wl_list_for_each(seat, &ec->seat_list, link) {
5430 if (seat->keyboard)
5431 weston_keyboard_set_locks(seat->keyboard,
5432 WESTON_NUM_LOCK,
5433 WESTON_NUM_LOCK);
5434 }
5435 }
5436
Pekka Paalanen17a1a962015-03-24 15:56:15 +02005437 for (i = 1; i < argc; i++)
5438 weston_log("fatal: unhandled option: %s\n", argv[i]);
5439 if (argc > 1)
5440 goto out;
5441
Pekka Paalanenc0444e32012-01-05 16:28:21 +02005442 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005443
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04005444 wl_display_run(display);
5445
Frederic Plourdec336f062014-10-29 14:44:33 -04005446 /* Allow for setting return exit code after
5447 * wl_display_run returns normally. This is
5448 * useful for devs/testers and automated tests
5449 * that want to indicate failure status to
5450 * testing infrastructure above
5451 */
5452 ret = ec->exit_code;
5453
Ryo Munakata03faed22014-09-06 07:32:51 +09005454out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005455 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005456 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005457
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005458 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005459
Daniel Stone855028f2012-05-01 20:37:10 +01005460 weston_compositor_xkb_destroy(ec);
5461
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005462 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305463
5464out_signals:
5465 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
5466 if (signals[i])
5467 wl_event_source_remove(signals[i]);
5468
Tiago Vignatti9e2be082011-12-19 00:04:46 +02005469 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005470
Martin Minarik19e6f262012-06-07 13:08:46 +02005471 weston_log_file_close();
5472
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005473 free(config_file);
Ryo Munakata03faed22014-09-06 07:32:51 +09005474 free(backend);
5475 free(shell);
5476 free(socket_name);
5477 free(option_modules);
5478 free(log);
5479 free(modules);
5480
Pekka Paalanen3ab72692012-06-08 17:27:28 +03005481 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04005482}