blob: 1d61dde33c8b5dc3f019bae96cc4b2cd9fca7d98 [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"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030064#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040065#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050066#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050067
Pekka Paalanen0513a952014-05-21 16:17:27 +030068#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
69
70#define NSEC_PER_SEC 1000000000
71
72static void
73timespec_sub(struct timespec *r,
74 const struct timespec *a, const struct timespec *b)
75{
76 r->tv_sec = a->tv_sec - b->tv_sec;
77 r->tv_nsec = a->tv_nsec - b->tv_nsec;
78 if (r->tv_nsec < 0) {
79 r->tv_sec--;
80 r->tv_nsec += NSEC_PER_SEC;
81 }
82}
83
84static int64_t
85timespec_to_nsec(const struct timespec *a)
86{
87 return (int64_t)a->tv_sec * NSEC_PER_SEC + a->tv_nsec;
88}
89
Kristian Høgsberg27da5382011-06-21 17:32:25 -040090static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040091static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040092
93static int
94sigchld_handler(int signal_number, void *data)
95{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050096 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040097 int status;
98 pid_t pid;
99
Pekka Paalanen23ade622014-08-27 13:31:26 +0300100 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
101 wl_list_for_each(p, &child_process_list, link) {
102 if (p->pid == pid)
103 break;
104 }
Bill Spitzak027b9622012-03-17 15:22:03 -0700105
Pekka Paalanen23ade622014-08-27 13:31:26 +0300106 if (&p->link == &child_process_list) {
107 weston_log("unknown child process exited\n");
108 continue;
109 }
110
111 wl_list_remove(&p->link);
112 p->cleanup(p, status);
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400113 }
114
Pekka Paalanen23ade622014-08-27 13:31:26 +0300115 if (pid < 0 && errno != ECHILD)
116 weston_log("waitpid error %m\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400117
118 return 1;
119}
120
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200121static void
Alexander Larsson0b135062013-05-28 16:23:36 +0200122weston_output_transform_scale_init(struct weston_output *output,
123 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200124
Rob Bradford27b17932013-06-26 18:08:46 +0100125static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500126weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +0100127
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600128static void weston_mode_switch_finish(struct weston_output *output,
129 int mode_changed,
130 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800131{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200132 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +0200133 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200134 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -0600135 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +0200136
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200137 pixman_region32_init(&old_output_region);
138 pixman_region32_copy(&old_output_region, &output->region);
139
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200140 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200141 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200142
143 pixman_region32_init(&output->previous_damage);
144 pixman_region32_init_rect(&output->region, output->x, output->y,
145 output->width, output->height);
146
147 weston_output_update_matrix(output);
148
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200149 /* If a pointer falls outside the outputs new geometry, move it to its
150 * lower-right corner */
151 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400152 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200153 int32_t x, y;
154
155 if (!pointer)
156 continue;
157
158 x = wl_fixed_to_int(pointer->x);
159 y = wl_fixed_to_int(pointer->y);
160
161 if (!pixman_region32_contains_point(&old_output_region,
162 x, y, NULL) ||
163 pixman_region32_contains_point(&output->region,
164 x, y, NULL))
165 continue;
166
167 if (x >= output->x + output->width)
168 x = output->x + output->width - 1;
169 if (y >= output->y + output->height)
170 y = output->y + output->height - 1;
171
172 pointer->x = wl_fixed_from_int(x);
173 pointer->y = wl_fixed_from_int(y);
174 }
175
176 pixman_region32_fini(&old_output_region);
177
Derek Foremandd4cd332014-11-10 10:29:59 -0600178 if (!mode_changed && !scale_changed)
179 return;
180
Hardening57388e42013-09-18 23:56:36 +0200181 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600182 wl_resource_for_each(resource, &output->resource_list) {
183 if (mode_changed) {
184 wl_output_send_mode(resource,
185 output->current_mode->flags,
186 output->current_mode->width,
187 output->current_mode->height,
188 output->current_mode->refresh);
189 }
Hardening57388e42013-09-18 23:56:36 +0200190
Derek Foreman41bdc272014-11-05 13:26:57 -0600191 version = wl_resource_get_version(resource);
192 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600193 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200194
Derek Foreman41bdc272014-11-05 13:26:57 -0600195 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
196 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600197 }
198}
199
200WL_EXPORT int
201weston_output_mode_set_native(struct weston_output *output,
202 struct weston_mode *mode,
203 int32_t scale)
204{
205 int ret;
206 int mode_changed = 0, scale_changed = 0;
207
208 if (!output->switch_mode)
209 return -1;
210
211 if (!output->original_mode) {
212 mode_changed = 1;
213 ret = output->switch_mode(output, mode);
214 if (ret < 0)
215 return ret;
216 if (output->current_scale != scale) {
217 scale_changed = 1;
218 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200219 }
220 }
221
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600222 output->native_mode = mode;
223 output->native_scale = scale;
224
225 weston_mode_switch_finish(output, mode_changed, scale_changed);
226
227 return 0;
228}
229
230WL_EXPORT int
231weston_output_mode_switch_to_native(struct weston_output *output)
232{
233 int ret;
234 int mode_changed = 0, scale_changed = 0;
235
236 if (!output->switch_mode)
237 return -1;
238
239 if (!output->original_mode) {
240 weston_log("already in the native mode\n");
241 return -1;
242 }
243 /* the non fullscreen clients haven't seen a mode set since we
244 * switched into a temporary, so we need to notify them if the
245 * mode at that time is different from the native mode now.
246 */
247 mode_changed = (output->original_mode != output->native_mode);
248 scale_changed = (output->original_scale != output->native_scale);
249
250 ret = output->switch_mode(output, output->native_mode);
251 if (ret < 0)
252 return ret;
253
254 output->current_scale = output->native_scale;
255
256 output->original_mode = NULL;
257 output->original_scale = 0;
258
259 weston_mode_switch_finish(output, mode_changed, scale_changed);
260
261 return 0;
262}
263
264WL_EXPORT int
265weston_output_mode_switch_to_temporary(struct weston_output *output,
266 struct weston_mode *mode,
267 int32_t scale)
268{
269 int ret;
270
271 if (!output->switch_mode)
272 return -1;
273
274 /* original_mode is the last mode non full screen clients have seen,
275 * so we shouldn't change it if we already have one set.
276 */
277 if (!output->original_mode) {
278 output->original_mode = output->native_mode;
279 output->original_scale = output->native_scale;
280 }
281 ret = output->switch_mode(output, mode);
282 if (ret < 0)
283 return ret;
284
285 output->current_scale = scale;
286
287 weston_mode_switch_finish(output, 0, 0);
288
289 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800290}
291
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400292WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500293weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400294{
295 wl_list_insert(&child_process_list, &process->link);
296}
297
Benjamin Franzke06286262011-05-06 19:12:33 +0200298static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200299child_client_exec(int sockfd, const char *path)
300{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500301 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200302 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200303 sigset_t allsigs;
304
305 /* do not give our signal mask to the new process */
306 sigfillset(&allsigs);
307 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200308
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100309 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
310 if (seteuid(getuid()) == -1) {
311 weston_log("compositor: failed seteuid\n");
312 return;
313 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500314
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500315 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
316 * non-CLOEXEC fd to pass through exec. */
317 clientfd = dup(sockfd);
318 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200319 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500320 return;
321 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200322
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500323 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200324 setenv("WAYLAND_SOCKET", s, 1);
325
326 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200327 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200328 path);
329}
330
331WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500332weston_client_launch(struct weston_compositor *compositor,
333 struct weston_process *proc,
334 const char *path,
335 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200336{
337 int sv[2];
338 pid_t pid;
339 struct wl_client *client;
340
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300341 weston_log("launching '%s'\n", path);
342
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300343 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200344 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200345 "socketpair failed while launching '%s': %m\n",
346 path);
347 return NULL;
348 }
349
350 pid = fork();
351 if (pid == -1) {
352 close(sv[0]);
353 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200354 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200355 "fork failed while launching '%s': %m\n", path);
356 return NULL;
357 }
358
359 if (pid == 0) {
360 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700361 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200362 }
363
364 close(sv[1]);
365
366 client = wl_client_create(compositor->wl_display, sv[0]);
367 if (!client) {
368 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200369 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200370 "wl_client_create failed while launching '%s'.\n",
371 path);
372 return NULL;
373 }
374
375 proc->pid = pid;
376 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500377 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200378
379 return client;
380}
381
Pekka Paalanen9c1ac7b2014-08-27 12:03:38 +0300382struct process_info {
383 struct weston_process proc;
384 char *path;
385};
386
387static void
388process_handle_sigchld(struct weston_process *process, int status)
389{
390 struct process_info *pinfo =
391 container_of(process, struct process_info, proc);
392
393 /*
394 * There are no guarantees whether this runs before or after
395 * the wl_client destructor.
396 */
397
398 if (WIFEXITED(status)) {
399 weston_log("%s exited with status %d\n", pinfo->path,
400 WEXITSTATUS(status));
401 } else if (WIFSIGNALED(status)) {
402 weston_log("%s died on signal %d\n", pinfo->path,
403 WTERMSIG(status));
404 } else {
405 weston_log("%s disappeared\n", pinfo->path);
406 }
407
408 free(pinfo->path);
409 free(pinfo);
410}
411
412WL_EXPORT struct wl_client *
413weston_client_start(struct weston_compositor *compositor, const char *path)
414{
415 struct process_info *pinfo;
416 struct wl_client *client;
417
418 pinfo = zalloc(sizeof *pinfo);
419 if (!pinfo)
420 return NULL;
421
422 pinfo->path = strdup(path);
423 if (!pinfo->path)
424 goto out_free;
425
426 client = weston_client_launch(compositor, &pinfo->proc, path,
427 process_handle_sigchld);
428 if (!client)
429 goto out_str;
430
431 return client;
432
433out_str:
434 free(pinfo->path);
435
436out_free:
437 free(pinfo);
438
439 return NULL;
440}
441
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200442static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300443region_init_infinite(pixman_region32_t *region)
444{
445 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
446 UINT32_MAX, UINT32_MAX);
447}
448
Pekka Paalanene67858b2013-04-25 13:57:42 +0300449static struct weston_subsurface *
450weston_surface_to_subsurface(struct weston_surface *surface);
451
Jason Ekstranda7af7042013-10-12 22:38:11 -0500452WL_EXPORT struct weston_view *
453weston_view_create(struct weston_surface *surface)
454{
455 struct weston_view *view;
456
Bryce Harringtonde16d892014-11-20 22:21:57 -0800457 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500458 if (view == NULL)
459 return NULL;
460
461 view->surface = surface;
462
Jason Ekstranda7af7042013-10-12 22:38:11 -0500463 /* Assign to surface */
464 wl_list_insert(&surface->views, &view->surface_link);
465
466 wl_signal_init(&view->destroy_signal);
467 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300468 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500469
Jason Ekstranda7af7042013-10-12 22:38:11 -0500470 pixman_region32_init(&view->clip);
471
472 view->alpha = 1.0;
473 pixman_region32_init(&view->transform.opaque);
474
475 wl_list_init(&view->geometry.transformation_list);
476 wl_list_insert(&view->geometry.transformation_list,
477 &view->transform.position.link);
478 weston_matrix_init(&view->transform.position.matrix);
479 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200480 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500481 pixman_region32_init(&view->transform.boundingbox);
482 view->transform.dirty = 1;
483
Jason Ekstranda7af7042013-10-12 22:38:11 -0500484 return view;
485}
486
Jason Ekstrand108865d2014-06-26 10:04:49 -0700487struct weston_frame_callback {
488 struct wl_resource *resource;
489 struct wl_list link;
490};
491
Pekka Paalanen133e4392014-09-23 22:08:46 -0400492struct weston_presentation_feedback {
493 struct wl_resource *resource;
494
495 /* XXX: could use just wl_resource_get_link() instead */
496 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200497
498 /* The per-surface feedback flags */
499 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400500};
501
502static void
503weston_presentation_feedback_discard(
504 struct weston_presentation_feedback *feedback)
505{
506 presentation_feedback_send_discarded(feedback->resource);
507 wl_resource_destroy(feedback->resource);
508}
509
510static void
511weston_presentation_feedback_discard_list(struct wl_list *list)
512{
513 struct weston_presentation_feedback *feedback, *tmp;
514
515 wl_list_for_each_safe(feedback, tmp, list, link)
516 weston_presentation_feedback_discard(feedback);
517}
518
519static void
520weston_presentation_feedback_present(
521 struct weston_presentation_feedback *feedback,
522 struct weston_output *output,
523 uint32_t refresh_nsec,
524 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200525 uint64_t seq,
526 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400527{
528 struct wl_client *client = wl_resource_get_client(feedback->resource);
529 struct wl_resource *o;
530 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400531
532 wl_resource_for_each(o, &output->resource_list) {
533 if (wl_resource_get_client(o) != client)
534 continue;
535
536 presentation_feedback_send_sync_output(feedback->resource, o);
537 }
538
539 secs = ts->tv_sec;
540 presentation_feedback_send_presented(feedback->resource,
541 secs >> 32, secs & 0xffffffff,
542 ts->tv_nsec,
543 refresh_nsec,
544 seq >> 32, seq & 0xffffffff,
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200545 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400546 wl_resource_destroy(feedback->resource);
547}
548
549static void
550weston_presentation_feedback_present_list(struct wl_list *list,
551 struct weston_output *output,
552 uint32_t refresh_nsec,
553 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200554 uint64_t seq,
555 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400556{
557 struct weston_presentation_feedback *feedback, *tmp;
558
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200559 assert(!(flags & PRESENTATION_FEEDBACK_INVALID) ||
560 wl_list_empty(list));
561
Pekka Paalanen133e4392014-09-23 22:08:46 -0400562 wl_list_for_each_safe(feedback, tmp, list, link)
563 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200564 refresh_nsec, ts, seq,
565 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400566}
567
Jason Ekstrand7b982072014-05-20 14:33:03 -0500568static void
569surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
570{
571 struct weston_surface_state *state =
572 container_of(listener, struct weston_surface_state,
573 buffer_destroy_listener);
574
575 state->buffer = NULL;
576}
577
578static void
579weston_surface_state_init(struct weston_surface_state *state)
580{
581 state->newly_attached = 0;
582 state->buffer = NULL;
583 state->buffer_destroy_listener.notify =
584 surface_state_handle_buffer_destroy;
585 state->sx = 0;
586 state->sy = 0;
587
588 pixman_region32_init(&state->damage);
589 pixman_region32_init(&state->opaque);
590 region_init_infinite(&state->input);
591
592 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400593 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500594
595 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
596 state->buffer_viewport.buffer.scale = 1;
597 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
598 state->buffer_viewport.surface.width = -1;
599 state->buffer_viewport.changed = 0;
600}
601
602static void
603weston_surface_state_fini(struct weston_surface_state *state)
604{
605 struct weston_frame_callback *cb, *next;
606
607 wl_list_for_each_safe(cb, next,
608 &state->frame_callback_list, link)
609 wl_resource_destroy(cb->resource);
610
Pekka Paalanen133e4392014-09-23 22:08:46 -0400611 weston_presentation_feedback_discard_list(&state->feedback_list);
612
Jason Ekstrand7b982072014-05-20 14:33:03 -0500613 pixman_region32_fini(&state->input);
614 pixman_region32_fini(&state->opaque);
615 pixman_region32_fini(&state->damage);
616
617 if (state->buffer)
618 wl_list_remove(&state->buffer_destroy_listener.link);
619 state->buffer = NULL;
620}
621
622static void
623weston_surface_state_set_buffer(struct weston_surface_state *state,
624 struct weston_buffer *buffer)
625{
626 if (state->buffer == buffer)
627 return;
628
629 if (state->buffer)
630 wl_list_remove(&state->buffer_destroy_listener.link);
631 state->buffer = buffer;
632 if (state->buffer)
633 wl_signal_add(&state->buffer->destroy_signal,
634 &state->buffer_destroy_listener);
635}
636
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500637WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500638weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500639{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500640 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400641
Bryce Harringtonde16d892014-11-20 22:21:57 -0800642 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400643 if (surface == NULL)
644 return NULL;
645
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500646 wl_signal_init(&surface->destroy_signal);
647
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500648 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200649 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400650
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200651 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
652 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200653 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
654 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500655
656 weston_surface_state_init(&surface->pending);
657
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400658 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500659 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300660 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400661
Jason Ekstranda7af7042013-10-12 22:38:11 -0500662 wl_list_init(&surface->views);
663
664 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400665 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500666
Pekka Paalanene67858b2013-04-25 13:57:42 +0300667 wl_list_init(&surface->subsurface_list);
668 wl_list_init(&surface->subsurface_list_pending);
669
Jason Ekstrand1e059042014-10-16 10:55:19 -0500670 weston_matrix_init(&surface->buffer_to_surface_matrix);
671 weston_matrix_init(&surface->surface_to_buffer_matrix);
672
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400673 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500674}
675
Alex Wu8811bf92012-02-28 18:07:54 +0800676WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500677weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200678 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500679{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100680 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500681}
682
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400683WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500684weston_view_to_global_float(struct weston_view *view,
685 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200686{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500687 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200688 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
689
Jason Ekstranda7af7042013-10-12 22:38:11 -0500690 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200691
692 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200693 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700694 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200695 v.f[3]);
696 *x = 0;
697 *y = 0;
698 return;
699 }
700
701 *x = v.f[0] / v.f[3];
702 *y = v.f[1] / v.f[3];
703 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500704 *x = sx + view->geometry.x;
705 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200706 }
707}
708
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500709WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200710weston_transformed_coord(int width, int height,
711 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200712 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200713 float sx, float sy, float *bx, float *by)
714{
715 switch (transform) {
716 case WL_OUTPUT_TRANSFORM_NORMAL:
717 default:
718 *bx = sx;
719 *by = sy;
720 break;
721 case WL_OUTPUT_TRANSFORM_FLIPPED:
722 *bx = width - sx;
723 *by = sy;
724 break;
725 case WL_OUTPUT_TRANSFORM_90:
726 *bx = height - sy;
727 *by = sx;
728 break;
729 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
730 *bx = height - sy;
731 *by = width - sx;
732 break;
733 case WL_OUTPUT_TRANSFORM_180:
734 *bx = width - sx;
735 *by = height - sy;
736 break;
737 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
738 *bx = sx;
739 *by = height - sy;
740 break;
741 case WL_OUTPUT_TRANSFORM_270:
742 *bx = sy;
743 *by = width - sx;
744 break;
745 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
746 *bx = sy;
747 *by = sx;
748 break;
749 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200750
751 *bx *= scale;
752 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200753}
754
755WL_EXPORT pixman_box32_t
756weston_transformed_rect(int width, int height,
757 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200758 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200759 pixman_box32_t rect)
760{
761 float x1, x2, y1, y2;
762
763 pixman_box32_t ret;
764
Alexander Larsson4ea95522013-05-22 14:41:37 +0200765 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200766 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200767 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200768 rect.x2, rect.y2, &x2, &y2);
769
770 if (x1 <= x2) {
771 ret.x1 = x1;
772 ret.x2 = x2;
773 } else {
774 ret.x1 = x2;
775 ret.x2 = x1;
776 }
777
778 if (y1 <= y2) {
779 ret.y1 = y1;
780 ret.y2 = y2;
781 } else {
782 ret.y1 = y2;
783 ret.y2 = y1;
784 }
785
786 return ret;
787}
788
789WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500790weston_transformed_region(int width, int height,
791 enum wl_output_transform transform,
792 int32_t scale,
793 pixman_region32_t *src, pixman_region32_t *dest)
794{
795 pixman_box32_t *src_rects, *dest_rects;
796 int nrects, i;
797
798 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
799 if (src != dest)
800 pixman_region32_copy(dest, src);
801 return;
802 }
803
804 src_rects = pixman_region32_rectangles(src, &nrects);
805 dest_rects = malloc(nrects * sizeof(*dest_rects));
806 if (!dest_rects)
807 return;
808
809 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
810 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
811 } else {
812 for (i = 0; i < nrects; i++) {
813 switch (transform) {
814 default:
815 case WL_OUTPUT_TRANSFORM_NORMAL:
816 dest_rects[i].x1 = src_rects[i].x1;
817 dest_rects[i].y1 = src_rects[i].y1;
818 dest_rects[i].x2 = src_rects[i].x2;
819 dest_rects[i].y2 = src_rects[i].y2;
820 break;
821 case WL_OUTPUT_TRANSFORM_90:
822 dest_rects[i].x1 = height - src_rects[i].y2;
823 dest_rects[i].y1 = src_rects[i].x1;
824 dest_rects[i].x2 = height - src_rects[i].y1;
825 dest_rects[i].y2 = src_rects[i].x2;
826 break;
827 case WL_OUTPUT_TRANSFORM_180:
828 dest_rects[i].x1 = width - src_rects[i].x2;
829 dest_rects[i].y1 = height - src_rects[i].y2;
830 dest_rects[i].x2 = width - src_rects[i].x1;
831 dest_rects[i].y2 = height - src_rects[i].y1;
832 break;
833 case WL_OUTPUT_TRANSFORM_270:
834 dest_rects[i].x1 = src_rects[i].y1;
835 dest_rects[i].y1 = width - src_rects[i].x2;
836 dest_rects[i].x2 = src_rects[i].y2;
837 dest_rects[i].y2 = width - src_rects[i].x1;
838 break;
839 case WL_OUTPUT_TRANSFORM_FLIPPED:
840 dest_rects[i].x1 = width - src_rects[i].x2;
841 dest_rects[i].y1 = src_rects[i].y1;
842 dest_rects[i].x2 = width - src_rects[i].x1;
843 dest_rects[i].y2 = src_rects[i].y2;
844 break;
845 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
846 dest_rects[i].x1 = height - src_rects[i].y2;
847 dest_rects[i].y1 = width - src_rects[i].x2;
848 dest_rects[i].x2 = height - src_rects[i].y1;
849 dest_rects[i].y2 = width - src_rects[i].x1;
850 break;
851 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
852 dest_rects[i].x1 = src_rects[i].x1;
853 dest_rects[i].y1 = height - src_rects[i].y2;
854 dest_rects[i].x2 = src_rects[i].x2;
855 dest_rects[i].y2 = height - src_rects[i].y1;
856 break;
857 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
858 dest_rects[i].x1 = src_rects[i].y1;
859 dest_rects[i].y1 = src_rects[i].x1;
860 dest_rects[i].x2 = src_rects[i].y2;
861 dest_rects[i].y2 = src_rects[i].x2;
862 break;
863 }
864 }
865 }
866
867 if (scale != 1) {
868 for (i = 0; i < nrects; i++) {
869 dest_rects[i].x1 *= scale;
870 dest_rects[i].x2 *= scale;
871 dest_rects[i].y1 *= scale;
872 dest_rects[i].y2 *= scale;
873 }
874 }
875
876 pixman_region32_clear(dest);
877 pixman_region32_init_rects(dest, dest_rects, nrects);
878 free(dest_rects);
879}
880
Jonny Lamb74130762013-11-26 18:19:46 +0100881static void
882scaler_surface_to_buffer(struct weston_surface *surface,
883 float sx, float sy, float *bx, float *by)
884{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200885 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200886 double src_width, src_height;
887 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200888
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200889 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
890 if (vp->surface.width == -1) {
891 *bx = sx;
892 *by = sy;
893 return;
894 }
Jonny Lamb74130762013-11-26 18:19:46 +0100895
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200896 src_x = 0.0;
897 src_y = 0.0;
898 src_width = surface->width_from_buffer;
899 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100900 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200901 src_x = wl_fixed_to_double(vp->buffer.src_x);
902 src_y = wl_fixed_to_double(vp->buffer.src_y);
903 src_width = wl_fixed_to_double(vp->buffer.src_width);
904 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100905 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200906
907 *bx = sx * src_width / surface->width + src_x;
908 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100909}
910
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500911WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200912weston_surface_to_buffer_float(struct weston_surface *surface,
913 float sx, float sy, float *bx, float *by)
914{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200915 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
916
Jonny Lamb74130762013-11-26 18:19:46 +0100917 /* first transform coordinates if the scaler is set */
918 scaler_surface_to_buffer(surface, sx, sy, bx, by);
919
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500920 weston_transformed_coord(surface->width_from_buffer,
921 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200922 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100923 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200924}
925
Alexander Larsson4ea95522013-05-22 14:41:37 +0200926WL_EXPORT void
927weston_surface_to_buffer(struct weston_surface *surface,
928 int sx, int sy, int *bx, int *by)
929{
930 float bxf, byf;
931
Jonny Lamb74130762013-11-26 18:19:46 +0100932 weston_surface_to_buffer_float(surface,
933 sx, sy, &bxf, &byf);
934
Alexander Larsson4ea95522013-05-22 14:41:37 +0200935 *bx = floorf(bxf);
936 *by = floorf(byf);
937}
938
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200939WL_EXPORT pixman_box32_t
940weston_surface_to_buffer_rect(struct weston_surface *surface,
941 pixman_box32_t rect)
942{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200943 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100944 float xf, yf;
945
946 /* first transform box coordinates if the scaler is set */
947 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
948 rect.x1 = floorf(xf);
949 rect.y1 = floorf(yf);
950
951 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
952 rect.x2 = floorf(xf);
953 rect.y2 = floorf(yf);
954
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500955 return weston_transformed_rect(surface->width_from_buffer,
956 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200957 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200958 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200959}
960
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200961/** Transform a region from surface coordinates to buffer coordinates
962 *
963 * \param surface The surface to fetch wl_viewport and buffer transformation
964 * from.
965 * \param surface_region[in] The region in surface coordinates.
966 * \param buffer_region[out] The region converted to buffer coordinates.
967 *
968 * Buffer_region must be init'd, but will be completely overwritten.
969 *
970 * Viewport and buffer transformations can only do translation, scaling,
971 * and rotations in 90-degree steps. Therefore the only loss in the
972 * conversion is coordinate flooring (rounding).
973 */
974WL_EXPORT void
975weston_surface_to_buffer_region(struct weston_surface *surface,
976 pixman_region32_t *surface_region,
977 pixman_region32_t *buffer_region)
978{
979 pixman_box32_t *src_rects, *dest_rects;
980 int nrects, i;
981
982 src_rects = pixman_region32_rectangles(surface_region, &nrects);
983 dest_rects = malloc(nrects * sizeof(*dest_rects));
984 if (!dest_rects)
985 return;
986
987 for (i = 0; i < nrects; i++) {
988 dest_rects[i] = weston_surface_to_buffer_rect(surface,
989 src_rects[i]);
990 }
991
992 pixman_region32_fini(buffer_region);
993 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
994 free(dest_rects);
995}
996
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200997WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500998weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400999 struct weston_plane *plane)
1000{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001001 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001002 return;
1003
Jason Ekstranda7af7042013-10-12 22:38:11 -05001004 weston_view_damage_below(view);
1005 view->plane = plane;
1006 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001007}
1008
Pekka Paalanen51723d52015-02-17 13:10:01 +02001009/** Inflict damage on the plane where the view is visible.
1010 *
1011 * \param view The view that causes the damage.
1012 *
1013 * If the view is currently on a plane (including the primary plane),
1014 * take the view's boundingbox, subtract all the opaque views that cover it,
1015 * and add the remaining region as damage to the plane. This corresponds
1016 * to the damage inflicted to the plane if this view disappeared.
1017 *
1018 * A repaint is scheduled for this view.
1019 *
1020 * The region of all opaque views covering this view is stored in
1021 * weston_view::clip and updated by view_accumulate_damage() during
1022 * weston_output_repaint(). Specifically, that region matches the
1023 * scenegraph as it was last painted.
1024 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001025WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001026weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001027{
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001028 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001029
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001030 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001031 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001032 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +08001033 if (view->plane)
1034 pixman_region32_union(&view->plane->damage,
1035 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001036 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001037 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001038}
1039
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001040static void
1041weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1042{
1043 uint32_t different = es->output_mask ^ mask;
1044 uint32_t entered = mask & different;
1045 uint32_t left = es->output_mask & different;
1046 struct weston_output *output;
1047 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001048 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001049
1050 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001051 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001052 return;
1053 if (different == 0)
1054 return;
1055
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001056 client = wl_resource_get_client(es->resource);
1057
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001058 wl_list_for_each(output, &es->compositor->output_list, link) {
1059 if (1 << output->id & different)
1060 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001061 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001062 client);
1063 if (resource == NULL)
1064 continue;
1065 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001066 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001067 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001068 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001069 }
1070}
1071
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001072
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001073static void
1074weston_surface_assign_output(struct weston_surface *es)
1075{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001076 struct weston_output *new_output;
1077 struct weston_view *view;
1078 pixman_region32_t region;
1079 uint32_t max, area, mask;
1080 pixman_box32_t *e;
1081
1082 new_output = NULL;
1083 max = 0;
1084 mask = 0;
1085 pixman_region32_init(&region);
1086 wl_list_for_each(view, &es->views, surface_link) {
1087 if (!view->output)
1088 continue;
1089
1090 pixman_region32_intersect(&region, &view->transform.boundingbox,
1091 &view->output->region);
1092
1093 e = pixman_region32_extents(&region);
1094 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1095
1096 mask |= view->output_mask;
1097
1098 if (area >= max) {
1099 new_output = view->output;
1100 max = area;
1101 }
1102 }
1103 pixman_region32_fini(&region);
1104
1105 es->output = new_output;
1106 weston_surface_update_output_mask(es, mask);
1107}
1108
1109static void
1110weston_view_assign_output(struct weston_view *ev)
1111{
1112 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001113 struct weston_output *output, *new_output;
1114 pixman_region32_t region;
1115 uint32_t max, area, mask;
1116 pixman_box32_t *e;
1117
1118 new_output = NULL;
1119 max = 0;
1120 mask = 0;
1121 pixman_region32_init(&region);
1122 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001123 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001124 &output->region);
1125
1126 e = pixman_region32_extents(&region);
1127 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1128
1129 if (area > 0)
1130 mask |= 1 << output->id;
1131
1132 if (area >= max) {
1133 new_output = output;
1134 max = area;
1135 }
1136 }
1137 pixman_region32_fini(&region);
1138
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139 ev->output = new_output;
1140 ev->output_mask = mask;
1141
1142 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001143}
1144
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001145static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001146weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1147 int from_x, int from_y, int *to_x, int *to_y)
1148{
1149 float x, y;
1150
1151 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1152 weston_view_from_global_float(to, x, y, &x, &y);
1153
1154 *to_x = round(x);
1155 *to_y = round(y);
1156}
1157
1158static void
1159weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1160{
1161 pixman_box32_t *a;
1162 pixman_box32_t b;
1163
1164 a = pixman_region32_extents(&from->geometry.scissor);
1165
1166 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1167 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1168
1169 pixman_region32_fini(&to->geometry.scissor);
1170 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1171}
1172
1173static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001174view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001175 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001176{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001177 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1178 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001179 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001180 { inbox->x1, inbox->y1 },
1181 { inbox->x1, inbox->y2 },
1182 { inbox->x2, inbox->y1 },
1183 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001184 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001185 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001186 int i;
1187
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001188 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001189 /* avoid rounding empty bbox to 1x1 */
1190 pixman_region32_init(bbox);
1191 return;
1192 }
1193
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001194 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001195 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001196 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001197 if (x < min_x)
1198 min_x = x;
1199 if (x > max_x)
1200 max_x = x;
1201 if (y < min_y)
1202 min_y = y;
1203 if (y > max_y)
1204 max_y = y;
1205 }
1206
Pekka Paalanen219b9822012-02-08 15:38:37 +02001207 int_x = floorf(min_x);
1208 int_y = floorf(min_y);
1209 pixman_region32_init_rect(bbox, int_x, int_y,
1210 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001211}
1212
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001213static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001215{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001216 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001217
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001218 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001219 view->geometry.x = roundf(view->geometry.x);
1220 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001221
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001222 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1224 view->transform.position.matrix.d[12] = view->geometry.x;
1225 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001226
Jason Ekstranda7af7042013-10-12 22:38:11 -05001227 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001228
Jason Ekstranda7af7042013-10-12 22:38:11 -05001229 view->transform.inverse = view->transform.position.matrix;
1230 view->transform.inverse.d[12] = -view->geometry.x;
1231 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001232
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001234 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001235 view->surface->width,
1236 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001237 if (view->geometry.scissor_enabled)
1238 pixman_region32_intersect(&view->transform.boundingbox,
1239 &view->transform.boundingbox,
1240 &view->geometry.scissor);
1241
1242 pixman_region32_translate(&view->transform.boundingbox,
1243 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001244
Jason Ekstranda7af7042013-10-12 22:38:11 -05001245 if (view->alpha == 1.0) {
1246 pixman_region32_copy(&view->transform.opaque,
1247 &view->surface->opaque);
1248 pixman_region32_translate(&view->transform.opaque,
1249 view->geometry.x,
1250 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001251 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001252}
1253
1254static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001255weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001256{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001257 struct weston_view *parent = view->geometry.parent;
1258 struct weston_matrix *matrix = &view->transform.matrix;
1259 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001260 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001261 pixman_region32_t surfregion;
1262 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001263
Jason Ekstranda7af7042013-10-12 22:38:11 -05001264 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001265
1266 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001267 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1268 view->transform.position.matrix.d[12] = view->geometry.x;
1269 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001270
1271 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001273 weston_matrix_multiply(matrix, &tform->matrix);
1274
Pekka Paalanen483243f2013-03-08 14:56:50 +02001275 if (parent)
1276 weston_matrix_multiply(matrix, &parent->transform.matrix);
1277
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001278 if (weston_matrix_invert(inverse, matrix) < 0) {
1279 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001280 weston_log("error: weston_view %p"
1281 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001282 return -1;
1283 }
1284
Pekka Paalanen380adf52015-02-16 14:39:11 +02001285 pixman_region32_init_rect(&surfregion, 0, 0,
1286 view->surface->width, view->surface->height);
1287 if (view->geometry.scissor_enabled)
1288 pixman_region32_intersect(&surfregion, &surfregion,
1289 &view->geometry.scissor);
1290 surfbox = pixman_region32_extents(&surfregion);
1291
1292 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1293 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001294
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001295 return 0;
1296}
1297
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001298static struct weston_layer *
1299get_view_layer(struct weston_view *view)
1300{
1301 if (view->parent_view)
1302 return get_view_layer(view->parent_view);
1303 return view->layer_link.layer;
1304}
1305
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001306WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001307weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001308{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001310 struct weston_layer *layer;
1311 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001312
Jason Ekstranda7af7042013-10-12 22:38:11 -05001313 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001314 return;
1315
Pekka Paalanen483243f2013-03-08 14:56:50 +02001316 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001317 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001318
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001320
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001322
Jason Ekstranda7af7042013-10-12 22:38:11 -05001323 pixman_region32_fini(&view->transform.boundingbox);
1324 pixman_region32_fini(&view->transform.opaque);
1325 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001326
Pekka Paalanencd403622012-01-25 13:37:39 +02001327 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328 if (view->geometry.transformation_list.next ==
1329 &view->transform.position.link &&
1330 view->geometry.transformation_list.prev ==
1331 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001332 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001333 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001334 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335 if (weston_view_update_transform_enable(view) < 0)
1336 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001337 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001338
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001339 layer = get_view_layer(view);
1340 if (layer) {
1341 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001342 pixman_region32_intersect(&view->transform.boundingbox,
1343 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001344 pixman_region32_intersect(&view->transform.opaque,
1345 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001346 pixman_region32_fini(&mask);
1347 }
1348
Pekka Paalanen380adf52015-02-16 14:39:11 +02001349 if (parent) {
1350 if (parent->geometry.scissor_enabled) {
1351 view->geometry.scissor_enabled = true;
1352 weston_view_transfer_scissor(parent, view);
1353 } else {
1354 view->geometry.scissor_enabled = false;
1355 }
1356 }
1357
Jason Ekstranda7af7042013-10-12 22:38:11 -05001358 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001359
Jason Ekstranda7af7042013-10-12 22:38:11 -05001360 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001361
Jason Ekstranda7af7042013-10-12 22:38:11 -05001362 wl_signal_emit(&view->surface->compositor->transform_signal,
1363 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001364}
1365
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001366WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001367weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001368{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001369 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001370
1371 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001372 * The invariant: if view->geometry.dirty, then all views
1373 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001374 * Corollary: if not parent->geometry.dirty, then all ancestors
1375 * are not dirty.
1376 */
1377
Jason Ekstranda7af7042013-10-12 22:38:11 -05001378 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001379 return;
1380
Jason Ekstranda7af7042013-10-12 22:38:11 -05001381 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001382
Jason Ekstranda7af7042013-10-12 22:38:11 -05001383 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001384 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001385 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001386}
1387
1388WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389weston_view_to_global_fixed(struct weston_view *view,
1390 wl_fixed_t vx, wl_fixed_t vy,
1391 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001392{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001393 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001394
Jason Ekstranda7af7042013-10-12 22:38:11 -05001395 weston_view_to_global_float(view,
1396 wl_fixed_to_double(vx),
1397 wl_fixed_to_double(vy),
1398 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001399 *x = wl_fixed_from_double(xf);
1400 *y = wl_fixed_from_double(yf);
1401}
1402
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001403WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001404weston_view_from_global_float(struct weston_view *view,
1405 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001406{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001407 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001408 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1409
Jason Ekstranda7af7042013-10-12 22:38:11 -05001410 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001411
1412 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001413 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001414 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001415 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416 *vx = 0;
1417 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001418 return;
1419 }
1420
Jason Ekstranda7af7042013-10-12 22:38:11 -05001421 *vx = v.f[0] / v.f[3];
1422 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001423 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001424 *vx = x - view->geometry.x;
1425 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001426 }
1427}
1428
1429WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001430weston_view_from_global_fixed(struct weston_view *view,
1431 wl_fixed_t x, wl_fixed_t y,
1432 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001433{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001434 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001435
Jason Ekstranda7af7042013-10-12 22:38:11 -05001436 weston_view_from_global_float(view,
1437 wl_fixed_to_double(x),
1438 wl_fixed_to_double(y),
1439 &vxf, &vyf);
1440 *vx = wl_fixed_from_double(vxf);
1441 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001442}
1443
1444WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001445weston_view_from_global(struct weston_view *view,
1446 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001447{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001449
Jason Ekstranda7af7042013-10-12 22:38:11 -05001450 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1451 *vx = floorf(vxf);
1452 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001453}
1454
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001455WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001456weston_surface_schedule_repaint(struct weston_surface *surface)
1457{
1458 struct weston_output *output;
1459
1460 wl_list_for_each(output, &surface->compositor->output_list, link)
1461 if (surface->output_mask & (1 << output->id))
1462 weston_output_schedule_repaint(output);
1463}
1464
1465WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001466weston_view_schedule_repaint(struct weston_view *view)
1467{
1468 struct weston_output *output;
1469
1470 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1471 if (view->output_mask & (1 << output->id))
1472 weston_output_schedule_repaint(output);
1473}
1474
Pekka Paalanene508ce62015-02-19 13:59:55 +02001475/**
1476 * XXX: This function does it the wrong way.
1477 * surface->damage is the damage from the client, and causes
1478 * surface_flush_damage() to copy pixels. No window management action can
1479 * cause damage to the client-provided content, warranting re-upload!
1480 *
1481 * Instead of surface->damage, this function should record the damage
1482 * with all the views for this surface to avoid extraneous texture
1483 * uploads.
1484 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001485WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001486weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001487{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001488 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001489 0, 0, surface->width,
1490 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001491
Kristian Høgsberg98238702012-08-03 16:29:12 -04001492 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001493}
1494
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001495WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001496weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001497{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001498 if (view->geometry.x == x && view->geometry.y == y)
1499 return;
1500
Jason Ekstranda7af7042013-10-12 22:38:11 -05001501 view->geometry.x = x;
1502 view->geometry.y = y;
1503 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001504}
1505
Pekka Paalanen483243f2013-03-08 14:56:50 +02001506static void
1507transform_parent_handle_parent_destroy(struct wl_listener *listener,
1508 void *data)
1509{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001510 struct weston_view *view =
1511 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001512 geometry.parent_destroy_listener);
1513
Jason Ekstranda7af7042013-10-12 22:38:11 -05001514 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001515}
1516
1517WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001518weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001519 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001520{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001521 if (view->geometry.parent) {
1522 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1523 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001524
1525 if (!parent)
1526 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001527 }
1528
Jason Ekstranda7af7042013-10-12 22:38:11 -05001529 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001530
Jason Ekstranda7af7042013-10-12 22:38:11 -05001531 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001532 transform_parent_handle_parent_destroy;
1533 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001534 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001535 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001536 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001537 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001538 }
1539
Jason Ekstranda7af7042013-10-12 22:38:11 -05001540 weston_view_geometry_dirty(view);
1541}
1542
Pekka Paalanen380adf52015-02-16 14:39:11 +02001543/** Set a clip mask rectangle on a view
1544 *
1545 * \param view The view to set the clip mask on.
1546 * \param x Top-left corner X coordinate of the clip rectangle.
1547 * \param y Top-left corner Y coordinate of the clip rectangle.
1548 * \param width Width of the clip rectangle, non-negative.
1549 * \param height Height of the clip rectangle, non-negative.
1550 *
1551 * A shell may set a clip mask rectangle on a view. Everything outside
1552 * the rectangle is cut away for input and output purposes: it is
1553 * not drawn and cannot be hit by hit-test based input like pointer
1554 * motion or touch-downs. Everything inside the rectangle will behave
1555 * normally. Clients are unaware of clipping.
1556 *
1557 * The rectangle is set in the surface local coordinates. Setting a clip
1558 * mask rectangle does not affect the view position, the view is positioned
1559 * as it would be without a clip. The clip also does not change
1560 * weston_surface::width,height.
1561 *
1562 * The clip mask rectangle is part of transformation inheritance
1563 * (weston_view_set_transform_parent()). A clip set in the root of the
1564 * transformation inheritance tree will affect all views in the tree.
1565 * A clip can be set only on the root view. Attempting to set a clip
1566 * on view that has a transformation parent will fail. Assigning a parent
1567 * to a view that has a clip set will cause the clip to be forgotten.
1568 *
1569 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1570 * on the additional transformations in the child views. These transformations
1571 * may not rotate the coordinate axes, i.e., only translation and scaling
1572 * are allowed. Violating this restriction causes the clipping to malfunction.
1573 * Furthermore, using scaling may cause rounding errors in child clipping.
1574 *
1575 * The clip mask rectangle is not automatically adjusted based on
1576 * wl_surface.attach dx and dy arguments.
1577 *
1578 * A clip mask rectangle can be set only if the compositor capability
1579 * WESTON_CAP_VIEW_CLIP_MASK is present.
1580 *
1581 * This function sets the clip mask rectangle and schedules a repaint for
1582 * the view.
1583 */
1584WL_EXPORT void
1585weston_view_set_mask(struct weston_view *view,
1586 int x, int y, int width, int height)
1587{
1588 struct weston_compositor *compositor = view->surface->compositor;
1589
1590 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1591 weston_log("%s not allowed without capability!\n", __func__);
1592 return;
1593 }
1594
1595 if (view->geometry.parent) {
1596 weston_log("view %p has a parent, clip forbidden!\n", view);
1597 return;
1598 }
1599
1600 if (width < 0 || height < 0) {
1601 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1602 x, y, width, height);
1603 return;
1604 }
1605
1606 pixman_region32_fini(&view->geometry.scissor);
1607 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1608 view->geometry.scissor_enabled = true;
1609 weston_view_geometry_dirty(view);
1610 weston_view_schedule_repaint(view);
1611}
1612
1613/** Remove the clip mask from a view
1614 *
1615 * \param view The view to remove the clip mask from.
1616 *
1617 * Removed the clip mask rectangle and schedules a repaint.
1618 *
1619 * \sa weston_view_set_mask
1620 */
1621WL_EXPORT void
1622weston_view_set_mask_infinite(struct weston_view *view)
1623{
1624 view->geometry.scissor_enabled = false;
1625 weston_view_geometry_dirty(view);
1626 weston_view_schedule_repaint(view);
1627}
1628
Derek Foreman280e7dd2014-10-03 13:13:42 -05001629WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001630weston_view_is_mapped(struct weston_view *view)
1631{
1632 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001633 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001634 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001635 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001636}
1637
Derek Foreman280e7dd2014-10-03 13:13:42 -05001638WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001639weston_surface_is_mapped(struct weston_surface *surface)
1640{
1641 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001642 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001643 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001644 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001645}
1646
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001647static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001648surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001649{
1650 struct weston_view *view;
1651
1652 if (surface->width == width && surface->height == height)
1653 return;
1654
1655 surface->width = width;
1656 surface->height = height;
1657
1658 wl_list_for_each(view, &surface->views, surface_link)
1659 weston_view_geometry_dirty(view);
1660}
1661
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001662WL_EXPORT void
1663weston_surface_set_size(struct weston_surface *surface,
1664 int32_t width, int32_t height)
1665{
1666 assert(!surface->resource);
1667 surface_set_size(surface, width, height);
1668}
1669
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001670static int
1671fixed_round_up_to_int(wl_fixed_t f)
1672{
1673 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1674}
1675
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001676static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001677weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001678{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001679 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001680 int32_t width, height;
1681
1682 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001683 surface->width_from_buffer = 0;
1684 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001685 return;
1686 }
1687
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001688 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001689 case WL_OUTPUT_TRANSFORM_90:
1690 case WL_OUTPUT_TRANSFORM_270:
1691 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1692 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001693 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1694 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001695 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001696 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001697 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1698 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001699 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001700 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001701
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001702 surface->width_from_buffer = width;
1703 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001704}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001705
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001706static void
1707weston_surface_update_size(struct weston_surface *surface)
1708{
1709 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1710 int32_t width, height;
1711
1712 width = surface->width_from_buffer;
1713 height = surface->height_from_buffer;
1714
1715 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001716 surface_set_size(surface,
1717 vp->surface.width, vp->surface.height);
1718 return;
1719 }
1720
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001721 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001722 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1723 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1724
1725 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001726 return;
1727 }
1728
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001729 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001730}
1731
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001732WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001733weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001734{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001735 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001736
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001737 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001738
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001739 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001740}
1741
Jason Ekstranda7af7042013-10-12 22:38:11 -05001742WL_EXPORT struct weston_view *
1743weston_compositor_pick_view(struct weston_compositor *compositor,
1744 wl_fixed_t x, wl_fixed_t y,
1745 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001746{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001747 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001748 wl_fixed_t view_x, view_y;
1749 int view_ix, view_iy;
1750 int ix = wl_fixed_to_int(x);
1751 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001752
Jason Ekstranda7af7042013-10-12 22:38:11 -05001753 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001754 if (!pixman_region32_contains_point(
1755 &view->transform.boundingbox, ix, iy, NULL))
1756 continue;
1757
1758 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1759 view_ix = wl_fixed_to_int(view_x);
1760 view_iy = wl_fixed_to_int(view_y);
1761
1762 if (!pixman_region32_contains_point(&view->surface->input,
1763 view_ix, view_iy, NULL))
1764 continue;
1765
Pekka Paalanen380adf52015-02-16 14:39:11 +02001766 if (view->geometry.scissor_enabled &&
1767 !pixman_region32_contains_point(&view->geometry.scissor,
1768 view_ix, view_iy, NULL))
1769 continue;
1770
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001771 *vx = view_x;
1772 *vy = view_y;
1773 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001774 }
1775
1776 return NULL;
1777}
1778
1779static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001780weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001781{
Daniel Stone37816df2012-05-16 18:45:18 +01001782 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001783
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001784 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001785 return;
1786
Daniel Stone37816df2012-05-16 18:45:18 +01001787 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001788 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001789}
1790
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001791WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001792weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001793{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001794 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001795
Jason Ekstranda7af7042013-10-12 22:38:11 -05001796 if (!weston_view_is_mapped(view))
1797 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001798
Jason Ekstranda7af7042013-10-12 22:38:11 -05001799 weston_view_damage_below(view);
1800 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001801 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001802 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001803 wl_list_remove(&view->link);
1804 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001805 view->output_mask = 0;
1806 weston_surface_assign_output(view->surface);
1807
1808 if (weston_surface_is_mapped(view->surface))
1809 return;
1810
1811 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1812 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001813 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001814 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001815 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001816 NULL,
1817 wl_fixed_from_int(0),
1818 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001819 if (seat->touch && seat->touch->focus == view)
Derek Foreman4c93c082015-04-30 16:45:41 -05001820 weston_touch_set_focus(seat->touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001821 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001822}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001823
Jason Ekstranda7af7042013-10-12 22:38:11 -05001824WL_EXPORT void
1825weston_surface_unmap(struct weston_surface *surface)
1826{
1827 struct weston_view *view;
1828
1829 wl_list_for_each(view, &surface->views, surface_link)
1830 weston_view_unmap(view);
1831 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001832}
1833
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001834static void
1835weston_surface_reset_pending_buffer(struct weston_surface *surface)
1836{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001837 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001838 surface->pending.sx = 0;
1839 surface->pending.sy = 0;
1840 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001841 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001842}
1843
Jason Ekstranda7af7042013-10-12 22:38:11 -05001844WL_EXPORT void
1845weston_view_destroy(struct weston_view *view)
1846{
1847 wl_signal_emit(&view->destroy_signal, view);
1848
1849 assert(wl_list_empty(&view->geometry.child_list));
1850
1851 if (weston_view_is_mapped(view)) {
1852 weston_view_unmap(view);
1853 weston_compositor_build_view_list(view->surface->compositor);
1854 }
1855
1856 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001857 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001858
1859 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001860 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001861 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001862 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001863
1864 weston_view_set_transform_parent(view, NULL);
1865
Jason Ekstranda7af7042013-10-12 22:38:11 -05001866 wl_list_remove(&view->surface_link);
1867
1868 free(view);
1869}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001870
1871WL_EXPORT void
1872weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001873{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001874 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001875 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001876
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001877 if (--surface->ref_count > 0)
1878 return;
1879
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001880 assert(surface->resource == NULL);
1881
Pekka Paalanenca790762015-04-17 14:23:38 +03001882 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001883
Pekka Paalanene67858b2013-04-25 13:57:42 +03001884 assert(wl_list_empty(&surface->subsurface_list_pending));
1885 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001886
Jason Ekstranda7af7042013-10-12 22:38:11 -05001887 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1888 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001889
Jason Ekstrand7b982072014-05-20 14:33:03 -05001890 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001891
Pekka Paalanende685b82012-12-04 15:58:12 +02001892 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001893
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001894 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001895 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001896 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001897
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001898 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001899 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001900
Pekka Paalanen133e4392014-09-23 22:08:46 -04001901 weston_presentation_feedback_discard_list(&surface->feedback_list);
1902
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001903 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001904}
1905
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001906static void
1907destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001908{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001909 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001910
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001911 assert(surface);
1912
Giulio Camuffo0d379742013-11-15 22:06:15 +01001913 /* Set the resource to NULL, since we don't want to leave a
1914 * dangling pointer if the surface was refcounted and survives
1915 * the weston_surface_destroy() call. */
1916 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001917 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001918}
1919
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001920static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001921weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1922{
1923 struct weston_buffer *buffer =
1924 container_of(listener, struct weston_buffer, destroy_listener);
1925
1926 wl_signal_emit(&buffer->destroy_signal, buffer);
1927 free(buffer);
1928}
1929
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001930WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001931weston_buffer_from_resource(struct wl_resource *resource)
1932{
1933 struct weston_buffer *buffer;
1934 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001935
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001936 listener = wl_resource_get_destroy_listener(resource,
1937 weston_buffer_destroy_handler);
1938
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001939 if (listener)
1940 return container_of(listener, struct weston_buffer,
1941 destroy_listener);
1942
1943 buffer = zalloc(sizeof *buffer);
1944 if (buffer == NULL)
1945 return NULL;
1946
1947 buffer->resource = resource;
1948 wl_signal_init(&buffer->destroy_signal);
1949 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001950 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001951 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001952
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001953 return buffer;
1954}
1955
1956static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001957weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1958 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001959{
Pekka Paalanende685b82012-12-04 15:58:12 +02001960 struct weston_buffer_reference *ref =
1961 container_of(listener, struct weston_buffer_reference,
1962 destroy_listener);
1963
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001964 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001965 ref->buffer = NULL;
1966}
1967
1968WL_EXPORT void
1969weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001970 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001971{
1972 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001973 ref->buffer->busy_count--;
1974 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001975 assert(wl_resource_get_client(ref->buffer->resource));
1976 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001977 WL_BUFFER_RELEASE);
1978 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001979 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001980 }
1981
Pekka Paalanende685b82012-12-04 15:58:12 +02001982 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001983 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001984 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001985 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001986 }
1987
Pekka Paalanende685b82012-12-04 15:58:12 +02001988 ref->buffer = buffer;
1989 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1990}
1991
1992static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001993weston_surface_attach(struct weston_surface *surface,
1994 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001995{
1996 weston_buffer_reference(&surface->buffer_ref, buffer);
1997
Pekka Paalanena6421c42012-12-04 15:58:10 +02001998 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001999 if (weston_surface_is_mapped(surface))
2000 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002001 }
2002
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002003 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002004
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002005 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002006 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002007}
2008
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002009WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002010weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002011{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002012 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002013
2014 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002015 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002016}
2017
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002018WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002019weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002020{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002021 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002022
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002023 pixman_region32_union(&compositor->primary_plane.damage,
2024 &compositor->primary_plane.damage,
2025 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002026 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002027}
2028
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002029static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002030surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002031{
Pekka Paalanende685b82012-12-04 15:58:12 +02002032 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002033 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002034 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002035
Pekka Paalanenb5026542014-11-12 15:09:24 +02002036 if (weston_timeline_enabled_ &&
2037 pixman_region32_not_empty(&surface->damage))
2038 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2039 TLP_OUTPUT(surface->output), TLP_END);
2040
Jason Ekstrandef540082014-06-26 10:37:36 -07002041 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002042}
2043
2044static void
2045view_accumulate_damage(struct weston_view *view,
2046 pixman_region32_t *opaque)
2047{
2048 pixman_region32_t damage;
2049
2050 pixman_region32_init(&damage);
2051 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002052 pixman_box32_t *extents;
2053
Jason Ekstranda7af7042013-10-12 22:38:11 -05002054 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002055 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002056 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002057 pixman_region32_copy(&damage, &view->surface->damage);
2058 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002059 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002060 }
2061
Pekka Paalanen380adf52015-02-16 14:39:11 +02002062 pixman_region32_intersect(&damage, &damage,
2063 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002064 pixman_region32_subtract(&damage, &damage, opaque);
2065 pixman_region32_union(&view->plane->damage,
2066 &view->plane->damage, &damage);
2067 pixman_region32_fini(&damage);
2068 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002069 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002070}
2071
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002072static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002073compositor_accumulate_damage(struct weston_compositor *ec)
2074{
2075 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002076 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002077 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002078
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002079 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002080
2081 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002082 pixman_region32_copy(&plane->clip, &clip);
2083
2084 pixman_region32_init(&opaque);
2085
Jason Ekstranda7af7042013-10-12 22:38:11 -05002086 wl_list_for_each(ev, &ec->view_list, link) {
2087 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002088 continue;
2089
Jason Ekstranda7af7042013-10-12 22:38:11 -05002090 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002091 }
2092
2093 pixman_region32_union(&clip, &clip, &opaque);
2094 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002095 }
2096
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002097 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002098
Jason Ekstranda7af7042013-10-12 22:38:11 -05002099 wl_list_for_each(ev, &ec->view_list, link)
2100 ev->surface->touched = 0;
2101
2102 wl_list_for_each(ev, &ec->view_list, link) {
2103 if (ev->surface->touched)
2104 continue;
2105 ev->surface->touched = 1;
2106
2107 surface_flush_damage(ev->surface);
2108
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002109 /* Both the renderer and the backend have seen the buffer
2110 * by now. If renderer needs the buffer, it has its own
2111 * reference set. If the backend wants to keep the buffer
2112 * around for migrating the surface into a non-primary plane
2113 * later, keep_buffer is true. Otherwise, drop the core
2114 * reference now, and allow early buffer release. This enables
2115 * clients to use single-buffering.
2116 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002117 if (!ev->surface->keep_buffer)
2118 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002119 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002120}
2121
2122static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002123surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002124{
2125 struct weston_subsurface *sub;
2126
Pekka Paalanene67858b2013-04-25 13:57:42 +03002127 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002128 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002129 continue;
2130
Jason Ekstranda7af7042013-10-12 22:38:11 -05002131 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2132 wl_list_init(&sub->surface->views);
2133
2134 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002135 }
2136}
2137
2138static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002139surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002140{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002141 struct weston_subsurface *sub;
2142 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002143
Jason Ekstranda7af7042013-10-12 22:38:11 -05002144 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2145 if (sub->surface == surface)
2146 continue;
2147
George Kiagiadakised04d382014-06-13 18:10:26 +02002148 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2149 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002150 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002151 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002152
2153 surface_free_unused_subsurface_views(sub->surface);
2154 }
2155}
2156
2157static void
2158view_list_add_subsurface_view(struct weston_compositor *compositor,
2159 struct weston_subsurface *sub,
2160 struct weston_view *parent)
2161{
2162 struct weston_subsurface *child;
2163 struct weston_view *view = NULL, *iv;
2164
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002165 if (!weston_surface_is_mapped(sub->surface))
2166 return;
2167
Jason Ekstranda7af7042013-10-12 22:38:11 -05002168 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2169 if (iv->geometry.parent == parent) {
2170 view = iv;
2171 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002172 }
2173 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002174
2175 if (view) {
2176 /* Put it back in the surface's list of views */
2177 wl_list_remove(&view->surface_link);
2178 wl_list_insert(&sub->surface->views, &view->surface_link);
2179 } else {
2180 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002181 weston_view_set_position(view,
2182 sub->position.x,
2183 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002184 weston_view_set_transform_parent(view, parent);
2185 }
2186
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002187 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002188 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002189
Pekka Paalanenb188e912013-11-19 14:03:35 +02002190 if (wl_list_empty(&sub->surface->subsurface_list)) {
2191 wl_list_insert(compositor->view_list.prev, &view->link);
2192 return;
2193 }
2194
2195 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2196 if (child->surface == sub->surface)
2197 wl_list_insert(compositor->view_list.prev, &view->link);
2198 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002199 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002200 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002201}
2202
2203static void
2204view_list_add(struct weston_compositor *compositor,
2205 struct weston_view *view)
2206{
2207 struct weston_subsurface *sub;
2208
2209 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002210
Pekka Paalanenb188e912013-11-19 14:03:35 +02002211 if (wl_list_empty(&view->surface->subsurface_list)) {
2212 wl_list_insert(compositor->view_list.prev, &view->link);
2213 return;
2214 }
2215
2216 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2217 if (sub->surface == view->surface)
2218 wl_list_insert(compositor->view_list.prev, &view->link);
2219 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002220 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002221 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002222}
2223
2224static void
2225weston_compositor_build_view_list(struct weston_compositor *compositor)
2226{
2227 struct weston_view *view;
2228 struct weston_layer *layer;
2229
2230 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002231 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002232 surface_stash_subsurface_views(view->surface);
2233
2234 wl_list_init(&compositor->view_list);
2235 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002236 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002237 view_list_add(compositor, view);
2238 }
2239 }
2240
2241 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002242 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002243 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002244}
2245
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002246static void
2247weston_output_take_feedback_list(struct weston_output *output,
2248 struct weston_surface *surface)
2249{
2250 struct weston_view *view;
2251 struct weston_presentation_feedback *feedback;
2252 uint32_t flags = 0xffffffff;
2253
2254 if (wl_list_empty(&surface->feedback_list))
2255 return;
2256
2257 /* All views must have the flag for the flag to survive. */
2258 wl_list_for_each(view, &surface->views, surface_link) {
2259 /* ignore views that are not on this output at all */
2260 if (view->output_mask & (1u << output->id))
2261 flags &= view->psf_flags;
2262 }
2263
2264 wl_list_for_each(feedback, &surface->feedback_list, link)
2265 feedback->psf_flags = flags;
2266
2267 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2268 wl_list_init(&surface->feedback_list);
2269}
2270
David Herrmann1edf44c2013-10-22 17:11:26 +02002271static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002272weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002273{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002274 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002275 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002276 struct weston_animation *animation, *next;
2277 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002278 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002279 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002280 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002281
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002282 if (output->destroying)
2283 return 0;
2284
Pekka Paalanenb5026542014-11-12 15:09:24 +02002285 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2286
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002287 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002288 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002289
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002290 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002291 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002292 } else {
2293 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002294 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002295 ev->psf_flags = 0;
2296 }
2297 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002298
Pekka Paalanene67858b2013-04-25 13:57:42 +03002299 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002300 wl_list_for_each(ev, &ec->view_list, link) {
2301 /* Note: This operation is safe to do multiple times on the
2302 * same surface.
2303 */
2304 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002305 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002306 &ev->surface->frame_callback_list);
2307 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002308
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002309 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002310 }
2311 }
2312
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002313 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002314
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002315 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002316 pixman_region32_intersect(&output_damage,
2317 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002318 pixman_region32_subtract(&output_damage,
2319 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002320
Scott Moreauccbf29d2012-02-22 14:21:41 -07002321 if (output->dirty)
2322 weston_output_update_matrix(output);
2323
David Herrmann1edf44c2013-10-22 17:11:26 +02002324 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002325
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002326 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002327
Kristian Høgsbergef044142011-06-21 15:02:12 -04002328 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002329
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002330 weston_compositor_repick(ec);
2331 wl_event_loop_dispatch(ec->input_loop, 0);
2332
Jonas Ådahldb773762012-06-13 00:01:21 +02002333 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002334 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002335 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002336 }
2337
Scott Moreaud64cf212012-06-08 19:40:54 -06002338 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002339 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002340 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002341 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002342
Pekka Paalanenb5026542014-11-12 15:09:24 +02002343 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2344
David Herrmann1edf44c2013-10-22 17:11:26 +02002345 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002346}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002347
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002348static int
2349weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002350{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002351 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002352
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002353 wl_event_loop_dispatch(compositor->input_loop, 0);
2354
2355 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002356}
2357
Pekka Paalanen82919792014-05-21 13:51:49 +03002358static void
2359weston_output_schedule_repaint_reset(struct weston_output *output)
2360{
2361 struct weston_compositor *compositor = output->compositor;
2362 struct wl_event_loop *loop;
2363 int fd;
2364
2365 output->repaint_scheduled = 0;
2366 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2367
2368 if (compositor->input_loop_source)
2369 return;
2370
2371 loop = wl_display_get_event_loop(compositor->wl_display);
2372 fd = wl_event_loop_get_fd(compositor->input_loop);
2373 compositor->input_loop_source =
2374 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2375 weston_compositor_read_input, compositor);
2376}
2377
Pekka Paalanen0513a952014-05-21 16:17:27 +03002378static int
2379output_repaint_timer_handler(void *data)
2380{
2381 struct weston_output *output = data;
2382 struct weston_compositor *compositor = output->compositor;
2383
2384 if (output->repaint_needed &&
2385 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2386 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2387 weston_output_repaint(output) == 0)
2388 return 0;
2389
2390 weston_output_schedule_repaint_reset(output);
2391
2392 return 0;
2393}
2394
Kristian Høgsbergef044142011-06-21 15:02:12 -04002395WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002396weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002397 const struct timespec *stamp,
2398 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002399{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002400 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002401 int32_t refresh_nsec;
2402 struct timespec now;
2403 struct timespec gone;
2404 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002405
Pekka Paalanenb5026542014-11-12 15:09:24 +02002406 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2407 TLP_VBLANK(stamp), TLP_END);
2408
Pekka Paalanen0513a952014-05-21 16:17:27 +03002409 refresh_nsec = 1000000000000LL / output->current_mode->refresh;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002410 weston_presentation_feedback_present_list(&output->feedback_list,
2411 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002412 output->msc,
2413 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002414
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002415 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002416
Pekka Paalanen0513a952014-05-21 16:17:27 +03002417 weston_compositor_read_presentation_clock(compositor, &now);
2418 timespec_sub(&gone, &now, stamp);
2419 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2420 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002421
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002422 if (msec < -1000 || msec > 1000) {
2423 static bool warned;
2424
2425 if (!warned)
2426 weston_log("Warning: computed repaint delay is "
2427 "insane: %d msec\n", msec);
2428 warned = true;
2429
2430 msec = 0;
2431 }
2432
2433 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002434 output_repaint_timer_handler(output);
2435 else
2436 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002437}
2438
2439static void
2440idle_repaint(void *data)
2441{
2442 struct weston_output *output = data;
2443
Jonas Ådahle5a12252013-04-05 23:07:11 +02002444 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002445}
2446
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002447WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002448weston_layer_entry_insert(struct weston_layer_entry *list,
2449 struct weston_layer_entry *entry)
2450{
2451 wl_list_insert(&list->link, &entry->link);
2452 entry->layer = list->layer;
2453}
2454
2455WL_EXPORT void
2456weston_layer_entry_remove(struct weston_layer_entry *entry)
2457{
2458 wl_list_remove(&entry->link);
2459 wl_list_init(&entry->link);
2460 entry->layer = NULL;
2461}
2462
2463WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002464weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2465{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002466 wl_list_init(&layer->view_list.link);
2467 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002468 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002469 if (below != NULL)
2470 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002471}
2472
2473WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002474weston_layer_set_mask(struct weston_layer *layer,
2475 int x, int y, int width, int height)
2476{
2477 struct weston_view *view;
2478
2479 layer->mask.x1 = x;
2480 layer->mask.x2 = x + width;
2481 layer->mask.y1 = y;
2482 layer->mask.y2 = y + height;
2483
2484 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2485 weston_view_geometry_dirty(view);
2486 }
2487}
2488
2489WL_EXPORT void
2490weston_layer_set_mask_infinite(struct weston_layer *layer)
2491{
2492 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2493 UINT32_MAX, UINT32_MAX);
2494}
2495
2496WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002497weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002498{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002499 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002500 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002501
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002502 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2503 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002504 return;
2505
Pekka Paalanenb5026542014-11-12 15:09:24 +02002506 if (!output->repaint_needed)
2507 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2508
Kristian Høgsbergef044142011-06-21 15:02:12 -04002509 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002510 output->repaint_needed = 1;
2511 if (output->repaint_scheduled)
2512 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002513
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002514 wl_event_loop_add_idle(loop, idle_repaint, output);
2515 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002516 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2517
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002518
2519 if (compositor->input_loop_source) {
2520 wl_event_source_remove(compositor->input_loop_source);
2521 compositor->input_loop_source = NULL;
2522 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002523}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002524
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002525WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002526weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2527{
2528 struct weston_output *output;
2529
2530 wl_list_for_each(output, &compositor->output_list, link)
2531 weston_output_schedule_repaint(output);
2532}
2533
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002534static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002535surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002536{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002537 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002538}
2539
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002540static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002541surface_attach(struct wl_client *client,
2542 struct wl_resource *resource,
2543 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2544{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002545 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002546 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002547
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002548 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002549 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002550 if (buffer == NULL) {
2551 wl_client_post_no_memory(client);
2552 return;
2553 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002554 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002555
Pekka Paalanende685b82012-12-04 15:58:12 +02002556 /* Attach, attach, without commit in between does not send
2557 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002558 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002559
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002560 surface->pending.sx = sx;
2561 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002562 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002563}
2564
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002565static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002566surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002567 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002568 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002569{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002570 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002571
Pekka Paalanen8e159182012-10-10 12:49:25 +03002572 pixman_region32_union_rect(&surface->pending.damage,
2573 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002574 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002575}
2576
Kristian Høgsberg33418202011-08-16 23:01:28 -04002577static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002578destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002579{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002580 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002581
2582 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002583 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002584}
2585
2586static void
2587surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002588 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002589{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002590 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002591 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002592
2593 cb = malloc(sizeof *cb);
2594 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002595 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002596 return;
2597 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002598
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002599 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2600 callback);
2601 if (cb->resource == NULL) {
2602 free(cb);
2603 wl_resource_post_no_memory(resource);
2604 return;
2605 }
2606
Jason Ekstranda85118c2013-06-27 20:17:02 -05002607 wl_resource_set_implementation(cb->resource, NULL, cb,
2608 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002609
Pekka Paalanenbc106382012-10-10 12:49:31 +03002610 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002611}
2612
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002613static void
2614surface_set_opaque_region(struct wl_client *client,
2615 struct wl_resource *resource,
2616 struct wl_resource *region_resource)
2617{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002618 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002619 struct weston_region *region;
2620
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002621 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002622 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002623 pixman_region32_copy(&surface->pending.opaque,
2624 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002625 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002626 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002627 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002628}
2629
2630static void
2631surface_set_input_region(struct wl_client *client,
2632 struct wl_resource *resource,
2633 struct wl_resource *region_resource)
2634{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002635 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002636 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002637
2638 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002639 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002640 pixman_region32_copy(&surface->pending.input,
2641 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002642 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002643 pixman_region32_fini(&surface->pending.input);
2644 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002645 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002646}
2647
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002648static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002649weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002650{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002651 struct weston_subsurface *sub;
2652
2653 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2654 parent_link_pending) {
2655 wl_list_remove(&sub->parent_link);
2656 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2657 }
2658}
2659
2660static void
Jason Ekstrand1e059042014-10-16 10:55:19 -05002661weston_surface_build_buffer_matrix(struct weston_surface *surface,
2662 struct weston_matrix *matrix)
2663{
2664 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2665 double src_width, src_height, dest_width, dest_height;
2666
2667 weston_matrix_init(matrix);
2668
2669 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2670 src_width = surface->width_from_buffer;
2671 src_height = surface->height_from_buffer;
2672 } else {
2673 src_width = wl_fixed_to_double(vp->buffer.src_width);
2674 src_height = wl_fixed_to_double(vp->buffer.src_height);
2675 }
2676
2677 if (vp->surface.width == -1) {
2678 dest_width = src_width;
2679 dest_height = src_height;
2680 } else {
2681 dest_width = vp->surface.width;
2682 dest_height = vp->surface.height;
2683 }
2684
2685 if (src_width != dest_width || src_height != dest_height)
2686 weston_matrix_scale(matrix,
2687 src_width / dest_width,
2688 src_height / dest_height, 1);
2689
2690 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2691 weston_matrix_translate(matrix,
2692 wl_fixed_to_double(vp->buffer.src_x),
2693 wl_fixed_to_double(vp->buffer.src_y),
2694 0);
2695
2696 switch (vp->buffer.transform) {
2697 case WL_OUTPUT_TRANSFORM_FLIPPED:
2698 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2699 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2700 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2701 weston_matrix_scale(matrix, -1, 1, 1);
2702 weston_matrix_translate(matrix,
2703 surface->width_from_buffer, 0, 0);
2704 break;
2705 }
2706
2707 switch (vp->buffer.transform) {
2708 default:
2709 case WL_OUTPUT_TRANSFORM_NORMAL:
2710 case WL_OUTPUT_TRANSFORM_FLIPPED:
2711 break;
2712 case WL_OUTPUT_TRANSFORM_90:
2713 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2714 weston_matrix_rotate_xy(matrix, 0, 1);
2715 weston_matrix_translate(matrix,
2716 surface->height_from_buffer, 0, 0);
2717 break;
2718 case WL_OUTPUT_TRANSFORM_180:
2719 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2720 weston_matrix_rotate_xy(matrix, -1, 0);
2721 weston_matrix_translate(matrix,
2722 surface->width_from_buffer,
2723 surface->height_from_buffer, 0);
2724 break;
2725 case WL_OUTPUT_TRANSFORM_270:
2726 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2727 weston_matrix_rotate_xy(matrix, 0, -1);
2728 weston_matrix_translate(matrix,
2729 0, surface->width_from_buffer, 0);
2730 break;
2731 }
2732
2733 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2734}
2735
2736static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002737weston_surface_commit_state(struct weston_surface *surface,
2738 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002739{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002740 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002741 pixman_region32_t opaque;
2742
Alexander Larsson4ea95522013-05-22 14:41:37 +02002743 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002744 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002745 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002746 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002747
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002748 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002749 if (state->newly_attached)
2750 weston_surface_attach(surface, state->buffer);
2751 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002752
Jason Ekstrand1e059042014-10-16 10:55:19 -05002753 weston_surface_build_buffer_matrix(surface,
2754 &surface->surface_to_buffer_matrix);
2755 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2756 &surface->surface_to_buffer_matrix);
2757
Jason Ekstrand7b982072014-05-20 14:33:03 -05002758 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002759 weston_surface_update_size(surface);
2760 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002761 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002762 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002763
Jason Ekstrand7b982072014-05-20 14:33:03 -05002764 state->sx = 0;
2765 state->sy = 0;
2766 state->newly_attached = 0;
2767 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002768
2769 /* wl_surface.damage */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002770 if (weston_timeline_enabled_ &&
2771 pixman_region32_not_empty(&state->damage))
2772 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002773 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002774 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002775 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002776 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002777 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002778
2779 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002780 pixman_region32_init(&opaque);
2781 pixman_region32_intersect_rect(&opaque, &state->opaque,
2782 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002783
2784 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2785 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002786 wl_list_for_each(view, &surface->views, surface_link)
2787 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002788 }
2789
2790 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002791
2792 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002793 pixman_region32_intersect_rect(&surface->input, &state->input,
2794 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002795
Pekka Paalanenbc106382012-10-10 12:49:31 +03002796 /* wl_surface.frame */
2797 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002798 &state->frame_callback_list);
2799 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002800
2801 /* XXX:
2802 * What should happen with a feedback request, if there
2803 * is no wl_buffer attached for this commit?
2804 */
2805
2806 /* presentation.feedback */
2807 wl_list_insert_list(&surface->feedback_list,
2808 &state->feedback_list);
2809 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002810}
2811
2812static void
2813weston_surface_commit(struct weston_surface *surface)
2814{
2815 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002816
Pekka Paalanene67858b2013-04-25 13:57:42 +03002817 weston_surface_commit_subsurface_order(surface);
2818
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002819 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002820}
2821
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002822static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002823weston_subsurface_commit(struct weston_subsurface *sub);
2824
2825static void
2826weston_subsurface_parent_commit(struct weston_subsurface *sub,
2827 int parent_is_synchronized);
2828
2829static void
2830surface_commit(struct wl_client *client, struct wl_resource *resource)
2831{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002832 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002833 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2834
2835 if (sub) {
2836 weston_subsurface_commit(sub);
2837 return;
2838 }
2839
2840 weston_surface_commit(surface);
2841
2842 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2843 if (sub->surface != surface)
2844 weston_subsurface_parent_commit(sub, 0);
2845 }
2846}
2847
2848static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002849surface_set_buffer_transform(struct wl_client *client,
2850 struct wl_resource *resource, int transform)
2851{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002852 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002853
Jonny Lamba55f1392014-05-30 12:07:15 +02002854 /* if wl_output.transform grows more members this will need to be updated. */
2855 if (transform < 0 ||
2856 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2857 wl_resource_post_error(resource,
2858 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2859 "buffer transform must be a valid transform "
2860 "('%d' specified)", transform);
2861 return;
2862 }
2863
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002864 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002865 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002866}
2867
Alexander Larsson4ea95522013-05-22 14:41:37 +02002868static void
2869surface_set_buffer_scale(struct wl_client *client,
2870 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002871 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002872{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002873 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002874
Jonny Lamba55f1392014-05-30 12:07:15 +02002875 if (scale < 1) {
2876 wl_resource_post_error(resource,
2877 WL_SURFACE_ERROR_INVALID_SCALE,
2878 "buffer scale must be at least one "
2879 "('%d' specified)", scale);
2880 return;
2881 }
2882
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002883 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002884 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002885}
2886
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002887static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002888 surface_destroy,
2889 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002890 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002891 surface_frame,
2892 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002893 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002894 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002895 surface_set_buffer_transform,
2896 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002897};
2898
2899static void
2900compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002901 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002902{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002903 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002904 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002905
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002906 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002907 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002908 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002909 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002910 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002911
Jason Ekstranda85118c2013-06-27 20:17:02 -05002912 surface->resource =
2913 wl_resource_create(client, &wl_surface_interface,
2914 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002915 if (surface->resource == NULL) {
2916 weston_surface_destroy(surface);
2917 wl_resource_post_no_memory(resource);
2918 return;
2919 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002920 wl_resource_set_implementation(surface->resource, &surface_interface,
2921 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002922
2923 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002924}
2925
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002926static void
2927destroy_region(struct wl_resource *resource)
2928{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002929 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002930
2931 pixman_region32_fini(&region->region);
2932 free(region);
2933}
2934
2935static void
2936region_destroy(struct wl_client *client, struct wl_resource *resource)
2937{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002938 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002939}
2940
2941static void
2942region_add(struct wl_client *client, struct wl_resource *resource,
2943 int32_t x, int32_t y, int32_t width, int32_t height)
2944{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002945 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002946
2947 pixman_region32_union_rect(&region->region, &region->region,
2948 x, y, width, height);
2949}
2950
2951static void
2952region_subtract(struct wl_client *client, struct wl_resource *resource,
2953 int32_t x, int32_t y, int32_t width, int32_t height)
2954{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002955 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002956 pixman_region32_t rect;
2957
2958 pixman_region32_init_rect(&rect, x, y, width, height);
2959 pixman_region32_subtract(&region->region, &region->region, &rect);
2960 pixman_region32_fini(&rect);
2961}
2962
2963static const struct wl_region_interface region_interface = {
2964 region_destroy,
2965 region_add,
2966 region_subtract
2967};
2968
2969static void
2970compositor_create_region(struct wl_client *client,
2971 struct wl_resource *resource, uint32_t id)
2972{
2973 struct weston_region *region;
2974
2975 region = malloc(sizeof *region);
2976 if (region == NULL) {
2977 wl_resource_post_no_memory(resource);
2978 return;
2979 }
2980
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002981 pixman_region32_init(&region->region);
2982
Jason Ekstranda85118c2013-06-27 20:17:02 -05002983 region->resource =
2984 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002985 if (region->resource == NULL) {
2986 free(region);
2987 wl_resource_post_no_memory(resource);
2988 return;
2989 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002990 wl_resource_set_implementation(region->resource, &region_interface,
2991 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002992}
2993
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002994static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002995 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002996 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002997};
2998
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002999static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003000weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3001{
3002 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003003
Jason Ekstrand7b982072014-05-20 14:33:03 -05003004 weston_surface_commit_state(surface, &sub->cached);
3005 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003006
3007 weston_surface_commit_subsurface_order(surface);
3008
3009 weston_surface_schedule_repaint(surface);
3010
Jason Ekstrand7b982072014-05-20 14:33:03 -05003011 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003012}
3013
3014static void
3015weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3016{
3017 struct weston_surface *surface = sub->surface;
3018
3019 /*
3020 * If this commit would cause the surface to move by the
3021 * attach(dx, dy) parameters, the old damage region must be
3022 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02003023 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003024 */
3025 pixman_region32_translate(&sub->cached.damage,
3026 -surface->pending.sx, -surface->pending.sy);
3027 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
3028 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07003029 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003030
3031 if (surface->pending.newly_attached) {
3032 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003033 weston_surface_state_set_buffer(&sub->cached,
3034 surface->pending.buffer);
3035 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003036 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003037 weston_presentation_feedback_discard_list(
3038 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003039 }
3040 sub->cached.sx += surface->pending.sx;
3041 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003042
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003043 sub->cached.buffer_viewport.changed |=
3044 surface->pending.buffer_viewport.changed;
3045 sub->cached.buffer_viewport.buffer =
3046 surface->pending.buffer_viewport.buffer;
3047 sub->cached.buffer_viewport.surface =
3048 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003049
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003050 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003051
3052 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3053
3054 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3055
3056 wl_list_insert_list(&sub->cached.frame_callback_list,
3057 &surface->pending.frame_callback_list);
3058 wl_list_init(&surface->pending.frame_callback_list);
3059
Pekka Paalanen133e4392014-09-23 22:08:46 -04003060 wl_list_insert_list(&sub->cached.feedback_list,
3061 &surface->pending.feedback_list);
3062 wl_list_init(&surface->pending.feedback_list);
3063
Jason Ekstrand7b982072014-05-20 14:33:03 -05003064 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003065}
3066
Derek Foreman280e7dd2014-10-03 13:13:42 -05003067static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003068weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3069{
3070 while (sub) {
3071 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003072 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003073
3074 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003075 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003076
3077 sub = weston_surface_to_subsurface(sub->parent);
3078 }
3079
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003080 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003081}
3082
3083static void
3084weston_subsurface_commit(struct weston_subsurface *sub)
3085{
3086 struct weston_surface *surface = sub->surface;
3087 struct weston_subsurface *tmp;
3088
3089 /* Recursive check for effectively synchronized. */
3090 if (weston_subsurface_is_synchronized(sub)) {
3091 weston_subsurface_commit_to_cache(sub);
3092 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003093 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003094 /* flush accumulated state from cache */
3095 weston_subsurface_commit_to_cache(sub);
3096 weston_subsurface_commit_from_cache(sub);
3097 } else {
3098 weston_surface_commit(surface);
3099 }
3100
3101 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3102 if (tmp->surface != surface)
3103 weston_subsurface_parent_commit(tmp, 0);
3104 }
3105 }
3106}
3107
3108static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003109weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003110{
3111 struct weston_surface *surface = sub->surface;
3112 struct weston_subsurface *tmp;
3113
Pekka Paalanene67858b2013-04-25 13:57:42 +03003114 /* From now on, commit_from_cache the whole sub-tree, regardless of
3115 * the synchronized mode of each child. This sub-surface or some
3116 * of its ancestors were synchronized, so we are synchronized
3117 * all the way down.
3118 */
3119
Jason Ekstrand7b982072014-05-20 14:33:03 -05003120 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003121 weston_subsurface_commit_from_cache(sub);
3122
3123 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3124 if (tmp->surface != surface)
3125 weston_subsurface_parent_commit(tmp, 1);
3126 }
3127}
3128
3129static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003130weston_subsurface_parent_commit(struct weston_subsurface *sub,
3131 int parent_is_synchronized)
3132{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003133 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003134 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003135 wl_list_for_each(view, &sub->surface->views, surface_link)
3136 weston_view_set_position(view,
3137 sub->position.x,
3138 sub->position.y);
3139
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003140 sub->position.set = 0;
3141 }
3142
3143 if (parent_is_synchronized || sub->synchronized)
3144 weston_subsurface_synchronized_commit(sub);
3145}
3146
Pekka Paalanen8274d902014-08-06 19:36:51 +03003147static int
3148subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3149{
3150 return snprintf(buf, len, "sub-surface");
3151}
3152
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003153static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003154subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003155{
3156 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003157 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003158
Jason Ekstranda7af7042013-10-12 22:38:11 -05003159 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003160 weston_view_set_position(view,
3161 view->geometry.x + dx,
3162 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003163
3164 /* No need to check parent mappedness, because if parent is not
3165 * mapped, parent is not in a visible layer, so this sub-surface
3166 * will not be drawn either.
3167 */
3168 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003169 struct weston_output *output;
3170
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003171 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003172 * because that would call it also for the parent surface,
3173 * which might not be mapped yet. That would lead to
3174 * inconsistent state, where the window could never be
3175 * mapped.
3176 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003177 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003178 * weston_surface_is_mapped() return true, so that when the
3179 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003180 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003181 */
3182 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003183 output = container_of(compositor->output_list.next,
3184 struct weston_output, link);
3185
3186 surface->output = output;
3187 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003188 }
3189}
3190
3191static struct weston_subsurface *
3192weston_surface_to_subsurface(struct weston_surface *surface)
3193{
3194 if (surface->configure == subsurface_configure)
3195 return surface->configure_private;
3196
3197 return NULL;
3198}
3199
Pekka Paalanen01388e22013-04-25 13:57:44 +03003200WL_EXPORT struct weston_surface *
3201weston_surface_get_main_surface(struct weston_surface *surface)
3202{
3203 struct weston_subsurface *sub;
3204
3205 while (surface && (sub = weston_surface_to_subsurface(surface)))
3206 surface = sub->parent;
3207
3208 return surface;
3209}
3210
Pekka Paalanen50b67472014-10-01 15:02:41 +03003211WL_EXPORT int
3212weston_surface_set_role(struct weston_surface *surface,
3213 const char *role_name,
3214 struct wl_resource *error_resource,
3215 uint32_t error_code)
3216{
3217 assert(role_name);
3218
3219 if (surface->role_name == NULL ||
3220 surface->role_name == role_name ||
3221 strcmp(surface->role_name, role_name) == 0) {
3222 surface->role_name = role_name;
3223
3224 return 0;
3225 }
3226
3227 wl_resource_post_error(error_resource, error_code,
3228 "Cannot assign role %s to wl_surface@%d,"
3229 " already has role %s\n",
3230 role_name,
3231 wl_resource_get_id(surface->resource),
3232 surface->role_name);
3233 return -1;
3234}
3235
Pekka Paalanen8274d902014-08-06 19:36:51 +03003236WL_EXPORT void
3237weston_surface_set_label_func(struct weston_surface *surface,
3238 int (*desc)(struct weston_surface *,
3239 char *, size_t))
3240{
3241 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003242 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003243}
3244
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003245/** Get the size of surface contents
3246 *
3247 * \param surface The surface to query.
3248 * \param width Returns the width of raw contents.
3249 * \param height Returns the height of raw contents.
3250 *
3251 * Retrieves the raw surface content size in pixels for the given surface.
3252 * This is the whole content size in buffer pixels. If the surface
3253 * has no content or the renderer does not implement this feature,
3254 * zeroes are returned.
3255 *
3256 * This function is used to determine the buffer size needed for
3257 * a weston_surface_copy_content() call.
3258 */
3259WL_EXPORT void
3260weston_surface_get_content_size(struct weston_surface *surface,
3261 int *width, int *height)
3262{
3263 struct weston_renderer *rer = surface->compositor->renderer;
3264
3265 if (!rer->surface_get_content_size) {
3266 *width = 0;
3267 *height = 0;
3268 return;
3269 }
3270
3271 rer->surface_get_content_size(surface, width, height);
3272}
3273
3274/** Copy surface contents to system memory.
3275 *
3276 * \param surface The surface to copy from.
3277 * \param target Pointer to the target memory buffer.
3278 * \param size Size of the target buffer in bytes.
3279 * \param src_x X location on contents to copy from.
3280 * \param src_y Y location on contents to copy from.
3281 * \param width Width in pixels of the area to copy.
3282 * \param height Height in pixels of the area to copy.
3283 * \return 0 for success, -1 for failure.
3284 *
3285 * Surface contents are maintained by the renderer. They can be in a
3286 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3287 * else.
3288 *
3289 * Surface contents are copied into memory pointed to by target,
3290 * which has size bytes of space available. The target memory
3291 * may be larger than needed, but being smaller returns an error.
3292 * The extra bytes in target may or may not be written; their content is
3293 * unspecified. Size must be large enough to hold the image.
3294 *
3295 * The image in the target memory will be arranged in rows from
3296 * top to bottom, and pixels on a row from left to right. The pixel
3297 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3298 * width * 4.
3299 *
3300 * Parameters src_x and src_y define the upper-left corner in buffer
3301 * coordinates (pixels) to copy from. Parameters width and height
3302 * define the size of the area to copy in pixels.
3303 *
3304 * The rectangle defined by src_x, src_y, width, height must fit in
3305 * the surface contents. Otherwise an error is returned.
3306 *
3307 * Use surface_get_data_size to determine the content size; the
3308 * needed target buffer size and rectangle limits.
3309 *
3310 * CURRENT IMPLEMENTATION RESTRICTIONS:
3311 * - the machine must be little-endian due to Pixman formats.
3312 *
3313 * NOTE: Pixman formats are premultiplied.
3314 */
3315WL_EXPORT int
3316weston_surface_copy_content(struct weston_surface *surface,
3317 void *target, size_t size,
3318 int src_x, int src_y,
3319 int width, int height)
3320{
3321 struct weston_renderer *rer = surface->compositor->renderer;
3322 int cw, ch;
3323 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3324
3325 if (!rer->surface_copy_content)
3326 return -1;
3327
3328 weston_surface_get_content_size(surface, &cw, &ch);
3329
3330 if (src_x < 0 || src_y < 0)
3331 return -1;
3332
3333 if (width <= 0 || height <= 0)
3334 return -1;
3335
3336 if (src_x + width > cw || src_y + height > ch)
3337 return -1;
3338
3339 if (width * bytespp * height > size)
3340 return -1;
3341
3342 return rer->surface_copy_content(surface, target, size,
3343 src_x, src_y, width, height);
3344}
3345
Pekka Paalanene67858b2013-04-25 13:57:42 +03003346static void
3347subsurface_set_position(struct wl_client *client,
3348 struct wl_resource *resource, int32_t x, int32_t y)
3349{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003350 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003351
3352 if (!sub)
3353 return;
3354
3355 sub->position.x = x;
3356 sub->position.y = y;
3357 sub->position.set = 1;
3358}
3359
3360static struct weston_subsurface *
3361subsurface_from_surface(struct weston_surface *surface)
3362{
3363 struct weston_subsurface *sub;
3364
3365 sub = weston_surface_to_subsurface(surface);
3366 if (sub)
3367 return sub;
3368
3369 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3370 if (sub->surface == surface)
3371 return sub;
3372
3373 return NULL;
3374}
3375
3376static struct weston_subsurface *
3377subsurface_sibling_check(struct weston_subsurface *sub,
3378 struct weston_surface *surface,
3379 const char *request)
3380{
3381 struct weston_subsurface *sibling;
3382
3383 sibling = subsurface_from_surface(surface);
3384
3385 if (!sibling) {
3386 wl_resource_post_error(sub->resource,
3387 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3388 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003389 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003390 return NULL;
3391 }
3392
3393 if (sibling->parent != sub->parent) {
3394 wl_resource_post_error(sub->resource,
3395 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3396 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003397 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003398 return NULL;
3399 }
3400
3401 return sibling;
3402}
3403
3404static void
3405subsurface_place_above(struct wl_client *client,
3406 struct wl_resource *resource,
3407 struct wl_resource *sibling_resource)
3408{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003409 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003410 struct weston_surface *surface =
3411 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003412 struct weston_subsurface *sibling;
3413
3414 if (!sub)
3415 return;
3416
3417 sibling = subsurface_sibling_check(sub, surface, "place_above");
3418 if (!sibling)
3419 return;
3420
3421 wl_list_remove(&sub->parent_link_pending);
3422 wl_list_insert(sibling->parent_link_pending.prev,
3423 &sub->parent_link_pending);
3424}
3425
3426static void
3427subsurface_place_below(struct wl_client *client,
3428 struct wl_resource *resource,
3429 struct wl_resource *sibling_resource)
3430{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003431 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003432 struct weston_surface *surface =
3433 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003434 struct weston_subsurface *sibling;
3435
3436 if (!sub)
3437 return;
3438
3439 sibling = subsurface_sibling_check(sub, surface, "place_below");
3440 if (!sibling)
3441 return;
3442
3443 wl_list_remove(&sub->parent_link_pending);
3444 wl_list_insert(&sibling->parent_link_pending,
3445 &sub->parent_link_pending);
3446}
3447
3448static void
3449subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3450{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003451 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003452
3453 if (sub)
3454 sub->synchronized = 1;
3455}
3456
3457static void
3458subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3459{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003460 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003461
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003462 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003463 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003464
3465 /* If sub became effectively desynchronized, flush. */
3466 if (!weston_subsurface_is_synchronized(sub))
3467 weston_subsurface_synchronized_commit(sub);
3468 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003469}
3470
3471static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003472weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3473{
3474 wl_list_remove(&sub->parent_link);
3475 wl_list_remove(&sub->parent_link_pending);
3476 wl_list_remove(&sub->parent_destroy_listener.link);
3477 sub->parent = NULL;
3478}
3479
3480static void
3481weston_subsurface_destroy(struct weston_subsurface *sub);
3482
3483static void
3484subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3485{
3486 struct weston_subsurface *sub =
3487 container_of(listener, struct weston_subsurface,
3488 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003489 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003490
3491 /* The protocol object (wl_resource) is left inert. */
3492 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003493 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003494
3495 weston_subsurface_destroy(sub);
3496}
3497
3498static void
3499subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3500{
3501 struct weston_subsurface *sub =
3502 container_of(listener, struct weston_subsurface,
3503 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003504 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003505 assert(sub->surface != sub->parent);
3506
3507 if (weston_surface_is_mapped(sub->surface))
3508 weston_surface_unmap(sub->surface);
3509
3510 weston_subsurface_unlink_parent(sub);
3511}
3512
3513static void
3514subsurface_resource_destroy(struct wl_resource *resource)
3515{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003516 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003517
3518 if (sub)
3519 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003520}
3521
3522static void
3523subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3524{
3525 wl_resource_destroy(resource);
3526}
3527
3528static void
3529weston_subsurface_link_parent(struct weston_subsurface *sub,
3530 struct weston_surface *parent)
3531{
3532 sub->parent = parent;
3533 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003534 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003535 &sub->parent_destroy_listener);
3536
3537 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3538 wl_list_insert(&parent->subsurface_list_pending,
3539 &sub->parent_link_pending);
3540}
3541
3542static void
3543weston_subsurface_link_surface(struct weston_subsurface *sub,
3544 struct weston_surface *surface)
3545{
3546 sub->surface = surface;
3547 sub->surface_destroy_listener.notify =
3548 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003549 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003550 &sub->surface_destroy_listener);
3551}
3552
3553static void
3554weston_subsurface_destroy(struct weston_subsurface *sub)
3555{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003556 struct weston_view *view, *next;
3557
Pekka Paalanene67858b2013-04-25 13:57:42 +03003558 assert(sub->surface);
3559
3560 if (sub->resource) {
3561 assert(weston_surface_to_subsurface(sub->surface) == sub);
3562 assert(sub->parent_destroy_listener.notify ==
3563 subsurface_handle_parent_destroy);
3564
George Kiagiadakised04d382014-06-13 18:10:26 +02003565 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3566 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003567 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003568 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003569
Pekka Paalanene67858b2013-04-25 13:57:42 +03003570 if (sub->parent)
3571 weston_subsurface_unlink_parent(sub);
3572
Jason Ekstrand7b982072014-05-20 14:33:03 -05003573 weston_surface_state_fini(&sub->cached);
3574 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003575
3576 sub->surface->configure = NULL;
3577 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003578 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003579 } else {
3580 /* the dummy weston_subsurface for the parent itself */
3581 assert(sub->parent_destroy_listener.notify == NULL);
3582 wl_list_remove(&sub->parent_link);
3583 wl_list_remove(&sub->parent_link_pending);
3584 }
3585
3586 wl_list_remove(&sub->surface_destroy_listener.link);
3587 free(sub);
3588}
3589
3590static const struct wl_subsurface_interface subsurface_implementation = {
3591 subsurface_destroy,
3592 subsurface_set_position,
3593 subsurface_place_above,
3594 subsurface_place_below,
3595 subsurface_set_sync,
3596 subsurface_set_desync
3597};
3598
3599static struct weston_subsurface *
3600weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3601 struct weston_surface *parent)
3602{
3603 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003604 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003605
Bryce Harringtonde16d892014-11-20 22:21:57 -08003606 sub = zalloc(sizeof *sub);
3607 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003608 return NULL;
3609
Jason Ekstranda7af7042013-10-12 22:38:11 -05003610 wl_list_init(&sub->unused_views);
3611
Jason Ekstranda85118c2013-06-27 20:17:02 -05003612 sub->resource =
3613 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003614 if (!sub->resource) {
3615 free(sub);
3616 return NULL;
3617 }
3618
Jason Ekstranda85118c2013-06-27 20:17:02 -05003619 wl_resource_set_implementation(sub->resource,
3620 &subsurface_implementation,
3621 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003622 weston_subsurface_link_surface(sub, surface);
3623 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003624 weston_surface_state_init(&sub->cached);
3625 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003626 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003627
3628 return sub;
3629}
3630
3631/* Create a dummy subsurface for having the parent itself in its
3632 * sub-surface lists. Makes stacking order manipulation easy.
3633 */
3634static struct weston_subsurface *
3635weston_subsurface_create_for_parent(struct weston_surface *parent)
3636{
3637 struct weston_subsurface *sub;
3638
Bryce Harringtonde16d892014-11-20 22:21:57 -08003639 sub = zalloc(sizeof *sub);
3640 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003641 return NULL;
3642
3643 weston_subsurface_link_surface(sub, parent);
3644 sub->parent = parent;
3645 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3646 wl_list_insert(&parent->subsurface_list_pending,
3647 &sub->parent_link_pending);
3648
3649 return sub;
3650}
3651
3652static void
3653subcompositor_get_subsurface(struct wl_client *client,
3654 struct wl_resource *resource,
3655 uint32_t id,
3656 struct wl_resource *surface_resource,
3657 struct wl_resource *parent_resource)
3658{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003659 struct weston_surface *surface =
3660 wl_resource_get_user_data(surface_resource);
3661 struct weston_surface *parent =
3662 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003663 struct weston_subsurface *sub;
3664 static const char where[] = "get_subsurface: wl_subsurface@";
3665
3666 if (surface == parent) {
3667 wl_resource_post_error(resource,
3668 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3669 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003670 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003671 return;
3672 }
3673
3674 if (weston_surface_to_subsurface(surface)) {
3675 wl_resource_post_error(resource,
3676 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3677 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003678 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003679 return;
3680 }
3681
Pekka Paalanen50b67472014-10-01 15:02:41 +03003682 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3683 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003684 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003685
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003686 if (weston_surface_get_main_surface(parent) == surface) {
3687 wl_resource_post_error(resource,
3688 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3689 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003690 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003691 return;
3692 }
3693
Pekka Paalanene67858b2013-04-25 13:57:42 +03003694 /* make sure the parent is in its own list */
3695 if (wl_list_empty(&parent->subsurface_list)) {
3696 if (!weston_subsurface_create_for_parent(parent)) {
3697 wl_resource_post_no_memory(resource);
3698 return;
3699 }
3700 }
3701
3702 sub = weston_subsurface_create(id, surface, parent);
3703 if (!sub) {
3704 wl_resource_post_no_memory(resource);
3705 return;
3706 }
3707
3708 surface->configure = subsurface_configure;
3709 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003710 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003711}
3712
3713static void
3714subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3715{
3716 wl_resource_destroy(resource);
3717}
3718
3719static const struct wl_subcompositor_interface subcompositor_interface = {
3720 subcompositor_destroy,
3721 subcompositor_get_subsurface
3722};
3723
3724static void
3725bind_subcompositor(struct wl_client *client,
3726 void *data, uint32_t version, uint32_t id)
3727{
3728 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003729 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003730
Jason Ekstranda85118c2013-06-27 20:17:02 -05003731 resource =
3732 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003733 if (resource == NULL) {
3734 wl_client_post_no_memory(client);
3735 return;
3736 }
3737 wl_resource_set_implementation(resource, &subcompositor_interface,
3738 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003739}
3740
3741static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003742weston_compositor_dpms(struct weston_compositor *compositor,
3743 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003744{
3745 struct weston_output *output;
3746
3747 wl_list_for_each(output, &compositor->output_list, link)
3748 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003749 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003750}
3751
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003752WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003753weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003754{
Neil Roberts8b62e202013-09-30 13:14:47 +01003755 uint32_t old_state = compositor->state;
3756
3757 /* The state needs to be changed before emitting the wake
3758 * signal because that may try to schedule a repaint which
3759 * will not work if the compositor is still sleeping */
3760 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3761
3762 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003763 case WESTON_COMPOSITOR_SLEEPING:
3764 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3765 /* fall through */
3766 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003767 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003768 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003769 /* fall through */
3770 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003771 wl_event_source_timer_update(compositor->idle_source,
3772 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003773 }
3774}
3775
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003776WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003777weston_compositor_offscreen(struct weston_compositor *compositor)
3778{
3779 switch (compositor->state) {
3780 case WESTON_COMPOSITOR_OFFSCREEN:
3781 return;
3782 case WESTON_COMPOSITOR_SLEEPING:
3783 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3784 /* fall through */
3785 default:
3786 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3787 wl_event_source_timer_update(compositor->idle_source, 0);
3788 }
3789}
3790
3791WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003792weston_compositor_sleep(struct weston_compositor *compositor)
3793{
3794 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3795 return;
3796
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003797 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003798 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3799 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3800}
3801
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003802static int
3803idle_handler(void *data)
3804{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003805 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003806
3807 if (compositor->idle_inhibit)
3808 return 1;
3809
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003810 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003811 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003812
3813 return 1;
3814}
3815
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003816WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003817weston_plane_init(struct weston_plane *plane,
3818 struct weston_compositor *ec,
3819 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003820{
3821 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003822 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003823 plane->x = x;
3824 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003825 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003826
3827 /* Init the link so that the call to wl_list_remove() when releasing
3828 * the plane without ever stacking doesn't lead to a crash */
3829 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003830}
3831
3832WL_EXPORT void
3833weston_plane_release(struct weston_plane *plane)
3834{
Xiong Zhang97116532013-10-23 13:58:31 +08003835 struct weston_view *view;
3836
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003837 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003838 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003839
Xiong Zhang97116532013-10-23 13:58:31 +08003840 wl_list_for_each(view, &plane->compositor->view_list, link) {
3841 if (view->plane == plane)
3842 view->plane = NULL;
3843 }
3844
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003845 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003846}
3847
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003848WL_EXPORT void
3849weston_compositor_stack_plane(struct weston_compositor *ec,
3850 struct weston_plane *plane,
3851 struct weston_plane *above)
3852{
3853 if (above)
3854 wl_list_insert(above->link.prev, &plane->link);
3855 else
3856 wl_list_insert(&ec->plane_list, &plane->link);
3857}
3858
Casey Dahlin9074db52012-04-19 22:50:09 -04003859static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003860{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003861 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003862}
3863
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003864static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003865bind_output(struct wl_client *client,
3866 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003867{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003868 struct weston_output *output = data;
3869 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003870 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003871
Jason Ekstranda85118c2013-06-27 20:17:02 -05003872 resource = wl_resource_create(client, &wl_output_interface,
3873 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003874 if (resource == NULL) {
3875 wl_client_post_no_memory(client);
3876 return;
3877 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003878
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003879 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003880 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003881
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003882 wl_output_send_geometry(resource,
3883 output->x,
3884 output->y,
3885 output->mm_width,
3886 output->mm_height,
3887 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003888 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003889 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003890 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003891 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003892 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003893
3894 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003895 wl_output_send_mode(resource,
3896 mode->flags,
3897 mode->width,
3898 mode->height,
3899 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003900 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003901
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003902 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003903 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003904}
3905
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003906/* Move other outputs when one is removed so the space remains contiguos. */
3907static void
3908weston_compositor_remove_output(struct weston_compositor *compositor,
3909 struct weston_output *remove_output)
3910{
3911 struct weston_output *output;
3912 int offset = 0;
3913
3914 wl_list_for_each(output, &compositor->output_list, link) {
3915 if (output == remove_output) {
3916 offset = output->width;
3917 continue;
3918 }
3919
3920 if (offset > 0) {
3921 weston_output_move(output,
3922 output->x - offset, output->y);
3923 output->dirty = 1;
3924 }
3925 }
3926}
3927
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003928WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003929weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003930{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003931 struct wl_resource *resource;
3932
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003933 output->destroying = 1;
3934
Pekka Paalanen0513a952014-05-21 16:17:27 +03003935 wl_event_source_remove(output->repaint_timer);
3936
Pekka Paalanen133e4392014-09-23 22:08:46 -04003937 weston_presentation_feedback_discard_list(&output->feedback_list);
3938
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003939 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003940 wl_list_remove(&output->link);
3941
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003942 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003943 wl_signal_emit(&output->destroy_signal, output);
3944
Richard Hughesafe690c2013-05-02 10:10:04 +01003945 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003946 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003947 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003948 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003949
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003950 wl_resource_for_each(resource, &output->resource_list) {
3951 wl_resource_set_destructor(resource, NULL);
3952 }
3953
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003954 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003955}
3956
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003957WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003958weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003959{
Scott Moreau850ca422012-05-21 15:21:25 -06003960 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003961
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003962 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003963 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003964
Scott Moreauccbf29d2012-02-22 14:21:41 -07003965 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003966 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003967 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003968 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003969 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003970 weston_matrix_scale(&output->matrix, magnification,
3971 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003972 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003973
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003974 switch (output->transform) {
3975 case WL_OUTPUT_TRANSFORM_FLIPPED:
3976 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3977 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3978 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3979 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3980 weston_matrix_scale(&output->matrix, -1, 1, 1);
3981 break;
3982 }
3983
3984 switch (output->transform) {
3985 default:
3986 case WL_OUTPUT_TRANSFORM_NORMAL:
3987 case WL_OUTPUT_TRANSFORM_FLIPPED:
3988 break;
3989 case WL_OUTPUT_TRANSFORM_90:
3990 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3991 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
3992 weston_matrix_rotate_xy(&output->matrix, 0, 1);
3993 break;
3994 case WL_OUTPUT_TRANSFORM_180:
3995 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3996 weston_matrix_translate(&output->matrix,
3997 -output->width, -output->height, 0);
3998 weston_matrix_rotate_xy(&output->matrix, -1, 0);
3999 break;
4000 case WL_OUTPUT_TRANSFORM_270:
4001 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4002 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4003 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4004 break;
4005 }
4006
4007 if (output->current_scale != 1)
4008 weston_matrix_scale(&output->matrix,
4009 output->current_scale,
4010 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004011
Scott Moreauccbf29d2012-02-22 14:21:41 -07004012 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004013
4014 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004015}
4016
Scott Moreau1bad5db2012-08-18 01:04:05 -06004017static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004018weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004019{
4020 output->transform = transform;
4021
4022 switch (transform) {
4023 case WL_OUTPUT_TRANSFORM_90:
4024 case WL_OUTPUT_TRANSFORM_270:
4025 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4026 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4027 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02004028 output->width = output->current_mode->height;
4029 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004030 break;
4031 case WL_OUTPUT_TRANSFORM_NORMAL:
4032 case WL_OUTPUT_TRANSFORM_180:
4033 case WL_OUTPUT_TRANSFORM_FLIPPED:
4034 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02004035 output->width = output->current_mode->width;
4036 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004037 break;
4038 default:
4039 break;
4040 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06004041
Hardening57388e42013-09-18 23:56:36 +02004042 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02004043 output->width /= scale;
4044 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02004045}
4046
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004047static void
4048weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004049{
4050 output->x = x;
4051 output->y = y;
4052
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004053 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004054 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004055 output->width,
4056 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004057}
4058
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004059WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004060weston_output_move(struct weston_output *output, int x, int y)
4061{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004062 struct wl_resource *resource;
4063
4064 output->move_x = x - output->x;
4065 output->move_y = y - output->y;
4066
4067 if (output->move_x == 0 && output->move_y == 0)
4068 return;
4069
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004070 weston_output_init_geometry(output, x, y);
4071
4072 output->dirty = 1;
4073
4074 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004075 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004076
4077 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004078 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004079 wl_output_send_geometry(resource,
4080 output->x,
4081 output->y,
4082 output->mm_width,
4083 output->mm_height,
4084 output->subpixel,
4085 output->make,
4086 output->model,
4087 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004088
4089 if (wl_resource_get_version(resource) >= 2)
4090 wl_output_send_done(resource);
4091 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004092}
4093
4094WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004095weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004096 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004097 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004098{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004099 struct wl_event_loop *loop;
4100
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004101 output->compositor = c;
4102 output->x = x;
4103 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004104 output->mm_width = mm_width;
4105 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004106 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004107 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004108
Alexander Larsson0b135062013-05-28 16:23:36 +02004109 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004110 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004111
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004112 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004113 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004114
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004115 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004116 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004117 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004118 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004119 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02004120
Pekka Paalanen0513a952014-05-21 16:17:27 +03004121 loop = wl_display_get_event_loop(c->wl_display);
4122 output->repaint_timer = wl_event_loop_add_timer(loop,
4123 output_repaint_timer_handler, output);
4124
Casey Dahlin58ba1372012-04-19 22:50:08 -04004125 output->id = ffs(~output->compositor->output_id_pool) - 1;
4126 output->compositor->output_id_pool |= 1 << output->id;
4127
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004128 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004129 wl_global_create(c->wl_display, &wl_output_interface, 2,
4130 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004131}
4132
4133/** Adds an output to the compositor's output list and
4134 * send the compositor's output_created signal.
4135 *
4136 * \param compositor The compositor instance.
4137 * \param output The output to be added.
4138 */
4139WL_EXPORT void
4140weston_compositor_add_output(struct weston_compositor *compositor,
4141 struct weston_output *output)
4142{
4143 wl_list_insert(compositor->output_list.prev, &output->link);
4144 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004145}
4146
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004147WL_EXPORT void
4148weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05004149 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004150 wl_fixed_t *x, wl_fixed_t *y)
4151{
Derek Foreman0f679412014-10-02 13:41:17 -05004152 struct weston_vector p = { {
4153 wl_fixed_to_double(device_x),
4154 wl_fixed_to_double(device_y),
4155 0.0,
4156 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004157
Derek Foreman67a18b92015-03-24 11:36:14 -05004158 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004159
Derek Foreman0f679412014-10-02 13:41:17 -05004160 *x = wl_fixed_from_double(p.f[0] / p.f[3]);
4161 *y = wl_fixed_from_double(p.f[1] / p.f[3]);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004162}
4163
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004164static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004165destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004166{
Jonny Lamb74130762013-11-26 18:19:46 +01004167 struct weston_surface *surface =
4168 wl_resource_get_user_data(resource);
4169
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004170 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004171 surface->pending.buffer_viewport.buffer.src_width =
4172 wl_fixed_from_int(-1);
4173 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004174 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004175}
4176
4177static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004178viewport_destroy(struct wl_client *client,
4179 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004180{
4181 wl_resource_destroy(resource);
4182}
4183
4184static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004185viewport_set(struct wl_client *client,
4186 struct wl_resource *resource,
4187 wl_fixed_t src_x,
4188 wl_fixed_t src_y,
4189 wl_fixed_t src_width,
4190 wl_fixed_t src_height,
4191 int32_t dst_width,
4192 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004193{
Jonny Lamb74130762013-11-26 18:19:46 +01004194 struct weston_surface *surface =
4195 wl_resource_get_user_data(resource);
4196
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004197 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01004198
Jonny Lamb8ae35902013-11-26 18:19:45 +01004199 if (wl_fixed_to_double(src_width) < 0 ||
4200 wl_fixed_to_double(src_height) < 0) {
4201 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004202 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004203 "source dimensions must be non-negative (%fx%f)",
4204 wl_fixed_to_double(src_width),
4205 wl_fixed_to_double(src_height));
4206 return;
4207 }
4208
4209 if (dst_width <= 0 || dst_height <= 0) {
4210 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004211 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004212 "destination dimensions must be positive (%dx%d)",
4213 dst_width, dst_height);
4214 return;
4215 }
4216
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004217 surface->pending.buffer_viewport.buffer.src_x = src_x;
4218 surface->pending.buffer_viewport.buffer.src_y = src_y;
4219 surface->pending.buffer_viewport.buffer.src_width = src_width;
4220 surface->pending.buffer_viewport.buffer.src_height = src_height;
4221 surface->pending.buffer_viewport.surface.width = dst_width;
4222 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004223 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004224}
4225
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004226static void
4227viewport_set_source(struct wl_client *client,
4228 struct wl_resource *resource,
4229 wl_fixed_t src_x,
4230 wl_fixed_t src_y,
4231 wl_fixed_t src_width,
4232 wl_fixed_t src_height)
4233{
4234 struct weston_surface *surface =
4235 wl_resource_get_user_data(resource);
4236
4237 assert(surface->viewport_resource != NULL);
4238
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004239 if (src_width == wl_fixed_from_int(-1) &&
4240 src_height == wl_fixed_from_int(-1)) {
4241 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004242 surface->pending.buffer_viewport.buffer.src_width =
4243 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004244 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004245 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004246 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004247
4248 if (src_width <= 0 || src_height <= 0) {
4249 wl_resource_post_error(resource,
4250 WL_VIEWPORT_ERROR_BAD_VALUE,
4251 "source size must be positive (%fx%f)",
4252 wl_fixed_to_double(src_width),
4253 wl_fixed_to_double(src_height));
4254 return;
4255 }
4256
4257 surface->pending.buffer_viewport.buffer.src_x = src_x;
4258 surface->pending.buffer_viewport.buffer.src_y = src_y;
4259 surface->pending.buffer_viewport.buffer.src_width = src_width;
4260 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004261 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004262}
4263
4264static void
4265viewport_set_destination(struct wl_client *client,
4266 struct wl_resource *resource,
4267 int32_t dst_width,
4268 int32_t dst_height)
4269{
4270 struct weston_surface *surface =
4271 wl_resource_get_user_data(resource);
4272
4273 assert(surface->viewport_resource != NULL);
4274
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004275 if (dst_width == -1 && dst_height == -1) {
4276 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004277 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004278 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004279 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004280 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004281
4282 if (dst_width <= 0 || dst_height <= 0) {
4283 wl_resource_post_error(resource,
4284 WL_VIEWPORT_ERROR_BAD_VALUE,
4285 "destination size must be positive (%dx%d)",
4286 dst_width, dst_height);
4287 return;
4288 }
4289
4290 surface->pending.buffer_viewport.surface.width = dst_width;
4291 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004292 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004293}
4294
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004295static const struct wl_viewport_interface viewport_interface = {
4296 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004297 viewport_set,
4298 viewport_set_source,
4299 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004300};
4301
4302static void
4303scaler_destroy(struct wl_client *client,
4304 struct wl_resource *resource)
4305{
4306 wl_resource_destroy(resource);
4307}
4308
4309static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004310scaler_get_viewport(struct wl_client *client,
4311 struct wl_resource *scaler,
4312 uint32_t id,
4313 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004314{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004315 int version = wl_resource_get_version(scaler);
4316 struct weston_surface *surface =
4317 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004318 struct wl_resource *resource;
4319
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004320 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01004321 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004322 WL_SCALER_ERROR_VIEWPORT_EXISTS,
4323 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004324 return;
4325 }
4326
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004327 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004328 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004329 if (resource == NULL) {
4330 wl_client_post_no_memory(client);
4331 return;
4332 }
4333
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004334 wl_resource_set_implementation(resource, &viewport_interface,
4335 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004336
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004337 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004338}
4339
4340static const struct wl_scaler_interface scaler_interface = {
4341 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004342 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004343};
4344
4345static void
4346bind_scaler(struct wl_client *client,
4347 void *data, uint32_t version, uint32_t id)
4348{
4349 struct wl_resource *resource;
4350
4351 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004352 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004353 if (resource == NULL) {
4354 wl_client_post_no_memory(client);
4355 return;
4356 }
4357
4358 wl_resource_set_implementation(resource, &scaler_interface,
4359 NULL, NULL);
4360}
4361
4362static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004363destroy_presentation_feedback(struct wl_resource *feedback_resource)
4364{
4365 struct weston_presentation_feedback *feedback;
4366
4367 feedback = wl_resource_get_user_data(feedback_resource);
4368
4369 wl_list_remove(&feedback->link);
4370 free(feedback);
4371}
4372
4373static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004374presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4375{
4376 wl_resource_destroy(resource);
4377}
4378
4379static void
4380presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004381 struct wl_resource *presentation_resource,
4382 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004383 uint32_t callback)
4384{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004385 struct weston_surface *surface;
4386 struct weston_presentation_feedback *feedback;
4387
4388 surface = wl_resource_get_user_data(surface_resource);
4389
Bryce Harringtonde16d892014-11-20 22:21:57 -08004390 feedback = zalloc(sizeof *feedback);
4391 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004392 goto err_calloc;
4393
4394 feedback->resource = wl_resource_create(client,
4395 &presentation_feedback_interface,
4396 1, callback);
4397 if (!feedback->resource)
4398 goto err_create;
4399
4400 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4401 destroy_presentation_feedback);
4402
4403 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4404
4405 return;
4406
4407err_create:
4408 free(feedback);
4409
4410err_calloc:
4411 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004412}
4413
4414static const struct presentation_interface presentation_implementation = {
4415 presentation_destroy,
4416 presentation_feedback
4417};
4418
4419static void
4420bind_presentation(struct wl_client *client,
4421 void *data, uint32_t version, uint32_t id)
4422{
4423 struct weston_compositor *compositor = data;
4424 struct wl_resource *resource;
4425
4426 resource = wl_resource_create(client, &presentation_interface,
4427 MIN(version, 1), id);
4428 if (resource == NULL) {
4429 wl_client_post_no_memory(client);
4430 return;
4431 }
4432
4433 wl_resource_set_implementation(resource, &presentation_implementation,
4434 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004435 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004436}
4437
4438static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004439compositor_bind(struct wl_client *client,
4440 void *data, uint32_t version, uint32_t id)
4441{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004442 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004443 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004444
Jason Ekstranda85118c2013-06-27 20:17:02 -05004445 resource = wl_resource_create(client, &wl_compositor_interface,
4446 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004447 if (resource == NULL) {
4448 wl_client_post_no_memory(client);
4449 return;
4450 }
4451
4452 wl_resource_set_implementation(resource, &compositor_interface,
4453 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004454}
4455
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04004456static void
Martin Minarikf12c2872012-06-11 00:57:39 +02004457log_uname(void)
4458{
4459 struct utsname usys;
4460
4461 uname(&usys);
4462
4463 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
4464 usys.version, usys.machine);
4465}
4466
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004467WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004468weston_environment_get_fd(const char *env)
4469{
4470 char *e, *end;
4471 int fd, flags;
4472
4473 e = getenv(env);
4474 if (!e)
4475 return -1;
4476 fd = strtol(e, &end, 0);
4477 if (*end != '\0')
4478 return -1;
4479
4480 flags = fcntl(fd, F_GETFD);
4481 if (flags == -1)
4482 return -1;
4483
4484 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4485 unsetenv(env);
4486
4487 return fd;
4488}
4489
Pekka Paalanenb5026542014-11-12 15:09:24 +02004490static void
4491timeline_key_binding_handler(struct weston_seat *seat, uint32_t time,
4492 uint32_t key, void *data)
4493{
4494 struct weston_compositor *compositor = data;
4495
4496 if (weston_timeline_enabled_)
4497 weston_timeline_close();
4498 else
4499 weston_timeline_open(compositor);
4500}
4501
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004502WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04004503weston_compositor_init(struct weston_compositor *ec,
4504 struct wl_display *display,
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øgsbergf9212892008-10-11 18:40:23 -04004513 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004514 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004515 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004516 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004517 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004518 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004519 wl_signal_init(&ec->idle_signal);
4520 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004521 wl_signal_init(&ec->show_input_panel_signal);
4522 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004523 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004524 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004525 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004526 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004527 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004528 wl_signal_init(&ec->session_signal);
4529 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004530
Casey Dahlin58ba1372012-04-19 22:50:08 -04004531 ec->output_id_pool = 0;
4532
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004533 if (!wl_global_create(display, &wl_compositor_interface, 3,
4534 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05004535 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004536
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004537 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
4538 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03004539 return -1;
4540
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004541 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004542 ec, bind_scaler))
4543 return -1;
4544
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004545 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4546 ec, bind_presentation))
4547 return -1;
4548
Jason Ekstranda7af7042013-10-12 22:38:11 -05004549 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004550 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004551 wl_list_init(&ec->layer_list);
4552 wl_list_init(&ec->seat_list);
4553 wl_list_init(&ec->output_list);
4554 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004555 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004556 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004557 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004558 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004559 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004560
Xiong Zhang97116532013-10-23 13:58:31 +08004561 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004562 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004563
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004564 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
4565 weston_config_section_get_string(s, "keymap_rules",
4566 (char **) &xkb_names.rules, NULL);
4567 weston_config_section_get_string(s, "keymap_model",
4568 (char **) &xkb_names.model, NULL);
4569 weston_config_section_get_string(s, "keymap_layout",
4570 (char **) &xkb_names.layout, NULL);
4571 weston_config_section_get_string(s, "keymap_variant",
4572 (char **) &xkb_names.variant, NULL);
4573 weston_config_section_get_string(s, "keymap_options",
4574 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01004575
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05004576 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
4577 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01004578
Jonny Lamb66a41a02014-08-12 14:58:25 +02004579 weston_config_section_get_int(s, "repeat-rate",
4580 &ec->kb_repeat_rate, 40);
4581 weston_config_section_get_int(s, "repeat-delay",
4582 &ec->kb_repeat_delay, 400);
4583
Daniel Stone725c2c32012-06-22 14:04:36 +01004584 wl_data_device_manager_init(ec->wl_display);
4585
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04004586 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004587
Daniel Stone725c2c32012-06-22 14:04:36 +01004588 loop = wl_display_get_event_loop(ec->wl_display);
4589 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4590 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4591
4592 ec->input_loop = wl_event_loop_create();
4593
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004594 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4595 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4596
Pekka Paalanenb5026542014-11-12 15:09:24 +02004597 weston_compositor_add_debug_binding(ec, KEY_T,
4598 timeline_key_binding_handler, ec);
4599
Pekka Paalanen0513a952014-05-21 16:17:27 +03004600 s = weston_config_get_section(ec->config, "core", NULL, NULL);
4601 weston_config_section_get_int(s, "repaint-window", &ec->repaint_msec,
4602 DEFAULT_REPAINT_WINDOW);
4603 if (ec->repaint_msec < -10 || ec->repaint_msec > 1000) {
4604 weston_log("Invalid repaint_window value in config: %d\n",
4605 ec->repaint_msec);
4606 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
4607 }
4608 weston_log("Output repaint window is %d ms maximum.\n",
4609 ec->repaint_msec);
4610
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004611 weston_compositor_schedule_repaint(ec);
4612
Daniel Stone725c2c32012-06-22 14:04:36 +01004613 return 0;
4614}
4615
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004616WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004617weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004618{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004619 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004620
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004621 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004622 if (ec->input_loop_source)
4623 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004624
Matt Roper361d2ad2011-08-29 13:52:23 -07004625 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004626 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004627 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004628
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004629 if (ec->renderer)
4630 ec->renderer->destroy(ec);
4631
Daniel Stone325fc2d2012-05-30 16:31:58 +01004632 weston_binding_list_destroy_all(&ec->key_binding_list);
4633 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004634 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004635 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004636 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004637
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004638 weston_plane_release(&ec->primary_plane);
4639
Jonas Ådahlc97af922012-03-28 22:36:09 +02004640 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07004641
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004642 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07004643}
4644
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004645WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004646weston_compositor_exit_with_code(struct weston_compositor *compositor,
4647 int exit_code)
4648{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004649 if (compositor->exit_code == EXIT_SUCCESS)
4650 compositor->exit_code = exit_code;
4651
Frederic Plourdec336f062014-10-29 14:44:33 -04004652 wl_display_terminate(compositor->wl_display);
4653}
4654
4655WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004656weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4657 const struct weston_pointer_grab_interface *interface)
4658{
4659 struct weston_seat *seat;
4660
4661 ec->default_pointer_grab = interface;
4662 wl_list_for_each(seat, &ec->seat_list, link) {
4663 if (seat->pointer) {
4664 weston_pointer_set_default_grab(seat->pointer,
4665 interface);
4666 }
4667 }
4668}
4669
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004670WL_EXPORT int
4671weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4672 clockid_t clk_id)
4673{
4674 struct timespec ts;
4675
4676 if (clock_gettime(clk_id, &ts) < 0)
4677 return -1;
4678
4679 compositor->presentation_clock = clk_id;
4680
4681 return 0;
4682}
4683
4684/*
4685 * For choosing the software clock, when the display hardware or API
4686 * does not expose a compatible presentation timestamp.
4687 */
4688WL_EXPORT int
4689weston_compositor_set_presentation_clock_software(
4690 struct weston_compositor *compositor)
4691{
4692 /* In order of preference */
4693 static const clockid_t clocks[] = {
4694 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4695 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4696 CLOCK_MONOTONIC, /* no jumps, may crawl */
4697 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4698 CLOCK_REALTIME /* may jump and crawl */
4699 };
4700 unsigned i;
4701
4702 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4703 if (weston_compositor_set_presentation_clock(compositor,
4704 clocks[i]) == 0)
4705 return 0;
4706
4707 weston_log("Error: no suitable presentation clock available.\n");
4708
4709 return -1;
4710}
4711
Pekka Paalanen662f3842015-03-18 12:17:26 +02004712/** Read the current time from the Presentation clock
4713 *
4714 * \param compositor
4715 * \param ts[out] The current time.
4716 *
4717 * \note Reading the current time in user space is always imprecise to some
4718 * degree.
4719 *
4720 * This function is never meant to fail. If reading the clock does fail,
4721 * an error message is logged and a zero time is returned. Callers are not
4722 * supposed to detect or react to failures.
4723 */
4724WL_EXPORT void
4725weston_compositor_read_presentation_clock(
4726 const struct weston_compositor *compositor,
4727 struct timespec *ts)
4728{
4729 static bool warned;
4730 int ret;
4731
4732 ret = clock_gettime(compositor->presentation_clock, ts);
4733 if (ret < 0) {
4734 ts->tv_sec = 0;
4735 ts->tv_nsec = 0;
4736
4737 if (!warned)
4738 weston_log("Error: failure to read "
4739 "the presentation clock %#x: '%m' (%d)\n",
4740 compositor->presentation_clock, errno);
4741 warned = true;
4742 }
4743}
4744
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004745WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004746weston_version(int *major, int *minor, int *micro)
4747{
4748 *major = WESTON_VERSION_MAJOR;
4749 *minor = WESTON_VERSION_MINOR;
4750 *micro = WESTON_VERSION_MICRO;
4751}
4752
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004753static const char *
4754clock_name(clockid_t clk_id)
4755{
4756 static const char *names[] = {
4757 [CLOCK_REALTIME] = "CLOCK_REALTIME",
4758 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
4759 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
4760 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
4761 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
4762 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
4763 };
4764
4765 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
4766 return "unknown";
4767
4768 return names[clk_id];
4769}
4770
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004771static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004772 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004773 const char *desc;
4774} capability_strings[] = {
4775 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004776 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004777};
4778
4779static void
4780weston_compositor_log_capabilities(struct weston_compositor *compositor)
4781{
4782 unsigned i;
4783 int yes;
4784
4785 weston_log("Compositor capabilities:\n");
4786 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
4787 yes = compositor->capabilities & capability_strings[i].bit;
4788 weston_log_continue(STAMP_SPACE "%s %s\n",
4789 capability_strings[i].desc,
4790 yes ? "yes" : "no");
4791 }
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004792
4793 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
4794 clock_name(compositor->presentation_clock),
4795 compositor->presentation_clock);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004796}
4797
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004798static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004799{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004800 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004801
Martin Minarik6d118362012-06-07 18:01:59 +02004802 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004803 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004804
4805 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004806}
4807
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004808#ifdef HAVE_LIBUNWIND
4809
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004810static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004811print_backtrace(void)
4812{
4813 unw_cursor_t cursor;
4814 unw_context_t context;
4815 unw_word_t off;
4816 unw_proc_info_t pip;
4817 int ret, i = 0;
4818 char procname[256];
4819 const char *filename;
4820 Dl_info dlinfo;
4821
4822 pip.unwind_info = NULL;
4823 ret = unw_getcontext(&context);
4824 if (ret) {
4825 weston_log("unw_getcontext: %d\n", ret);
4826 return;
4827 }
4828
4829 ret = unw_init_local(&cursor, &context);
4830 if (ret) {
4831 weston_log("unw_init_local: %d\n", ret);
4832 return;
4833 }
4834
4835 ret = unw_step(&cursor);
4836 while (ret > 0) {
4837 ret = unw_get_proc_info(&cursor, &pip);
4838 if (ret) {
4839 weston_log("unw_get_proc_info: %d\n", ret);
4840 break;
4841 }
4842
4843 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4844 if (ret && ret != -UNW_ENOMEM) {
4845 if (ret != -UNW_EUNSPEC)
4846 weston_log("unw_get_proc_name: %d\n", ret);
4847 procname[0] = '?';
4848 procname[1] = 0;
4849 }
4850
4851 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4852 *dlinfo.dli_fname)
4853 filename = dlinfo.dli_fname;
4854 else
4855 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004856
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004857 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4858 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4859
4860 ret = unw_step(&cursor);
4861 if (ret < 0)
4862 weston_log("unw_step: %d\n", ret);
4863 }
4864}
4865
4866#else
4867
4868static void
4869print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004870{
4871 void *buffer[32];
4872 int i, count;
4873 Dl_info info;
4874
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004875 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4876 for (i = 0; i < count; i++) {
4877 dladdr(buffer[i], &info);
4878 weston_log(" [%016lx] %s (%s)\n",
4879 (long) buffer[i],
4880 info.dli_sname ? info.dli_sname : "--",
4881 info.dli_fname);
4882 }
4883}
4884
4885#endif
4886
4887static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004888on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004889{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004890 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004891 * then call the backend restore function, which will switch
4892 * back to the vt we launched from or ungrab X etc and then
4893 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004894 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004895 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004896 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004897
Peter Maatmane5b42e42013-03-27 22:38:53 +01004898 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004899
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004900 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004901
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004902 segv_compositor->restore(segv_compositor);
4903
4904 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004905}
4906
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004907WL_EXPORT void *
4908weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004909{
4910 char path[PATH_MAX];
4911 void *module, *init;
4912
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004913 if (name == NULL)
4914 return NULL;
4915
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004916 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004917 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004918 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004919 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004920
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004921 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4922 if (module) {
4923 weston_log("Module '%s' already loaded\n", path);
4924 dlclose(module);
4925 return NULL;
4926 }
4927
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004928 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004929 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004930 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004931 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004932 return NULL;
4933 }
4934
4935 init = dlsym(module, entrypoint);
4936 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004937 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004938 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004939 return NULL;
4940 }
4941
4942 return init;
4943}
4944
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004945static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004946load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004947 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004948{
4949 const char *p, *end;
4950 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004951 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004952 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004953
4954 if (modules == NULL)
4955 return 0;
4956
4957 p = modules;
4958 while (*p) {
4959 end = strchrnul(p, ',');
4960 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004961 module_init = weston_load_module(buffer, "module_init");
Ondřej Majerech01e98b62014-12-06 02:49:17 +01004962 if (!module_init)
4963 return -1;
4964 if (module_init(ec, argc, argv) < 0)
4965 return -1;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004966 p = end;
4967 while (*p == ',')
4968 p++;
4969
4970 }
4971
4972 return 0;
4973}
4974
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004975static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004976 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4977
4978static const char xdg_wrong_message[] =
4979 "fatal: environment variable XDG_RUNTIME_DIR\n"
4980 "is set to \"%s\", which is not a directory.\n";
4981
4982static const char xdg_wrong_mode_message[] =
4983 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004984 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4985 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004986
4987static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004988 "Refer to your distribution on how to get it, or\n"
4989 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4990 "on how to implement it.\n";
4991
Martin Minarik37032f82012-06-18 20:15:18 +02004992static void
4993verify_xdg_runtime_dir(void)
4994{
4995 char *dir = getenv("XDG_RUNTIME_DIR");
4996 struct stat s;
4997
4998 if (!dir) {
4999 weston_log(xdg_error_message);
5000 weston_log_continue(xdg_detail_message);
5001 exit(EXIT_FAILURE);
5002 }
5003
5004 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
5005 weston_log(xdg_wrong_message, dir);
5006 weston_log_continue(xdg_detail_message);
5007 exit(EXIT_FAILURE);
5008 }
5009
5010 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
5011 weston_log(xdg_wrong_mode_message,
5012 dir, s.st_mode & 0777, s.st_uid);
5013 weston_log_continue(xdg_detail_message);
5014 }
5015}
5016
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005017static int
5018usage(int error_code)
5019{
5020 fprintf(stderr,
5021 "Usage: weston [OPTIONS]\n\n"
5022 "This is weston version " VERSION ", the Wayland reference compositor.\n"
5023 "Weston supports multiple backends, and depending on which backend is in use\n"
5024 "different options will be accepted.\n\n"
5025
5026
5027 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06005028 " --version\t\tPrint weston version\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07005029 " -B, --backend=MODULE\tBackend module, one of\n"
5030#if defined(BUILD_DRM_COMPOSITOR)
5031 "\t\t\t\tdrm-backend.so\n"
5032#endif
5033#if defined(BUILD_FBDEV_COMPOSITOR)
Pekka Paalanen86b70e12014-11-11 14:10:46 +02005034 "\t\t\t\tfbdev-backend.so\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07005035#endif
5036#if defined(BUILD_X11_COMPOSITOR)
5037 "\t\t\t\tx11-backend.so\n"
5038#endif
5039#if defined(BUILD_WAYLAND_COMPOSITOR)
5040 "\t\t\t\twayland-backend.so\n"
5041#endif
5042#if defined(BUILD_RDP_COMPOSITOR)
5043 "\t\t\t\trdp-backend.so\n"
5044#endif
5045#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
5046 "\t\t\t\trpi-backend.so\n"
5047#endif
Jason Ekstranda985da42013-08-22 17:28:03 -05005048 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005049 " -S, --socket=NAME\tName of socket to listen on\n"
5050 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005051 " --modules\t\tLoad the comma-separated list of modules\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07005052 " --log=FILE\t\tLog to the given file\n"
Bryce Harrington06c42742015-04-02 19:16:50 -07005053 " -c, --config=FILE\tConfig file to load, defaults to weston.ini\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03005054 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005055 " -h, --help\t\tThis help message\n\n");
5056
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005057#if defined(BUILD_DRM_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005058 fprintf(stderr,
5059 "Options for drm-backend.so:\n\n"
5060 " --connector=ID\tBring up only this connector\n"
5061 " --seat=SEAT\t\tThe seat that weston should run on\n"
5062 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02005063 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005064 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005065#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005066
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005067#if defined(BUILD_FBDEV_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005068 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01005069 "Options for fbdev-backend.so:\n\n"
5070 " --tty=TTY\t\tThe tty to use\n"
5071 " --device=DEVICE\tThe framebuffer device to use\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005072#endif
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01005073
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005074#if defined(BUILD_X11_COMPOSITOR)
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01005075 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005076 "Options for x11-backend.so:\n\n"
5077 " --width=WIDTH\t\tWidth of X window\n"
5078 " --height=HEIGHT\tHeight of X window\n"
5079 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05005080 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005081 " --output-count=COUNT\tCreate multiple outputs\n"
5082 " --no-input\t\tDont create input devices\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005083#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005084
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005085#if defined(BUILD_WAYLAND_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005086 fprintf(stderr,
5087 "Options for wayland-backend.so:\n\n"
5088 " --width=WIDTH\t\tWidth of Wayland surface\n"
5089 " --height=HEIGHT\tHeight of Wayland surface\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07005090 " --scale=SCALE\t\tScale factor of output\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06005091 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05005092 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05005093 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05005094 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005095 " --display=DISPLAY\tWayland display to connect to\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005096#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005097
Pekka Paalanene31e0532013-05-22 18:03:07 +03005098#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
5099 fprintf(stderr,
5100 "Options for rpi-backend.so:\n\n"
5101 " --tty=TTY\t\tThe tty to use\n"
5102 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
5103 " --transform=TR\tThe output transformation, TR is one of:\n"
5104 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01005105 " --opaque-regions\tEnable support for opaque regions, can be "
5106 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03005107 "\n");
5108#endif
5109
Hardeningc077a842013-07-08 00:51:35 +02005110#if defined(BUILD_RDP_COMPOSITOR)
Bryce Harrington59fe4232014-10-23 14:44:34 -07005111 fprintf(stderr,
5112 "Options for rdp-backend.so:\n\n"
5113 " --width=WIDTH\t\tWidth of desktop\n"
5114 " --height=HEIGHT\tHeight of desktop\n"
5115 " --env-socket=SOCKET\tUse that socket as peer connection\n"
5116 " --address=ADDR\tThe address to bind\n"
Bryce Harringtonf5b34ae2014-10-23 14:44:35 -07005117 " --port=PORT\t\tThe port to listen on\n"
Bryce Harrington59fe4232014-10-23 14:44:34 -07005118 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
5119 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
5120 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
5121 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
5122 "\n");
Hardeningc077a842013-07-08 00:51:35 +02005123#endif
5124
Bryce Harrington3e3b59e2014-11-19 15:06:19 -08005125#if defined(BUILD_HEADLESS_COMPOSITOR)
5126 fprintf(stderr,
5127 "Options for headless-backend.so:\n\n"
5128 " --width=WIDTH\t\tWidth of memory surface\n"
5129 " --height=HEIGHT\tHeight of memory surface\n"
5130 " --transform=TR\tThe output transformation, TR is one of:\n"
5131 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
5132 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
5133#endif
5134
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005135 exit(error_code);
5136}
5137
Peter Maatmane5b42e42013-03-27 22:38:53 +01005138static void
5139catch_signals(void)
5140{
5141 struct sigaction action;
5142
5143 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
5144 action.sa_sigaction = on_caught_signal;
5145 sigemptyset(&action.sa_mask);
5146 sigaction(SIGSEGV, &action, NULL);
5147 sigaction(SIGABRT, &action, NULL);
5148}
5149
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005150static void
5151handle_primary_client_destroyed(struct wl_listener *listener, void *data)
5152{
5153 struct wl_client *client = data;
5154
5155 weston_log("Primary client died. Closing...\n");
5156
5157 wl_display_terminate(wl_client_get_display(client));
5158}
5159
Ryo Munakatad8deff62014-09-06 07:32:05 +09005160static char *
5161weston_choose_default_backend(void)
5162{
5163 char *backend = NULL;
5164
5165 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
5166 backend = strdup("wayland-backend.so");
5167 else if (getenv("DISPLAY"))
5168 backend = strdup("x11-backend.so");
5169 else
5170 backend = strdup(WESTON_NATIVE_BACKEND);
5171
5172 return backend;
5173}
5174
5175static int
5176weston_create_listening_socket(struct wl_display *display, const char *socket_name)
5177{
5178 if (socket_name) {
5179 if (wl_display_add_socket(display, socket_name)) {
5180 weston_log("fatal: failed to add socket: %m\n");
5181 return -1;
5182 }
5183 } else {
5184 socket_name = wl_display_add_socket_auto(display);
5185 if (!socket_name) {
5186 weston_log("fatal: failed to add socket: %m\n");
5187 return -1;
5188 }
5189 }
5190
5191 setenv("WAYLAND_DISPLAY", socket_name, 1);
5192
5193 return 0;
5194}
5195
Derek Foreman64a3df02014-10-23 12:24:18 -05005196static const struct { const char *name; uint32_t token; } transforms[] = {
5197 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
5198 { "90", WL_OUTPUT_TRANSFORM_90 },
5199 { "180", WL_OUTPUT_TRANSFORM_180 },
5200 { "270", WL_OUTPUT_TRANSFORM_270 },
5201 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
5202 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
5203 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
5204 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
5205};
5206
5207WL_EXPORT int
5208weston_parse_transform(const char *transform, uint32_t *out)
5209{
5210 unsigned int i;
5211
5212 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
5213 if (strcmp(transforms[i].name, transform) == 0) {
5214 *out = transforms[i].token;
5215 return 0;
5216 }
5217
5218 *out = WL_OUTPUT_TRANSFORM_NORMAL;
5219 return -1;
5220}
5221
5222WL_EXPORT const char *
5223weston_transform_to_string(uint32_t output_transform)
5224{
5225 unsigned int i;
5226
5227 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
5228 if (transforms[i].token == output_transform)
5229 return transforms[i].name;
5230
5231 return "<illegal value>";
5232}
5233
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005234static int
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005235load_configuration(struct weston_config **config, int32_t noconfig,
5236 const char *config_file)
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005237{
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005238 const char *file = "weston.ini";
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02005239 const char *full_path;
5240
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005241 *config = NULL;
5242
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005243 if (config_file)
5244 file = config_file;
5245
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005246 if (noconfig == 0)
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005247 *config = weston_config_parse(file);
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005248
5249 if (*config) {
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02005250 full_path = weston_config_get_full_path(*config);
5251
5252 weston_log("Using config file '%s'\n", full_path);
5253 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005254
5255 return 0;
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005256 }
5257
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005258 if (config_file && noconfig == 0) {
5259 weston_log("fatal: error opening or reading config file"
5260 " '%s'.\n", config_file);
5261
5262 return -1;
5263 }
5264
5265 weston_log("Starting with no config file.\n");
5266 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
5267
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005268 return 0;
5269}
Derek Foreman64a3df02014-10-23 12:24:18 -05005270
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005271int main(int argc, char *argv[])
5272{
Pekka Paalanen111c6f92015-03-20 14:35:58 +02005273 int ret = EXIT_FAILURE;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005274 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005275 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005276 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05005277 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005278 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005279 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005280 int *argc, char *argv[],
5281 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005282 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005283 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05005284 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02005285 char *modules = NULL;
5286 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02005287 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005288 char *server_socket = NULL, *end;
Frederic Plourde4a84c832014-10-30 15:06:34 -04005289 int32_t idle_time = -1;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005290 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09005291 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06005292 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03005293 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005294 int32_t numlock_on;
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005295 char *config_file = NULL;
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005296 struct weston_config *config;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005297 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005298 struct wl_client *primary_client;
5299 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005300 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04005301
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005302 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09005303 { WESTON_OPTION_STRING, "backend", 'B', &backend },
5304 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005305 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
5306 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005307 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02005308 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005309 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06005310 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03005311 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005312 { WESTON_OPTION_STRING, "config", 'c', &config_file },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04005313 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05005314
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005315 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005316
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005317 if (help)
5318 usage(EXIT_SUCCESS);
5319
Scott Moreau12245142012-08-29 15:15:58 -06005320 if (version) {
5321 printf(PACKAGE_STRING "\n");
5322 return EXIT_SUCCESS;
5323 }
5324
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02005325 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005326
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03005327 weston_log("%s\n"
5328 STAMP_SPACE "%s\n"
5329 STAMP_SPACE "Bug reports to: %s\n"
5330 STAMP_SPACE "Build: %s\n",
5331 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04005332 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03005333 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04005334
Martin Minarik37032f82012-06-18 20:15:18 +02005335 verify_xdg_runtime_dir();
5336
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005337 display = wl_display_create();
5338
Tiago Vignatti2116b892011-08-08 05:52:59 -07005339 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005340 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
5341 display);
5342 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
5343 display);
5344 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
5345 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07005346
5347 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005348 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
5349 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05005350
Pekka Paalanen111c6f92015-03-20 14:35:58 +02005351 if (!signals[0] || !signals[1] || !signals[2] || !signals[3])
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305352 goto out_signals;
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305353
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005354 if (load_configuration(&config, noconfig, config_file) < 0)
Pekka Paalanendc940ca2015-03-24 15:56:18 +02005355 goto out_signals;
5356
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01005357 section = weston_config_get_section(config, "core", NULL, NULL);
5358
Ryo Munakata03faed22014-09-06 07:32:51 +09005359 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005360 weston_config_section_get_string(section, "backend", &backend,
5361 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09005362 if (!backend)
5363 backend = weston_choose_default_backend();
5364 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005365
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03005366 backend_init = weston_load_module(backend, "backend_init");
Pekka Paalanen111c6f92015-03-20 14:35:58 +02005367 if (!backend_init)
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305368 goto out_signals;
Kristian Høgsberga9410222011-01-14 17:22:35 -05005369
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005370 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05005371 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03005372 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305373 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05005374 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04005375
Peter Maatmane5b42e42013-03-27 22:38:53 +01005376 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04005377 segv_compositor = ec;
5378
Frederic Plourde4a84c832014-10-30 15:06:34 -04005379 if (idle_time < 0)
5380 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
5381 if (idle_time < 0)
5382 idle_time = 300; /* default idle timeout, in seconds */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005383 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005384 ec->default_pointer_grab = NULL;
Frederic Plourdec336f062014-10-29 14:44:33 -04005385 ec->exit_code = EXIT_SUCCESS;
Pekka Paalanen7296e792011-12-07 16:22:00 +02005386
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005387 weston_compositor_log_capabilities(ec);
5388
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005389 server_socket = getenv("WAYLAND_SERVER_SOCKET");
5390 if (server_socket) {
5391 weston_log("Running with single client\n");
5392 fd = strtol(server_socket, &end, 0);
5393 if (*end != '\0')
5394 fd = -1;
5395 } else {
5396 fd = -1;
5397 }
5398
5399 if (fd != -1) {
5400 primary_client = wl_client_create(display, fd);
5401 if (!primary_client) {
5402 weston_log("fatal: failed to add client: %m\n");
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005403 goto out;
5404 }
5405 primary_client_destroyed.notify =
5406 handle_primary_client_destroyed;
5407 wl_client_add_destroy_listener(primary_client,
5408 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09005409 } else if (weston_create_listening_socket(display, socket_name)) {
Ryo Munakatad8deff62014-09-06 07:32:05 +09005410 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005411 }
5412
Ryo Munakata03faed22014-09-06 07:32:51 +09005413 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005414 weston_config_section_get_string(section, "shell", &shell,
5415 "desktop-shell.so");
5416
Ryo Munakata03faed22014-09-06 07:32:51 +09005417 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005418 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005419
5420 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09005421 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005422 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005423
5424 if (load_modules(ec, option_modules, &argc, argv) < 0)
5425 goto out;
5426
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005427 section = weston_config_get_section(config, "keyboard", NULL, NULL);
5428 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
5429 if (numlock_on) {
5430 wl_list_for_each(seat, &ec->seat_list, link) {
5431 if (seat->keyboard)
5432 weston_keyboard_set_locks(seat->keyboard,
5433 WESTON_NUM_LOCK,
5434 WESTON_NUM_LOCK);
5435 }
5436 }
5437
Pekka Paalanen17a1a962015-03-24 15:56:15 +02005438 for (i = 1; i < argc; i++)
5439 weston_log("fatal: unhandled option: %s\n", argv[i]);
5440 if (argc > 1)
5441 goto out;
5442
Pekka Paalanenc0444e32012-01-05 16:28:21 +02005443 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005444
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04005445 wl_display_run(display);
5446
Frederic Plourdec336f062014-10-29 14:44:33 -04005447 /* Allow for setting return exit code after
5448 * wl_display_run returns normally. This is
5449 * useful for devs/testers and automated tests
5450 * that want to indicate failure status to
5451 * testing infrastructure above
5452 */
5453 ret = ec->exit_code;
5454
Ryo Munakata03faed22014-09-06 07:32:51 +09005455out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005456 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005457 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005458
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005459 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005460
Daniel Stone855028f2012-05-01 20:37:10 +01005461 weston_compositor_xkb_destroy(ec);
5462
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005463 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305464
5465out_signals:
5466 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
5467 if (signals[i])
5468 wl_event_source_remove(signals[i]);
5469
Tiago Vignatti9e2be082011-12-19 00:04:46 +02005470 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005471
Martin Minarik19e6f262012-06-07 13:08:46 +02005472 weston_log_file_close();
5473
Pekka Paalanen8a0e0ba2015-03-24 15:56:20 +02005474 free(config_file);
Ryo Munakata03faed22014-09-06 07:32:51 +09005475 free(backend);
5476 free(shell);
5477 free(socket_name);
5478 free(option_modules);
5479 free(log);
5480 free(modules);
5481
Pekka Paalanen3ab72692012-06-08 17:27:28 +03005482 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04005483}