blob: 06474a26ccb1abf1dcb9f75465fecfba0e36dbf5 [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 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07006 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050013 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070014 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050026 */
27
Kristian Høgsberga9410222011-01-14 17:22:35 -050028#include "config.h"
29
Daniel Stoneb7452fe2012-06-01 12:14:06 +010030#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040031#include <stdio.h>
32#include <string.h>
33#include <stdlib.h>
34#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010035#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050036#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020037#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040038#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010039#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040040#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020041#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020042#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020043#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040044#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050045#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040046#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040047#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040048#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050049#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040050#include <sys/time.h>
51#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030052#include <errno.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050053
Marcin Slusarz554a0da2013-02-18 13:27:22 -050054#ifdef HAVE_LIBUNWIND
55#define UNW_LOCAL_ONLY
56#include <libunwind.h>
57#endif
58
Pekka Paalanenb5026542014-11-12 15:09:24 +020059#include "timeline.h"
60
Kristian Høgsberg82863022010-06-04 21:52:02 -040061#include "compositor.h"
Jonny Lamb8ae35902013-11-26 18:19:45 +010062#include "scaler-server-protocol.h"
Pekka Paalanen31f7d782014-09-23 22:08:43 -040063#include "presentation_timing-server-protocol.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070064#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070065#include "shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040066#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050067#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050068
Pekka Paalanen0513a952014-05-21 16:17:27 +030069#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
70
71#define NSEC_PER_SEC 1000000000
72
73static void
74timespec_sub(struct timespec *r,
75 const struct timespec *a, const struct timespec *b)
76{
77 r->tv_sec = a->tv_sec - b->tv_sec;
78 r->tv_nsec = a->tv_nsec - b->tv_nsec;
79 if (r->tv_nsec < 0) {
80 r->tv_sec--;
81 r->tv_nsec += NSEC_PER_SEC;
82 }
83}
84
85static int64_t
86timespec_to_nsec(const struct timespec *a)
87{
88 return (int64_t)a->tv_sec * NSEC_PER_SEC + a->tv_nsec;
89}
90
Kristian Høgsberg27da5382011-06-21 17:32:25 -040091static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040092static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040093
94static int
95sigchld_handler(int signal_number, void *data)
96{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050097 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040098 int status;
99 pid_t pid;
100
Pekka Paalanen23ade622014-08-27 13:31:26 +0300101 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
102 wl_list_for_each(p, &child_process_list, link) {
103 if (p->pid == pid)
104 break;
105 }
Bill Spitzak027b9622012-03-17 15:22:03 -0700106
Pekka Paalanen23ade622014-08-27 13:31:26 +0300107 if (&p->link == &child_process_list) {
108 weston_log("unknown child process exited\n");
109 continue;
110 }
111
112 wl_list_remove(&p->link);
113 p->cleanup(p, status);
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400114 }
115
Pekka Paalanen23ade622014-08-27 13:31:26 +0300116 if (pid < 0 && errno != ECHILD)
117 weston_log("waitpid error %m\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400118
119 return 1;
120}
121
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200122static void
Alexander Larsson0b135062013-05-28 16:23:36 +0200123weston_output_transform_scale_init(struct weston_output *output,
124 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200125
Rob Bradford27b17932013-06-26 18:08:46 +0100126static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500127weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +0100128
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600129static void weston_mode_switch_finish(struct weston_output *output,
130 int mode_changed,
131 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800132{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200133 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +0200134 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200135 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -0600136 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +0200137
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200138 pixman_region32_init(&old_output_region);
139 pixman_region32_copy(&old_output_region, &output->region);
140
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200141 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200142 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200143
144 pixman_region32_init(&output->previous_damage);
145 pixman_region32_init_rect(&output->region, output->x, output->y,
146 output->width, output->height);
147
148 weston_output_update_matrix(output);
149
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200150 /* If a pointer falls outside the outputs new geometry, move it to its
151 * lower-right corner */
152 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400153 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200154 int32_t x, y;
155
156 if (!pointer)
157 continue;
158
159 x = wl_fixed_to_int(pointer->x);
160 y = wl_fixed_to_int(pointer->y);
161
162 if (!pixman_region32_contains_point(&old_output_region,
163 x, y, NULL) ||
164 pixman_region32_contains_point(&output->region,
165 x, y, NULL))
166 continue;
167
168 if (x >= output->x + output->width)
169 x = output->x + output->width - 1;
170 if (y >= output->y + output->height)
171 y = output->y + output->height - 1;
172
173 pointer->x = wl_fixed_from_int(x);
174 pointer->y = wl_fixed_from_int(y);
175 }
176
177 pixman_region32_fini(&old_output_region);
178
Derek Foremandd4cd332014-11-10 10:29:59 -0600179 if (!mode_changed && !scale_changed)
180 return;
181
Hardening57388e42013-09-18 23:56:36 +0200182 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600183 wl_resource_for_each(resource, &output->resource_list) {
184 if (mode_changed) {
185 wl_output_send_mode(resource,
186 output->current_mode->flags,
187 output->current_mode->width,
188 output->current_mode->height,
189 output->current_mode->refresh);
190 }
Hardening57388e42013-09-18 23:56:36 +0200191
Derek Foreman41bdc272014-11-05 13:26:57 -0600192 version = wl_resource_get_version(resource);
193 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600194 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200195
Derek Foreman41bdc272014-11-05 13:26:57 -0600196 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
197 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600198 }
199}
200
201WL_EXPORT int
202weston_output_mode_set_native(struct weston_output *output,
203 struct weston_mode *mode,
204 int32_t scale)
205{
206 int ret;
207 int mode_changed = 0, scale_changed = 0;
208
209 if (!output->switch_mode)
210 return -1;
211
212 if (!output->original_mode) {
213 mode_changed = 1;
214 ret = output->switch_mode(output, mode);
215 if (ret < 0)
216 return ret;
217 if (output->current_scale != scale) {
218 scale_changed = 1;
219 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200220 }
221 }
222
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600223 output->native_mode = mode;
224 output->native_scale = scale;
225
226 weston_mode_switch_finish(output, mode_changed, scale_changed);
227
228 return 0;
229}
230
231WL_EXPORT int
232weston_output_mode_switch_to_native(struct weston_output *output)
233{
234 int ret;
235 int mode_changed = 0, scale_changed = 0;
236
237 if (!output->switch_mode)
238 return -1;
239
240 if (!output->original_mode) {
241 weston_log("already in the native mode\n");
242 return -1;
243 }
244 /* the non fullscreen clients haven't seen a mode set since we
245 * switched into a temporary, so we need to notify them if the
246 * mode at that time is different from the native mode now.
247 */
248 mode_changed = (output->original_mode != output->native_mode);
249 scale_changed = (output->original_scale != output->native_scale);
250
251 ret = output->switch_mode(output, output->native_mode);
252 if (ret < 0)
253 return ret;
254
255 output->current_scale = output->native_scale;
256
257 output->original_mode = NULL;
258 output->original_scale = 0;
259
260 weston_mode_switch_finish(output, mode_changed, scale_changed);
261
262 return 0;
263}
264
265WL_EXPORT int
266weston_output_mode_switch_to_temporary(struct weston_output *output,
267 struct weston_mode *mode,
268 int32_t scale)
269{
270 int ret;
271
272 if (!output->switch_mode)
273 return -1;
274
275 /* original_mode is the last mode non full screen clients have seen,
276 * so we shouldn't change it if we already have one set.
277 */
278 if (!output->original_mode) {
279 output->original_mode = output->native_mode;
280 output->original_scale = output->native_scale;
281 }
282 ret = output->switch_mode(output, mode);
283 if (ret < 0)
284 return ret;
285
286 output->current_scale = scale;
287
288 weston_mode_switch_finish(output, 0, 0);
289
290 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800291}
292
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400293WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500294weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400295{
296 wl_list_insert(&child_process_list, &process->link);
297}
298
Benjamin Franzke06286262011-05-06 19:12:33 +0200299static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200300child_client_exec(int sockfd, const char *path)
301{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500302 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200303 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200304 sigset_t allsigs;
305
306 /* do not give our signal mask to the new process */
307 sigfillset(&allsigs);
308 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200309
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100310 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
311 if (seteuid(getuid()) == -1) {
312 weston_log("compositor: failed seteuid\n");
313 return;
314 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500315
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500316 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
317 * non-CLOEXEC fd to pass through exec. */
318 clientfd = dup(sockfd);
319 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200320 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500321 return;
322 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200323
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500324 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200325 setenv("WAYLAND_SOCKET", s, 1);
326
327 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200328 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200329 path);
330}
331
332WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500333weston_client_launch(struct weston_compositor *compositor,
334 struct weston_process *proc,
335 const char *path,
336 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200337{
338 int sv[2];
339 pid_t pid;
340 struct wl_client *client;
341
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300342 weston_log("launching '%s'\n", path);
343
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300344 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200345 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200346 "socketpair failed while launching '%s': %m\n",
347 path);
348 return NULL;
349 }
350
351 pid = fork();
352 if (pid == -1) {
353 close(sv[0]);
354 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200355 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200356 "fork failed while launching '%s': %m\n", path);
357 return NULL;
358 }
359
360 if (pid == 0) {
361 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700362 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200363 }
364
365 close(sv[1]);
366
367 client = wl_client_create(compositor->wl_display, sv[0]);
368 if (!client) {
369 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200370 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200371 "wl_client_create failed while launching '%s'.\n",
372 path);
373 return NULL;
374 }
375
376 proc->pid = pid;
377 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500378 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200379
380 return client;
381}
382
Pekka Paalanen9c1ac7b2014-08-27 12:03:38 +0300383struct process_info {
384 struct weston_process proc;
385 char *path;
386};
387
388static void
389process_handle_sigchld(struct weston_process *process, int status)
390{
391 struct process_info *pinfo =
392 container_of(process, struct process_info, proc);
393
394 /*
395 * There are no guarantees whether this runs before or after
396 * the wl_client destructor.
397 */
398
399 if (WIFEXITED(status)) {
400 weston_log("%s exited with status %d\n", pinfo->path,
401 WEXITSTATUS(status));
402 } else if (WIFSIGNALED(status)) {
403 weston_log("%s died on signal %d\n", pinfo->path,
404 WTERMSIG(status));
405 } else {
406 weston_log("%s disappeared\n", pinfo->path);
407 }
408
409 free(pinfo->path);
410 free(pinfo);
411}
412
413WL_EXPORT struct wl_client *
414weston_client_start(struct weston_compositor *compositor, const char *path)
415{
416 struct process_info *pinfo;
417 struct wl_client *client;
418
419 pinfo = zalloc(sizeof *pinfo);
420 if (!pinfo)
421 return NULL;
422
423 pinfo->path = strdup(path);
424 if (!pinfo->path)
425 goto out_free;
426
427 client = weston_client_launch(compositor, &pinfo->proc, path,
428 process_handle_sigchld);
429 if (!client)
430 goto out_str;
431
432 return client;
433
434out_str:
435 free(pinfo->path);
436
437out_free:
438 free(pinfo);
439
440 return NULL;
441}
442
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200443static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300444region_init_infinite(pixman_region32_t *region)
445{
446 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
447 UINT32_MAX, UINT32_MAX);
448}
449
Pekka Paalanene67858b2013-04-25 13:57:42 +0300450static struct weston_subsurface *
451weston_surface_to_subsurface(struct weston_surface *surface);
452
Jason Ekstranda7af7042013-10-12 22:38:11 -0500453WL_EXPORT struct weston_view *
454weston_view_create(struct weston_surface *surface)
455{
456 struct weston_view *view;
457
Bryce Harringtonde16d892014-11-20 22:21:57 -0800458 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500459 if (view == NULL)
460 return NULL;
461
462 view->surface = surface;
463
Jason Ekstranda7af7042013-10-12 22:38:11 -0500464 /* Assign to surface */
465 wl_list_insert(&surface->views, &view->surface_link);
466
467 wl_signal_init(&view->destroy_signal);
468 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300469 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500470
Jason Ekstranda7af7042013-10-12 22:38:11 -0500471 pixman_region32_init(&view->clip);
472
473 view->alpha = 1.0;
474 pixman_region32_init(&view->transform.opaque);
475
476 wl_list_init(&view->geometry.transformation_list);
477 wl_list_insert(&view->geometry.transformation_list,
478 &view->transform.position.link);
479 weston_matrix_init(&view->transform.position.matrix);
480 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200481 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500482 pixman_region32_init(&view->transform.boundingbox);
483 view->transform.dirty = 1;
484
Jason Ekstranda7af7042013-10-12 22:38:11 -0500485 return view;
486}
487
Jason Ekstrand108865d2014-06-26 10:04:49 -0700488struct weston_frame_callback {
489 struct wl_resource *resource;
490 struct wl_list link;
491};
492
Pekka Paalanen133e4392014-09-23 22:08:46 -0400493struct weston_presentation_feedback {
494 struct wl_resource *resource;
495
496 /* XXX: could use just wl_resource_get_link() instead */
497 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200498
499 /* The per-surface feedback flags */
500 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400501};
502
503static void
504weston_presentation_feedback_discard(
505 struct weston_presentation_feedback *feedback)
506{
507 presentation_feedback_send_discarded(feedback->resource);
508 wl_resource_destroy(feedback->resource);
509}
510
511static void
512weston_presentation_feedback_discard_list(struct wl_list *list)
513{
514 struct weston_presentation_feedback *feedback, *tmp;
515
516 wl_list_for_each_safe(feedback, tmp, list, link)
517 weston_presentation_feedback_discard(feedback);
518}
519
520static void
521weston_presentation_feedback_present(
522 struct weston_presentation_feedback *feedback,
523 struct weston_output *output,
524 uint32_t refresh_nsec,
525 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200526 uint64_t seq,
527 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400528{
529 struct wl_client *client = wl_resource_get_client(feedback->resource);
530 struct wl_resource *o;
531 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400532
533 wl_resource_for_each(o, &output->resource_list) {
534 if (wl_resource_get_client(o) != client)
535 continue;
536
537 presentation_feedback_send_sync_output(feedback->resource, o);
538 }
539
540 secs = ts->tv_sec;
541 presentation_feedback_send_presented(feedback->resource,
542 secs >> 32, secs & 0xffffffff,
543 ts->tv_nsec,
544 refresh_nsec,
545 seq >> 32, seq & 0xffffffff,
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200546 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400547 wl_resource_destroy(feedback->resource);
548}
549
550static void
551weston_presentation_feedback_present_list(struct wl_list *list,
552 struct weston_output *output,
553 uint32_t refresh_nsec,
554 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200555 uint64_t seq,
556 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400557{
558 struct weston_presentation_feedback *feedback, *tmp;
559
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200560 assert(!(flags & PRESENTATION_FEEDBACK_INVALID) ||
561 wl_list_empty(list));
562
Pekka Paalanen133e4392014-09-23 22:08:46 -0400563 wl_list_for_each_safe(feedback, tmp, list, link)
564 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200565 refresh_nsec, ts, seq,
566 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400567}
568
Jason Ekstrand7b982072014-05-20 14:33:03 -0500569static void
570surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
571{
572 struct weston_surface_state *state =
573 container_of(listener, struct weston_surface_state,
574 buffer_destroy_listener);
575
576 state->buffer = NULL;
577}
578
579static void
580weston_surface_state_init(struct weston_surface_state *state)
581{
582 state->newly_attached = 0;
583 state->buffer = NULL;
584 state->buffer_destroy_listener.notify =
585 surface_state_handle_buffer_destroy;
586 state->sx = 0;
587 state->sy = 0;
588
589 pixman_region32_init(&state->damage);
590 pixman_region32_init(&state->opaque);
591 region_init_infinite(&state->input);
592
593 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400594 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500595
596 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
597 state->buffer_viewport.buffer.scale = 1;
598 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
599 state->buffer_viewport.surface.width = -1;
600 state->buffer_viewport.changed = 0;
601}
602
603static void
604weston_surface_state_fini(struct weston_surface_state *state)
605{
606 struct weston_frame_callback *cb, *next;
607
608 wl_list_for_each_safe(cb, next,
609 &state->frame_callback_list, link)
610 wl_resource_destroy(cb->resource);
611
Pekka Paalanen133e4392014-09-23 22:08:46 -0400612 weston_presentation_feedback_discard_list(&state->feedback_list);
613
Jason Ekstrand7b982072014-05-20 14:33:03 -0500614 pixman_region32_fini(&state->input);
615 pixman_region32_fini(&state->opaque);
616 pixman_region32_fini(&state->damage);
617
618 if (state->buffer)
619 wl_list_remove(&state->buffer_destroy_listener.link);
620 state->buffer = NULL;
621}
622
623static void
624weston_surface_state_set_buffer(struct weston_surface_state *state,
625 struct weston_buffer *buffer)
626{
627 if (state->buffer == buffer)
628 return;
629
630 if (state->buffer)
631 wl_list_remove(&state->buffer_destroy_listener.link);
632 state->buffer = buffer;
633 if (state->buffer)
634 wl_signal_add(&state->buffer->destroy_signal,
635 &state->buffer_destroy_listener);
636}
637
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500638WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500639weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500640{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500641 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400642
Bryce Harringtonde16d892014-11-20 22:21:57 -0800643 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400644 if (surface == NULL)
645 return NULL;
646
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500647 wl_signal_init(&surface->destroy_signal);
648
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500649 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200650 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400651
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200652 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
653 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200654 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
655 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500656
657 weston_surface_state_init(&surface->pending);
658
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400659 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500660 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300661 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400662
Jason Ekstranda7af7042013-10-12 22:38:11 -0500663 wl_list_init(&surface->views);
664
665 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400666 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500667
Pekka Paalanene67858b2013-04-25 13:57:42 +0300668 wl_list_init(&surface->subsurface_list);
669 wl_list_init(&surface->subsurface_list_pending);
670
Jason Ekstrand1e059042014-10-16 10:55:19 -0500671 weston_matrix_init(&surface->buffer_to_surface_matrix);
672 weston_matrix_init(&surface->surface_to_buffer_matrix);
673
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400674 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500675}
676
Alex Wu8811bf92012-02-28 18:07:54 +0800677WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500678weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200679 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500680{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100681 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500682}
683
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400684WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500685weston_view_to_global_float(struct weston_view *view,
686 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200687{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500688 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200689 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
690
Jason Ekstranda7af7042013-10-12 22:38:11 -0500691 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200692
693 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200694 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700695 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200696 v.f[3]);
697 *x = 0;
698 *y = 0;
699 return;
700 }
701
702 *x = v.f[0] / v.f[3];
703 *y = v.f[1] / v.f[3];
704 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500705 *x = sx + view->geometry.x;
706 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200707 }
708}
709
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500710WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200711weston_transformed_coord(int width, int height,
712 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200713 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200714 float sx, float sy, float *bx, float *by)
715{
716 switch (transform) {
717 case WL_OUTPUT_TRANSFORM_NORMAL:
718 default:
719 *bx = sx;
720 *by = sy;
721 break;
722 case WL_OUTPUT_TRANSFORM_FLIPPED:
723 *bx = width - sx;
724 *by = sy;
725 break;
726 case WL_OUTPUT_TRANSFORM_90:
727 *bx = height - sy;
728 *by = sx;
729 break;
730 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
731 *bx = height - sy;
732 *by = width - sx;
733 break;
734 case WL_OUTPUT_TRANSFORM_180:
735 *bx = width - sx;
736 *by = height - sy;
737 break;
738 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
739 *bx = sx;
740 *by = height - sy;
741 break;
742 case WL_OUTPUT_TRANSFORM_270:
743 *bx = sy;
744 *by = width - sx;
745 break;
746 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
747 *bx = sy;
748 *by = sx;
749 break;
750 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200751
752 *bx *= scale;
753 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200754}
755
756WL_EXPORT pixman_box32_t
757weston_transformed_rect(int width, int height,
758 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200759 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200760 pixman_box32_t rect)
761{
762 float x1, x2, y1, y2;
763
764 pixman_box32_t ret;
765
Alexander Larsson4ea95522013-05-22 14:41:37 +0200766 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200767 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200768 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200769 rect.x2, rect.y2, &x2, &y2);
770
771 if (x1 <= x2) {
772 ret.x1 = x1;
773 ret.x2 = x2;
774 } else {
775 ret.x1 = x2;
776 ret.x2 = x1;
777 }
778
779 if (y1 <= y2) {
780 ret.y1 = y1;
781 ret.y2 = y2;
782 } else {
783 ret.y1 = y2;
784 ret.y2 = y1;
785 }
786
787 return ret;
788}
789
790WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500791weston_transformed_region(int width, int height,
792 enum wl_output_transform transform,
793 int32_t scale,
794 pixman_region32_t *src, pixman_region32_t *dest)
795{
796 pixman_box32_t *src_rects, *dest_rects;
797 int nrects, i;
798
799 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
800 if (src != dest)
801 pixman_region32_copy(dest, src);
802 return;
803 }
804
805 src_rects = pixman_region32_rectangles(src, &nrects);
806 dest_rects = malloc(nrects * sizeof(*dest_rects));
807 if (!dest_rects)
808 return;
809
810 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
811 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
812 } else {
813 for (i = 0; i < nrects; i++) {
814 switch (transform) {
815 default:
816 case WL_OUTPUT_TRANSFORM_NORMAL:
817 dest_rects[i].x1 = src_rects[i].x1;
818 dest_rects[i].y1 = src_rects[i].y1;
819 dest_rects[i].x2 = src_rects[i].x2;
820 dest_rects[i].y2 = src_rects[i].y2;
821 break;
822 case WL_OUTPUT_TRANSFORM_90:
823 dest_rects[i].x1 = height - src_rects[i].y2;
824 dest_rects[i].y1 = src_rects[i].x1;
825 dest_rects[i].x2 = height - src_rects[i].y1;
826 dest_rects[i].y2 = src_rects[i].x2;
827 break;
828 case WL_OUTPUT_TRANSFORM_180:
829 dest_rects[i].x1 = width - src_rects[i].x2;
830 dest_rects[i].y1 = height - src_rects[i].y2;
831 dest_rects[i].x2 = width - src_rects[i].x1;
832 dest_rects[i].y2 = height - src_rects[i].y1;
833 break;
834 case WL_OUTPUT_TRANSFORM_270:
835 dest_rects[i].x1 = src_rects[i].y1;
836 dest_rects[i].y1 = width - src_rects[i].x2;
837 dest_rects[i].x2 = src_rects[i].y2;
838 dest_rects[i].y2 = width - src_rects[i].x1;
839 break;
840 case WL_OUTPUT_TRANSFORM_FLIPPED:
841 dest_rects[i].x1 = width - src_rects[i].x2;
842 dest_rects[i].y1 = src_rects[i].y1;
843 dest_rects[i].x2 = width - src_rects[i].x1;
844 dest_rects[i].y2 = src_rects[i].y2;
845 break;
846 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
847 dest_rects[i].x1 = height - src_rects[i].y2;
848 dest_rects[i].y1 = width - src_rects[i].x2;
849 dest_rects[i].x2 = height - src_rects[i].y1;
850 dest_rects[i].y2 = width - src_rects[i].x1;
851 break;
852 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
853 dest_rects[i].x1 = src_rects[i].x1;
854 dest_rects[i].y1 = height - src_rects[i].y2;
855 dest_rects[i].x2 = src_rects[i].x2;
856 dest_rects[i].y2 = height - src_rects[i].y1;
857 break;
858 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
859 dest_rects[i].x1 = src_rects[i].y1;
860 dest_rects[i].y1 = src_rects[i].x1;
861 dest_rects[i].x2 = src_rects[i].y2;
862 dest_rects[i].y2 = src_rects[i].x2;
863 break;
864 }
865 }
866 }
867
868 if (scale != 1) {
869 for (i = 0; i < nrects; i++) {
870 dest_rects[i].x1 *= scale;
871 dest_rects[i].x2 *= scale;
872 dest_rects[i].y1 *= scale;
873 dest_rects[i].y2 *= scale;
874 }
875 }
876
877 pixman_region32_clear(dest);
878 pixman_region32_init_rects(dest, dest_rects, nrects);
879 free(dest_rects);
880}
881
Jonny Lamb74130762013-11-26 18:19:46 +0100882static void
883scaler_surface_to_buffer(struct weston_surface *surface,
884 float sx, float sy, float *bx, float *by)
885{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200886 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200887 double src_width, src_height;
888 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200889
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200890 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
891 if (vp->surface.width == -1) {
892 *bx = sx;
893 *by = sy;
894 return;
895 }
Jonny Lamb74130762013-11-26 18:19:46 +0100896
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200897 src_x = 0.0;
898 src_y = 0.0;
899 src_width = surface->width_from_buffer;
900 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100901 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200902 src_x = wl_fixed_to_double(vp->buffer.src_x);
903 src_y = wl_fixed_to_double(vp->buffer.src_y);
904 src_width = wl_fixed_to_double(vp->buffer.src_width);
905 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100906 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200907
908 *bx = sx * src_width / surface->width + src_x;
909 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100910}
911
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500912WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200913weston_surface_to_buffer_float(struct weston_surface *surface,
914 float sx, float sy, float *bx, float *by)
915{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200916 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
917
Jonny Lamb74130762013-11-26 18:19:46 +0100918 /* first transform coordinates if the scaler is set */
919 scaler_surface_to_buffer(surface, sx, sy, bx, by);
920
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500921 weston_transformed_coord(surface->width_from_buffer,
922 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200923 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100924 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200925}
926
Alexander Larsson4ea95522013-05-22 14:41:37 +0200927WL_EXPORT void
928weston_surface_to_buffer(struct weston_surface *surface,
929 int sx, int sy, int *bx, int *by)
930{
931 float bxf, byf;
932
Jonny Lamb74130762013-11-26 18:19:46 +0100933 weston_surface_to_buffer_float(surface,
934 sx, sy, &bxf, &byf);
935
Alexander Larsson4ea95522013-05-22 14:41:37 +0200936 *bx = floorf(bxf);
937 *by = floorf(byf);
938}
939
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200940WL_EXPORT pixman_box32_t
941weston_surface_to_buffer_rect(struct weston_surface *surface,
942 pixman_box32_t rect)
943{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200944 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100945 float xf, yf;
946
947 /* first transform box coordinates if the scaler is set */
948 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
949 rect.x1 = floorf(xf);
950 rect.y1 = floorf(yf);
951
952 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
953 rect.x2 = floorf(xf);
954 rect.y2 = floorf(yf);
955
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500956 return weston_transformed_rect(surface->width_from_buffer,
957 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200958 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200959 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200960}
961
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200962/** Transform a region from surface coordinates to buffer coordinates
963 *
964 * \param surface The surface to fetch wl_viewport and buffer transformation
965 * from.
966 * \param surface_region[in] The region in surface coordinates.
967 * \param buffer_region[out] The region converted to buffer coordinates.
968 *
969 * Buffer_region must be init'd, but will be completely overwritten.
970 *
971 * Viewport and buffer transformations can only do translation, scaling,
972 * and rotations in 90-degree steps. Therefore the only loss in the
973 * conversion is coordinate flooring (rounding).
974 */
975WL_EXPORT void
976weston_surface_to_buffer_region(struct weston_surface *surface,
977 pixman_region32_t *surface_region,
978 pixman_region32_t *buffer_region)
979{
980 pixman_box32_t *src_rects, *dest_rects;
981 int nrects, i;
982
983 src_rects = pixman_region32_rectangles(surface_region, &nrects);
984 dest_rects = malloc(nrects * sizeof(*dest_rects));
985 if (!dest_rects)
986 return;
987
988 for (i = 0; i < nrects; i++) {
989 dest_rects[i] = weston_surface_to_buffer_rect(surface,
990 src_rects[i]);
991 }
992
993 pixman_region32_fini(buffer_region);
994 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
995 free(dest_rects);
996}
997
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200998WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500999weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001000 struct weston_plane *plane)
1001{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001002 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001003 return;
1004
Jason Ekstranda7af7042013-10-12 22:38:11 -05001005 weston_view_damage_below(view);
1006 view->plane = plane;
1007 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001008}
1009
Pekka Paalanen51723d52015-02-17 13:10:01 +02001010/** Inflict damage on the plane where the view is visible.
1011 *
1012 * \param view The view that causes the damage.
1013 *
1014 * If the view is currently on a plane (including the primary plane),
1015 * take the view's boundingbox, subtract all the opaque views that cover it,
1016 * and add the remaining region as damage to the plane. This corresponds
1017 * to the damage inflicted to the plane if this view disappeared.
1018 *
1019 * A repaint is scheduled for this view.
1020 *
1021 * The region of all opaque views covering this view is stored in
1022 * weston_view::clip and updated by view_accumulate_damage() during
1023 * weston_output_repaint(). Specifically, that region matches the
1024 * scenegraph as it was last painted.
1025 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001026WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001027weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001028{
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001029 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001030
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001031 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001032 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001033 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +08001034 if (view->plane)
1035 pixman_region32_union(&view->plane->damage,
1036 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001037 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001038 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001039}
1040
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001041static void
1042weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1043{
1044 uint32_t different = es->output_mask ^ mask;
1045 uint32_t entered = mask & different;
1046 uint32_t left = es->output_mask & different;
1047 struct weston_output *output;
1048 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001049 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001050
1051 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001052 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001053 return;
1054 if (different == 0)
1055 return;
1056
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001057 client = wl_resource_get_client(es->resource);
1058
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001059 wl_list_for_each(output, &es->compositor->output_list, link) {
1060 if (1 << output->id & different)
1061 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001062 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001063 client);
1064 if (resource == NULL)
1065 continue;
1066 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001067 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001068 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001069 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001070 }
1071}
1072
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001073
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001074static void
1075weston_surface_assign_output(struct weston_surface *es)
1076{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001077 struct weston_output *new_output;
1078 struct weston_view *view;
1079 pixman_region32_t region;
1080 uint32_t max, area, mask;
1081 pixman_box32_t *e;
1082
1083 new_output = NULL;
1084 max = 0;
1085 mask = 0;
1086 pixman_region32_init(&region);
1087 wl_list_for_each(view, &es->views, surface_link) {
1088 if (!view->output)
1089 continue;
1090
1091 pixman_region32_intersect(&region, &view->transform.boundingbox,
1092 &view->output->region);
1093
1094 e = pixman_region32_extents(&region);
1095 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1096
1097 mask |= view->output_mask;
1098
1099 if (area >= max) {
1100 new_output = view->output;
1101 max = area;
1102 }
1103 }
1104 pixman_region32_fini(&region);
1105
1106 es->output = new_output;
1107 weston_surface_update_output_mask(es, mask);
1108}
1109
1110static void
1111weston_view_assign_output(struct weston_view *ev)
1112{
1113 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001114 struct weston_output *output, *new_output;
1115 pixman_region32_t region;
1116 uint32_t max, area, mask;
1117 pixman_box32_t *e;
1118
1119 new_output = NULL;
1120 max = 0;
1121 mask = 0;
1122 pixman_region32_init(&region);
1123 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001124 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001125 &output->region);
1126
1127 e = pixman_region32_extents(&region);
1128 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1129
1130 if (area > 0)
1131 mask |= 1 << output->id;
1132
1133 if (area >= max) {
1134 new_output = output;
1135 max = area;
1136 }
1137 }
1138 pixman_region32_fini(&region);
1139
Jason Ekstranda7af7042013-10-12 22:38:11 -05001140 ev->output = new_output;
1141 ev->output_mask = mask;
1142
1143 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001144}
1145
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001146static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001147weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1148 int from_x, int from_y, int *to_x, int *to_y)
1149{
1150 float x, y;
1151
1152 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1153 weston_view_from_global_float(to, x, y, &x, &y);
1154
1155 *to_x = round(x);
1156 *to_y = round(y);
1157}
1158
1159static void
1160weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1161{
1162 pixman_box32_t *a;
1163 pixman_box32_t b;
1164
1165 a = pixman_region32_extents(&from->geometry.scissor);
1166
1167 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1168 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1169
1170 pixman_region32_fini(&to->geometry.scissor);
1171 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1172}
1173
1174static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001175view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001176 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001177{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001178 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1179 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001180 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001181 { inbox->x1, inbox->y1 },
1182 { inbox->x1, inbox->y2 },
1183 { inbox->x2, inbox->y1 },
1184 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001185 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001186 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001187 int i;
1188
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001189 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001190 /* avoid rounding empty bbox to 1x1 */
1191 pixman_region32_init(bbox);
1192 return;
1193 }
1194
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001195 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001196 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001197 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001198 if (x < min_x)
1199 min_x = x;
1200 if (x > max_x)
1201 max_x = x;
1202 if (y < min_y)
1203 min_y = y;
1204 if (y > max_y)
1205 max_y = y;
1206 }
1207
Pekka Paalanen219b9822012-02-08 15:38:37 +02001208 int_x = floorf(min_x);
1209 int_y = floorf(min_y);
1210 pixman_region32_init_rect(bbox, int_x, int_y,
1211 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001212}
1213
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001214static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001215weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001216{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001217 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001218
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001219 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001220 view->geometry.x = roundf(view->geometry.x);
1221 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001222
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001223 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001224 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1225 view->transform.position.matrix.d[12] = view->geometry.x;
1226 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001227
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001229
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230 view->transform.inverse = view->transform.position.matrix;
1231 view->transform.inverse.d[12] = -view->geometry.x;
1232 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001233
Jason Ekstranda7af7042013-10-12 22:38:11 -05001234 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001235 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001236 view->surface->width,
1237 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001238 if (view->geometry.scissor_enabled)
1239 pixman_region32_intersect(&view->transform.boundingbox,
1240 &view->transform.boundingbox,
1241 &view->geometry.scissor);
1242
1243 pixman_region32_translate(&view->transform.boundingbox,
1244 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001245
Jason Ekstranda7af7042013-10-12 22:38:11 -05001246 if (view->alpha == 1.0) {
1247 pixman_region32_copy(&view->transform.opaque,
1248 &view->surface->opaque);
1249 pixman_region32_translate(&view->transform.opaque,
1250 view->geometry.x,
1251 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001252 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001253}
1254
1255static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001257{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001258 struct weston_view *parent = view->geometry.parent;
1259 struct weston_matrix *matrix = &view->transform.matrix;
1260 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001261 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001262 pixman_region32_t surfregion;
1263 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001264
Jason Ekstranda7af7042013-10-12 22:38:11 -05001265 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001266
1267 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1269 view->transform.position.matrix.d[12] = view->geometry.x;
1270 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001271
1272 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001273 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001274 weston_matrix_multiply(matrix, &tform->matrix);
1275
Pekka Paalanen483243f2013-03-08 14:56:50 +02001276 if (parent)
1277 weston_matrix_multiply(matrix, &parent->transform.matrix);
1278
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001279 if (weston_matrix_invert(inverse, matrix) < 0) {
1280 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001281 weston_log("error: weston_view %p"
1282 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001283 return -1;
1284 }
1285
Pekka Paalanen380adf52015-02-16 14:39:11 +02001286 pixman_region32_init_rect(&surfregion, 0, 0,
1287 view->surface->width, view->surface->height);
1288 if (view->geometry.scissor_enabled)
1289 pixman_region32_intersect(&surfregion, &surfregion,
1290 &view->geometry.scissor);
1291 surfbox = pixman_region32_extents(&surfregion);
1292
1293 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1294 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001295
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001296 return 0;
1297}
1298
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001299static struct weston_layer *
1300get_view_layer(struct weston_view *view)
1301{
1302 if (view->parent_view)
1303 return get_view_layer(view->parent_view);
1304 return view->layer_link.layer;
1305}
1306
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001307WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001309{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001311 struct weston_layer *layer;
1312 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001313
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001315 return;
1316
Pekka Paalanen483243f2013-03-08 14:56:50 +02001317 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001318 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001319
Jason Ekstranda7af7042013-10-12 22:38:11 -05001320 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001321
Jason Ekstranda7af7042013-10-12 22:38:11 -05001322 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001323
Jason Ekstranda7af7042013-10-12 22:38:11 -05001324 pixman_region32_fini(&view->transform.boundingbox);
1325 pixman_region32_fini(&view->transform.opaque);
1326 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001327
Pekka Paalanencd403622012-01-25 13:37:39 +02001328 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001329 if (view->geometry.transformation_list.next ==
1330 &view->transform.position.link &&
1331 view->geometry.transformation_list.prev ==
1332 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001333 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001334 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001335 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001336 if (weston_view_update_transform_enable(view) < 0)
1337 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001338 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001339
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001340 layer = get_view_layer(view);
1341 if (layer) {
1342 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001343 pixman_region32_intersect(&view->transform.boundingbox,
1344 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001345 pixman_region32_intersect(&view->transform.opaque,
1346 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001347 pixman_region32_fini(&mask);
1348 }
1349
Pekka Paalanen380adf52015-02-16 14:39:11 +02001350 if (parent) {
1351 if (parent->geometry.scissor_enabled) {
1352 view->geometry.scissor_enabled = true;
1353 weston_view_transfer_scissor(parent, view);
1354 } else {
1355 view->geometry.scissor_enabled = false;
1356 }
1357 }
1358
Jason Ekstranda7af7042013-10-12 22:38:11 -05001359 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001360
Jason Ekstranda7af7042013-10-12 22:38:11 -05001361 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001362
Jason Ekstranda7af7042013-10-12 22:38:11 -05001363 wl_signal_emit(&view->surface->compositor->transform_signal,
1364 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001365}
1366
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001367WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001368weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001369{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001370 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001371
1372 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001373 * The invariant: if view->geometry.dirty, then all views
1374 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001375 * Corollary: if not parent->geometry.dirty, then all ancestors
1376 * are not dirty.
1377 */
1378
Jason Ekstranda7af7042013-10-12 22:38:11 -05001379 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001380 return;
1381
Jason Ekstranda7af7042013-10-12 22:38:11 -05001382 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001383
Jason Ekstranda7af7042013-10-12 22:38:11 -05001384 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001385 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001386 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001387}
1388
1389WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001390weston_view_to_global_fixed(struct weston_view *view,
1391 wl_fixed_t vx, wl_fixed_t vy,
1392 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001393{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001394 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001395
Jason Ekstranda7af7042013-10-12 22:38:11 -05001396 weston_view_to_global_float(view,
1397 wl_fixed_to_double(vx),
1398 wl_fixed_to_double(vy),
1399 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001400 *x = wl_fixed_from_double(xf);
1401 *y = wl_fixed_from_double(yf);
1402}
1403
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001404WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001405weston_view_from_global_float(struct weston_view *view,
1406 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001407{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001408 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001409 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1410
Jason Ekstranda7af7042013-10-12 22:38:11 -05001411 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001412
1413 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001414 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001415 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001416 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001417 *vx = 0;
1418 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001419 return;
1420 }
1421
Jason Ekstranda7af7042013-10-12 22:38:11 -05001422 *vx = v.f[0] / v.f[3];
1423 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001424 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001425 *vx = x - view->geometry.x;
1426 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001427 }
1428}
1429
1430WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001431weston_view_from_global_fixed(struct weston_view *view,
1432 wl_fixed_t x, wl_fixed_t y,
1433 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001434{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001435 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001436
Jason Ekstranda7af7042013-10-12 22:38:11 -05001437 weston_view_from_global_float(view,
1438 wl_fixed_to_double(x),
1439 wl_fixed_to_double(y),
1440 &vxf, &vyf);
1441 *vx = wl_fixed_from_double(vxf);
1442 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001443}
1444
1445WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001446weston_view_from_global(struct weston_view *view,
1447 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001448{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001449 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001450
Jason Ekstranda7af7042013-10-12 22:38:11 -05001451 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1452 *vx = floorf(vxf);
1453 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001454}
1455
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001456WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001457weston_surface_schedule_repaint(struct weston_surface *surface)
1458{
1459 struct weston_output *output;
1460
1461 wl_list_for_each(output, &surface->compositor->output_list, link)
1462 if (surface->output_mask & (1 << output->id))
1463 weston_output_schedule_repaint(output);
1464}
1465
1466WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001467weston_view_schedule_repaint(struct weston_view *view)
1468{
1469 struct weston_output *output;
1470
1471 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1472 if (view->output_mask & (1 << output->id))
1473 weston_output_schedule_repaint(output);
1474}
1475
Pekka Paalanene508ce62015-02-19 13:59:55 +02001476/**
1477 * XXX: This function does it the wrong way.
1478 * surface->damage is the damage from the client, and causes
1479 * surface_flush_damage() to copy pixels. No window management action can
1480 * cause damage to the client-provided content, warranting re-upload!
1481 *
1482 * Instead of surface->damage, this function should record the damage
1483 * with all the views for this surface to avoid extraneous texture
1484 * uploads.
1485 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001486WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001487weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001488{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001489 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001490 0, 0, surface->width,
1491 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001492
Kristian Høgsberg98238702012-08-03 16:29:12 -04001493 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001494}
1495
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001496WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001497weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001498{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001499 if (view->geometry.x == x && view->geometry.y == y)
1500 return;
1501
Jason Ekstranda7af7042013-10-12 22:38:11 -05001502 view->geometry.x = x;
1503 view->geometry.y = y;
1504 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001505}
1506
Pekka Paalanen483243f2013-03-08 14:56:50 +02001507static void
1508transform_parent_handle_parent_destroy(struct wl_listener *listener,
1509 void *data)
1510{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001511 struct weston_view *view =
1512 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001513 geometry.parent_destroy_listener);
1514
Jason Ekstranda7af7042013-10-12 22:38:11 -05001515 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001516}
1517
1518WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001519weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001520 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001521{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001522 if (view->geometry.parent) {
1523 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1524 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001525
1526 if (!parent)
1527 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001528 }
1529
Jason Ekstranda7af7042013-10-12 22:38:11 -05001530 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001531
Jason Ekstranda7af7042013-10-12 22:38:11 -05001532 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001533 transform_parent_handle_parent_destroy;
1534 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001535 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001536 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001537 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001538 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001539 }
1540
Jason Ekstranda7af7042013-10-12 22:38:11 -05001541 weston_view_geometry_dirty(view);
1542}
1543
Pekka Paalanen380adf52015-02-16 14:39:11 +02001544/** Set a clip mask rectangle on a view
1545 *
1546 * \param view The view to set the clip mask on.
1547 * \param x Top-left corner X coordinate of the clip rectangle.
1548 * \param y Top-left corner Y coordinate of the clip rectangle.
1549 * \param width Width of the clip rectangle, non-negative.
1550 * \param height Height of the clip rectangle, non-negative.
1551 *
1552 * A shell may set a clip mask rectangle on a view. Everything outside
1553 * the rectangle is cut away for input and output purposes: it is
1554 * not drawn and cannot be hit by hit-test based input like pointer
1555 * motion or touch-downs. Everything inside the rectangle will behave
1556 * normally. Clients are unaware of clipping.
1557 *
1558 * The rectangle is set in the surface local coordinates. Setting a clip
1559 * mask rectangle does not affect the view position, the view is positioned
1560 * as it would be without a clip. The clip also does not change
1561 * weston_surface::width,height.
1562 *
1563 * The clip mask rectangle is part of transformation inheritance
1564 * (weston_view_set_transform_parent()). A clip set in the root of the
1565 * transformation inheritance tree will affect all views in the tree.
1566 * A clip can be set only on the root view. Attempting to set a clip
1567 * on view that has a transformation parent will fail. Assigning a parent
1568 * to a view that has a clip set will cause the clip to be forgotten.
1569 *
1570 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1571 * on the additional transformations in the child views. These transformations
1572 * may not rotate the coordinate axes, i.e., only translation and scaling
1573 * are allowed. Violating this restriction causes the clipping to malfunction.
1574 * Furthermore, using scaling may cause rounding errors in child clipping.
1575 *
1576 * The clip mask rectangle is not automatically adjusted based on
1577 * wl_surface.attach dx and dy arguments.
1578 *
1579 * A clip mask rectangle can be set only if the compositor capability
1580 * WESTON_CAP_VIEW_CLIP_MASK is present.
1581 *
1582 * This function sets the clip mask rectangle and schedules a repaint for
1583 * the view.
1584 */
1585WL_EXPORT void
1586weston_view_set_mask(struct weston_view *view,
1587 int x, int y, int width, int height)
1588{
1589 struct weston_compositor *compositor = view->surface->compositor;
1590
1591 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1592 weston_log("%s not allowed without capability!\n", __func__);
1593 return;
1594 }
1595
1596 if (view->geometry.parent) {
1597 weston_log("view %p has a parent, clip forbidden!\n", view);
1598 return;
1599 }
1600
1601 if (width < 0 || height < 0) {
1602 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1603 x, y, width, height);
1604 return;
1605 }
1606
1607 pixman_region32_fini(&view->geometry.scissor);
1608 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1609 view->geometry.scissor_enabled = true;
1610 weston_view_geometry_dirty(view);
1611 weston_view_schedule_repaint(view);
1612}
1613
1614/** Remove the clip mask from a view
1615 *
1616 * \param view The view to remove the clip mask from.
1617 *
1618 * Removed the clip mask rectangle and schedules a repaint.
1619 *
1620 * \sa weston_view_set_mask
1621 */
1622WL_EXPORT void
1623weston_view_set_mask_infinite(struct weston_view *view)
1624{
1625 view->geometry.scissor_enabled = false;
1626 weston_view_geometry_dirty(view);
1627 weston_view_schedule_repaint(view);
1628}
1629
Derek Foreman280e7dd2014-10-03 13:13:42 -05001630WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001631weston_view_is_mapped(struct weston_view *view)
1632{
1633 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001634 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001635 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001636 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001637}
1638
Derek Foreman280e7dd2014-10-03 13:13:42 -05001639WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001640weston_surface_is_mapped(struct weston_surface *surface)
1641{
1642 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001643 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001644 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001645 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001646}
1647
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001648static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001649surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001650{
1651 struct weston_view *view;
1652
1653 if (surface->width == width && surface->height == height)
1654 return;
1655
1656 surface->width = width;
1657 surface->height = height;
1658
1659 wl_list_for_each(view, &surface->views, surface_link)
1660 weston_view_geometry_dirty(view);
1661}
1662
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001663WL_EXPORT void
1664weston_surface_set_size(struct weston_surface *surface,
1665 int32_t width, int32_t height)
1666{
1667 assert(!surface->resource);
1668 surface_set_size(surface, width, height);
1669}
1670
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001671static int
1672fixed_round_up_to_int(wl_fixed_t f)
1673{
1674 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1675}
1676
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001677static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001678weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001679{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001680 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001681 int32_t width, height;
1682
1683 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001684 surface->width_from_buffer = 0;
1685 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001686 return;
1687 }
1688
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001689 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001690 case WL_OUTPUT_TRANSFORM_90:
1691 case WL_OUTPUT_TRANSFORM_270:
1692 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1693 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001694 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1695 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001696 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001697 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001698 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1699 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001700 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001701 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001702
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001703 surface->width_from_buffer = width;
1704 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001705}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001706
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001707static void
1708weston_surface_update_size(struct weston_surface *surface)
1709{
1710 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1711 int32_t width, height;
1712
1713 width = surface->width_from_buffer;
1714 height = surface->height_from_buffer;
1715
1716 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001717 surface_set_size(surface,
1718 vp->surface.width, vp->surface.height);
1719 return;
1720 }
1721
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001722 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001723 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1724 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1725
1726 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001727 return;
1728 }
1729
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001730 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001731}
1732
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001733WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001734weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001735{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001736 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001737
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001738 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001739
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001740 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001741}
1742
Jason Ekstranda7af7042013-10-12 22:38:11 -05001743WL_EXPORT struct weston_view *
1744weston_compositor_pick_view(struct weston_compositor *compositor,
1745 wl_fixed_t x, wl_fixed_t y,
1746 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001747{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001748 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001749 wl_fixed_t view_x, view_y;
1750 int view_ix, view_iy;
1751 int ix = wl_fixed_to_int(x);
1752 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001753
Jason Ekstranda7af7042013-10-12 22:38:11 -05001754 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001755 if (!pixman_region32_contains_point(
1756 &view->transform.boundingbox, ix, iy, NULL))
1757 continue;
1758
1759 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1760 view_ix = wl_fixed_to_int(view_x);
1761 view_iy = wl_fixed_to_int(view_y);
1762
1763 if (!pixman_region32_contains_point(&view->surface->input,
1764 view_ix, view_iy, NULL))
1765 continue;
1766
Pekka Paalanen380adf52015-02-16 14:39:11 +02001767 if (view->geometry.scissor_enabled &&
1768 !pixman_region32_contains_point(&view->geometry.scissor,
1769 view_ix, view_iy, NULL))
1770 continue;
1771
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001772 *vx = view_x;
1773 *vy = view_y;
1774 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001775 }
1776
1777 return NULL;
1778}
1779
1780static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001781weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001782{
Daniel Stone37816df2012-05-16 18:45:18 +01001783 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001784
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001785 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001786 return;
1787
Daniel Stone37816df2012-05-16 18:45:18 +01001788 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001789 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001790}
1791
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001792WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001793weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001794{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001795 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001796
Jason Ekstranda7af7042013-10-12 22:38:11 -05001797 if (!weston_view_is_mapped(view))
1798 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001799
Jason Ekstranda7af7042013-10-12 22:38:11 -05001800 weston_view_damage_below(view);
1801 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001802 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001803 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001804 wl_list_remove(&view->link);
1805 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001806 view->output_mask = 0;
1807 weston_surface_assign_output(view->surface);
1808
1809 if (weston_surface_is_mapped(view->surface))
1810 return;
1811
1812 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1813 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001814 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001815 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001816 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001817 NULL,
1818 wl_fixed_from_int(0),
1819 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001820 if (seat->touch && seat->touch->focus == view)
Derek Foreman4c93c082015-04-30 16:45:41 -05001821 weston_touch_set_focus(seat->touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001822 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001823}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001824
Jason Ekstranda7af7042013-10-12 22:38:11 -05001825WL_EXPORT void
1826weston_surface_unmap(struct weston_surface *surface)
1827{
1828 struct weston_view *view;
1829
1830 wl_list_for_each(view, &surface->views, surface_link)
1831 weston_view_unmap(view);
1832 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001833}
1834
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001835static void
1836weston_surface_reset_pending_buffer(struct weston_surface *surface)
1837{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001838 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001839 surface->pending.sx = 0;
1840 surface->pending.sy = 0;
1841 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001842 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001843}
1844
Jason Ekstranda7af7042013-10-12 22:38:11 -05001845WL_EXPORT void
1846weston_view_destroy(struct weston_view *view)
1847{
1848 wl_signal_emit(&view->destroy_signal, view);
1849
1850 assert(wl_list_empty(&view->geometry.child_list));
1851
1852 if (weston_view_is_mapped(view)) {
1853 weston_view_unmap(view);
1854 weston_compositor_build_view_list(view->surface->compositor);
1855 }
1856
1857 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001858 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001859
1860 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001861 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001862 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001863 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001864
1865 weston_view_set_transform_parent(view, NULL);
1866
Jason Ekstranda7af7042013-10-12 22:38:11 -05001867 wl_list_remove(&view->surface_link);
1868
1869 free(view);
1870}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001871
1872WL_EXPORT void
1873weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001874{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001875 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001876 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001877
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001878 if (--surface->ref_count > 0)
1879 return;
1880
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001881 assert(surface->resource == NULL);
1882
Pekka Paalanenca790762015-04-17 14:23:38 +03001883 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001884
Pekka Paalanene67858b2013-04-25 13:57:42 +03001885 assert(wl_list_empty(&surface->subsurface_list_pending));
1886 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001887
Jason Ekstranda7af7042013-10-12 22:38:11 -05001888 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1889 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001890
Jason Ekstrand7b982072014-05-20 14:33:03 -05001891 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001892
Pekka Paalanende685b82012-12-04 15:58:12 +02001893 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001894
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001895 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001896 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001897 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001898
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001899 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001900 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001901
Pekka Paalanen133e4392014-09-23 22:08:46 -04001902 weston_presentation_feedback_discard_list(&surface->feedback_list);
1903
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001904 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001905}
1906
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001907static void
1908destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001909{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001910 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001911
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001912 assert(surface);
1913
Giulio Camuffo0d379742013-11-15 22:06:15 +01001914 /* Set the resource to NULL, since we don't want to leave a
1915 * dangling pointer if the surface was refcounted and survives
1916 * the weston_surface_destroy() call. */
1917 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001918 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001919}
1920
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001921static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001922weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1923{
1924 struct weston_buffer *buffer =
1925 container_of(listener, struct weston_buffer, destroy_listener);
1926
1927 wl_signal_emit(&buffer->destroy_signal, buffer);
1928 free(buffer);
1929}
1930
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001931WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001932weston_buffer_from_resource(struct wl_resource *resource)
1933{
1934 struct weston_buffer *buffer;
1935 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001936
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001937 listener = wl_resource_get_destroy_listener(resource,
1938 weston_buffer_destroy_handler);
1939
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001940 if (listener)
1941 return container_of(listener, struct weston_buffer,
1942 destroy_listener);
1943
1944 buffer = zalloc(sizeof *buffer);
1945 if (buffer == NULL)
1946 return NULL;
1947
1948 buffer->resource = resource;
1949 wl_signal_init(&buffer->destroy_signal);
1950 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001951 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001952 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001953
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001954 return buffer;
1955}
1956
1957static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001958weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1959 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001960{
Pekka Paalanende685b82012-12-04 15:58:12 +02001961 struct weston_buffer_reference *ref =
1962 container_of(listener, struct weston_buffer_reference,
1963 destroy_listener);
1964
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001965 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001966 ref->buffer = NULL;
1967}
1968
1969WL_EXPORT void
1970weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001971 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001972{
1973 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001974 ref->buffer->busy_count--;
1975 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001976 assert(wl_resource_get_client(ref->buffer->resource));
1977 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001978 WL_BUFFER_RELEASE);
1979 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001980 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001981 }
1982
Pekka Paalanende685b82012-12-04 15:58:12 +02001983 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001984 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001985 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001986 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001987 }
1988
Pekka Paalanende685b82012-12-04 15:58:12 +02001989 ref->buffer = buffer;
1990 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1991}
1992
1993static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001994weston_surface_attach(struct weston_surface *surface,
1995 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001996{
1997 weston_buffer_reference(&surface->buffer_ref, buffer);
1998
Pekka Paalanena6421c42012-12-04 15:58:10 +02001999 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002000 if (weston_surface_is_mapped(surface))
2001 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002002 }
2003
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002004 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002005
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002006 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002007 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002008}
2009
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002010WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002011weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002012{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002013 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002014
2015 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002016 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002017}
2018
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002019WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002020weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002021{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002022 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002023
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002024 pixman_region32_union(&compositor->primary_plane.damage,
2025 &compositor->primary_plane.damage,
2026 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002027 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002028}
2029
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002030static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002031surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002032{
Pekka Paalanende685b82012-12-04 15:58:12 +02002033 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002034 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002035 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002036
Pekka Paalanenb5026542014-11-12 15:09:24 +02002037 if (weston_timeline_enabled_ &&
2038 pixman_region32_not_empty(&surface->damage))
2039 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2040 TLP_OUTPUT(surface->output), TLP_END);
2041
Jason Ekstrandef540082014-06-26 10:37:36 -07002042 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002043}
2044
2045static void
2046view_accumulate_damage(struct weston_view *view,
2047 pixman_region32_t *opaque)
2048{
2049 pixman_region32_t damage;
2050
2051 pixman_region32_init(&damage);
2052 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002053 pixman_box32_t *extents;
2054
Jason Ekstranda7af7042013-10-12 22:38:11 -05002055 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002056 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002057 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002058 pixman_region32_copy(&damage, &view->surface->damage);
2059 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002060 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002061 }
2062
Pekka Paalanen380adf52015-02-16 14:39:11 +02002063 pixman_region32_intersect(&damage, &damage,
2064 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002065 pixman_region32_subtract(&damage, &damage, opaque);
2066 pixman_region32_union(&view->plane->damage,
2067 &view->plane->damage, &damage);
2068 pixman_region32_fini(&damage);
2069 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002070 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002071}
2072
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002073static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002074compositor_accumulate_damage(struct weston_compositor *ec)
2075{
2076 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002077 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002078 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002079
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002080 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002081
2082 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002083 pixman_region32_copy(&plane->clip, &clip);
2084
2085 pixman_region32_init(&opaque);
2086
Jason Ekstranda7af7042013-10-12 22:38:11 -05002087 wl_list_for_each(ev, &ec->view_list, link) {
2088 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002089 continue;
2090
Jason Ekstranda7af7042013-10-12 22:38:11 -05002091 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002092 }
2093
2094 pixman_region32_union(&clip, &clip, &opaque);
2095 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002096 }
2097
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002098 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002099
Jason Ekstranda7af7042013-10-12 22:38:11 -05002100 wl_list_for_each(ev, &ec->view_list, link)
2101 ev->surface->touched = 0;
2102
2103 wl_list_for_each(ev, &ec->view_list, link) {
2104 if (ev->surface->touched)
2105 continue;
2106 ev->surface->touched = 1;
2107
2108 surface_flush_damage(ev->surface);
2109
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002110 /* Both the renderer and the backend have seen the buffer
2111 * by now. If renderer needs the buffer, it has its own
2112 * reference set. If the backend wants to keep the buffer
2113 * around for migrating the surface into a non-primary plane
2114 * later, keep_buffer is true. Otherwise, drop the core
2115 * reference now, and allow early buffer release. This enables
2116 * clients to use single-buffering.
2117 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002118 if (!ev->surface->keep_buffer)
2119 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002120 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002121}
2122
2123static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002124surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002125{
2126 struct weston_subsurface *sub;
2127
Pekka Paalanene67858b2013-04-25 13:57:42 +03002128 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002129 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002130 continue;
2131
Jason Ekstranda7af7042013-10-12 22:38:11 -05002132 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2133 wl_list_init(&sub->surface->views);
2134
2135 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002136 }
2137}
2138
2139static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002140surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002141{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002142 struct weston_subsurface *sub;
2143 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002144
Jason Ekstranda7af7042013-10-12 22:38:11 -05002145 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2146 if (sub->surface == surface)
2147 continue;
2148
George Kiagiadakised04d382014-06-13 18:10:26 +02002149 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2150 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002151 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002152 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002153
2154 surface_free_unused_subsurface_views(sub->surface);
2155 }
2156}
2157
2158static void
2159view_list_add_subsurface_view(struct weston_compositor *compositor,
2160 struct weston_subsurface *sub,
2161 struct weston_view *parent)
2162{
2163 struct weston_subsurface *child;
2164 struct weston_view *view = NULL, *iv;
2165
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002166 if (!weston_surface_is_mapped(sub->surface))
2167 return;
2168
Jason Ekstranda7af7042013-10-12 22:38:11 -05002169 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2170 if (iv->geometry.parent == parent) {
2171 view = iv;
2172 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002173 }
2174 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002175
2176 if (view) {
2177 /* Put it back in the surface's list of views */
2178 wl_list_remove(&view->surface_link);
2179 wl_list_insert(&sub->surface->views, &view->surface_link);
2180 } else {
2181 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002182 weston_view_set_position(view,
2183 sub->position.x,
2184 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002185 weston_view_set_transform_parent(view, parent);
2186 }
2187
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002188 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002189 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002190
Pekka Paalanenb188e912013-11-19 14:03:35 +02002191 if (wl_list_empty(&sub->surface->subsurface_list)) {
2192 wl_list_insert(compositor->view_list.prev, &view->link);
2193 return;
2194 }
2195
2196 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2197 if (child->surface == sub->surface)
2198 wl_list_insert(compositor->view_list.prev, &view->link);
2199 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002200 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002201 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002202}
2203
2204static void
2205view_list_add(struct weston_compositor *compositor,
2206 struct weston_view *view)
2207{
2208 struct weston_subsurface *sub;
2209
2210 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002211
Pekka Paalanenb188e912013-11-19 14:03:35 +02002212 if (wl_list_empty(&view->surface->subsurface_list)) {
2213 wl_list_insert(compositor->view_list.prev, &view->link);
2214 return;
2215 }
2216
2217 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2218 if (sub->surface == view->surface)
2219 wl_list_insert(compositor->view_list.prev, &view->link);
2220 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002221 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002222 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002223}
2224
2225static void
2226weston_compositor_build_view_list(struct weston_compositor *compositor)
2227{
2228 struct weston_view *view;
2229 struct weston_layer *layer;
2230
2231 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002232 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002233 surface_stash_subsurface_views(view->surface);
2234
2235 wl_list_init(&compositor->view_list);
2236 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002237 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002238 view_list_add(compositor, view);
2239 }
2240 }
2241
2242 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002243 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002244 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002245}
2246
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002247static void
2248weston_output_take_feedback_list(struct weston_output *output,
2249 struct weston_surface *surface)
2250{
2251 struct weston_view *view;
2252 struct weston_presentation_feedback *feedback;
2253 uint32_t flags = 0xffffffff;
2254
2255 if (wl_list_empty(&surface->feedback_list))
2256 return;
2257
2258 /* All views must have the flag for the flag to survive. */
2259 wl_list_for_each(view, &surface->views, surface_link) {
2260 /* ignore views that are not on this output at all */
2261 if (view->output_mask & (1u << output->id))
2262 flags &= view->psf_flags;
2263 }
2264
2265 wl_list_for_each(feedback, &surface->feedback_list, link)
2266 feedback->psf_flags = flags;
2267
2268 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2269 wl_list_init(&surface->feedback_list);
2270}
2271
David Herrmann1edf44c2013-10-22 17:11:26 +02002272static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002273weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002274{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002275 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002276 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002277 struct weston_animation *animation, *next;
2278 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002279 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002280 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002281 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002282
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002283 if (output->destroying)
2284 return 0;
2285
Pekka Paalanenb5026542014-11-12 15:09:24 +02002286 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2287
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002288 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002289 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002290
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002291 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002292 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002293 } else {
2294 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002295 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002296 ev->psf_flags = 0;
2297 }
2298 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002299
Pekka Paalanene67858b2013-04-25 13:57:42 +03002300 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002301 wl_list_for_each(ev, &ec->view_list, link) {
2302 /* Note: This operation is safe to do multiple times on the
2303 * same surface.
2304 */
2305 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002306 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002307 &ev->surface->frame_callback_list);
2308 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002309
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002310 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002311 }
2312 }
2313
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002314 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002315
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002316 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002317 pixman_region32_intersect(&output_damage,
2318 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002319 pixman_region32_subtract(&output_damage,
2320 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002321
Scott Moreauccbf29d2012-02-22 14:21:41 -07002322 if (output->dirty)
2323 weston_output_update_matrix(output);
2324
David Herrmann1edf44c2013-10-22 17:11:26 +02002325 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002326
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002327 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002328
Kristian Høgsbergef044142011-06-21 15:02:12 -04002329 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002330
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002331 weston_compositor_repick(ec);
2332 wl_event_loop_dispatch(ec->input_loop, 0);
2333
Jonas Ådahldb773762012-06-13 00:01:21 +02002334 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002335 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002336 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002337 }
2338
Scott Moreaud64cf212012-06-08 19:40:54 -06002339 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002340 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002341 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002342 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002343
Pekka Paalanenb5026542014-11-12 15:09:24 +02002344 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2345
David Herrmann1edf44c2013-10-22 17:11:26 +02002346 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002347}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002348
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002349static int
2350weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002351{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002352 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002353
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002354 wl_event_loop_dispatch(compositor->input_loop, 0);
2355
2356 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002357}
2358
Pekka Paalanen82919792014-05-21 13:51:49 +03002359static void
2360weston_output_schedule_repaint_reset(struct weston_output *output)
2361{
2362 struct weston_compositor *compositor = output->compositor;
2363 struct wl_event_loop *loop;
2364 int fd;
2365
2366 output->repaint_scheduled = 0;
2367 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2368
2369 if (compositor->input_loop_source)
2370 return;
2371
2372 loop = wl_display_get_event_loop(compositor->wl_display);
2373 fd = wl_event_loop_get_fd(compositor->input_loop);
2374 compositor->input_loop_source =
2375 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2376 weston_compositor_read_input, compositor);
2377}
2378
Pekka Paalanen0513a952014-05-21 16:17:27 +03002379static int
2380output_repaint_timer_handler(void *data)
2381{
2382 struct weston_output *output = data;
2383 struct weston_compositor *compositor = output->compositor;
2384
2385 if (output->repaint_needed &&
2386 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2387 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2388 weston_output_repaint(output) == 0)
2389 return 0;
2390
2391 weston_output_schedule_repaint_reset(output);
2392
2393 return 0;
2394}
2395
Kristian Høgsbergef044142011-06-21 15:02:12 -04002396WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002397weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002398 const struct timespec *stamp,
2399 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002400{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002401 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002402 int32_t refresh_nsec;
2403 struct timespec now;
2404 struct timespec gone;
2405 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002406
Pekka Paalanenb5026542014-11-12 15:09:24 +02002407 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2408 TLP_VBLANK(stamp), TLP_END);
2409
Pekka Paalanen0513a952014-05-21 16:17:27 +03002410 refresh_nsec = 1000000000000LL / output->current_mode->refresh;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002411 weston_presentation_feedback_present_list(&output->feedback_list,
2412 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002413 output->msc,
2414 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002415
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002416 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002417
Pekka Paalanen0513a952014-05-21 16:17:27 +03002418 weston_compositor_read_presentation_clock(compositor, &now);
2419 timespec_sub(&gone, &now, stamp);
2420 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2421 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002422
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002423 if (msec < -1000 || msec > 1000) {
2424 static bool warned;
2425
2426 if (!warned)
2427 weston_log("Warning: computed repaint delay is "
2428 "insane: %d msec\n", msec);
2429 warned = true;
2430
2431 msec = 0;
2432 }
2433
2434 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002435 output_repaint_timer_handler(output);
2436 else
2437 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002438}
2439
2440static void
2441idle_repaint(void *data)
2442{
2443 struct weston_output *output = data;
2444
Jonas Ådahle5a12252013-04-05 23:07:11 +02002445 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002446}
2447
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002448WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002449weston_layer_entry_insert(struct weston_layer_entry *list,
2450 struct weston_layer_entry *entry)
2451{
2452 wl_list_insert(&list->link, &entry->link);
2453 entry->layer = list->layer;
2454}
2455
2456WL_EXPORT void
2457weston_layer_entry_remove(struct weston_layer_entry *entry)
2458{
2459 wl_list_remove(&entry->link);
2460 wl_list_init(&entry->link);
2461 entry->layer = NULL;
2462}
2463
2464WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002465weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2466{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002467 wl_list_init(&layer->view_list.link);
2468 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002469 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002470 if (below != NULL)
2471 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002472}
2473
2474WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002475weston_layer_set_mask(struct weston_layer *layer,
2476 int x, int y, int width, int height)
2477{
2478 struct weston_view *view;
2479
2480 layer->mask.x1 = x;
2481 layer->mask.x2 = x + width;
2482 layer->mask.y1 = y;
2483 layer->mask.y2 = y + height;
2484
2485 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2486 weston_view_geometry_dirty(view);
2487 }
2488}
2489
2490WL_EXPORT void
2491weston_layer_set_mask_infinite(struct weston_layer *layer)
2492{
2493 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2494 UINT32_MAX, UINT32_MAX);
2495}
2496
2497WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002498weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002499{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002500 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002501 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002502
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002503 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2504 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002505 return;
2506
Pekka Paalanenb5026542014-11-12 15:09:24 +02002507 if (!output->repaint_needed)
2508 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2509
Kristian Høgsbergef044142011-06-21 15:02:12 -04002510 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002511 output->repaint_needed = 1;
2512 if (output->repaint_scheduled)
2513 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002514
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002515 wl_event_loop_add_idle(loop, idle_repaint, output);
2516 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002517 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2518
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002519
2520 if (compositor->input_loop_source) {
2521 wl_event_source_remove(compositor->input_loop_source);
2522 compositor->input_loop_source = NULL;
2523 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002524}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002525
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002526WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002527weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2528{
2529 struct weston_output *output;
2530
2531 wl_list_for_each(output, &compositor->output_list, link)
2532 weston_output_schedule_repaint(output);
2533}
2534
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002535static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002536surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002537{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002538 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002539}
2540
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002541static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002542surface_attach(struct wl_client *client,
2543 struct wl_resource *resource,
2544 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2545{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002546 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002547 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002548
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002549 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002550 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002551 if (buffer == NULL) {
2552 wl_client_post_no_memory(client);
2553 return;
2554 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002555 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002556
Pekka Paalanende685b82012-12-04 15:58:12 +02002557 /* Attach, attach, without commit in between does not send
2558 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002559 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002560
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002561 surface->pending.sx = sx;
2562 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002563 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002564}
2565
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002566static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002567surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002568 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002569 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002570{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002571 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002572
Pekka Paalanen8e159182012-10-10 12:49:25 +03002573 pixman_region32_union_rect(&surface->pending.damage,
2574 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002575 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002576}
2577
Kristian Høgsberg33418202011-08-16 23:01:28 -04002578static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002579destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002580{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002581 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002582
2583 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002584 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002585}
2586
2587static void
2588surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002589 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002590{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002591 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002592 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002593
2594 cb = malloc(sizeof *cb);
2595 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002596 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002597 return;
2598 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002599
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002600 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2601 callback);
2602 if (cb->resource == NULL) {
2603 free(cb);
2604 wl_resource_post_no_memory(resource);
2605 return;
2606 }
2607
Jason Ekstranda85118c2013-06-27 20:17:02 -05002608 wl_resource_set_implementation(cb->resource, NULL, cb,
2609 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002610
Pekka Paalanenbc106382012-10-10 12:49:31 +03002611 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002612}
2613
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002614static void
2615surface_set_opaque_region(struct wl_client *client,
2616 struct wl_resource *resource,
2617 struct wl_resource *region_resource)
2618{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002619 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002620 struct weston_region *region;
2621
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002622 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002623 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002624 pixman_region32_copy(&surface->pending.opaque,
2625 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002626 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002627 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002628 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002629}
2630
2631static void
2632surface_set_input_region(struct wl_client *client,
2633 struct wl_resource *resource,
2634 struct wl_resource *region_resource)
2635{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002636 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002637 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002638
2639 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002640 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002641 pixman_region32_copy(&surface->pending.input,
2642 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002643 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002644 pixman_region32_fini(&surface->pending.input);
2645 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002646 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002647}
2648
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002649static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002650weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002651{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002652 struct weston_subsurface *sub;
2653
2654 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2655 parent_link_pending) {
2656 wl_list_remove(&sub->parent_link);
2657 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2658 }
2659}
2660
2661static void
Jason Ekstrand1e059042014-10-16 10:55:19 -05002662weston_surface_build_buffer_matrix(struct weston_surface *surface,
2663 struct weston_matrix *matrix)
2664{
2665 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2666 double src_width, src_height, dest_width, dest_height;
2667
2668 weston_matrix_init(matrix);
2669
2670 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2671 src_width = surface->width_from_buffer;
2672 src_height = surface->height_from_buffer;
2673 } else {
2674 src_width = wl_fixed_to_double(vp->buffer.src_width);
2675 src_height = wl_fixed_to_double(vp->buffer.src_height);
2676 }
2677
2678 if (vp->surface.width == -1) {
2679 dest_width = src_width;
2680 dest_height = src_height;
2681 } else {
2682 dest_width = vp->surface.width;
2683 dest_height = vp->surface.height;
2684 }
2685
2686 if (src_width != dest_width || src_height != dest_height)
2687 weston_matrix_scale(matrix,
2688 src_width / dest_width,
2689 src_height / dest_height, 1);
2690
2691 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2692 weston_matrix_translate(matrix,
2693 wl_fixed_to_double(vp->buffer.src_x),
2694 wl_fixed_to_double(vp->buffer.src_y),
2695 0);
2696
2697 switch (vp->buffer.transform) {
2698 case WL_OUTPUT_TRANSFORM_FLIPPED:
2699 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2700 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2701 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2702 weston_matrix_scale(matrix, -1, 1, 1);
2703 weston_matrix_translate(matrix,
2704 surface->width_from_buffer, 0, 0);
2705 break;
2706 }
2707
2708 switch (vp->buffer.transform) {
2709 default:
2710 case WL_OUTPUT_TRANSFORM_NORMAL:
2711 case WL_OUTPUT_TRANSFORM_FLIPPED:
2712 break;
2713 case WL_OUTPUT_TRANSFORM_90:
2714 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2715 weston_matrix_rotate_xy(matrix, 0, 1);
2716 weston_matrix_translate(matrix,
2717 surface->height_from_buffer, 0, 0);
2718 break;
2719 case WL_OUTPUT_TRANSFORM_180:
2720 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2721 weston_matrix_rotate_xy(matrix, -1, 0);
2722 weston_matrix_translate(matrix,
2723 surface->width_from_buffer,
2724 surface->height_from_buffer, 0);
2725 break;
2726 case WL_OUTPUT_TRANSFORM_270:
2727 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2728 weston_matrix_rotate_xy(matrix, 0, -1);
2729 weston_matrix_translate(matrix,
2730 0, surface->width_from_buffer, 0);
2731 break;
2732 }
2733
2734 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2735}
2736
2737static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002738weston_surface_commit_state(struct weston_surface *surface,
2739 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002740{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002741 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002742 pixman_region32_t opaque;
2743
Alexander Larsson4ea95522013-05-22 14:41:37 +02002744 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002745 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002746 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002747 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002748
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002749 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002750 if (state->newly_attached)
2751 weston_surface_attach(surface, state->buffer);
2752 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002753
Jason Ekstrand1e059042014-10-16 10:55:19 -05002754 weston_surface_build_buffer_matrix(surface,
2755 &surface->surface_to_buffer_matrix);
2756 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2757 &surface->surface_to_buffer_matrix);
2758
Jason Ekstrand7b982072014-05-20 14:33:03 -05002759 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002760 weston_surface_update_size(surface);
2761 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002762 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002763 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002764
Jason Ekstrand7b982072014-05-20 14:33:03 -05002765 state->sx = 0;
2766 state->sy = 0;
2767 state->newly_attached = 0;
2768 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002769
2770 /* wl_surface.damage */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002771 if (weston_timeline_enabled_ &&
2772 pixman_region32_not_empty(&state->damage))
2773 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002774 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002775 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002776 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002777 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002778 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002779
2780 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002781 pixman_region32_init(&opaque);
2782 pixman_region32_intersect_rect(&opaque, &state->opaque,
2783 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002784
2785 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2786 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002787 wl_list_for_each(view, &surface->views, surface_link)
2788 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002789 }
2790
2791 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002792
2793 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002794 pixman_region32_intersect_rect(&surface->input, &state->input,
2795 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002796
Pekka Paalanenbc106382012-10-10 12:49:31 +03002797 /* wl_surface.frame */
2798 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002799 &state->frame_callback_list);
2800 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002801
2802 /* XXX:
2803 * What should happen with a feedback request, if there
2804 * is no wl_buffer attached for this commit?
2805 */
2806
2807 /* presentation.feedback */
2808 wl_list_insert_list(&surface->feedback_list,
2809 &state->feedback_list);
2810 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002811}
2812
2813static void
2814weston_surface_commit(struct weston_surface *surface)
2815{
2816 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002817
Pekka Paalanene67858b2013-04-25 13:57:42 +03002818 weston_surface_commit_subsurface_order(surface);
2819
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002820 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002821}
2822
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002823static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002824weston_subsurface_commit(struct weston_subsurface *sub);
2825
2826static void
2827weston_subsurface_parent_commit(struct weston_subsurface *sub,
2828 int parent_is_synchronized);
2829
2830static void
2831surface_commit(struct wl_client *client, struct wl_resource *resource)
2832{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002833 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002834 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2835
2836 if (sub) {
2837 weston_subsurface_commit(sub);
2838 return;
2839 }
2840
2841 weston_surface_commit(surface);
2842
2843 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2844 if (sub->surface != surface)
2845 weston_subsurface_parent_commit(sub, 0);
2846 }
2847}
2848
2849static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002850surface_set_buffer_transform(struct wl_client *client,
2851 struct wl_resource *resource, int transform)
2852{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002853 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002854
Jonny Lamba55f1392014-05-30 12:07:15 +02002855 /* if wl_output.transform grows more members this will need to be updated. */
2856 if (transform < 0 ||
2857 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2858 wl_resource_post_error(resource,
2859 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2860 "buffer transform must be a valid transform "
2861 "('%d' specified)", transform);
2862 return;
2863 }
2864
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002865 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002866 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002867}
2868
Alexander Larsson4ea95522013-05-22 14:41:37 +02002869static void
2870surface_set_buffer_scale(struct wl_client *client,
2871 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002872 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002873{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002874 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002875
Jonny Lamba55f1392014-05-30 12:07:15 +02002876 if (scale < 1) {
2877 wl_resource_post_error(resource,
2878 WL_SURFACE_ERROR_INVALID_SCALE,
2879 "buffer scale must be at least one "
2880 "('%d' specified)", scale);
2881 return;
2882 }
2883
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002884 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002885 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002886}
2887
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002888static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002889 surface_destroy,
2890 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002891 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002892 surface_frame,
2893 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002894 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002895 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002896 surface_set_buffer_transform,
2897 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002898};
2899
2900static void
2901compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002902 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002903{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002904 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002905 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002906
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002907 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002908 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002909 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002910 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002911 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002912
Jason Ekstranda85118c2013-06-27 20:17:02 -05002913 surface->resource =
2914 wl_resource_create(client, &wl_surface_interface,
2915 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002916 if (surface->resource == NULL) {
2917 weston_surface_destroy(surface);
2918 wl_resource_post_no_memory(resource);
2919 return;
2920 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002921 wl_resource_set_implementation(surface->resource, &surface_interface,
2922 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002923
2924 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002925}
2926
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002927static void
2928destroy_region(struct wl_resource *resource)
2929{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002930 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002931
2932 pixman_region32_fini(&region->region);
2933 free(region);
2934}
2935
2936static void
2937region_destroy(struct wl_client *client, struct wl_resource *resource)
2938{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002939 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002940}
2941
2942static void
2943region_add(struct wl_client *client, struct wl_resource *resource,
2944 int32_t x, int32_t y, int32_t width, int32_t height)
2945{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002946 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002947
2948 pixman_region32_union_rect(&region->region, &region->region,
2949 x, y, width, height);
2950}
2951
2952static void
2953region_subtract(struct wl_client *client, struct wl_resource *resource,
2954 int32_t x, int32_t y, int32_t width, int32_t height)
2955{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002956 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002957 pixman_region32_t rect;
2958
2959 pixman_region32_init_rect(&rect, x, y, width, height);
2960 pixman_region32_subtract(&region->region, &region->region, &rect);
2961 pixman_region32_fini(&rect);
2962}
2963
2964static const struct wl_region_interface region_interface = {
2965 region_destroy,
2966 region_add,
2967 region_subtract
2968};
2969
2970static void
2971compositor_create_region(struct wl_client *client,
2972 struct wl_resource *resource, uint32_t id)
2973{
2974 struct weston_region *region;
2975
2976 region = malloc(sizeof *region);
2977 if (region == NULL) {
2978 wl_resource_post_no_memory(resource);
2979 return;
2980 }
2981
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002982 pixman_region32_init(&region->region);
2983
Jason Ekstranda85118c2013-06-27 20:17:02 -05002984 region->resource =
2985 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002986 if (region->resource == NULL) {
2987 free(region);
2988 wl_resource_post_no_memory(resource);
2989 return;
2990 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002991 wl_resource_set_implementation(region->resource, &region_interface,
2992 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002993}
2994
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002995static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002996 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002997 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002998};
2999
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003000static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003001weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3002{
3003 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003004
Jason Ekstrand7b982072014-05-20 14:33:03 -05003005 weston_surface_commit_state(surface, &sub->cached);
3006 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003007
3008 weston_surface_commit_subsurface_order(surface);
3009
3010 weston_surface_schedule_repaint(surface);
3011
Jason Ekstrand7b982072014-05-20 14:33:03 -05003012 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003013}
3014
3015static void
3016weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3017{
3018 struct weston_surface *surface = sub->surface;
3019
3020 /*
3021 * If this commit would cause the surface to move by the
3022 * attach(dx, dy) parameters, the old damage region must be
3023 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02003024 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003025 */
3026 pixman_region32_translate(&sub->cached.damage,
3027 -surface->pending.sx, -surface->pending.sy);
3028 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
3029 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07003030 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003031
3032 if (surface->pending.newly_attached) {
3033 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003034 weston_surface_state_set_buffer(&sub->cached,
3035 surface->pending.buffer);
3036 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003037 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003038 weston_presentation_feedback_discard_list(
3039 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003040 }
3041 sub->cached.sx += surface->pending.sx;
3042 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003043
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003044 sub->cached.buffer_viewport.changed |=
3045 surface->pending.buffer_viewport.changed;
3046 sub->cached.buffer_viewport.buffer =
3047 surface->pending.buffer_viewport.buffer;
3048 sub->cached.buffer_viewport.surface =
3049 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003050
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003051 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003052
3053 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3054
3055 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3056
3057 wl_list_insert_list(&sub->cached.frame_callback_list,
3058 &surface->pending.frame_callback_list);
3059 wl_list_init(&surface->pending.frame_callback_list);
3060
Pekka Paalanen133e4392014-09-23 22:08:46 -04003061 wl_list_insert_list(&sub->cached.feedback_list,
3062 &surface->pending.feedback_list);
3063 wl_list_init(&surface->pending.feedback_list);
3064
Jason Ekstrand7b982072014-05-20 14:33:03 -05003065 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003066}
3067
Derek Foreman280e7dd2014-10-03 13:13:42 -05003068static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003069weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3070{
3071 while (sub) {
3072 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003073 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003074
3075 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003076 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003077
3078 sub = weston_surface_to_subsurface(sub->parent);
3079 }
3080
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003081 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003082}
3083
3084static void
3085weston_subsurface_commit(struct weston_subsurface *sub)
3086{
3087 struct weston_surface *surface = sub->surface;
3088 struct weston_subsurface *tmp;
3089
3090 /* Recursive check for effectively synchronized. */
3091 if (weston_subsurface_is_synchronized(sub)) {
3092 weston_subsurface_commit_to_cache(sub);
3093 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003094 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003095 /* flush accumulated state from cache */
3096 weston_subsurface_commit_to_cache(sub);
3097 weston_subsurface_commit_from_cache(sub);
3098 } else {
3099 weston_surface_commit(surface);
3100 }
3101
3102 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3103 if (tmp->surface != surface)
3104 weston_subsurface_parent_commit(tmp, 0);
3105 }
3106 }
3107}
3108
3109static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003110weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003111{
3112 struct weston_surface *surface = sub->surface;
3113 struct weston_subsurface *tmp;
3114
Pekka Paalanene67858b2013-04-25 13:57:42 +03003115 /* From now on, commit_from_cache the whole sub-tree, regardless of
3116 * the synchronized mode of each child. This sub-surface or some
3117 * of its ancestors were synchronized, so we are synchronized
3118 * all the way down.
3119 */
3120
Jason Ekstrand7b982072014-05-20 14:33:03 -05003121 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003122 weston_subsurface_commit_from_cache(sub);
3123
3124 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3125 if (tmp->surface != surface)
3126 weston_subsurface_parent_commit(tmp, 1);
3127 }
3128}
3129
3130static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003131weston_subsurface_parent_commit(struct weston_subsurface *sub,
3132 int parent_is_synchronized)
3133{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003134 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003135 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003136 wl_list_for_each(view, &sub->surface->views, surface_link)
3137 weston_view_set_position(view,
3138 sub->position.x,
3139 sub->position.y);
3140
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003141 sub->position.set = 0;
3142 }
3143
3144 if (parent_is_synchronized || sub->synchronized)
3145 weston_subsurface_synchronized_commit(sub);
3146}
3147
Pekka Paalanen8274d902014-08-06 19:36:51 +03003148static int
3149subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3150{
3151 return snprintf(buf, len, "sub-surface");
3152}
3153
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003154static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003155subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003156{
3157 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003158 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003159
Jason Ekstranda7af7042013-10-12 22:38:11 -05003160 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003161 weston_view_set_position(view,
3162 view->geometry.x + dx,
3163 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003164
3165 /* No need to check parent mappedness, because if parent is not
3166 * mapped, parent is not in a visible layer, so this sub-surface
3167 * will not be drawn either.
3168 */
3169 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003170 struct weston_output *output;
3171
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003172 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003173 * because that would call it also for the parent surface,
3174 * which might not be mapped yet. That would lead to
3175 * inconsistent state, where the window could never be
3176 * mapped.
3177 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003178 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003179 * weston_surface_is_mapped() return true, so that when the
3180 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003181 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003182 */
3183 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003184 output = container_of(compositor->output_list.next,
3185 struct weston_output, link);
3186
3187 surface->output = output;
3188 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003189 }
3190}
3191
3192static struct weston_subsurface *
3193weston_surface_to_subsurface(struct weston_surface *surface)
3194{
3195 if (surface->configure == subsurface_configure)
3196 return surface->configure_private;
3197
3198 return NULL;
3199}
3200
Pekka Paalanen01388e22013-04-25 13:57:44 +03003201WL_EXPORT struct weston_surface *
3202weston_surface_get_main_surface(struct weston_surface *surface)
3203{
3204 struct weston_subsurface *sub;
3205
3206 while (surface && (sub = weston_surface_to_subsurface(surface)))
3207 surface = sub->parent;
3208
3209 return surface;
3210}
3211
Pekka Paalanen50b67472014-10-01 15:02:41 +03003212WL_EXPORT int
3213weston_surface_set_role(struct weston_surface *surface,
3214 const char *role_name,
3215 struct wl_resource *error_resource,
3216 uint32_t error_code)
3217{
3218 assert(role_name);
3219
3220 if (surface->role_name == NULL ||
3221 surface->role_name == role_name ||
3222 strcmp(surface->role_name, role_name) == 0) {
3223 surface->role_name = role_name;
3224
3225 return 0;
3226 }
3227
3228 wl_resource_post_error(error_resource, error_code,
3229 "Cannot assign role %s to wl_surface@%d,"
3230 " already has role %s\n",
3231 role_name,
3232 wl_resource_get_id(surface->resource),
3233 surface->role_name);
3234 return -1;
3235}
3236
Pekka Paalanen8274d902014-08-06 19:36:51 +03003237WL_EXPORT void
3238weston_surface_set_label_func(struct weston_surface *surface,
3239 int (*desc)(struct weston_surface *,
3240 char *, size_t))
3241{
3242 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003243 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003244}
3245
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003246/** Get the size of surface contents
3247 *
3248 * \param surface The surface to query.
3249 * \param width Returns the width of raw contents.
3250 * \param height Returns the height of raw contents.
3251 *
3252 * Retrieves the raw surface content size in pixels for the given surface.
3253 * This is the whole content size in buffer pixels. If the surface
3254 * has no content or the renderer does not implement this feature,
3255 * zeroes are returned.
3256 *
3257 * This function is used to determine the buffer size needed for
3258 * a weston_surface_copy_content() call.
3259 */
3260WL_EXPORT void
3261weston_surface_get_content_size(struct weston_surface *surface,
3262 int *width, int *height)
3263{
3264 struct weston_renderer *rer = surface->compositor->renderer;
3265
3266 if (!rer->surface_get_content_size) {
3267 *width = 0;
3268 *height = 0;
3269 return;
3270 }
3271
3272 rer->surface_get_content_size(surface, width, height);
3273}
3274
3275/** Copy surface contents to system memory.
3276 *
3277 * \param surface The surface to copy from.
3278 * \param target Pointer to the target memory buffer.
3279 * \param size Size of the target buffer in bytes.
3280 * \param src_x X location on contents to copy from.
3281 * \param src_y Y location on contents to copy from.
3282 * \param width Width in pixels of the area to copy.
3283 * \param height Height in pixels of the area to copy.
3284 * \return 0 for success, -1 for failure.
3285 *
3286 * Surface contents are maintained by the renderer. They can be in a
3287 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3288 * else.
3289 *
3290 * Surface contents are copied into memory pointed to by target,
3291 * which has size bytes of space available. The target memory
3292 * may be larger than needed, but being smaller returns an error.
3293 * The extra bytes in target may or may not be written; their content is
3294 * unspecified. Size must be large enough to hold the image.
3295 *
3296 * The image in the target memory will be arranged in rows from
3297 * top to bottom, and pixels on a row from left to right. The pixel
3298 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3299 * width * 4.
3300 *
3301 * Parameters src_x and src_y define the upper-left corner in buffer
3302 * coordinates (pixels) to copy from. Parameters width and height
3303 * define the size of the area to copy in pixels.
3304 *
3305 * The rectangle defined by src_x, src_y, width, height must fit in
3306 * the surface contents. Otherwise an error is returned.
3307 *
3308 * Use surface_get_data_size to determine the content size; the
3309 * needed target buffer size and rectangle limits.
3310 *
3311 * CURRENT IMPLEMENTATION RESTRICTIONS:
3312 * - the machine must be little-endian due to Pixman formats.
3313 *
3314 * NOTE: Pixman formats are premultiplied.
3315 */
3316WL_EXPORT int
3317weston_surface_copy_content(struct weston_surface *surface,
3318 void *target, size_t size,
3319 int src_x, int src_y,
3320 int width, int height)
3321{
3322 struct weston_renderer *rer = surface->compositor->renderer;
3323 int cw, ch;
3324 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3325
3326 if (!rer->surface_copy_content)
3327 return -1;
3328
3329 weston_surface_get_content_size(surface, &cw, &ch);
3330
3331 if (src_x < 0 || src_y < 0)
3332 return -1;
3333
3334 if (width <= 0 || height <= 0)
3335 return -1;
3336
3337 if (src_x + width > cw || src_y + height > ch)
3338 return -1;
3339
3340 if (width * bytespp * height > size)
3341 return -1;
3342
3343 return rer->surface_copy_content(surface, target, size,
3344 src_x, src_y, width, height);
3345}
3346
Pekka Paalanene67858b2013-04-25 13:57:42 +03003347static void
3348subsurface_set_position(struct wl_client *client,
3349 struct wl_resource *resource, int32_t x, int32_t y)
3350{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003351 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003352
3353 if (!sub)
3354 return;
3355
3356 sub->position.x = x;
3357 sub->position.y = y;
3358 sub->position.set = 1;
3359}
3360
3361static struct weston_subsurface *
3362subsurface_from_surface(struct weston_surface *surface)
3363{
3364 struct weston_subsurface *sub;
3365
3366 sub = weston_surface_to_subsurface(surface);
3367 if (sub)
3368 return sub;
3369
3370 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3371 if (sub->surface == surface)
3372 return sub;
3373
3374 return NULL;
3375}
3376
3377static struct weston_subsurface *
3378subsurface_sibling_check(struct weston_subsurface *sub,
3379 struct weston_surface *surface,
3380 const char *request)
3381{
3382 struct weston_subsurface *sibling;
3383
3384 sibling = subsurface_from_surface(surface);
3385
3386 if (!sibling) {
3387 wl_resource_post_error(sub->resource,
3388 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3389 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003390 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003391 return NULL;
3392 }
3393
3394 if (sibling->parent != sub->parent) {
3395 wl_resource_post_error(sub->resource,
3396 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3397 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003398 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003399 return NULL;
3400 }
3401
3402 return sibling;
3403}
3404
3405static void
3406subsurface_place_above(struct wl_client *client,
3407 struct wl_resource *resource,
3408 struct wl_resource *sibling_resource)
3409{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003410 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003411 struct weston_surface *surface =
3412 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003413 struct weston_subsurface *sibling;
3414
3415 if (!sub)
3416 return;
3417
3418 sibling = subsurface_sibling_check(sub, surface, "place_above");
3419 if (!sibling)
3420 return;
3421
3422 wl_list_remove(&sub->parent_link_pending);
3423 wl_list_insert(sibling->parent_link_pending.prev,
3424 &sub->parent_link_pending);
3425}
3426
3427static void
3428subsurface_place_below(struct wl_client *client,
3429 struct wl_resource *resource,
3430 struct wl_resource *sibling_resource)
3431{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003432 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003433 struct weston_surface *surface =
3434 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003435 struct weston_subsurface *sibling;
3436
3437 if (!sub)
3438 return;
3439
3440 sibling = subsurface_sibling_check(sub, surface, "place_below");
3441 if (!sibling)
3442 return;
3443
3444 wl_list_remove(&sub->parent_link_pending);
3445 wl_list_insert(&sibling->parent_link_pending,
3446 &sub->parent_link_pending);
3447}
3448
3449static void
3450subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3451{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003452 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003453
3454 if (sub)
3455 sub->synchronized = 1;
3456}
3457
3458static void
3459subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3460{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003461 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003462
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003463 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003464 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003465
3466 /* If sub became effectively desynchronized, flush. */
3467 if (!weston_subsurface_is_synchronized(sub))
3468 weston_subsurface_synchronized_commit(sub);
3469 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003470}
3471
3472static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003473weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3474{
3475 wl_list_remove(&sub->parent_link);
3476 wl_list_remove(&sub->parent_link_pending);
3477 wl_list_remove(&sub->parent_destroy_listener.link);
3478 sub->parent = NULL;
3479}
3480
3481static void
3482weston_subsurface_destroy(struct weston_subsurface *sub);
3483
3484static void
3485subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3486{
3487 struct weston_subsurface *sub =
3488 container_of(listener, struct weston_subsurface,
3489 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003490 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003491
3492 /* The protocol object (wl_resource) is left inert. */
3493 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003494 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003495
3496 weston_subsurface_destroy(sub);
3497}
3498
3499static void
3500subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3501{
3502 struct weston_subsurface *sub =
3503 container_of(listener, struct weston_subsurface,
3504 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003505 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003506 assert(sub->surface != sub->parent);
3507
3508 if (weston_surface_is_mapped(sub->surface))
3509 weston_surface_unmap(sub->surface);
3510
3511 weston_subsurface_unlink_parent(sub);
3512}
3513
3514static void
3515subsurface_resource_destroy(struct wl_resource *resource)
3516{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003517 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003518
3519 if (sub)
3520 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003521}
3522
3523static void
3524subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3525{
3526 wl_resource_destroy(resource);
3527}
3528
3529static void
3530weston_subsurface_link_parent(struct weston_subsurface *sub,
3531 struct weston_surface *parent)
3532{
3533 sub->parent = parent;
3534 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003535 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003536 &sub->parent_destroy_listener);
3537
3538 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3539 wl_list_insert(&parent->subsurface_list_pending,
3540 &sub->parent_link_pending);
3541}
3542
3543static void
3544weston_subsurface_link_surface(struct weston_subsurface *sub,
3545 struct weston_surface *surface)
3546{
3547 sub->surface = surface;
3548 sub->surface_destroy_listener.notify =
3549 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003550 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003551 &sub->surface_destroy_listener);
3552}
3553
3554static void
3555weston_subsurface_destroy(struct weston_subsurface *sub)
3556{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003557 struct weston_view *view, *next;
3558
Pekka Paalanene67858b2013-04-25 13:57:42 +03003559 assert(sub->surface);
3560
3561 if (sub->resource) {
3562 assert(weston_surface_to_subsurface(sub->surface) == sub);
3563 assert(sub->parent_destroy_listener.notify ==
3564 subsurface_handle_parent_destroy);
3565
George Kiagiadakised04d382014-06-13 18:10:26 +02003566 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3567 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003568 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003569 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003570
Pekka Paalanene67858b2013-04-25 13:57:42 +03003571 if (sub->parent)
3572 weston_subsurface_unlink_parent(sub);
3573
Jason Ekstrand7b982072014-05-20 14:33:03 -05003574 weston_surface_state_fini(&sub->cached);
3575 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003576
3577 sub->surface->configure = NULL;
3578 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003579 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003580 } else {
3581 /* the dummy weston_subsurface for the parent itself */
3582 assert(sub->parent_destroy_listener.notify == NULL);
3583 wl_list_remove(&sub->parent_link);
3584 wl_list_remove(&sub->parent_link_pending);
3585 }
3586
3587 wl_list_remove(&sub->surface_destroy_listener.link);
3588 free(sub);
3589}
3590
3591static const struct wl_subsurface_interface subsurface_implementation = {
3592 subsurface_destroy,
3593 subsurface_set_position,
3594 subsurface_place_above,
3595 subsurface_place_below,
3596 subsurface_set_sync,
3597 subsurface_set_desync
3598};
3599
3600static struct weston_subsurface *
3601weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3602 struct weston_surface *parent)
3603{
3604 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003605 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003606
Bryce Harringtonde16d892014-11-20 22:21:57 -08003607 sub = zalloc(sizeof *sub);
3608 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003609 return NULL;
3610
Jason Ekstranda7af7042013-10-12 22:38:11 -05003611 wl_list_init(&sub->unused_views);
3612
Jason Ekstranda85118c2013-06-27 20:17:02 -05003613 sub->resource =
3614 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003615 if (!sub->resource) {
3616 free(sub);
3617 return NULL;
3618 }
3619
Jason Ekstranda85118c2013-06-27 20:17:02 -05003620 wl_resource_set_implementation(sub->resource,
3621 &subsurface_implementation,
3622 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003623 weston_subsurface_link_surface(sub, surface);
3624 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003625 weston_surface_state_init(&sub->cached);
3626 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003627 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003628
3629 return sub;
3630}
3631
3632/* Create a dummy subsurface for having the parent itself in its
3633 * sub-surface lists. Makes stacking order manipulation easy.
3634 */
3635static struct weston_subsurface *
3636weston_subsurface_create_for_parent(struct weston_surface *parent)
3637{
3638 struct weston_subsurface *sub;
3639
Bryce Harringtonde16d892014-11-20 22:21:57 -08003640 sub = zalloc(sizeof *sub);
3641 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003642 return NULL;
3643
3644 weston_subsurface_link_surface(sub, parent);
3645 sub->parent = parent;
3646 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3647 wl_list_insert(&parent->subsurface_list_pending,
3648 &sub->parent_link_pending);
3649
3650 return sub;
3651}
3652
3653static void
3654subcompositor_get_subsurface(struct wl_client *client,
3655 struct wl_resource *resource,
3656 uint32_t id,
3657 struct wl_resource *surface_resource,
3658 struct wl_resource *parent_resource)
3659{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003660 struct weston_surface *surface =
3661 wl_resource_get_user_data(surface_resource);
3662 struct weston_surface *parent =
3663 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003664 struct weston_subsurface *sub;
3665 static const char where[] = "get_subsurface: wl_subsurface@";
3666
3667 if (surface == parent) {
3668 wl_resource_post_error(resource,
3669 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3670 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003671 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003672 return;
3673 }
3674
3675 if (weston_surface_to_subsurface(surface)) {
3676 wl_resource_post_error(resource,
3677 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3678 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003679 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003680 return;
3681 }
3682
Pekka Paalanen50b67472014-10-01 15:02:41 +03003683 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3684 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003685 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003686
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003687 if (weston_surface_get_main_surface(parent) == surface) {
3688 wl_resource_post_error(resource,
3689 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3690 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003691 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003692 return;
3693 }
3694
Pekka Paalanene67858b2013-04-25 13:57:42 +03003695 /* make sure the parent is in its own list */
3696 if (wl_list_empty(&parent->subsurface_list)) {
3697 if (!weston_subsurface_create_for_parent(parent)) {
3698 wl_resource_post_no_memory(resource);
3699 return;
3700 }
3701 }
3702
3703 sub = weston_subsurface_create(id, surface, parent);
3704 if (!sub) {
3705 wl_resource_post_no_memory(resource);
3706 return;
3707 }
3708
3709 surface->configure = subsurface_configure;
3710 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003711 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003712}
3713
3714static void
3715subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3716{
3717 wl_resource_destroy(resource);
3718}
3719
3720static const struct wl_subcompositor_interface subcompositor_interface = {
3721 subcompositor_destroy,
3722 subcompositor_get_subsurface
3723};
3724
3725static void
3726bind_subcompositor(struct wl_client *client,
3727 void *data, uint32_t version, uint32_t id)
3728{
3729 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003730 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003731
Jason Ekstranda85118c2013-06-27 20:17:02 -05003732 resource =
3733 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003734 if (resource == NULL) {
3735 wl_client_post_no_memory(client);
3736 return;
3737 }
3738 wl_resource_set_implementation(resource, &subcompositor_interface,
3739 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003740}
3741
3742static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003743weston_compositor_dpms(struct weston_compositor *compositor,
3744 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003745{
3746 struct weston_output *output;
3747
3748 wl_list_for_each(output, &compositor->output_list, link)
3749 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003750 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003751}
3752
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003753WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003754weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003755{
Neil Roberts8b62e202013-09-30 13:14:47 +01003756 uint32_t old_state = compositor->state;
3757
3758 /* The state needs to be changed before emitting the wake
3759 * signal because that may try to schedule a repaint which
3760 * will not work if the compositor is still sleeping */
3761 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3762
3763 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003764 case WESTON_COMPOSITOR_SLEEPING:
3765 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3766 /* fall through */
3767 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003768 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003769 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003770 /* fall through */
3771 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003772 wl_event_source_timer_update(compositor->idle_source,
3773 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003774 }
3775}
3776
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003777WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003778weston_compositor_offscreen(struct weston_compositor *compositor)
3779{
3780 switch (compositor->state) {
3781 case WESTON_COMPOSITOR_OFFSCREEN:
3782 return;
3783 case WESTON_COMPOSITOR_SLEEPING:
3784 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3785 /* fall through */
3786 default:
3787 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3788 wl_event_source_timer_update(compositor->idle_source, 0);
3789 }
3790}
3791
3792WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003793weston_compositor_sleep(struct weston_compositor *compositor)
3794{
3795 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3796 return;
3797
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003798 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003799 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3800 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3801}
3802
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003803static int
3804idle_handler(void *data)
3805{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003806 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003807
3808 if (compositor->idle_inhibit)
3809 return 1;
3810
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003811 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003812 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003813
3814 return 1;
3815}
3816
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003817WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003818weston_plane_init(struct weston_plane *plane,
3819 struct weston_compositor *ec,
3820 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003821{
3822 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003823 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003824 plane->x = x;
3825 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003826 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003827
3828 /* Init the link so that the call to wl_list_remove() when releasing
3829 * the plane without ever stacking doesn't lead to a crash */
3830 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003831}
3832
3833WL_EXPORT void
3834weston_plane_release(struct weston_plane *plane)
3835{
Xiong Zhang97116532013-10-23 13:58:31 +08003836 struct weston_view *view;
3837
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003838 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003839 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003840
Xiong Zhang97116532013-10-23 13:58:31 +08003841 wl_list_for_each(view, &plane->compositor->view_list, link) {
3842 if (view->plane == plane)
3843 view->plane = NULL;
3844 }
3845
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003846 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003847}
3848
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003849WL_EXPORT void
3850weston_compositor_stack_plane(struct weston_compositor *ec,
3851 struct weston_plane *plane,
3852 struct weston_plane *above)
3853{
3854 if (above)
3855 wl_list_insert(above->link.prev, &plane->link);
3856 else
3857 wl_list_insert(&ec->plane_list, &plane->link);
3858}
3859
Casey Dahlin9074db52012-04-19 22:50:09 -04003860static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003861{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003862 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003863}
3864
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003865static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003866bind_output(struct wl_client *client,
3867 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003868{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003869 struct weston_output *output = data;
3870 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003871 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003872
Jason Ekstranda85118c2013-06-27 20:17:02 -05003873 resource = wl_resource_create(client, &wl_output_interface,
3874 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003875 if (resource == NULL) {
3876 wl_client_post_no_memory(client);
3877 return;
3878 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003879
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003880 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003881 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003882
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003883 wl_output_send_geometry(resource,
3884 output->x,
3885 output->y,
3886 output->mm_width,
3887 output->mm_height,
3888 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003889 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003890 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003891 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003892 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003893 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003894
3895 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003896 wl_output_send_mode(resource,
3897 mode->flags,
3898 mode->width,
3899 mode->height,
3900 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003901 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003902
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003903 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003904 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003905}
3906
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003907/* Move other outputs when one is removed so the space remains contiguos. */
3908static void
3909weston_compositor_remove_output(struct weston_compositor *compositor,
3910 struct weston_output *remove_output)
3911{
3912 struct weston_output *output;
3913 int offset = 0;
3914
3915 wl_list_for_each(output, &compositor->output_list, link) {
3916 if (output == remove_output) {
3917 offset = output->width;
3918 continue;
3919 }
3920
3921 if (offset > 0) {
3922 weston_output_move(output,
3923 output->x - offset, output->y);
3924 output->dirty = 1;
3925 }
3926 }
3927}
3928
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003929WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003930weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003931{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003932 struct wl_resource *resource;
3933
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003934 output->destroying = 1;
3935
Pekka Paalanen0513a952014-05-21 16:17:27 +03003936 wl_event_source_remove(output->repaint_timer);
3937
Pekka Paalanen133e4392014-09-23 22:08:46 -04003938 weston_presentation_feedback_discard_list(&output->feedback_list);
3939
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003940 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003941 wl_list_remove(&output->link);
3942
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003943 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003944 wl_signal_emit(&output->destroy_signal, output);
3945
Richard Hughesafe690c2013-05-02 10:10:04 +01003946 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003947 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003948 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003949 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003950
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003951 wl_resource_for_each(resource, &output->resource_list) {
3952 wl_resource_set_destructor(resource, NULL);
3953 }
3954
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003955 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003956}
3957
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003958WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003959weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003960{
Scott Moreau850ca422012-05-21 15:21:25 -06003961 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003962
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003963 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003964 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003965
Scott Moreauccbf29d2012-02-22 14:21:41 -07003966 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003967 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003968 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003969 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003970 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003971 weston_matrix_scale(&output->matrix, magnification,
3972 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003973 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003974
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003975 switch (output->transform) {
3976 case WL_OUTPUT_TRANSFORM_FLIPPED:
3977 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3978 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3979 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3980 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3981 weston_matrix_scale(&output->matrix, -1, 1, 1);
3982 break;
3983 }
3984
3985 switch (output->transform) {
3986 default:
3987 case WL_OUTPUT_TRANSFORM_NORMAL:
3988 case WL_OUTPUT_TRANSFORM_FLIPPED:
3989 break;
3990 case WL_OUTPUT_TRANSFORM_90:
3991 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3992 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
3993 weston_matrix_rotate_xy(&output->matrix, 0, 1);
3994 break;
3995 case WL_OUTPUT_TRANSFORM_180:
3996 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3997 weston_matrix_translate(&output->matrix,
3998 -output->width, -output->height, 0);
3999 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4000 break;
4001 case WL_OUTPUT_TRANSFORM_270:
4002 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4003 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4004 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4005 break;
4006 }
4007
4008 if (output->current_scale != 1)
4009 weston_matrix_scale(&output->matrix,
4010 output->current_scale,
4011 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004012
Scott Moreauccbf29d2012-02-22 14:21:41 -07004013 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004014
4015 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004016}
4017
Scott Moreau1bad5db2012-08-18 01:04:05 -06004018static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004019weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004020{
4021 output->transform = transform;
4022
4023 switch (transform) {
4024 case WL_OUTPUT_TRANSFORM_90:
4025 case WL_OUTPUT_TRANSFORM_270:
4026 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4027 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4028 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02004029 output->width = output->current_mode->height;
4030 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004031 break;
4032 case WL_OUTPUT_TRANSFORM_NORMAL:
4033 case WL_OUTPUT_TRANSFORM_180:
4034 case WL_OUTPUT_TRANSFORM_FLIPPED:
4035 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02004036 output->width = output->current_mode->width;
4037 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004038 break;
4039 default:
4040 break;
4041 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06004042
Hardening57388e42013-09-18 23:56:36 +02004043 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02004044 output->width /= scale;
4045 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02004046}
4047
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004048static void
4049weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004050{
4051 output->x = x;
4052 output->y = y;
4053
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004054 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004055 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004056 output->width,
4057 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004058}
4059
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004060WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004061weston_output_move(struct weston_output *output, int x, int y)
4062{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004063 struct wl_resource *resource;
4064
4065 output->move_x = x - output->x;
4066 output->move_y = y - output->y;
4067
4068 if (output->move_x == 0 && output->move_y == 0)
4069 return;
4070
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004071 weston_output_init_geometry(output, x, y);
4072
4073 output->dirty = 1;
4074
4075 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004076 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004077
4078 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004079 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004080 wl_output_send_geometry(resource,
4081 output->x,
4082 output->y,
4083 output->mm_width,
4084 output->mm_height,
4085 output->subpixel,
4086 output->make,
4087 output->model,
4088 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004089
4090 if (wl_resource_get_version(resource) >= 2)
4091 wl_output_send_done(resource);
4092 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004093}
4094
4095WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004096weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004097 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004098 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004099{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004100 struct wl_event_loop *loop;
4101
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004102 output->compositor = c;
4103 output->x = x;
4104 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004105 output->mm_width = mm_width;
4106 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004107 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004108 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004109
Alexander Larsson0b135062013-05-28 16:23:36 +02004110 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004111 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004112
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004113 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004114 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004115
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004116 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004117 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004118 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004119 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004120 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02004121
Pekka Paalanen0513a952014-05-21 16:17:27 +03004122 loop = wl_display_get_event_loop(c->wl_display);
4123 output->repaint_timer = wl_event_loop_add_timer(loop,
4124 output_repaint_timer_handler, output);
4125
Casey Dahlin58ba1372012-04-19 22:50:08 -04004126 output->id = ffs(~output->compositor->output_id_pool) - 1;
4127 output->compositor->output_id_pool |= 1 << output->id;
4128
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004129 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004130 wl_global_create(c->wl_display, &wl_output_interface, 2,
4131 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004132}
4133
4134/** Adds an output to the compositor's output list and
4135 * send the compositor's output_created signal.
4136 *
4137 * \param compositor The compositor instance.
4138 * \param output The output to be added.
4139 */
4140WL_EXPORT void
4141weston_compositor_add_output(struct weston_compositor *compositor,
4142 struct weston_output *output)
4143{
4144 wl_list_insert(compositor->output_list.prev, &output->link);
4145 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004146}
4147
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004148WL_EXPORT void
4149weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05004150 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004151 wl_fixed_t *x, wl_fixed_t *y)
4152{
Derek Foreman0f679412014-10-02 13:41:17 -05004153 struct weston_vector p = { {
4154 wl_fixed_to_double(device_x),
4155 wl_fixed_to_double(device_y),
4156 0.0,
4157 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004158
Derek Foreman67a18b92015-03-24 11:36:14 -05004159 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004160
Derek Foreman0f679412014-10-02 13:41:17 -05004161 *x = wl_fixed_from_double(p.f[0] / p.f[3]);
4162 *y = wl_fixed_from_double(p.f[1] / p.f[3]);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004163}
4164
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004165static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004166destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004167{
Jonny Lamb74130762013-11-26 18:19:46 +01004168 struct weston_surface *surface =
4169 wl_resource_get_user_data(resource);
4170
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004171 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004172 surface->pending.buffer_viewport.buffer.src_width =
4173 wl_fixed_from_int(-1);
4174 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004175 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004176}
4177
4178static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004179viewport_destroy(struct wl_client *client,
4180 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004181{
4182 wl_resource_destroy(resource);
4183}
4184
4185static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004186viewport_set(struct wl_client *client,
4187 struct wl_resource *resource,
4188 wl_fixed_t src_x,
4189 wl_fixed_t src_y,
4190 wl_fixed_t src_width,
4191 wl_fixed_t src_height,
4192 int32_t dst_width,
4193 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004194{
Jonny Lamb74130762013-11-26 18:19:46 +01004195 struct weston_surface *surface =
4196 wl_resource_get_user_data(resource);
4197
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004198 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01004199
Jonny Lamb8ae35902013-11-26 18:19:45 +01004200 if (wl_fixed_to_double(src_width) < 0 ||
4201 wl_fixed_to_double(src_height) < 0) {
4202 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004203 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004204 "source dimensions must be non-negative (%fx%f)",
4205 wl_fixed_to_double(src_width),
4206 wl_fixed_to_double(src_height));
4207 return;
4208 }
4209
4210 if (dst_width <= 0 || dst_height <= 0) {
4211 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004212 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004213 "destination dimensions must be positive (%dx%d)",
4214 dst_width, dst_height);
4215 return;
4216 }
4217
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004218 surface->pending.buffer_viewport.buffer.src_x = src_x;
4219 surface->pending.buffer_viewport.buffer.src_y = src_y;
4220 surface->pending.buffer_viewport.buffer.src_width = src_width;
4221 surface->pending.buffer_viewport.buffer.src_height = src_height;
4222 surface->pending.buffer_viewport.surface.width = dst_width;
4223 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004224 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004225}
4226
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004227static void
4228viewport_set_source(struct wl_client *client,
4229 struct wl_resource *resource,
4230 wl_fixed_t src_x,
4231 wl_fixed_t src_y,
4232 wl_fixed_t src_width,
4233 wl_fixed_t src_height)
4234{
4235 struct weston_surface *surface =
4236 wl_resource_get_user_data(resource);
4237
4238 assert(surface->viewport_resource != NULL);
4239
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004240 if (src_width == wl_fixed_from_int(-1) &&
4241 src_height == wl_fixed_from_int(-1)) {
4242 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004243 surface->pending.buffer_viewport.buffer.src_width =
4244 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004245 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004246 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004247 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004248
4249 if (src_width <= 0 || src_height <= 0) {
4250 wl_resource_post_error(resource,
4251 WL_VIEWPORT_ERROR_BAD_VALUE,
4252 "source size must be positive (%fx%f)",
4253 wl_fixed_to_double(src_width),
4254 wl_fixed_to_double(src_height));
4255 return;
4256 }
4257
4258 surface->pending.buffer_viewport.buffer.src_x = src_x;
4259 surface->pending.buffer_viewport.buffer.src_y = src_y;
4260 surface->pending.buffer_viewport.buffer.src_width = src_width;
4261 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004262 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004263}
4264
4265static void
4266viewport_set_destination(struct wl_client *client,
4267 struct wl_resource *resource,
4268 int32_t dst_width,
4269 int32_t dst_height)
4270{
4271 struct weston_surface *surface =
4272 wl_resource_get_user_data(resource);
4273
4274 assert(surface->viewport_resource != NULL);
4275
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004276 if (dst_width == -1 && dst_height == -1) {
4277 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004278 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004279 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004280 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004281 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004282
4283 if (dst_width <= 0 || dst_height <= 0) {
4284 wl_resource_post_error(resource,
4285 WL_VIEWPORT_ERROR_BAD_VALUE,
4286 "destination size must be positive (%dx%d)",
4287 dst_width, dst_height);
4288 return;
4289 }
4290
4291 surface->pending.buffer_viewport.surface.width = dst_width;
4292 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004293 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004294}
4295
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004296static const struct wl_viewport_interface viewport_interface = {
4297 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004298 viewport_set,
4299 viewport_set_source,
4300 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004301};
4302
4303static void
4304scaler_destroy(struct wl_client *client,
4305 struct wl_resource *resource)
4306{
4307 wl_resource_destroy(resource);
4308}
4309
4310static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004311scaler_get_viewport(struct wl_client *client,
4312 struct wl_resource *scaler,
4313 uint32_t id,
4314 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004315{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004316 int version = wl_resource_get_version(scaler);
4317 struct weston_surface *surface =
4318 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004319 struct wl_resource *resource;
4320
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004321 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01004322 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004323 WL_SCALER_ERROR_VIEWPORT_EXISTS,
4324 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004325 return;
4326 }
4327
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004328 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004329 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004330 if (resource == NULL) {
4331 wl_client_post_no_memory(client);
4332 return;
4333 }
4334
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004335 wl_resource_set_implementation(resource, &viewport_interface,
4336 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004337
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004338 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004339}
4340
4341static const struct wl_scaler_interface scaler_interface = {
4342 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004343 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004344};
4345
4346static void
4347bind_scaler(struct wl_client *client,
4348 void *data, uint32_t version, uint32_t id)
4349{
4350 struct wl_resource *resource;
4351
4352 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004353 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004354 if (resource == NULL) {
4355 wl_client_post_no_memory(client);
4356 return;
4357 }
4358
4359 wl_resource_set_implementation(resource, &scaler_interface,
4360 NULL, NULL);
4361}
4362
4363static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004364destroy_presentation_feedback(struct wl_resource *feedback_resource)
4365{
4366 struct weston_presentation_feedback *feedback;
4367
4368 feedback = wl_resource_get_user_data(feedback_resource);
4369
4370 wl_list_remove(&feedback->link);
4371 free(feedback);
4372}
4373
4374static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004375presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4376{
4377 wl_resource_destroy(resource);
4378}
4379
4380static void
4381presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004382 struct wl_resource *presentation_resource,
4383 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004384 uint32_t callback)
4385{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004386 struct weston_surface *surface;
4387 struct weston_presentation_feedback *feedback;
4388
4389 surface = wl_resource_get_user_data(surface_resource);
4390
Bryce Harringtonde16d892014-11-20 22:21:57 -08004391 feedback = zalloc(sizeof *feedback);
4392 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004393 goto err_calloc;
4394
4395 feedback->resource = wl_resource_create(client,
4396 &presentation_feedback_interface,
4397 1, callback);
4398 if (!feedback->resource)
4399 goto err_create;
4400
4401 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4402 destroy_presentation_feedback);
4403
4404 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4405
4406 return;
4407
4408err_create:
4409 free(feedback);
4410
4411err_calloc:
4412 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004413}
4414
4415static const struct presentation_interface presentation_implementation = {
4416 presentation_destroy,
4417 presentation_feedback
4418};
4419
4420static void
4421bind_presentation(struct wl_client *client,
4422 void *data, uint32_t version, uint32_t id)
4423{
4424 struct weston_compositor *compositor = data;
4425 struct wl_resource *resource;
4426
4427 resource = wl_resource_create(client, &presentation_interface,
4428 MIN(version, 1), id);
4429 if (resource == NULL) {
4430 wl_client_post_no_memory(client);
4431 return;
4432 }
4433
4434 wl_resource_set_implementation(resource, &presentation_implementation,
4435 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004436 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004437}
4438
4439static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004440compositor_bind(struct wl_client *client,
4441 void *data, uint32_t version, uint32_t id)
4442{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004443 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004444 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004445
Jason Ekstranda85118c2013-06-27 20:17:02 -05004446 resource = wl_resource_create(client, &wl_compositor_interface,
4447 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004448 if (resource == NULL) {
4449 wl_client_post_no_memory(client);
4450 return;
4451 }
4452
4453 wl_resource_set_implementation(resource, &compositor_interface,
4454 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004455}
4456
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04004457static void
Martin Minarikf12c2872012-06-11 00:57:39 +02004458log_uname(void)
4459{
4460 struct utsname usys;
4461
4462 uname(&usys);
4463
4464 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
4465 usys.version, usys.machine);
4466}
4467
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004468WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004469weston_environment_get_fd(const char *env)
4470{
4471 char *e, *end;
4472 int fd, flags;
4473
4474 e = getenv(env);
4475 if (!e)
4476 return -1;
4477 fd = strtol(e, &end, 0);
4478 if (*end != '\0')
4479 return -1;
4480
4481 flags = fcntl(fd, F_GETFD);
4482 if (flags == -1)
4483 return -1;
4484
4485 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4486 unsetenv(env);
4487
4488 return fd;
4489}
4490
Pekka Paalanenb5026542014-11-12 15:09:24 +02004491static void
4492timeline_key_binding_handler(struct weston_seat *seat, uint32_t time,
4493 uint32_t key, void *data)
4494{
4495 struct weston_compositor *compositor = data;
4496
4497 if (weston_timeline_enabled_)
4498 weston_timeline_close();
4499 else
4500 weston_timeline_open(compositor);
4501}
4502
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004503WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04004504weston_compositor_init(struct weston_compositor *ec,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004505 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004506 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004507{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004508 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01004509 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004510 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004511
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004512 ec->config = config;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004513 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004514 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004515 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004516 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004517 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004518 wl_signal_init(&ec->idle_signal);
4519 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004520 wl_signal_init(&ec->show_input_panel_signal);
4521 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004522 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004523 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004524 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004525 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004526 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004527 wl_signal_init(&ec->session_signal);
4528 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004529
Casey Dahlin58ba1372012-04-19 22:50:08 -04004530 ec->output_id_pool = 0;
4531
Giulio Camuffo954f1832014-10-11 18:27:30 +03004532 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004533 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05004534 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004535
Giulio Camuffo954f1832014-10-11 18:27:30 +03004536 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004537 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03004538 return -1;
4539
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004540 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004541 ec, bind_scaler))
4542 return -1;
4543
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004544 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4545 ec, bind_presentation))
4546 return -1;
4547
Jason Ekstranda7af7042013-10-12 22:38:11 -05004548 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004549 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004550 wl_list_init(&ec->layer_list);
4551 wl_list_init(&ec->seat_list);
4552 wl_list_init(&ec->output_list);
4553 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004554 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004555 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004556 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004557 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004558 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004559
Xiong Zhang97116532013-10-23 13:58:31 +08004560 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004561 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004562
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004563 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
4564 weston_config_section_get_string(s, "keymap_rules",
4565 (char **) &xkb_names.rules, NULL);
4566 weston_config_section_get_string(s, "keymap_model",
4567 (char **) &xkb_names.model, NULL);
4568 weston_config_section_get_string(s, "keymap_layout",
4569 (char **) &xkb_names.layout, NULL);
4570 weston_config_section_get_string(s, "keymap_variant",
4571 (char **) &xkb_names.variant, NULL);
4572 weston_config_section_get_string(s, "keymap_options",
4573 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01004574
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05004575 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
4576 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01004577
Jonny Lamb66a41a02014-08-12 14:58:25 +02004578 weston_config_section_get_int(s, "repeat-rate",
4579 &ec->kb_repeat_rate, 40);
4580 weston_config_section_get_int(s, "repeat-delay",
4581 &ec->kb_repeat_delay, 400);
4582
Daniel Stone725c2c32012-06-22 14:04:36 +01004583 wl_data_device_manager_init(ec->wl_display);
4584
Giulio Camuffo954f1832014-10-11 18:27:30 +03004585 wl_display_init_shm(ec->wl_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
Giulio Camuffo954f1832014-10-11 18:27:30 +03004901 segv_compositor->backend->restore(segv_compositor);
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004902
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
JoonCheol Parke28ee342015-06-12 18:26:02 +09005044#if defined(BUILD_HEADLESS_COMPOSITOR)
5045 "\t\t\t\theadless-backend.so\n"
5046#endif
Bryce Harrington32297c92014-10-23 15:25:04 -07005047#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
5048 "\t\t\t\trpi-backend.so\n"
5049#endif
Jason Ekstranda985da42013-08-22 17:28:03 -05005050 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005051 " -S, --socket=NAME\tName of socket to listen on\n"
5052 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005053 " --modules\t\tLoad the comma-separated list of modules\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07005054 " --log=FILE\t\tLog to the given file\n"
Bryce Harrington06c42742015-04-02 19:16:50 -07005055 " -c, --config=FILE\tConfig file to load, defaults to weston.ini\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03005056 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005057 " -h, --help\t\tThis help message\n\n");
5058
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005059#if defined(BUILD_DRM_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005060 fprintf(stderr,
5061 "Options for drm-backend.so:\n\n"
5062 " --connector=ID\tBring up only this connector\n"
5063 " --seat=SEAT\t\tThe seat that weston should run on\n"
5064 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02005065 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005066 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005067#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005068
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005069#if defined(BUILD_FBDEV_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005070 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01005071 "Options for fbdev-backend.so:\n\n"
5072 " --tty=TTY\t\tThe tty to use\n"
Derek Foreman4fa7b7f2015-06-10 16:57:09 -05005073 " --device=DEVICE\tThe framebuffer device to use\n"
5074 " --use-gl\t\tUse the GL renderer\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005075#endif
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01005076
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005077#if defined(BUILD_X11_COMPOSITOR)
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01005078 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005079 "Options for x11-backend.so:\n\n"
5080 " --width=WIDTH\t\tWidth of X window\n"
5081 " --height=HEIGHT\tHeight of X window\n"
Derek Foreman4fa7b7f2015-06-10 16:57:09 -05005082 " --scale=SCALE\t\tScale factor of output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005083 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05005084 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005085 " --output-count=COUNT\tCreate multiple outputs\n"
5086 " --no-input\t\tDont create input devices\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005087#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005088
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005089#if defined(BUILD_WAYLAND_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005090 fprintf(stderr,
5091 "Options for wayland-backend.so:\n\n"
5092 " --width=WIDTH\t\tWidth of Wayland surface\n"
5093 " --height=HEIGHT\tHeight of Wayland surface\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07005094 " --scale=SCALE\t\tScale factor of output\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06005095 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05005096 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05005097 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05005098 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005099 " --display=DISPLAY\tWayland display to connect to\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005100#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005101
Pekka Paalanene31e0532013-05-22 18:03:07 +03005102#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
5103 fprintf(stderr,
5104 "Options for rpi-backend.so:\n\n"
5105 " --tty=TTY\t\tThe tty to use\n"
5106 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
5107 " --transform=TR\tThe output transformation, TR is one of:\n"
5108 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01005109 " --opaque-regions\tEnable support for opaque regions, can be "
5110 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03005111 "\n");
5112#endif
5113
Hardeningc077a842013-07-08 00:51:35 +02005114#if defined(BUILD_RDP_COMPOSITOR)
Bryce Harrington59fe4232014-10-23 14:44:34 -07005115 fprintf(stderr,
5116 "Options for rdp-backend.so:\n\n"
5117 " --width=WIDTH\t\tWidth of desktop\n"
5118 " --height=HEIGHT\tHeight of desktop\n"
5119 " --env-socket=SOCKET\tUse that socket as peer connection\n"
5120 " --address=ADDR\tThe address to bind\n"
Bryce Harringtonf5b34ae2014-10-23 14:44:35 -07005121 " --port=PORT\t\tThe port to listen on\n"
Bryce Harrington59fe4232014-10-23 14:44:34 -07005122 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
5123 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
5124 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
5125 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
5126 "\n");
Hardeningc077a842013-07-08 00:51:35 +02005127#endif
5128
Bryce Harrington3e3b59e2014-11-19 15:06:19 -08005129#if defined(BUILD_HEADLESS_COMPOSITOR)
5130 fprintf(stderr,
5131 "Options for headless-backend.so:\n\n"
5132 " --width=WIDTH\t\tWidth of memory surface\n"
5133 " --height=HEIGHT\tHeight of memory surface\n"
5134 " --transform=TR\tThe output transformation, TR is one of:\n"
5135 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
5136 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
5137#endif
5138
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005139 exit(error_code);
5140}
5141
Peter Maatmane5b42e42013-03-27 22:38:53 +01005142static void
5143catch_signals(void)
5144{
5145 struct sigaction action;
5146
5147 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
5148 action.sa_sigaction = on_caught_signal;
5149 sigemptyset(&action.sa_mask);
5150 sigaction(SIGSEGV, &action, NULL);
5151 sigaction(SIGABRT, &action, NULL);
5152}
5153
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005154static void
5155handle_primary_client_destroyed(struct wl_listener *listener, void *data)
5156{
5157 struct wl_client *client = data;
5158
5159 weston_log("Primary client died. Closing...\n");
5160
5161 wl_display_terminate(wl_client_get_display(client));
5162}
5163
Ryo Munakatad8deff62014-09-06 07:32:05 +09005164static char *
5165weston_choose_default_backend(void)
5166{
5167 char *backend = NULL;
5168
5169 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
5170 backend = strdup("wayland-backend.so");
5171 else if (getenv("DISPLAY"))
5172 backend = strdup("x11-backend.so");
5173 else
5174 backend = strdup(WESTON_NATIVE_BACKEND);
5175
5176 return backend;
5177}
5178
5179static int
5180weston_create_listening_socket(struct wl_display *display, const char *socket_name)
5181{
5182 if (socket_name) {
5183 if (wl_display_add_socket(display, socket_name)) {
5184 weston_log("fatal: failed to add socket: %m\n");
5185 return -1;
5186 }
5187 } else {
5188 socket_name = wl_display_add_socket_auto(display);
5189 if (!socket_name) {
5190 weston_log("fatal: failed to add socket: %m\n");
5191 return -1;
5192 }
5193 }
5194
5195 setenv("WAYLAND_DISPLAY", socket_name, 1);
5196
5197 return 0;
5198}
5199
Derek Foreman64a3df02014-10-23 12:24:18 -05005200static const struct { const char *name; uint32_t token; } transforms[] = {
5201 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
5202 { "90", WL_OUTPUT_TRANSFORM_90 },
5203 { "180", WL_OUTPUT_TRANSFORM_180 },
5204 { "270", WL_OUTPUT_TRANSFORM_270 },
5205 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
5206 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
5207 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
5208 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
5209};
5210
5211WL_EXPORT int
5212weston_parse_transform(const char *transform, uint32_t *out)
5213{
5214 unsigned int i;
5215
5216 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
5217 if (strcmp(transforms[i].name, transform) == 0) {
5218 *out = transforms[i].token;
5219 return 0;
5220 }
5221
5222 *out = WL_OUTPUT_TRANSFORM_NORMAL;
5223 return -1;
5224}
5225
5226WL_EXPORT const char *
5227weston_transform_to_string(uint32_t output_transform)
5228{
5229 unsigned int i;
5230
5231 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
5232 if (transforms[i].token == output_transform)
5233 return transforms[i].name;
5234
5235 return "<illegal value>";
5236}
5237
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005238static int
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005239load_configuration(struct weston_config **config, int32_t noconfig,
5240 const char *config_file)
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005241{
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005242 const char *file = "weston.ini";
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02005243 const char *full_path;
5244
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005245 *config = NULL;
5246
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005247 if (config_file)
5248 file = config_file;
5249
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005250 if (noconfig == 0)
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005251 *config = weston_config_parse(file);
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005252
5253 if (*config) {
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02005254 full_path = weston_config_get_full_path(*config);
5255
5256 weston_log("Using config file '%s'\n", full_path);
5257 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005258
5259 return 0;
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005260 }
5261
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005262 if (config_file && noconfig == 0) {
5263 weston_log("fatal: error opening or reading config file"
5264 " '%s'.\n", config_file);
5265
5266 return -1;
5267 }
5268
5269 weston_log("Starting with no config file.\n");
5270 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
5271
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005272 return 0;
5273}
Derek Foreman64a3df02014-10-23 12:24:18 -05005274
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005275int main(int argc, char *argv[])
5276{
Pekka Paalanen111c6f92015-03-20 14:35:58 +02005277 int ret = EXIT_FAILURE;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005278 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005279 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005280 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05005281 struct wl_event_loop *loop;
Giulio Camuffo954f1832014-10-11 18:27:30 +03005282 int (*backend_init)(struct weston_compositor *c,
5283 int *argc, char *argv[],
5284 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005285 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005286 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05005287 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02005288 char *modules = NULL;
5289 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02005290 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005291 char *server_socket = NULL, *end;
Frederic Plourde4a84c832014-10-30 15:06:34 -04005292 int32_t idle_time = -1;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005293 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09005294 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06005295 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03005296 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005297 int32_t numlock_on;
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005298 char *config_file = NULL;
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005299 struct weston_config *config;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005300 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005301 struct wl_client *primary_client;
5302 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005303 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04005304
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005305 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09005306 { WESTON_OPTION_STRING, "backend", 'B', &backend },
5307 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005308 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
5309 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005310 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02005311 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005312 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06005313 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03005314 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005315 { WESTON_OPTION_STRING, "config", 'c', &config_file },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04005316 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05005317
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005318 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005319
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005320 if (help)
5321 usage(EXIT_SUCCESS);
5322
Scott Moreau12245142012-08-29 15:15:58 -06005323 if (version) {
5324 printf(PACKAGE_STRING "\n");
5325 return EXIT_SUCCESS;
5326 }
5327
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02005328 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005329
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03005330 weston_log("%s\n"
5331 STAMP_SPACE "%s\n"
5332 STAMP_SPACE "Bug reports to: %s\n"
5333 STAMP_SPACE "Build: %s\n",
5334 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04005335 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03005336 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04005337
Martin Minarik37032f82012-06-18 20:15:18 +02005338 verify_xdg_runtime_dir();
5339
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005340 display = wl_display_create();
5341
Tiago Vignatti2116b892011-08-08 05:52:59 -07005342 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005343 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
5344 display);
5345 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
5346 display);
5347 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
5348 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07005349
5350 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005351 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
5352 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05005353
Pekka Paalanen111c6f92015-03-20 14:35:58 +02005354 if (!signals[0] || !signals[1] || !signals[2] || !signals[3])
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305355 goto out_signals;
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305356
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005357 if (load_configuration(&config, noconfig, config_file) < 0)
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005358 goto out_signals;
5359
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01005360 section = weston_config_get_section(config, "core", NULL, NULL);
5361
Ryo Munakata03faed22014-09-06 07:32:51 +09005362 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005363 weston_config_section_get_string(section, "backend", &backend,
5364 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09005365 if (!backend)
5366 backend = weston_choose_default_backend();
5367 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005368
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03005369 backend_init = weston_load_module(backend, "backend_init");
Pekka Paalanen111c6f92015-03-20 14:35:58 +02005370 if (!backend_init)
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305371 goto out_signals;
Kristian Høgsberga9410222011-01-14 17:22:35 -05005372
Giulio Camuffo954f1832014-10-11 18:27:30 +03005373 ec = zalloc(sizeof *ec);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05005374 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03005375 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305376 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05005377 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04005378
Giulio Camuffo954f1832014-10-11 18:27:30 +03005379 ec->wl_display = display;
5380 if (backend_init(ec, &argc, argv, config) < 0) {
5381 weston_log("fatal: failed to create compositor backend\n");
5382 ret = EXIT_FAILURE;
5383 goto out_signals;
5384 }
5385
Peter Maatmane5b42e42013-03-27 22:38:53 +01005386 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04005387 segv_compositor = ec;
5388
Frederic Plourde4a84c832014-10-30 15:06:34 -04005389 if (idle_time < 0)
5390 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
5391 if (idle_time < 0)
5392 idle_time = 300; /* default idle timeout, in seconds */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005393 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005394 ec->default_pointer_grab = NULL;
Frederic Plourdec336f062014-10-29 14:44:33 -04005395 ec->exit_code = EXIT_SUCCESS;
Pekka Paalanen7296e792011-12-07 16:22:00 +02005396
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005397 weston_compositor_log_capabilities(ec);
5398
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005399 server_socket = getenv("WAYLAND_SERVER_SOCKET");
5400 if (server_socket) {
5401 weston_log("Running with single client\n");
5402 fd = strtol(server_socket, &end, 0);
5403 if (*end != '\0')
5404 fd = -1;
5405 } else {
5406 fd = -1;
5407 }
5408
5409 if (fd != -1) {
5410 primary_client = wl_client_create(display, fd);
5411 if (!primary_client) {
5412 weston_log("fatal: failed to add client: %m\n");
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005413 goto out;
5414 }
5415 primary_client_destroyed.notify =
5416 handle_primary_client_destroyed;
5417 wl_client_add_destroy_listener(primary_client,
5418 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09005419 } else if (weston_create_listening_socket(display, socket_name)) {
Ryo Munakatad8deff62014-09-06 07:32:05 +09005420 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005421 }
5422
Ryo Munakata03faed22014-09-06 07:32:51 +09005423 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005424 weston_config_section_get_string(section, "shell", &shell,
5425 "desktop-shell.so");
5426
Ryo Munakata03faed22014-09-06 07:32:51 +09005427 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005428 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005429
5430 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09005431 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005432 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005433
5434 if (load_modules(ec, option_modules, &argc, argv) < 0)
5435 goto out;
5436
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005437 section = weston_config_get_section(config, "keyboard", NULL, NULL);
5438 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
5439 if (numlock_on) {
5440 wl_list_for_each(seat, &ec->seat_list, link) {
5441 if (seat->keyboard)
5442 weston_keyboard_set_locks(seat->keyboard,
5443 WESTON_NUM_LOCK,
5444 WESTON_NUM_LOCK);
5445 }
5446 }
5447
Pekka Paalanen17a1a962015-03-24 15:56:15 +02005448 for (i = 1; i < argc; i++)
5449 weston_log("fatal: unhandled option: %s\n", argv[i]);
5450 if (argc > 1)
5451 goto out;
5452
Pekka Paalanenc0444e32012-01-05 16:28:21 +02005453 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005454
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04005455 wl_display_run(display);
5456
Frederic Plourdec336f062014-10-29 14:44:33 -04005457 /* Allow for setting return exit code after
5458 * wl_display_run returns normally. This is
5459 * useful for devs/testers and automated tests
5460 * that want to indicate failure status to
5461 * testing infrastructure above
5462 */
5463 ret = ec->exit_code;
5464
Ryo Munakata03faed22014-09-06 07:32:51 +09005465out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005466 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005467 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005468
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005469 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005470
Daniel Stone855028f2012-05-01 20:37:10 +01005471 weston_compositor_xkb_destroy(ec);
5472
Giulio Camuffo954f1832014-10-11 18:27:30 +03005473 ec->backend->destroy(ec);
5474 free(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305475
5476out_signals:
5477 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
5478 if (signals[i])
5479 wl_event_source_remove(signals[i]);
5480
Tiago Vignatti9e2be082011-12-19 00:04:46 +02005481 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005482
Martin Minarik19e6f262012-06-07 13:08:46 +02005483 weston_log_file_close();
5484
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005485 free(config_file);
Ryo Munakata03faed22014-09-06 07:32:51 +09005486 free(backend);
5487 free(shell);
5488 free(socket_name);
5489 free(option_modules);
5490 free(log);
5491 free(modules);
5492
Pekka Paalanen3ab72692012-06-08 17:27:28 +03005493 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04005494}